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