Home | History | Annotate | Line # | Download | only in libdecnumber
decNumberLocal.h revision 1.6
      1  1.1  christos /* Local definitions for the decNumber C Library.
      2  1.6  christos    Copyright (C) 2007-2018 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.3  christos   /* Has to use uiwork because i may be an expression.		      */
    157  1.3  christos   #define UBFROMUS(b, i)  (uswork=(i), memcpy(b, (void *)&uswork, 2))
    158  1.3  christos   #define UBFROMUI(b, i)  (uiwork=(i), memcpy(b, (void *)&uiwork, 4))
    159  1.1  christos 
    160  1.1  christos   /* X10 and X100 -- multiply integer i by 10 or 100		      */
    161  1.1  christos   /* [shifts are usually faster than multiply; could be conditional]  */
    162  1.1  christos   #define X10(i)  (((i)<<1)+((i)<<3))
    163  1.1  christos   #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))
    164  1.1  christos 
    165  1.1  christos   /* MAXI and MINI -- general max & min (not in ANSI) for integers    */
    166  1.1  christos   #define MAXI(x,y) ((x)<(y)?(y):(x))
    167  1.1  christos   #define MINI(x,y) ((x)>(y)?(y):(x))
    168  1.1  christos 
    169  1.1  christos   /* Useful constants						      */
    170  1.1  christos   #define BILLION      1000000000	     /* 10**9		      */
    171  1.1  christos   /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC       */
    172  1.1  christos   #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0')
    173  1.1  christos 
    174  1.1  christos 
    175  1.1  christos   /* ---------------------------------------------------------------- */
    176  1.1  christos   /* Definitions for arbitary-precision modules (only valid after     */
    177  1.1  christos   /* decNumber.h has been included)				      */
    178  1.1  christos   /* ---------------------------------------------------------------- */
    179  1.1  christos 
    180  1.1  christos   /* Limits and constants					      */
    181  1.1  christos   #define DECNUMMAXP 999999999	/* maximum precision code can handle  */
    182  1.1  christos   #define DECNUMMAXE 999999999	/* maximum adjusted exponent ditto    */
    183  1.1  christos   #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto    */
    184  1.1  christos   #if (DECNUMMAXP != DEC_MAX_DIGITS)
    185  1.1  christos     #error Maximum digits mismatch
    186  1.1  christos   #endif
    187  1.1  christos   #if (DECNUMMAXE != DEC_MAX_EMAX)
    188  1.1  christos     #error Maximum exponent mismatch
    189  1.1  christos   #endif
    190  1.1  christos   #if (DECNUMMINE != DEC_MIN_EMIN)
    191  1.1  christos     #error Minimum exponent mismatch
    192  1.1  christos   #endif
    193  1.1  christos 
    194  1.1  christos   /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN       */
    195  1.1  christos   /* digits, and D2UTABLE -- the initializer for the D2U table	      */
    196  1.1  christos   #if	DECDPUN==1
    197  1.1  christos     #define DECDPUNMAX 9
    198  1.1  christos     #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,  \
    199  1.1  christos 		      18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
    200  1.1  christos 		      33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
    201  1.1  christos 		      48,49}
    202  1.1  christos   #elif DECDPUN==2
    203  1.1  christos     #define DECDPUNMAX 99
    204  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,  \
    205  1.1  christos 		      11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \
    206  1.1  christos 		      18,19,19,20,20,21,21,22,22,23,23,24,24,25}
    207  1.1  christos   #elif DECDPUN==3
    208  1.1  christos     #define DECDPUNMAX 999
    209  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,  \
    210  1.1  christos 		      8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \
    211  1.1  christos 		      13,14,14,14,15,15,15,16,16,16,17}
    212  1.1  christos   #elif DECDPUN==4
    213  1.1  christos     #define DECDPUNMAX 9999
    214  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,  \
    215  1.1  christos 		      6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \
    216  1.1  christos 		      11,11,11,12,12,12,12,13}
    217  1.1  christos   #elif DECDPUN==5
    218  1.1  christos     #define DECDPUNMAX 99999
    219  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,  \
    220  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,  \
    221  1.1  christos 		      9,9,10,10,10,10}
    222  1.1  christos   #elif DECDPUN==6
    223  1.1  christos     #define DECDPUNMAX 999999
    224  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,  \
    225  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,  \
    226  1.1  christos 		      8,8,8,8,8,9}
    227  1.1  christos   #elif DECDPUN==7
    228  1.1  christos     #define DECDPUNMAX 9999999
    229  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,  \
    230  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,  \
    231  1.1  christos 		      7,7,7,7,7,7}
    232  1.1  christos   #elif DECDPUN==8
    233  1.1  christos     #define DECDPUNMAX 99999999
    234  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,  \
    235  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,  \
    236  1.1  christos 		      6,6,6,6,6,7}
    237  1.1  christos   #elif DECDPUN==9
    238  1.1  christos     #define DECDPUNMAX 999999999
    239  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,  \
    240  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,  \
    241  1.1  christos 		      5,5,6,6,6,6}
    242  1.1  christos   #elif defined(DECDPUN)
    243  1.1  christos     #error DECDPUN must be in the range 1-9
    244  1.1  christos   #endif
    245  1.1  christos 
    246  1.1  christos   /* ----- Shared data (in decNumber.c) ----- */
    247  1.1  christos   /* Public lookup table used by the D2U macro (see below)	      */
    248  1.1  christos   #define DECMAXD2U 49
    249  1.1  christos   extern const uByte d2utable[DECMAXD2U+1];
    250  1.1  christos 
    251  1.1  christos   /* ----- Macros ----- */
    252  1.1  christos   /* ISZERO -- return true if decNumber dn is a zero		      */
    253  1.1  christos   /* [performance-critical in some situations]			      */
    254  1.1  christos   #define ISZERO(dn) decNumberIsZero(dn)     /* now just a local name */
    255  1.1  christos 
    256  1.1  christos   /* D2U -- return the number of Units needed to hold d digits	      */
    257  1.1  christos   /* (runtime version, with table lookaside for small d)	      */
    258  1.1  christos   #if DECDPUN==8
    259  1.1  christos     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
    260  1.1  christos   #elif DECDPUN==4
    261  1.1  christos     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
    262  1.1  christos   #else
    263  1.1  christos     #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
    264  1.1  christos   #endif
    265  1.1  christos   /* SD2U -- static D2U macro (for compile-time calculation)	      */
    266  1.1  christos   #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)
    267  1.1  christos 
    268  1.1  christos   /* MSUDIGITS -- returns digits in msu, from digits, calculated      */
    269  1.1  christos   /* using D2U							      */
    270  1.1  christos   #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)
    271  1.1  christos 
    272  1.1  christos   /* D2N -- return the number of decNumber structs that would be      */
    273  1.1  christos   /* needed to contain that number of digits (and the initial	      */
    274  1.1  christos   /* decNumber struct) safely.	Note that one Unit is included in the */
    275  1.1  christos   /* initial structure.  Used for allocating space that is aligned on */
    276  1.1  christos   /* a decNumber struct boundary. */
    277  1.1  christos   #define D2N(d) \
    278  1.1  christos     ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))
    279  1.1  christos 
    280  1.1  christos   /* TODIGIT -- macro to remove the leading digit from the unsigned   */
    281  1.1  christos   /* integer u at column cut (counting from the right, LSD=0) and     */
    282  1.1  christos   /* place it as an ASCII character into the character pointed to by  */
    283  1.1  christos   /* c.  Note that cut must be <= 9, and the maximum value for u is   */
    284  1.1  christos   /* 2,000,000,000 (as is needed for negative exponents of	      */
    285  1.1  christos   /* subnormals).  The unsigned integer pow is used as a temporary    */
    286  1.1  christos   /* variable. */
    287  1.1  christos   #define TODIGIT(u, cut, c, pow) {	  \
    288  1.1  christos     *(c)='0';				  \
    289  1.1  christos     pow=DECPOWERS[cut]*2;		  \
    290  1.1  christos     if ((u)>pow) {			  \
    291  1.1  christos       pow*=4;				  \
    292  1.1  christos       if ((u)>=pow) {(u)-=pow; *(c)+=8;}  \
    293  1.1  christos       pow/=2;				  \
    294  1.1  christos       if ((u)>=pow) {(u)-=pow; *(c)+=4;}  \
    295  1.1  christos       pow/=2;				  \
    296  1.1  christos       } 				  \
    297  1.1  christos     if ((u)>=pow) {(u)-=pow; *(c)+=2;}	  \
    298  1.1  christos     pow/=2;				  \
    299  1.1  christos     if ((u)>=pow) {(u)-=pow; *(c)+=1;}	  \
    300  1.1  christos     }
    301  1.1  christos 
    302  1.1  christos   /* ---------------------------------------------------------------- */
    303  1.1  christos   /* Definitions for fixed-precision modules (only valid after	      */
    304  1.1  christos   /* decSingle.h, decDouble.h, or decQuad.h has been included)	      */
    305  1.1  christos   /* ---------------------------------------------------------------- */
    306  1.1  christos 
    307  1.1  christos   /* bcdnum -- a structure describing a format-independent finite     */
    308  1.1  christos   /* number, whose coefficient is a string of bcd8 uBytes	      */
    309  1.1  christos   typedef struct {
    310  1.1  christos     uByte   *msd;	      /* -> most significant digit	      */
    311  1.1  christos     uByte   *lsd;	      /* -> least ditto 		      */
    312  1.1  christos     uInt     sign;	      /* 0=positive, DECFLOAT_Sign=negative   */
    313  1.1  christos     Int      exponent;	      /* Unadjusted signed exponent (q), or   */
    314  1.1  christos 			      /* DECFLOAT_NaN etc. for a special      */
    315  1.1  christos     } bcdnum;
    316  1.1  christos 
    317  1.1  christos   /* Test if exponent or bcdnum exponent must be a special, etc.      */
    318  1.1  christos   #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp)
    319  1.1  christos   #define EXPISINF(exp) (exp==DECFLOAT_Inf)
    320  1.1  christos   #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN)
    321  1.1  christos   #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent))
    322  1.1  christos 
    323  1.1  christos   /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian  */
    324  1.1  christos   /* (array) notation (the 0 word or byte contains the sign bit),     */
    325  1.1  christos   /* automatically adjusting for endianness; similarly address a word */
    326  1.1  christos   /* in the next-wider format (decFloatWider, or dfw)		      */
    327  1.1  christos   #define DECWORDS  (DECBYTES/4)
    328  1.1  christos   #define DECWWORDS (DECWBYTES/4)
    329  1.1  christos   #if DECLITEND
    330  1.1  christos     #define DFBYTE(df, off)   ((df)->bytes[DECBYTES-1-(off)])
    331  1.1  christos     #define DFWORD(df, off)   ((df)->words[DECWORDS-1-(off)])
    332  1.1  christos     #define DFWWORD(dfw, off) ((dfw)->words[DECWWORDS-1-(off)])
    333  1.1  christos   #else
    334  1.1  christos     #define DFBYTE(df, off)   ((df)->bytes[off])
    335  1.1  christos     #define DFWORD(df, off)   ((df)->words[off])
    336  1.1  christos     #define DFWWORD(dfw, off) ((dfw)->words[off])
    337  1.1  christos   #endif
    338  1.1  christos 
    339  1.1  christos   /* Tests for sign or specials, directly on DECFLOATs		      */
    340  1.1  christos   #define DFISSIGNED(df)   (DFWORD(df, 0)&0x80000000)
    341  1.1  christos   #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000)
    342  1.1  christos   #define DFISINF(df)	  ((DFWORD(df, 0)&0x7c000000)==0x78000000)
    343  1.1  christos   #define DFISNAN(df)	  ((DFWORD(df, 0)&0x7c000000)==0x7c000000)
    344  1.1  christos   #define DFISQNAN(df)	  ((DFWORD(df, 0)&0x7e000000)==0x7c000000)
    345  1.1  christos   #define DFISSNAN(df)	  ((DFWORD(df, 0)&0x7e000000)==0x7e000000)
    346  1.1  christos 
    347  1.1  christos   /* Shared lookup tables					      */
    348  1.1  christos #include "decCommonSymbols.h"
    349  1.1  christos   extern const uInt   DECCOMBMSD[64];	/* Combination field -> MSD   */
    350  1.1  christos   extern const uInt   DECCOMBFROM[48];	/* exp+msd -> Combination     */
    351  1.1  christos 
    352  1.1  christos   /* Private generic (utility) routine				      */
    353  1.1  christos   #if DECCHECK || DECTRACE
    354  1.1  christos     extern void decShowNum(const bcdnum *, const char *);
    355  1.1  christos   #endif
    356  1.1  christos 
    357  1.1  christos   /* Format-dependent macros and constants			      */
    358  1.1  christos   #if defined(DECPMAX)
    359  1.1  christos 
    360  1.1  christos     /* Useful constants 					      */
    361  1.1  christos     #define DECPMAX9  (ROUNDUP(DECPMAX, 9)/9)  /* 'Pmax' in 10**9s    */
    362  1.1  christos     /* Top words for a zero					      */
    363  1.1  christos     #define SINGLEZERO	 0x22500000
    364  1.1  christos     #define DOUBLEZERO	 0x22380000
    365  1.1  christos     #define QUADZERO	 0x22080000
    366  1.1  christos     /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */
    367  1.1  christos 
    368  1.1  christos     /* Format-dependent common tests:				      */
    369  1.1  christos     /*	 DFISZERO   -- test for (any) zero			      */
    370  1.1  christos     /*	 DFISCCZERO -- test for coefficient continuation being zero   */
    371  1.1  christos     /*	 DFISCC01   -- test for coefficient contains only 0s and 1s   */
    372  1.1  christos     /*	 DFISINT    -- test for finite and exponent q=0 	      */
    373  1.1  christos     /*	 DFISUINT01 -- test for sign=0, finite, exponent q=0, and     */
    374  1.1  christos     /*		       MSD=0 or 1				      */
    375  1.1  christos     /*	 ZEROWORD is also defined here. 			      */
    376  1.1  christos     /* In DFISZERO the first test checks the least-significant word   */
    377  1.1  christos     /* (most likely to be non-zero); the penultimate tests MSD and    */
    378  1.1  christos     /* DPDs in the signword, and the final test excludes specials and */
    379  1.1  christos     /* MSD>7.  DFISINT similarly has to allow for the two forms of    */
    380  1.1  christos     /* MSD codes.  DFISUINT01 only has to allow for one form of MSD   */
    381  1.1  christos     /* code.							      */
    382  1.1  christos     #if DECPMAX==7
    383  1.1  christos       #define ZEROWORD SINGLEZERO
    384  1.1  christos       /* [test macros not needed except for Zero]		      */
    385  1.1  christos       #define DFISZERO(df)  ((DFWORD(df, 0)&0x1c0fffff)==0	   \
    386  1.1  christos 			  && (DFWORD(df, 0)&0x60000000)!=0x60000000)
    387  1.1  christos     #elif DECPMAX==16
    388  1.1  christos       #define ZEROWORD DOUBLEZERO
    389  1.1  christos       #define DFISZERO(df)  ((DFWORD(df, 1)==0			   \
    390  1.1  christos 			  && (DFWORD(df, 0)&0x1c03ffff)==0	   \
    391  1.1  christos 			  && (DFWORD(df, 0)&0x60000000)!=0x60000000))
    392  1.1  christos       #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000  \
    393  1.1  christos 			 ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000)
    394  1.1  christos       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000)
    395  1.1  christos       #define DFISCCZERO(df) (DFWORD(df, 1)==0			   \
    396  1.1  christos 			  && (DFWORD(df, 0)&0x0003ffff)==0)
    397  1.1  christos       #define DFISCC01(df)  ((DFWORD(df, 0)&~0xfffc9124)==0	   \
    398  1.1  christos 			  && (DFWORD(df, 1)&~0x49124491)==0)
    399  1.1  christos     #elif DECPMAX==34
    400  1.1  christos       #define ZEROWORD QUADZERO
    401  1.1  christos       #define DFISZERO(df)  ((DFWORD(df, 3)==0			   \
    402  1.1  christos 			  &&  DFWORD(df, 2)==0			   \
    403  1.1  christos 			  &&  DFWORD(df, 1)==0			   \
    404  1.1  christos 			  && (DFWORD(df, 0)&0x1c003fff)==0	   \
    405  1.1  christos 			  && (DFWORD(df, 0)&0x60000000)!=0x60000000))
    406  1.1  christos       #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000  \
    407  1.1  christos 			 ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000)
    408  1.1  christos       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000)
    409  1.1  christos       #define DFISCCZERO(df) (DFWORD(df, 3)==0			   \
    410  1.1  christos 			  &&  DFWORD(df, 2)==0			   \
    411  1.1  christos 			  &&  DFWORD(df, 1)==0			   \
    412  1.1  christos 			  && (DFWORD(df, 0)&0x00003fff)==0)
    413  1.1  christos 
    414  1.1  christos       #define DFISCC01(df)   ((DFWORD(df, 0)&~0xffffc912)==0	   \
    415  1.1  christos 			  &&  (DFWORD(df, 1)&~0x44912449)==0	   \
    416  1.1  christos 			  &&  (DFWORD(df, 2)&~0x12449124)==0	   \
    417  1.1  christos 			  &&  (DFWORD(df, 3)&~0x49124491)==0)
    418  1.1  christos     #endif
    419  1.1  christos 
    420  1.1  christos     /* Macros to test if a certain 10 bits of a uInt or pair of uInts */
    421  1.1  christos     /* are a canonical declet [higher or lower bits are ignored].     */
    422  1.1  christos     /* declet is at offset 0 (from the right) in a uInt:	      */
    423  1.1  christos     #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e)
    424  1.1  christos     /* declet is at offset k (a multiple of 2) in a uInt:	      */
    425  1.1  christos     #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0	    \
    426  1.1  christos       || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
    427  1.1  christos     /* declet is at offset k (a multiple of 2) in a pair of uInts:    */
    428  1.1  christos     /* [the top 2 bits will always be in the more-significant uInt]   */
    429  1.1  christos     #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0     \
    430  1.1  christos       || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k)))		    \
    431  1.1  christos       || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
    432  1.1  christos 
    433  1.1  christos     /* Macro to test whether a full-length (length DECPMAX) BCD8      */
    434  1.1  christos     /* coefficient, starting at uByte u, is all zeros		      */
    435  1.1  christos     /* Test just the LSWord first, then the remainder as a sequence   */
    436  1.1  christos     /* of tests in order to avoid same-level use of UBTOUI	      */
    437  1.1  christos     #if DECPMAX==7
    438  1.1  christos       #define ISCOEFFZERO(u) (					    \
    439  1.1  christos 	   UBTOUI((u)+DECPMAX-4)==0				    \
    440  1.1  christos 	&& UBTOUS((u)+DECPMAX-6)==0				    \
    441  1.1  christos 	&& *(u)==0)
    442  1.1  christos     #elif DECPMAX==16
    443  1.1  christos       #define ISCOEFFZERO(u) (					    \
    444  1.1  christos 	   UBTOUI((u)+DECPMAX-4)==0				    \
    445  1.1  christos 	&& UBTOUI((u)+DECPMAX-8)==0				    \
    446  1.1  christos 	&& UBTOUI((u)+DECPMAX-12)==0				    \
    447  1.1  christos 	&& UBTOUI(u)==0)
    448  1.1  christos     #elif DECPMAX==34
    449  1.1  christos       #define ISCOEFFZERO(u) (					    \
    450  1.1  christos 	   UBTOUI((u)+DECPMAX-4)==0				    \
    451  1.1  christos 	&& UBTOUI((u)+DECPMAX-8)==0				    \
    452  1.1  christos 	&& UBTOUI((u)+DECPMAX-12)==0				    \
    453  1.1  christos 	&& UBTOUI((u)+DECPMAX-16)==0				    \
    454  1.1  christos 	&& UBTOUI((u)+DECPMAX-20)==0				    \
    455  1.1  christos 	&& UBTOUI((u)+DECPMAX-24)==0				    \
    456  1.1  christos 	&& UBTOUI((u)+DECPMAX-28)==0				    \
    457  1.1  christos 	&& UBTOUI((u)+DECPMAX-32)==0				    \
    458  1.1  christos 	&& UBTOUS(u)==0)
    459  1.1  christos     #endif
    460  1.1  christos 
    461  1.1  christos     /* Macros and masks for the exponent continuation field and MSD   */
    462  1.1  christos     /* Get the exponent continuation from a decFloat *df as an Int    */
    463  1.1  christos     #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL)))
    464  1.1  christos     /* Ditto, from the next-wider format			      */
    465  1.1  christos     #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL)))
    466  1.1  christos     /* Get the biased exponent similarly			      */
    467  1.1  christos     #define GETEXP(df)	((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df)))
    468  1.1  christos     /* Get the unbiased exponent similarly			      */
    469  1.1  christos     #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS)
    470  1.1  christos     /* Get the MSD similarly (as uInt)				      */
    471  1.1  christos     #define GETMSD(df)	 (DECCOMBMSD[DFWORD((df), 0)>>26])
    472  1.1  christos 
    473  1.1  christos     /* Compile-time computes of the exponent continuation field masks */
    474  1.1  christos     /* full exponent continuation field:			      */
    475  1.1  christos     #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
    476  1.1  christos     /* same, not including its first digit (the qNaN/sNaN selector):  */
    477  1.1  christos     #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
    478  1.1  christos 
    479  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df into */
    480  1.1  christos     /* a BCD string (uByte *bcdin) of length DECPMAX uBytes.	      */
    481  1.1  christos 
    482  1.1  christos     /* In-line sequence to convert least significant 10 bits of uInt  */
    483  1.1  christos     /* dpd to three BCD8 digits starting at uByte u.  Note that an    */
    484  1.1  christos     /* extra byte is written to the right of the three digits because */
    485  1.1  christos     /* four bytes are moved at a time for speed; the alternative      */
    486  1.1  christos     /* macro moves exactly three bytes (usually slower).	      */
    487  1.1  christos     #define dpd2bcd8(u, dpd)  memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 4)
    488  1.1  christos     #define dpd2bcd83(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 3)
    489  1.1  christos 
    490  1.1  christos     /* Decode the declets.  After extracting each one, it is decoded  */
    491  1.1  christos     /* to BCD8 using a table lookup (also used for variable-length    */
    492  1.1  christos     /* decode).  Each DPD decode is 3 bytes BCD8 plus a one-byte      */
    493  1.1  christos     /* length which is not used, here).  Fixed-length 4-byte moves    */
    494  1.1  christos     /* are fast, however, almost everywhere, and so are used except   */
    495  1.1  christos     /* for the final three bytes (to avoid overrun).  The code below  */
    496  1.1  christos     /* is 36 instructions for Doubles and about 70 for Quads, even    */
    497  1.1  christos     /* on IA32. 						      */
    498  1.1  christos 
    499  1.1  christos     /* Two macros are defined for each format:			      */
    500  1.1  christos     /*	 GETCOEFF extracts the coefficient of the current format      */
    501  1.1  christos     /*	 GETWCOEFF extracts the coefficient of the next-wider format. */
    502  1.1  christos     /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */
    503  1.1  christos 
    504  1.1  christos     #if DECPMAX==7
    505  1.1  christos     #define GETCOEFF(df, bcd) { 			 \
    506  1.1  christos       uInt sourhi=DFWORD(df, 0);			 \
    507  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    508  1.1  christos       dpd2bcd8(bcd+1, sourhi>>10);			 \
    509  1.1  christos       dpd2bcd83(bcd+4, sourhi);}
    510  1.1  christos     #define GETWCOEFF(df, bcd) {			 \
    511  1.1  christos       uInt sourhi=DFWWORD(df, 0);			 \
    512  1.1  christos       uInt sourlo=DFWWORD(df, 1);			 \
    513  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    514  1.1  christos       dpd2bcd8(bcd+1, sourhi>>8);			 \
    515  1.1  christos       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));	 \
    516  1.1  christos       dpd2bcd8(bcd+7, sourlo>>20);			 \
    517  1.1  christos       dpd2bcd8(bcd+10, sourlo>>10);			 \
    518  1.1  christos       dpd2bcd83(bcd+13, sourlo);}
    519  1.1  christos 
    520  1.1  christos     #elif DECPMAX==16
    521  1.1  christos     #define GETCOEFF(df, bcd) { 			 \
    522  1.1  christos       uInt sourhi=DFWORD(df, 0);			 \
    523  1.1  christos       uInt sourlo=DFWORD(df, 1);			 \
    524  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    525  1.1  christos       dpd2bcd8(bcd+1, sourhi>>8);			 \
    526  1.1  christos       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));	 \
    527  1.1  christos       dpd2bcd8(bcd+7, sourlo>>20);			 \
    528  1.1  christos       dpd2bcd8(bcd+10, sourlo>>10);			 \
    529  1.1  christos       dpd2bcd83(bcd+13, sourlo);}
    530  1.1  christos     #define GETWCOEFF(df, bcd) {			 \
    531  1.1  christos       uInt sourhi=DFWWORD(df, 0);			 \
    532  1.1  christos       uInt sourmh=DFWWORD(df, 1);			 \
    533  1.1  christos       uInt sourml=DFWWORD(df, 2);			 \
    534  1.1  christos       uInt sourlo=DFWWORD(df, 3);			 \
    535  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    536  1.1  christos       dpd2bcd8(bcd+1, sourhi>>4);			 \
    537  1.1  christos       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));	 \
    538  1.1  christos       dpd2bcd8(bcd+7, sourmh>>16);			 \
    539  1.1  christos       dpd2bcd8(bcd+10, sourmh>>6);			 \
    540  1.1  christos       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));	 \
    541  1.1  christos       dpd2bcd8(bcd+16, sourml>>18);			 \
    542  1.1  christos       dpd2bcd8(bcd+19, sourml>>8);			 \
    543  1.1  christos       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));	 \
    544  1.1  christos       dpd2bcd8(bcd+25, sourlo>>20);			 \
    545  1.1  christos       dpd2bcd8(bcd+28, sourlo>>10);			 \
    546  1.1  christos       dpd2bcd83(bcd+31, sourlo);}
    547  1.1  christos 
    548  1.1  christos     #elif DECPMAX==34
    549  1.1  christos     #define GETCOEFF(df, bcd) { 			 \
    550  1.1  christos       uInt sourhi=DFWORD(df, 0);			 \
    551  1.1  christos       uInt sourmh=DFWORD(df, 1);			 \
    552  1.1  christos       uInt sourml=DFWORD(df, 2);			 \
    553  1.1  christos       uInt sourlo=DFWORD(df, 3);			 \
    554  1.1  christos       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];		 \
    555  1.1  christos       dpd2bcd8(bcd+1, sourhi>>4);			 \
    556  1.1  christos       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));	 \
    557  1.1  christos       dpd2bcd8(bcd+7, sourmh>>16);			 \
    558  1.1  christos       dpd2bcd8(bcd+10, sourmh>>6);			 \
    559  1.1  christos       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));	 \
    560  1.1  christos       dpd2bcd8(bcd+16, sourml>>18);			 \
    561  1.1  christos       dpd2bcd8(bcd+19, sourml>>8);			 \
    562  1.1  christos       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));	 \
    563  1.1  christos       dpd2bcd8(bcd+25, sourlo>>20);			 \
    564  1.1  christos       dpd2bcd8(bcd+28, sourlo>>10);			 \
    565  1.1  christos       dpd2bcd83(bcd+31, sourlo);}
    566  1.1  christos 
    567  1.1  christos       #define GETWCOEFF(df, bcd) {??} /* [should never be used]       */
    568  1.1  christos     #endif
    569  1.1  christos 
    570  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df into */
    571  1.1  christos     /* a base-billion uInt array, with the least-significant	      */
    572  1.1  christos     /* 0-999999999 'digit' at offset 0. 			      */
    573  1.1  christos 
    574  1.1  christos     /* Decode the declets.  After extracting each one, it is decoded  */
    575  1.1  christos     /* to binary using a table lookup.	Three tables are used; one    */
    576  1.1  christos     /* the usual DPD to binary, the other two pre-multiplied by 1000  */
    577  1.1  christos     /* and 1000000 to avoid multiplication during decode.  These      */
    578  1.1  christos     /* tables can also be used for multiplying up the MSD as the DPD  */
    579  1.1  christos     /* code for 0 through 9 is the identity.			      */
    580  1.1  christos     #define DPD2BIN0 DPD2BIN	     /* for prettier code	      */
    581  1.1  christos 
    582  1.1  christos     #if DECPMAX==7
    583  1.1  christos     #define GETCOEFFBILL(df, buf) {			      \
    584  1.1  christos       uInt sourhi=DFWORD(df, 0);			      \
    585  1.1  christos       (buf)[0]=DPD2BIN0[sourhi&0x3ff]			      \
    586  1.1  christos 	      +DPD2BINK[(sourhi>>10)&0x3ff]		      \
    587  1.1  christos 	      +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    588  1.1  christos 
    589  1.1  christos     #elif DECPMAX==16
    590  1.1  christos     #define GETCOEFFBILL(df, buf) {			      \
    591  1.1  christos       uInt sourhi, sourlo;				      \
    592  1.1  christos       sourlo=DFWORD(df, 1);				      \
    593  1.1  christos       (buf)[0]=DPD2BIN0[sourlo&0x3ff]			      \
    594  1.1  christos 	      +DPD2BINK[(sourlo>>10)&0x3ff]		      \
    595  1.1  christos 	      +DPD2BINM[(sourlo>>20)&0x3ff];		      \
    596  1.1  christos       sourhi=DFWORD(df, 0);				      \
    597  1.1  christos       (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff]   \
    598  1.1  christos 	      +DPD2BINK[(sourhi>>8)&0x3ff]		      \
    599  1.1  christos 	      +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    600  1.1  christos 
    601  1.1  christos     #elif DECPMAX==34
    602  1.1  christos     #define GETCOEFFBILL(df, buf) {			      \
    603  1.1  christos       uInt sourhi, sourmh, sourml, sourlo;		      \
    604  1.1  christos       sourlo=DFWORD(df, 3);				      \
    605  1.1  christos       (buf)[0]=DPD2BIN0[sourlo&0x3ff]			      \
    606  1.1  christos 	      +DPD2BINK[(sourlo>>10)&0x3ff]		      \
    607  1.1  christos 	      +DPD2BINM[(sourlo>>20)&0x3ff];		      \
    608  1.1  christos       sourml=DFWORD(df, 2);				      \
    609  1.1  christos       (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff]   \
    610  1.1  christos 	      +DPD2BINK[(sourml>>8)&0x3ff]		      \
    611  1.1  christos 	      +DPD2BINM[(sourml>>18)&0x3ff];		      \
    612  1.1  christos       sourmh=DFWORD(df, 1);				      \
    613  1.1  christos       (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff]   \
    614  1.1  christos 	      +DPD2BINK[(sourmh>>6)&0x3ff]		      \
    615  1.1  christos 	      +DPD2BINM[(sourmh>>16)&0x3ff];		      \
    616  1.1  christos       sourhi=DFWORD(df, 0);				      \
    617  1.1  christos       (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff]   \
    618  1.1  christos 	      +DPD2BINK[(sourhi>>4)&0x3ff]		      \
    619  1.1  christos 	      +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    620  1.1  christos 
    621  1.1  christos     #endif
    622  1.1  christos 
    623  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df into */
    624  1.1  christos     /* a base-thousand uInt array (of size DECLETS+1, to allow for    */
    625  1.1  christos     /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/
    626  1.1  christos 
    627  1.1  christos     /* Decode the declets.  After extracting each one, it is decoded  */
    628  1.1  christos     /* to binary using a table lookup.				      */
    629  1.1  christos     #if DECPMAX==7
    630  1.1  christos     #define GETCOEFFTHOU(df, buf) {			      \
    631  1.1  christos       uInt sourhi=DFWORD(df, 0);			      \
    632  1.1  christos       (buf)[0]=DPD2BIN[sourhi&0x3ff];			      \
    633  1.1  christos       (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff];		      \
    634  1.1  christos       (buf)[2]=DECCOMBMSD[sourhi>>26];}
    635  1.1  christos 
    636  1.1  christos     #elif DECPMAX==16
    637  1.1  christos     #define GETCOEFFTHOU(df, buf) {			      \
    638  1.1  christos       uInt sourhi, sourlo;				      \
    639  1.1  christos       sourlo=DFWORD(df, 1);				      \
    640  1.1  christos       (buf)[0]=DPD2BIN[sourlo&0x3ff];			      \
    641  1.1  christos       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    642  1.1  christos       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    643  1.1  christos       sourhi=DFWORD(df, 0);				      \
    644  1.1  christos       (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];   \
    645  1.1  christos       (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff];		      \
    646  1.1  christos       (buf)[5]=DECCOMBMSD[sourhi>>26];}
    647  1.1  christos 
    648  1.1  christos     #elif DECPMAX==34
    649  1.1  christos     #define GETCOEFFTHOU(df, buf) {			      \
    650  1.1  christos       uInt sourhi, sourmh, sourml, sourlo;		      \
    651  1.1  christos       sourlo=DFWORD(df, 3);				      \
    652  1.1  christos       (buf)[0]=DPD2BIN[sourlo&0x3ff];			      \
    653  1.1  christos       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    654  1.1  christos       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    655  1.1  christos       sourml=DFWORD(df, 2);				      \
    656  1.1  christos       (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];   \
    657  1.1  christos       (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff];		      \
    658  1.1  christos       (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff];		      \
    659  1.1  christos       sourmh=DFWORD(df, 1);				      \
    660  1.1  christos       (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];   \
    661  1.1  christos       (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff];		      \
    662  1.1  christos       (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff];		      \
    663  1.1  christos       sourhi=DFWORD(df, 0);				      \
    664  1.1  christos       (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];   \
    665  1.1  christos       (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff];		      \
    666  1.1  christos       (buf)[11]=DECCOMBMSD[sourhi>>26];}
    667  1.1  christos     #endif
    668  1.1  christos 
    669  1.1  christos 
    670  1.1  christos     /* Macros to decode the coefficient in a finite decFloat *df and  */
    671  1.1  christos     /* add to a base-thousand uInt array (as for GETCOEFFTHOU).       */
    672  1.1  christos     /* After the addition then most significant 'digit' in the array  */
    673  1.1  christos     /* might have a value larger then 10 (with a maximum of 19).      */
    674  1.1  christos     #if DECPMAX==7
    675  1.1  christos     #define ADDCOEFFTHOU(df, buf) {			      \
    676  1.1  christos       uInt sourhi=DFWORD(df, 0);			      \
    677  1.1  christos       (buf)[0]+=DPD2BIN[sourhi&0x3ff];			      \
    678  1.1  christos       if (buf[0]>999) {buf[0]-=1000; buf[1]++;} 	      \
    679  1.1  christos       (buf)[1]+=DPD2BIN[(sourhi>>10)&0x3ff];		      \
    680  1.1  christos       if (buf[1]>999) {buf[1]-=1000; buf[2]++;} 	      \
    681  1.1  christos       (buf)[2]+=DECCOMBMSD[sourhi>>26];}
    682  1.1  christos 
    683  1.1  christos     #elif DECPMAX==16
    684  1.1  christos     #define ADDCOEFFTHOU(df, buf) {			      \
    685  1.1  christos       uInt sourhi, sourlo;				      \
    686  1.1  christos       sourlo=DFWORD(df, 1);				      \
    687  1.1  christos       (buf)[0]+=DPD2BIN[sourlo&0x3ff];			      \
    688  1.1  christos       if (buf[0]>999) {buf[0]-=1000; buf[1]++;} 	      \
    689  1.1  christos       (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    690  1.1  christos       if (buf[1]>999) {buf[1]-=1000; buf[2]++;} 	      \
    691  1.1  christos       (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    692  1.1  christos       if (buf[2]>999) {buf[2]-=1000; buf[3]++;} 	      \
    693  1.1  christos       sourhi=DFWORD(df, 0);				      \
    694  1.1  christos       (buf)[3]+=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];  \
    695  1.1  christos       if (buf[3]>999) {buf[3]-=1000; buf[4]++;} 	      \
    696  1.1  christos       (buf)[4]+=DPD2BIN[(sourhi>>8)&0x3ff];		      \
    697  1.1  christos       if (buf[4]>999) {buf[4]-=1000; buf[5]++;} 	      \
    698  1.1  christos       (buf)[5]+=DECCOMBMSD[sourhi>>26];}
    699  1.1  christos 
    700  1.1  christos     #elif DECPMAX==34
    701  1.1  christos     #define ADDCOEFFTHOU(df, buf) {			      \
    702  1.1  christos       uInt sourhi, sourmh, sourml, sourlo;		      \
    703  1.1  christos       sourlo=DFWORD(df, 3);				      \
    704  1.1  christos       (buf)[0]+=DPD2BIN[sourlo&0x3ff];			      \
    705  1.1  christos       if (buf[0]>999) {buf[0]-=1000; buf[1]++;} 	      \
    706  1.1  christos       (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff];		      \
    707  1.1  christos       if (buf[1]>999) {buf[1]-=1000; buf[2]++;} 	      \
    708  1.1  christos       (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff];		      \
    709  1.1  christos       if (buf[2]>999) {buf[2]-=1000; buf[3]++;} 	      \
    710  1.1  christos       sourml=DFWORD(df, 2);				      \
    711  1.1  christos       (buf)[3]+=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];  \
    712  1.1  christos       if (buf[3]>999) {buf[3]-=1000; buf[4]++;} 	      \
    713  1.1  christos       (buf)[4]+=DPD2BIN[(sourml>>8)&0x3ff];		      \
    714  1.1  christos       if (buf[4]>999) {buf[4]-=1000; buf[5]++;} 	      \
    715  1.1  christos       (buf)[5]+=DPD2BIN[(sourml>>18)&0x3ff];		      \
    716  1.1  christos       if (buf[5]>999) {buf[5]-=1000; buf[6]++;} 	      \
    717  1.1  christos       sourmh=DFWORD(df, 1);				      \
    718  1.1  christos       (buf)[6]+=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];  \
    719  1.1  christos       if (buf[6]>999) {buf[6]-=1000; buf[7]++;} 	      \
    720  1.1  christos       (buf)[7]+=DPD2BIN[(sourmh>>6)&0x3ff];		      \
    721  1.1  christos       if (buf[7]>999) {buf[7]-=1000; buf[8]++;} 	      \
    722  1.1  christos       (buf)[8]+=DPD2BIN[(sourmh>>16)&0x3ff];		      \
    723  1.1  christos       if (buf[8]>999) {buf[8]-=1000; buf[9]++;} 	      \
    724  1.1  christos       sourhi=DFWORD(df, 0);				      \
    725  1.1  christos       (buf)[9]+=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];  \
    726  1.1  christos       if (buf[9]>999) {buf[9]-=1000; buf[10]++;}	      \
    727  1.1  christos       (buf)[10]+=DPD2BIN[(sourhi>>4)&0x3ff];		      \
    728  1.1  christos       if (buf[10]>999) {buf[10]-=1000; buf[11]++;}	      \
    729  1.1  christos       (buf)[11]+=DECCOMBMSD[sourhi>>26];}
    730  1.1  christos     #endif
    731  1.1  christos 
    732  1.1  christos 
    733  1.1  christos     /* Set a decFloat to the maximum positive finite number (Nmax)    */
    734  1.1  christos     #if DECPMAX==7
    735  1.1  christos     #define DFSETNMAX(df)	     \
    736  1.1  christos       {DFWORD(df, 0)=0x77f3fcff;}
    737  1.1  christos     #elif DECPMAX==16
    738  1.1  christos     #define DFSETNMAX(df)	     \
    739  1.1  christos       {DFWORD(df, 0)=0x77fcff3f;     \
    740  1.1  christos        DFWORD(df, 1)=0xcff3fcff;}
    741  1.1  christos     #elif DECPMAX==34
    742  1.1  christos     #define DFSETNMAX(df)	     \
    743  1.1  christos       {DFWORD(df, 0)=0x77ffcff3;     \
    744  1.1  christos        DFWORD(df, 1)=0xfcff3fcf;     \
    745  1.1  christos        DFWORD(df, 2)=0xf3fcff3f;     \
    746  1.1  christos        DFWORD(df, 3)=0xcff3fcff;}
    747  1.1  christos     #endif
    748  1.1  christos 
    749  1.1  christos   /* [end of format-dependent macros and constants]		      */
    750  1.1  christos   #endif
    751  1.1  christos 
    752  1.1  christos #else
    753  1.1  christos   #error decNumberLocal included more than once
    754  1.1  christos #endif
    755