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
41 #if !defined(DECCONTEXT)
42 # define DECCONTEXT
43 # define DECCNAME "decContext"
44 # define DECCFULLNAME "Decimal Context Descriptor"
45 # define DECCAUTHOR "Mike Cowlishaw"
46
47 # if !defined(int32_t)
48 # include <stdint.h>
49 # endif
50 # include <stdio.h>
51 # include <signal.h>
52
53
54 # if !defined(DECEXTFLAG)
55 # define DECEXTFLAG 1
56 # endif
57
58
59 # if !defined(DECSUBSET)
60 # define DECSUBSET 0
61 # endif
62
63
64 enum rounding {
65 DEC_ROUND_CEILING,
66 DEC_ROUND_UP,
67 DEC_ROUND_HALF_UP,
68 DEC_ROUND_HALF_EVEN,
69 DEC_ROUND_HALF_DOWN,
70 DEC_ROUND_DOWN,
71 DEC_ROUND_FLOOR,
72 DEC_ROUND_05UP,
73 DEC_ROUND_MAX
74 };
75 # define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
76
77 typedef struct {
78 int32_t digits;
79 int32_t emax;
80 int32_t emin;
81 enum rounding round;
82 uint32_t traps;
83 uint32_t status;
84 uint8_t clamp;
85 # if DECSUBSET
86 uint8_t extended;
87 # endif
88 } decContext;
89
90
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
98
99
100
101
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
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
128
129 # if DECEXTFLAG
130
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
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
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
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
168
169
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
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
190 # define DEC_NaNs DEC_IEEE_754_Invalid_operation
191
192
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
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
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
227
228
229
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
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
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