Home | History | Annotate | Download | only in i18n
      1 /* ------------------------------------------------------------------ */
      2 /* Decimal Number arithmetic module header                            */
      3 /* ------------------------------------------------------------------ */
      4 /* Copyright (c) IBM Corporation, 2000-2010.   All rights reserved.   */
      5 /*                                                                    */
      6 /* This software is made available under the terms of the             */
      7 /* ICU License -- ICU 1.8.1 and later.                                */
      8 /*                                                                    */
      9 /* The description and User's Guide ("The decNumber C Library") for   */
     10 /* this software is called decNumber.pdf.  This document is           */
     11 /* available, together with arithmetic and format specifications,     */
     12 /* testcases, and Web links, on the General Decimal Arithmetic page.  */
     13 /*                                                                    */
     14 /* Please send comments, suggestions, and corrections to the author:  */
     15 /*   mfc (at) uk.ibm.com                                                   */
     16 /*   Mike Cowlishaw, IBM Fellow                                       */
     17 /*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
     18 /* ------------------------------------------------------------------ */
     19 
     20 /* Modified version, for use from within ICU.
     21  *    Renamed public functions, to avoid an unwanted export of the
     22  *    standard names from the ICU library.
     23  *
     24  *    Use ICU's uprv_malloc() and uprv_free()
     25  *
     26  *    Revert comment syntax to plain C
     27  *
     28  *    Remove a few compiler warnings.
     29  */
     30 
     31 #if !defined(DECNUMBER)
     32   #define DECNUMBER
     33   #define DECNAME     "decNumber"                       /* Short name */
     34   #define DECFULLNAME "Decimal Number Module"         /* Verbose name */
     35   #define DECAUTHOR   "Mike Cowlishaw"                /* Who to blame */
     36 
     37   #if !defined(DECCONTEXT)
     38     #include "decContext.h"
     39   #endif
     40 
     41   /* Bit settings for decNumber.bits                                  */
     42   #define DECNEG    0x80      /* Sign; 1=negative, 0=positive or zero */
     43   #define DECINF    0x40      /* 1=Infinity                           */
     44   #define DECNAN    0x20      /* 1=NaN                                */
     45   #define DECSNAN   0x10      /* 1=sNaN                               */
     46   /* The remaining bits are reserved; they must be 0                  */
     47   #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value     */
     48 
     49   /* Define the decNumber data structure.  The size and shape of the  */
     50   /* units array in the structure is determined by the following      */
     51   /* constant.  This must not be changed without recompiling the      */
     52   /* decNumber library modules. */
     53 
     54   /* For ICU, use one digit per byte, to make it easier to emulate the
     55    * old DigitList interface on top of a decNumber
     56    */
     57   #define DECDPUN 1           /* DECimal Digits Per UNit [must be >0  */
     58                               /* and <10; 3 or powers of 2 are best]. */
     59 
     60   /* DECNUMDIGITS is the default number of digits that can be held in */
     61   /* the structure.  If undefined, 1 is assumed and it is assumed     */
     62   /* that the structure will be immediately followed by extra space,  */
     63   /* as required.  DECNUMDIGITS is always >0.                         */
     64   #if !defined(DECNUMDIGITS)
     65     #define DECNUMDIGITS 1
     66   #endif
     67 
     68   /* The size (integer data type) of each unit is determined by the   */
     69   /* number of digits it will hold.                                   */
     70   #if   DECDPUN<=2
     71     #define decNumberUnit uint8_t
     72   #elif DECDPUN<=4
     73     #define decNumberUnit uint16_t
     74   #else
     75     #define decNumberUnit uint32_t
     76   #endif
     77   /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN)         */
     78   #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)
     79 
     80   /* The data structure... */
     81   typedef struct {
     82     int32_t digits;      /* Count of digits in the coefficient; >0    */
     83     int32_t exponent;    /* Unadjusted exponent, unbiased, in         */
     84                          /* range: -1999999997 through 999999999      */
     85     uint8_t bits;        /* Indicator bits (see above)                */
     86                          /* Coefficient, from least significant unit  */
     87     decNumberUnit lsu[DECNUMUNITS];
     88     } decNumber;
     89 
     90   /* Notes:                                                           */
     91   /* 1. If digits is > DECDPUN then there will one or more            */
     92   /*    decNumberUnits immediately following the first element of lsu.*/
     93   /*    These contain the remaining (more significant) digits of the  */
     94   /*    number, and may be in the lsu array, or may be guaranteed by  */
     95   /*    some other mechanism (such as being contained in another      */
     96   /*    structure, or being overlaid on dynamically allocated         */
     97   /*    storage).                                                     */
     98   /*                                                                  */
     99   /*    Each integer of the coefficient (except potentially the last) */
    100   /*    contains DECDPUN digits (e.g., a value in the range 0 through */
    101   /*    99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3).  */
    102   /*                                                                  */
    103   /* 2. A decNumber converted to a string may need up to digits+14    */
    104   /*    characters.  The worst cases (non-exponential and exponential */
    105   /*    formats) are -0.00000{9...}# and -9.{9...}E+999999999#        */
    106   /*    (where # is '\0')                                             */
    107 
    108 
    109   /* ---------------------------------------------------------------- */
    110   /* decNumber public functions and macros                            */
    111   /* ---------------------------------------------------------------- */
    112   /* Conversions                                                      */
    113   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromInt32(decNumber *, int32_t);
    114   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromUInt32(decNumber *, uint32_t);
    115   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromString(decNumber *, const char *, decContext *);
    116   U_INTERNAL char      * U_EXPORT2 uprv_decNumberToString(const decNumber *, char *);
    117   U_INTERNAL char      * U_EXPORT2 uprv_decNumberToEngString(const decNumber *, char *);
    118   U_INTERNAL uint32_t    U_EXPORT2 uprv_decNumberToUInt32(const decNumber *, decContext *);
    119   U_INTERNAL int32_t     U_EXPORT2 uprv_decNumberToInt32(const decNumber *, decContext *);
    120   U_INTERNAL uint8_t   * U_EXPORT2 uprv_decNumberGetBCD(const decNumber *, uint8_t *);
    121   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
    122 
    123   /* Operators and elementary functions                               */
    124   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAbs(decNumber *, const decNumber *, decContext *);
    125   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
    126   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
    127   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
    128   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
    129   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
    130   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
    131   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
    132   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
    133   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberExp(decNumber *, const decNumber *, decContext *);
    134   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
    135   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberInvert(decNumber *, const decNumber *, decContext *);
    136   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLn(decNumber *, const decNumber *, decContext *);
    137   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLogB(decNumber *, const decNumber *, decContext *);
    138   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLog10(decNumber *, const decNumber *, decContext *);
    139   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
    140   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
    141   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
    142   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
    143   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMinus(decNumber *, const decNumber *, decContext *);
    144   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
    145   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNormalize(decNumber *, const decNumber *, decContext *);
    146   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
    147   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberPlus(decNumber *, const decNumber *, decContext *);
    148   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
    149   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
    150   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberReduce(decNumber *, const decNumber *, decContext *);
    151   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
    152   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
    153   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
    154   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
    155   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
    156   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
    157   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
    158   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
    159   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
    160   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
    161   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
    162   U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);
    163 
    164   /* Utilities                                                        */
    165   enum decClass uprv_decNumberClass(const decNumber *, decContext *);
    166   U_INTERNAL const char * U_EXPORT2 uprv_decNumberClassToString(enum decClass);
    167   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberCopy(decNumber *, const decNumber *);
    168   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberCopyAbs(decNumber *, const decNumber *);
    169   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberCopyNegate(decNumber *, const decNumber *);
    170   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
    171   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberNextMinus(decNumber *, const decNumber *, decContext *);
    172   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberNextPlus(decNumber *, const decNumber *, decContext *);
    173   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
    174   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberTrim(decNumber *);
    175   U_INTERNAL const char * U_EXPORT2 uprv_decNumberVersion(void);
    176   U_INTERNAL decNumber  * U_EXPORT2 uprv_decNumberZero(decNumber *);
    177 
    178   /* Functions for testing decNumbers (normality depends on context)  */
    179   U_INTERNAL int32_t U_EXPORT2 uprv_decNumberIsNormal(const decNumber *, decContext *);
    180   U_INTERNAL int32_t U_EXPORT2 uprv_decNumberIsSubnormal(const decNumber *, decContext *);
    181 
    182   /* Macros for testing decNumber *dn                                 */
    183   #define decNumberIsCanonical(dn) (1)  /* All decNumbers are saintly */
    184   #define decNumberIsFinite(dn)    (((dn)->bits&DECSPECIAL)==0)
    185   #define decNumberIsInfinite(dn)  (((dn)->bits&DECINF)!=0)
    186   #define decNumberIsNaN(dn)       (((dn)->bits&(DECNAN|DECSNAN))!=0)
    187   #define decNumberIsNegative(dn)  (((dn)->bits&DECNEG)!=0)
    188   #define decNumberIsQNaN(dn)      (((dn)->bits&(DECNAN))!=0)
    189   #define decNumberIsSNaN(dn)      (((dn)->bits&(DECSNAN))!=0)
    190   #define decNumberIsSpecial(dn)   (((dn)->bits&DECSPECIAL)!=0)
    191   #define decNumberIsZero(dn)      (*(dn)->lsu==0 \
    192                                     && (dn)->digits==1 \
    193                                     && (((dn)->bits&DECSPECIAL)==0))
    194   #define decNumberRadix(dn)       (10)
    195 
    196 #endif
    197