decNumber.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Decimal number arithmetic module header for the decNumber C Library.
  2. Copyright (C) 2005-2013 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 Number arithmetic module header */
  22. /* ------------------------------------------------------------------ */
  23. #if !defined(DECNUMBER)
  24. #define DECNUMBER
  25. #define DECNAME "decNumber" /* Short name */
  26. #define DECFULLNAME "Decimal Number Module" /* Verbose name */
  27. #define DECAUTHOR "Mike Cowlishaw" /* Who to blame */
  28. #if !defined(DECCONTEXT)
  29. #include "decContext.h"
  30. #endif
  31. /* Bit settings for decNumber.bits */
  32. #define DECNEG 0x80 /* Sign; 1=negative, 0=positive or zero */
  33. #define DECINF 0x40 /* 1=Infinity */
  34. #define DECNAN 0x20 /* 1=NaN */
  35. #define DECSNAN 0x10 /* 1=sNaN */
  36. /* The remaining bits are reserved; they must be 0 */
  37. #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value */
  38. /* Define the decNumber data structure. The size and shape of the */
  39. /* units array in the structure is determined by the following */
  40. /* constant. This must not be changed without recompiling the */
  41. /* decNumber library modules. */
  42. #define DECDPUN 3 /* DECimal Digits Per UNit [must be >0 */
  43. /* and <10; 3 or powers of 2 are best]. */
  44. /* DECNUMDIGITS is the default number of digits that can be held in */
  45. /* the structure. If undefined, 1 is assumed and it is assumed */
  46. /* that the structure will be immediately followed by extra space, */
  47. /* as required. DECNUMDIGITS is always >0. */
  48. #if !defined(DECNUMDIGITS)
  49. #define DECNUMDIGITS 1
  50. #endif
  51. /* The size (integer data type) of each unit is determined by the */
  52. /* number of digits it will hold. */
  53. #if DECDPUN<=2
  54. #define decNumberUnit uint8_t
  55. #elif DECDPUN<=4
  56. #define decNumberUnit uint16_t
  57. #else
  58. #define decNumberUnit uint32_t
  59. #endif
  60. /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN) */
  61. #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)
  62. /* The data structure... */
  63. typedef struct {
  64. int32_t digits; /* Count of digits in the coefficient; >0 */
  65. int32_t exponent; /* Unadjusted exponent, unbiased, in */
  66. /* range: -1999999997 through 999999999 */
  67. uint8_t bits; /* Indicator bits (see above) */
  68. /* Coefficient, from least significant unit */
  69. decNumberUnit lsu[DECNUMUNITS];
  70. } decNumber;
  71. /* Notes: */
  72. /* 1. If digits is > DECDPUN then there will one or more */
  73. /* decNumberUnits immediately following the first element of lsu.*/
  74. /* These contain the remaining (more significant) digits of the */
  75. /* number, and may be in the lsu array, or may be guaranteed by */
  76. /* some other mechanism (such as being contained in another */
  77. /* structure, or being overlaid on dynamically allocated */
  78. /* storage). */
  79. /* */
  80. /* Each integer of the coefficient (except potentially the last) */
  81. /* contains DECDPUN digits (e.g., a value in the range 0 through */
  82. /* 99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3). */
  83. /* */
  84. /* 2. A decNumber converted to a string may need up to digits+14 */
  85. /* characters. The worst cases (non-exponential and exponential */
  86. /* formats) are -0.00000{9...}# and -9.{9...}E+999999999# */
  87. /* (where # is '\0') */
  88. /* ---------------------------------------------------------------- */
  89. /* decNumber public functions and macros */
  90. /* ---------------------------------------------------------------- */
  91. #include "decNumberSymbols.h"
  92. #ifdef __cplusplus
  93. extern "C" {
  94. #endif
  95. /* Conversions */
  96. decNumber * decNumberFromInt32(decNumber *, int32_t);
  97. decNumber * decNumberFromUInt32(decNumber *, uint32_t);
  98. decNumber * decNumberFromString(decNumber *, const char *, decContext *);
  99. char * decNumberToString(const decNumber *, char *);
  100. char * decNumberToEngString(const decNumber *, char *);
  101. uint32_t decNumberToUInt32(const decNumber *, decContext *);
  102. int32_t decNumberToInt32(const decNumber *, decContext *);
  103. uint8_t * decNumberGetBCD(const decNumber *, uint8_t *);
  104. decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
  105. /* Operators and elementary functions */
  106. decNumber * decNumberAbs(decNumber *, const decNumber *, decContext *);
  107. decNumber * decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
  108. decNumber * decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
  109. decNumber * decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
  110. decNumber * decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
  111. decNumber * decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
  112. decNumber * decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  113. decNumber * decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
  114. decNumber * decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
  115. decNumber * decNumberExp(decNumber *, const decNumber *, decContext *);
  116. decNumber * decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
  117. decNumber * decNumberInvert(decNumber *, const decNumber *, decContext *);
  118. decNumber * decNumberLn(decNumber *, const decNumber *, decContext *);
  119. decNumber * decNumberLogB(decNumber *, const decNumber *, decContext *);
  120. decNumber * decNumberLog10(decNumber *, const decNumber *, decContext *);
  121. decNumber * decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
  122. decNumber * decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  123. decNumber * decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
  124. decNumber * decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
  125. decNumber * decNumberMinus(decNumber *, const decNumber *, decContext *);
  126. decNumber * decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
  127. decNumber * decNumberNormalize(decNumber *, const decNumber *, decContext *);
  128. decNumber * decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
  129. decNumber * decNumberPlus(decNumber *, const decNumber *, decContext *);
  130. decNumber * decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
  131. decNumber * decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
  132. decNumber * decNumberReduce(decNumber *, const decNumber *, decContext *);
  133. decNumber * decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
  134. decNumber * decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
  135. decNumber * decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
  136. decNumber * decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
  137. decNumber * decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
  138. decNumber * decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
  139. decNumber * decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
  140. decNumber * decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
  141. decNumber * decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
  142. decNumber * decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
  143. decNumber * decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
  144. decNumber * decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);
  145. /* Utilities */
  146. enum decClass decNumberClass(const decNumber *, decContext *);
  147. const char * decNumberClassToString(enum decClass);
  148. decNumber * decNumberCopy(decNumber *, const decNumber *);
  149. decNumber * decNumberCopyAbs(decNumber *, const decNumber *);
  150. decNumber * decNumberCopyNegate(decNumber *, const decNumber *);
  151. decNumber * decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
  152. decNumber * decNumberNextMinus(decNumber *, const decNumber *, decContext *);
  153. decNumber * decNumberNextPlus(decNumber *, const decNumber *, decContext *);
  154. decNumber * decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
  155. decNumber * decNumberTrim(decNumber *);
  156. const char * decNumberVersion(void);
  157. decNumber * decNumberZero(decNumber *);
  158. /* Functions for testing decNumbers (normality depends on context) */
  159. int32_t decNumberIsNormal(const decNumber *, decContext *);
  160. int32_t decNumberIsSubnormal(const decNumber *, decContext *);
  161. /* Macros for testing decNumber *dn */
  162. #define decNumberIsCanonical(dn) (1) /* All decNumbers are saintly */
  163. #define decNumberIsFinite(dn) (((dn)->bits&DECSPECIAL)==0)
  164. #define decNumberIsInfinite(dn) (((dn)->bits&DECINF)!=0)
  165. #define decNumberIsNaN(dn) (((dn)->bits&(DECNAN|DECSNAN))!=0)
  166. #define decNumberIsNegative(dn) (((dn)->bits&DECNEG)!=0)
  167. #define decNumberIsQNaN(dn) (((dn)->bits&(DECNAN))!=0)
  168. #define decNumberIsSNaN(dn) (((dn)->bits&(DECSNAN))!=0)
  169. #define decNumberIsSpecial(dn) (((dn)->bits&DECSPECIAL)!=0)
  170. #define decNumberIsZero(dn) (*(dn)->lsu==0 \
  171. && (dn)->digits==1 \
  172. && (((dn)->bits&DECSPECIAL)==0))
  173. #define decNumberRadix(dn) (10)
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif