decContext.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /* ------------------------------------------------------------------ */
  4. /* Decimal Context module header */
  5. /* ------------------------------------------------------------------ */
  6. /* Copyright (c) IBM Corporation, 2000-2011. All rights reserved. */
  7. /* */
  8. /* This software is made available under the terms of the */
  9. /* ICU License -- ICU 1.8.1 and later. */
  10. /* */
  11. /* The description and User's Guide ("The decNumber C Library") for */
  12. /* this software is called decNumber.pdf. This document is */
  13. /* available, together with arithmetic and format specifications, */
  14. /* testcases, and Web links, on the General Decimal Arithmetic page. */
  15. /* */
  16. /* Please send comments, suggestions, and corrections to the author: */
  17. /* mfc@uk.ibm.com */
  18. /* Mike Cowlishaw, IBM Fellow */
  19. /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
  20. /* ------------------------------------------------------------------ */
  21. /* Modified version, for use from within ICU.
  22. * Renamed public functions, to avoid an unwanted export of the
  23. * standard names from the ICU library.
  24. *
  25. * Use ICU's uprv_malloc() and uprv_free()
  26. *
  27. * Revert comment syntax to plain C
  28. *
  29. * Remove a few compiler warnings.
  30. */
  31. #include "unicode/utypes.h"
  32. #include "putilimp.h"
  33. /* */
  34. /* Context variables must always have valid values: */
  35. /* */
  36. /* status -- [any bits may be cleared, but not set, by user] */
  37. /* round -- must be one of the enumerated rounding modes */
  38. /* */
  39. /* The following variables are implied for fixed size formats (i.e., */
  40. /* they are ignored) but should still be set correctly in case used */
  41. /* with decNumber functions: */
  42. /* */
  43. /* clamp -- must be either 0 or 1 */
  44. /* digits -- must be in the range 1 through 999999999 */
  45. /* emax -- must be in the range 0 through 999999999 */
  46. /* emin -- must be in the range 0 through -999999999 */
  47. /* extended -- must be either 0 or 1 [present only if DECSUBSET] */
  48. /* traps -- only defined bits may be set */
  49. /* */
  50. /* ------------------------------------------------------------------ */
  51. #if !defined(DECCONTEXT)
  52. #define DECCONTEXT
  53. #define DECCNAME "decContext" /* Short name */
  54. #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */
  55. #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */
  56. #if !defined(int32_t)
  57. /* #include <stdint.h> */ /* C99 standard integers */
  58. #endif
  59. #include <stdio.h> /* for printf, etc. */
  60. #include <signal.h> /* for traps */
  61. /* Extended flags setting -- set this to 0 to use only IEEE flags */
  62. #if !defined(DECEXTFLAG)
  63. #define DECEXTFLAG 1 /* 1=enable extended flags */
  64. #endif
  65. /* Conditional code flag -- set this to 0 for best performance */
  66. #if !defined(DECSUBSET)
  67. #define DECSUBSET 0 /* 1=enable subset arithmetic */
  68. #endif
  69. /* Context for operations, with associated constants */
  70. enum rounding {
  71. DEC_ROUND_CEILING, /* round towards +infinity */
  72. DEC_ROUND_UP, /* round away from 0 */
  73. DEC_ROUND_HALF_UP, /* 0.5 rounds up */
  74. DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */
  75. DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */
  76. DEC_ROUND_DOWN, /* round towards 0 (truncate) */
  77. DEC_ROUND_FLOOR, /* round towards -infinity */
  78. DEC_ROUND_05UP, /* round for reround */
  79. DEC_ROUND_MAX /* enum must be less than this */
  80. };
  81. #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
  82. typedef struct {
  83. int32_t digits; /* working precision */
  84. int32_t emax; /* maximum positive exponent */
  85. int32_t emin; /* minimum negative exponent */
  86. enum rounding round; /* rounding mode */
  87. uint32_t traps; /* trap-enabler flags */
  88. uint32_t status; /* status flags */
  89. uint8_t clamp; /* flag: apply IEEE exponent clamp */
  90. #if DECSUBSET
  91. uint8_t extended; /* flag: special-values allowed */
  92. #endif
  93. } decContext;
  94. /* Maxima and Minima for context settings */
  95. #define DEC_MAX_DIGITS 999999999
  96. #define DEC_MIN_DIGITS 1
  97. #define DEC_MAX_EMAX 999999999
  98. #define DEC_MIN_EMAX 0
  99. #define DEC_MAX_EMIN 0
  100. #define DEC_MIN_EMIN -999999999
  101. #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */
  102. /* Classifications for decimal numbers, aligned with 754 (note that */
  103. /* 'normal' and 'subnormal' are meaningful only with a decContext */
  104. /* or a fixed size format). */
  105. enum decClass {
  106. DEC_CLASS_SNAN,
  107. DEC_CLASS_QNAN,
  108. DEC_CLASS_NEG_INF,
  109. DEC_CLASS_NEG_NORMAL,
  110. DEC_CLASS_NEG_SUBNORMAL,
  111. DEC_CLASS_NEG_ZERO,
  112. DEC_CLASS_POS_ZERO,
  113. DEC_CLASS_POS_SUBNORMAL,
  114. DEC_CLASS_POS_NORMAL,
  115. DEC_CLASS_POS_INF
  116. };
  117. /* Strings for the decClasses */
  118. #define DEC_ClassString_SN "sNaN"
  119. #define DEC_ClassString_QN "NaN"
  120. #define DEC_ClassString_NI "-Infinity"
  121. #define DEC_ClassString_NN "-Normal"
  122. #define DEC_ClassString_NS "-Subnormal"
  123. #define DEC_ClassString_NZ "-Zero"
  124. #define DEC_ClassString_PZ "+Zero"
  125. #define DEC_ClassString_PS "+Subnormal"
  126. #define DEC_ClassString_PN "+Normal"
  127. #define DEC_ClassString_PI "+Infinity"
  128. #define DEC_ClassString_UN "Invalid"
  129. /* Trap-enabler and Status flags (exceptional conditions), and */
  130. /* their names. The top byte is reserved for internal use */
  131. #if DECEXTFLAG
  132. /* Extended flags */
  133. #define DEC_Conversion_syntax 0x00000001
  134. #define DEC_Division_by_zero 0x00000002
  135. #define DEC_Division_impossible 0x00000004
  136. #define DEC_Division_undefined 0x00000008
  137. #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */
  138. #define DEC_Inexact 0x00000020
  139. #define DEC_Invalid_context 0x00000040
  140. #define DEC_Invalid_operation 0x00000080
  141. #if DECSUBSET
  142. #define DEC_Lost_digits 0x00000100
  143. #endif
  144. #define DEC_Overflow 0x00000200
  145. #define DEC_Clamped 0x00000400
  146. #define DEC_Rounded 0x00000800
  147. #define DEC_Subnormal 0x00001000
  148. #define DEC_Underflow 0x00002000
  149. #else
  150. /* IEEE flags only */
  151. #define DEC_Conversion_syntax 0x00000010
  152. #define DEC_Division_by_zero 0x00000002
  153. #define DEC_Division_impossible 0x00000010
  154. #define DEC_Division_undefined 0x00000010
  155. #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */
  156. #define DEC_Inexact 0x00000001
  157. #define DEC_Invalid_context 0x00000010
  158. #define DEC_Invalid_operation 0x00000010
  159. #if DECSUBSET
  160. #define DEC_Lost_digits 0x00000000
  161. #endif
  162. #define DEC_Overflow 0x00000008
  163. #define DEC_Clamped 0x00000000
  164. #define DEC_Rounded 0x00000000
  165. #define DEC_Subnormal 0x00000000
  166. #define DEC_Underflow 0x00000004
  167. #endif
  168. /* IEEE 754 groupings for the flags */
  169. /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */
  170. /* are not in IEEE 754] */
  171. #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero)
  172. #if DECSUBSET
  173. #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits)
  174. #else
  175. #define DEC_IEEE_754_Inexact (DEC_Inexact)
  176. #endif
  177. #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \
  178. DEC_Division_impossible | \
  179. DEC_Division_undefined | \
  180. DEC_Insufficient_storage | \
  181. DEC_Invalid_context | \
  182. DEC_Invalid_operation)
  183. #define DEC_IEEE_754_Overflow (DEC_Overflow)
  184. #define DEC_IEEE_754_Underflow (DEC_Underflow)
  185. /* flags which are normally errors (result is qNaN, infinite, or 0) */
  186. #define DEC_Errors (DEC_IEEE_754_Division_by_zero | \
  187. DEC_IEEE_754_Invalid_operation | \
  188. DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow)
  189. /* flags which cause a result to become qNaN */
  190. #define DEC_NaNs DEC_IEEE_754_Invalid_operation
  191. /* flags which are normally for information only (finite results) */
  192. #if DECSUBSET
  193. #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \
  194. | DEC_Lost_digits)
  195. #else
  196. #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
  197. #endif
  198. /* IEEE 854 names (for compatibility with older decNumber versions) */
  199. #define DEC_IEEE_854_Division_by_zero DEC_IEEE_754_Division_by_zero
  200. #define DEC_IEEE_854_Inexact DEC_IEEE_754_Inexact
  201. #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation
  202. #define DEC_IEEE_854_Overflow DEC_IEEE_754_Overflow
  203. #define DEC_IEEE_854_Underflow DEC_IEEE_754_Underflow
  204. /* Name strings for the exceptional conditions */
  205. #define DEC_Condition_CS "Conversion syntax"
  206. #define DEC_Condition_DZ "Division by zero"
  207. #define DEC_Condition_DI "Division impossible"
  208. #define DEC_Condition_DU "Division undefined"
  209. #define DEC_Condition_IE "Inexact"
  210. #define DEC_Condition_IS "Insufficient storage"
  211. #define DEC_Condition_IC "Invalid context"
  212. #define DEC_Condition_IO "Invalid operation"
  213. #if DECSUBSET
  214. #define DEC_Condition_LD "Lost digits"
  215. #endif
  216. #define DEC_Condition_OV "Overflow"
  217. #define DEC_Condition_PA "Clamped"
  218. #define DEC_Condition_RO "Rounded"
  219. #define DEC_Condition_SU "Subnormal"
  220. #define DEC_Condition_UN "Underflow"
  221. #define DEC_Condition_ZE "No status"
  222. #define DEC_Condition_MU "Multiple status"
  223. #define DEC_Condition_Length 21 /* length of the longest string, */
  224. /* including terminator */
  225. /* Initialization descriptors, used by decContextDefault */
  226. #define DEC_INIT_BASE 0
  227. #define DEC_INIT_DECIMAL32 32
  228. #define DEC_INIT_DECIMAL64 64
  229. #define DEC_INIT_DECIMAL128 128
  230. /* Synonyms */
  231. #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32
  232. #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64
  233. #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128
  234. /* decContext routines */
  235. U_INTERNAL decContext * U_EXPORT2 uprv_decContextClearStatus(decContext *, uint32_t);
  236. U_INTERNAL decContext * U_EXPORT2 uprv_decContextDefault(decContext *, int32_t);
  237. U_INTERNAL enum rounding U_EXPORT2 uprv_decContextGetRounding(decContext *);
  238. U_INTERNAL uint32_t U_EXPORT2 uprv_decContextGetStatus(decContext *);
  239. U_INTERNAL decContext * U_EXPORT2 uprv_decContextRestoreStatus(decContext *, uint32_t, uint32_t);
  240. U_INTERNAL uint32_t U_EXPORT2 uprv_decContextSaveStatus(decContext *, uint32_t);
  241. U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetRounding(decContext *, enum rounding);
  242. U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatus(decContext *, uint32_t);
  243. U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusFromString(decContext *, const char *);
  244. U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusFromStringQuiet(decContext *, const char *);
  245. U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusQuiet(decContext *, uint32_t);
  246. U_INTERNAL const char * U_EXPORT2 uprv_decContextStatusToString(const decContext *);
  247. U_INTERNAL int32_t U_EXPORT2 uprv_decContextTestEndian(uint8_t);
  248. U_INTERNAL uint32_t U_EXPORT2 uprv_decContextTestSavedStatus(uint32_t, uint32_t);
  249. U_INTERNAL uint32_t U_EXPORT2 uprv_decContextTestStatus(decContext *, uint32_t);
  250. U_INTERNAL decContext * U_EXPORT2 uprv_decContextZeroStatus(decContext *);
  251. #endif