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