Home | History | Annotate | Line # | Download | only in libdecnumber
decNumberLocal.h revision 1.1.1.1
      1  1.1  christos /* Local definitions for the decNumber C Library.
      2  1.1  christos    Copyright (C) 2007-2013 Free Software Foundation, Inc.
      3  1.1  christos    Contributed by IBM Corporation.  Author Mike Cowlishaw.
      4  1.1  christos 
      5  1.1  christos    This file is part of GCC.
      6  1.1  christos 
      7  1.1  christos    GCC is free software; you can redistribute it and/or modify it under
      8  1.1  christos    the terms of the GNU General Public License as published by the Free
      9  1.1  christos    Software Foundation; either version 3, or (at your option) any later
     10  1.1  christos    version.
     11  1.1  christos 
     12  1.1  christos    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     13  1.1  christos    WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  1.1  christos    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  1.1  christos    for more details.
     16  1.1  christos 
     17  1.1  christos Under Section 7 of GPL version 3, you are granted additional
     18  1.1  christos permissions described in the GCC Runtime Library Exception, version
     19  1.1  christos 3.1, as published by the Free Software Foundation.
     20  1.1  christos 
     21  1.1  christos You should have received a copy of the GNU General Public License and
     22  1.1  christos a copy of the GCC Runtime Library Exception along with this program;
     23  1.1  christos see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24  1.1  christos <http://www.gnu.org/licenses/>.  */
     25  1.1  christos 
     26  1.1  christos /* ------------------------------------------------------------------ */
     27  1.1  christos /* decNumber package local type, tuning, and macro definitions	      */
     28  1.1  christos /* ------------------------------------------------------------------ */
     29  1.1  christos /* This header file is included by all modules in the decNumber       */
     30  1.1  christos /* library, and contains local type definitions, tuning parameters,   */
     31  1.1  christos /* etc.  It should not need to be used by application programs.       */
     32  1.1  christos /* decNumber.h or one of decDouble (etc.) must be included first.     */
     33  1.1  christos /* ------------------------------------------------------------------ */
     34  1.1  christos 
     35  1.1  christos #if !defined(DECNUMBERLOC)
     36  1.1  christos   #define DECNUMBERLOC
     37  1.1  christos   #define DECVERSION	"decNumber 3.61" /* Package Version [16 max.] */
     38  1.1  christos   #define DECNLAUTHOR	"Mike Cowlishaw"	      /* Who to blame */
     39  1.1  christos 
     40  1.1  christos   #include <stdlib.h>	      /* for abs			      */
     41  1.1  christos   #include <string.h>	      /* for memset, strcpy		      */
     42  1.1  christos   #include "dconfig.h"        /* for WORDS_BIGENDIAN		      */
     43  1.1  christos 
     44  1.1  christos   /* Conditional code flag -- set this to match hardware platform     */
     45  1.1  christos   /* 1=little-endian, 0=big-endian                                   */
     46  1.1  christos   #if WORDS_BIGENDIAN
     47  1.1  christos   #define DECLITEND 0
     48  1.1  christos   #else
     49  1.1  christos   #define DECLITEND 1
     50  1.1  christos   #endif
     51  1.1  christos 
     52  1.1  christos   #if !defined(DECLITEND)
     53  1.1  christos   #define DECLITEND 1	      /* 1=little-endian, 0=big-endian	      */
     54  1.1  christos   #endif
     55  1.1  christos 
     56  1.1  christos   /* Conditional code flag -- set this to 1 for best performance      */
     57  1.1  christos   #if !defined(DECUSE64)
     58  1.1  christos   #define DECUSE64  1	      /* 1=use int64s, 0=int32 & smaller only */
     59  1.1  christos   #endif
     60  1.1  christos 
     61  1.1  christos   /* Conditional check flags -- set these to 0 for best performance   */
     62  1.1  christos   #if !defined(DECCHECK)
     63  1.1  christos   #define DECCHECK  0	      /* 1 to enable robust checking	      */
     64  1.1  christos   #endif
     65  1.1  christos   #if !defined(DECALLOC)
     66  1.1  christos   #define DECALLOC  0	      /* 1 to enable memory accounting	      */
     67  1.1  christos   #endif
     68  1.1  christos   #if !defined(DECTRACE)
     69  1.1  christos   #define DECTRACE  0	      /* 1 to trace certain internals, etc.   */
     70  1.1  christos   #endif
     71  1.1  christos 
     72  1.1  christos   /* Tuning parameter for decNumber (arbitrary precision) module      */
     73  1.1  christos   #if !defined(DECBUFFER)
     74  1.1  christos   #define DECBUFFER 36	      /* Size basis for local buffers.	This  */
     75  1.1  christos 			      /* should be a common maximum precision */
     76  1.1  christos 			      /* rounded up to a multiple of 4; must  */
     77  1.1  christos 			      /* be zero or positive.		      */
     78  1.1  christos   #endif
     79  1.1  christos 
     80  1.1  christos   /* ---------------------------------------------------------------- */
     81  1.1  christos   /* Definitions for all modules (general-purpose)		      */
     82  1.1  christos   /* ---------------------------------------------------------------- */
     83  1.1  christos 
     84  1.1  christos   /* Local names for common types -- for safety, decNumber modules do */
     85  1.1  christos   /* not use int or long directly.				      */
     86  1.1  christos   #define Flag	 uint8_t
     87  1.1  christos   #define Byte	 int8_t
     88  1.1  christos   #define uByte  uint8_t
     89  1.1  christos   #define Short  int16_t
     90  1.1  christos   #define uShort uint16_t
     91  1.1  christos   #define Int	 int32_t
     92  1.1  christos   #define uInt	 uint32_t
     93  1.1  christos   #define Unit	 decNumberUnit
     94  1.1  christos   #if DECUSE64
     95  1.1  christos   #define Long	 int64_t
     96  1.1  christos   #define uLong  uint64_t
     97  1.1  christos   #endif
     98  1.1  christos 
     99  1.1  christos   /* Development-use definitions				      */
    100  1.1  christos   typedef long int LI;	      /* for printf arguments only	      */
    101  1.1  christos   #define DECNOINT  0	      /* 1 to check no internal use of 'int'  */
    102  1.1  christos 			      /*   or stdint types		      */
    103  1.1  christos   #if DECNOINT
    104  1.1  christos     /* if these interfere with your C includes, do not set DECNOINT   */
    105  1.1  christos     #define int     ?	      /* enable to ensure that plain C 'int'  */
    106  1.1  christos     #define long    ??	      /* .. or 'long' types are not used      */
    107  1.1  christos   #endif
    108  1.1  christos 
    109  1.1  christos   /* Shared lookup tables					      */
    110  1.1  christos   extern const uByte  DECSTICKYTAB[10]; /* re-round digits if sticky  */
    111  1.1  christos   extern const uInt   DECPOWERS[10];	/* powers of ten table	      */
    112  1.1  christos   /* The following are included from decDPD.h			      */
    113  1.1  christos   #include "decDPDSymbols.h"
    114  1.1  christos   extern const uShort DPD2BIN[1024];	/* DPD -> 0-999 	      */
    115  1.1  christos   extern const uShort BIN2DPD[1000];	/* 0-999 -> DPD 	      */
    116  1.1  christos   extern const uInt   DPD2BINK[1024];	/* DPD -> 0-999000	      */
    117  1.1  christos   extern const uInt   DPD2BINM[1024];	/* DPD -> 0-999000000	      */
    118  1.1  christos   extern const uByte  DPD2BCD8[4096];	/* DPD -> ddd + len	      */
    119  1.1  christos   extern const uByte  BIN2BCD8[4000];	/* 0-999 -> ddd + len	      */
    120  1.1  christos   extern const uShort BCD2DPD[2458];	/* 0-0x999 -> DPD (0x999=2457)*/
    121  1.1  christos 
    122  1.1  christos   /* LONGMUL32HI -- set w=(u*v)>>32, where w, u, and v are uInts      */
    123  1.1  christos   /* (that is, sets w to be the high-order word of the 64-bit result; */
    124  1.1  christos   /* the low-order word is simply u*v.) 			      */
    125  1.1  christos   /* This version is derived from Knuth via Hacker's Delight;	      */
    126  1.1  christos   /* it seems to optimize better than some others tried 	      */
    127  1.1  christos   #define LONGMUL32HI(w, u, v) {	     \
    128  1.1  christos     uInt u0, u1, v0, v1, w0, w1, w2, t;      \
    129  1.1  christos     u0=u & 0xffff; u1=u>>16;		     \
    130  1.1  christos     v0=v & 0xffff; v1=v>>16;		     \
    131  1.1  christos     w0=u0*v0;				     \
    132  1.1  christos     t=u1*v0 + (w0>>16); 		     \
    133  1.1  christos     w1=t & 0xffff; w2=t>>16;		     \
    134  1.1  christos     w1=u0*v1 + w1;			     \
    135  1.1  christos     (w)=u1*v1 + w2 + (w1>>16);}
    136  1.1  christos 
    137  1.1  christos   /* ROUNDUP -- round an integer up to a multiple of n		      */
    138  1.1  christos   #define ROUNDUP(i, n) ((((i)+(n)-1)/n)*n)
    139  1.1  christos   #define ROUNDUP4(i)	(((i)+3)&~3)	/* special for n=4	      */
    140  1.1  christos 
    141  1.1  christos   /* ROUNDDOWN -- round an integer down to a multiple of n	      */
    142  1.1  christos   #define ROUNDDOWN(i, n) (((i)/n)*n)
    143  1.1  christos   #define ROUNDDOWN4(i)   ((i)&~3)	/* special for n=4	      */
    144  1.1  christos 
    145  1.1  christos   /* References to multi-byte sequences under different sizes; these  */
    146  1.1  christos   /* require locally declared variables, but do not violate strict    */
    147  1.1  christos   /* aliasing or alignment (as did the UINTAT simple cast to uInt).   */
    148  1.1  christos   /* Variables needed are uswork, uiwork, etc. [so do not use at same */
    149  1.1  christos   /* level in an expression, e.g., UBTOUI(x)==UBTOUI(y) may fail].    */
    150  1.1  christos 
    151  1.1  christos   /* Return a uInt, etc., from bytes starting at a char* or uByte*    */
    152  1.1  christos   #define UBTOUS(b)  (memcpy((void *)&uswork, b, 2), uswork)
    153  1.1  christos   #define UBTOUI(b)  (memcpy((void *)&uiwork, b, 4), uiwork)
    154  1.1  christos 
    155  1.1  christos   /* Store a uInt, etc., into bytes starting at a char* or uByte*.    */
    156  1.1  christos   /* Returns i, evaluated, for convenience; has to use uiwork because */
    157  1.1  christos   /* i may be an expression.					      */
    158  1.1  christos   #define UBFROMUS(b, i)  (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork)
    159  1.1  christos   #define UBFROMUI(b, i)  (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork)
    160  1.1  christos 
    161  1.1  christos   /* X10 and X100 -- multiply integer i by 10 or 100		      */
    162  1.1  christos   /* [shifts are usually faster than multiply; could be conditional]  */
    163  1.1  christos   #define X10(i)  (((i)<<1)+((i)<<3))
    164  1.1  christos   #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))
    165  1.1  christos 
    166  1.1  christos   /* MAXI and MINI -- general max & min (not in ANSI) for integers    */
    167  1.1  christos   #define MAXI(x,y) ((x)<(y)?(y):(x))
    168  1.1  christos   #define MINI(x,y) ((x)>(y)?(y):(x))
    169  1.1  christos 
    170  1.1  christos   /* Useful constants						      */
    171  1.1  christos   #define BILLION      1000000000	     /* 10**9		      */
    172  1.1  christos   /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC       */
    173  1.1  christos   #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0')
    174  1.1  christos 
    175  1.1  christos 
    176  1.1  christos   /* ---------------------------------------------------------------- */
    177  1.1  christos   /* Definitions for arbitary-precision modules (only valid after     */
    178  1.1  christos   /* decNumber.h has been included)				      */
    179  1.1  christos   /* ---------------------------------------------------------------- */
    180  1.1  christos 
    181  1.1  christos   /* Limits and constants					      */
    182  1.1  christos   #define DECNUMMAXP 999999999	/* maximum precision code can handle  */
    183  1.1  christos   #define DECNUMMAXE 999999999	/* maximum adjusted exponent ditto    */
    184  1.1  christos   #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto    */
    185  1.1  christos   #if (DECNUMMAXP != DEC_MAX_DIGITS)
    186  1.1  christos     #error Maximum digits mismatch
    187  1.1  christos   #endif
    188  1.1  christos   #if (DECNUMMAXE != DEC_MAX_EMAX)
    189  1.1  christos     #error Maximum exponent mismatch
    190  1.1  christos   #endif
    191  1.1  christos   #if (DECNUMMINE != DEC_MIN_EMIN)
    192  1.1  christos     #error Minimum exponent mismatch
    193  1.1  christos   #endif
    194  1.1  christos 
    195  1.1  christos   /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN       */
    196  1.1  christos   /* digits, and D2UTABLE -- the initializer for the D2U table	      */
    197  1.1  christos   #if	DECDPUN==1
    198  1.1  christos     #define DECDPUNMAX 9
    199  1.1  christos     #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,  \
    200  1.1  christos 		      18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
    201  1.1  christos 		      33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
    202  1.1  christos 		      48,49}
    203  1.1  christos   #elif DECDPUN==2
    204  1.1  christos     #define DECDPUNMAX 99
    205  1.1  christos     #define D2UTABLE {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,  \
    206  1.1  christos 		      11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \
    207  1.1  christos 		      18,19,19,20,20,21,21,22,22,23,23,24,24,25}
    208  1.1  christos   #elif DECDPUN==3
    209  1.1  christos     #define DECDPUNMAX 999
    210  1.1  christos     #define D2UTABLE {0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,  \
    211  1.1  christos 		      8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \
    212  1.1  christos 		      13,14,14,14,15,15,15,16,16,16,17}
    213  1.1  christos   #elif DECDPUN==4
    214  1.1  christos     #define DECDPUNMAX 9999
    215  1.1  christos     #define D2UTABLE {0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,  \
    216  1.1  christos 		      6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \
    217  1.1  christos 		      11,11,11,12,12,12,12,13}
    218  1.1  christos   #elif DECDPUN==5
    219  1.1  christos     #define DECDPUNMAX 99999
    220  1.1  christos     #define D2UTABLE {0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,  \
    221  1.1  christos 		      5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,  \
    222  1.1  christos 		      9,9,10,10,10,10}
    223  1.1  christos   #elif DECDPUN==6
    224  1.1  christos     #define DECDPUNMAX 999999
    225  1.1  christos     #define D2UTABLE {0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,  \
    226  1.1  christos 		      4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8,  \
    227  1.1  christos 		      8,8,8,8,8,9}
    228  1.1  christos   #elif DECDPUN==7
    229  1.1  christos     #define DECDPUNMAX 9999999
    230  1.1  christos     #define D2UTABLE {0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,  \
    231  1.1  christos 		      4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7,  \
    232  1.1  christos 		      7,7,7,7,7,7}
    233  1.1  christos   #elif DECDPUN==8
    234  1.1  christos     #define DECDPUNMAX 99999999
    235  1.1  christos     #define D2UTABLE {0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,  \
    236  1.1  christos 		      3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,  \
    237  1.1  christos 		      6,6,6,6,6,7}
    238  1.1  christos   #elif DECDPUN==9
    239  1.1  christos     #define DECDPUNMAX 999999999
    240  1.1  christos     #define D2UTABLE {0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,  \
    241  1.1  christos 		      3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,  \
    242  1.1  christos 		      5,5,6,6,6,6}
    243  1.1  christos   #elif defined(DECDPUN)
    244  1.1  christos     #error DECDPUN must be in the range 1-9
    245  1.1  christos   #endif
    246  1.1  christos 
    247  1.1  christos   /* ----- Shared data (in decNumber.c) ----- */
    248  1.1  christos   /* Public lookup table used by the D2U macro (see below)	      */
    249  1.1  christos   #define DECMAXD2U 49
    250  1.1  christos   extern const uByte d2utable[DECMAXD2U+1];
    251  1.1  christos 
    252  1.1  christos   /* ----- Macros ----- */
    253  1.1  christos   /* ISZERO -- return true if decNumber dn is a zero		      */
    254  1.1  christos   /* [performance-critical in some situations]			      */
    255  1.1  christos   #define ISZERO(dn) decNumberIsZero(dn)     /* now just a local name */
    256  1.1  christos 
    257  1.1  christos   /* D2U -- return the number of Units needed to hold d digits	      */
    258  1.1  christos   /* (runtime version, with table lookaside for small d)	      */
    259  1.1  christos   #if DECDPUN==8
    260  1.1  christos     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
    261  1.1  christos   #elif DECDPUN==4
    262  1.1  christos     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
    263  1.1  christos   #else
    264  1.1  christos     #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
    265  1.1  christos   #endif
    266  1.1  christos   /* SD2U -- static D2U macro (for compile-time calculation)	      */
    267  1.1  christos   #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)
    268  1.1  christos 
    269  1.1  christos   /* MSUDIGITS -- returns digits in msu, from digits, calculated      */
    270  1.1  christos   /* using D2U							      */
    271  1.1  christos   #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)
    272  1.1  christos 
    273  1.1  christos   /* D2N -- return the number of decNumber structs that would be      */
    274  1.1  christos   /* needed to contain that number of digits (and the initial	      */
    275  1.1  christos   /* decNumber struct) safely.	Note that one Unit is included in the */
    276  1.1  christos   /* initial structure.  Used for allocating space that is aligned on */
    277  1.1  christos   /* a decNumber struct boundary. */
    278  1.1  christos   #define D2N(d) \
    279  1.1  christos     ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))
    280  1.1  christos 
    281  1.1  christos   /* TODIGIT -- macro to remove the leading digit from the unsigned   */
    282  1.1  christos   /* integer u at column cut (counting from the right, LSD=0) and     */
    283  1.1  christos   /* place it as an ASCII character into the character pointed to by  */
    284  1.1  christos   /* c.  Note that cut must be <= 9, and the maximum value for u is   */
    285  1.1  christos   /* 2,000,000,000 (as is needed for negative exponents of	      */
    286  1.1  christos   /* subnormals).  The unsigned integer pow is used as a temporary    */
    287  1.1  christos   /* variable. */
    288  1.1  christos   #define TODIGIT(u, cut, c, pow) {	  \
    289  1.1  christos     *(c)='0';				  \
    290  1.1  christos     pow=DECPOWERS[cut]*2;		  \
    291  1.1  christos     if ((u)>pow) {			  \
    292  1.1  christos       pow*=4;				  \
    293  1.1  christos       if ((u)>=pow) {(u)-=pow; *(c)+=8;}  \
    294  1.1  christos       pow/=2;				  \
    295  1.1  christos       if ((u)>=pow) {(u)-=pow; *(c)+=4;}  \
    296  1.1  christos       pow/=2;				  \
    297  1.1  christos       } 				  \
    298  1.1  christos     if ((u)>=pow) {(u)-=pow; *(c)+=2;}	  \
    299  1.1  christos     pow/=2;				  \
    300  1.1  christos     if ((u)>=pow) {(u)-=pow; *(c)+=1;}	  \
    301  1.1  christos     }
    302  1.1  christos 
    303  1.1  christos   /* ---------------------------------------------------------------- */
    304  1.1  christos   /* Definitions for fixed-precision modules (only valid after	      */
    305  1.1  christos   /* decSingle.h, decDouble.h, or decQuad.h has been included)	      */
    306  1.1  christos   /* ---------------------------------------------------------------- */
    307  1.1  christos 
    308  1.1  christos   /* bcdnum -- a structure describing a format-independent finite     */
    309  1.1  christos   /* number, whose coefficient is a string of bcd8 uBytes	      */
    310  1.1  christos   typedef struct {
    311  1.1  christos     uByte   *msd;	      /* -> most significant digit	      */
    312  1.1  christos     uByte   *lsd;	      /* -> least ditto 		      */
    313  1.1  christos     uInt     sign;	      /* 0=positive, DECFLOAT_Sign=negative   */
    314  1.1  christos     Int      exponent;	      /* Unadjusted signed exponent (q), or   */
    315  1.1  christos 			      /* DECFLOAT_NaN etc. for a special      */
    316  1.1  christos     } bcdnum;
    317  1.1  christos 
    318  1.1  christos   /* Test if exponent or bcdnum exponent must be a special, etc.      */
    319  1.1  christos   #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp)
    320  1.1  christos   #define EXPISINF(exp) (exp==DECFLOAT_Inf)
    321  1.1  christos   #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN)
    322  1.1  christos   #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent))
    323  1.1  christos 
    324  1.1  christos   /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian  */
    325  1.1  christos   /* (array) notation (the 0 word or byte contains the sign bit),     */
    326  1.1  christos   /* automatically adjusting for endianness; similarly address a word */
    327  1.1  christos   /* in the next-wider format (decFloatWider, or dfw)		      */
    328  1.1  christos   #define DECWORDS  (DECBYTES/4)
    329  1.1  christos   #define DECWWORDS (DECWBYTES/4)
    330  1.1  christos   #if DECLITEND
    331  1.1  christos     #define DFBYTE(df, off)   ((df)->bytes[DECBYTES-1-(off)])
    332  1.1  christos     #define DFWORD(df, off)   ((df)->words[DECWORDS-1-(off)])
    333  1.1  christos     #define DFWWORD(dfw, off) ((dfw)->words[DECWWORDS-1-(off)])
    334  1.1  christos   #else
    335  1.1  christos     #define DFBYTE(df, off)   ((df)->bytes[off])
    336  1.1  christos     #define DFWORD(df, off)   ((df)->words[off])
    337  1.1  christos     #define DFWWORD(dfw, off) ((dfw)->words[off])
    338  1.1  christos   #endif
    339  1.1  christos 
    340  1.1  christos   /* Tests for sign or specials, directly on DECFLOATs		      */
    341  1.1  christos   #define DFISSIGNED(df)   (DFWORD(df, 0)&0x80000000)
    342  1.1  christos   #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000)
    343  1.1  christos   #define DFISINF(df)	  ((DFWORD(df, 0)&0x7c000000)==0x78000000)
    344  1.1  christos   #define DFISNAN(df)	  ((DFWORD(df, 0)&0x7c000000)==0x7c000000)
    345  1.1  christos   #define DFISQNAN(df)	  ((DFWORD(df, 0)&0x7e000000)==0x7c000000)
    346  1.1  christos   #define DFISSNAN(df)	  ((DFWORD(df, 0)&0x7e000000)==0x7e000000)
    347  1.1  christos 
    348  1.1  christos   /* Shared lookup tables					      */
    349  1.1  christos #include "decCommonSymbols.h"
    350  1.1  christos   extern const uInt   DECCOMBMSD[64];	/* Combination field -> MSD   */
    351  1.1  christos   extern const uInt   DECCOMBFROM[48];	/* exp+msd -> Combination     */
    352  1.1  christos 
    353  1.1  christos   /* Private generic (utility) routine				      */
    354  1.1  christos   #if DECCHECK || DECTRACE
    355  1.1  christos     extern void decShowNum(const bcdnum *, const char *);
    356  1.1  christos   #endif
    357  1.1  christos 
    358  1.1  christos   /* Format-dependent macros and constants			      */
    359  1.1  christos   #if defined(DECPMAX)
    360  1.1  christos 
    361  1.1  christos     /* Useful constants 					      */
    362  1.1  christos     #define DECPMAX9  (ROUNDUP(DECPMAX, 9)/9)  /* 'Pmax' in 10**9s    */
    363  1.1  christos     /* Top words for a zero					      */
    364  1.1  christos     #define SINGLEZERO	 0x22500000
    365  1.1  christos     #define DOUBLEZERO	 0x22380000
    366  1.1  christos     #define QUADZERO	 0x22080000
    367  1.1  christos     /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */
    368  1.1  christos 
    369  1.1  christos     /* Format-dependent common tests:				      */
    370  1.1  christos     /*	 DFISZERO   -- test for (any) zero			      */
    371  1.1  christos     /*	 DFISCCZERO -- test for coefficient continuation being zero   */
    372  1.1  christos     /*	 DFISCC01   -- test for coefficient contains only 0s and 1s   */
    373  1.1  christos     /*	 DFISINT    -- test for finite and exponent q=0 	      */
    374  1.1  christos     /*	 DFISUINT01 -- test for sign=0, finite, exponent q=0, and     */
    375  1.1  christos     /*		       MSD=0 or 1				      */
    376  1.1  christos     /*	 ZEROWORD is also defined here. 			      */
    377  1.1  christos     /* In DFISZERO the first test checks the least-significant word   */
    378  1.1  christos     /* (most likely to be non-zero); the penultimate tests MSD and    */
    379  1.1  christos     /* DPDs in the signword, and the final test excludes specials and */
    380  1.1  christos     /* MSD>7.  DFISINT similarly has to allow for the two forms of    */
    381  1.1  christos     /* MSD codes.  DFISUINT01 only has to allow for one form of MSD   */
    382  1.1  christos     /* code.							      */
    383  1.1  christos     #if DECPMAX==7
    384  1.1  christos       #define ZEROWORD SINGLEZERO
    385  1.1  christos       /* [test macros not needed except for Zero]		      */
    386  1.1  christos       #define DFISZERO(df)  ((DFWORD(df, 0)&0x1c0fffff)==0	   \
    387  1.1  christos 			  && (DFWORD(df, 0)&0x60000000)!=0x60000000)
    388  1.1  christos     #elif DECPMAX==16
    389  1.1  christos       #define ZEROWORD DOUBLEZERO
    390  1.1  christos       #define DFISZERO(df)  ((DFWORD(df, 1)==0			   \
    391  1.1  christos 			  && (DFWORD(df, 0)&0x1c03ffff)==0	   \
    392  1.1  christos 			  && (DFWORD(df, 0)&0x60000000)!=0x60000000))
    393  1.1  christos       #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000  \
    394  1.1  christos 			 ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000)
    395  1.1  christos       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000)
    396  1.1  christos       #define DFISCCZERO(df) (DFWORD(df, 1)==0			   \
    397  1.1  christos 			  && (DFWORD(df, 0)&0x0003ffff)==0)
    398  1.1  christos       #define DFISCC01(df)  ((DFWORD(df, 0)&~0xfffc9124)==0	   \
    399  1.1  christos 			  && (DFWORD(df, 1)&~0x49124491)==0)
    400  1.1  christos     #elif DECPMAX==34
    401  1.1  christos       #define ZEROWORD QUADZERO
    402  1.1  christos       #define DFISZERO(df)  ((DFWORD(df, 3)==0			   \
    403  1.1  christos 			  &&  DFWORD(df, 2)==0			   \
    404  1.1  christos 			  &&  DFWORD(df, 1)==0			   \
    405  1.1  christos 			  && (DFWORD(df, 0)&0x1c003fff)==0	   \
    406  1.1  christos 			  && (DFWORD(df, 0)&0x60000000)!=0x60000000))
    407  1.1  christos       #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000  \
    408  1.1  christos 			 ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000)
    409  1.1  christos       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000)
    410  1.1  christos       #define DFISCCZERO(df) (DFWORD(df, 3)==0			   \
    411  1.1  christos 			  &&  DFWORD(df, 2)==0			   \
    412  1.1  christos 			  &&  DFWORD(df, 1)==0			   \
    413  1.1  christos 			  && (DFWORD(df, 0)&0x00003fff)==0)
    414  1.1  christos 
    415  1.1  christos       #define DFISCC01(df)   ((DFWORD(df, 0)&~0xffffc912)==0	   \
    416  1.1  christos 			  &&  (DFWORD(df, 1)&~0x44912449)==0	   \
    417  1.1  christos 			  &&  (DFWORD(df, 2)&~0x12449124)==0	   \
    418  1.1  christos 			  &&  (DFWORD(df, 3)&~0x49124491)==0)
    419  1.1  christos     #endif
    420  1.1  christos 
    421  1.1  christos     /* Macros to test if a certain 10 bits of a uInt or pair of uInts */
    422  1.1  christos     /* are a canonical declet [higher or lower bits are ignored].     */
    423  1.1  christos     /* declet is at offset 0 (from the right) in a uInt:	      */
    424  1.1  christos     #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e)
    425  1.1  christos     /* declet is at offset k (a multiple of 2) in a uInt:	      */
    426  1.1  christos     #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0	    \
    427  1.1  christos       || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
    428  1.1  christos     /* declet is at offset k (a multiple of 2) in a pair of uInts:    */
    429  1.1  christos     /* [the top 2 bits will always be in the more-significant uInt]   */
    430  1.1  christos     #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0     \
    431  1.1  christos       || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k)))		    \
    432  1.1  christos       || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
    433  1.1  christos 
    434  1.1  christos     /* Macro to test whether a full-length (length DECPMAX) BCD8      */
    435  1.1  christos     /* coefficient, starting at uByte u, is all zeros		      */
    436  1.1  christos     /* Test just the LSWord first, then the remainder as a sequence   */
    437  1.1  christos     /* of tests in order to avoid same-level use of UBTOUI	      */
    438  1.1  christos     #if DECPMAX==7
    439  1.1  christos       #define ISCOEFFZERO(u) (					    \
    440  1.1  christos 	   UBTOUI((u)+DECPMAX-4)==0				    \
    441  1.1  christos 	&& UBTOUS((u)+DECPMAX-6)==0				    \
    442  1.1  christos 	&& *(u)==0)
    443  1.1  christos     #elif DECPMAX==16
    444  1.1  christos       #define ISCOEFFZERO(u) (					    \
    445  1.1  christos 	   UBTOUI((u)+DECPMAX-4)==0				    \
    446  1.1  christos 	&& UBTOUI((u)+DECPMAX-8)==0				    \
    447  1.1  christos 	&& UBTOUI((u)+DECPMAX-12)==0				    \
    448  1.1  christos 	&& UBTOUI(u)==0)
    449  1.1  christos     #elif DECPMAX==34
    450  1.1  christos       #define ISCOEFFZERO(u) (					    \
    451  1.1  christos 	   UBTOUI((u)+DECPMAX-4)==0				    \
    452  1.1  christos 	&& UBTOUI((u)+DECPMAX-8)==0				    \
    453  1.1  christos 	&& UBTOUI((u)+DECPMAX-12)==0				    \
    454  1.1  christos 	&& UBTOUI((u)+DECPMAX-16)==0				    \
    455  1.1  christos 	&& UBTOUI((u)+DECPMAX-20)==0				    \
    456  1.1  christos 	&& UBTOUI((u)+DECPMAX-24)==0				    \
    457  1.1  christos 	&& UBTOUI((u)+DECPMAX-28)==0				    \
    458  1.1  christos 	&& UBTOUI((u)+DECPMAX-32)==0				    \
    459  1.1  christos 	&& UBTOUS(u)==0)
    460  1.1  christos     #endif
    461  1.1  christos 
    462  1.1  christos     /* Macros and masks for the exponent continuation field and MSD   */
    463  1.1  christos     /* Get the exponent continuation from a decFloat *df as an Int    */
    464  1.1  christos     #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL)))
    465  1.1  christos     /* Ditto, from the next-wider format			      */
    466  1.1  christos     #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL)))
    467  1.1  christos     /* Get the biased exponent similarly			      */
    468  1.1  christos     #define GETEXP(df)	((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df)))
    469  1.1  christos     /* Get the unbiased exponent similarly			      */
    470  1.1  christos     #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS)
    471  1.1  christos     /* Get the MSD similarly (as uInt)				      */
    472  1.1  christos     #define GETMSD(df)	 (DECCOMBMSD[DFWORD((df), 0)>>26])
    473  1.1  christos 
    474  1.1  christos     /* Compile-time computes of the exponent continuation field masks */
    475  1.1  christos     /* full exponent continuation field:			      */
    476  1.1  christos     #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
    477  1.1  christos     /* same, not including its first digit (the qNaN/sNaN selector):  */
    478  1.1  christos     #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
    479  1.1  christos 
    480  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df into */
    481  1.1  christos     /* a BCD string (uByte *bcdin) of length DECPMAX uBytes.	      */
    482  1.1  christos 
    483  1.1  christos     /* In-line sequence to convert least significant 10 bits of uInt  */
    484  1.1  christos     /* dpd to three BCD8 digits starting at uByte u.  Note that an    */
    485  1.1  christos     /* extra byte is written to the right of the three digits because */
    486  1.1  christos     /* four bytes are moved at a time for speed; the alternative      */
    487  1.1  christos     /* macro moves exactly three bytes (usually slower).	      */
    488  1.1  christos     #define dpd2bcd8(u, dpd)  memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 4)
    489  1.1  christos     #define dpd2bcd83(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 3)
    490  1.1  christos 
    491  1.1  christos     /* Decode the declets.  After extracting each one, it is decoded  */
    492  1.1  christos     /* to BCD8 using a table lookup (also used for variable-length    */
    493  1.1  christos     /* decode).  Each DPD decode is 3 bytes BCD8 plus a one-byte      */
    494  1.1  christos     /* length which is not used, here).  Fixed-length 4-byte moves    */
    495  1.1  christos     /* are fast, however, almost everywhere, and so are used except   */
    496  1.1  christos     /* for the final three bytes (to avoid overrun).  The code below  */
    497  1.1  christos     /* is 36 instructions for Doubles and about 70 for Quads, even    */
    498  1.1  christos     /* on IA32. 						      */
    499  1.1  christos 
    500  1.1  christos     /* Two macros are defined for each format:			      */
    501  1.1  christos     /*	 GETCOEFF extracts the coefficient of the current format      */
    502  1.1  christos     /*	 GETWCOEFF extracts the coefficient of the next-wider format. */
    503  1.1  christos     /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */
    504  1.1  christos 
    505  1.1  christos     #if DECPMAX==7
    506  1.1  christos     #define GETCOEFF(df, bcd) { 			 \
    507  1.1  christos       uInt sourhi=DFWORD(df, 0);			 \
    508  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    509  1.1  christos       dpd2bcd8(bcd+1, sourhi>>10);			 \
    510  1.1  christos       dpd2bcd83(bcd+4, sourhi);}
    511  1.1  christos     #define GETWCOEFF(df, bcd) {			 \
    512  1.1  christos       uInt sourhi=DFWWORD(df, 0);			 \
    513  1.1  christos       uInt sourlo=DFWWORD(df, 1);			 \
    514  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    515  1.1  christos       dpd2bcd8(bcd+1, sourhi>>8);			 \
    516  1.1  christos       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));	 \
    517  1.1  christos       dpd2bcd8(bcd+7, sourlo>>20);			 \
    518  1.1  christos       dpd2bcd8(bcd+10, sourlo>>10);			 \
    519  1.1  christos       dpd2bcd83(bcd+13, sourlo);}
    520  1.1  christos 
    521  1.1  christos     #elif DECPMAX==16
    522  1.1  christos     #define GETCOEFF(df, bcd) { 			 \
    523  1.1  christos       uInt sourhi=DFWORD(df, 0);			 \
    524  1.1  christos       uInt sourlo=DFWORD(df, 1);			 \
    525  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    526  1.1  christos       dpd2bcd8(bcd+1, sourhi>>8);			 \
    527  1.1  christos       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));	 \
    528  1.1  christos       dpd2bcd8(bcd+7, sourlo>>20);			 \
    529  1.1  christos       dpd2bcd8(bcd+10, sourlo>>10);			 \
    530  1.1  christos       dpd2bcd83(bcd+13, sourlo);}
    531  1.1  christos     #define GETWCOEFF(df, bcd) {			 \
    532  1.1  christos       uInt sourhi=DFWWORD(df, 0);			 \
    533  1.1  christos       uInt sourmh=DFWWORD(df, 1);			 \
    534  1.1  christos       uInt sourml=DFWWORD(df, 2);			 \
    535  1.1  christos       uInt sourlo=DFWWORD(df, 3);			 \
    536  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    537  1.1  christos       dpd2bcd8(bcd+1, sourhi>>4);			 \
    538  1.1  christos       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));	 \
    539  1.1  christos       dpd2bcd8(bcd+7, sourmh>>16);			 \
    540  1.1  christos       dpd2bcd8(bcd+10, sourmh>>6);			 \
    541  1.1  christos       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));	 \
    542  1.1  christos       dpd2bcd8(bcd+16, sourml>>18);			 \
    543  1.1  christos       dpd2bcd8(bcd+19, sourml>>8);			 \
    544  1.1  christos       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));	 \
    545  1.1  christos       dpd2bcd8(bcd+25, sourlo>>20);			 \
    546  1.1  christos       dpd2bcd8(bcd+28, sourlo>>10);			 \
    547  1.1  christos       dpd2bcd83(bcd+31, sourlo);}
    548  1.1  christos 
    549  1.1  christos     #elif DECPMAX==34
    550  1.1  christos     #define GETCOEFF(df, bcd) { 			 \
    551  1.1  christos       uInt sourhi=DFWORD(df, 0);			 \
    552  1.1  christos       uInt sourmh=DFWORD(df, 1);			 \
    553  1.1  christos       uInt sourml=DFWORD(df, 2);			 \
    554  1.1  christos       uInt sourlo=DFWORD(df, 3);			 \
    555  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    556  1.1  christos       dpd2bcd8(bcd+1, sourhi>>4);			 \
    557  1.1  christos       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));	 \
    558  1.1  christos       dpd2bcd8(bcd+7, sourmh>>16);			 \
    559  1.1  christos       dpd2bcd8(bcd+10, sourmh>>6);			 \
    560  1.1  christos       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));	 \
    561  1.1  christos       dpd2bcd8(bcd+16, sourml>>18);			 \
    562  1.1  christos       dpd2bcd8(bcd+19, sourml>>8);			 \
    563  1.1  christos       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));	 \
    564  1.1  christos       dpd2bcd8(bcd+25, sourlo>>20);			 \
    565  1.1  christos       dpd2bcd8(bcd+28, sourlo>>10);			 \
    566  1.1  christos       dpd2bcd83(bcd+31, sourlo);}
    567  1.1  christos 
    568  1.1  christos       #define GETWCOEFF(df, bcd) {??} /* [should never be used]       */
    569  1.1  christos     #endif
    570  1.1  christos 
    571  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df into */
    572  1.1  christos     /* a base-billion uInt array, with the least-significant	      */
    573  1.1  christos     /* 0-999999999 'digit' at offset 0. 			      */
    574  1.1  christos 
    575  1.1  christos     /* Decode the declets.  After extracting each one, it is decoded  */
    576  1.1  christos     /* to binary using a table lookup.	Three tables are used; one    */
    577  1.1  christos     /* the usual DPD to binary, the other two pre-multiplied by 1000  */
    578  1.1  christos     /* and 1000000 to avoid multiplication during decode.  These      */
    579  1.1  christos     /* tables can also be used for multiplying up the MSD as the DPD  */
    580  1.1  christos     /* code for 0 through 9 is the identity.			      */
    581  1.1  christos     #define DPD2BIN0 DPD2BIN	     /* for prettier code	      */
    582  1.1  christos 
    583  1.1  christos     #if DECPMAX==7
    584  1.1  christos     #define GETCOEFFBILL(df, buf) {			      \
    585  1.1  christos       uInt sourhi=DFWORD(df, 0);			      \
    586  1.1  christos       (buf)[0]=DPD2BIN0[sourhi&0x3ff]			      \
    587  1.1  christos 	      +DPD2BINK[(sourhi>>10)&0x3ff]		      \
    588  1.1  christos 	      +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    589  1.1  christos 
    590  1.1  christos     #elif DECPMAX==16
    591  1.1  christos     #define GETCOEFFBILL(df, buf) {			      \
    592  1.1  christos       uInt sourhi, sourlo;				      \
    593  1.1  christos       sourlo=DFWORD(df, 1);				      \
    594  1.1  christos       (buf)[0]=DPD2BIN0[sourlo&0x3ff]			      \
    595  1.1  christos 	      +DPD2BINK[(sourlo>>10)&0x3ff]		      \
    596  1.1  christos 	      +DPD2BINM[(sourlo>>20)&0x3ff];		      \
    597  1.1  christos       sourhi=DFWORD(df, 0);				      \
    598  1.1  christos       (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff]   \
    599  1.1  christos 	      +DPD2BINK[(sourhi>>8)&0x3ff]		      \
    600  1.1  christos 	      +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    601  1.1  christos 
    602  1.1  christos     #elif DECPMAX==34
    603  1.1  christos     #define GETCOEFFBILL(df, buf) {			      \
    604  1.1  christos       uInt sourhi, sourmh, sourml, sourlo;		      \
    605  1.1  christos       sourlo=DFWORD(df, 3);				      \
    606  1.1  christos       (buf)[0]=DPD2BIN0[sourlo&0x3ff]			      \
    607  1.1  christos 	      +DPD2BINK[(sourlo>>10)&0x3ff]		      \
    608  1.1  christos 	      +DPD2BINM[(sourlo>>20)&0x3ff];		      \
    609  1.1  christos       sourml=DFWORD(df, 2);				      \
    610  1.1  christos       (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff]   \
    611  1.1  christos 	      +DPD2BINK[(sourml>>8)&0x3ff]		      \
    612  1.1  christos 	      +DPD2BINM[(sourml>>18)&0x3ff];		      \
    613  1.1  christos       sourmh=DFWORD(df, 1);				      \
    614  1.1  christos       (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff]   \
    615  1.1  christos 	      +DPD2BINK[(sourmh>>6)&0x3ff]		      \
    616  1.1  christos 	      +DPD2BINM[(sourmh>>16)&0x3ff];		      \
    617  1.1  christos       sourhi=DFWORD(df, 0);				      \
    618  1.1  christos       (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff]   \
    619  1.1  christos 	      +DPD2BINK[(sourhi>>4)&0x3ff]		      \
    620  1.1  christos 	      +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    621  1.1  christos 
    622  1.1  christos     #endif
    623  1.1  christos 
    624  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df into */
    625  1.1  christos     /* a base-thousand uInt array (of size DECLETS+1, to allow for    */
    626  1.1  christos     /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/
    627  1.1  christos 
    628  1.1  christos     /* Decode the declets.  After extracting each one, it is decoded  */
    629  1.1  christos     /* to binary using a table lookup.				      */
    630  1.1  christos     #if DECPMAX==7
    631  1.1  christos     #define GETCOEFFTHOU(df, buf) {			      \
    632  1.1  christos       uInt sourhi=DFWORD(df, 0);			      \
    633  1.1  christos       (buf)[0]=DPD2BIN[sourhi&0x3ff];			      \
    634  1.1  christos       (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff];		      \
    635  1.1  christos       (buf)[2]=DECCOMBMSD[sourhi>>26];}
    636  1.1  christos 
    637  1.1  christos     #elif DECPMAX==16
    638  1.1  christos     #define GETCOEFFTHOU(df, buf) {			      \
    639  1.1  christos       uInt sourhi, sourlo;				      \
    640  1.1  christos       sourlo=DFWORD(df, 1);				      \
    641  1.1  christos       (buf)[0]=DPD2BIN[sourlo&0x3ff];			      \
    642  1.1  christos       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    643  1.1  christos       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    644  1.1  christos       sourhi=DFWORD(df, 0);				      \
    645  1.1  christos       (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];   \
    646  1.1  christos       (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff];		      \
    647  1.1  christos       (buf)[5]=DECCOMBMSD[sourhi>>26];}
    648  1.1  christos 
    649  1.1  christos     #elif DECPMAX==34
    650  1.1  christos     #define GETCOEFFTHOU(df, buf) {			      \
    651  1.1  christos       uInt sourhi, sourmh, sourml, sourlo;		      \
    652  1.1  christos       sourlo=DFWORD(df, 3);				      \
    653  1.1  christos       (buf)[0]=DPD2BIN[sourlo&0x3ff];			      \
    654  1.1  christos       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    655  1.1  christos       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    656  1.1  christos       sourml=DFWORD(df, 2);				      \
    657  1.1  christos       (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];   \
    658  1.1  christos       (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff];		      \
    659  1.1  christos       (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff];		      \
    660  1.1  christos       sourmh=DFWORD(df, 1);				      \
    661  1.1  christos       (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];   \
    662  1.1  christos       (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff];		      \
    663  1.1  christos       (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff];		      \
    664  1.1  christos       sourhi=DFWORD(df, 0);				      \
    665  1.1  christos       (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];   \
    666  1.1  christos       (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff];		      \
    667  1.1  christos       (buf)[11]=DECCOMBMSD[sourhi>>26];}
    668  1.1  christos     #endif
    669  1.1  christos 
    670  1.1  christos 
    671  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df and  */
    672  1.1  christos     /* add to a base-thousand uInt array (as for GETCOEFFTHOU).       */
    673  1.1  christos     /* After the addition then most significant 'digit' in the array  */
    674  1.1  christos     /* might have a value larger then 10 (with a maximum of 19).      */
    675  1.1  christos     #if DECPMAX==7
    676  1.1  christos     #define ADDCOEFFTHOU(df, buf) {			      \
    677  1.1  christos       uInt sourhi=DFWORD(df, 0);			      \
    678  1.1  christos       (buf)[0]+=DPD2BIN[sourhi&0x3ff];			      \
    679  1.1  christos       if (buf[0]>999) {buf[0]-=1000; buf[1]++;} 	      \
    680  1.1  christos       (buf)[1]+=DPD2BIN[(sourhi>>10)&0x3ff];		      \
    681  1.1  christos       if (buf[1]>999) {buf[1]-=1000; buf[2]++;} 	      \
    682  1.1  christos       (buf)[2]+=DECCOMBMSD[sourhi>>26];}
    683  1.1  christos 
    684  1.1  christos     #elif DECPMAX==16
    685  1.1  christos     #define ADDCOEFFTHOU(df, buf) {			      \
    686  1.1  christos       uInt sourhi, sourlo;				      \
    687  1.1  christos       sourlo=DFWORD(df, 1);				      \
    688  1.1  christos       (buf)[0]+=DPD2BIN[sourlo&0x3ff];			      \
    689  1.1  christos       if (buf[0]>999) {buf[0]-=1000; buf[1]++;} 	      \
    690  1.1  christos       (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    691  1.1  christos       if (buf[1]>999) {buf[1]-=1000; buf[2]++;} 	      \
    692  1.1  christos       (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    693  1.1  christos       if (buf[2]>999) {buf[2]-=1000; buf[3]++;} 	      \
    694  1.1  christos       sourhi=DFWORD(df, 0);				      \
    695  1.1  christos       (buf)[3]+=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];  \
    696  1.1  christos       if (buf[3]>999) {buf[3]-=1000; buf[4]++;} 	      \
    697  1.1  christos       (buf)[4]+=DPD2BIN[(sourhi>>8)&0x3ff];		      \
    698  1.1  christos       if (buf[4]>999) {buf[4]-=1000; buf[5]++;} 	      \
    699  1.1  christos       (buf)[5]+=DECCOMBMSD[sourhi>>26];}
    700  1.1  christos 
    701  1.1  christos     #elif DECPMAX==34
    702  1.1  christos     #define ADDCOEFFTHOU(df, buf) {			      \
    703  1.1  christos       uInt sourhi, sourmh, sourml, sourlo;		      \
    704  1.1  christos       sourlo=DFWORD(df, 3);				      \
    705  1.1  christos       (buf)[0]+=DPD2BIN[sourlo&0x3ff];			      \
    706  1.1  christos       if (buf[0]>999) {buf[0]-=1000; buf[1]++;} 	      \
    707  1.1  christos       (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    708  1.1  christos       if (buf[1]>999) {buf[1]-=1000; buf[2]++;} 	      \
    709  1.1  christos       (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    710  1.1  christos       if (buf[2]>999) {buf[2]-=1000; buf[3]++;} 	      \
    711  1.1  christos       sourml=DFWORD(df, 2);				      \
    712  1.1  christos       (buf)[3]+=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];  \
    713  1.1  christos       if (buf[3]>999) {buf[3]-=1000; buf[4]++;} 	      \
    714  1.1  christos       (buf)[4]+=DPD2BIN[(sourml>>8)&0x3ff];		      \
    715  1.1  christos       if (buf[4]>999) {buf[4]-=1000; buf[5]++;} 	      \
    716  1.1  christos       (buf)[5]+=DPD2BIN[(sourml>>18)&0x3ff];		      \
    717  1.1  christos       if (buf[5]>999) {buf[5]-=1000; buf[6]++;} 	      \
    718  1.1  christos       sourmh=DFWORD(df, 1);				      \
    719  1.1  christos       (buf)[6]+=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];  \
    720  1.1  christos       if (buf[6]>999) {buf[6]-=1000; buf[7]++;} 	      \
    721  1.1  christos       (buf)[7]+=DPD2BIN[(sourmh>>6)&0x3ff];		      \
    722  1.1  christos       if (buf[7]>999) {buf[7]-=1000; buf[8]++;} 	      \
    723  1.1  christos       (buf)[8]+=DPD2BIN[(sourmh>>16)&0x3ff];		      \
    724  1.1  christos       if (buf[8]>999) {buf[8]-=1000; buf[9]++;} 	      \
    725  1.1  christos       sourhi=DFWORD(df, 0);				      \
    726  1.1  christos       (buf)[9]+=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];  \
    727  1.1  christos       if (buf[9]>999) {buf[9]-=1000; buf[10]++;}	      \
    728  1.1  christos       (buf)[10]+=DPD2BIN[(sourhi>>4)&0x3ff];		      \
    729  1.1  christos       if (buf[10]>999) {buf[10]-=1000; buf[11]++;}	      \
    730  1.1  christos       (buf)[11]+=DECCOMBMSD[sourhi>>26];}
    731  1.1  christos     #endif
    732  1.1  christos 
    733  1.1  christos 
    734  1.1  christos     /* Set a decFloat to the maximum positive finite number (Nmax)    */
    735  1.1  christos     #if DECPMAX==7
    736  1.1  christos     #define DFSETNMAX(df)	     \
    737  1.1  christos       {DFWORD(df, 0)=0x77f3fcff;}
    738  1.1  christos     #elif DECPMAX==16
    739  1.1  christos     #define DFSETNMAX(df)	     \
    740  1.1  christos       {DFWORD(df, 0)=0x77fcff3f;     \
    741  1.1  christos        DFWORD(df, 1)=0xcff3fcff;}
    742  1.1  christos     #elif DECPMAX==34
    743  1.1  christos     #define DFSETNMAX(df)	     \
    744  1.1  christos       {DFWORD(df, 0)=0x77ffcff3;     \
    745  1.1  christos        DFWORD(df, 1)=0xfcff3fcf;     \
    746  1.1  christos        DFWORD(df, 2)=0xf3fcff3f;     \
    747  1.1  christos        DFWORD(df, 3)=0xcff3fcff;}
    748  1.1  christos     #endif
    749  1.1  christos 
    750  1.1  christos   /* [end of format-dependent macros and constants]		      */
    751  1.1  christos   #endif
    752  1.1  christos 
    753  1.1  christos #else
    754  1.1  christos   #error decNumberLocal included more than once
    755  1.1  christos #endif
    756