michael@0: /* ------------------------------------------------------------------ */ michael@0: /* decNumber package local type, tuning, and macro definitions */ michael@0: /* ------------------------------------------------------------------ */ michael@0: /* Copyright (c) IBM Corporation, 2000-2012. 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: /* This header file is included by all modules in the decNumber */ michael@0: /* library, and contains local type definitions, tuning parameters, */ michael@0: /* etc. It should not need to be used by application programs. */ michael@0: /* decNumber.h or one of decDouble (etc.) must be included first. */ michael@0: /* ------------------------------------------------------------------ */ michael@0: michael@0: #if !defined(DECNUMBERLOC) michael@0: #define DECNUMBERLOC michael@0: #define DECVERSION "decNumber 3.61" /* Package Version [16 max.] */ michael@0: #define DECNLAUTHOR "Mike Cowlishaw" /* Who to blame */ michael@0: michael@0: #include /* for abs */ michael@0: #include /* for memset, strcpy */ michael@0: michael@0: /* Conditional code flag -- set this to match hardware platform */ michael@0: #if !defined(DECLITEND) michael@0: #define DECLITEND 1 /* 1=little-endian, 0=big-endian */ michael@0: #endif michael@0: michael@0: /* Conditional code flag -- set this to 1 for best performance */ michael@0: #if !defined(DECUSE64) michael@0: #define DECUSE64 1 /* 1=use int64s, 0=int32 & smaller only */ michael@0: #endif michael@0: michael@0: /* Conditional check flags -- set these to 0 for best performance */ michael@0: #if !defined(DECCHECK) michael@0: #define DECCHECK 0 /* 1 to enable robust checking */ michael@0: #endif michael@0: #if !defined(DECALLOC) michael@0: #define DECALLOC 0 /* 1 to enable memory accounting */ michael@0: #endif michael@0: #if !defined(DECTRACE) michael@0: #define DECTRACE 0 /* 1 to trace certain internals, etc. */ michael@0: #endif michael@0: michael@0: /* Tuning parameter for decNumber (arbitrary precision) module */ michael@0: #if !defined(DECBUFFER) michael@0: #define DECBUFFER 36 /* Size basis for local buffers. This */ michael@0: /* should be a common maximum precision */ michael@0: /* rounded up to a multiple of 4; must */ michael@0: /* be zero or positive. */ michael@0: #endif michael@0: michael@0: /* ---------------------------------------------------------------- */ michael@0: /* Definitions for all modules (general-purpose) */ michael@0: /* ---------------------------------------------------------------- */ michael@0: michael@0: /* Local names for common types -- for safety, decNumber modules do */ michael@0: /* not use int or long directly. */ michael@0: #define Flag uint8_t michael@0: #define Byte int8_t michael@0: #define uByte uint8_t michael@0: #define Short int16_t michael@0: #define uShort uint16_t michael@0: #define Int int32_t michael@0: #define uInt uint32_t michael@0: #define Unit decNumberUnit michael@0: #if DECUSE64 michael@0: #define Long int64_t michael@0: #define uLong uint64_t michael@0: #endif michael@0: michael@0: /* Development-use definitions */ michael@0: typedef long int LI; /* for printf arguments only */ michael@0: #define DECNOINT 0 /* 1 to check no internal use of 'int' */ michael@0: /* or stdint types */ michael@0: #if DECNOINT michael@0: /* if these interfere with your C includes, do not set DECNOINT */ michael@0: #define int ? /* enable to ensure that plain C 'int' */ michael@0: #define long ?? /* .. or 'long' types are not used */ michael@0: #endif michael@0: michael@0: /* LONGMUL32HI -- set w=(u*v)>>32, where w, u, and v are uInts */ michael@0: /* (that is, sets w to be the high-order word of the 64-bit result; */ michael@0: /* the low-order word is simply u*v.) */ michael@0: /* This version is derived from Knuth via Hacker's Delight; */ michael@0: /* it seems to optimize better than some others tried */ michael@0: #define LONGMUL32HI(w, u, v) { \ michael@0: uInt u0, u1, v0, v1, w0, w1, w2, t; \ michael@0: u0=u & 0xffff; u1=u>>16; \ michael@0: v0=v & 0xffff; v1=v>>16; \ michael@0: w0=u0*v0; \ michael@0: t=u1*v0 + (w0>>16); \ michael@0: w1=t & 0xffff; w2=t>>16; \ michael@0: w1=u0*v1 + w1; \ michael@0: (w)=u1*v1 + w2 + (w1>>16);} michael@0: michael@0: /* ROUNDUP -- round an integer up to a multiple of n */ michael@0: #define ROUNDUP(i, n) ((((i)+(n)-1)/n)*n) michael@0: #define ROUNDUP4(i) (((i)+3)&~3) /* special for n=4 */ michael@0: michael@0: /* ROUNDDOWN -- round an integer down to a multiple of n */ michael@0: #define ROUNDDOWN(i, n) (((i)/n)*n) michael@0: #define ROUNDDOWN4(i) ((i)&~3) /* special for n=4 */ michael@0: michael@0: /* References to multi-byte sequences under different sizes; these */ michael@0: /* require locally declared variables, but do not violate strict */ michael@0: /* aliasing or alignment (as did the UINTAT simple cast to uInt). */ michael@0: /* Variables needed are uswork, uiwork, etc. [so do not use at same */ michael@0: /* level in an expression, e.g., UBTOUI(x)==UBTOUI(y) may fail]. */ michael@0: michael@0: /* Return a uInt, etc., from bytes starting at a char* or uByte* */ michael@0: #define UBTOUS(b) (memcpy((void *)&uswork, b, 2), uswork) michael@0: #define UBTOUI(b) (memcpy((void *)&uiwork, b, 4), uiwork) michael@0: michael@0: /* Store a uInt, etc., into bytes starting at a char* or uByte*. */ michael@0: /* Returns i, evaluated, for convenience; has to use uiwork because */ michael@0: /* i may be an expression. */ michael@0: #define UBFROMUS(b, i) (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork) michael@0: #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork) michael@0: michael@0: /* X10 and X100 -- multiply integer i by 10 or 100 */ michael@0: /* [shifts are usually faster than multiply; could be conditional] */ michael@0: #define X10(i) (((i)<<1)+((i)<<3)) michael@0: #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6)) michael@0: michael@0: /* MAXI and MINI -- general max & min (not in ANSI) for integers */ michael@0: #define MAXI(x,y) ((x)<(y)?(y):(x)) michael@0: #define MINI(x,y) ((x)>(y)?(y):(x)) michael@0: michael@0: /* Useful constants */ michael@0: #define BILLION 1000000000 /* 10**9 */ michael@0: /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC */ michael@0: #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0') michael@0: michael@0: michael@0: /* ---------------------------------------------------------------- */ michael@0: /* Definitions for arbitary-precision modules (only valid after */ michael@0: /* decNumber.h has been included) */ michael@0: /* ---------------------------------------------------------------- */ michael@0: michael@0: /* Limits and constants */ michael@0: #define DECNUMMAXP 999999999 /* maximum precision code can handle */ michael@0: #define DECNUMMAXE 999999999 /* maximum adjusted exponent ditto */ michael@0: #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto */ michael@0: #if (DECNUMMAXP != DEC_MAX_DIGITS) michael@0: #error Maximum digits mismatch michael@0: #endif michael@0: #if (DECNUMMAXE != DEC_MAX_EMAX) michael@0: #error Maximum exponent mismatch michael@0: #endif michael@0: #if (DECNUMMINE != DEC_MIN_EMIN) michael@0: #error Minimum exponent mismatch michael@0: #endif michael@0: michael@0: /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN */ michael@0: /* digits, and D2UTABLE -- the initializer for the D2U table */ michael@0: #if DECDPUN==1 michael@0: #define DECDPUNMAX 9 michael@0: #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, \ michael@0: 18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \ michael@0: 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \ michael@0: 48,49} michael@0: #elif DECDPUN==2 michael@0: #define DECDPUNMAX 99 michael@0: #define D2UTABLE {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10, \ michael@0: 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \ michael@0: 18,19,19,20,20,21,21,22,22,23,23,24,24,25} michael@0: #elif DECDPUN==3 michael@0: #define DECDPUNMAX 999 michael@0: #define D2UTABLE {0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7, \ michael@0: 8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \ michael@0: 13,14,14,14,15,15,15,16,16,16,17} michael@0: #elif DECDPUN==4 michael@0: #define DECDPUNMAX 9999 michael@0: #define D2UTABLE {0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6, \ michael@0: 6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \ michael@0: 11,11,11,12,12,12,12,13} michael@0: #elif DECDPUN==5 michael@0: #define DECDPUNMAX 99999 michael@0: #define D2UTABLE {0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5, \ michael@0: 5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9, \ michael@0: 9,9,10,10,10,10} michael@0: #elif DECDPUN==6 michael@0: #define DECDPUNMAX 999999 michael@0: #define D2UTABLE {0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4, \ michael@0: 4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8, \ michael@0: 8,8,8,8,8,9} michael@0: #elif DECDPUN==7 michael@0: #define DECDPUNMAX 9999999 michael@0: #define D2UTABLE {0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3, \ michael@0: 4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7, \ michael@0: 7,7,7,7,7,7} michael@0: #elif DECDPUN==8 michael@0: #define DECDPUNMAX 99999999 michael@0: #define D2UTABLE {0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3, \ michael@0: 3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6, \ michael@0: 6,6,6,6,6,7} michael@0: #elif DECDPUN==9 michael@0: #define DECDPUNMAX 999999999 michael@0: #define D2UTABLE {0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3, \ michael@0: 3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5, \ michael@0: 5,5,6,6,6,6} michael@0: #elif defined(DECDPUN) michael@0: #error DECDPUN must be in the range 1-9 michael@0: #endif michael@0: michael@0: /* ----- Shared data (in decNumber.c) ----- */ michael@0: /* Public lookup table used by the D2U macro (see below) */ michael@0: #define DECMAXD2U 49 michael@0: /*extern const uByte d2utable[DECMAXD2U+1];*/ michael@0: michael@0: /* ----- Macros ----- */ michael@0: /* ISZERO -- return true if decNumber dn is a zero */ michael@0: /* [performance-critical in some situations] */ michael@0: #define ISZERO(dn) decNumberIsZero(dn) /* now just a local name */ michael@0: michael@0: /* D2U -- return the number of Units needed to hold d digits */ michael@0: /* (runtime version, with table lookaside for small d) */ michael@0: #if DECDPUN==8 michael@0: #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3)) michael@0: #elif DECDPUN==4 michael@0: #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2)) michael@0: #else michael@0: #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN) michael@0: #endif michael@0: /* SD2U -- static D2U macro (for compile-time calculation) */ michael@0: #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN) michael@0: michael@0: /* MSUDIGITS -- returns digits in msu, from digits, calculated */ michael@0: /* using D2U */ michael@0: #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN) michael@0: michael@0: /* D2N -- return the number of decNumber structs that would be */ michael@0: /* needed to contain that number of digits (and the initial */ michael@0: /* decNumber struct) safely. Note that one Unit is included in the */ michael@0: /* initial structure. Used for allocating space that is aligned on */ michael@0: /* a decNumber struct boundary. */ michael@0: #define D2N(d) \ michael@0: ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber)) michael@0: michael@0: /* TODIGIT -- macro to remove the leading digit from the unsigned */ michael@0: /* integer u at column cut (counting from the right, LSD=0) and */ michael@0: /* place it as an ASCII character into the character pointed to by */ michael@0: /* c. Note that cut must be <= 9, and the maximum value for u is */ michael@0: /* 2,000,000,000 (as is needed for negative exponents of */ michael@0: /* subnormals). The unsigned integer pow is used as a temporary */ michael@0: /* variable. */ michael@0: #define TODIGIT(u, cut, c, pow) { \ michael@0: *(c)='0'; \ michael@0: pow=DECPOWERS[cut]*2; \ michael@0: if ((u)>pow) { \ michael@0: pow*=4; \ michael@0: if ((u)>=pow) {(u)-=pow; *(c)+=8;} \ michael@0: pow/=2; \ michael@0: if ((u)>=pow) {(u)-=pow; *(c)+=4;} \ michael@0: pow/=2; \ michael@0: } \ michael@0: if ((u)>=pow) {(u)-=pow; *(c)+=2;} \ michael@0: pow/=2; \ michael@0: if ((u)>=pow) {(u)-=pow; *(c)+=1;} \ michael@0: } michael@0: michael@0: /* ---------------------------------------------------------------- */ michael@0: /* Definitions for fixed-precision modules (only valid after */ michael@0: /* decSingle.h, decDouble.h, or decQuad.h has been included) */ michael@0: /* ---------------------------------------------------------------- */ michael@0: michael@0: /* bcdnum -- a structure describing a format-independent finite */ michael@0: /* number, whose coefficient is a string of bcd8 uBytes */ michael@0: typedef struct { michael@0: uByte *msd; /* -> most significant digit */ michael@0: uByte *lsd; /* -> least ditto */ michael@0: uInt sign; /* 0=positive, DECFLOAT_Sign=negative */ michael@0: Int exponent; /* Unadjusted signed exponent (q), or */ michael@0: /* DECFLOAT_NaN etc. for a special */ michael@0: } bcdnum; michael@0: michael@0: /* Test if exponent or bcdnum exponent must be a special, etc. */ michael@0: #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp) michael@0: #define EXPISINF(exp) (exp==DECFLOAT_Inf) michael@0: #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN) michael@0: #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent)) michael@0: michael@0: /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian */ michael@0: /* (array) notation (the 0 word or byte contains the sign bit), */ michael@0: /* automatically adjusting for endianness; similarly address a word */ michael@0: /* in the next-wider format (decFloatWider, or dfw) */ michael@0: #define DECWORDS (DECBYTES/4) michael@0: #define DECWWORDS (DECWBYTES/4) michael@0: #if DECLITEND michael@0: #define DFBYTE(df, off) ((df)->bytes[DECBYTES-1-(off)]) michael@0: #define DFWORD(df, off) ((df)->words[DECWORDS-1-(off)]) michael@0: #define DFWWORD(dfw, off) ((dfw)->words[DECWWORDS-1-(off)]) michael@0: #else michael@0: #define DFBYTE(df, off) ((df)->bytes[off]) michael@0: #define DFWORD(df, off) ((df)->words[off]) michael@0: #define DFWWORD(dfw, off) ((dfw)->words[off]) michael@0: #endif michael@0: michael@0: /* Tests for sign or specials, directly on DECFLOATs */ michael@0: #define DFISSIGNED(df) (DFWORD(df, 0)&0x80000000) michael@0: #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000) michael@0: #define DFISINF(df) ((DFWORD(df, 0)&0x7c000000)==0x78000000) michael@0: #define DFISNAN(df) ((DFWORD(df, 0)&0x7c000000)==0x7c000000) michael@0: #define DFISQNAN(df) ((DFWORD(df, 0)&0x7e000000)==0x7c000000) michael@0: #define DFISSNAN(df) ((DFWORD(df, 0)&0x7e000000)==0x7e000000) michael@0: michael@0: /* Shared lookup tables */ michael@0: extern const uInt DECCOMBMSD[64]; /* Combination field -> MSD */ michael@0: extern const uInt DECCOMBFROM[48]; /* exp+msd -> Combination */ michael@0: michael@0: /* Private generic (utility) routine */ michael@0: #if DECCHECK || DECTRACE michael@0: extern void decShowNum(const bcdnum *, const char *); michael@0: #endif michael@0: michael@0: /* Format-dependent macros and constants */ michael@0: #if defined(DECPMAX) michael@0: michael@0: /* Useful constants */ michael@0: #define DECPMAX9 (ROUNDUP(DECPMAX, 9)/9) /* 'Pmax' in 10**9s */ michael@0: /* Top words for a zero */ michael@0: #define SINGLEZERO 0x22500000 michael@0: #define DOUBLEZERO 0x22380000 michael@0: #define QUADZERO 0x22080000 michael@0: /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */ michael@0: michael@0: /* Format-dependent common tests: */ michael@0: /* DFISZERO -- test for (any) zero */ michael@0: /* DFISCCZERO -- test for coefficient continuation being zero */ michael@0: /* DFISCC01 -- test for coefficient contains only 0s and 1s */ michael@0: /* DFISINT -- test for finite and exponent q=0 */ michael@0: /* DFISUINT01 -- test for sign=0, finite, exponent q=0, and */ michael@0: /* MSD=0 or 1 */ michael@0: /* ZEROWORD is also defined here. */ michael@0: /* In DFISZERO the first test checks the least-significant word */ michael@0: /* (most likely to be non-zero); the penultimate tests MSD and */ michael@0: /* DPDs in the signword, and the final test excludes specials and */ michael@0: /* MSD>7. DFISINT similarly has to allow for the two forms of */ michael@0: /* MSD codes. DFISUINT01 only has to allow for one form of MSD */ michael@0: /* code. */ michael@0: #if DECPMAX==7 michael@0: #define ZEROWORD SINGLEZERO michael@0: /* [test macros not needed except for Zero] */ michael@0: #define DFISZERO(df) ((DFWORD(df, 0)&0x1c0fffff)==0 \ michael@0: && (DFWORD(df, 0)&0x60000000)!=0x60000000) michael@0: #elif DECPMAX==16 michael@0: #define ZEROWORD DOUBLEZERO michael@0: #define DFISZERO(df) ((DFWORD(df, 1)==0 \ michael@0: && (DFWORD(df, 0)&0x1c03ffff)==0 \ michael@0: && (DFWORD(df, 0)&0x60000000)!=0x60000000)) michael@0: #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000 \ michael@0: ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000) michael@0: #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000) michael@0: #define DFISCCZERO(df) (DFWORD(df, 1)==0 \ michael@0: && (DFWORD(df, 0)&0x0003ffff)==0) michael@0: #define DFISCC01(df) ((DFWORD(df, 0)&~0xfffc9124)==0 \ michael@0: && (DFWORD(df, 1)&~0x49124491)==0) michael@0: #elif DECPMAX==34 michael@0: #define ZEROWORD QUADZERO michael@0: #define DFISZERO(df) ((DFWORD(df, 3)==0 \ michael@0: && DFWORD(df, 2)==0 \ michael@0: && DFWORD(df, 1)==0 \ michael@0: && (DFWORD(df, 0)&0x1c003fff)==0 \ michael@0: && (DFWORD(df, 0)&0x60000000)!=0x60000000)) michael@0: #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000 \ michael@0: ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000) michael@0: #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000) michael@0: #define DFISCCZERO(df) (DFWORD(df, 3)==0 \ michael@0: && DFWORD(df, 2)==0 \ michael@0: && DFWORD(df, 1)==0 \ michael@0: && (DFWORD(df, 0)&0x00003fff)==0) michael@0: michael@0: #define DFISCC01(df) ((DFWORD(df, 0)&~0xffffc912)==0 \ michael@0: && (DFWORD(df, 1)&~0x44912449)==0 \ michael@0: && (DFWORD(df, 2)&~0x12449124)==0 \ michael@0: && (DFWORD(df, 3)&~0x49124491)==0) michael@0: #endif michael@0: michael@0: /* Macros to test if a certain 10 bits of a uInt or pair of uInts */ michael@0: /* are a canonical declet [higher or lower bits are ignored]. */ michael@0: /* declet is at offset 0 (from the right) in a uInt: */ michael@0: #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e) michael@0: /* declet is at offset k (a multiple of 2) in a uInt: */ michael@0: #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0 \ michael@0: || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k))) michael@0: /* declet is at offset k (a multiple of 2) in a pair of uInts: */ michael@0: /* [the top 2 bits will always be in the more-significant uInt] */ michael@0: #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0 \ michael@0: || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k))) \ michael@0: || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k))) michael@0: michael@0: /* Macro to test whether a full-length (length DECPMAX) BCD8 */ michael@0: /* coefficient, starting at uByte u, is all zeros */ michael@0: /* Test just the LSWord first, then the remainder as a sequence */ michael@0: /* of tests in order to avoid same-level use of UBTOUI */ michael@0: #if DECPMAX==7 michael@0: #define ISCOEFFZERO(u) ( \ michael@0: UBTOUI((u)+DECPMAX-4)==0 \ michael@0: && UBTOUS((u)+DECPMAX-6)==0 \ michael@0: && *(u)==0) michael@0: #elif DECPMAX==16 michael@0: #define ISCOEFFZERO(u) ( \ michael@0: UBTOUI((u)+DECPMAX-4)==0 \ michael@0: && UBTOUI((u)+DECPMAX-8)==0 \ michael@0: && UBTOUI((u)+DECPMAX-12)==0 \ michael@0: && UBTOUI(u)==0) michael@0: #elif DECPMAX==34 michael@0: #define ISCOEFFZERO(u) ( \ michael@0: UBTOUI((u)+DECPMAX-4)==0 \ michael@0: && UBTOUI((u)+DECPMAX-8)==0 \ michael@0: && UBTOUI((u)+DECPMAX-12)==0 \ michael@0: && UBTOUI((u)+DECPMAX-16)==0 \ michael@0: && UBTOUI((u)+DECPMAX-20)==0 \ michael@0: && UBTOUI((u)+DECPMAX-24)==0 \ michael@0: && UBTOUI((u)+DECPMAX-28)==0 \ michael@0: && UBTOUI((u)+DECPMAX-32)==0 \ michael@0: && UBTOUS(u)==0) michael@0: #endif michael@0: michael@0: /* Macros and masks for the exponent continuation field and MSD */ michael@0: /* Get the exponent continuation from a decFloat *df as an Int */ michael@0: #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL))) michael@0: /* Ditto, from the next-wider format */ michael@0: #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL))) michael@0: /* Get the biased exponent similarly */ michael@0: #define GETEXP(df) ((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df))) michael@0: /* Get the unbiased exponent similarly */ michael@0: #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS) michael@0: /* Get the MSD similarly (as uInt) */ michael@0: #define GETMSD(df) (DECCOMBMSD[DFWORD((df), 0)>>26]) michael@0: michael@0: /* Compile-time computes of the exponent continuation field masks */ michael@0: /* full exponent continuation field: */ michael@0: #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL)) michael@0: /* same, not including its first digit (the qNaN/sNaN selector): */ michael@0: #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL)) michael@0: michael@0: /* Macros to decode the coefficient in a finite decFloat *df into */ michael@0: /* a BCD string (uByte *bcdin) of length DECPMAX uBytes. */ michael@0: michael@0: /* In-line sequence to convert least significant 10 bits of uInt */ michael@0: /* dpd to three BCD8 digits starting at uByte u. Note that an */ michael@0: /* extra byte is written to the right of the three digits because */ michael@0: /* four bytes are moved at a time for speed; the alternative */ michael@0: /* macro moves exactly three bytes (usually slower). */ michael@0: #define dpd2bcd8(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 4) michael@0: #define dpd2bcd83(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 3) michael@0: michael@0: /* Decode the declets. After extracting each one, it is decoded */ michael@0: /* to BCD8 using a table lookup (also used for variable-length */ michael@0: /* decode). Each DPD decode is 3 bytes BCD8 plus a one-byte */ michael@0: /* length which is not used, here). Fixed-length 4-byte moves */ michael@0: /* are fast, however, almost everywhere, and so are used except */ michael@0: /* for the final three bytes (to avoid overrun). The code below */ michael@0: /* is 36 instructions for Doubles and about 70 for Quads, even */ michael@0: /* on IA32. */ michael@0: michael@0: /* Two macros are defined for each format: */ michael@0: /* GETCOEFF extracts the coefficient of the current format */ michael@0: /* GETWCOEFF extracts the coefficient of the next-wider format. */ michael@0: /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */ michael@0: michael@0: #if DECPMAX==7 michael@0: #define GETCOEFF(df, bcd) { \ michael@0: uInt sourhi=DFWORD(df, 0); \ michael@0: *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \ michael@0: dpd2bcd8(bcd+1, sourhi>>10); \ michael@0: dpd2bcd83(bcd+4, sourhi);} michael@0: #define GETWCOEFF(df, bcd) { \ michael@0: uInt sourhi=DFWWORD(df, 0); \ michael@0: uInt sourlo=DFWWORD(df, 1); \ michael@0: *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \ michael@0: dpd2bcd8(bcd+1, sourhi>>8); \ michael@0: dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30)); \ michael@0: dpd2bcd8(bcd+7, sourlo>>20); \ michael@0: dpd2bcd8(bcd+10, sourlo>>10); \ michael@0: dpd2bcd83(bcd+13, sourlo);} michael@0: michael@0: #elif DECPMAX==16 michael@0: #define GETCOEFF(df, bcd) { \ michael@0: uInt sourhi=DFWORD(df, 0); \ michael@0: uInt sourlo=DFWORD(df, 1); \ michael@0: *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \ michael@0: dpd2bcd8(bcd+1, sourhi>>8); \ michael@0: dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30)); \ michael@0: dpd2bcd8(bcd+7, sourlo>>20); \ michael@0: dpd2bcd8(bcd+10, sourlo>>10); \ michael@0: dpd2bcd83(bcd+13, sourlo);} michael@0: #define GETWCOEFF(df, bcd) { \ michael@0: uInt sourhi=DFWWORD(df, 0); \ michael@0: uInt sourmh=DFWWORD(df, 1); \ michael@0: uInt sourml=DFWWORD(df, 2); \ michael@0: uInt sourlo=DFWWORD(df, 3); \ michael@0: *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \ michael@0: dpd2bcd8(bcd+1, sourhi>>4); \ michael@0: dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26)); \ michael@0: dpd2bcd8(bcd+7, sourmh>>16); \ michael@0: dpd2bcd8(bcd+10, sourmh>>6); \ michael@0: dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28)); \ michael@0: dpd2bcd8(bcd+16, sourml>>18); \ michael@0: dpd2bcd8(bcd+19, sourml>>8); \ michael@0: dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30)); \ michael@0: dpd2bcd8(bcd+25, sourlo>>20); \ michael@0: dpd2bcd8(bcd+28, sourlo>>10); \ michael@0: dpd2bcd83(bcd+31, sourlo);} michael@0: michael@0: #elif DECPMAX==34 michael@0: #define GETCOEFF(df, bcd) { \ michael@0: uInt sourhi=DFWORD(df, 0); \ michael@0: uInt sourmh=DFWORD(df, 1); \ michael@0: uInt sourml=DFWORD(df, 2); \ michael@0: uInt sourlo=DFWORD(df, 3); \ michael@0: *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \ michael@0: dpd2bcd8(bcd+1, sourhi>>4); \ michael@0: dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26)); \ michael@0: dpd2bcd8(bcd+7, sourmh>>16); \ michael@0: dpd2bcd8(bcd+10, sourmh>>6); \ michael@0: dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28)); \ michael@0: dpd2bcd8(bcd+16, sourml>>18); \ michael@0: dpd2bcd8(bcd+19, sourml>>8); \ michael@0: dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30)); \ michael@0: dpd2bcd8(bcd+25, sourlo>>20); \ michael@0: dpd2bcd8(bcd+28, sourlo>>10); \ michael@0: dpd2bcd83(bcd+31, sourlo);} michael@0: michael@0: #define GETWCOEFF(df, bcd) {??} /* [should never be used] */ michael@0: #endif michael@0: michael@0: /* Macros to decode the coefficient in a finite decFloat *df into */ michael@0: /* a base-billion uInt array, with the least-significant */ michael@0: /* 0-999999999 'digit' at offset 0. */ michael@0: michael@0: /* Decode the declets. After extracting each one, it is decoded */ michael@0: /* to binary using a table lookup. Three tables are used; one */ michael@0: /* the usual DPD to binary, the other two pre-multiplied by 1000 */ michael@0: /* and 1000000 to avoid multiplication during decode. These */ michael@0: /* tables can also be used for multiplying up the MSD as the DPD */ michael@0: /* code for 0 through 9 is the identity. */ michael@0: #define DPD2BIN0 DPD2BIN /* for prettier code */ michael@0: michael@0: #if DECPMAX==7 michael@0: #define GETCOEFFBILL(df, buf) { \ michael@0: uInt sourhi=DFWORD(df, 0); \ michael@0: (buf)[0]=DPD2BIN0[sourhi&0x3ff] \ michael@0: +DPD2BINK[(sourhi>>10)&0x3ff] \ michael@0: +DPD2BINM[DECCOMBMSD[sourhi>>26]];} michael@0: michael@0: #elif DECPMAX==16 michael@0: #define GETCOEFFBILL(df, buf) { \ michael@0: uInt sourhi, sourlo; \ michael@0: sourlo=DFWORD(df, 1); \ michael@0: (buf)[0]=DPD2BIN0[sourlo&0x3ff] \ michael@0: +DPD2BINK[(sourlo>>10)&0x3ff] \ michael@0: +DPD2BINM[(sourlo>>20)&0x3ff]; \ michael@0: sourhi=DFWORD(df, 0); \ michael@0: (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff] \ michael@0: +DPD2BINK[(sourhi>>8)&0x3ff] \ michael@0: +DPD2BINM[DECCOMBMSD[sourhi>>26]];} michael@0: michael@0: #elif DECPMAX==34 michael@0: #define GETCOEFFBILL(df, buf) { \ michael@0: uInt sourhi, sourmh, sourml, sourlo; \ michael@0: sourlo=DFWORD(df, 3); \ michael@0: (buf)[0]=DPD2BIN0[sourlo&0x3ff] \ michael@0: +DPD2BINK[(sourlo>>10)&0x3ff] \ michael@0: +DPD2BINM[(sourlo>>20)&0x3ff]; \ michael@0: sourml=DFWORD(df, 2); \ michael@0: (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff] \ michael@0: +DPD2BINK[(sourml>>8)&0x3ff] \ michael@0: +DPD2BINM[(sourml>>18)&0x3ff]; \ michael@0: sourmh=DFWORD(df, 1); \ michael@0: (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff] \ michael@0: +DPD2BINK[(sourmh>>6)&0x3ff] \ michael@0: +DPD2BINM[(sourmh>>16)&0x3ff]; \ michael@0: sourhi=DFWORD(df, 0); \ michael@0: (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff] \ michael@0: +DPD2BINK[(sourhi>>4)&0x3ff] \ michael@0: +DPD2BINM[DECCOMBMSD[sourhi>>26]];} michael@0: michael@0: #endif michael@0: michael@0: /* Macros to decode the coefficient in a finite decFloat *df into */ michael@0: /* a base-thousand uInt array (of size DECLETS+1, to allow for */ michael@0: /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/ michael@0: michael@0: /* Decode the declets. After extracting each one, it is decoded */ michael@0: /* to binary using a table lookup. */ michael@0: #if DECPMAX==7 michael@0: #define GETCOEFFTHOU(df, buf) { \ michael@0: uInt sourhi=DFWORD(df, 0); \ michael@0: (buf)[0]=DPD2BIN[sourhi&0x3ff]; \ michael@0: (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff]; \ michael@0: (buf)[2]=DECCOMBMSD[sourhi>>26];} michael@0: michael@0: #elif DECPMAX==16 michael@0: #define GETCOEFFTHOU(df, buf) { \ michael@0: uInt sourhi, sourlo; \ michael@0: sourlo=DFWORD(df, 1); \ michael@0: (buf)[0]=DPD2BIN[sourlo&0x3ff]; \ michael@0: (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff]; \ michael@0: (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff]; \ michael@0: sourhi=DFWORD(df, 0); \ michael@0: (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff]; \ michael@0: (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff]; \ michael@0: (buf)[5]=DECCOMBMSD[sourhi>>26];} michael@0: michael@0: #elif DECPMAX==34 michael@0: #define GETCOEFFTHOU(df, buf) { \ michael@0: uInt sourhi, sourmh, sourml, sourlo; \ michael@0: sourlo=DFWORD(df, 3); \ michael@0: (buf)[0]=DPD2BIN[sourlo&0x3ff]; \ michael@0: (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff]; \ michael@0: (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff]; \ michael@0: sourml=DFWORD(df, 2); \ michael@0: (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff]; \ michael@0: (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff]; \ michael@0: (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff]; \ michael@0: sourmh=DFWORD(df, 1); \ michael@0: (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff]; \ michael@0: (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff]; \ michael@0: (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff]; \ michael@0: sourhi=DFWORD(df, 0); \ michael@0: (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff]; \ michael@0: (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff]; \ michael@0: (buf)[11]=DECCOMBMSD[sourhi>>26];} michael@0: #endif michael@0: michael@0: michael@0: /* Macros to decode the coefficient in a finite decFloat *df and */ michael@0: /* add to a base-thousand uInt array (as for GETCOEFFTHOU). */ michael@0: /* After the addition then most significant 'digit' in the array */ michael@0: /* might have a value larger then 10 (with a maximum of 19). */ michael@0: #if DECPMAX==7 michael@0: #define ADDCOEFFTHOU(df, buf) { \ michael@0: uInt sourhi=DFWORD(df, 0); \ michael@0: (buf)[0]+=DPD2BIN[sourhi&0x3ff]; \ michael@0: if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \ michael@0: (buf)[1]+=DPD2BIN[(sourhi>>10)&0x3ff]; \ michael@0: if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \ michael@0: (buf)[2]+=DECCOMBMSD[sourhi>>26];} michael@0: michael@0: #elif DECPMAX==16 michael@0: #define ADDCOEFFTHOU(df, buf) { \ michael@0: uInt sourhi, sourlo; \ michael@0: sourlo=DFWORD(df, 1); \ michael@0: (buf)[0]+=DPD2BIN[sourlo&0x3ff]; \ michael@0: if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \ michael@0: (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff]; \ michael@0: if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \ michael@0: (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff]; \ michael@0: if (buf[2]>999) {buf[2]-=1000; buf[3]++;} \ michael@0: sourhi=DFWORD(df, 0); \ michael@0: (buf)[3]+=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff]; \ michael@0: if (buf[3]>999) {buf[3]-=1000; buf[4]++;} \ michael@0: (buf)[4]+=DPD2BIN[(sourhi>>8)&0x3ff]; \ michael@0: if (buf[4]>999) {buf[4]-=1000; buf[5]++;} \ michael@0: (buf)[5]+=DECCOMBMSD[sourhi>>26];} michael@0: michael@0: #elif DECPMAX==34 michael@0: #define ADDCOEFFTHOU(df, buf) { \ michael@0: uInt sourhi, sourmh, sourml, sourlo; \ michael@0: sourlo=DFWORD(df, 3); \ michael@0: (buf)[0]+=DPD2BIN[sourlo&0x3ff]; \ michael@0: if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \ michael@0: (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff]; \ michael@0: if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \ michael@0: (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff]; \ michael@0: if (buf[2]>999) {buf[2]-=1000; buf[3]++;} \ michael@0: sourml=DFWORD(df, 2); \ michael@0: (buf)[3]+=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff]; \ michael@0: if (buf[3]>999) {buf[3]-=1000; buf[4]++;} \ michael@0: (buf)[4]+=DPD2BIN[(sourml>>8)&0x3ff]; \ michael@0: if (buf[4]>999) {buf[4]-=1000; buf[5]++;} \ michael@0: (buf)[5]+=DPD2BIN[(sourml>>18)&0x3ff]; \ michael@0: if (buf[5]>999) {buf[5]-=1000; buf[6]++;} \ michael@0: sourmh=DFWORD(df, 1); \ michael@0: (buf)[6]+=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff]; \ michael@0: if (buf[6]>999) {buf[6]-=1000; buf[7]++;} \ michael@0: (buf)[7]+=DPD2BIN[(sourmh>>6)&0x3ff]; \ michael@0: if (buf[7]>999) {buf[7]-=1000; buf[8]++;} \ michael@0: (buf)[8]+=DPD2BIN[(sourmh>>16)&0x3ff]; \ michael@0: if (buf[8]>999) {buf[8]-=1000; buf[9]++;} \ michael@0: sourhi=DFWORD(df, 0); \ michael@0: (buf)[9]+=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff]; \ michael@0: if (buf[9]>999) {buf[9]-=1000; buf[10]++;} \ michael@0: (buf)[10]+=DPD2BIN[(sourhi>>4)&0x3ff]; \ michael@0: if (buf[10]>999) {buf[10]-=1000; buf[11]++;} \ michael@0: (buf)[11]+=DECCOMBMSD[sourhi>>26];} michael@0: #endif michael@0: michael@0: michael@0: /* Set a decFloat to the maximum positive finite number (Nmax) */ michael@0: #if DECPMAX==7 michael@0: #define DFSETNMAX(df) \ michael@0: {DFWORD(df, 0)=0x77f3fcff;} michael@0: #elif DECPMAX==16 michael@0: #define DFSETNMAX(df) \ michael@0: {DFWORD(df, 0)=0x77fcff3f; \ michael@0: DFWORD(df, 1)=0xcff3fcff;} michael@0: #elif DECPMAX==34 michael@0: #define DFSETNMAX(df) \ michael@0: {DFWORD(df, 0)=0x77ffcff3; \ michael@0: DFWORD(df, 1)=0xfcff3fcf; \ michael@0: DFWORD(df, 2)=0xf3fcff3f; \ michael@0: DFWORD(df, 3)=0xcff3fcff;} michael@0: #endif michael@0: michael@0: /* [end of format-dependent macros and constants] */ michael@0: #endif michael@0: michael@0: #else michael@0: #error decNumberLocal included more than once michael@0: #endif