comparison libdecnumber/decContext.c @ 0:a06113de4d67

first commit
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2009 14:47:48 +0900
parents
children 77e2b8dfacca
comparison
equal deleted inserted replaced
-1:000000000000 0:a06113de4d67
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.
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
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/>. */
25
26 /* ------------------------------------------------------------------ */
27 /* Decimal Context module */
28 /* ------------------------------------------------------------------ */
29 /* This module comprises the routines for handling arithmetic */
30 /* context structures. */
31 /* ------------------------------------------------------------------ */
32
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. */
38
39 #if DECCHECK
40 /* compile-time endian tester [assumes sizeof(Int)>1] */
41 static const Int mfcone=1; /* constant 1 */
42 static const Flag *mfctop=(Flag *)&mfcone; /* -> top byte */
43 #define LITEND *mfctop /* named flag; 1=little-endian */
44 #endif
45
46 /* ------------------------------------------------------------------ */
47 /* round-for-reround digits */
48 /* ------------------------------------------------------------------ */
49 const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
50
51 /* ------------------------------------------------------------------ */
52 /* Powers of ten (powers[n]==10**n, 0<=n<=9) */
53 /* ------------------------------------------------------------------ */
54 const uInt DECPOWERS[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
55 10000000, 100000000, 1000000000};
56
57 /* ------------------------------------------------------------------ */
58 /* decContextClearStatus -- clear bits in current status */
59 /* */
60 /* context is the context structure to be queried */
61 /* mask indicates the bits to be cleared (the status bit that */
62 /* corresponds to each 1 bit in the mask is cleared) */
63 /* returns context */
64 /* */
65 /* No error is possible. */
66 /* ------------------------------------------------------------------ */
67 decContext *decContextClearStatus(decContext *context, uInt mask) {
68 context->status&=~mask;
69 return context;
70 } /* decContextClearStatus */
71
72 /* ------------------------------------------------------------------ */
73 /* decContextDefault -- initialize a context structure */
74 /* */
75 /* context is the structure to be initialized */
76 /* kind selects the required set of default values, one of: */
77 /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
78 /* DEC_INIT_DECIMAL32 -- select IEEE 754r defaults, 32-bit */
79 /* DEC_INIT_DECIMAL64 -- select IEEE 754r defaults, 64-bit */
80 /* DEC_INIT_DECIMAL128 -- select IEEE 754r defaults, 128-bit */
81 /* For any other value a valid context is returned, but with */
82 /* Invalid_operation set in the status field. */
83 /* returns a context structure with the appropriate initial values. */
84 /* ------------------------------------------------------------------ */
85 decContext * decContextDefault(decContext *context, Int kind) {
86 /* set defaults... */
87 context->digits=9; /* 9 digits */
88 context->emax=DEC_MAX_EMAX; /* 9-digit exponents */
89 context->emin=DEC_MIN_EMIN; /* .. balanced */
90 context->round=DEC_ROUND_HALF_UP; /* 0.5 rises */
91 context->traps=DEC_Errors; /* all but informational */
92 context->status=0; /* cleared */
93 context->clamp=0; /* no clamping */
94 #if DECSUBSET
95 context->extended=0; /* cleared */
96 #endif
97 switch (kind) {
98 case DEC_INIT_BASE:
99 /* [use defaults] */
100 break;
101 case DEC_INIT_DECIMAL32:
102 context->digits=7; /* digits */
103 context->emax=96; /* Emax */
104 context->emin=-95; /* Emin */
105 context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
106 context->traps=0; /* no traps set */
107 context->clamp=1; /* clamp exponents */
108 #if DECSUBSET
109 context->extended=1; /* set */
110 #endif
111 break;
112 case DEC_INIT_DECIMAL64:
113 context->digits=16; /* digits */
114 context->emax=384; /* Emax */
115 context->emin=-383; /* Emin */
116 context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
117 context->traps=0; /* no traps set */
118 context->clamp=1; /* clamp exponents */
119 #if DECSUBSET
120 context->extended=1; /* set */
121 #endif
122 break;
123 case DEC_INIT_DECIMAL128:
124 context->digits=34; /* digits */
125 context->emax=6144; /* Emax */
126 context->emin=-6143; /* Emin */
127 context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
128 context->traps=0; /* no traps set */
129 context->clamp=1; /* clamp exponents */
130 #if DECSUBSET
131 context->extended=1; /* set */
132 #endif
133 break;
134
135 default: /* invalid Kind */
136 /* use defaults, and .. */
137 decContextSetStatus(context, DEC_Invalid_operation); /* trap */
138 }
139
140 #if DECCHECK
141 if (LITEND!=DECLITEND) {
142 const char *adj;
143 if (LITEND) adj="little";
144 else adj="big";
145 printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
146 DECLITEND, adj);
147 }
148 #endif
149 return context;} /* decContextDefault */
150
151 /* ------------------------------------------------------------------ */
152 /* decContextGetRounding -- return current rounding mode */
153 /* */
154 /* context is the context structure to be queried */
155 /* returns the rounding mode */
156 /* */
157 /* No error is possible. */
158 /* ------------------------------------------------------------------ */
159 enum rounding decContextGetRounding(decContext *context) {
160 return context->round;
161 } /* decContextGetRounding */
162
163 /* ------------------------------------------------------------------ */
164 /* decContextGetStatus -- return current status */
165 /* */
166 /* context is the context structure to be queried */
167 /* returns status */
168 /* */
169 /* No error is possible. */
170 /* ------------------------------------------------------------------ */
171 uInt decContextGetStatus(decContext *context) {
172 return context->status;
173 } /* decContextGetStatus */
174
175 /* ------------------------------------------------------------------ */
176 /* decContextRestoreStatus -- restore bits in current status */
177 /* */
178 /* context is the context structure to be updated */
179 /* newstatus is the source for the bits to be restored */
180 /* mask indicates the bits to be restored (the status bit that */
181 /* corresponds to each 1 bit in the mask is set to the value of */
182 /* the correspnding bit in newstatus) */
183 /* returns context */
184 /* */
185 /* No error is possible. */
186 /* ------------------------------------------------------------------ */
187 decContext *decContextRestoreStatus(decContext *context,
188 uInt newstatus, uInt mask) {
189 context->status&=~mask; /* clear the selected bits */
190 context->status|=(mask&newstatus); /* or in the new bits */
191 return context;
192 } /* decContextRestoreStatus */
193
194 /* ------------------------------------------------------------------ */
195 /* decContextSaveStatus -- save bits in current status */
196 /* */
197 /* context is the context structure to be queried */
198 /* mask indicates the bits to be saved (the status bits that */
199 /* correspond to each 1 bit in the mask are saved) */
200 /* returns the AND of the mask and the current status */
201 /* */
202 /* No error is possible. */
203 /* ------------------------------------------------------------------ */
204 uInt decContextSaveStatus(decContext *context, uInt mask) {
205 return context->status&mask;
206 } /* decContextSaveStatus */
207
208 /* ------------------------------------------------------------------ */
209 /* decContextSetRounding -- set current rounding mode */
210 /* */
211 /* context is the context structure to be updated */
212 /* newround is the value which will replace the current mode */
213 /* returns context */
214 /* */
215 /* No error is possible. */
216 /* ------------------------------------------------------------------ */
217 decContext *decContextSetRounding(decContext *context,
218 enum rounding newround) {
219 context->round=newround;
220 return context;
221 } /* decContextSetRounding */
222
223 /* ------------------------------------------------------------------ */
224 /* decContextSetStatus -- set status and raise trap if appropriate */
225 /* */
226 /* context is the context structure to be updated */
227 /* status is the DEC_ exception code */
228 /* returns the context structure */
229 /* */
230 /* Control may never return from this routine, if there is a signal */
231 /* handler and it takes a long jump. */
232 /* ------------------------------------------------------------------ */
233 decContext * decContextSetStatus(decContext *context, uInt status) {
234 context->status|=status;
235 if (status & context->traps) raise(SIGFPE);
236 return context;} /* decContextSetStatus */
237
238 /* ------------------------------------------------------------------ */
239 /* decContextSetStatusFromString -- set status from a string + trap */
240 /* */
241 /* context is the context structure to be updated */
242 /* string is a string exactly equal to one that might be returned */
243 /* by decContextStatusToString */
244 /* */
245 /* The status bit corresponding to the string is set, and a trap */
246 /* is raised if appropriate. */
247 /* */
248 /* returns the context structure, unless the string is equal to */
249 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
250 /* returned. */
251 /* ------------------------------------------------------------------ */
252 decContext * decContextSetStatusFromString(decContext *context,
253 const char *string) {
254 if (strcmp(string, DEC_Condition_CS)==0)
255 return decContextSetStatus(context, DEC_Conversion_syntax);
256 if (strcmp(string, DEC_Condition_DZ)==0)
257 return decContextSetStatus(context, DEC_Division_by_zero);
258 if (strcmp(string, DEC_Condition_DI)==0)
259 return decContextSetStatus(context, DEC_Division_impossible);
260 if (strcmp(string, DEC_Condition_DU)==0)
261 return decContextSetStatus(context, DEC_Division_undefined);
262 if (strcmp(string, DEC_Condition_IE)==0)
263 return decContextSetStatus(context, DEC_Inexact);
264 if (strcmp(string, DEC_Condition_IS)==0)
265 return decContextSetStatus(context, DEC_Insufficient_storage);
266 if (strcmp(string, DEC_Condition_IC)==0)
267 return decContextSetStatus(context, DEC_Invalid_context);
268 if (strcmp(string, DEC_Condition_IO)==0)
269 return decContextSetStatus(context, DEC_Invalid_operation);
270 #if DECSUBSET
271 if (strcmp(string, DEC_Condition_LD)==0)
272 return decContextSetStatus(context, DEC_Lost_digits);
273 #endif
274 if (strcmp(string, DEC_Condition_OV)==0)
275 return decContextSetStatus(context, DEC_Overflow);
276 if (strcmp(string, DEC_Condition_PA)==0)
277 return decContextSetStatus(context, DEC_Clamped);
278 if (strcmp(string, DEC_Condition_RO)==0)
279 return decContextSetStatus(context, DEC_Rounded);
280 if (strcmp(string, DEC_Condition_SU)==0)
281 return decContextSetStatus(context, DEC_Subnormal);
282 if (strcmp(string, DEC_Condition_UN)==0)
283 return decContextSetStatus(context, DEC_Underflow);
284 if (strcmp(string, DEC_Condition_ZE)==0)
285 return context;
286 return NULL; /* Multiple status, or unknown */
287 } /* decContextSetStatusFromString */
288
289 /* ------------------------------------------------------------------ */
290 /* decContextSetStatusFromStringQuiet -- set status from a string */
291 /* */
292 /* context is the context structure to be updated */
293 /* string is a string exactly equal to one that might be returned */
294 /* by decContextStatusToString */
295 /* */
296 /* The status bit corresponding to the string is set; no trap is */
297 /* raised. */
298 /* */
299 /* returns the context structure, unless the string is equal to */
300 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
301 /* returned. */
302 /* ------------------------------------------------------------------ */
303 decContext * decContextSetStatusFromStringQuiet(decContext *context,
304 const char *string) {
305 if (strcmp(string, DEC_Condition_CS)==0)
306 return decContextSetStatusQuiet(context, DEC_Conversion_syntax);
307 if (strcmp(string, DEC_Condition_DZ)==0)
308 return decContextSetStatusQuiet(context, DEC_Division_by_zero);
309 if (strcmp(string, DEC_Condition_DI)==0)
310 return decContextSetStatusQuiet(context, DEC_Division_impossible);
311 if (strcmp(string, DEC_Condition_DU)==0)
312 return decContextSetStatusQuiet(context, DEC_Division_undefined);
313 if (strcmp(string, DEC_Condition_IE)==0)
314 return decContextSetStatusQuiet(context, DEC_Inexact);
315 if (strcmp(string, DEC_Condition_IS)==0)
316 return decContextSetStatusQuiet(context, DEC_Insufficient_storage);
317 if (strcmp(string, DEC_Condition_IC)==0)
318 return decContextSetStatusQuiet(context, DEC_Invalid_context);
319 if (strcmp(string, DEC_Condition_IO)==0)
320 return decContextSetStatusQuiet(context, DEC_Invalid_operation);
321 #if DECSUBSET
322 if (strcmp(string, DEC_Condition_LD)==0)
323 return decContextSetStatusQuiet(context, DEC_Lost_digits);
324 #endif
325 if (strcmp(string, DEC_Condition_OV)==0)
326 return decContextSetStatusQuiet(context, DEC_Overflow);
327 if (strcmp(string, DEC_Condition_PA)==0)
328 return decContextSetStatusQuiet(context, DEC_Clamped);
329 if (strcmp(string, DEC_Condition_RO)==0)
330 return decContextSetStatusQuiet(context, DEC_Rounded);
331 if (strcmp(string, DEC_Condition_SU)==0)
332 return decContextSetStatusQuiet(context, DEC_Subnormal);
333 if (strcmp(string, DEC_Condition_UN)==0)
334 return decContextSetStatusQuiet(context, DEC_Underflow);
335 if (strcmp(string, DEC_Condition_ZE)==0)
336 return context;
337 return NULL; /* Multiple status, or unknown */
338 } /* decContextSetStatusFromStringQuiet */
339
340 /* ------------------------------------------------------------------ */
341 /* decContextSetStatusQuiet -- set status without trap */
342 /* */
343 /* context is the context structure to be updated */
344 /* status is the DEC_ exception code */
345 /* returns the context structure */
346 /* */
347 /* No error is possible. */
348 /* ------------------------------------------------------------------ */
349 decContext * decContextSetStatusQuiet(decContext *context, uInt status) {
350 context->status|=status;
351 return context;} /* decContextSetStatusQuiet */
352
353 /* ------------------------------------------------------------------ */
354 /* decContextStatusToString -- convert status flags to a string */
355 /* */
356 /* context is a context with valid status field */
357 /* */
358 /* returns a constant string describing the condition. If multiple */
359 /* (or no) flags are set, a generic constant message is returned. */
360 /* ------------------------------------------------------------------ */
361 const char *decContextStatusToString(const decContext *context) {
362 Int status=context->status;
363
364 /* test the five IEEE first, as some of the others are ambiguous when */
365 /* DECEXTFLAG=0 */
366 if (status==DEC_Invalid_operation ) return DEC_Condition_IO;
367 if (status==DEC_Division_by_zero ) return DEC_Condition_DZ;
368 if (status==DEC_Overflow ) return DEC_Condition_OV;
369 if (status==DEC_Underflow ) return DEC_Condition_UN;
370 if (status==DEC_Inexact ) return DEC_Condition_IE;
371
372 if (status==DEC_Division_impossible ) return DEC_Condition_DI;
373 if (status==DEC_Division_undefined ) return DEC_Condition_DU;
374 if (status==DEC_Rounded ) return DEC_Condition_RO;
375 if (status==DEC_Clamped ) return DEC_Condition_PA;
376 if (status==DEC_Subnormal ) return DEC_Condition_SU;
377 if (status==DEC_Conversion_syntax ) return DEC_Condition_CS;
378 if (status==DEC_Insufficient_storage ) return DEC_Condition_IS;
379 if (status==DEC_Invalid_context ) return DEC_Condition_IC;
380 #if DECSUBSET
381 if (status==DEC_Lost_digits ) return DEC_Condition_LD;
382 #endif
383 if (status==0 ) return DEC_Condition_ZE;
384 return DEC_Condition_MU; /* Multiple errors */
385 } /* decContextStatusToString */
386
387 /* ------------------------------------------------------------------ */
388 /* decContextTestSavedStatus -- test bits in saved status */
389 /* */
390 /* oldstatus is the status word to be tested */
391 /* mask indicates the bits to be tested (the oldstatus bits that */
392 /* correspond to each 1 bit in the mask are tested) */
393 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
394 /* */
395 /* No error is possible. */
396 /* ------------------------------------------------------------------ */
397 uInt decContextTestSavedStatus(uInt oldstatus, uInt mask) {
398 return (oldstatus&mask)!=0;
399 } /* decContextTestSavedStatus */
400
401 /* ------------------------------------------------------------------ */
402 /* decContextTestStatus -- test bits in current status */
403 /* */
404 /* context is the context structure to be updated */
405 /* mask indicates the bits to be tested (the status bits that */
406 /* correspond to each 1 bit in the mask are tested) */
407 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
408 /* */
409 /* No error is possible. */
410 /* ------------------------------------------------------------------ */
411 uInt decContextTestStatus(decContext *context, uInt mask) {
412 return (context->status&mask)!=0;
413 } /* decContextTestStatus */
414
415 /* ------------------------------------------------------------------ */
416 /* decContextZeroStatus -- clear all status bits */
417 /* */
418 /* context is the context structure to be updated */
419 /* returns context */
420 /* */
421 /* No error is possible. */
422 /* ------------------------------------------------------------------ */
423 decContext *decContextZeroStatus(decContext *context) {
424 context->status=0;
425 return context;
426 } /* decContextZeroStatus */
427