HPGCC3 Documentation 3.0 R003

decContext.h

Go to the documentation of this file.
00001 /* ------------------------------------------------------------------ */
00002 /* Decimal Context module header                                      */
00003 /* ------------------------------------------------------------------ */
00004 /* Copyright (c) IBM Corporation, 2000, 2005.  All rights reserved.   */
00005 /*                                                                    */
00006 /* This software is made available under the terms of the             */
00007 /* ICU License -- ICU 1.8.1 and later.                                */
00008 /*                                                                    */
00009 /* The description and User's Guide ("The decNumber C Library") for   */
00010 /* this software is called decNumber.pdf.  This document is           */
00011 /* available, together with arithmetic and format specifications,     */
00012 /* testcases, and Web links, at: http://www2.hursley.ibm.com/decimal  */
00013 /*                                                                    */
00014 /* Please send comments, suggestions, and corrections to the author:  */
00015 /*   mfc@uk.ibm.com                                                   */
00016 /*   Mike Cowlishaw, IBM Fellow                                       */
00017 /*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
00018 /* ------------------------------------------------------------------ */
00019 /*                                                                    */
00020 /* Context variables must always have valid values:                   */
00021 /*                                                                    */
00022 /*  status   -- [any bits may be cleared, but not set, by user]       */
00023 /*  round    -- must be one of the enumerated rounding modes          */
00024 /*                                                                    */
00025 /* The following variables are implied for fixed size formats (i.e.,  */
00026 /* they are ignored) but should still be set correctly in case used   */
00027 /* with decNumber functions:                                          */
00028 /*                                                                    */
00029 /*  clamp    -- must be either 0 or 1                                 */
00030 /*  digits   -- must be in the range 1 through 999999999              */
00031 /*  emax     -- must be in the range 0 through 999999999              */
00032 /*  emin     -- must be in the range 0 through -999999999             */
00033 /*  extended -- must be either 0 or 1 [present only if DECSUBSET]     */
00034 /*  traps    -- only defined bits may be set                          */
00035 /*                                                                    */
00036 /* ------------------------------------------------------------------ */
00037 
00038 #if !defined(DECCONTEXT)
00039   #define DECCONTEXT
00040   #define DECCNAME     "decContext"                     /* Short name */
00041   #define DECCFULLNAME "Decimal Context Descriptor"   /* Verbose name */
00042   #define DECCAUTHOR   "Mike Cowlishaw"               /* Who to blame */
00043 
00044   #if !defined(int32_t)
00045     #include <stdint.h>            /* C99 standard integers           */
00046   #endif
00047 
00048   #include <stdio.h>
00049 //  #include <stdio.h>               /* for printf, etc.                */
00050   #include <signal.h>              /* for traps                       */
00051 
00052   /* Extended flags setting -- set this to 0 to use only IEEE flags   */
00053   #define DECEXTFLAG 1             /* 1=enable extended flags         */
00054 
00055   /* Conditional code flag -- set this to 0 for best performance      */
00056   #define DECSUBSET  0             /* 1=enable subset arithmetic      */
00057 
00058   /* Context for operations, with associated constants                */
00059   enum rounding {
00060     DEC_ROUND_CEILING,             /* round towards +infinity         */
00061     DEC_ROUND_UP,                  /* round away from 0               */
00062     DEC_ROUND_HALF_UP,             /* 0.5 rounds up                   */
00063     DEC_ROUND_HALF_EVEN,           /* 0.5 rounds to nearest even      */
00064     DEC_ROUND_HALF_DOWN,           /* 0.5 rounds down                 */
00065     DEC_ROUND_DOWN,                /* round towards 0 (truncate)      */
00066     DEC_ROUND_FLOOR,               /* round towards -infinity         */
00067     DEC_ROUND_05UP,                /* round for reround               */
00068     DEC_ROUND_MAX                  /* enum must be less than this     */
00069     };
00070   #define DEC_ROUND_DEFAULT DEC_ROUND_HALF_EVEN;
00071 
00072   typedef struct {
00073     int32_t  digits;               /* working precision               */
00074     int32_t  emax;                 /* maximum positive exponent       */
00075     int32_t  emin;                 /* minimum negative exponent       */
00076     enum     rounding round;       /* rounding mode                   */
00077     uint32_t traps;                /* trap-enabler flags              */
00078     uint32_t status;               /* status flags                    */
00079     uint8_t  clamp;                /* flag: apply IEEE exponent clamp */
00080     #if DECSUBSET
00081     uint8_t  extended;             /* flag: special-values allowed    */
00082     #endif
00083     } decContext;
00084 
00085   /* Maxima and Minima for context settings                           */
00086   #define DEC_MAX_DIGITS 999999999
00087   #define DEC_MIN_DIGITS         1
00088   #define DEC_MAX_EMAX   999999999
00089   #define DEC_MIN_EMAX           0
00090   #define DEC_MAX_EMIN           0
00091   #define DEC_MIN_EMIN  -999999999
00092   #define DEC_MAX_MATH      999999 /* max emax, etc., for math funcs. */
00093 
00094   /* Classifications for decimal numbers, aligned with 754r (note     */
00095   /* that 'normal' and 'subnormal' are meaningful only with a         */
00096   /* decContext or a fixed size format).                              */
00097   enum decClass {
00098     DEC_CLASS_SNAN,
00099     DEC_CLASS_QNAN,
00100     DEC_CLASS_NEG_INF,
00101     DEC_CLASS_NEG_NORMAL,
00102     DEC_CLASS_NEG_SUBNORMAL,
00103     DEC_CLASS_NEG_ZERO,
00104     DEC_CLASS_POS_ZERO,
00105     DEC_CLASS_POS_SUBNORMAL,
00106     DEC_CLASS_POS_NORMAL,
00107     DEC_CLASS_POS_INF,
00108     };
00109   /* Strings for the decClasses */
00110   #define DEC_ClassString_SN  "sNaN"
00111   #define DEC_ClassString_QN  "NaN"
00112   #define DEC_ClassString_NI  "-Infinity"
00113   #define DEC_ClassString_NN  "-Normal"
00114   #define DEC_ClassString_NS  "-Subnormal"
00115   #define DEC_ClassString_NZ  "-Zero"
00116   #define DEC_ClassString_PZ  "+Zero"
00117   #define DEC_ClassString_PS  "+Subnormal"
00118   #define DEC_ClassString_PN  "+Normal"
00119   #define DEC_ClassString_PI  "+Infinity"
00120   #define DEC_ClassString_UN  "Invalid"
00121 
00122   /* Trap-enabler and Status flags (exceptional conditions), and      */
00123   /* their names.  The top byte is reserved for internal use          */
00124   #if DECEXTFLAG
00125     /* Extended flags */
00126     #define DEC_Conversion_syntax    0x00000001
00127     #define DEC_Division_by_zero     0x00000002
00128     #define DEC_Division_impossible  0x00000004
00129     #define DEC_Division_undefined   0x00000008
00130     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]  */
00131     #define DEC_Inexact              0x00000020
00132     #define DEC_Invalid_context      0x00000040
00133     #define DEC_Invalid_operation    0x00000080
00134     #if DECSUBSET
00135     #define DEC_Lost_digits          0x00000100
00136     #endif
00137     #define DEC_Overflow             0x00000200
00138     #define DEC_Clamped              0x00000400
00139     #define DEC_Rounded              0x00000800
00140     #define DEC_Subnormal            0x00001000
00141     #define DEC_Underflow            0x00002000
00142   #else
00143     /* IEEE flags only */
00144     #define DEC_Conversion_syntax    0x00000010
00145     #define DEC_Division_by_zero     0x00000002
00146     #define DEC_Division_impossible  0x00000010
00147     #define DEC_Division_undefined   0x00000010
00148     #define DEC_Insufficient_storage 0x00000010 /* [when malloc fails]  */
00149     #define DEC_Inexact              0x00000001
00150     #define DEC_Invalid_context      0x00000010
00151     #define DEC_Invalid_operation    0x00000010
00152     #if DECSUBSET
00153     #define DEC_Lost_digits          0x00000000
00154     #endif
00155     #define DEC_Overflow             0x00000008
00156     #define DEC_Clamped              0x00000000
00157     #define DEC_Rounded              0x00000000
00158     #define DEC_Subnormal            0x00000000
00159     #define DEC_Underflow            0x00000004
00160   #endif
00161 
00162   /* IEEE 854 groupings for the flags                                 */
00163   /* [DEC_Clamped, DEC_Lost_digits, DEC_Rounded, and DEC_Subnormal    */
00164   /* are not in IEEE 854]                                             */
00165   #define DEC_IEEE_854_Division_by_zero  (DEC_Division_by_zero)
00166   #if DECSUBSET
00167   #define DEC_IEEE_854_Inexact           (DEC_Inexact | DEC_Lost_digits)
00168   #else
00169   #define DEC_IEEE_854_Inexact           (DEC_Inexact)
00170   #endif
00171   #define DEC_IEEE_854_Invalid_operation (DEC_Conversion_syntax |     \
00172                                           DEC_Division_impossible |   \
00173                                           DEC_Division_undefined |    \
00174                                           DEC_Insufficient_storage |  \
00175                                           DEC_Invalid_context |       \
00176                                           DEC_Invalid_operation)
00177   #define DEC_IEEE_854_Overflow          (DEC_Overflow)
00178   #define DEC_IEEE_854_Underflow         (DEC_Underflow)
00179 
00180   /* flags which are normally errors (result is qNaN, infinite, or 0) */
00181   #define DEC_Errors (DEC_IEEE_854_Division_by_zero |                 \
00182                       DEC_IEEE_854_Invalid_operation |                \
00183                       DEC_IEEE_854_Overflow | DEC_IEEE_854_Underflow)
00184   /* flags which cause a result to become qNaN                        */
00185   #define DEC_NaNs    DEC_IEEE_854_Invalid_operation
00186 
00187   /* flags which are normally for information only (finite results)   */
00188   #if DECSUBSET
00189   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact    \
00190                           | DEC_Lost_digits)
00191   #else
00192   #define DEC_Information (DEC_Clamped | DEC_Rounded | DEC_Inexact)
00193   #endif
00194 
00195   /* Name strings for the exceptional conditions                      */
00196   #define DEC_Condition_CS "Conversion syntax"
00197   #define DEC_Condition_DZ "Division by zero"
00198   #define DEC_Condition_DI "Division impossible"
00199   #define DEC_Condition_DU "Division undefined"
00200   #define DEC_Condition_IE "Inexact"
00201   #define DEC_Condition_IS "Insufficient storage"
00202   #define DEC_Condition_IC "Invalid context"
00203   #define DEC_Condition_IO "Invalid operation"
00204   #if DECSUBSET
00205   #define DEC_Condition_LD "Lost digits"
00206   #endif
00207   #define DEC_Condition_OV "Overflow"
00208   #define DEC_Condition_PA "Clamped"
00209   #define DEC_Condition_RO "Rounded"
00210   #define DEC_Condition_SU "Subnormal"
00211   #define DEC_Condition_UN "Underflow"
00212   #define DEC_Condition_ZE "No status"
00213   #define DEC_Condition_MU "Multiple status"
00214   #define DEC_Condition_Length 21  /* length of the longest string,   */
00215                                    /* including terminator            */
00216 
00217   /* Initialization descriptors, used by decContextDefault            */
00218   #define DEC_INIT_BASE         0
00219   #define DEC_INIT_DECIMAL32   32
00220   #define DEC_INIT_DECIMAL64   64
00221   #define DEC_INIT_DECIMAL128 128
00222   /* Synonyms */
00223   #define DEC_INIT_DECSINGLE  DEC_INIT_DECIMAL32
00224   #define DEC_INIT_DECDOUBLE  DEC_INIT_DECIMAL64
00225   #define DEC_INIT_DECQUAD    DEC_INIT_DECIMAL128
00226 
00227   /* decContext routines                                              */
00228   extern decContext  * decContextClearStatus(decContext *, uint32_t);
00229   extern decContext  * decContextDefault(decContext *, int32_t);
00230   extern enum rounding decContextGetRounding(decContext *);
00231   extern uint32_t      decContextGetStatus(decContext *);
00232   extern decContext  * decContextRestoreStatus(decContext *, uint32_t, uint32_t);
00233   extern uint32_t      decContextSaveStatus(decContext *, uint32_t);
00234   extern decContext  * decContextSetRounding(decContext *, enum rounding);
00235   extern decContext  * decContextSetStatus(decContext *, uint32_t);
00236   extern decContext  * decContextSetStatusFromString(decContext *, const char *);
00237   extern decContext  * decContextSetStatusFromStringQuiet(decContext *, const char *);
00238   extern decContext  * decContextSetStatusQuiet(decContext *, uint32_t);
00239   extern const char  * decContextStatusToString(const decContext *);
00240   extern uint32_t      decContextTestSavedStatus(uint32_t, uint32_t);
00241   extern uint32_t      decContextTestStatus(decContext *, uint32_t);
00242   extern decContext  * decContextZeroStatus(decContext *);
00243 
00244 #endif