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