root/src/decNumber/decNumber.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


   1 // vim: filetype=c:tabstop=4:ai:expandtab
   2 // SPDX-License-Identifier: ICU
   3 // scspell-id: ca5518dc-f62c-11ec-9785-80ee73e9b8e7
   4 /* ------------------------------------------------------------------ */
   5 /* Decimal Number arithmetic module header                            */
   6 /* ------------------------------------------------------------------ */
   7 /* Copyright (c) IBM Corporation, 2000, 2010.  All rights reserved.   */
   8 /*                                                                    */
   9 /* This software is made available under the terms of the             */
  10 /* ICU License -- ICU 1.8.1 and later.                                */
  11 /*                                                                    */
  12 /* The description and User's Guide ("The decNumber C Library") for   */
  13 /* this software is called decNumber.pdf.  This document is           */
  14 /* available, together with arithmetic and format specifications,     */
  15 /* testcases, and Web links, on the General Decimal Arithmetic page.  */
  16 /*                                                                    */
  17 /* Please send comments, suggestions, and corrections to the author:  */
  18 /*   mfc@uk.ibm.com                                                   */
  19 /*   Mike Cowlishaw, IBM Fellow                                       */
  20 /*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
  21 /* ------------------------------------------------------------------ */
  22 
  23 #if !defined(DECNUMBER)
  24 # define DECNUMBER
  25 # define DECNAME     "decNumber"                        /* Short name */
  26 # define DECFULLNAME "Decimal Number Module"          /* Verbose name */
  27 # define DECAUTHOR   "Mike Cowlishaw"                 /* Who to blame */
  28 
  29 # if !defined(DECCONTEXT)
  30 #  include "decContext.h"
  31 # endif
  32 
  33   /* Bit settings for decNumber.bits                                  */
  34 # define DECNEG    0x80      /* Sign; 1=negative, 0=positive or zero  */
  35 # define DECINF    0x40      /* 1=Infinity                            */
  36 # define DECNAN    0x20      /* 1=NaN                                 */
  37 # define DECSNAN   0x10      /* 1=sNaN                                */
  38   /* The remaining bits are reserved; they must be 0                  */
  39 # define DECSPECIAL (DECINF|DECNAN|DECSNAN)  /* any special value     */
  40 
  41   /* Define the decNumber data structure.  The size and shape of the  */
  42   /* units array in the structure is determined by the following      */
  43   /* constant.  This must not be changed without recompiling the      */
  44   /* decNumber library modules. */
  45 
  46   //#define DECDPUN 3         /* DECimal Digits Per UNit [must be >0  */
  47 # define DECDPUN 8            /* DECimal Digits Per UNit [must be >0  */
  48                               /* and <10; 3 or powers of 2 are best]. */
  49 
  50   /* DECNUMDIGITS is the default number of digits that can be held in */
  51   /* the structure.  If undefined, 1 is assumed and it is assumed     */
  52   /* that the structure will be immediately followed by extra space,  */
  53   /* as required.  DECNUMDIGITS is always >0.                         */
  54 # if !defined(DECNUMDIGITS)
  55 #  define DECNUMDIGITS 1
  56 # endif
  57 
  58   /* The size (integer data type) of each unit is determined by the   */
  59   /* number of digits it will hold.                                   */
  60 # if DECDPUN<=2
  61 #  define decNumberUnit uint8_t
  62 # elif DECDPUN<=4
  63 #  define decNumberUnit uint16_t
  64 # else
  65 #  define decNumberUnit uint32_t
  66 # endif
  67   /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN)         */
  68 # define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN)
  69 
  70   /* The data structure... */
  71   typedef struct {
  72     int32_t digits;      /* Count of digits in the coefficient; >0    */
  73     int32_t exponent;    /* Unadjusted exponent, unbiased, in         */
  74                          /* range: -1999999997 through 999999999      */
  75     uint8_t bits;        /* Indicator bits (see above)                */
  76                          /* Coefficient, from least significant unit  */
  77     decNumberUnit lsu[DECNUMUNITS];
  78     } decNumber;
  79 
  80   /* Notes:                                                           */
  81   /* 1. If digits is > DECDPUN then there will one or more            */
  82   /*    decNumberUnits immediately following the first element of lsu.*/
  83   /*    These contain the remaining (more significant) digits of the  */
  84   /*    number, and may be in the lsu array, or may be guaranteed by  */
  85   /*    some other mechanism (such as being contained in another      */
  86   /*    structure, or being overlaid on dynamically allocated         */
  87   /*    storage).                                                     */
  88   /*                                                                  */
  89   /*    Each integer of the coefficient (except potentially the last) */
  90   /*    contains DECDPUN digits (e.g., a value in the range 0 through */
  91   /*    99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3).  */
  92   /*                                                                  */
  93   /* 2. A decNumber converted to a string may need up to digits+14    */
  94   /*    characters.  The worst cases (non-exponential and exponential */
  95   /*    formats) are -0.00000{9...}# and -9.{9...}E+999999999#        */
  96   /*    (where # is '\0')                                             */
  97 
  98   /* ---------------------------------------------------------------- */
  99   /* decNumber public functions and macros                            */
 100   /* ---------------------------------------------------------------- */
 101   /* Conversions                                                      */
 102   decNumber * decNumberFromInt32(decNumber *, int32_t);
 103   decNumber * decNumberFromUInt32(decNumber *, uint32_t);
 104   decNumber * decNumberFromString(decNumber *, const char *, decContext *);
 105   char      * decNumberToString(const decNumber *, char *);
 106   char      * decNumberToEngString(const decNumber *, char *);
 107   uint32_t    decNumberToUInt32(const decNumber *, decContext *);
 108   int32_t     decNumberToInt32(const decNumber *, decContext *);
 109   uint8_t   * decNumberGetBCD(const decNumber *, uint8_t *);
 110   decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
 111 
 112   /* Operators and elementary functions                               */
 113   decNumber * decNumberAbs(decNumber *, const decNumber *, decContext *);
 114   decNumber * decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *);
 115   decNumber * decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *);
 116   decNumber * decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *);
 117   decNumber * decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *);
 118   decNumber * decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *);
 119   decNumber * decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *);
 120   decNumber * decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *);
 121   decNumber * decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *);
 122   decNumber * decNumberExp(decNumber *, const decNumber *, decContext *);
 123   decNumber * decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *);
 124   decNumber * decNumberInvert(decNumber *, const decNumber *, decContext *);
 125   decNumber * decNumberLn(decNumber *, const decNumber *, decContext *);
 126   decNumber * decNumberLogB(decNumber *, const decNumber *, decContext *);
 127   decNumber * decNumberLog10(decNumber *, const decNumber *, decContext *);
 128   decNumber * decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *);
 129   decNumber * decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *);
 130   decNumber * decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *);
 131   decNumber * decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *);
 132   decNumber * decNumberMinus(decNumber *, const decNumber *, decContext *);
 133   decNumber * decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *);
 134   decNumber * decNumberNormalize(decNumber *, const decNumber *, decContext *);
 135   decNumber * decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *);
 136   decNumber * decNumberPlus(decNumber *, const decNumber *, decContext *);
 137   decNumber * decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *);
 138   decNumber * decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *);
 139   decNumber * decNumberReduce(decNumber *, const decNumber *, decContext *);
 140   decNumber * decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *);
 141   decNumber * decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *);
 142   decNumber * decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *);
 143   decNumber * decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *);
 144   decNumber * decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *);
 145   decNumber * decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *);
 146   decNumber * decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *);
 147   decNumber * decNumberSquareRoot(decNumber *, const decNumber *, decContext *);
 148   decNumber * decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *);
 149   decNumber * decNumberToIntegralExact(decNumber *, const decNumber *, decContext *);
 150   decNumber * decNumberToIntegralValue(decNumber *, const decNumber *, decContext *);
 151   decNumber * decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *);
 152 
 153   /* Utilities                                                        */
 154   enum decClass decNumberClass(const decNumber *, decContext *);
 155   const char * decNumberClassToString(enum decClass);
 156   decNumber  * decNumberCopy(decNumber *, const decNumber *);
 157   decNumber  * decNumberCopyAbs(decNumber *, const decNumber *);
 158   decNumber  * decNumberCopyNegate(decNumber *, const decNumber *);
 159   decNumber  * decNumberCopySign(decNumber *, const decNumber *, const decNumber *);
 160   decNumber  * decNumberNextMinus(decNumber *, const decNumber *, decContext *);
 161   decNumber  * decNumberNextPlus(decNumber *, const decNumber *, decContext *);
 162   decNumber  * decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *);
 163   decNumber  * decNumberTrim(decNumber *);
 164   const char * decNumberVersion(void);
 165   decNumber  * decNumberZero(decNumber *);
 166 
 167   /* Functions for testing decNumbers (normality depends on context)  */
 168   int32_t decNumberIsNormal(const decNumber *, decContext *);
 169   int32_t decNumberIsSubnormal(const decNumber *, decContext *);
 170 
 171   /* Macros for testing decNumber *dn                                 */
 172 # define decNumberIsCanonical(dn) (1)   /* All decNumbers are saintly */
 173 # define decNumberIsFinite(dn)    (((dn)->bits&DECSPECIAL)==0)
 174 # define decNumberIsInfinite(dn)  (((dn)->bits&DECINF)!=0)
 175 # define decNumberIsNaN(dn)       (((dn)->bits&(DECNAN|DECSNAN))!=0)
 176 # define decNumberIsNegative(dn)  (((dn)->bits&DECNEG)!=0)
 177 # define decNumberIsQNaN(dn)      (((dn)->bits&(DECNAN))!=0)
 178 # define decNumberIsSNaN(dn)      (((dn)->bits&(DECSNAN))!=0)
 179 # define decNumberIsSpecial(dn)   (((dn)->bits&DECSPECIAL)!=0)
 180 # define decNumberIsZero(dn)      (*(dn)->lsu==0 \
 181                                     && (dn)->digits==1 \
 182                                     && (((dn)->bits&DECSPECIAL)==0))
 183 # define decNumberRadix(dn)       (10)
 184 
 185 #endif

/* [previous][next][first][last][top][bottom][index][help] */