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