1 /* Decimal context module for the decNumber C Library.
2 Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
3 Contributed by IBM Corporation. Author Mike Cowlishaw.
5 This file is part of GCC.
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
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
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.
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/>. */
26 /* ------------------------------------------------------------------ */
27 /* Decimal Context module */
28 /* ------------------------------------------------------------------ */
29 /* This module comprises the routines for handling arithmetic */
30 /* context structures. */
31 /* ------------------------------------------------------------------ */
33 #include <string.h> /* for strcmp */
34 #include <stdio.h> /* for printf if DECCHECK */
35 #include "dconfig.h" /* for GCC definitions */
36 #include "decContext.h" /* context and base types */
37 #include "decNumberLocal.h" /* decNumber local types, etc. */
39 /* compile-time endian tester [assumes sizeof(Int)>1] */
40 static const Int mfcone
=1; /* constant 1 */
41 static const Flag
*mfctop
=(const Flag
*)&mfcone
; /* -> top byte */
42 #define LITEND *mfctop /* named flag; 1=little-endian */
44 /* ------------------------------------------------------------------ */
45 /* round-for-reround digits */
46 /* ------------------------------------------------------------------ */
47 const uByte DECSTICKYTAB
[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
49 /* ------------------------------------------------------------------ */
50 /* Powers of ten (powers[n]==10**n, 0<=n<=9) */
51 /* ------------------------------------------------------------------ */
52 const uInt DECPOWERS
[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
53 10000000, 100000000, 1000000000};
55 /* ------------------------------------------------------------------ */
56 /* decContextClearStatus -- clear bits in current status */
58 /* context is the context structure to be queried */
59 /* mask indicates the bits to be cleared (the status bit that */
60 /* corresponds to each 1 bit in the mask is cleared) */
63 /* No error is possible. */
64 /* ------------------------------------------------------------------ */
65 decContext
*decContextClearStatus(decContext
*context
, uInt mask
) {
66 context
->status
&=~mask
;
68 } /* decContextClearStatus */
70 /* ------------------------------------------------------------------ */
71 /* decContextDefault -- initialize a context structure */
73 /* context is the structure to be initialized */
74 /* kind selects the required set of default values, one of: */
75 /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
76 /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */
77 /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */
78 /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */
79 /* For any other value a valid context is returned, but with */
80 /* Invalid_operation set in the status field. */
81 /* returns a context structure with the appropriate initial values. */
82 /* ------------------------------------------------------------------ */
83 decContext
* decContextDefault(decContext
*context
, Int kind
) {
85 context
->digits
=9; /* 9 digits */
86 context
->emax
=DEC_MAX_EMAX
; /* 9-digit exponents */
87 context
->emin
=DEC_MIN_EMIN
; /* .. balanced */
88 context
->round
=DEC_ROUND_HALF_UP
; /* 0.5 rises */
89 context
->traps
=DEC_Errors
; /* all but informational */
90 context
->status
=0; /* cleared */
91 context
->clamp
=0; /* no clamping */
93 context
->extended
=0; /* cleared */
99 case DEC_INIT_DECIMAL32
:
100 context
->digits
=7; /* digits */
101 context
->emax
=96; /* Emax */
102 context
->emin
=-95; /* Emin */
103 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
104 context
->traps
=0; /* no traps set */
105 context
->clamp
=1; /* clamp exponents */
107 context
->extended
=1; /* set */
110 case DEC_INIT_DECIMAL64
:
111 context
->digits
=16; /* digits */
112 context
->emax
=384; /* Emax */
113 context
->emin
=-383; /* Emin */
114 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
115 context
->traps
=0; /* no traps set */
116 context
->clamp
=1; /* clamp exponents */
118 context
->extended
=1; /* set */
121 case DEC_INIT_DECIMAL128
:
122 context
->digits
=34; /* digits */
123 context
->emax
=6144; /* Emax */
124 context
->emin
=-6143; /* Emin */
125 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
126 context
->traps
=0; /* no traps set */
127 context
->clamp
=1; /* clamp exponents */
129 context
->extended
=1; /* set */
133 default: /* invalid Kind */
134 /* use defaults, and .. */
135 decContextSetStatus(context
, DEC_Invalid_operation
); /* trap */
138 return context
;} /* decContextDefault */
140 /* ------------------------------------------------------------------ */
141 /* decContextGetRounding -- return current rounding mode */
143 /* context is the context structure to be queried */
144 /* returns the rounding mode */
146 /* No error is possible. */
147 /* ------------------------------------------------------------------ */
148 enum rounding
decContextGetRounding(decContext
*context
) {
149 return context
->round
;
150 } /* decContextGetRounding */
152 /* ------------------------------------------------------------------ */
153 /* decContextGetStatus -- return current status */
155 /* context is the context structure to be queried */
158 /* No error is possible. */
159 /* ------------------------------------------------------------------ */
160 uInt
decContextGetStatus(decContext
*context
) {
161 return context
->status
;
162 } /* decContextGetStatus */
164 /* ------------------------------------------------------------------ */
165 /* decContextRestoreStatus -- restore bits in current status */
167 /* context is the context structure to be updated */
168 /* newstatus is the source for the bits to be restored */
169 /* mask indicates the bits to be restored (the status bit that */
170 /* corresponds to each 1 bit in the mask is set to the value of */
171 /* the correspnding bit in newstatus) */
172 /* returns context */
174 /* No error is possible. */
175 /* ------------------------------------------------------------------ */
176 decContext
*decContextRestoreStatus(decContext
*context
,
177 uInt newstatus
, uInt mask
) {
178 context
->status
&=~mask
; /* clear the selected bits */
179 context
->status
|=(mask
&newstatus
); /* or in the new bits */
181 } /* decContextRestoreStatus */
183 /* ------------------------------------------------------------------ */
184 /* decContextSaveStatus -- save bits in current status */
186 /* context is the context structure to be queried */
187 /* mask indicates the bits to be saved (the status bits that */
188 /* correspond to each 1 bit in the mask are saved) */
189 /* returns the AND of the mask and the current status */
191 /* No error is possible. */
192 /* ------------------------------------------------------------------ */
193 uInt
decContextSaveStatus(decContext
*context
, uInt mask
) {
194 return context
->status
&mask
;
195 } /* decContextSaveStatus */
197 /* ------------------------------------------------------------------ */
198 /* decContextSetRounding -- set current rounding mode */
200 /* context is the context structure to be updated */
201 /* newround is the value which will replace the current mode */
202 /* returns context */
204 /* No error is possible. */
205 /* ------------------------------------------------------------------ */
206 decContext
*decContextSetRounding(decContext
*context
,
207 enum rounding newround
) {
208 context
->round
=newround
;
210 } /* decContextSetRounding */
212 /* ------------------------------------------------------------------ */
213 /* decContextSetStatus -- set status and raise trap if appropriate */
215 /* context is the context structure to be updated */
216 /* status is the DEC_ exception code */
217 /* returns the context structure */
219 /* Control may never return from this routine, if there is a signal */
220 /* handler and it takes a long jump. */
221 /* ------------------------------------------------------------------ */
222 decContext
* decContextSetStatus(decContext
*context
, uInt status
) {
223 context
->status
|=status
;
224 if (status
& context
->traps
) raise(SIGFPE
);
225 return context
;} /* decContextSetStatus */
227 /* ------------------------------------------------------------------ */
228 /* decContextSetStatusFromString -- set status from a string + trap */
230 /* context is the context structure to be updated */
231 /* string is a string exactly equal to one that might be returned */
232 /* by decContextStatusToString */
234 /* The status bit corresponding to the string is set, and a trap */
235 /* is raised if appropriate. */
237 /* returns the context structure, unless the string is equal to */
238 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
240 /* ------------------------------------------------------------------ */
241 decContext
* decContextSetStatusFromString(decContext
*context
,
242 const char *string
) {
243 if (strcmp(string
, DEC_Condition_CS
)==0)
244 return decContextSetStatus(context
, DEC_Conversion_syntax
);
245 if (strcmp(string
, DEC_Condition_DZ
)==0)
246 return decContextSetStatus(context
, DEC_Division_by_zero
);
247 if (strcmp(string
, DEC_Condition_DI
)==0)
248 return decContextSetStatus(context
, DEC_Division_impossible
);
249 if (strcmp(string
, DEC_Condition_DU
)==0)
250 return decContextSetStatus(context
, DEC_Division_undefined
);
251 if (strcmp(string
, DEC_Condition_IE
)==0)
252 return decContextSetStatus(context
, DEC_Inexact
);
253 if (strcmp(string
, DEC_Condition_IS
)==0)
254 return decContextSetStatus(context
, DEC_Insufficient_storage
);
255 if (strcmp(string
, DEC_Condition_IC
)==0)
256 return decContextSetStatus(context
, DEC_Invalid_context
);
257 if (strcmp(string
, DEC_Condition_IO
)==0)
258 return decContextSetStatus(context
, DEC_Invalid_operation
);
260 if (strcmp(string
, DEC_Condition_LD
)==0)
261 return decContextSetStatus(context
, DEC_Lost_digits
);
263 if (strcmp(string
, DEC_Condition_OV
)==0)
264 return decContextSetStatus(context
, DEC_Overflow
);
265 if (strcmp(string
, DEC_Condition_PA
)==0)
266 return decContextSetStatus(context
, DEC_Clamped
);
267 if (strcmp(string
, DEC_Condition_RO
)==0)
268 return decContextSetStatus(context
, DEC_Rounded
);
269 if (strcmp(string
, DEC_Condition_SU
)==0)
270 return decContextSetStatus(context
, DEC_Subnormal
);
271 if (strcmp(string
, DEC_Condition_UN
)==0)
272 return decContextSetStatus(context
, DEC_Underflow
);
273 if (strcmp(string
, DEC_Condition_ZE
)==0)
275 return NULL
; /* Multiple status, or unknown */
276 } /* decContextSetStatusFromString */
278 /* ------------------------------------------------------------------ */
279 /* decContextSetStatusFromStringQuiet -- set status from a string */
281 /* context is the context structure to be updated */
282 /* string is a string exactly equal to one that might be returned */
283 /* by decContextStatusToString */
285 /* The status bit corresponding to the string is set; no trap is */
288 /* returns the context structure, unless the string is equal to */
289 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
291 /* ------------------------------------------------------------------ */
292 decContext
* decContextSetStatusFromStringQuiet(decContext
*context
,
293 const char *string
) {
294 if (strcmp(string
, DEC_Condition_CS
)==0)
295 return decContextSetStatusQuiet(context
, DEC_Conversion_syntax
);
296 if (strcmp(string
, DEC_Condition_DZ
)==0)
297 return decContextSetStatusQuiet(context
, DEC_Division_by_zero
);
298 if (strcmp(string
, DEC_Condition_DI
)==0)
299 return decContextSetStatusQuiet(context
, DEC_Division_impossible
);
300 if (strcmp(string
, DEC_Condition_DU
)==0)
301 return decContextSetStatusQuiet(context
, DEC_Division_undefined
);
302 if (strcmp(string
, DEC_Condition_IE
)==0)
303 return decContextSetStatusQuiet(context
, DEC_Inexact
);
304 if (strcmp(string
, DEC_Condition_IS
)==0)
305 return decContextSetStatusQuiet(context
, DEC_Insufficient_storage
);
306 if (strcmp(string
, DEC_Condition_IC
)==0)
307 return decContextSetStatusQuiet(context
, DEC_Invalid_context
);
308 if (strcmp(string
, DEC_Condition_IO
)==0)
309 return decContextSetStatusQuiet(context
, DEC_Invalid_operation
);
311 if (strcmp(string
, DEC_Condition_LD
)==0)
312 return decContextSetStatusQuiet(context
, DEC_Lost_digits
);
314 if (strcmp(string
, DEC_Condition_OV
)==0)
315 return decContextSetStatusQuiet(context
, DEC_Overflow
);
316 if (strcmp(string
, DEC_Condition_PA
)==0)
317 return decContextSetStatusQuiet(context
, DEC_Clamped
);
318 if (strcmp(string
, DEC_Condition_RO
)==0)
319 return decContextSetStatusQuiet(context
, DEC_Rounded
);
320 if (strcmp(string
, DEC_Condition_SU
)==0)
321 return decContextSetStatusQuiet(context
, DEC_Subnormal
);
322 if (strcmp(string
, DEC_Condition_UN
)==0)
323 return decContextSetStatusQuiet(context
, DEC_Underflow
);
324 if (strcmp(string
, DEC_Condition_ZE
)==0)
326 return NULL
; /* Multiple status, or unknown */
327 } /* decContextSetStatusFromStringQuiet */
329 /* ------------------------------------------------------------------ */
330 /* decContextSetStatusQuiet -- set status without trap */
332 /* context is the context structure to be updated */
333 /* status is the DEC_ exception code */
334 /* returns the context structure */
336 /* No error is possible. */
337 /* ------------------------------------------------------------------ */
338 decContext
* decContextSetStatusQuiet(decContext
*context
, uInt status
) {
339 context
->status
|=status
;
340 return context
;} /* decContextSetStatusQuiet */
342 /* ------------------------------------------------------------------ */
343 /* decContextStatusToString -- convert status flags to a string */
345 /* context is a context with valid status field */
347 /* returns a constant string describing the condition. If multiple */
348 /* (or no) flags are set, a generic constant message is returned. */
349 /* ------------------------------------------------------------------ */
350 const char *decContextStatusToString(const decContext
*context
) {
351 Int status
=context
->status
;
353 /* test the five IEEE first, as some of the others are ambiguous when */
355 if (status
==DEC_Invalid_operation
) return DEC_Condition_IO
;
356 if (status
==DEC_Division_by_zero
) return DEC_Condition_DZ
;
357 if (status
==DEC_Overflow
) return DEC_Condition_OV
;
358 if (status
==DEC_Underflow
) return DEC_Condition_UN
;
359 if (status
==DEC_Inexact
) return DEC_Condition_IE
;
361 if (status
==DEC_Division_impossible
) return DEC_Condition_DI
;
362 if (status
==DEC_Division_undefined
) return DEC_Condition_DU
;
363 if (status
==DEC_Rounded
) return DEC_Condition_RO
;
364 if (status
==DEC_Clamped
) return DEC_Condition_PA
;
365 if (status
==DEC_Subnormal
) return DEC_Condition_SU
;
366 if (status
==DEC_Conversion_syntax
) return DEC_Condition_CS
;
367 if (status
==DEC_Insufficient_storage
) return DEC_Condition_IS
;
368 if (status
==DEC_Invalid_context
) return DEC_Condition_IC
;
370 if (status
==DEC_Lost_digits
) return DEC_Condition_LD
;
372 if (status
==0 ) return DEC_Condition_ZE
;
373 return DEC_Condition_MU
; /* Multiple errors */
374 } /* decContextStatusToString */
376 /* ------------------------------------------------------------------ */
377 /* decContextTestEndian -- test whether DECLITEND is set correctly */
379 /* quiet is 1 to suppress message; 0 otherwise */
380 /* returns 0 if DECLITEND is correct */
381 /* 1 if DECLITEND is incorrect and should be 1 */
382 /* -1 if DECLITEND is incorrect and should be 0 */
384 /* A message is displayed if the return value is not 0 and quiet==0. */
386 /* No error is possible. */
387 /* ------------------------------------------------------------------ */
388 Int
decContextTestEndian(Flag quiet
) {
389 Int res
=0; /* optimist */
390 uInt dle
=(uInt
)DECLITEND
; /* unsign */
391 if (dle
>1) dle
=1; /* ensure 0 or 1 */
393 if (LITEND
!=DECLITEND
) {
396 if (LITEND
) adj
="little";
398 printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
404 } /* decContextTestEndian */
406 /* ------------------------------------------------------------------ */
407 /* decContextTestSavedStatus -- test bits in saved status */
409 /* oldstatus is the status word to be tested */
410 /* mask indicates the bits to be tested (the oldstatus bits that */
411 /* correspond to each 1 bit in the mask are tested) */
412 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
414 /* No error is possible. */
415 /* ------------------------------------------------------------------ */
416 uInt
decContextTestSavedStatus(uInt oldstatus
, uInt mask
) {
417 return (oldstatus
&mask
)!=0;
418 } /* decContextTestSavedStatus */
420 /* ------------------------------------------------------------------ */
421 /* decContextTestStatus -- test bits in current status */
423 /* context is the context structure to be updated */
424 /* mask indicates the bits to be tested (the status bits that */
425 /* correspond to each 1 bit in the mask are tested) */
426 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
428 /* No error is possible. */
429 /* ------------------------------------------------------------------ */
430 uInt
decContextTestStatus(decContext
*context
, uInt mask
) {
431 return (context
->status
&mask
)!=0;
432 } /* decContextTestStatus */
434 /* ------------------------------------------------------------------ */
435 /* decContextZeroStatus -- clear all status bits */
437 /* context is the context structure to be updated */
438 /* returns context */
440 /* No error is possible. */
441 /* ------------------------------------------------------------------ */
442 decContext
*decContextZeroStatus(decContext
*context
) {
445 } /* decContextZeroStatus */
This page took 0.046215 seconds and 4 git commands to generate.