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