Commit | Line | Data |
---|---|---|
f5bc1778 | 1 | /* Decimal context header module for the decNumber C Library. |
168a2f77 | 2 | Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc. |
f5bc1778 DJ |
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 | |
168a2f77 | 9 | Software Foundation; either version 3, or (at your option) any later |
f5bc1778 DJ |
10 | version. |
11 | ||
f5bc1778 DJ |
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 | ||
168a2f77 DD |
17 | Under Section 7 of GPL version 3, you are granted additional |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | <http://www.gnu.org/licenses/>. */ | |
f5bc1778 DJ |
25 | |
26 | /* ------------------------------------------------------------------ */ | |
27 | /* Decimal Context module header */ | |
28 | /* ------------------------------------------------------------------ */ | |
29 | /* */ | |
30 | /* Context variables must always have valid values: */ | |
31 | /* */ | |
87d32bb7 | 32 | /* status -- [any bits may be cleared, but not set, by user] */ |
f5bc1778 DJ |
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 */ | |
87d32bb7 | 52 | #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */ |
f5bc1778 | 53 | |
1aa34cc5 | 54 | #include "gstdint.h" /* C99 standard integers */ |
f5bc1778 | 55 | #include <stdio.h> /* for printf, etc. */ |
87d32bb7 | 56 | #include <signal.h> /* for traps */ |
f5bc1778 DJ |
57 | |
58 | /* Extended flags setting -- set this to 0 to use only IEEE flags */ | |
87d32bb7 | 59 | #if !defined(DECEXTFLAG) |
f5bc1778 | 60 | #define DECEXTFLAG 1 /* 1=enable extended flags */ |
87d32bb7 | 61 | #endif |
f5bc1778 DJ |
62 | |
63 | /* Conditional code flag -- set this to 0 for best performance */ | |
87d32bb7 | 64 | #if !defined(DECSUBSET) |
f5bc1778 | 65 | #define DECSUBSET 0 /* 1=enable subset arithmetic */ |
87d32bb7 | 66 | #endif |
f5bc1778 DJ |
67 | |
68 | /* Context for operations, with associated constants */ | |
69 | enum rounding { | |
70 | DEC_ROUND_CEILING, /* round towards +infinity */ | |
87d32bb7 | 71 | DEC_ROUND_UP, /* round away from 0 */ |
f5bc1778 DJ |
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 */ | |
87d32bb7 | 77 | DEC_ROUND_05UP, /* round for reround */ |
f5bc1778 DJ |
78 | DEC_ROUND_MAX /* enum must be less than this */ |
79 | }; | |
80 | #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN; | |
81 | ||
82 | typedef struct { | |
87d32bb7 DD |
83 | int32_t digits; /* working precision */ |
84 | int32_t emax; /* maximum positive exponent */ | |
85 | int32_t emin; /* minimum negative exponent */ | |
f5bc1778 DJ |
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 | ||
87d32bb7 DD |
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). */ | |
f5bc1778 DJ |
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] */ | |
87d32bb7 DD |
141 | #define DEC_Inexact 0x00000020 |
142 | #define DEC_Invalid_context 0x00000040 | |
f5bc1778 DJ |
143 | #define DEC_Invalid_operation 0x00000080 |
144 | #if DECSUBSET | |
145 | #define DEC_Lost_digits 0x00000100 | |
146 | #endif | |
147 | #define DEC_Overflow 0x00000200 | |
87d32bb7 DD |
148 | #define DEC_Clamped 0x00000400 |
149 | #define DEC_Rounded 0x00000800 | |
f5bc1778 DJ |
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] */ | |
87d32bb7 DD |
159 | #define DEC_Inexact 0x00000001 |
160 | #define DEC_Invalid_context 0x00000010 | |
f5bc1778 DJ |
161 | #define DEC_Invalid_operation 0x00000010 |
162 | #if DECSUBSET | |
163 | #define DEC_Lost_digits 0x00000000 | |
164 | #endif | |
165 | #define DEC_Overflow 0x00000008 | |
87d32bb7 DD |
166 | #define DEC_Clamped 0x00000000 |
167 | #define DEC_Rounded 0x00000000 | |
f5bc1778 DJ |
168 | #define DEC_Subnormal 0x00000000 |
169 | #define DEC_Underflow 0x00000004 | |
170 | #endif | |
171 | ||
87d32bb7 | 172 | /* IEEE 754 groupings for the flags */ |
f5bc1778 | 173 | /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */ |
87d32bb7 DD |
174 | /* are not in IEEE 754] */ |
175 | #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero) | |
f5bc1778 | 176 | #if DECSUBSET |
87d32bb7 | 177 | #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits) |
f5bc1778 | 178 | #else |
87d32bb7 | 179 | #define DEC_IEEE_754_Inexact (DEC_Inexact) |
f5bc1778 | 180 | #endif |
87d32bb7 | 181 | #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \ |
f5bc1778 DJ |
182 | DEC_Division_impossible | \ |
183 | DEC_Division_undefined | \ | |
184 | DEC_Insufficient_storage | \ | |
87d32bb7 | 185 | DEC_Invalid_context | \ |
f5bc1778 | 186 | DEC_Invalid_operation) |
87d32bb7 DD |
187 | #define DEC_IEEE_754_Overflow (DEC_Overflow) |
188 | #define DEC_IEEE_754_Underflow (DEC_Underflow) | |
f5bc1778 DJ |
189 | |
190 | /* flags which are normally errors (result is qNaN, infinite, or 0) */ | |
87d32bb7 DD |
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) | |
f5bc1778 | 194 | /* flags which cause a result to become qNaN */ |
87d32bb7 | 195 | #define DEC_NaNs DEC_IEEE_754_Invalid_operation |
f5bc1778 DJ |
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 | ||
87d32bb7 DD |
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 | ||
f5bc1778 DJ |
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 */ | |
87d32bb7 | 235 | #define DEC_INIT_BASE 0 |
f5bc1778 DJ |
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 | ||
52d6785f DD |
248 | #ifdef __cplusplus |
249 | extern "C" { | |
250 | #endif | |
251 | ||
f5bc1778 DJ |
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 *); | |
87d32bb7 | 264 | extern int32_t decContextTestEndian(uint8_t); |
f5bc1778 DJ |
265 | extern uint32_t decContextTestSavedStatus(uint32_t, uint32_t); |
266 | extern uint32_t decContextTestStatus(decContext *, uint32_t); | |
267 | extern decContext * decContextZeroStatus(decContext *); | |
268 | ||
52d6785f DD |
269 | #ifdef __cplusplus |
270 | } | |
271 | #endif | |
272 | ||
f5bc1778 | 273 | #endif |