HPGCC3 Documentation 3.0 R003

decNumber.h

Go to the documentation of this file.
00001 /* ------------------------------------------------------------------ */
00002 /* Decimal Number arithmetic module header                            */
00003 /* ------------------------------------------------------------------ */
00004 /* Copyright (c) IBM Corporation, 2000, 2007.  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 #if !defined(DECNUMBER)
00021   #define DECNUMBER
00022   #define DECNAME     "decNumber"                       /* Short name */
00023   #define DECFULLNAME "Decimal Number Module"         /* Verbose name */
00024   #define DECAUTHOR   "Mike Cowlishaw"                /* Who to blame */
00025 
00026   #if !defined(DECCONTEXT)
00027     #include "decContext.h"
00028   #endif
00029 
00030   /* Bit settings for decNumber.bits                                  */
00031   #define DECNEG    0x80      /* Sign; 1=negative, 0=positive or zero */
00032   #define DECINF    0x40      /* 1=Infinity                           */
00033   #define DECNAN    0x20      /* 1=NaN                                */
00034   #define DECSNAN   0x10      /* 1=sNaN                               */
00035   /* The remaining bits are reserved; they must be 0                  */
00036   #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value     */
00037 
00038   /* Define the decNumber data structure.  The size and shape of the  */
00039   /* units array in the structure is determined by the following      */
00040   /* constant.  This must not be changed without recompiling the      */
00041   /* decNumber library modules. */
00042 
00043   #define DECDPUN 4          /* DECimal Digits Per UNit [must be >0  */
00044                               /* and <10; 3 or powers of 2 are best]. */
00045 
00046   /* DECNUMDIGITS is the default number of digits that can be held in */
00047   /* the structure.  If undefined, 1 is assumed and it is assumed     */
00048   /* that the structure will be immediately followed by extra space,  */
00049   /* as required.  DECNUMDIGITS is always >0.                         */
00050   #if !defined(DECNUMDIGITS)
00051     #define DECNUMDIGITS 32
00052   #endif
00053 
00054   /* The size (integer data type) of each unit is determined by the   */
00055   /* number of digits it will hold.                                   */
00056   #if   DECDPUN<=2
00057     #define decNumberUnit uint8_t
00058   #elif DECDPUN<=4
00059     #define decNumberUnit uint16_t
00060   #else
00061     #define decNumberUnit uint32_t
00062   #endif
00063   /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN)         */
00064   #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)
00065 
00066   /* The data structure... */
00067   typedef struct {
00068     int32_t digits;      /* Count of digits in the coefficient; >0    */
00069     int32_t exponent;    /* Unadjusted exponent, unbiased, in         */
00070                          /* range: -1999999997 through 999999999      */
00071     uint8_t bits;        /* Indicator bits (see above)                */
00072                          /* Coefficient, from least significant unit  */
00073     decNumberUnit lsu[DECNUMUNITS];
00074     } decNumber;
00075 
00076   /* Notes:                                                           */
00077   /* 1. If digits is > DECDPUN then there will one or more            */
00078   /*    decNumberUnits immediately following the first element of lsu.*/
00079   /*    These contain the remaining (more significant) digits of the  */
00080   /*    number, and may be in the lsu array, or may be guaranteed by  */
00081   /*    some other mechanism (such as being contained in another      */
00082   /*    structure, or being overlaid on dynamically allocated         */
00083   /*    storage).                                                     */
00084   /*                                                                  */
00085   /*    Each integer of the coefficient (except potentially the last) */
00086   /*    contains DECDPUN digits (e.g., a value in the range 0 through */
00087   /*    99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3).  */
00088   /*                                                                  */
00089   /* 2. A decNumber converted to a string may need up to digits+14    */
00090   /*    characters.  The worst cases (non-exponential and exponential */
00091   /*    formats) are -0.00000{9...}# and -9.{9...}E+999999999#        */
00092   /*    (where # is '\0')                                             */
00093 
00094 
00095   /* ---------------------------------------------------------------- */
00096   /* decNumber public functions and macros                            */
00097   /* ---------------------------------------------------------------- */
00098   /* Conversions                                                      */
00099   decNumber * decNumberFromInt32(decNumber *, int32_t);
00100   decNumber * decNumberFromUInt32(decNumber *, uint32_t);
00101   decNumber * decNumberFromString(decNumber *, const char *, decContext *);
00102   char      * decNumberToString(const decNumber *, char *);
00103   char      * decNumberToEngString(const decNumber *, char *);
00104   uint32_t    decNumberToUInt32(const decNumber *, decContext *);
00105   int32_t     decNumberToInt32(const decNumber *, decContext *);
00106   uint8_t   * decNumberGetBCD(const decNumber *, uint8_t *);
00107   decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
00108 
00109   /* Operators and elementary functions                               */
00110   decNumber * decNumberAbs(decNumber *, const decNumber *, decContext *);
00111   decNumber * decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
00112   decNumber * decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
00113   decNumber * decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
00114   decNumber * decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
00115   decNumber * decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
00116   decNumber * decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
00117   decNumber * decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
00118   decNumber * decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
00119   decNumber * decNumberExp(decNumber *, const decNumber *, decContext *);
00120   decNumber * decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
00121   decNumber * decNumberInvert(decNumber *, const decNumber *, decContext *);
00122   decNumber * decNumberLn(decNumber *, const decNumber *, decContext *);
00123   decNumber * decNumberLogB(decNumber *, const decNumber *, decContext *);
00124   decNumber * decNumberLog10(decNumber *, const decNumber *, decContext *);
00125   decNumber * decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
00126   decNumber * decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
00127   decNumber * decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
00128   decNumber * decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
00129   decNumber * decNumberMinus(decNumber *, const decNumber *, decContext *);
00130   decNumber * decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
00131   decNumber * decNumberNormalize(decNumber *, const decNumber *, decContext *);
00132   decNumber * decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
00133   decNumber * decNumberPlus(decNumber *, const decNumber *, decContext *);
00134   decNumber * decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
00135   decNumber * decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
00136   decNumber * decNumberReduce(decNumber *, const decNumber *, decContext *);
00137   decNumber * decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
00138   decNumber * decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
00139   decNumber * decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
00140   decNumber * decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
00141   decNumber * decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
00142   decNumber * decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
00143   decNumber * decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
00144   decNumber * decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
00145   decNumber * decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
00146   decNumber * decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
00147   decNumber * decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
00148   decNumber * decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);
00149 
00150   /* Utilities                                                        */
00151   enum decClass decNumberClass(const decNumber *, decContext *);
00152   const char * decNumberClassToString(enum decClass);
00153   decNumber  * decNumberCopy(decNumber *, const decNumber *);
00154   decNumber  * decNumberCopyAbs(decNumber *, const decNumber *);
00155   decNumber  * decNumberCopyNegate(decNumber *, const decNumber *);
00156   decNumber  * decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
00157   decNumber  * decNumberNextMinus(decNumber *, const decNumber *, decContext *);
00158   decNumber  * decNumberNextPlus(decNumber *, const decNumber *, decContext *);
00159   decNumber  * decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
00160   decNumber  * decNumberTrim(decNumber *);
00161   const char * decNumberVersion(void);
00162   decNumber  * decNumberZero(decNumber *);
00163 
00164   /* Functions for testing decNumbers (normality depends on context)  */
00165   int32_t decNumberIsNormal(const decNumber *, decContext *);
00166   int32_t decNumberIsSubnormal(const decNumber *, decContext *);
00167 
00168   /* Macros for testing decNumber *dn                                 */
00169   #define decNumberIsCanonical(dn) (1)  /* All decNumbers are saintly */
00170   #define decNumberIsFinite(dn)    (((dn)->bits&DECSPECIAL)==0)
00171   #define decNumberIsInfinite(dn)  (((dn)->bits&DECINF)!=0)
00172   #define decNumberIsNaN(dn)       (((dn)->bits&(DECNAN|DECSNAN))!=0)
00173   #define decNumberIsNegative(dn)  (((dn)->bits&DECNEG)!=0)
00174   #define decNumberIsQNaN(dn)      (((dn)->bits&(DECNAN))!=0)
00175   #define decNumberIsSNaN(dn)      (((dn)->bits&(DECSNAN))!=0)
00176   #define decNumberIsSpecial(dn)   (((dn)->bits&DECSPECIAL)!=0)
00177   #define decNumberIsZero(dn)      (*(dn)->lsu==0 \
00178                                     && (dn)->digits==1 \
00179                                     && (((dn)->bits&DECSPECIAL)==0))
00180   #define decNumberRadix(dn)       (10)
00181 
00182 #endif