1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #if !defined(DECCONTEXT)
41 # define DECCONTEXT
42 # define DECCNAME "decContext"
43 # define DECCFULLNAME "Decimal Context Descriptor"
44 # define DECCAUTHOR "Mike Cowlishaw"
45
46 # if !defined(int32_t)
47 # include <stdint.h>
48 # endif
49 # include <signal.h>
50
51
52 # if !defined(DECEXTFLAG)
53 # define DECEXTFLAG 1
54 # endif
55
56
57 # if !defined(DECSUBSET)
58 # define DECSUBSET 0
59 # endif
60
61
62 enum rounding {
63 DEC_ROUND_CEILING,
64 DEC_ROUND_UP,
65 DEC_ROUND_HALF_UP,
66 DEC_ROUND_HALF_EVEN,
67 DEC_ROUND_HALF_DOWN,
68 DEC_ROUND_DOWN,
69 DEC_ROUND_FLOOR,
70 DEC_ROUND_05UP,
71 DEC_ROUND_MAX
72 };
73 # define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
74
75 typedef struct {
76 int32_t digits;
77 int32_t emax;
78 int32_t emin;
79 enum rounding round;
80 uint32_t traps;
81 uint32_t status;
82 uint8_t clamp;
83 # if DECSUBSET
84 uint8_t extended;
85 # endif
86 } decContext;
87
88
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
96
97
98
99
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
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
126
127 # if DECEXTFLAG
128
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
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
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
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
166
167
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
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
188 # define DEC_NaNs DEC_IEEE_754_Invalid_operation
189
190
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
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
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
225
226
227
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
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
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