// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ------------------------------------------------------------------ */ /* decNumber package local type, tuning, and macro definitions */ /* ------------------------------------------------------------------ */ /* Copyright (c) IBM Corporation, 2000-2016. All rights reserved. */ /* */ /* This software is made available under the terms of the */ /* ICU License -- ICU 1.8.1 and later. */ /* */ /* The description and User's Guide ("The decNumber C Library") for */ /* this software is called decNumber.pdf. This document is */ /* available, together with arithmetic and format specifications, */ /* testcases, and Web links, on the General Decimal Arithmetic page. */ /* */ /* Please send comments, suggestions, and corrections to the author: */ /* [email protected] */ /* Mike Cowlishaw, IBM Fellow */ /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */ /* ------------------------------------------------------------------ */ /* This header file is included by all modules in the decNumber */ /* library, and contains local type definitions, tuning parameters, */ /* etc. It should not need to be used by application programs. */ /* decNumber.h or one of decDouble (etc.) must be included first. */ /* ------------------------------------------------------------------ */ #if !defined(DECNUMBERLOC) #define DECNUMBERLOC #define DECVERSION … #define DECNLAUTHOR … #include <stdlib.h> /* for abs */ #include <string.h> /* for memset, strcpy */ #include "decContext.h" /* Conditional code flag -- set this to match hardware platform */ #if !defined(DECLITEND) #define DECLITEND … #endif /* Conditional code flag -- set this to 1 for best performance */ #if !defined(DECUSE64) #define DECUSE64 … #endif /* Conditional check flags -- set these to 0 for best performance */ #if !defined(DECCHECK) #define DECCHECK … #endif #if !defined(DECALLOC) #define DECALLOC … #endif #if !defined(DECTRACE) #define DECTRACE … #endif /* Tuning parameter for decNumber (arbitrary precision) module */ #if !defined(DECBUFFER) #define DECBUFFER … /* should be a common maximum precision */ /* rounded up to a multiple of 4; must */ /* be zero or positive. */ #endif /* ---------------------------------------------------------------- */ /* Definitions for all modules (general-purpose) */ /* ---------------------------------------------------------------- */ /* Local names for common types -- for safety, decNumber modules do */ /* not use int or long directly. */ #define Flag … #define Byte … #define uByte … #define Short … #define uShort … #define Int … #define uInt … #define Unit … #if DECUSE64 #define Long … #define uLong … #endif /* Development-use definitions */ LI; /* for printf arguments only */ #define DECNOINT … /* or stdint types */ #if DECNOINT /* if these interfere with your C includes, do not set DECNOINT */ #define int ? /* enable to ensure that plain C 'int' */ #define long ?? /* .. or 'long' types are not used */ #endif /* LONGMUL32HI -- set w=(u*v)>>32, where w, u, and v are uInts */ /* (that is, sets w to be the high-order word of the 64-bit result; */ /* the low-order word is simply u*v.) */ /* This version is derived from Knuth via Hacker's Delight; */ /* it seems to optimize better than some others tried */ #define LONGMUL32HI(w, u, v) … /* ROUNDUP -- round an integer up to a multiple of n */ #define ROUNDUP(i, n) … #define ROUNDUP4(i) … /* ROUNDDOWN -- round an integer down to a multiple of n */ #define ROUNDDOWN(i, n) … #define ROUNDDOWN4(i) … /* References to multi-byte sequences under different sizes; these */ /* require locally declared variables, but do not violate strict */ /* aliasing or alignment (as did the UINTAT simple cast to uInt). */ /* Variables needed are uswork, uiwork, etc. [so do not use at same */ /* level in an expression, e.g., UBTOUI(x)==UBTOUI(y) may fail]. */ /* Return a uInt, etc., from bytes starting at a char* or uByte* */ #define UBTOUS(b) … #define UBTOUI(b) … /* Store a uInt, etc., into bytes starting at a char* or uByte*. */ /* Returns i, evaluated, for convenience; has to use uiwork because */ /* i may be an expression. */ #define UBFROMUS(b, i) … #define UBFROMUI(b, i) … /* X10 and X100 -- multiply integer i by 10 or 100 */ /* [shifts are usually faster than multiply; could be conditional] */ #define X10(i) … #define X100(i) … /* MAXI and MINI -- general max & min (not in ANSI) for integers */ #define MAXI(x,y) … #define MINI(x,y) … /* Useful constants */ #define BILLION … /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC */ #define CHARMASK … /* ---------------------------------------------------------------- */ /* Definitions for arbitrary-precision modules (only valid after */ /* decNumber.h has been included) */ /* ---------------------------------------------------------------- */ /* Limits and constants */ #define DECNUMMAXP … #define DECNUMMAXE … #define DECNUMMINE … #if (DECNUMMAXP != DEC_MAX_DIGITS) #error Maximum digits mismatch #endif #if (DECNUMMAXE != DEC_MAX_EMAX) #error Maximum exponent mismatch #endif #if (DECNUMMINE != DEC_MIN_EMIN) #error Minimum exponent mismatch #endif /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN */ /* digits, and D2UTABLE -- the initializer for the D2U table */ #ifndef DECDPUN // no-op #elif DECDPUN==1 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==2 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==3 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==4 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==5 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==6 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==7 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==8 #define DECDPUNMAX … #define D2UTABLE … #elif DECDPUN==9 #define DECDPUNMAX … #define D2UTABLE … #else #error DECDPUN must be in the range 1-9 #endif /* ----- Shared data (in decNumber.c) ----- */ /* Public lookup table used by the D2U macro (see below) */ #define DECMAXD2U … /*extern const uByte d2utable[DECMAXD2U+1];*/ /* ----- Macros ----- */ /* ISZERO -- return true if decNumber dn is a zero */ /* [performance-critical in some situations] */ #define ISZERO(dn) … /* D2U -- return the number of Units needed to hold d digits */ /* (runtime version, with table lookaside for small d) */ #if defined(DECDPUN) && DECDPUN==8 #define D2U … #elif defined(DECDPUN) && DECDPUN==4 #define D2U … #else #define D2U(d) … #endif /* SD2U -- static D2U macro (for compile-time calculation) */ #define SD2U(d) … /* MSUDIGITS -- returns digits in msu, from digits, calculated */ /* using D2U */ #define MSUDIGITS(d) … /* D2N -- return the number of decNumber structs that would be */ /* needed to contain that number of digits (and the initial */ /* decNumber struct) safely. Note that one Unit is included in the */ /* initial structure. Used for allocating space that is aligned on */ /* a decNumber struct boundary. */ #define D2N(d) … /* TODIGIT -- macro to remove the leading digit from the unsigned */ /* integer u at column cut (counting from the right, LSD=0) and */ /* place it as an ASCII character into the character pointed to by */ /* c. Note that cut must be <= 9, and the maximum value for u is */ /* 2,000,000,000 (as is needed for negative exponents of */ /* subnormals). The unsigned integer pow is used as a temporary */ /* variable. */ #define TODIGIT(u, cut, c, pow) … /* ---------------------------------------------------------------- */ /* Definitions for fixed-precision modules (only valid after */ /* decSingle.h, decDouble.h, or decQuad.h has been included) */ /* ---------------------------------------------------------------- */ /* bcdnum -- a structure describing a format-independent finite */ /* number, whose coefficient is a string of bcd8 uBytes */ bcdnum; /* Test if exponent or bcdnum exponent must be a special, etc. */ #define EXPISSPECIAL(exp) … #define EXPISINF(exp) … #define EXPISNAN(exp) … #define NUMISSPECIAL(num) … /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian */ /* (array) notation (the 0 word or byte contains the sign bit), */ /* automatically adjusting for endianness; similarly address a word */ /* in the next-wider format (decFloatWider, or dfw) */ #define DECWORDS … #define DECWWORDS … #if DECLITEND #define DFBYTE(df, off) … #define DFWORD(df, off) … #define DFWWORD(dfw, off) … #else #define DFBYTE … #define DFWORD … #define DFWWORD … #endif /* Tests for sign or specials, directly on DECFLOATs */ #define DFISSIGNED(df) … #define DFISSPECIAL(df) … #define DFISINF(df) … #define DFISNAN(df) … #define DFISQNAN(df) … #define DFISSNAN(df) … /* Shared lookup tables */ extern const uInt DECCOMBMSD[64]; /* Combination field -> MSD */ extern const uInt DECCOMBFROM[48]; /* exp+msd -> Combination */ /* Private generic (utility) routine */ #if DECCHECK || DECTRACE extern void decShowNum(const bcdnum *, const char *); #endif /* Format-dependent macros and constants */ #if defined(DECPMAX) /* Useful constants */ #define DECPMAX9 … /* Top words for a zero */ #define SINGLEZERO … #define DOUBLEZERO … #define QUADZERO … /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */ /* Format-dependent common tests: */ /* DFISZERO -- test for (any) zero */ /* DFISCCZERO -- test for coefficient continuation being zero */ /* DFISCC01 -- test for coefficient contains only 0s and 1s */ /* DFISINT -- test for finite and exponent q=0 */ /* DFISUINT01 -- test for sign=0, finite, exponent q=0, and */ /* MSD=0 or 1 */ /* ZEROWORD is also defined here. */ /* In DFISZERO the first test checks the least-significant word */ /* (most likely to be non-zero); the penultimate tests MSD and */ /* DPDs in the signword, and the final test excludes specials and */ /* MSD>7. DFISINT similarly has to allow for the two forms of */ /* MSD codes. DFISUINT01 only has to allow for one form of MSD */ /* code. */ #if DECPMAX==7 #define ZEROWORD … /* [test macros not needed except for Zero] */ #define DFISZERO … #elif DECPMAX==16 #define ZEROWORD … #define DFISZERO … #define DFISINT … #define DFISUINT01 … #define DFISCCZERO … #define DFISCC01 … #elif DECPMAX==34 #define ZEROWORD … #define DFISZERO … #define DFISINT … #define DFISUINT01 … #define DFISCCZERO … #define DFISCC01 … #endif /* Macros to test if a certain 10 bits of a uInt or pair of uInts */ /* are a canonical declet [higher or lower bits are ignored]. */ /* declet is at offset 0 (from the right) in a uInt: */ #define CANONDPD … /* declet is at offset k (a multiple of 2) in a uInt: */ #define CANONDPDOFF … /* declet is at offset k (a multiple of 2) in a pair of uInts: */ /* [the top 2 bits will always be in the more-significant uInt] */ #define CANONDPDTWO … /* Macro to test whether a full-length (length DECPMAX) BCD8 */ /* coefficient, starting at uByte u, is all zeros */ /* Test just the LSWord first, then the remainder as a sequence */ /* of tests in order to avoid same-level use of UBTOUI */ #if DECPMAX==7 #define ISCOEFFZERO … #elif DECPMAX==16 #define ISCOEFFZERO … #elif DECPMAX==34 #define ISCOEFFZERO … #endif /* Macros and masks for the exponent continuation field and MSD */ /* Get the exponent continuation from a decFloat *df as an Int */ #define GETECON … /* Ditto, from the next-wider format */ #define GETWECON … /* Get the biased exponent similarly */ #define GETEXP … /* Get the unbiased exponent similarly */ #define GETEXPUN … /* Get the MSD similarly (as uInt) */ #define GETMSD … /* Compile-time computes of the exponent continuation field masks */ /* full exponent continuation field: */ #define ECONMASK … /* same, not including its first digit (the qNaN/sNaN selector): */ #define ECONNANMASK … /* Macros to decode the coefficient in a finite decFloat *df into */ /* a BCD string (uByte *bcdin) of length DECPMAX uBytes. */ /* In-line sequence to convert least significant 10 bits of uInt */ /* dpd to three BCD8 digits starting at uByte u. Note that an */ /* extra byte is written to the right of the three digits because */ /* four bytes are moved at a time for speed; the alternative */ /* macro moves exactly three bytes (usually slower). */ #define dpd2bcd8 … #define dpd2bcd83 … /* Decode the declets. After extracting each one, it is decoded */ /* to BCD8 using a table lookup (also used for variable-length */ /* decode). Each DPD decode is 3 bytes BCD8 plus a one-byte */ /* length which is not used, here). Fixed-length 4-byte moves */ /* are fast, however, almost everywhere, and so are used except */ /* for the final three bytes (to avoid overrun). The code below */ /* is 36 instructions for Doubles and about 70 for Quads, even */ /* on IA32. */ /* Two macros are defined for each format: */ /* GETCOEFF extracts the coefficient of the current format */ /* GETWCOEFF extracts the coefficient of the next-wider format. */ /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */ #if DECPMAX==7 #define GETCOEFF … #define GETWCOEFF … #elif DECPMAX==16 #define GETCOEFF … #define GETWCOEFF … #elif DECPMAX==34 #define GETCOEFF … #define GETWCOEFF … #endif /* Macros to decode the coefficient in a finite decFloat *df into */ /* a base-billion uInt array, with the least-significant */ /* 0-999999999 'digit' at offset 0. */ /* Decode the declets. After extracting each one, it is decoded */ /* to binary using a table lookup. Three tables are used; one */ /* the usual DPD to binary, the other two pre-multiplied by 1000 */ /* and 1000000 to avoid multiplication during decode. These */ /* tables can also be used for multiplying up the MSD as the DPD */ /* code for 0 through 9 is the identity. */ #define DPD2BIN0 … #if DECPMAX==7 #define GETCOEFFBILL … #elif DECPMAX==16 #define GETCOEFFBILL … #elif DECPMAX==34 #define GETCOEFFBILL … #endif /* Macros to decode the coefficient in a finite decFloat *df into */ /* a base-thousand uInt array (of size DECLETS+1, to allow for */ /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/ /* Decode the declets. After extracting each one, it is decoded */ /* to binary using a table lookup. */ #if DECPMAX==7 #define GETCOEFFTHOU … #elif DECPMAX==16 #define GETCOEFFTHOU … #elif DECPMAX==34 #define GETCOEFFTHOU … #endif /* Macros to decode the coefficient in a finite decFloat *df and */ /* add to a base-thousand uInt array (as for GETCOEFFTHOU). */ /* After the addition then most significant 'digit' in the array */ /* might have a value larger then 10 (with a maximum of 19). */ #if DECPMAX==7 #define ADDCOEFFTHOU … #elif DECPMAX==16 #define ADDCOEFFTHOU … #elif DECPMAX==34 #define ADDCOEFFTHOU … #endif /* Set a decFloat to the maximum positive finite number (Nmax) */ #if DECPMAX==7 #define DFSETNMAX … #elif DECPMAX==16 #define DFSETNMAX … #elif DECPMAX==34 #define DFSETNMAX … #endif /* [end of format-dependent macros and constants] */ #endif #else #error decNumberLocal included more than once #endif