decContext.h 11 KB

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