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