michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Decimal Context module header */ michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Copyright (c) IBM Corporation, 2000-2011. 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: michael@0: /* Modified version, for use from within ICU. michael@0: * Renamed public functions, to avoid an unwanted export of the michael@0: * standard names from the ICU library. michael@0: * michael@0: * Use ICU's uprv_malloc() and uprv_free() michael@0: * michael@0: * Revert comment syntax to plain C michael@0: * michael@0: * Remove a few compiler warnings. michael@0: */ michael@0: #include "unicode/utypes.h" michael@0: #include "putilimp.h" michael@0: michael@0: /* */ michael@0: /* Context variables must always have valid values: */ michael@0: /* */ michael@0: /* status -- [any bits may be cleared, but not set, by user] */ michael@0: /* round -- must be one of the enumerated rounding modes */ michael@0: /* */ michael@0: /* The following variables are implied for fixed size formats (i.e., */ michael@0: /* they are ignored) but should still be set correctly in case used */ michael@0: /* with decNumber functions: */ michael@0: /* */ michael@0: /* clamp -- must be either 0 or 1 */ michael@0: /* digits -- must be in the range 1 through 999999999 */ michael@0: /* emax -- must be in the range 0 through 999999999 */ michael@0: /* emin -- must be in the range 0 through -999999999 */ michael@0: /* extended -- must be either 0 or 1 [present only if DECSUBSET] */ michael@0: /* traps -- only defined bits may be set */ michael@0: /* */ michael@0: /* ------------------------------------------------------------------ */ michael@0: michael@0: #if !defined(DECCONTEXT) michael@0: #define DECCONTEXT michael@0: #define DECCNAME "decContext" /* Short name */ michael@0: #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */ michael@0: #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */ michael@0: michael@0: #if !defined(int32_t) michael@0: /* #include */ /* C99 standard integers */ michael@0: #endif michael@0: #include /* for printf, etc. */ michael@0: #include /* for traps */ michael@0: michael@0: /* Extended flags setting -- set this to 0 to use only IEEE flags */ michael@0: #if !defined(DECEXTFLAG) michael@0: #define DECEXTFLAG 1 /* 1=enable extended flags */ michael@0: #endif michael@0: michael@0: /* Conditional code flag -- set this to 0 for best performance */ michael@0: #if !defined(DECSUBSET) michael@0: #define DECSUBSET 0 /* 1=enable subset arithmetic */ michael@0: #endif michael@0: michael@0: /* Context for operations, with associated constants */ michael@0: enum rounding { michael@0: DEC_ROUND_CEILING, /* round towards +infinity */ michael@0: DEC_ROUND_UP, /* round away from 0 */ michael@0: DEC_ROUND_HALF_UP, /* 0.5 rounds up */ michael@0: DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */ michael@0: DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */ michael@0: DEC_ROUND_DOWN, /* round towards 0 (truncate) */ michael@0: DEC_ROUND_FLOOR, /* round towards -infinity */ michael@0: DEC_ROUND_05UP, /* round for reround */ michael@0: DEC_ROUND_MAX /* enum must be less than this */ michael@0: }; michael@0: #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN; michael@0: michael@0: typedef struct { michael@0: int32_t digits; /* working precision */ michael@0: int32_t emax; /* maximum positive exponent */ michael@0: int32_t emin; /* minimum negative exponent */ michael@0: enum rounding round; /* rounding mode */ michael@0: uint32_t traps; /* trap-enabler flags */ michael@0: uint32_t status; /* status flags */ michael@0: uint8_t clamp; /* flag: apply IEEE exponent clamp */ michael@0: #if DECSUBSET michael@0: uint8_t extended; /* flag: special-values allowed */ michael@0: #endif michael@0: } decContext; michael@0: michael@0: /* Maxima and Minima for context settings */ michael@0: #define DEC_MAX_DIGITS 999999999 michael@0: #define DEC_MIN_DIGITS 1 michael@0: #define DEC_MAX_EMAX 999999999 michael@0: #define DEC_MIN_EMAX 0 michael@0: #define DEC_MAX_EMIN 0 michael@0: #define DEC_MIN_EMIN -999999999 michael@0: #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */ michael@0: michael@0: /* Classifications for decimal numbers, aligned with 754 (note that */ michael@0: /* 'normal' and 'subnormal' are meaningful only with a decContext */ michael@0: /* or a fixed size format). */ michael@0: enum decClass { michael@0: DEC_CLASS_SNAN, michael@0: DEC_CLASS_QNAN, michael@0: DEC_CLASS_NEG_INF, michael@0: DEC_CLASS_NEG_NORMAL, michael@0: DEC_CLASS_NEG_SUBNORMAL, michael@0: DEC_CLASS_NEG_ZERO, michael@0: DEC_CLASS_POS_ZERO, michael@0: DEC_CLASS_POS_SUBNORMAL, michael@0: DEC_CLASS_POS_NORMAL, michael@0: DEC_CLASS_POS_INF michael@0: }; michael@0: /* Strings for the decClasses */ michael@0: #define DEC_ClassString_SN "sNaN" michael@0: #define DEC_ClassString_QN "NaN" michael@0: #define DEC_ClassString_NI "-Infinity" michael@0: #define DEC_ClassString_NN "-Normal" michael@0: #define DEC_ClassString_NS "-Subnormal" michael@0: #define DEC_ClassString_NZ "-Zero" michael@0: #define DEC_ClassString_PZ "+Zero" michael@0: #define DEC_ClassString_PS "+Subnormal" michael@0: #define DEC_ClassString_PN "+Normal" michael@0: #define DEC_ClassString_PI "+Infinity" michael@0: #define DEC_ClassString_UN "Invalid" michael@0: michael@0: /* Trap-enabler and Status flags (exceptional conditions), and */ michael@0: /* their names. The top byte is reserved for internal use */ michael@0: #if DECEXTFLAG michael@0: /* Extended flags */ michael@0: #define DEC_Conversion_syntax 0x00000001 michael@0: #define DEC_Division_by_zero 0x00000002 michael@0: #define DEC_Division_impossible 0x00000004 michael@0: #define DEC_Division_undefined 0x00000008 michael@0: #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ michael@0: #define DEC_Inexact 0x00000020 michael@0: #define DEC_Invalid_context 0x00000040 michael@0: #define DEC_Invalid_operation 0x00000080 michael@0: #if DECSUBSET michael@0: #define DEC_Lost_digits 0x00000100 michael@0: #endif michael@0: #define DEC_Overflow 0x00000200 michael@0: #define DEC_Clamped 0x00000400 michael@0: #define DEC_Rounded 0x00000800 michael@0: #define DEC_Subnormal 0x00001000 michael@0: #define DEC_Underflow 0x00002000 michael@0: #else michael@0: /* IEEE flags only */ michael@0: #define DEC_Conversion_syntax 0x00000010 michael@0: #define DEC_Division_by_zero 0x00000002 michael@0: #define DEC_Division_impossible 0x00000010 michael@0: #define DEC_Division_undefined 0x00000010 michael@0: #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ michael@0: #define DEC_Inexact 0x00000001 michael@0: #define DEC_Invalid_context 0x00000010 michael@0: #define DEC_Invalid_operation 0x00000010 michael@0: #if DECSUBSET michael@0: #define DEC_Lost_digits 0x00000000 michael@0: #endif michael@0: #define DEC_Overflow 0x00000008 michael@0: #define DEC_Clamped 0x00000000 michael@0: #define DEC_Rounded 0x00000000 michael@0: #define DEC_Subnormal 0x00000000 michael@0: #define DEC_Underflow 0x00000004 michael@0: #endif michael@0: michael@0: /* IEEE 754 groupings for the flags */ michael@0: /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */ michael@0: /* are not in IEEE 754] */ michael@0: #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero) michael@0: #if DECSUBSET michael@0: #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits) michael@0: #else michael@0: #define DEC_IEEE_754_Inexact (DEC_Inexact) michael@0: #endif michael@0: #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \ michael@0: DEC_Division_impossible | \ michael@0: DEC_Division_undefined | \ michael@0: DEC_Insufficient_storage | \ michael@0: DEC_Invalid_context | \ michael@0: DEC_Invalid_operation) michael@0: #define DEC_IEEE_754_Overflow (DEC_Overflow) michael@0: #define DEC_IEEE_754_Underflow (DEC_Underflow) michael@0: michael@0: /* flags which are normally errors (result is qNaN, infinite, or 0) */ michael@0: #define DEC_Errors (DEC_IEEE_754_Division_by_zero | \ michael@0: DEC_IEEE_754_Invalid_operation | \ michael@0: DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow) michael@0: /* flags which cause a result to become qNaN */ michael@0: #define DEC_NaNs DEC_IEEE_754_Invalid_operation michael@0: michael@0: /* flags which are normally for information only (finite results) */ michael@0: #if DECSUBSET michael@0: #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \ michael@0: | DEC_Lost_digits) michael@0: #else michael@0: #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact) michael@0: #endif michael@0: michael@0: /* IEEE 854 names (for compatibility with older decNumber versions) */ michael@0: #define DEC_IEEE_854_Division_by_zero DEC_IEEE_754_Division_by_zero michael@0: #define DEC_IEEE_854_Inexact DEC_IEEE_754_Inexact michael@0: #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation michael@0: #define DEC_IEEE_854_Overflow DEC_IEEE_754_Overflow michael@0: #define DEC_IEEE_854_Underflow DEC_IEEE_754_Underflow michael@0: michael@0: /* Name strings for the exceptional conditions */ michael@0: #define DEC_Condition_CS "Conversion syntax" michael@0: #define DEC_Condition_DZ "Division by zero" michael@0: #define DEC_Condition_DI "Division impossible" michael@0: #define DEC_Condition_DU "Division undefined" michael@0: #define DEC_Condition_IE "Inexact" michael@0: #define DEC_Condition_IS "Insufficient storage" michael@0: #define DEC_Condition_IC "Invalid context" michael@0: #define DEC_Condition_IO "Invalid operation" michael@0: #if DECSUBSET michael@0: #define DEC_Condition_LD "Lost digits" michael@0: #endif michael@0: #define DEC_Condition_OV "Overflow" michael@0: #define DEC_Condition_PA "Clamped" michael@0: #define DEC_Condition_RO "Rounded" michael@0: #define DEC_Condition_SU "Subnormal" michael@0: #define DEC_Condition_UN "Underflow" michael@0: #define DEC_Condition_ZE "No status" michael@0: #define DEC_Condition_MU "Multiple status" michael@0: #define DEC_Condition_Length 21 /* length of the longest string, */ michael@0: /* including terminator */ michael@0: michael@0: /* Initialization descriptors, used by decContextDefault */ michael@0: #define DEC_INIT_BASE 0 michael@0: #define DEC_INIT_DECIMAL32 32 michael@0: #define DEC_INIT_DECIMAL64 64 michael@0: #define DEC_INIT_DECIMAL128 128 michael@0: /* Synonyms */ michael@0: #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32 michael@0: #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64 michael@0: #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128 michael@0: michael@0: /* decContext routines */ michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextClearStatus(decContext *, uint32_t); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextDefault(decContext *, int32_t); michael@0: U_INTERNAL enum rounding U_EXPORT2 uprv_decContextGetRounding(decContext *); michael@0: U_INTERNAL uint32_t U_EXPORT2 uprv_decContextGetStatus(decContext *); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextRestoreStatus(decContext *, uint32_t, uint32_t); michael@0: U_INTERNAL uint32_t U_EXPORT2 uprv_decContextSaveStatus(decContext *, uint32_t); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetRounding(decContext *, enum rounding); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatus(decContext *, uint32_t); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusFromString(decContext *, const char *); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusFromStringQuiet(decContext *, const char *); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusQuiet(decContext *, uint32_t); michael@0: U_INTERNAL const char * U_EXPORT2 uprv_decContextStatusToString(const decContext *); michael@0: U_INTERNAL int32_t U_EXPORT2 uprv_decContextTestEndian(uint8_t); michael@0: U_INTERNAL uint32_t U_EXPORT2 uprv_decContextTestSavedStatus(uint32_t, uint32_t); michael@0: U_INTERNAL uint32_t U_EXPORT2 uprv_decContextTestStatus(decContext *, uint32_t); michael@0: U_INTERNAL decContext * U_EXPORT2 uprv_decContextZeroStatus(decContext *); michael@0: michael@0: #endif