1*e4b17023SJohn Marino /* Decimal context header module for the decNumber C Library. 2*e4b17023SJohn Marino Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc. 3*e4b17023SJohn Marino Contributed by IBM Corporation. Author Mike Cowlishaw. 4*e4b17023SJohn Marino 5*e4b17023SJohn Marino This file is part of GCC. 6*e4b17023SJohn Marino 7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under 8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free 9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later 10*e4b17023SJohn Marino version. 11*e4b17023SJohn Marino 12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or 14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15*e4b17023SJohn Marino for more details. 16*e4b17023SJohn Marino 17*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional 18*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version 19*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation. 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and 22*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program; 23*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */ 25*e4b17023SJohn Marino 26*e4b17023SJohn Marino /* ------------------------------------------------------------------ */ 27*e4b17023SJohn Marino /* Decimal Context module header */ 28*e4b17023SJohn Marino /* ------------------------------------------------------------------ */ 29*e4b17023SJohn Marino /* */ 30*e4b17023SJohn Marino /* Context variables must always have valid values: */ 31*e4b17023SJohn Marino /* */ 32*e4b17023SJohn Marino /* status -- [any bits may be cleared, but not set, by user] */ 33*e4b17023SJohn Marino /* round -- must be one of the enumerated rounding modes */ 34*e4b17023SJohn Marino /* */ 35*e4b17023SJohn Marino /* The following variables are implied for fixed size formats (i.e., */ 36*e4b17023SJohn Marino /* they are ignored) but should still be set correctly in case used */ 37*e4b17023SJohn Marino /* with decNumber functions: */ 38*e4b17023SJohn Marino /* */ 39*e4b17023SJohn Marino /* clamp -- must be either 0 or 1 */ 40*e4b17023SJohn Marino /* digits -- must be in the range 1 through 999999999 */ 41*e4b17023SJohn Marino /* emax -- must be in the range 0 through 999999999 */ 42*e4b17023SJohn Marino /* emin -- must be in the range 0 through -999999999 */ 43*e4b17023SJohn Marino /* extended -- must be either 0 or 1 [present only if DECSUBSET] */ 44*e4b17023SJohn Marino /* traps -- only defined bits may be set */ 45*e4b17023SJohn Marino /* */ 46*e4b17023SJohn Marino /* ------------------------------------------------------------------ */ 47*e4b17023SJohn Marino 48*e4b17023SJohn Marino #if !defined(DECCONTEXT) 49*e4b17023SJohn Marino #define DECCONTEXT 50*e4b17023SJohn Marino #define DECCNAME "decContext" /* Short name */ 51*e4b17023SJohn Marino #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */ 52*e4b17023SJohn Marino #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */ 53*e4b17023SJohn Marino 54*e4b17023SJohn Marino #include "gstdint.h" /* C99 standard integers */ 55*e4b17023SJohn Marino #include <stdio.h> /* for printf, etc. */ 56*e4b17023SJohn Marino #include <signal.h> /* for traps */ 57*e4b17023SJohn Marino 58*e4b17023SJohn Marino /* Extended flags setting -- set this to 0 to use only IEEE flags */ 59*e4b17023SJohn Marino #if !defined(DECEXTFLAG) 60*e4b17023SJohn Marino #define DECEXTFLAG 1 /* 1=enable extended flags */ 61*e4b17023SJohn Marino #endif 62*e4b17023SJohn Marino 63*e4b17023SJohn Marino /* Conditional code flag -- set this to 0 for best performance */ 64*e4b17023SJohn Marino #if !defined(DECSUBSET) 65*e4b17023SJohn Marino #define DECSUBSET 0 /* 1=enable subset arithmetic */ 66*e4b17023SJohn Marino #endif 67*e4b17023SJohn Marino 68*e4b17023SJohn Marino /* Context for operations, with associated constants */ 69*e4b17023SJohn Marino enum rounding { 70*e4b17023SJohn Marino DEC_ROUND_CEILING, /* round towards +infinity */ 71*e4b17023SJohn Marino DEC_ROUND_UP, /* round away from 0 */ 72*e4b17023SJohn Marino DEC_ROUND_HALF_UP, /* 0.5 rounds up */ 73*e4b17023SJohn Marino DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */ 74*e4b17023SJohn Marino DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */ 75*e4b17023SJohn Marino DEC_ROUND_DOWN, /* round towards 0 (truncate) */ 76*e4b17023SJohn Marino DEC_ROUND_FLOOR, /* round towards -infinity */ 77*e4b17023SJohn Marino DEC_ROUND_05UP, /* round for reround */ 78*e4b17023SJohn Marino DEC_ROUND_MAX /* enum must be less than this */ 79*e4b17023SJohn Marino }; 80*e4b17023SJohn Marino #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN; 81*e4b17023SJohn Marino 82*e4b17023SJohn Marino typedef struct { 83*e4b17023SJohn Marino int32_t digits; /* working precision */ 84*e4b17023SJohn Marino int32_t emax; /* maximum positive exponent */ 85*e4b17023SJohn Marino int32_t emin; /* minimum negative exponent */ 86*e4b17023SJohn Marino enum rounding round; /* rounding mode */ 87*e4b17023SJohn Marino uint32_t traps; /* trap-enabler flags */ 88*e4b17023SJohn Marino uint32_t status; /* status flags */ 89*e4b17023SJohn Marino uint8_t clamp; /* flag: apply IEEE exponent clamp */ 90*e4b17023SJohn Marino #if DECSUBSET 91*e4b17023SJohn Marino uint8_t extended; /* flag: special-values allowed */ 92*e4b17023SJohn Marino #endif 93*e4b17023SJohn Marino } decContext; 94*e4b17023SJohn Marino 95*e4b17023SJohn Marino /* Maxima and Minima for context settings */ 96*e4b17023SJohn Marino #define DEC_MAX_DIGITS 999999999 97*e4b17023SJohn Marino #define DEC_MIN_DIGITS 1 98*e4b17023SJohn Marino #define DEC_MAX_EMAX 999999999 99*e4b17023SJohn Marino #define DEC_MIN_EMAX 0 100*e4b17023SJohn Marino #define DEC_MAX_EMIN 0 101*e4b17023SJohn Marino #define DEC_MIN_EMIN -999999999 102*e4b17023SJohn Marino #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */ 103*e4b17023SJohn Marino 104*e4b17023SJohn Marino /* Classifications for decimal numbers, aligned with 754 (note that */ 105*e4b17023SJohn Marino /* 'normal' and 'subnormal' are meaningful only with a decContext */ 106*e4b17023SJohn Marino /* or a fixed size format). */ 107*e4b17023SJohn Marino enum decClass { 108*e4b17023SJohn Marino DEC_CLASS_SNAN, 109*e4b17023SJohn Marino DEC_CLASS_QNAN, 110*e4b17023SJohn Marino DEC_CLASS_NEG_INF, 111*e4b17023SJohn Marino DEC_CLASS_NEG_NORMAL, 112*e4b17023SJohn Marino DEC_CLASS_NEG_SUBNORMAL, 113*e4b17023SJohn Marino DEC_CLASS_NEG_ZERO, 114*e4b17023SJohn Marino DEC_CLASS_POS_ZERO, 115*e4b17023SJohn Marino DEC_CLASS_POS_SUBNORMAL, 116*e4b17023SJohn Marino DEC_CLASS_POS_NORMAL, 117*e4b17023SJohn Marino DEC_CLASS_POS_INF 118*e4b17023SJohn Marino }; 119*e4b17023SJohn Marino /* Strings for the decClasses */ 120*e4b17023SJohn Marino #define DEC_ClassString_SN "sNaN" 121*e4b17023SJohn Marino #define DEC_ClassString_QN "NaN" 122*e4b17023SJohn Marino #define DEC_ClassString_NI "-Infinity" 123*e4b17023SJohn Marino #define DEC_ClassString_NN "-Normal" 124*e4b17023SJohn Marino #define DEC_ClassString_NS "-Subnormal" 125*e4b17023SJohn Marino #define DEC_ClassString_NZ "-Zero" 126*e4b17023SJohn Marino #define DEC_ClassString_PZ "+Zero" 127*e4b17023SJohn Marino #define DEC_ClassString_PS "+Subnormal" 128*e4b17023SJohn Marino #define DEC_ClassString_PN "+Normal" 129*e4b17023SJohn Marino #define DEC_ClassString_PI "+Infinity" 130*e4b17023SJohn Marino #define DEC_ClassString_UN "Invalid" 131*e4b17023SJohn Marino 132*e4b17023SJohn Marino /* Trap-enabler and Status flags (exceptional conditions), and */ 133*e4b17023SJohn Marino /* their names. The top byte is reserved for internal use */ 134*e4b17023SJohn Marino #if DECEXTFLAG 135*e4b17023SJohn Marino /* Extended flags */ 136*e4b17023SJohn Marino #define DEC_Conversion_syntax 0x00000001 137*e4b17023SJohn Marino #define DEC_Division_by_zero 0x00000002 138*e4b17023SJohn Marino #define DEC_Division_impossible 0x00000004 139*e4b17023SJohn Marino #define DEC_Division_undefined 0x00000008 140*e4b17023SJohn Marino #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ 141*e4b17023SJohn Marino #define DEC_Inexact 0x00000020 142*e4b17023SJohn Marino #define DEC_Invalid_context 0x00000040 143*e4b17023SJohn Marino #define DEC_Invalid_operation 0x00000080 144*e4b17023SJohn Marino #if DECSUBSET 145*e4b17023SJohn Marino #define DEC_Lost_digits 0x00000100 146*e4b17023SJohn Marino #endif 147*e4b17023SJohn Marino #define DEC_Overflow 0x00000200 148*e4b17023SJohn Marino #define DEC_Clamped 0x00000400 149*e4b17023SJohn Marino #define DEC_Rounded 0x00000800 150*e4b17023SJohn Marino #define DEC_Subnormal 0x00001000 151*e4b17023SJohn Marino #define DEC_Underflow 0x00002000 152*e4b17023SJohn Marino #else 153*e4b17023SJohn Marino /* IEEE flags only */ 154*e4b17023SJohn Marino #define DEC_Conversion_syntax 0x00000010 155*e4b17023SJohn Marino #define DEC_Division_by_zero 0x00000002 156*e4b17023SJohn Marino #define DEC_Division_impossible 0x00000010 157*e4b17023SJohn Marino #define DEC_Division_undefined 0x00000010 158*e4b17023SJohn Marino #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ 159*e4b17023SJohn Marino #define DEC_Inexact 0x00000001 160*e4b17023SJohn Marino #define DEC_Invalid_context 0x00000010 161*e4b17023SJohn Marino #define DEC_Invalid_operation 0x00000010 162*e4b17023SJohn Marino #if DECSUBSET 163*e4b17023SJohn Marino #define DEC_Lost_digits 0x00000000 164*e4b17023SJohn Marino #endif 165*e4b17023SJohn Marino #define DEC_Overflow 0x00000008 166*e4b17023SJohn Marino #define DEC_Clamped 0x00000000 167*e4b17023SJohn Marino #define DEC_Rounded 0x00000000 168*e4b17023SJohn Marino #define DEC_Subnormal 0x00000000 169*e4b17023SJohn Marino #define DEC_Underflow 0x00000004 170*e4b17023SJohn Marino #endif 171*e4b17023SJohn Marino 172*e4b17023SJohn Marino /* IEEE 754 groupings for the flags */ 173*e4b17023SJohn Marino /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */ 174*e4b17023SJohn Marino /* are not in IEEE 754] */ 175*e4b17023SJohn Marino #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero) 176*e4b17023SJohn Marino #if DECSUBSET 177*e4b17023SJohn Marino #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits) 178*e4b17023SJohn Marino #else 179*e4b17023SJohn Marino #define DEC_IEEE_754_Inexact (DEC_Inexact) 180*e4b17023SJohn Marino #endif 181*e4b17023SJohn Marino #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \ 182*e4b17023SJohn Marino DEC_Division_impossible | \ 183*e4b17023SJohn Marino DEC_Division_undefined | \ 184*e4b17023SJohn Marino DEC_Insufficient_storage | \ 185*e4b17023SJohn Marino DEC_Invalid_context | \ 186*e4b17023SJohn Marino DEC_Invalid_operation) 187*e4b17023SJohn Marino #define DEC_IEEE_754_Overflow (DEC_Overflow) 188*e4b17023SJohn Marino #define DEC_IEEE_754_Underflow (DEC_Underflow) 189*e4b17023SJohn Marino 190*e4b17023SJohn Marino /* flags which are normally errors (result is qNaN, infinite, or 0) */ 191*e4b17023SJohn Marino #define DEC_Errors (DEC_IEEE_754_Division_by_zero | \ 192*e4b17023SJohn Marino DEC_IEEE_754_Invalid_operation | \ 193*e4b17023SJohn Marino DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow) 194*e4b17023SJohn Marino /* flags which cause a result to become qNaN */ 195*e4b17023SJohn Marino #define DEC_NaNs DEC_IEEE_754_Invalid_operation 196*e4b17023SJohn Marino 197*e4b17023SJohn Marino /* flags which are normally for information only (finite results) */ 198*e4b17023SJohn Marino #if DECSUBSET 199*e4b17023SJohn Marino #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \ 200*e4b17023SJohn Marino | DEC_Lost_digits) 201*e4b17023SJohn Marino #else 202*e4b17023SJohn Marino #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact) 203*e4b17023SJohn Marino #endif 204*e4b17023SJohn Marino 205*e4b17023SJohn Marino /* IEEE 854 names (for compatibility with older decNumber versions) */ 206*e4b17023SJohn Marino #define DEC_IEEE_854_Division_by_zero DEC_IEEE_754_Division_by_zero 207*e4b17023SJohn Marino #define DEC_IEEE_854_Inexact DEC_IEEE_754_Inexact 208*e4b17023SJohn Marino #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation 209*e4b17023SJohn Marino #define DEC_IEEE_854_Overflow DEC_IEEE_754_Overflow 210*e4b17023SJohn Marino #define DEC_IEEE_854_Underflow DEC_IEEE_754_Underflow 211*e4b17023SJohn Marino 212*e4b17023SJohn Marino /* Name strings for the exceptional conditions */ 213*e4b17023SJohn Marino #define DEC_Condition_CS "Conversion syntax" 214*e4b17023SJohn Marino #define DEC_Condition_DZ "Division by zero" 215*e4b17023SJohn Marino #define DEC_Condition_DI "Division impossible" 216*e4b17023SJohn Marino #define DEC_Condition_DU "Division undefined" 217*e4b17023SJohn Marino #define DEC_Condition_IE "Inexact" 218*e4b17023SJohn Marino #define DEC_Condition_IS "Insufficient storage" 219*e4b17023SJohn Marino #define DEC_Condition_IC "Invalid context" 220*e4b17023SJohn Marino #define DEC_Condition_IO "Invalid operation" 221*e4b17023SJohn Marino #if DECSUBSET 222*e4b17023SJohn Marino #define DEC_Condition_LD "Lost digits" 223*e4b17023SJohn Marino #endif 224*e4b17023SJohn Marino #define DEC_Condition_OV "Overflow" 225*e4b17023SJohn Marino #define DEC_Condition_PA "Clamped" 226*e4b17023SJohn Marino #define DEC_Condition_RO "Rounded" 227*e4b17023SJohn Marino #define DEC_Condition_SU "Subnormal" 228*e4b17023SJohn Marino #define DEC_Condition_UN "Underflow" 229*e4b17023SJohn Marino #define DEC_Condition_ZE "No status" 230*e4b17023SJohn Marino #define DEC_Condition_MU "Multiple status" 231*e4b17023SJohn Marino #define DEC_Condition_Length 21 /* length of the longest string, */ 232*e4b17023SJohn Marino /* including terminator */ 233*e4b17023SJohn Marino 234*e4b17023SJohn Marino /* Initialization descriptors, used by decContextDefault */ 235*e4b17023SJohn Marino #define DEC_INIT_BASE 0 236*e4b17023SJohn Marino #define DEC_INIT_DECIMAL32 32 237*e4b17023SJohn Marino #define DEC_INIT_DECIMAL64 64 238*e4b17023SJohn Marino #define DEC_INIT_DECIMAL128 128 239*e4b17023SJohn Marino /* Synonyms */ 240*e4b17023SJohn Marino #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32 241*e4b17023SJohn Marino #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64 242*e4b17023SJohn Marino #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128 243*e4b17023SJohn Marino 244*e4b17023SJohn Marino /* decContext routines */ 245*e4b17023SJohn Marino 246*e4b17023SJohn Marino #include "decContextSymbols.h" 247*e4b17023SJohn Marino 248*e4b17023SJohn Marino #ifdef __cplusplus 249*e4b17023SJohn Marino extern "C" { 250*e4b17023SJohn Marino #endif 251*e4b17023SJohn Marino 252*e4b17023SJohn Marino extern decContext * decContextClearStatus(decContext *, uint32_t); 253*e4b17023SJohn Marino extern decContext * decContextDefault(decContext *, int32_t); 254*e4b17023SJohn Marino extern enum rounding decContextGetRounding(decContext *); 255*e4b17023SJohn Marino extern uint32_t decContextGetStatus(decContext *); 256*e4b17023SJohn Marino extern decContext * decContextRestoreStatus(decContext *, uint32_t, uint32_t); 257*e4b17023SJohn Marino extern uint32_t decContextSaveStatus(decContext *, uint32_t); 258*e4b17023SJohn Marino extern decContext * decContextSetRounding(decContext *, enum rounding); 259*e4b17023SJohn Marino extern decContext * decContextSetStatus(decContext *, uint32_t); 260*e4b17023SJohn Marino extern decContext * decContextSetStatusFromString(decContext *, const char *); 261*e4b17023SJohn Marino extern decContext * decContextSetStatusFromStringQuiet(decContext *, const char *); 262*e4b17023SJohn Marino extern decContext * decContextSetStatusQuiet(decContext *, uint32_t); 263*e4b17023SJohn Marino extern const char * decContextStatusToString(const decContext *); 264*e4b17023SJohn Marino extern int32_t decContextTestEndian(uint8_t); 265*e4b17023SJohn Marino extern uint32_t decContextTestSavedStatus(uint32_t, uint32_t); 266*e4b17023SJohn Marino extern uint32_t decContextTestStatus(decContext *, uint32_t); 267*e4b17023SJohn Marino extern decContext * decContextZeroStatus(decContext *); 268*e4b17023SJohn Marino 269*e4b17023SJohn Marino #ifdef __cplusplus 270*e4b17023SJohn Marino } 271*e4b17023SJohn Marino #endif 272*e4b17023SJohn Marino 273*e4b17023SJohn Marino #endif 274