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