tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

decContext.h (13220B)


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