decContext.h 12 KB

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