michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Decimal Context module */ michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Copyright (c) IBM Corporation, 2000-2012. All rights reserved. */ michael@0: /* */ michael@0: /* This software is made available under the terms of the */ michael@0: /* ICU License -- ICU 1.8.1 and later. */ michael@0: /* */ michael@0: /* The description and User's Guide ("The decNumber C Library") for */ michael@0: /* this software is called decNumber.pdf. This document is */ michael@0: /* available, together with arithmetic and format specifications, */ michael@0: /* testcases, and Web links, on the General Decimal Arithmetic page. */ michael@0: /* */ michael@0: /* Please send comments, suggestions, and corrections to the author: */ michael@0: /* mfc@uk.ibm.com */ michael@0: /* Mike Cowlishaw, IBM Fellow */ michael@0: /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */ michael@0: /* ------------------------------------------------------------------ */ michael@0: /* This module comprises the routines for handling arithmetic */ michael@0: /* context structures. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: michael@0: #include /* for strcmp */ michael@0: #include /* for printf if DECCHECK */ michael@0: #include "decContext.h" /* context and base types */ michael@0: #include "decNumberLocal.h" /* decNumber local types, etc. */ michael@0: michael@0: #if 0 /* ICU: No need to test endianness at runtime. */ michael@0: /* compile-time endian tester [assumes sizeof(Int)>1] */ michael@0: static const Int mfcone=1; /* constant 1 */ michael@0: static const Flag *mfctop=(Flag *)&mfcone; /* -> top byte */ michael@0: #define LITEND *mfctop /* named flag; 1=little-endian */ michael@0: #endif michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextClearStatus -- clear bits in current status */ michael@0: /* */ michael@0: /* context is the context structure to be queried */ michael@0: /* mask indicates the bits to be cleared (the status bit that */ michael@0: /* corresponds to each 1 bit in the mask is cleared) */ michael@0: /* returns context */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextClearStatus(decContext *context, uInt mask) { michael@0: context->status&=~mask; michael@0: return context; michael@0: } /* decContextClearStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextDefault -- initialize a context structure */ michael@0: /* */ michael@0: /* context is the structure to be initialized */ michael@0: /* kind selects the required set of default values, one of: */ michael@0: /* DEC_INIT_BASE -- select ANSI X3-274 defaults */ michael@0: /* DEC_INIT_DECIMAL32 -- select IEEE 754 defaults, 32-bit */ michael@0: /* DEC_INIT_DECIMAL64 -- select IEEE 754 defaults, 64-bit */ michael@0: /* DEC_INIT_DECIMAL128 -- select IEEE 754 defaults, 128-bit */ michael@0: /* For any other value a valid context is returned, but with */ michael@0: /* Invalid_operation set in the status field. */ michael@0: /* returns a context structure with the appropriate initial values. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextDefault(decContext *context, Int kind) { michael@0: /* set defaults... */ michael@0: context->digits=9; /* 9 digits */ michael@0: context->emax=DEC_MAX_EMAX; /* 9-digit exponents */ michael@0: context->emin=DEC_MIN_EMIN; /* .. balanced */ michael@0: context->round=DEC_ROUND_HALF_UP; /* 0.5 rises */ michael@0: context->traps=DEC_Errors; /* all but informational */ michael@0: context->status=0; /* cleared */ michael@0: context->clamp=0; /* no clamping */ michael@0: #if DECSUBSET michael@0: context->extended=0; /* cleared */ michael@0: #endif michael@0: switch (kind) { michael@0: case DEC_INIT_BASE: michael@0: /* [use defaults] */ michael@0: break; michael@0: case DEC_INIT_DECIMAL32: michael@0: context->digits=7; /* digits */ michael@0: context->emax=96; /* Emax */ michael@0: context->emin=-95; /* Emin */ michael@0: context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */ michael@0: context->traps=0; /* no traps set */ michael@0: context->clamp=1; /* clamp exponents */ michael@0: #if DECSUBSET michael@0: context->extended=1; /* set */ michael@0: #endif michael@0: break; michael@0: case DEC_INIT_DECIMAL64: michael@0: context->digits=16; /* digits */ michael@0: context->emax=384; /* Emax */ michael@0: context->emin=-383; /* Emin */ michael@0: context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */ michael@0: context->traps=0; /* no traps set */ michael@0: context->clamp=1; /* clamp exponents */ michael@0: #if DECSUBSET michael@0: context->extended=1; /* set */ michael@0: #endif michael@0: break; michael@0: case DEC_INIT_DECIMAL128: michael@0: context->digits=34; /* digits */ michael@0: context->emax=6144; /* Emax */ michael@0: context->emin=-6143; /* Emin */ michael@0: context->round=DEC_ROUND_HALF_EVEN; /* 0.5 to nearest even */ michael@0: context->traps=0; /* no traps set */ michael@0: context->clamp=1; /* clamp exponents */ michael@0: #if DECSUBSET michael@0: context->extended=1; /* set */ michael@0: #endif michael@0: break; michael@0: michael@0: default: /* invalid Kind */ michael@0: /* use defaults, and .. */ michael@0: uprv_decContextSetStatus(context, DEC_Invalid_operation); /* trap */ michael@0: } michael@0: michael@0: return context;} /* decContextDefault */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextGetRounding -- return current rounding mode */ michael@0: /* */ michael@0: /* context is the context structure to be queried */ michael@0: /* returns the rounding mode */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI enum rounding U_EXPORT2 uprv_decContextGetRounding(decContext *context) { michael@0: return context->round; michael@0: } /* decContextGetRounding */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextGetStatus -- return current status */ michael@0: /* */ michael@0: /* context is the context structure to be queried */ michael@0: /* returns status */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI uInt U_EXPORT2 uprv_decContextGetStatus(decContext *context) { michael@0: return context->status; michael@0: } /* decContextGetStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextRestoreStatus -- restore bits in current status */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* newstatus is the source for the bits to be restored */ michael@0: /* mask indicates the bits to be restored (the status bit that */ michael@0: /* corresponds to each 1 bit in the mask is set to the value of */ michael@0: /* the correspnding bit in newstatus) */ michael@0: /* returns context */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextRestoreStatus(decContext *context, michael@0: uInt newstatus, uInt mask) { michael@0: context->status&=~mask; /* clear the selected bits */ michael@0: context->status|=(mask&newstatus); /* or in the new bits */ michael@0: return context; michael@0: } /* decContextRestoreStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextSaveStatus -- save bits in current status */ michael@0: /* */ michael@0: /* context is the context structure to be queried */ michael@0: /* mask indicates the bits to be saved (the status bits that */ michael@0: /* correspond to each 1 bit in the mask are saved) */ michael@0: /* returns the AND of the mask and the current status */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI uInt U_EXPORT2 uprv_decContextSaveStatus(decContext *context, uInt mask) { michael@0: return context->status&mask; michael@0: } /* decContextSaveStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextSetRounding -- set current rounding mode */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* newround is the value which will replace the current mode */ michael@0: /* returns context */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextSetRounding(decContext *context, michael@0: enum rounding newround) { michael@0: context->round=newround; michael@0: return context; michael@0: } /* decContextSetRounding */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextSetStatus -- set status and raise trap if appropriate */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* status is the DEC_ exception code */ michael@0: /* returns the context structure */ michael@0: /* */ michael@0: /* Control may never return from this routine, if there is a signal */ michael@0: /* handler and it takes a long jump. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextSetStatus(decContext *context, uInt status) { michael@0: context->status|=status; michael@0: #if 0 /* ICU: Do not raise signals. */ michael@0: if (status & context->traps) raise(SIGFPE); michael@0: #endif michael@0: return context;} /* decContextSetStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextSetStatusFromString -- set status from a string + trap */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* string is a string exactly equal to one that might be returned */ michael@0: /* by decContextStatusToString */ michael@0: /* */ michael@0: /* The status bit corresponding to the string is set, and a trap */ michael@0: /* is raised if appropriate. */ michael@0: /* */ michael@0: /* returns the context structure, unless the string is equal to */ michael@0: /* DEC_Condition_MU or is not recognized. In these cases NULL is */ michael@0: /* returned. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextSetStatusFromString(decContext *context, michael@0: const char *string) { michael@0: if (strcmp(string, DEC_Condition_CS)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Conversion_syntax); michael@0: if (strcmp(string, DEC_Condition_DZ)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Division_by_zero); michael@0: if (strcmp(string, DEC_Condition_DI)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Division_impossible); michael@0: if (strcmp(string, DEC_Condition_DU)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Division_undefined); michael@0: if (strcmp(string, DEC_Condition_IE)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Inexact); michael@0: if (strcmp(string, DEC_Condition_IS)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Insufficient_storage); michael@0: if (strcmp(string, DEC_Condition_IC)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Invalid_context); michael@0: if (strcmp(string, DEC_Condition_IO)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Invalid_operation); michael@0: #if DECSUBSET michael@0: if (strcmp(string, DEC_Condition_LD)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Lost_digits); michael@0: #endif michael@0: if (strcmp(string, DEC_Condition_OV)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Overflow); michael@0: if (strcmp(string, DEC_Condition_PA)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Clamped); michael@0: if (strcmp(string, DEC_Condition_RO)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Rounded); michael@0: if (strcmp(string, DEC_Condition_SU)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Subnormal); michael@0: if (strcmp(string, DEC_Condition_UN)==0) michael@0: return uprv_decContextSetStatus(context, DEC_Underflow); michael@0: if (strcmp(string, DEC_Condition_ZE)==0) michael@0: return context; michael@0: return NULL; /* Multiple status, or unknown */ michael@0: } /* decContextSetStatusFromString */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextSetStatusFromStringQuiet -- set status from a string */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* string is a string exactly equal to one that might be returned */ michael@0: /* by decContextStatusToString */ michael@0: /* */ michael@0: /* The status bit corresponding to the string is set; no trap is */ michael@0: /* raised. */ michael@0: /* */ michael@0: /* returns the context structure, unless the string is equal to */ michael@0: /* DEC_Condition_MU or is not recognized. In these cases NULL is */ michael@0: /* returned. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextSetStatusFromStringQuiet(decContext *context, michael@0: const char *string) { michael@0: if (strcmp(string, DEC_Condition_CS)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Conversion_syntax); michael@0: if (strcmp(string, DEC_Condition_DZ)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Division_by_zero); michael@0: if (strcmp(string, DEC_Condition_DI)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Division_impossible); michael@0: if (strcmp(string, DEC_Condition_DU)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Division_undefined); michael@0: if (strcmp(string, DEC_Condition_IE)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Inexact); michael@0: if (strcmp(string, DEC_Condition_IS)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Insufficient_storage); michael@0: if (strcmp(string, DEC_Condition_IC)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Invalid_context); michael@0: if (strcmp(string, DEC_Condition_IO)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Invalid_operation); michael@0: #if DECSUBSET michael@0: if (strcmp(string, DEC_Condition_LD)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Lost_digits); michael@0: #endif michael@0: if (strcmp(string, DEC_Condition_OV)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Overflow); michael@0: if (strcmp(string, DEC_Condition_PA)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Clamped); michael@0: if (strcmp(string, DEC_Condition_RO)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Rounded); michael@0: if (strcmp(string, DEC_Condition_SU)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Subnormal); michael@0: if (strcmp(string, DEC_Condition_UN)==0) michael@0: return uprv_decContextSetStatusQuiet(context, DEC_Underflow); michael@0: if (strcmp(string, DEC_Condition_ZE)==0) michael@0: return context; michael@0: return NULL; /* Multiple status, or unknown */ michael@0: } /* decContextSetStatusFromStringQuiet */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextSetStatusQuiet -- set status without trap */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* status is the DEC_ exception code */ michael@0: /* returns the context structure */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextSetStatusQuiet(decContext *context, uInt status) { michael@0: context->status|=status; michael@0: return context;} /* decContextSetStatusQuiet */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextStatusToString -- convert status flags to a string */ michael@0: /* */ michael@0: /* context is a context with valid status field */ michael@0: /* */ michael@0: /* returns a constant string describing the condition. If multiple */ michael@0: /* (or no) flags are set, a generic constant message is returned. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI const char * U_EXPORT2 uprv_decContextStatusToString(const decContext *context) { michael@0: Int status=context->status; michael@0: michael@0: /* test the five IEEE first, as some of the others are ambiguous when */ michael@0: /* DECEXTFLAG=0 */ michael@0: if (status==DEC_Invalid_operation ) return DEC_Condition_IO; michael@0: if (status==DEC_Division_by_zero ) return DEC_Condition_DZ; michael@0: if (status==DEC_Overflow ) return DEC_Condition_OV; michael@0: if (status==DEC_Underflow ) return DEC_Condition_UN; michael@0: if (status==DEC_Inexact ) return DEC_Condition_IE; michael@0: michael@0: if (status==DEC_Division_impossible ) return DEC_Condition_DI; michael@0: if (status==DEC_Division_undefined ) return DEC_Condition_DU; michael@0: if (status==DEC_Rounded ) return DEC_Condition_RO; michael@0: if (status==DEC_Clamped ) return DEC_Condition_PA; michael@0: if (status==DEC_Subnormal ) return DEC_Condition_SU; michael@0: if (status==DEC_Conversion_syntax ) return DEC_Condition_CS; michael@0: if (status==DEC_Insufficient_storage ) return DEC_Condition_IS; michael@0: if (status==DEC_Invalid_context ) return DEC_Condition_IC; michael@0: #if DECSUBSET michael@0: if (status==DEC_Lost_digits ) return DEC_Condition_LD; michael@0: #endif michael@0: if (status==0 ) return DEC_Condition_ZE; michael@0: return DEC_Condition_MU; /* Multiple errors */ michael@0: } /* decContextStatusToString */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextTestEndian -- test whether DECLITEND is set correctly */ michael@0: /* */ michael@0: /* quiet is 1 to suppress message; 0 otherwise */ michael@0: /* returns 0 if DECLITEND is correct */ michael@0: /* 1 if DECLITEND is incorrect and should be 1 */ michael@0: /* -1 if DECLITEND is incorrect and should be 0 */ michael@0: /* */ michael@0: /* A message is displayed if the return value is not 0 and quiet==0. */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: #if 0 /* ICU: Unused function. Anyway, do not call printf(). */ michael@0: U_CAPI Int U_EXPORT2 uprv_decContextTestEndian(Flag quiet) { michael@0: Int res=0; /* optimist */ michael@0: uInt dle=(uInt)DECLITEND; /* unsign */ michael@0: if (dle>1) dle=1; /* ensure 0 or 1 */ michael@0: michael@0: if (LITEND!=DECLITEND) { michael@0: const char *adj; michael@0: if (!quiet) { michael@0: if (LITEND) adj="little"; michael@0: else adj="big"; michael@0: printf("Warning: DECLITEND is set to %d, but this computer appears to be %s-endian\n", michael@0: DECLITEND, adj); michael@0: } michael@0: res=(Int)LITEND-dle; michael@0: } michael@0: return res; michael@0: } /* decContextTestEndian */ michael@0: #endif michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextTestSavedStatus -- test bits in saved status */ michael@0: /* */ michael@0: /* oldstatus is the status word to be tested */ michael@0: /* mask indicates the bits to be tested (the oldstatus bits that */ michael@0: /* correspond to each 1 bit in the mask are tested) */ michael@0: /* returns 1 if any of the tested bits are 1, or 0 otherwise */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI uInt U_EXPORT2 uprv_decContextTestSavedStatus(uInt oldstatus, uInt mask) { michael@0: return (oldstatus&mask)!=0; michael@0: } /* decContextTestSavedStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextTestStatus -- test bits in current status */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* mask indicates the bits to be tested (the status bits that */ michael@0: /* correspond to each 1 bit in the mask are tested) */ michael@0: /* returns 1 if any of the tested bits are 1, or 0 otherwise */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI uInt U_EXPORT2 uprv_decContextTestStatus(decContext *context, uInt mask) { michael@0: return (context->status&mask)!=0; michael@0: } /* decContextTestStatus */ michael@0: michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decContextZeroStatus -- clear all status bits */ michael@0: /* */ michael@0: /* context is the context structure to be updated */ michael@0: /* returns context */ michael@0: /* */ michael@0: /* No error is possible. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: U_CAPI decContext * U_EXPORT2 uprv_decContextZeroStatus(decContext *context) { michael@0: context->status=0; michael@0: return context; michael@0: } /* decContextZeroStatus */ michael@0: