michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Decimal Number arithmetic module header */ michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Copyright (c) IBM Corporation, 2000-2010. All rights reserved. */ michael@0: /* */ michael@0: /* This software is made available under the terms of the */ michael@0: /* ICU License -- ICU 1.8.1 and later. */ michael@0: /* */ michael@0: /* The description and User's Guide ("The decNumber C Library") for */ michael@0: /* this software is called decNumber.pdf. This document is */ michael@0: /* available, together with arithmetic and format specifications, */ michael@0: /* testcases, and Web links, on the General Decimal Arithmetic page. */ michael@0: /* */ michael@0: /* Please send comments, suggestions, and corrections to the author: */ michael@0: /* mfc@uk.ibm.com */ michael@0: /* Mike Cowlishaw, IBM Fellow */ michael@0: /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */ michael@0: /* ------------------------------------------------------------------ */ michael@0: michael@0: /* Modified version, for use from within ICU. michael@0: * Renamed public functions, to avoid an unwanted export of the michael@0: * standard names from the ICU library. michael@0: * michael@0: * Use ICU's uprv_malloc() and uprv_free() michael@0: * michael@0: * Revert comment syntax to plain C michael@0: * michael@0: * Remove a few compiler warnings. michael@0: */ michael@0: michael@0: #if !defined(DECNUMBER) michael@0: #define DECNUMBER michael@0: #define DECNAME "decNumber" /* Short name */ michael@0: #define DECFULLNAME "Decimal Number Module" /* Verbose name */ michael@0: #define DECAUTHOR "Mike Cowlishaw" /* Who to blame */ michael@0: michael@0: #if !defined(DECCONTEXT) michael@0: #include "decContext.h" michael@0: #endif michael@0: michael@0: /* Bit settings for decNumber.bits */ michael@0: #define DECNEG 0x80 /* Sign; 1=negative, 0=positive or zero */ michael@0: #define DECINF 0x40 /* 1=Infinity */ michael@0: #define DECNAN 0x20 /* 1=NaN */ michael@0: #define DECSNAN 0x10 /* 1=sNaN */ michael@0: /* The remaining bits are reserved; they must be 0 */ michael@0: #define DECSPECIAL (DECINF|DECNAN|DECSNAN) /* any special value */ michael@0: michael@0: /* Define the decNumber data structure. The size and shape of the */ michael@0: /* units array in the structure is determined by the following */ michael@0: /* constant. This must not be changed without recompiling the */ michael@0: /* decNumber library modules. */ michael@0: michael@0: /* For ICU, use one digit per byte, to make it easier to emulate the michael@0: * old DigitList interface on top of a decNumber michael@0: */ michael@0: #define DECDPUN 1 /* DECimal Digits Per UNit [must be >0 */ michael@0: /* and <10; 3 or powers of 2 are best]. */ michael@0: michael@0: /* DECNUMDIGITS is the default number of digits that can be held in */ michael@0: /* the structure. If undefined, 1 is assumed and it is assumed */ michael@0: /* that the structure will be immediately followed by extra space, */ michael@0: /* as required. DECNUMDIGITS is always >0. */ michael@0: #if !defined(DECNUMDIGITS) michael@0: #define DECNUMDIGITS 1 michael@0: #endif michael@0: michael@0: /* The size (integer data type) of each unit is determined by the */ michael@0: /* number of digits it will hold. */ michael@0: #if DECDPUN<=2 michael@0: #define decNumberUnit uint8_t michael@0: #elif DECDPUN<=4 michael@0: #define decNumberUnit uint16_t michael@0: #else michael@0: #define decNumberUnit uint32_t michael@0: #endif michael@0: /* The number of units needed is ceil(DECNUMDIGITS/DECDPUN) */ michael@0: #define DECNUMUNITS ((DECNUMDIGITS+DECDPUN-1)/DECDPUN) michael@0: michael@0: /* The data structure... */ michael@0: typedef struct { michael@0: int32_t digits; /* Count of digits in the coefficient; >0 */ michael@0: int32_t exponent; /* Unadjusted exponent, unbiased, in */ michael@0: /* range: -1999999997 through 999999999 */ michael@0: uint8_t bits; /* Indicator bits (see above) */ michael@0: /* Coefficient, from least significant unit */ michael@0: decNumberUnit lsu[DECNUMUNITS]; michael@0: } decNumber; michael@0: michael@0: /* Notes: */ michael@0: /* 1. If digits is > DECDPUN then there will one or more */ michael@0: /* decNumberUnits immediately following the first element of lsu.*/ michael@0: /* These contain the remaining (more significant) digits of the */ michael@0: /* number, and may be in the lsu array, or may be guaranteed by */ michael@0: /* some other mechanism (such as being contained in another */ michael@0: /* structure, or being overlaid on dynamically allocated */ michael@0: /* storage). */ michael@0: /* */ michael@0: /* Each integer of the coefficient (except potentially the last) */ michael@0: /* contains DECDPUN digits (e.g., a value in the range 0 through */ michael@0: /* 99999999 if DECDPUN is 8, or 0 through 999 if DECDPUN is 3). */ michael@0: /* */ michael@0: /* 2. A decNumber converted to a string may need up to digits+14 */ michael@0: /* characters. The worst cases (non-exponential and exponential */ michael@0: /* formats) are -0.00000{9...}# and -9.{9...}E+999999999# */ michael@0: /* (where # is '\0') */ michael@0: michael@0: michael@0: /* ---------------------------------------------------------------- */ michael@0: /* decNumber public functions and macros */ michael@0: /* ---------------------------------------------------------------- */ michael@0: /* Conversions */ michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromInt32(decNumber *, int32_t); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromUInt32(decNumber *, uint32_t); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFromString(decNumber *, const char *, decContext *); michael@0: U_INTERNAL char * U_EXPORT2 uprv_decNumberToString(const decNumber *, char *); michael@0: U_INTERNAL char * U_EXPORT2 uprv_decNumberToEngString(const decNumber *, char *); michael@0: U_INTERNAL uint32_t U_EXPORT2 uprv_decNumberToUInt32(const decNumber *, decContext *); michael@0: U_INTERNAL int32_t U_EXPORT2 uprv_decNumberToInt32(const decNumber *, decContext *); michael@0: U_INTERNAL uint8_t * U_EXPORT2 uprv_decNumberGetBCD(const decNumber *, uint8_t *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSetBCD(decNumber *, const uint8_t *, uint32_t); michael@0: michael@0: /* Operators and elementary functions */ michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAbs(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAdd(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberAnd(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompare(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareSignal(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareTotal(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCompareTotalMag(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberDivide(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberDivideInteger(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberExp(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberFMA(decNumber *, const decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberInvert(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLn(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLogB(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberLog10(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMax(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMaxMag(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMin(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMinMag(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMinus(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberMultiply(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNormalize(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberOr(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberPlus(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberPower(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberQuantize(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberReduce(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRemainder(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRemainderNear(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRescale(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberRotate(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSameQuantum(decNumber *, const decNumber *, const decNumber *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberScaleB(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberShift(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSquareRoot(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberSubtract(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberToIntegralExact(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberToIntegralValue(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberXor(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: michael@0: /* Utilities */ michael@0: enum decClass uprv_decNumberClass(const decNumber *, decContext *); michael@0: U_INTERNAL const char * U_EXPORT2 uprv_decNumberClassToString(enum decClass); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopy(decNumber *, const decNumber *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopyAbs(decNumber *, const decNumber *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopyNegate(decNumber *, const decNumber *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberCopySign(decNumber *, const decNumber *, const decNumber *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNextMinus(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNextPlus(decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberNextToward(decNumber *, const decNumber *, const decNumber *, decContext *); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberTrim(decNumber *); michael@0: U_INTERNAL const char * U_EXPORT2 uprv_decNumberVersion(void); michael@0: U_INTERNAL decNumber * U_EXPORT2 uprv_decNumberZero(decNumber *); michael@0: michael@0: /* Functions for testing decNumbers (normality depends on context) */ michael@0: U_INTERNAL int32_t U_EXPORT2 uprv_decNumberIsNormal(const decNumber *, decContext *); michael@0: U_INTERNAL int32_t U_EXPORT2 uprv_decNumberIsSubnormal(const decNumber *, decContext *); michael@0: michael@0: /* Macros for testing decNumber *dn */ michael@0: #define decNumberIsCanonical(dn) (1) /* All decNumbers are saintly */ michael@0: #define decNumberIsFinite(dn) (((dn)->bits&DECSPECIAL)==0) michael@0: #define decNumberIsInfinite(dn) (((dn)->bits&DECINF)!=0) michael@0: #define decNumberIsNaN(dn) (((dn)->bits&(DECNAN|DECSNAN))!=0) michael@0: #define decNumberIsNegative(dn) (((dn)->bits&DECNEG)!=0) michael@0: #define decNumberIsQNaN(dn) (((dn)->bits&(DECNAN))!=0) michael@0: #define decNumberIsSNaN(dn) (((dn)->bits&(DECSNAN))!=0) michael@0: #define decNumberIsSpecial(dn) (((dn)->bits&DECSPECIAL)!=0) michael@0: #define decNumberIsZero(dn) (*(dn)->lsu==0 \ michael@0: && (dn)->digits==1 \ michael@0: && (((dn)->bits&DECSPECIAL)==0)) michael@0: #define decNumberRadix(dn) (10) michael@0: michael@0: #endif