]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/decContext.c
1 /* ------------------------------------------------------------------ */
2 /* Decimal Context module */
3 /* ------------------------------------------------------------------ */
4 /* Copyright (c) IBM Corporation, 2000-2011. All rights reserved. */
6 /* This software is made available under the terms of the */
7 /* ICU License -- ICU 1.8.1 and later. */
9 /* The description and User's Guide ("The decNumber C Library") for */
10 /* this software is called decNumber.pdf. This document is */
11 /* available, together with arithmetic and format specifications, */
12 /* testcases, and Web links, on the General Decimal Arithmetic page. */
14 /* Please send comments, suggestions, and corrections to the author: */
16 /* Mike Cowlishaw, IBM Fellow */
17 /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
18 /* ------------------------------------------------------------------ */
19 /* This module comprises the routines for handling arithmetic */
20 /* context structures. */
21 /* ------------------------------------------------------------------ */
23 #include <string.h> /* for strcmp */
24 #include <stdio.h> /* for printf if DECCHECK */
25 #include "decContext.h" /* context and base types */
26 #include "decNumberLocal.h" /* decNumber local types, etc. */
28 #if 0 /* ICU: No need to test endianness at runtime. */
29 /* compile-time endian tester [assumes sizeof(Int)>1] */
30 static const Int mfcone
=1; /* constant 1 */
31 static const Flag
*mfctop
=(Flag
*)&mfcone
; /* -> top byte */
32 #define LITEND *mfctop /* named flag; 1=little-endian */
35 /* ------------------------------------------------------------------ */
36 /* round-for-reround digits */
37 /* ------------------------------------------------------------------ */
38 const uByte DECSTICKYTAB
[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
40 /* ------------------------------------------------------------------ */
41 /* Powers of ten (powers[n]==10**n, 0<=n<=9) */
42 /* ------------------------------------------------------------------ */
43 const uInt DECPOWERS
[10]={1, 10, 100, 1000, 10000, 100000, 1000000,
44 10000000, 100000000, 1000000000};
46 /* ------------------------------------------------------------------ */
47 /* decContextClearStatus -- clear bits in current status */
49 /* context is the context structure to be queried */
50 /* mask indicates the bits to be cleared (the status bit that */
51 /* corresponds to each 1 bit in the mask is cleared) */
54 /* No error is possible. */
55 /* ------------------------------------------------------------------ */
56 U_CAPI decContext
* U_EXPORT2
uprv_decContextClearStatus(decContext
*context
, uInt mask
) {
57 context
->status
&=~mask
;
59 } /* decContextClearStatus */
61 /* ------------------------------------------------------------------ */
62 /* decContextDefault -- initialize a context structure */
64 /* context is the structure to be initialized */
65 /* kind selects the required set of default values, one of: */
66 /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
67 /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */
68 /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */
69 /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */
70 /* For any other value a valid context is returned, but with */
71 /* Invalid_operation set in the status field. */
72 /* returns a context structure with the appropriate initial values. */
73 /* ------------------------------------------------------------------ */
74 U_CAPI decContext
* U_EXPORT2
uprv_decContextDefault(decContext
*context
, Int kind
) {
76 context
->digits
=9; /* 9 digits */
77 context
->emax
=DEC_MAX_EMAX
; /* 9-digit exponents */
78 context
->emin
=DEC_MIN_EMIN
; /* .. balanced */
79 context
->round
=DEC_ROUND_HALF_UP
; /* 0.5 rises */
80 context
->traps
=DEC_Errors
; /* all but informational */
81 context
->status
=0; /* cleared */
82 context
->clamp
=0; /* no clamping */
84 context
->extended
=0; /* cleared */
90 case DEC_INIT_DECIMAL32
:
91 context
->digits
=7; /* digits */
92 context
->emax
=96; /* Emax */
93 context
->emin
=-95; /* Emin */
94 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
95 context
->traps
=0; /* no traps set */
96 context
->clamp
=1; /* clamp exponents */
98 context
->extended
=1; /* set */
101 case DEC_INIT_DECIMAL64
:
102 context
->digits
=16; /* digits */
103 context
->emax
=384; /* Emax */
104 context
->emin
=-383; /* 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 */
109 context
->extended
=1; /* set */
112 case DEC_INIT_DECIMAL128
:
113 context
->digits
=34; /* digits */
114 context
->emax
=6144; /* Emax */
115 context
->emin
=-6143; /* 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 */
120 context
->extended
=1; /* set */
124 default: /* invalid Kind */
125 /* use defaults, and .. */
126 uprv_decContextSetStatus(context
, DEC_Invalid_operation
); /* trap */
129 return context
;} /* decContextDefault */
131 /* ------------------------------------------------------------------ */
132 /* decContextGetRounding -- return current rounding mode */
134 /* context is the context structure to be queried */
135 /* returns the rounding mode */
137 /* No error is possible. */
138 /* ------------------------------------------------------------------ */
139 U_CAPI
enum rounding U_EXPORT2
uprv_decContextGetRounding(decContext
*context
) {
140 return context
->round
;
141 } /* decContextGetRounding */
143 /* ------------------------------------------------------------------ */
144 /* decContextGetStatus -- return current status */
146 /* context is the context structure to be queried */
149 /* No error is possible. */
150 /* ------------------------------------------------------------------ */
151 U_CAPI uInt U_EXPORT2
uprv_decContextGetStatus(decContext
*context
) {
152 return context
->status
;
153 } /* decContextGetStatus */
155 /* ------------------------------------------------------------------ */
156 /* decContextRestoreStatus -- restore bits in current status */
158 /* context is the context structure to be updated */
159 /* newstatus is the source for the bits to be restored */
160 /* mask indicates the bits to be restored (the status bit that */
161 /* corresponds to each 1 bit in the mask is set to the value of */
162 /* the correspnding bit in newstatus) */
163 /* returns context */
165 /* No error is possible. */
166 /* ------------------------------------------------------------------ */
167 U_CAPI decContext
* U_EXPORT2
uprv_decContextRestoreStatus(decContext
*context
,
168 uInt newstatus
, uInt mask
) {
169 context
->status
&=~mask
; /* clear the selected bits */
170 context
->status
|=(mask
&newstatus
); /* or in the new bits */
172 } /* decContextRestoreStatus */
174 /* ------------------------------------------------------------------ */
175 /* decContextSaveStatus -- save bits in current status */
177 /* context is the context structure to be queried */
178 /* mask indicates the bits to be saved (the status bits that */
179 /* correspond to each 1 bit in the mask are saved) */
180 /* returns the AND of the mask and the current status */
182 /* No error is possible. */
183 /* ------------------------------------------------------------------ */
184 U_CAPI uInt U_EXPORT2
uprv_decContextSaveStatus(decContext
*context
, uInt mask
) {
185 return context
->status
&mask
;
186 } /* decContextSaveStatus */
188 /* ------------------------------------------------------------------ */
189 /* decContextSetRounding -- set current rounding mode */
191 /* context is the context structure to be updated */
192 /* newround is the value which will replace the current mode */
193 /* returns context */
195 /* No error is possible. */
196 /* ------------------------------------------------------------------ */
197 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetRounding(decContext
*context
,
198 enum rounding newround
) {
199 context
->round
=newround
;
201 } /* decContextSetRounding */
203 /* ------------------------------------------------------------------ */
204 /* decContextSetStatus -- set status and raise trap if appropriate */
206 /* context is the context structure to be updated */
207 /* status is the DEC_ exception code */
208 /* returns the context structure */
210 /* Control may never return from this routine, if there is a signal */
211 /* handler and it takes a long jump. */
212 /* ------------------------------------------------------------------ */
213 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatus(decContext
*context
, uInt status
) {
214 context
->status
|=status
;
215 #if 0 /* ICU: Do not raise signals. */
216 if (status
& context
->traps
) raise(SIGFPE
);
218 return context
;} /* decContextSetStatus */
220 /* ------------------------------------------------------------------ */
221 /* decContextSetStatusFromString -- set status from a string + trap */
223 /* context is the context structure to be updated */
224 /* string is a string exactly equal to one that might be returned */
225 /* by decContextStatusToString */
227 /* The status bit corresponding to the string is set, and a trap */
228 /* is raised if appropriate. */
230 /* returns the context structure, unless the string is equal to */
231 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
233 /* ------------------------------------------------------------------ */
234 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatusFromString(decContext
*context
,
235 const char *string
) {
236 if (strcmp(string
, DEC_Condition_CS
)==0)
237 return uprv_decContextSetStatus(context
, DEC_Conversion_syntax
);
238 if (strcmp(string
, DEC_Condition_DZ
)==0)
239 return uprv_decContextSetStatus(context
, DEC_Division_by_zero
);
240 if (strcmp(string
, DEC_Condition_DI
)==0)
241 return uprv_decContextSetStatus(context
, DEC_Division_impossible
);
242 if (strcmp(string
, DEC_Condition_DU
)==0)
243 return uprv_decContextSetStatus(context
, DEC_Division_undefined
);
244 if (strcmp(string
, DEC_Condition_IE
)==0)
245 return uprv_decContextSetStatus(context
, DEC_Inexact
);
246 if (strcmp(string
, DEC_Condition_IS
)==0)
247 return uprv_decContextSetStatus(context
, DEC_Insufficient_storage
);
248 if (strcmp(string
, DEC_Condition_IC
)==0)
249 return uprv_decContextSetStatus(context
, DEC_Invalid_context
);
250 if (strcmp(string
, DEC_Condition_IO
)==0)
251 return uprv_decContextSetStatus(context
, DEC_Invalid_operation
);
253 if (strcmp(string
, DEC_Condition_LD
)==0)
254 return uprv_decContextSetStatus(context
, DEC_Lost_digits
);
256 if (strcmp(string
, DEC_Condition_OV
)==0)
257 return uprv_decContextSetStatus(context
, DEC_Overflow
);
258 if (strcmp(string
, DEC_Condition_PA
)==0)
259 return uprv_decContextSetStatus(context
, DEC_Clamped
);
260 if (strcmp(string
, DEC_Condition_RO
)==0)
261 return uprv_decContextSetStatus(context
, DEC_Rounded
);
262 if (strcmp(string
, DEC_Condition_SU
)==0)
263 return uprv_decContextSetStatus(context
, DEC_Subnormal
);
264 if (strcmp(string
, DEC_Condition_UN
)==0)
265 return uprv_decContextSetStatus(context
, DEC_Underflow
);
266 if (strcmp(string
, DEC_Condition_ZE
)==0)
268 return NULL
; /* Multiple status, or unknown */
269 } /* decContextSetStatusFromString */
271 /* ------------------------------------------------------------------ */
272 /* decContextSetStatusFromStringQuiet -- set status from a string */
274 /* context is the context structure to be updated */
275 /* string is a string exactly equal to one that might be returned */
276 /* by decContextStatusToString */
278 /* The status bit corresponding to the string is set; no trap is */
281 /* returns the context structure, unless the string is equal to */
282 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
284 /* ------------------------------------------------------------------ */
285 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatusFromStringQuiet(decContext
*context
,
286 const char *string
) {
287 if (strcmp(string
, DEC_Condition_CS
)==0)
288 return uprv_decContextSetStatusQuiet(context
, DEC_Conversion_syntax
);
289 if (strcmp(string
, DEC_Condition_DZ
)==0)
290 return uprv_decContextSetStatusQuiet(context
, DEC_Division_by_zero
);
291 if (strcmp(string
, DEC_Condition_DI
)==0)
292 return uprv_decContextSetStatusQuiet(context
, DEC_Division_impossible
);
293 if (strcmp(string
, DEC_Condition_DU
)==0)
294 return uprv_decContextSetStatusQuiet(context
, DEC_Division_undefined
);
295 if (strcmp(string
, DEC_Condition_IE
)==0)
296 return uprv_decContextSetStatusQuiet(context
, DEC_Inexact
);
297 if (strcmp(string
, DEC_Condition_IS
)==0)
298 return uprv_decContextSetStatusQuiet(context
, DEC_Insufficient_storage
);
299 if (strcmp(string
, DEC_Condition_IC
)==0)
300 return uprv_decContextSetStatusQuiet(context
, DEC_Invalid_context
);
301 if (strcmp(string
, DEC_Condition_IO
)==0)
302 return uprv_decContextSetStatusQuiet(context
, DEC_Invalid_operation
);
304 if (strcmp(string
, DEC_Condition_LD
)==0)
305 return uprv_decContextSetStatusQuiet(context
, DEC_Lost_digits
);
307 if (strcmp(string
, DEC_Condition_OV
)==0)
308 return uprv_decContextSetStatusQuiet(context
, DEC_Overflow
);
309 if (strcmp(string
, DEC_Condition_PA
)==0)
310 return uprv_decContextSetStatusQuiet(context
, DEC_Clamped
);
311 if (strcmp(string
, DEC_Condition_RO
)==0)
312 return uprv_decContextSetStatusQuiet(context
, DEC_Rounded
);
313 if (strcmp(string
, DEC_Condition_SU
)==0)
314 return uprv_decContextSetStatusQuiet(context
, DEC_Subnormal
);
315 if (strcmp(string
, DEC_Condition_UN
)==0)
316 return uprv_decContextSetStatusQuiet(context
, DEC_Underflow
);
317 if (strcmp(string
, DEC_Condition_ZE
)==0)
319 return NULL
; /* Multiple status, or unknown */
320 } /* decContextSetStatusFromStringQuiet */
322 /* ------------------------------------------------------------------ */
323 /* decContextSetStatusQuiet -- set status without trap */
325 /* context is the context structure to be updated */
326 /* status is the DEC_ exception code */
327 /* returns the context structure */
329 /* No error is possible. */
330 /* ------------------------------------------------------------------ */
331 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatusQuiet(decContext
*context
, uInt status
) {
332 context
->status
|=status
;
333 return context
;} /* decContextSetStatusQuiet */
335 /* ------------------------------------------------------------------ */
336 /* decContextStatusToString -- convert status flags to a string */
338 /* context is a context with valid status field */
340 /* returns a constant string describing the condition. If multiple */
341 /* (or no) flags are set, a generic constant message is returned. */
342 /* ------------------------------------------------------------------ */
343 U_CAPI
const char * U_EXPORT2
uprv_decContextStatusToString(const decContext
*context
) {
344 Int status
=context
->status
;
346 /* test the five IEEE first, as some of the others are ambiguous when */
348 if (status
==DEC_Invalid_operation
) return DEC_Condition_IO
;
349 if (status
==DEC_Division_by_zero
) return DEC_Condition_DZ
;
350 if (status
==DEC_Overflow
) return DEC_Condition_OV
;
351 if (status
==DEC_Underflow
) return DEC_Condition_UN
;
352 if (status
==DEC_Inexact
) return DEC_Condition_IE
;
354 if (status
==DEC_Division_impossible
) return DEC_Condition_DI
;
355 if (status
==DEC_Division_undefined
) return DEC_Condition_DU
;
356 if (status
==DEC_Rounded
) return DEC_Condition_RO
;
357 if (status
==DEC_Clamped
) return DEC_Condition_PA
;
358 if (status
==DEC_Subnormal
) return DEC_Condition_SU
;
359 if (status
==DEC_Conversion_syntax
) return DEC_Condition_CS
;
360 if (status
==DEC_Insufficient_storage
) return DEC_Condition_IS
;
361 if (status
==DEC_Invalid_context
) return DEC_Condition_IC
;
363 if (status
==DEC_Lost_digits
) return DEC_Condition_LD
;
365 if (status
==0 ) return DEC_Condition_ZE
;
366 return DEC_Condition_MU
; /* Multiple errors */
367 } /* decContextStatusToString */
369 /* ------------------------------------------------------------------ */
370 /* decContextTestEndian -- test whether DECLITEND is set correctly */
372 /* quiet is 1 to suppress message; 0 otherwise */
373 /* returns 0 if DECLITEND is correct */
374 /* 1 if DECLITEND is incorrect and should be 1 */
375 /* -1 if DECLITEND is incorrect and should be 0 */
377 /* A message is displayed if the return value is not 0 and quiet==0. */
379 /* No error is possible. */
380 /* ------------------------------------------------------------------ */
381 #if 0 /* ICU: Unused function. Anyway, do not call printf(). */
382 U_CAPI Int U_EXPORT2
uprv_decContextTestEndian(Flag quiet
) {
383 Int res
=0; /* optimist */
384 uInt dle
=(uInt
)DECLITEND
; /* unsign */
385 if (dle
>1) dle
=1; /* ensure 0 or 1 */
387 if (LITEND
!=DECLITEND
) {
390 if (LITEND
) adj
="little";
392 printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
398 } /* decContextTestEndian */
401 /* ------------------------------------------------------------------ */
402 /* decContextTestSavedStatus -- test bits in saved status */
404 /* oldstatus is the status word to be tested */
405 /* mask indicates the bits to be tested (the oldstatus 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 */
409 /* No error is possible. */
410 /* ------------------------------------------------------------------ */
411 U_CAPI uInt U_EXPORT2
uprv_decContextTestSavedStatus(uInt oldstatus
, uInt mask
) {
412 return (oldstatus
&mask
)!=0;
413 } /* decContextTestSavedStatus */
415 /* ------------------------------------------------------------------ */
416 /* decContextTestStatus -- test bits in current status */
418 /* context is the context structure to be updated */
419 /* mask indicates the bits to be tested (the status bits that */
420 /* correspond to each 1 bit in the mask are tested) */
421 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
423 /* No error is possible. */
424 /* ------------------------------------------------------------------ */
425 U_CAPI uInt U_EXPORT2
uprv_decContextTestStatus(decContext
*context
, uInt mask
) {
426 return (context
->status
&mask
)!=0;
427 } /* decContextTestStatus */
429 /* ------------------------------------------------------------------ */
430 /* decContextZeroStatus -- clear all status bits */
432 /* context is the context structure to be updated */
433 /* returns context */
435 /* No error is possible. */
436 /* ------------------------------------------------------------------ */
437 U_CAPI decContext
* U_EXPORT2
uprv_decContextZeroStatus(decContext
*context
) {
440 } /* decContextZeroStatus */