decNumber.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /* ------------------------------------------------------------------ */
  4. /* Decimal Number arithmetic module header */
  5. /* ------------------------------------------------------------------ */
  6. /* Copyright (c) IBM Corporation, 2000-2010. 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. #if !defined(DECNUMBER)
  32. #define DECNUMBER
  33. #define DECNAME "decNumber" /* Short name */
  34. #define DECFULLNAME "Decimal Number Module" /* Verbose name */
  35. #define DECAUTHOR "Mike Cowlishaw" /* Who to blame */
  36. #if !defined(DECCONTEXT)
  37. #include "decContext.h"
  38. #endif
  39. /* Bit settings for decNumber.bits */
  40. #define DECNEG 0x80 /* Sign; 1=negative, 0=positive or zero */
  41. #define DECINF 0x40 /* 1=Infinity */
  42. #define DECNAN 0x20 /* 1=NaN */
  43. #define DECSNAN 0x10 /* 1=sNaN */
  44. /* The remaining bits are reserved; they must be 0 */
  45. #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value */
  46. /* Define the decNumber data structure. The size and shape of the */
  47. /* units array in the structure is determined by the following */
  48. /* constant. This must not be changed without recompiling the */
  49. /* decNumber library modules. */
  50. /* For ICU, use one digit per byte, to make it easier to emulate the
  51. * old DigitList interface on top of a decNumber
  52. */
  53. #define DECDPUN 1 /* DECimal Digits Per UNit [must be >0 */
  54. /* and <10; 3 or powers of 2 are best]. */
  55. /* DECNUMDIGITS is the default number of digits that can be held in */
  56. /* the structure. If undefined, 1 is assumed and it is assumed */
  57. /* that the structure will be immediately followed by extra space, */
  58. /* as required. DECNUMDIGITS is always >0. */
  59. #if !defined(DECNUMDIGITS)
  60. #define DECNUMDIGITS 1
  61. #endif
  62. /* The size (integer data type) of each unit is determined by the */
  63. /* number of digits it will hold. */
  64. #if DECDPUN<=2
  65. #define decNumberUnit uint8_t
  66. #elif DECDPUN<=4
  67. #define decNumberUnit uint16_t
  68. #else
  69. #define decNumberUnit uint32_t
  70. #endif
  71. /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN) */
  72. #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)
  73. /* The data structure... */
  74. typedef struct {
  75. int32_t digits; /* Count of digits in the coefficient; >0 */
  76. int32_t exponent; /* Unadjusted exponent, unbiased, in */
  77. /* range: -1999999997 through 999999999 */
  78. uint8_t bits; /* Indicator bits (see above) */
  79. /* Coefficient, from least significant unit */
  80. decNumberUnit lsu[DECNUMUNITS];
  81. } decNumber;
  82. /* Notes: */
  83. /* 1. If digits is > DECDPUN then there will one or more */
  84. /* decNumberUnits immediately following the first element of lsu.*/
  85. /* These contain the remaining (more significant) digits of the */
  86. /* number, and may be in the lsu array, or may be guaranteed by */
  87. /* some other mechanism (such as being contained in another */
  88. /* structure, or being overlaid on dynamically allocated */
  89. /* storage). */
  90. /* */
  91. /* Each integer of the coefficient (except potentially the last) */
  92. /* contains DECDPUN digits (e.g., a value in the range 0 through */
  93. /* 99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3). */
  94. /* */
  95. /* 2. A decNumber converted to a string may need up to digits+14 */
  96. /* characters. The worst cases (non-exponential and exponential */
  97. /* formats) are -0.00000{9...}# and -9.{9...}E+999999999# */
  98. /* (where # is '\0') */
  99. /* ---------------------------------------------------------------- */
  100. /* decNumber public functions and macros */
  101. /* ---------------------------------------------------------------- */
  102. /* Conversions */
  103. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromInt32(decNumber *, int32_t);
  104. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromUInt32(decNumber *, uint32_t);
  105. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromString(decNumber *, const char *, decContext *);
  106. U_INTERNAL char * U_EXPORT2 uprv_decNumberToString(const decNumber *, char *);
  107. U_INTERNAL char * U_EXPORT2 uprv_decNumberToEngString(const decNumber *, char *);
  108. U_INTERNAL uint32_t U_EXPORT2 uprv_decNumberToUInt32(const decNumber *, decContext *);
  109. U_INTERNAL int32_t U_EXPORT2 uprv_decNumberToInt32(const decNumber *, decContext *);
  110. U_INTERNAL uint8_t * U_EXPORT2 uprv_decNumberGetBCD(const decNumber *, uint8_t *);
  111. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
  112. /* Operators and elementary functions */
  113. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAbs(decNumber *, const decNumber *, decContext *);
  114. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
  115. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
  116. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
  117. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
  118. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
  119. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  120. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
  121. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
  122. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberExp(decNumber *, const decNumber *, decContext *);
  123. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
  124. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberInvert(decNumber *, const decNumber *, decContext *);
  125. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLn(decNumber *, const decNumber *, decContext *);
  126. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLogB(decNumber *, const decNumber *, decContext *);
  127. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLog10(decNumber *, const decNumber *, decContext *);
  128. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
  129. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  130. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
  131. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  132. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMinus(decNumber *, const decNumber *, decContext *);
  133. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
  134. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNormalize(decNumber *, const decNumber *, decContext *);
  135. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
  136. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberPlus(decNumber *, const decNumber *, decContext *);
  137. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
  138. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
  139. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberReduce(decNumber *, const decNumber *, decContext *);
  140. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
  141. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
  142. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
  143. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
  144. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
  145. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
  146. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
  147. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
  148. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
  149. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
  150. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
  151. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);
  152. /* Utilities */
  153. enum decClass uprv_decNumberClass(const decNumber *, decContext *);
  154. U_INTERNAL const char * U_EXPORT2 uprv_decNumberClassToString(enum decClass);
  155. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopy(decNumber *, const decNumber *);
  156. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopyAbs(decNumber *, const decNumber *);
  157. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopyNegate(decNumber *, const decNumber *);
  158. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
  159. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNextMinus(decNumber *, const decNumber *, decContext *);
  160. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNextPlus(decNumber *, const decNumber *, decContext *);
  161. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
  162. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberTrim(decNumber *);
  163. U_INTERNAL const char * U_EXPORT2 uprv_decNumberVersion(void);
  164. U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberZero(decNumber *);
  165. /* Functions for testing decNumbers (normality depends on context) */
  166. U_INTERNAL int32_t U_EXPORT2 uprv_decNumberIsNormal(const decNumber *, decContext *);
  167. U_INTERNAL int32_t U_EXPORT2 uprv_decNumberIsSubnormal(const decNumber *, decContext *);
  168. /* Macros for testing decNumber *dn */
  169. #define decNumberIsCanonical(dn) (1) /* All decNumbers are saintly */
  170. #define decNumberIsFinite(dn) (((dn)->bits&DECSPECIAL)==0)
  171. #define decNumberIsInfinite(dn) (((dn)->bits&DECINF)!=0)
  172. #define decNumberIsNaN(dn) (((dn)->bits&(DECNAN|DECSNAN))!=0)
  173. #define decNumberIsNegative(dn) (((dn)->bits&DECNEG)!=0)
  174. #define decNumberIsQNaN(dn) (((dn)->bits&(DECNAN))!=0)
  175. #define decNumberIsSNaN(dn) (((dn)->bits&(DECSNAN))!=0)
  176. #define decNumberIsSpecial(dn) (((dn)->bits&DECSPECIAL)!=0)
  177. #define decNumberIsZero(dn) (*(dn)->lsu==0 \
  178. && (dn)->digits==1 \
  179. && (((dn)->bits&DECSPECIAL)==0))
  180. #define decNumberRadix(dn) (10)
  181. #endif