merge from gcc
[deliverable/binutils-gdb.git] / libdecnumber / decContext.c
1 /* Decimal context module for the decNumber C Library.
2 Copyright (C) 2005, 2007 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 2, or (at your option) any later
10 version.
11
12 In addition to the permissions in the GNU General Public License,
13 the Free Software Foundation gives you unlimited permission to link
14 the compiled version of this file into combinations with other
15 programs, and to distribute those combinations without any
16 restriction coming from the use of this file. (The General Public
17 License restrictions do apply in other respects; for example, they
18 cover modification of the file, and distribution when not linked
19 into a combine executable.)
20
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
30
31 /* ------------------------------------------------------------------ */
32 /* Decimal Context module */
33 /* ------------------------------------------------------------------ */
34 /* This module comprises the routines for handling arithmetic */
35 /* context structures. */
36 /* ------------------------------------------------------------------ */
37
38 #include <string.h> /* for strcmp */
39 #include <stdio.h> /* for printf if DECCHECK */
40 #include "dconfig.h" /* for GCC definitions */
41 #include "decContext.h" /* context and base types */
42 #include "decNumberLocal.h" /* decNumber local types, etc. */
43
44 /* compile-time endian tester [assumes sizeof(Int)>1] */
45 static const Int mfcone=1; /* constant 1 */
46 static const Flag *mfctop=(const Flag *)&mfcone; /* -> top byte */
47 #define LITEND *mfctop /* named flag; 1=little-endian */
48
49 /* ------------------------------------------------------------------ */
50 /* round-for-reround digits */
51 /* ------------------------------------------------------------------ */
52 const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
53
54 /* ------------------------------------------------------------------ */
55 /* Powers of ten (powers[n]==10**n, 0<=n<=9) */
56 /* ------------------------------------------------------------------ */
57 const uInt DECPOWERS[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
58 10000000, 100000000, 1000000000};
59
60 /* ------------------------------------------------------------------ */
61 /* decContextClearStatus -- clear bits in current status */
62 /* */
63 /* context is the context structure to be queried */
64 /* mask indicates the bits to be cleared (the status bit that */
65 /* corresponds to each 1 bit in the mask is cleared) */
66 /* returns context */
67 /* */
68 /* No error is possible. */
69 /* ------------------------------------------------------------------ */
70 decContext *decContextClearStatus(decContext *context, uInt mask) {
71 context->status&=~mask;
72 return context;
73 } /* decContextClearStatus */
74
75 /* ------------------------------------------------------------------ */
76 /* decContextDefault -- initialize a context structure */
77 /* */
78 /* context is the structure to be initialized */
79 /* kind selects the required set of default values, one of: */
80 /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
81 /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */
82 /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */
83 /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */
84 /* For any other value a valid context is returned, but with */
85 /* Invalid_operation set in the status field. */
86 /* returns a context structure with the appropriate initial values. */
87 /* ------------------------------------------------------------------ */
88 decContext * decContextDefault(decContext *context, Int kind) {
89 /* set defaults... */
90 context->digits=9; /* 9 digits */
91 context->emax=DEC_MAX_EMAX; /* 9-digit exponents */
92 context->emin=DEC_MIN_EMIN; /* .. balanced */
93 context->round=DEC_ROUND_HALF_UP; /* 0.5 rises */
94 context->traps=DEC_Errors; /* all but informational */
95 context->status=0; /* cleared */
96 context->clamp=0; /* no clamping */
97 #if DECSUBSET
98 context->extended=0; /* cleared */
99 #endif
100 switch (kind) {
101 case DEC_INIT_BASE:
102 /* [use defaults] */
103 break;
104 case DEC_INIT_DECIMAL32:
105 context->digits=7; /* digits */
106 context->emax=96; /* Emax */
107 context->emin=-95; /* Emin */
108 context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
109 context->traps=0; /* no traps set */
110 context->clamp=1; /* clamp exponents */
111 #if DECSUBSET
112 context->extended=1; /* set */
113 #endif
114 break;
115 case DEC_INIT_DECIMAL64:
116 context->digits=16; /* digits */
117 context->emax=384; /* Emax */
118 context->emin=-383; /* Emin */
119 context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
120 context->traps=0; /* no traps set */
121 context->clamp=1; /* clamp exponents */
122 #if DECSUBSET
123 context->extended=1; /* set */
124 #endif
125 break;
126 case DEC_INIT_DECIMAL128:
127 context->digits=34; /* digits */
128 context->emax=6144; /* Emax */
129 context->emin=-6143; /* Emin */
130 context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */
131 context->traps=0; /* no traps set */
132 context->clamp=1; /* clamp exponents */
133 #if DECSUBSET
134 context->extended=1; /* set */
135 #endif
136 break;
137
138 default: /* invalid Kind */
139 /* use defaults, and .. */
140 decContextSetStatus(context, DEC_Invalid_operation); /* trap */
141 }
142
143 return context;} /* decContextDefault */
144
145 /* ------------------------------------------------------------------ */
146 /* decContextGetRounding -- return current rounding mode */
147 /* */
148 /* context is the context structure to be queried */
149 /* returns the rounding mode */
150 /* */
151 /* No error is possible. */
152 /* ------------------------------------------------------------------ */
153 enum rounding decContextGetRounding(decContext *context) {
154 return context->round;
155 } /* decContextGetRounding */
156
157 /* ------------------------------------------------------------------ */
158 /* decContextGetStatus -- return current status */
159 /* */
160 /* context is the context structure to be queried */
161 /* returns status */
162 /* */
163 /* No error is possible. */
164 /* ------------------------------------------------------------------ */
165 uInt decContextGetStatus(decContext *context) {
166 return context->status;
167 } /* decContextGetStatus */
168
169 /* ------------------------------------------------------------------ */
170 /* decContextRestoreStatus -- restore bits in current status */
171 /* */
172 /* context is the context structure to be updated */
173 /* newstatus is the source for the bits to be restored */
174 /* mask indicates the bits to be restored (the status bit that */
175 /* corresponds to each 1 bit in the mask is set to the value of */
176 /* the correspnding bit in newstatus) */
177 /* returns context */
178 /* */
179 /* No error is possible. */
180 /* ------------------------------------------------------------------ */
181 decContext *decContextRestoreStatus(decContext *context,
182 uInt newstatus, uInt mask) {
183 context->status&=~mask; /* clear the selected bits */
184 context->status|=(mask&newstatus); /* or in the new bits */
185 return context;
186 } /* decContextRestoreStatus */
187
188 /* ------------------------------------------------------------------ */
189 /* decContextSaveStatus -- save bits in current status */
190 /* */
191 /* context is the context structure to be queried */
192 /* mask indicates the bits to be saved (the status bits that */
193 /* correspond to each 1 bit in the mask are saved) */
194 /* returns the AND of the mask and the current status */
195 /* */
196 /* No error is possible. */
197 /* ------------------------------------------------------------------ */
198 uInt decContextSaveStatus(decContext *context, uInt mask) {
199 return context->status&mask;
200 } /* decContextSaveStatus */
201
202 /* ------------------------------------------------------------------ */
203 /* decContextSetRounding -- set current rounding mode */
204 /* */
205 /* context is the context structure to be updated */
206 /* newround is the value which will replace the current mode */
207 /* returns context */
208 /* */
209 /* No error is possible. */
210 /* ------------------------------------------------------------------ */
211 decContext *decContextSetRounding(decContext *context,
212 enum rounding newround) {
213 context->round=newround;
214 return context;
215 } /* decContextSetRounding */
216
217 /* ------------------------------------------------------------------ */
218 /* decContextSetStatus -- set status and raise trap if appropriate */
219 /* */
220 /* context is the context structure to be updated */
221 /* status is the DEC_ exception code */
222 /* returns the context structure */
223 /* */
224 /* Control may never return from this routine, if there is a signal */
225 /* handler and it takes a long jump. */
226 /* ------------------------------------------------------------------ */
227 decContext * decContextSetStatus(decContext *context, uInt status) {
228 context->status|=status;
229 if (status & context->traps) raise(SIGFPE);
230 return context;} /* decContextSetStatus */
231
232 /* ------------------------------------------------------------------ */
233 /* decContextSetStatusFromString -- set status from a string + trap */
234 /* */
235 /* context is the context structure to be updated */
236 /* string is a string exactly equal to one that might be returned */
237 /* by decContextStatusToString */
238 /* */
239 /* The status bit corresponding to the string is set, and a trap */
240 /* is raised if appropriate. */
241 /* */
242 /* returns the context structure, unless the string is equal to */
243 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
244 /* returned. */
245 /* ------------------------------------------------------------------ */
246 decContext * decContextSetStatusFromString(decContext *context,
247 const char *string) {
248 if (strcmp(string, DEC_Condition_CS)==0)
249 return decContextSetStatus(context, DEC_Conversion_syntax);
250 if (strcmp(string, DEC_Condition_DZ)==0)
251 return decContextSetStatus(context, DEC_Division_by_zero);
252 if (strcmp(string, DEC_Condition_DI)==0)
253 return decContextSetStatus(context, DEC_Division_impossible);
254 if (strcmp(string, DEC_Condition_DU)==0)
255 return decContextSetStatus(context, DEC_Division_undefined);
256 if (strcmp(string, DEC_Condition_IE)==0)
257 return decContextSetStatus(context, DEC_Inexact);
258 if (strcmp(string, DEC_Condition_IS)==0)
259 return decContextSetStatus(context, DEC_Insufficient_storage);
260 if (strcmp(string, DEC_Condition_IC)==0)
261 return decContextSetStatus(context, DEC_Invalid_context);
262 if (strcmp(string, DEC_Condition_IO)==0)
263 return decContextSetStatus(context, DEC_Invalid_operation);
264 #if DECSUBSET
265 if (strcmp(string, DEC_Condition_LD)==0)
266 return decContextSetStatus(context, DEC_Lost_digits);
267 #endif
268 if (strcmp(string, DEC_Condition_OV)==0)
269 return decContextSetStatus(context, DEC_Overflow);
270 if (strcmp(string, DEC_Condition_PA)==0)
271 return decContextSetStatus(context, DEC_Clamped);
272 if (strcmp(string, DEC_Condition_RO)==0)
273 return decContextSetStatus(context, DEC_Rounded);
274 if (strcmp(string, DEC_Condition_SU)==0)
275 return decContextSetStatus(context, DEC_Subnormal);
276 if (strcmp(string, DEC_Condition_UN)==0)
277 return decContextSetStatus(context, DEC_Underflow);
278 if (strcmp(string, DEC_Condition_ZE)==0)
279 return context;
280 return NULL; /* Multiple status, or unknown */
281 } /* decContextSetStatusFromString */
282
283 /* ------------------------------------------------------------------ */
284 /* decContextSetStatusFromStringQuiet -- set status from a string */
285 /* */
286 /* context is the context structure to be updated */
287 /* string is a string exactly equal to one that might be returned */
288 /* by decContextStatusToString */
289 /* */
290 /* The status bit corresponding to the string is set; no trap is */
291 /* raised. */
292 /* */
293 /* returns the context structure, unless the string is equal to */
294 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
295 /* returned. */
296 /* ------------------------------------------------------------------ */
297 decContext * decContextSetStatusFromStringQuiet(decContext *context,
298 const char *string) {
299 if (strcmp(string, DEC_Condition_CS)==0)
300 return decContextSetStatusQuiet(context, DEC_Conversion_syntax);
301 if (strcmp(string, DEC_Condition_DZ)==0)
302 return decContextSetStatusQuiet(context, DEC_Division_by_zero);
303 if (strcmp(string, DEC_Condition_DI)==0)
304 return decContextSetStatusQuiet(context, DEC_Division_impossible);
305 if (strcmp(string, DEC_Condition_DU)==0)
306 return decContextSetStatusQuiet(context, DEC_Division_undefined);
307 if (strcmp(string, DEC_Condition_IE)==0)
308 return decContextSetStatusQuiet(context, DEC_Inexact);
309 if (strcmp(string, DEC_Condition_IS)==0)
310 return decContextSetStatusQuiet(context, DEC_Insufficient_storage);
311 if (strcmp(string, DEC_Condition_IC)==0)
312 return decContextSetStatusQuiet(context, DEC_Invalid_context);
313 if (strcmp(string, DEC_Condition_IO)==0)
314 return decContextSetStatusQuiet(context, DEC_Invalid_operation);
315 #if DECSUBSET
316 if (strcmp(string, DEC_Condition_LD)==0)
317 return decContextSetStatusQuiet(context, DEC_Lost_digits);
318 #endif
319 if (strcmp(string, DEC_Condition_OV)==0)
320 return decContextSetStatusQuiet(context, DEC_Overflow);
321 if (strcmp(string, DEC_Condition_PA)==0)
322 return decContextSetStatusQuiet(context, DEC_Clamped);
323 if (strcmp(string, DEC_Condition_RO)==0)
324 return decContextSetStatusQuiet(context, DEC_Rounded);
325 if (strcmp(string, DEC_Condition_SU)==0)
326 return decContextSetStatusQuiet(context, DEC_Subnormal);
327 if (strcmp(string, DEC_Condition_UN)==0)
328 return decContextSetStatusQuiet(context, DEC_Underflow);
329 if (strcmp(string, DEC_Condition_ZE)==0)
330 return context;
331 return NULL; /* Multiple status, or unknown */
332 } /* decContextSetStatusFromStringQuiet */
333
334 /* ------------------------------------------------------------------ */
335 /* decContextSetStatusQuiet -- set status without trap */
336 /* */
337 /* context is the context structure to be updated */
338 /* status is the DEC_ exception code */
339 /* returns the context structure */
340 /* */
341 /* No error is possible. */
342 /* ------------------------------------------------------------------ */
343 decContext * decContextSetStatusQuiet(decContext *context, uInt status) {
344 context->status|=status;
345 return context;} /* decContextSetStatusQuiet */
346
347 /* ------------------------------------------------------------------ */
348 /* decContextStatusToString -- convert status flags to a string */
349 /* */
350 /* context is a context with valid status field */
351 /* */
352 /* returns a constant string describing the condition. If multiple */
353 /* (or no) flags are set, a generic constant message is returned. */
354 /* ------------------------------------------------------------------ */
355 const char *decContextStatusToString(const decContext *context) {
356 Int status=context->status;
357
358 /* test the five IEEE first, as some of the others are ambiguous when */
359 /* DECEXTFLAG=0 */
360 if (status==DEC_Invalid_operation ) return DEC_Condition_IO;
361 if (status==DEC_Division_by_zero ) return DEC_Condition_DZ;
362 if (status==DEC_Overflow ) return DEC_Condition_OV;
363 if (status==DEC_Underflow ) return DEC_Condition_UN;
364 if (status==DEC_Inexact ) return DEC_Condition_IE;
365
366 if (status==DEC_Division_impossible ) return DEC_Condition_DI;
367 if (status==DEC_Division_undefined ) return DEC_Condition_DU;
368 if (status==DEC_Rounded ) return DEC_Condition_RO;
369 if (status==DEC_Clamped ) return DEC_Condition_PA;
370 if (status==DEC_Subnormal ) return DEC_Condition_SU;
371 if (status==DEC_Conversion_syntax ) return DEC_Condition_CS;
372 if (status==DEC_Insufficient_storage ) return DEC_Condition_IS;
373 if (status==DEC_Invalid_context ) return DEC_Condition_IC;
374 #if DECSUBSET
375 if (status==DEC_Lost_digits ) return DEC_Condition_LD;
376 #endif
377 if (status==0 ) return DEC_Condition_ZE;
378 return DEC_Condition_MU; /* Multiple errors */
379 } /* decContextStatusToString */
380
381 /* ------------------------------------------------------------------ */
382 /* decContextTestEndian -- test whether DECLITEND is set correctly */
383 /* */
384 /* quiet is 1 to suppress message; 0 otherwise */
385 /* returns 0 if DECLITEND is correct */
386 /* 1 if DECLITEND is incorrect and should be 1 */
387 /* -1 if DECLITEND is incorrect and should be 0 */
388 /* */
389 /* A message is displayed if the return value is not 0 and quiet==0. */
390 /* */
391 /* No error is possible. */
392 /* ------------------------------------------------------------------ */
393 Int decContextTestEndian(Flag quiet) {
394 Int res=0; /* optimist */
395 uInt dle=(uInt)DECLITEND; /* unsign */
396 if (dle>1) dle=1; /* ensure 0 or 1 */
397
398 if (LITEND!=DECLITEND) {
399 const char *adj;
400 if (!quiet) {
401 if (LITEND) adj="little";
402 else adj="big";
403 printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
404 DECLITEND, adj);
405 }
406 res=(Int)LITEND-dle;
407 }
408 return res;
409 } /* decContextTestEndian */
410
411 /* ------------------------------------------------------------------ */
412 /* decContextTestSavedStatus -- test bits in saved status */
413 /* */
414 /* oldstatus is the status word to be tested */
415 /* mask indicates the bits to be tested (the oldstatus bits that */
416 /* correspond to each 1 bit in the mask are tested) */
417 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
418 /* */
419 /* No error is possible. */
420 /* ------------------------------------------------------------------ */
421 uInt decContextTestSavedStatus(uInt oldstatus, uInt mask) {
422 return (oldstatus&mask)!=0;
423 } /* decContextTestSavedStatus */
424
425 /* ------------------------------------------------------------------ */
426 /* decContextTestStatus -- test bits in current status */
427 /* */
428 /* context is the context structure to be updated */
429 /* mask indicates the bits to be tested (the status bits that */
430 /* correspond to each 1 bit in the mask are tested) */
431 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
432 /* */
433 /* No error is possible. */
434 /* ------------------------------------------------------------------ */
435 uInt decContextTestStatus(decContext *context, uInt mask) {
436 return (context->status&mask)!=0;
437 } /* decContextTestStatus */
438
439 /* ------------------------------------------------------------------ */
440 /* decContextZeroStatus -- clear all status bits */
441 /* */
442 /* context is the context structure to be updated */
443 /* returns context */
444 /* */
445 /* No error is possible. */
446 /* ------------------------------------------------------------------ */
447 decContext *decContextZeroStatus(decContext *context) {
448 context->status=0;
449 return context;
450 } /* decContextZeroStatus */
451
This page took 0.048976 seconds and 4 git commands to generate.