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