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