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

source code of libdecnumber/decContext.h