Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* ------------------------------------------------------------------ */ |
michael@0 | 2 | /* Decimal Context module header */ |
michael@0 | 3 | /* ------------------------------------------------------------------ */ |
michael@0 | 4 | /* Copyright (c) IBM Corporation, 2000-2011. All rights reserved. */ |
michael@0 | 5 | /* */ |
michael@0 | 6 | /* This software is made available under the terms of the */ |
michael@0 | 7 | /* ICU License -- ICU 1.8.1 and later. */ |
michael@0 | 8 | /* */ |
michael@0 | 9 | /* The description and User's Guide ("The decNumber C Library") for */ |
michael@0 | 10 | /* this software is called decNumber.pdf. This document is */ |
michael@0 | 11 | /* available, together with arithmetic and format specifications, */ |
michael@0 | 12 | /* testcases, and Web links, on the General Decimal Arithmetic page. */ |
michael@0 | 13 | /* */ |
michael@0 | 14 | /* Please send comments, suggestions, and corrections to the author: */ |
michael@0 | 15 | /* mfc@uk.ibm.com */ |
michael@0 | 16 | /* Mike Cowlishaw, IBM Fellow */ |
michael@0 | 17 | /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */ |
michael@0 | 18 | /* ------------------------------------------------------------------ */ |
michael@0 | 19 | |
michael@0 | 20 | /* Modified version, for use from within ICU. |
michael@0 | 21 | * Renamed public functions, to avoid an unwanted export of the |
michael@0 | 22 | * standard names from the ICU library. |
michael@0 | 23 | * |
michael@0 | 24 | * Use ICU's uprv_malloc() and uprv_free() |
michael@0 | 25 | * |
michael@0 | 26 | * Revert comment syntax to plain C |
michael@0 | 27 | * |
michael@0 | 28 | * Remove a few compiler warnings. |
michael@0 | 29 | */ |
michael@0 | 30 | #include "unicode/utypes.h" |
michael@0 | 31 | #include "putilimp.h" |
michael@0 | 32 | |
michael@0 | 33 | /* */ |
michael@0 | 34 | /* Context variables must always have valid values: */ |
michael@0 | 35 | /* */ |
michael@0 | 36 | /* status -- [any bits may be cleared, but not set, by user] */ |
michael@0 | 37 | /* round -- must be one of the enumerated rounding modes */ |
michael@0 | 38 | /* */ |
michael@0 | 39 | /* The following variables are implied for fixed size formats (i.e., */ |
michael@0 | 40 | /* they are ignored) but should still be set correctly in case used */ |
michael@0 | 41 | /* with decNumber functions: */ |
michael@0 | 42 | /* */ |
michael@0 | 43 | /* clamp -- must be either 0 or 1 */ |
michael@0 | 44 | /* digits -- must be in the range 1 through 999999999 */ |
michael@0 | 45 | /* emax -- must be in the range 0 through 999999999 */ |
michael@0 | 46 | /* emin -- must be in the range 0 through -999999999 */ |
michael@0 | 47 | /* extended -- must be either 0 or 1 [present only if DECSUBSET] */ |
michael@0 | 48 | /* traps -- only defined bits may be set */ |
michael@0 | 49 | /* */ |
michael@0 | 50 | /* ------------------------------------------------------------------ */ |
michael@0 | 51 | |
michael@0 | 52 | #if !defined(DECCONTEXT) |
michael@0 | 53 | #define DECCONTEXT |
michael@0 | 54 | #define DECCNAME "decContext" /* Short name */ |
michael@0 | 55 | #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */ |
michael@0 | 56 | #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */ |
michael@0 | 57 | |
michael@0 | 58 | #if !defined(int32_t) |
michael@0 | 59 | /* #include <stdint.h> */ /* C99 standard integers */ |
michael@0 | 60 | #endif |
michael@0 | 61 | #include <stdio.h> /* for printf, etc. */ |
michael@0 | 62 | #include <signal.h> /* for traps */ |
michael@0 | 63 | |
michael@0 | 64 | /* Extended flags setting -- set this to 0 to use only IEEE flags */ |
michael@0 | 65 | #if !defined(DECEXTFLAG) |
michael@0 | 66 | #define DECEXTFLAG 1 /* 1=enable extended flags */ |
michael@0 | 67 | #endif |
michael@0 | 68 | |
michael@0 | 69 | /* Conditional code flag -- set this to 0 for best performance */ |
michael@0 | 70 | #if !defined(DECSUBSET) |
michael@0 | 71 | #define DECSUBSET 0 /* 1=enable subset arithmetic */ |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | /* Context for operations, with associated constants */ |
michael@0 | 75 | enum rounding { |
michael@0 | 76 | DEC_ROUND_CEILING, /* round towards +infinity */ |
michael@0 | 77 | DEC_ROUND_UP, /* round away from 0 */ |
michael@0 | 78 | DEC_ROUND_HALF_UP, /* 0.5 rounds up */ |
michael@0 | 79 | DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */ |
michael@0 | 80 | DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */ |
michael@0 | 81 | DEC_ROUND_DOWN, /* round towards 0 (truncate) */ |
michael@0 | 82 | DEC_ROUND_FLOOR, /* round towards -infinity */ |
michael@0 | 83 | DEC_ROUND_05UP, /* round for reround */ |
michael@0 | 84 | DEC_ROUND_MAX /* enum must be less than this */ |
michael@0 | 85 | }; |
michael@0 | 86 | #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN; |
michael@0 | 87 | |
michael@0 | 88 | typedef struct { |
michael@0 | 89 | int32_t digits; /* working precision */ |
michael@0 | 90 | int32_t emax; /* maximum positive exponent */ |
michael@0 | 91 | int32_t emin; /* minimum negative exponent */ |
michael@0 | 92 | enum rounding round; /* rounding mode */ |
michael@0 | 93 | uint32_t traps; /* trap-enabler flags */ |
michael@0 | 94 | uint32_t status; /* status flags */ |
michael@0 | 95 | uint8_t clamp; /* flag: apply IEEE exponent clamp */ |
michael@0 | 96 | #if DECSUBSET |
michael@0 | 97 | uint8_t extended; /* flag: special-values allowed */ |
michael@0 | 98 | #endif |
michael@0 | 99 | } decContext; |
michael@0 | 100 | |
michael@0 | 101 | /* Maxima and Minima for context settings */ |
michael@0 | 102 | #define DEC_MAX_DIGITS 999999999 |
michael@0 | 103 | #define DEC_MIN_DIGITS 1 |
michael@0 | 104 | #define DEC_MAX_EMAX 999999999 |
michael@0 | 105 | #define DEC_MIN_EMAX 0 |
michael@0 | 106 | #define DEC_MAX_EMIN 0 |
michael@0 | 107 | #define DEC_MIN_EMIN -999999999 |
michael@0 | 108 | #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */ |
michael@0 | 109 | |
michael@0 | 110 | /* Classifications for decimal numbers, aligned with 754 (note that */ |
michael@0 | 111 | /* 'normal' and 'subnormal' are meaningful only with a decContext */ |
michael@0 | 112 | /* or a fixed size format). */ |
michael@0 | 113 | enum decClass { |
michael@0 | 114 | DEC_CLASS_SNAN, |
michael@0 | 115 | DEC_CLASS_QNAN, |
michael@0 | 116 | DEC_CLASS_NEG_INF, |
michael@0 | 117 | DEC_CLASS_NEG_NORMAL, |
michael@0 | 118 | DEC_CLASS_NEG_SUBNORMAL, |
michael@0 | 119 | DEC_CLASS_NEG_ZERO, |
michael@0 | 120 | DEC_CLASS_POS_ZERO, |
michael@0 | 121 | DEC_CLASS_POS_SUBNORMAL, |
michael@0 | 122 | DEC_CLASS_POS_NORMAL, |
michael@0 | 123 | DEC_CLASS_POS_INF |
michael@0 | 124 | }; |
michael@0 | 125 | /* Strings for the decClasses */ |
michael@0 | 126 | #define DEC_ClassString_SN "sNaN" |
michael@0 | 127 | #define DEC_ClassString_QN "NaN" |
michael@0 | 128 | #define DEC_ClassString_NI "-Infinity" |
michael@0 | 129 | #define DEC_ClassString_NN "-Normal" |
michael@0 | 130 | #define DEC_ClassString_NS "-Subnormal" |
michael@0 | 131 | #define DEC_ClassString_NZ "-Zero" |
michael@0 | 132 | #define DEC_ClassString_PZ "+Zero" |
michael@0 | 133 | #define DEC_ClassString_PS "+Subnormal" |
michael@0 | 134 | #define DEC_ClassString_PN "+Normal" |
michael@0 | 135 | #define DEC_ClassString_PI "+Infinity" |
michael@0 | 136 | #define DEC_ClassString_UN "Invalid" |
michael@0 | 137 | |
michael@0 | 138 | /* Trap-enabler and Status flags (exceptional conditions), and */ |
michael@0 | 139 | /* their names. The top byte is reserved for internal use */ |
michael@0 | 140 | #if DECEXTFLAG |
michael@0 | 141 | /* Extended flags */ |
michael@0 | 142 | #define DEC_Conversion_syntax 0x00000001 |
michael@0 | 143 | #define DEC_Division_by_zero 0x00000002 |
michael@0 | 144 | #define DEC_Division_impossible 0x00000004 |
michael@0 | 145 | #define DEC_Division_undefined 0x00000008 |
michael@0 | 146 | #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ |
michael@0 | 147 | #define DEC_Inexact 0x00000020 |
michael@0 | 148 | #define DEC_Invalid_context 0x00000040 |
michael@0 | 149 | #define DEC_Invalid_operation 0x00000080 |
michael@0 | 150 | #if DECSUBSET |
michael@0 | 151 | #define DEC_Lost_digits 0x00000100 |
michael@0 | 152 | #endif |
michael@0 | 153 | #define DEC_Overflow 0x00000200 |
michael@0 | 154 | #define DEC_Clamped 0x00000400 |
michael@0 | 155 | #define DEC_Rounded 0x00000800 |
michael@0 | 156 | #define DEC_Subnormal 0x00001000 |
michael@0 | 157 | #define DEC_Underflow 0x00002000 |
michael@0 | 158 | #else |
michael@0 | 159 | /* IEEE flags only */ |
michael@0 | 160 | #define DEC_Conversion_syntax 0x00000010 |
michael@0 | 161 | #define DEC_Division_by_zero 0x00000002 |
michael@0 | 162 | #define DEC_Division_impossible 0x00000010 |
michael@0 | 163 | #define DEC_Division_undefined 0x00000010 |
michael@0 | 164 | #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ |
michael@0 | 165 | #define DEC_Inexact 0x00000001 |
michael@0 | 166 | #define DEC_Invalid_context 0x00000010 |
michael@0 | 167 | #define DEC_Invalid_operation 0x00000010 |
michael@0 | 168 | #if DECSUBSET |
michael@0 | 169 | #define DEC_Lost_digits 0x00000000 |
michael@0 | 170 | #endif |
michael@0 | 171 | #define DEC_Overflow 0x00000008 |
michael@0 | 172 | #define DEC_Clamped 0x00000000 |
michael@0 | 173 | #define DEC_Rounded 0x00000000 |
michael@0 | 174 | #define DEC_Subnormal 0x00000000 |
michael@0 | 175 | #define DEC_Underflow 0x00000004 |
michael@0 | 176 | #endif |
michael@0 | 177 | |
michael@0 | 178 | /* IEEE 754 groupings for the flags */ |
michael@0 | 179 | /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */ |
michael@0 | 180 | /* are not in IEEE 754] */ |
michael@0 | 181 | #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero) |
michael@0 | 182 | #if DECSUBSET |
michael@0 | 183 | #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits) |
michael@0 | 184 | #else |
michael@0 | 185 | #define DEC_IEEE_754_Inexact (DEC_Inexact) |
michael@0 | 186 | #endif |
michael@0 | 187 | #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \ |
michael@0 | 188 | DEC_Division_impossible | \ |
michael@0 | 189 | DEC_Division_undefined | \ |
michael@0 | 190 | DEC_Insufficient_storage | \ |
michael@0 | 191 | DEC_Invalid_context | \ |
michael@0 | 192 | DEC_Invalid_operation) |
michael@0 | 193 | #define DEC_IEEE_754_Overflow (DEC_Overflow) |
michael@0 | 194 | #define DEC_IEEE_754_Underflow (DEC_Underflow) |
michael@0 | 195 | |
michael@0 | 196 | /* flags which are normally errors (result is qNaN, infinite, or 0) */ |
michael@0 | 197 | #define DEC_Errors (DEC_IEEE_754_Division_by_zero | \ |
michael@0 | 198 | DEC_IEEE_754_Invalid_operation | \ |
michael@0 | 199 | DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow) |
michael@0 | 200 | /* flags which cause a result to become qNaN */ |
michael@0 | 201 | #define DEC_NaNs DEC_IEEE_754_Invalid_operation |
michael@0 | 202 | |
michael@0 | 203 | /* flags which are normally for information only (finite results) */ |
michael@0 | 204 | #if DECSUBSET |
michael@0 | 205 | #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \ |
michael@0 | 206 | | DEC_Lost_digits) |
michael@0 | 207 | #else |
michael@0 | 208 | #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact) |
michael@0 | 209 | #endif |
michael@0 | 210 | |
michael@0 | 211 | /* IEEE 854 names (for compatibility with older decNumber versions) */ |
michael@0 | 212 | #define DEC_IEEE_854_Division_by_zero DEC_IEEE_754_Division_by_zero |
michael@0 | 213 | #define DEC_IEEE_854_Inexact DEC_IEEE_754_Inexact |
michael@0 | 214 | #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation |
michael@0 | 215 | #define DEC_IEEE_854_Overflow DEC_IEEE_754_Overflow |
michael@0 | 216 | #define DEC_IEEE_854_Underflow DEC_IEEE_754_Underflow |
michael@0 | 217 | |
michael@0 | 218 | /* Name strings for the exceptional conditions */ |
michael@0 | 219 | #define DEC_Condition_CS "Conversion syntax" |
michael@0 | 220 | #define DEC_Condition_DZ "Division by zero" |
michael@0 | 221 | #define DEC_Condition_DI "Division impossible" |
michael@0 | 222 | #define DEC_Condition_DU "Division undefined" |
michael@0 | 223 | #define DEC_Condition_IE "Inexact" |
michael@0 | 224 | #define DEC_Condition_IS "Insufficient storage" |
michael@0 | 225 | #define DEC_Condition_IC "Invalid context" |
michael@0 | 226 | #define DEC_Condition_IO "Invalid operation" |
michael@0 | 227 | #if DECSUBSET |
michael@0 | 228 | #define DEC_Condition_LD "Lost digits" |
michael@0 | 229 | #endif |
michael@0 | 230 | #define DEC_Condition_OV "Overflow" |
michael@0 | 231 | #define DEC_Condition_PA "Clamped" |
michael@0 | 232 | #define DEC_Condition_RO "Rounded" |
michael@0 | 233 | #define DEC_Condition_SU "Subnormal" |
michael@0 | 234 | #define DEC_Condition_UN "Underflow" |
michael@0 | 235 | #define DEC_Condition_ZE "No status" |
michael@0 | 236 | #define DEC_Condition_MU "Multiple status" |
michael@0 | 237 | #define DEC_Condition_Length 21 /* length of the longest string, */ |
michael@0 | 238 | /* including terminator */ |
michael@0 | 239 | |
michael@0 | 240 | /* Initialization descriptors, used by decContextDefault */ |
michael@0 | 241 | #define DEC_INIT_BASE 0 |
michael@0 | 242 | #define DEC_INIT_DECIMAL32 32 |
michael@0 | 243 | #define DEC_INIT_DECIMAL64 64 |
michael@0 | 244 | #define DEC_INIT_DECIMAL128 128 |
michael@0 | 245 | /* Synonyms */ |
michael@0 | 246 | #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32 |
michael@0 | 247 | #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64 |
michael@0 | 248 | #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128 |
michael@0 | 249 | |
michael@0 | 250 | /* decContext routines */ |
michael@0 | 251 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextClearStatus(decContext *, uint32_t); |
michael@0 | 252 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextDefault(decContext *, int32_t); |
michael@0 | 253 | U_INTERNAL enum rounding U_EXPORT2 uprv_decContextGetRounding(decContext *); |
michael@0 | 254 | U_INTERNAL uint32_t U_EXPORT2 uprv_decContextGetStatus(decContext *); |
michael@0 | 255 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextRestoreStatus(decContext *, uint32_t, uint32_t); |
michael@0 | 256 | U_INTERNAL uint32_t U_EXPORT2 uprv_decContextSaveStatus(decContext *, uint32_t); |
michael@0 | 257 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetRounding(decContext *, enum rounding); |
michael@0 | 258 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatus(decContext *, uint32_t); |
michael@0 | 259 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusFromString(decContext *, const char *); |
michael@0 | 260 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusFromStringQuiet(decContext *, const char *); |
michael@0 | 261 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextSetStatusQuiet(decContext *, uint32_t); |
michael@0 | 262 | U_INTERNAL const char * U_EXPORT2 uprv_decContextStatusToString(const decContext *); |
michael@0 | 263 | U_INTERNAL int32_t U_EXPORT2 uprv_decContextTestEndian(uint8_t); |
michael@0 | 264 | U_INTERNAL uint32_t U_EXPORT2 uprv_decContextTestSavedStatus(uint32_t, uint32_t); |
michael@0 | 265 | U_INTERNAL uint32_t U_EXPORT2 uprv_decContextTestStatus(decContext *, uint32_t); |
michael@0 | 266 | U_INTERNAL decContext * U_EXPORT2 uprv_decContextZeroStatus(decContext *); |
michael@0 | 267 | |
michael@0 | 268 | #endif |