15796c8dcSSimon Schubert /* Decimal context header module for the decNumber C Library. 2*ef5ccd6cSJohn Marino Copyright (C) 2005-2013 Free Software Foundation, Inc. 35796c8dcSSimon Schubert Contributed by IBM Corporation. Author Mike Cowlishaw. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GCC. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert GCC is free software; you can redistribute it and/or modify it under 85796c8dcSSimon Schubert the terms of the GNU General Public License as published by the Free 95796c8dcSSimon Schubert Software Foundation; either version 3, or (at your option) any later 105796c8dcSSimon Schubert version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert GCC is distributed in the hope that it will be useful, but WITHOUT ANY 135796c8dcSSimon Schubert WARRANTY; without even the implied warranty of MERCHANTABILITY or 145796c8dcSSimon Schubert FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 155796c8dcSSimon Schubert for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert Under Section 7 of GPL version 3, you are granted additional 185796c8dcSSimon Schubert permissions described in the GCC Runtime Library Exception, version 195796c8dcSSimon Schubert 3.1, as published by the Free Software Foundation. 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert You should have received a copy of the GNU General Public License and 225796c8dcSSimon Schubert a copy of the GCC Runtime Library Exception along with this program; 235796c8dcSSimon Schubert see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 245796c8dcSSimon Schubert <http://www.gnu.org/licenses/>. */ 255796c8dcSSimon Schubert 265796c8dcSSimon Schubert /* ------------------------------------------------------------------ */ 275796c8dcSSimon Schubert /* Decimal Context module header */ 285796c8dcSSimon Schubert /* ------------------------------------------------------------------ */ 295796c8dcSSimon Schubert /* */ 305796c8dcSSimon Schubert /* Context variables must always have valid values: */ 315796c8dcSSimon Schubert /* */ 325796c8dcSSimon Schubert /* status -- [any bits may be cleared, but not set, by user] */ 335796c8dcSSimon Schubert /* round -- must be one of the enumerated rounding modes */ 345796c8dcSSimon Schubert /* */ 355796c8dcSSimon Schubert /* The following variables are implied for fixed size formats (i.e., */ 365796c8dcSSimon Schubert /* they are ignored) but should still be set correctly in case used */ 375796c8dcSSimon Schubert /* with decNumber functions: */ 385796c8dcSSimon Schubert /* */ 395796c8dcSSimon Schubert /* clamp -- must be either 0 or 1 */ 405796c8dcSSimon Schubert /* digits -- must be in the range 1 through 999999999 */ 415796c8dcSSimon Schubert /* emax -- must be in the range 0 through 999999999 */ 425796c8dcSSimon Schubert /* emin -- must be in the range 0 through -999999999 */ 435796c8dcSSimon Schubert /* extended -- must be either 0 or 1 [present only if DECSUBSET] */ 445796c8dcSSimon Schubert /* traps -- only defined bits may be set */ 455796c8dcSSimon Schubert /* */ 465796c8dcSSimon Schubert /* ------------------------------------------------------------------ */ 475796c8dcSSimon Schubert 485796c8dcSSimon Schubert #if !defined(DECCONTEXT) 495796c8dcSSimon Schubert #define DECCONTEXT 505796c8dcSSimon Schubert #define DECCNAME "decContext" /* Short name */ 515796c8dcSSimon Schubert #define DECCFULLNAME "Decimal Context Descriptor" /* Verbose name */ 525796c8dcSSimon Schubert #define DECCAUTHOR "Mike Cowlishaw" /* Who to blame */ 535796c8dcSSimon Schubert 545796c8dcSSimon Schubert #include "gstdint.h" /* C99 standard integers */ 555796c8dcSSimon Schubert #include <stdio.h> /* for printf, etc. */ 565796c8dcSSimon Schubert #include <signal.h> /* for traps */ 575796c8dcSSimon Schubert 585796c8dcSSimon Schubert /* Extended flags setting -- set this to 0 to use only IEEE flags */ 595796c8dcSSimon Schubert #if !defined(DECEXTFLAG) 605796c8dcSSimon Schubert #define DECEXTFLAG 1 /* 1=enable extended flags */ 615796c8dcSSimon Schubert #endif 625796c8dcSSimon Schubert 635796c8dcSSimon Schubert /* Conditional code flag -- set this to 0 for best performance */ 645796c8dcSSimon Schubert #if !defined(DECSUBSET) 655796c8dcSSimon Schubert #define DECSUBSET 0 /* 1=enable subset arithmetic */ 665796c8dcSSimon Schubert #endif 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert /* Context for operations, with associated constants */ 695796c8dcSSimon Schubert enum rounding { 705796c8dcSSimon Schubert DEC_ROUND_CEILING, /* round towards +infinity */ 715796c8dcSSimon Schubert DEC_ROUND_UP, /* round away from 0 */ 725796c8dcSSimon Schubert DEC_ROUND_HALF_UP, /* 0.5 rounds up */ 735796c8dcSSimon Schubert DEC_ROUND_HALF_EVEN, /* 0.5 rounds to nearest even */ 745796c8dcSSimon Schubert DEC_ROUND_HALF_DOWN, /* 0.5 rounds down */ 755796c8dcSSimon Schubert DEC_ROUND_DOWN, /* round towards 0 (truncate) */ 765796c8dcSSimon Schubert DEC_ROUND_FLOOR, /* round towards -infinity */ 775796c8dcSSimon Schubert DEC_ROUND_05UP, /* round for reround */ 785796c8dcSSimon Schubert DEC_ROUND_MAX /* enum must be less than this */ 795796c8dcSSimon Schubert }; 805796c8dcSSimon Schubert #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN; 815796c8dcSSimon Schubert 825796c8dcSSimon Schubert typedef struct { 835796c8dcSSimon Schubert int32_t digits; /* working precision */ 845796c8dcSSimon Schubert int32_t emax; /* maximum positive exponent */ 855796c8dcSSimon Schubert int32_t emin; /* minimum negative exponent */ 865796c8dcSSimon Schubert enum rounding round; /* rounding mode */ 875796c8dcSSimon Schubert uint32_t traps; /* trap-enabler flags */ 885796c8dcSSimon Schubert uint32_t status; /* status flags */ 895796c8dcSSimon Schubert uint8_t clamp; /* flag: apply IEEE exponent clamp */ 905796c8dcSSimon Schubert #if DECSUBSET 915796c8dcSSimon Schubert uint8_t extended; /* flag: special-values allowed */ 925796c8dcSSimon Schubert #endif 935796c8dcSSimon Schubert } decContext; 945796c8dcSSimon Schubert 955796c8dcSSimon Schubert /* Maxima and Minima for context settings */ 965796c8dcSSimon Schubert #define DEC_MAX_DIGITS 999999999 975796c8dcSSimon Schubert #define DEC_MIN_DIGITS 1 985796c8dcSSimon Schubert #define DEC_MAX_EMAX 999999999 995796c8dcSSimon Schubert #define DEC_MIN_EMAX 0 1005796c8dcSSimon Schubert #define DEC_MAX_EMIN 0 1015796c8dcSSimon Schubert #define DEC_MIN_EMIN -999999999 1025796c8dcSSimon Schubert #define DEC_MAX_MATH 999999 /* max emax, etc., for math funcs. */ 1035796c8dcSSimon Schubert 1045796c8dcSSimon Schubert /* Classifications for decimal numbers, aligned with 754 (note that */ 1055796c8dcSSimon Schubert /* 'normal' and 'subnormal' are meaningful only with a decContext */ 1065796c8dcSSimon Schubert /* or a fixed size format). */ 1075796c8dcSSimon Schubert enum decClass { 1085796c8dcSSimon Schubert DEC_CLASS_SNAN, 1095796c8dcSSimon Schubert DEC_CLASS_QNAN, 1105796c8dcSSimon Schubert DEC_CLASS_NEG_INF, 1115796c8dcSSimon Schubert DEC_CLASS_NEG_NORMAL, 1125796c8dcSSimon Schubert DEC_CLASS_NEG_SUBNORMAL, 1135796c8dcSSimon Schubert DEC_CLASS_NEG_ZERO, 1145796c8dcSSimon Schubert DEC_CLASS_POS_ZERO, 1155796c8dcSSimon Schubert DEC_CLASS_POS_SUBNORMAL, 1165796c8dcSSimon Schubert DEC_CLASS_POS_NORMAL, 1175796c8dcSSimon Schubert DEC_CLASS_POS_INF 1185796c8dcSSimon Schubert }; 1195796c8dcSSimon Schubert /* Strings for the decClasses */ 1205796c8dcSSimon Schubert #define DEC_ClassString_SN "sNaN" 1215796c8dcSSimon Schubert #define DEC_ClassString_QN "NaN" 1225796c8dcSSimon Schubert #define DEC_ClassString_NI "-Infinity" 1235796c8dcSSimon Schubert #define DEC_ClassString_NN "-Normal" 1245796c8dcSSimon Schubert #define DEC_ClassString_NS "-Subnormal" 1255796c8dcSSimon Schubert #define DEC_ClassString_NZ "-Zero" 1265796c8dcSSimon Schubert #define DEC_ClassString_PZ "+Zero" 1275796c8dcSSimon Schubert #define DEC_ClassString_PS "+Subnormal" 1285796c8dcSSimon Schubert #define DEC_ClassString_PN "+Normal" 1295796c8dcSSimon Schubert #define DEC_ClassString_PI "+Infinity" 1305796c8dcSSimon Schubert #define DEC_ClassString_UN "Invalid" 1315796c8dcSSimon Schubert 1325796c8dcSSimon Schubert /* Trap-enabler and Status flags (exceptional conditions), and */ 1335796c8dcSSimon Schubert /* their names. The top byte is reserved for internal use */ 1345796c8dcSSimon Schubert #if DECEXTFLAG 1355796c8dcSSimon Schubert /* Extended flags */ 1365796c8dcSSimon Schubert #define DEC_Conversion_syntax 0x00000001 1375796c8dcSSimon Schubert #define DEC_Division_by_zero 0x00000002 1385796c8dcSSimon Schubert #define DEC_Division_impossible 0x00000004 1395796c8dcSSimon Schubert #define DEC_Division_undefined 0x00000008 1405796c8dcSSimon Schubert #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ 1415796c8dcSSimon Schubert #define DEC_Inexact 0x00000020 1425796c8dcSSimon Schubert #define DEC_Invalid_context 0x00000040 1435796c8dcSSimon Schubert #define DEC_Invalid_operation 0x00000080 1445796c8dcSSimon Schubert #if DECSUBSET 1455796c8dcSSimon Schubert #define DEC_Lost_digits 0x00000100 1465796c8dcSSimon Schubert #endif 1475796c8dcSSimon Schubert #define DEC_Overflow 0x00000200 1485796c8dcSSimon Schubert #define DEC_Clamped 0x00000400 1495796c8dcSSimon Schubert #define DEC_Rounded 0x00000800 1505796c8dcSSimon Schubert #define DEC_Subnormal 0x00001000 1515796c8dcSSimon Schubert #define DEC_Underflow 0x00002000 1525796c8dcSSimon Schubert #else 1535796c8dcSSimon Schubert /* IEEE flags only */ 1545796c8dcSSimon Schubert #define DEC_Conversion_syntax 0x00000010 1555796c8dcSSimon Schubert #define DEC_Division_by_zero 0x00000002 1565796c8dcSSimon Schubert #define DEC_Division_impossible 0x00000010 1575796c8dcSSimon Schubert #define DEC_Division_undefined 0x00000010 1585796c8dcSSimon Schubert #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails] */ 1595796c8dcSSimon Schubert #define DEC_Inexact 0x00000001 1605796c8dcSSimon Schubert #define DEC_Invalid_context 0x00000010 1615796c8dcSSimon Schubert #define DEC_Invalid_operation 0x00000010 1625796c8dcSSimon Schubert #if DECSUBSET 1635796c8dcSSimon Schubert #define DEC_Lost_digits 0x00000000 1645796c8dcSSimon Schubert #endif 1655796c8dcSSimon Schubert #define DEC_Overflow 0x00000008 1665796c8dcSSimon Schubert #define DEC_Clamped 0x00000000 1675796c8dcSSimon Schubert #define DEC_Rounded 0x00000000 1685796c8dcSSimon Schubert #define DEC_Subnormal 0x00000000 1695796c8dcSSimon Schubert #define DEC_Underflow 0x00000004 1705796c8dcSSimon Schubert #endif 1715796c8dcSSimon Schubert 1725796c8dcSSimon Schubert /* IEEE 754 groupings for the flags */ 1735796c8dcSSimon Schubert /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal */ 1745796c8dcSSimon Schubert /* are not in IEEE 754] */ 1755796c8dcSSimon Schubert #define DEC_IEEE_754_Division_by_zero (DEC_Division_by_zero) 1765796c8dcSSimon Schubert #if DECSUBSET 1775796c8dcSSimon Schubert #define DEC_IEEE_754_Inexact (DEC_Inexact | DEC_Lost_digits) 1785796c8dcSSimon Schubert #else 1795796c8dcSSimon Schubert #define DEC_IEEE_754_Inexact (DEC_Inexact) 1805796c8dcSSimon Schubert #endif 1815796c8dcSSimon Schubert #define DEC_IEEE_754_Invalid_operation (DEC_Conversion_syntax | \ 1825796c8dcSSimon Schubert DEC_Division_impossible | \ 1835796c8dcSSimon Schubert DEC_Division_undefined | \ 1845796c8dcSSimon Schubert DEC_Insufficient_storage | \ 1855796c8dcSSimon Schubert DEC_Invalid_context | \ 1865796c8dcSSimon Schubert DEC_Invalid_operation) 1875796c8dcSSimon Schubert #define DEC_IEEE_754_Overflow (DEC_Overflow) 1885796c8dcSSimon Schubert #define DEC_IEEE_754_Underflow (DEC_Underflow) 1895796c8dcSSimon Schubert 1905796c8dcSSimon Schubert /* flags which are normally errors (result is qNaN, infinite, or 0) */ 1915796c8dcSSimon Schubert #define DEC_Errors (DEC_IEEE_754_Division_by_zero | \ 1925796c8dcSSimon Schubert DEC_IEEE_754_Invalid_operation | \ 1935796c8dcSSimon Schubert DEC_IEEE_754_Overflow | DEC_IEEE_754_Underflow) 1945796c8dcSSimon Schubert /* flags which cause a result to become qNaN */ 1955796c8dcSSimon Schubert #define DEC_NaNs DEC_IEEE_754_Invalid_operation 1965796c8dcSSimon Schubert 1975796c8dcSSimon Schubert /* flags which are normally for information only (finite results) */ 1985796c8dcSSimon Schubert #if DECSUBSET 1995796c8dcSSimon Schubert #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact \ 2005796c8dcSSimon Schubert | DEC_Lost_digits) 2015796c8dcSSimon Schubert #else 2025796c8dcSSimon Schubert #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact) 2035796c8dcSSimon Schubert #endif 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert /* IEEE 854 names (for compatibility with older decNumber versions) */ 2065796c8dcSSimon Schubert #define DEC_IEEE_854_Division_by_zero DEC_IEEE_754_Division_by_zero 2075796c8dcSSimon Schubert #define DEC_IEEE_854_Inexact DEC_IEEE_754_Inexact 2085796c8dcSSimon Schubert #define DEC_IEEE_854_Invalid_operation DEC_IEEE_754_Invalid_operation 2095796c8dcSSimon Schubert #define DEC_IEEE_854_Overflow DEC_IEEE_754_Overflow 2105796c8dcSSimon Schubert #define DEC_IEEE_854_Underflow DEC_IEEE_754_Underflow 2115796c8dcSSimon Schubert 2125796c8dcSSimon Schubert /* Name strings for the exceptional conditions */ 2135796c8dcSSimon Schubert #define DEC_Condition_CS "Conversion syntax" 2145796c8dcSSimon Schubert #define DEC_Condition_DZ "Division by zero" 2155796c8dcSSimon Schubert #define DEC_Condition_DI "Division impossible" 2165796c8dcSSimon Schubert #define DEC_Condition_DU "Division undefined" 2175796c8dcSSimon Schubert #define DEC_Condition_IE "Inexact" 2185796c8dcSSimon Schubert #define DEC_Condition_IS "Insufficient storage" 2195796c8dcSSimon Schubert #define DEC_Condition_IC "Invalid context" 2205796c8dcSSimon Schubert #define DEC_Condition_IO "Invalid operation" 2215796c8dcSSimon Schubert #if DECSUBSET 2225796c8dcSSimon Schubert #define DEC_Condition_LD "Lost digits" 2235796c8dcSSimon Schubert #endif 2245796c8dcSSimon Schubert #define DEC_Condition_OV "Overflow" 2255796c8dcSSimon Schubert #define DEC_Condition_PA "Clamped" 2265796c8dcSSimon Schubert #define DEC_Condition_RO "Rounded" 2275796c8dcSSimon Schubert #define DEC_Condition_SU "Subnormal" 2285796c8dcSSimon Schubert #define DEC_Condition_UN "Underflow" 2295796c8dcSSimon Schubert #define DEC_Condition_ZE "No status" 2305796c8dcSSimon Schubert #define DEC_Condition_MU "Multiple status" 2315796c8dcSSimon Schubert #define DEC_Condition_Length 21 /* length of the longest string, */ 2325796c8dcSSimon Schubert /* including terminator */ 2335796c8dcSSimon Schubert 2345796c8dcSSimon Schubert /* Initialization descriptors, used by decContextDefault */ 2355796c8dcSSimon Schubert #define DEC_INIT_BASE 0 2365796c8dcSSimon Schubert #define DEC_INIT_DECIMAL32 32 2375796c8dcSSimon Schubert #define DEC_INIT_DECIMAL64 64 2385796c8dcSSimon Schubert #define DEC_INIT_DECIMAL128 128 2395796c8dcSSimon Schubert /* Synonyms */ 2405796c8dcSSimon Schubert #define DEC_INIT_DECSINGLE DEC_INIT_DECIMAL32 2415796c8dcSSimon Schubert #define DEC_INIT_DECDOUBLE DEC_INIT_DECIMAL64 2425796c8dcSSimon Schubert #define DEC_INIT_DECQUAD DEC_INIT_DECIMAL128 2435796c8dcSSimon Schubert 2445796c8dcSSimon Schubert /* decContext routines */ 2455796c8dcSSimon Schubert 2465796c8dcSSimon Schubert #include "decContextSymbols.h" 2475796c8dcSSimon Schubert 2485796c8dcSSimon Schubert #ifdef __cplusplus 2495796c8dcSSimon Schubert extern "C" { 2505796c8dcSSimon Schubert #endif 2515796c8dcSSimon Schubert 2525796c8dcSSimon Schubert extern decContext * decContextClearStatus(decContext *, uint32_t); 2535796c8dcSSimon Schubert extern decContext * decContextDefault(decContext *, int32_t); 2545796c8dcSSimon Schubert extern enum rounding decContextGetRounding(decContext *); 2555796c8dcSSimon Schubert extern uint32_t decContextGetStatus(decContext *); 2565796c8dcSSimon Schubert extern decContext * decContextRestoreStatus(decContext *, uint32_t, uint32_t); 2575796c8dcSSimon Schubert extern uint32_t decContextSaveStatus(decContext *, uint32_t); 2585796c8dcSSimon Schubert extern decContext * decContextSetRounding(decContext *, enum rounding); 2595796c8dcSSimon Schubert extern decContext * decContextSetStatus(decContext *, uint32_t); 2605796c8dcSSimon Schubert extern decContext * decContextSetStatusFromString(decContext *, const char *); 2615796c8dcSSimon Schubert extern decContext * decContextSetStatusFromStringQuiet(decContext *, const char *); 2625796c8dcSSimon Schubert extern decContext * decContextSetStatusQuiet(decContext *, uint32_t); 2635796c8dcSSimon Schubert extern const char * decContextStatusToString(const decContext *); 2645796c8dcSSimon Schubert extern int32_t decContextTestEndian(uint8_t); 2655796c8dcSSimon Schubert extern uint32_t decContextTestSavedStatus(uint32_t, uint32_t); 2665796c8dcSSimon Schubert extern uint32_t decContextTestStatus(decContext *, uint32_t); 2675796c8dcSSimon Schubert extern decContext * decContextZeroStatus(decContext *); 2685796c8dcSSimon Schubert 2695796c8dcSSimon Schubert #ifdef __cplusplus 2705796c8dcSSimon Schubert } 2715796c8dcSSimon Schubert #endif 2725796c8dcSSimon Schubert 2735796c8dcSSimon Schubert #endif 274