]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/decContext.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /* ------------------------------------------------------------------ */
4 /* Decimal Context module */
5 /* ------------------------------------------------------------------ */
6 /* Copyright (c) IBM Corporation, 2000-2012. All rights reserved. */
8 /* This software is made available under the terms of the */
9 /* ICU License -- ICU 1.8.1 and later. */
11 /* The description and User's Guide ("The decNumber C Library") for */
12 /* this software is called decNumber.pdf. This document is */
13 /* available, together with arithmetic and format specifications, */
14 /* testcases, and Web links, on the General Decimal Arithmetic page. */
16 /* Please send comments, suggestions, and corrections to the author: */
18 /* Mike Cowlishaw, IBM Fellow */
19 /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
20 /* ------------------------------------------------------------------ */
21 /* This module comprises the routines for handling arithmetic */
22 /* context structures. */
23 /* ------------------------------------------------------------------ */
25 #include <string.h> /* for strcmp */
26 #include <stdio.h> /* for printf if DECCHECK */
27 #include "decContext.h" /* context and base types */
28 #include "decNumberLocal.h" /* decNumber local types, etc. */
30 #if 0 /* ICU: No need to test endianness at runtime. */
31 /* compile-time endian tester [assumes sizeof(Int)>1] */
32 static const Int mfcone
=1; /* constant 1 */
33 static const Flag
*mfctop
=(Flag
*)&mfcone
; /* -> top byte */
34 #define LITEND *mfctop /* named flag; 1=little-endian */
37 /* ------------------------------------------------------------------ */
38 /* decContextClearStatus -- clear bits in current status */
40 /* context is the context structure to be queried */
41 /* mask indicates the bits to be cleared (the status bit that */
42 /* corresponds to each 1 bit in the mask is cleared) */
45 /* No error is possible. */
46 /* ------------------------------------------------------------------ */
47 U_CAPI decContext
* U_EXPORT2
uprv_decContextClearStatus(decContext
*context
, uInt mask
) {
48 context
->status
&=~mask
;
50 } /* decContextClearStatus */
52 /* ------------------------------------------------------------------ */
53 /* decContextDefault -- initialize a context structure */
55 /* context is the structure to be initialized */
56 /* kind selects the required set of default values, one of: */
57 /* DEC_INIT_BASE -- select ANSI X3-274 defaults */
58 /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */
59 /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */
60 /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */
61 /* For any other value a valid context is returned, but with */
62 /* Invalid_operation set in the status field. */
63 /* returns a context structure with the appropriate initial values. */
64 /* ------------------------------------------------------------------ */
65 U_CAPI decContext
* U_EXPORT2
uprv_decContextDefault(decContext
*context
, Int kind
) {
67 context
->digits
=9; /* 9 digits */
68 context
->emax
=DEC_MAX_EMAX
; /* 9-digit exponents */
69 context
->emin
=DEC_MIN_EMIN
; /* .. balanced */
70 context
->round
=DEC_ROUND_HALF_UP
; /* 0.5 rises */
71 context
->traps
=DEC_Errors
; /* all but informational */
72 context
->status
=0; /* cleared */
73 context
->clamp
=0; /* no clamping */
75 context
->extended
=0; /* cleared */
81 case DEC_INIT_DECIMAL32
:
82 context
->digits
=7; /* digits */
83 context
->emax
=96; /* Emax */
84 context
->emin
=-95; /* Emin */
85 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
86 context
->traps
=0; /* no traps set */
87 context
->clamp
=1; /* clamp exponents */
89 context
->extended
=1; /* set */
92 case DEC_INIT_DECIMAL64
:
93 context
->digits
=16; /* digits */
94 context
->emax
=384; /* Emax */
95 context
->emin
=-383; /* Emin */
96 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
97 context
->traps
=0; /* no traps set */
98 context
->clamp
=1; /* clamp exponents */
100 context
->extended
=1; /* set */
103 case DEC_INIT_DECIMAL128
:
104 context
->digits
=34; /* digits */
105 context
->emax
=6144; /* Emax */
106 context
->emin
=-6143; /* Emin */
107 context
->round
=DEC_ROUND_HALF_EVEN
; /* 0.5 to nearest even */
108 context
->traps
=0; /* no traps set */
109 context
->clamp
=1; /* clamp exponents */
111 context
->extended
=1; /* set */
115 default: /* invalid Kind */
116 /* use defaults, and .. */
117 uprv_decContextSetStatus(context
, DEC_Invalid_operation
); /* trap */
120 return context
;} /* decContextDefault */
122 /* ------------------------------------------------------------------ */
123 /* decContextGetRounding -- return current rounding mode */
125 /* context is the context structure to be queried */
126 /* returns the rounding mode */
128 /* No error is possible. */
129 /* ------------------------------------------------------------------ */
130 U_CAPI
enum rounding U_EXPORT2
uprv_decContextGetRounding(decContext
*context
) {
131 return context
->round
;
132 } /* decContextGetRounding */
134 /* ------------------------------------------------------------------ */
135 /* decContextGetStatus -- return current status */
137 /* context is the context structure to be queried */
140 /* No error is possible. */
141 /* ------------------------------------------------------------------ */
142 U_CAPI uInt U_EXPORT2
uprv_decContextGetStatus(decContext
*context
) {
143 return context
->status
;
144 } /* decContextGetStatus */
146 /* ------------------------------------------------------------------ */
147 /* decContextRestoreStatus -- restore bits in current status */
149 /* context is the context structure to be updated */
150 /* newstatus is the source for the bits to be restored */
151 /* mask indicates the bits to be restored (the status bit that */
152 /* corresponds to each 1 bit in the mask is set to the value of */
153 /* the correspnding bit in newstatus) */
154 /* returns context */
156 /* No error is possible. */
157 /* ------------------------------------------------------------------ */
158 U_CAPI decContext
* U_EXPORT2
uprv_decContextRestoreStatus(decContext
*context
,
159 uInt newstatus
, uInt mask
) {
160 context
->status
&=~mask
; /* clear the selected bits */
161 context
->status
|=(mask
&newstatus
); /* or in the new bits */
163 } /* decContextRestoreStatus */
165 /* ------------------------------------------------------------------ */
166 /* decContextSaveStatus -- save bits in current status */
168 /* context is the context structure to be queried */
169 /* mask indicates the bits to be saved (the status bits that */
170 /* correspond to each 1 bit in the mask are saved) */
171 /* returns the AND of the mask and the current status */
173 /* No error is possible. */
174 /* ------------------------------------------------------------------ */
175 U_CAPI uInt U_EXPORT2
uprv_decContextSaveStatus(decContext
*context
, uInt mask
) {
176 return context
->status
&mask
;
177 } /* decContextSaveStatus */
179 /* ------------------------------------------------------------------ */
180 /* decContextSetRounding -- set current rounding mode */
182 /* context is the context structure to be updated */
183 /* newround is the value which will replace the current mode */
184 /* returns context */
186 /* No error is possible. */
187 /* ------------------------------------------------------------------ */
188 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetRounding(decContext
*context
,
189 enum rounding newround
) {
190 context
->round
=newround
;
192 } /* decContextSetRounding */
194 /* ------------------------------------------------------------------ */
195 /* decContextSetStatus -- set status and raise trap if appropriate */
197 /* context is the context structure to be updated */
198 /* status is the DEC_ exception code */
199 /* returns the context structure */
201 /* Control may never return from this routine, if there is a signal */
202 /* handler and it takes a long jump. */
203 /* ------------------------------------------------------------------ */
204 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatus(decContext
*context
, uInt status
) {
205 context
->status
|=status
;
206 #if 0 /* ICU: Do not raise signals. */
207 if (status
& context
->traps
) raise(SIGFPE
);
209 return context
;} /* decContextSetStatus */
211 /* ------------------------------------------------------------------ */
212 /* decContextSetStatusFromString -- set status from a string + trap */
214 /* context is the context structure to be updated */
215 /* string is a string exactly equal to one that might be returned */
216 /* by decContextStatusToString */
218 /* The status bit corresponding to the string is set, and a trap */
219 /* is raised if appropriate. */
221 /* returns the context structure, unless the string is equal to */
222 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
224 /* ------------------------------------------------------------------ */
225 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatusFromString(decContext
*context
,
226 const char *string
) {
227 if (strcmp(string
, DEC_Condition_CS
)==0)
228 return uprv_decContextSetStatus(context
, DEC_Conversion_syntax
);
229 if (strcmp(string
, DEC_Condition_DZ
)==0)
230 return uprv_decContextSetStatus(context
, DEC_Division_by_zero
);
231 if (strcmp(string
, DEC_Condition_DI
)==0)
232 return uprv_decContextSetStatus(context
, DEC_Division_impossible
);
233 if (strcmp(string
, DEC_Condition_DU
)==0)
234 return uprv_decContextSetStatus(context
, DEC_Division_undefined
);
235 if (strcmp(string
, DEC_Condition_IE
)==0)
236 return uprv_decContextSetStatus(context
, DEC_Inexact
);
237 if (strcmp(string
, DEC_Condition_IS
)==0)
238 return uprv_decContextSetStatus(context
, DEC_Insufficient_storage
);
239 if (strcmp(string
, DEC_Condition_IC
)==0)
240 return uprv_decContextSetStatus(context
, DEC_Invalid_context
);
241 if (strcmp(string
, DEC_Condition_IO
)==0)
242 return uprv_decContextSetStatus(context
, DEC_Invalid_operation
);
244 if (strcmp(string
, DEC_Condition_LD
)==0)
245 return uprv_decContextSetStatus(context
, DEC_Lost_digits
);
247 if (strcmp(string
, DEC_Condition_OV
)==0)
248 return uprv_decContextSetStatus(context
, DEC_Overflow
);
249 if (strcmp(string
, DEC_Condition_PA
)==0)
250 return uprv_decContextSetStatus(context
, DEC_Clamped
);
251 if (strcmp(string
, DEC_Condition_RO
)==0)
252 return uprv_decContextSetStatus(context
, DEC_Rounded
);
253 if (strcmp(string
, DEC_Condition_SU
)==0)
254 return uprv_decContextSetStatus(context
, DEC_Subnormal
);
255 if (strcmp(string
, DEC_Condition_UN
)==0)
256 return uprv_decContextSetStatus(context
, DEC_Underflow
);
257 if (strcmp(string
, DEC_Condition_ZE
)==0)
259 return NULL
; /* Multiple status, or unknown */
260 } /* decContextSetStatusFromString */
262 /* ------------------------------------------------------------------ */
263 /* decContextSetStatusFromStringQuiet -- set status from a string */
265 /* context is the context structure to be updated */
266 /* string is a string exactly equal to one that might be returned */
267 /* by decContextStatusToString */
269 /* The status bit corresponding to the string is set; no trap is */
272 /* returns the context structure, unless the string is equal to */
273 /* DEC_Condition_MU or is not recognized. In these cases NULL is */
275 /* ------------------------------------------------------------------ */
276 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatusFromStringQuiet(decContext
*context
,
277 const char *string
) {
278 if (strcmp(string
, DEC_Condition_CS
)==0)
279 return uprv_decContextSetStatusQuiet(context
, DEC_Conversion_syntax
);
280 if (strcmp(string
, DEC_Condition_DZ
)==0)
281 return uprv_decContextSetStatusQuiet(context
, DEC_Division_by_zero
);
282 if (strcmp(string
, DEC_Condition_DI
)==0)
283 return uprv_decContextSetStatusQuiet(context
, DEC_Division_impossible
);
284 if (strcmp(string
, DEC_Condition_DU
)==0)
285 return uprv_decContextSetStatusQuiet(context
, DEC_Division_undefined
);
286 if (strcmp(string
, DEC_Condition_IE
)==0)
287 return uprv_decContextSetStatusQuiet(context
, DEC_Inexact
);
288 if (strcmp(string
, DEC_Condition_IS
)==0)
289 return uprv_decContextSetStatusQuiet(context
, DEC_Insufficient_storage
);
290 if (strcmp(string
, DEC_Condition_IC
)==0)
291 return uprv_decContextSetStatusQuiet(context
, DEC_Invalid_context
);
292 if (strcmp(string
, DEC_Condition_IO
)==0)
293 return uprv_decContextSetStatusQuiet(context
, DEC_Invalid_operation
);
295 if (strcmp(string
, DEC_Condition_LD
)==0)
296 return uprv_decContextSetStatusQuiet(context
, DEC_Lost_digits
);
298 if (strcmp(string
, DEC_Condition_OV
)==0)
299 return uprv_decContextSetStatusQuiet(context
, DEC_Overflow
);
300 if (strcmp(string
, DEC_Condition_PA
)==0)
301 return uprv_decContextSetStatusQuiet(context
, DEC_Clamped
);
302 if (strcmp(string
, DEC_Condition_RO
)==0)
303 return uprv_decContextSetStatusQuiet(context
, DEC_Rounded
);
304 if (strcmp(string
, DEC_Condition_SU
)==0)
305 return uprv_decContextSetStatusQuiet(context
, DEC_Subnormal
);
306 if (strcmp(string
, DEC_Condition_UN
)==0)
307 return uprv_decContextSetStatusQuiet(context
, DEC_Underflow
);
308 if (strcmp(string
, DEC_Condition_ZE
)==0)
310 return NULL
; /* Multiple status, or unknown */
311 } /* decContextSetStatusFromStringQuiet */
313 /* ------------------------------------------------------------------ */
314 /* decContextSetStatusQuiet -- set status without trap */
316 /* context is the context structure to be updated */
317 /* status is the DEC_ exception code */
318 /* returns the context structure */
320 /* No error is possible. */
321 /* ------------------------------------------------------------------ */
322 U_CAPI decContext
* U_EXPORT2
uprv_decContextSetStatusQuiet(decContext
*context
, uInt status
) {
323 context
->status
|=status
;
324 return context
;} /* decContextSetStatusQuiet */
326 /* ------------------------------------------------------------------ */
327 /* decContextStatusToString -- convert status flags to a string */
329 /* context is a context with valid status field */
331 /* returns a constant string describing the condition. If multiple */
332 /* (or no) flags are set, a generic constant message is returned. */
333 /* ------------------------------------------------------------------ */
334 U_CAPI
const char * U_EXPORT2
uprv_decContextStatusToString(const decContext
*context
) {
335 Int status
=context
->status
;
337 /* test the five IEEE first, as some of the others are ambiguous when */
339 if (status
==DEC_Invalid_operation
) return DEC_Condition_IO
;
340 if (status
==DEC_Division_by_zero
) return DEC_Condition_DZ
;
341 if (status
==DEC_Overflow
) return DEC_Condition_OV
;
342 if (status
==DEC_Underflow
) return DEC_Condition_UN
;
343 if (status
==DEC_Inexact
) return DEC_Condition_IE
;
345 if (status
==DEC_Division_impossible
) return DEC_Condition_DI
;
346 if (status
==DEC_Division_undefined
) return DEC_Condition_DU
;
347 if (status
==DEC_Rounded
) return DEC_Condition_RO
;
348 if (status
==DEC_Clamped
) return DEC_Condition_PA
;
349 if (status
==DEC_Subnormal
) return DEC_Condition_SU
;
350 if (status
==DEC_Conversion_syntax
) return DEC_Condition_CS
;
351 if (status
==DEC_Insufficient_storage
) return DEC_Condition_IS
;
352 if (status
==DEC_Invalid_context
) return DEC_Condition_IC
;
354 if (status
==DEC_Lost_digits
) return DEC_Condition_LD
;
356 if (status
==0 ) return DEC_Condition_ZE
;
357 return DEC_Condition_MU
; /* Multiple errors */
358 } /* decContextStatusToString */
360 /* ------------------------------------------------------------------ */
361 /* decContextTestEndian -- test whether DECLITEND is set correctly */
363 /* quiet is 1 to suppress message; 0 otherwise */
364 /* returns 0 if DECLITEND is correct */
365 /* 1 if DECLITEND is incorrect and should be 1 */
366 /* -1 if DECLITEND is incorrect and should be 0 */
368 /* A message is displayed if the return value is not 0 and quiet==0. */
370 /* No error is possible. */
371 /* ------------------------------------------------------------------ */
372 #if 0 /* ICU: Unused function. Anyway, do not call printf(). */
373 U_CAPI Int U_EXPORT2
uprv_decContextTestEndian(Flag quiet
) {
374 Int res
=0; /* optimist */
375 uInt dle
=(uInt
)DECLITEND
; /* unsign */
376 if (dle
>1) dle
=1; /* ensure 0 or 1 */
378 if (LITEND
!=DECLITEND
) {
381 if (LITEND
) adj
="little";
383 printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n",
389 } /* decContextTestEndian */
392 /* ------------------------------------------------------------------ */
393 /* decContextTestSavedStatus -- test bits in saved status */
395 /* oldstatus is the status word to be tested */
396 /* mask indicates the bits to be tested (the oldstatus bits that */
397 /* correspond to each 1 bit in the mask are tested) */
398 /* returns 1 if any of the tested bits are 1, or 0 otherwise */
400 /* No error is possible. */
401 /* ------------------------------------------------------------------ */
402 U_CAPI uInt U_EXPORT2
uprv_decContextTestSavedStatus(uInt oldstatus
, uInt mask
) {
403 return (oldstatus
&mask
)!=0;
404 } /* decContextTestSavedStatus */
406 /* ------------------------------------------------------------------ */
407 /* decContextTestStatus -- test bits in current status */
409 /* context is the context structure to be updated */
410 /* mask indicates the bits to be tested (the status 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 U_CAPI uInt U_EXPORT2
uprv_decContextTestStatus(decContext
*context
, uInt mask
) {
417 return (context
->status
&mask
)!=0;
418 } /* decContextTestStatus */
420 /* ------------------------------------------------------------------ */
421 /* decContextZeroStatus -- clear all status bits */
423 /* context is the context structure to be updated */
424 /* returns context */
426 /* No error is possible. */
427 /* ------------------------------------------------------------------ */
428 U_CAPI decContext
* U_EXPORT2
uprv_decContextZeroStatus(decContext
*context
) {
431 } /* decContextZeroStatus */