PageRenderTime 32ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/i18n/decContext.h

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