1*404b540aSrobert /* Utility functions for decimal floating point support via decNumber.
2*404b540aSrobert Copyright (C) 2005 Free Software Foundation, Inc.
3*404b540aSrobert Contributed by IBM Corporation. Author Mike Cowlishaw.
4*404b540aSrobert
5*404b540aSrobert This file is part of GCC.
6*404b540aSrobert
7*404b540aSrobert GCC is free software; you can redistribute it and/or modify it under
8*404b540aSrobert the terms of the GNU General Public License as published by the Free
9*404b540aSrobert Software Foundation; either version 2, or (at your option) any later
10*404b540aSrobert version.
11*404b540aSrobert
12*404b540aSrobert In addition to the permissions in the GNU General Public License,
13*404b540aSrobert the Free Software Foundation gives you unlimited permission to link
14*404b540aSrobert the compiled version of this file into combinations with other
15*404b540aSrobert programs, and to distribute those combinations without any
16*404b540aSrobert restriction coming from the use of this file. (The General Public
17*404b540aSrobert License restrictions do apply in other respects; for example, they
18*404b540aSrobert cover modification of the file, and distribution when not linked
19*404b540aSrobert into a combine executable.)
20*404b540aSrobert
21*404b540aSrobert GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or
23*404b540aSrobert FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24*404b540aSrobert for more details.
25*404b540aSrobert
26*404b540aSrobert You should have received a copy of the GNU General Public License
27*404b540aSrobert along with GCC; see the file COPYING. If not, write to the Free
28*404b540aSrobert Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29*404b540aSrobert 02110-1301, USA. */
30*404b540aSrobert
31*404b540aSrobert #include "config.h"
32*404b540aSrobert #include "decNumber.h" /* base number library */
33*404b540aSrobert #include "decNumberLocal.h" /* decNumber local types, etc. */
34*404b540aSrobert #include "decUtility.h" /* utility routines */
35*404b540aSrobert
36*404b540aSrobert /* ================================================================== */
37*404b540aSrobert /* Shared utility routines */
38*404b540aSrobert /* ================================================================== */
39*404b540aSrobert
40*404b540aSrobert /* define and include the conversion tables to use */
41*404b540aSrobert #define DEC_BIN2DPD 1 /* used for all sizes */
42*404b540aSrobert #if DECDPUN==3
43*404b540aSrobert #define DEC_DPD2BIN 1
44*404b540aSrobert #else
45*404b540aSrobert #define DEC_DPD2BCD 1
46*404b540aSrobert #endif
47*404b540aSrobert #include "decDPD.h" /* lookup tables */
48*404b540aSrobert
49*404b540aSrobert /* The maximum number of decNumberUnits we need for a working copy of */
50*404b540aSrobert /* the units array is the ceiling of digits/DECDPUN, where digits is */
51*404b540aSrobert /* the maximum number of digits in any of the formats for which this */
52*404b540aSrobert /* is used. We do not want to include decimal128.h, so, as a very */
53*404b540aSrobert /* special case, that number is defined here. */
54*404b540aSrobert #define DECMAX754 34
55*404b540aSrobert #define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
56*404b540aSrobert
57*404b540aSrobert /* ------------------------------------------------------------------ */
58*404b540aSrobert /* decDensePackCoeff -- densely pack coefficient into DPD form */
59*404b540aSrobert /* */
60*404b540aSrobert /* dn is the source number (assumed valid, max DECMAX754 digits) */
61*404b540aSrobert /* bytes is the target's byte array */
62*404b540aSrobert /* len is length of target format's byte array */
63*404b540aSrobert /* shift is the number of 0 digits to add on the right (normally 0) */
64*404b540aSrobert /* */
65*404b540aSrobert /* The coefficient must be known small enough to fit, and is filled */
66*404b540aSrobert /* in from the right (least significant first). Note that the full */
67*404b540aSrobert /* coefficient is copied, including the leading 'odd' digit. This */
68*404b540aSrobert /* digit is retrieved and packed into the combination field by the */
69*404b540aSrobert /* caller. */
70*404b540aSrobert /* */
71*404b540aSrobert /* shift is used for 'fold-down' padding. */
72*404b540aSrobert /* */
73*404b540aSrobert /* No error is possible. */
74*404b540aSrobert /* ------------------------------------------------------------------ */
75*404b540aSrobert void
decDensePackCoeff(const decNumber * dn,uByte * bytes,Int len,Int shift)76*404b540aSrobert decDensePackCoeff (const decNumber * dn, uByte * bytes, Int len, Int shift)
77*404b540aSrobert {
78*404b540aSrobert Int cut; /* work */
79*404b540aSrobert Int n; /* output bunch counter */
80*404b540aSrobert Int digits = dn->digits; /* digit countdown */
81*404b540aSrobert uInt dpd; /* densely packed decimal value */
82*404b540aSrobert uInt bin; /* binary value 0-999 */
83*404b540aSrobert uByte *bout; /* -> current output byte */
84*404b540aSrobert const Unit *inu = dn->lsu; /* -> current input unit */
85*404b540aSrobert Unit uar[DECMAXUNITS]; /* working copy of units, iff shifted */
86*404b540aSrobert #if DECDPUN!=3 /* not fast path */
87*404b540aSrobert Unit in; /* current input unit */
88*404b540aSrobert #endif
89*404b540aSrobert
90*404b540aSrobert if (shift != 0)
91*404b540aSrobert { /* shift towards most significant required */
92*404b540aSrobert /* shift the units array to the left by pad digits and copy */
93*404b540aSrobert /* [this code is a special case of decShiftToMost, which could */
94*404b540aSrobert /* be used instead if exposed and the array were copied first] */
95*404b540aSrobert Unit *target, *first; /* work */
96*404b540aSrobert const Unit *source; /* work */
97*404b540aSrobert uInt next = 0; /* work */
98*404b540aSrobert
99*404b540aSrobert source = dn->lsu + D2U (digits) - 1; /* where msu comes from */
100*404b540aSrobert first = uar + D2U (digits + shift) - 1; /* where msu will end up */
101*404b540aSrobert target = uar + D2U (digits) - 1 + D2U (shift); /* where upper part of first cut goes */
102*404b540aSrobert
103*404b540aSrobert cut = (DECDPUN - shift % DECDPUN) % DECDPUN;
104*404b540aSrobert for (; source >= dn->lsu; source--, target--)
105*404b540aSrobert {
106*404b540aSrobert /* split the source Unit and accumulate remainder for next */
107*404b540aSrobert uInt rem = *source % powers[cut];
108*404b540aSrobert next += *source / powers[cut];
109*404b540aSrobert if (target <= first)
110*404b540aSrobert *target = (Unit) next; /* write to target iff valid */
111*404b540aSrobert next = rem * powers[DECDPUN - cut]; /* save remainder for next Unit */
112*404b540aSrobert }
113*404b540aSrobert /* propagate remainder to one below and clear the rest */
114*404b540aSrobert for (; target >= uar; target--)
115*404b540aSrobert {
116*404b540aSrobert *target = (Unit) next;
117*404b540aSrobert next = 0;
118*404b540aSrobert }
119*404b540aSrobert digits += shift; /* add count (shift) of zeros added */
120*404b540aSrobert inu = uar; /* use units in working array */
121*404b540aSrobert }
122*404b540aSrobert
123*404b540aSrobert /* densely pack the coefficient into the byte array, starting from
124*404b540aSrobert the right (optionally padded) */
125*404b540aSrobert bout = &bytes[len - 1]; /* rightmost result byte for phase */
126*404b540aSrobert
127*404b540aSrobert #if DECDPUN!=3 /* not fast path */
128*404b540aSrobert in = *inu; /* prime */
129*404b540aSrobert cut = 0; /* at lowest digit */
130*404b540aSrobert bin = 0; /* [keep compiler quiet] */
131*404b540aSrobert #endif
132*404b540aSrobert
133*404b540aSrobert for (n = 0; digits > 0; n++)
134*404b540aSrobert { /* each output bunch */
135*404b540aSrobert #if DECDPUN==3 /* fast path, 3-at-a-time */
136*404b540aSrobert bin = *inu; /* 3 ready for convert */
137*404b540aSrobert digits -= 3; /* [may go negative] */
138*404b540aSrobert inu++; /* may need another */
139*404b540aSrobert
140*404b540aSrobert #else /* must collect digit-by-digit */
141*404b540aSrobert Unit dig; /* current digit */
142*404b540aSrobert Int j; /* digit-in-bunch count */
143*404b540aSrobert for (j = 0; j < 3; j++)
144*404b540aSrobert {
145*404b540aSrobert #if DECDPUN<=4
146*404b540aSrobert Unit temp = (Unit) ((uInt) (in * 6554) >> 16);
147*404b540aSrobert dig = (Unit) (in - X10 (temp));
148*404b540aSrobert in = temp;
149*404b540aSrobert #else
150*404b540aSrobert dig = in % 10;
151*404b540aSrobert in = in / 10;
152*404b540aSrobert #endif
153*404b540aSrobert
154*404b540aSrobert if (j == 0)
155*404b540aSrobert bin = dig;
156*404b540aSrobert else if (j == 1)
157*404b540aSrobert bin += X10 (dig);
158*404b540aSrobert else /* j==2 */
159*404b540aSrobert bin += X100 (dig);
160*404b540aSrobert
161*404b540aSrobert digits--;
162*404b540aSrobert if (digits == 0)
163*404b540aSrobert break; /* [also protects *inu below] */
164*404b540aSrobert cut++;
165*404b540aSrobert if (cut == DECDPUN)
166*404b540aSrobert {
167*404b540aSrobert inu++;
168*404b540aSrobert in = *inu;
169*404b540aSrobert cut = 0;
170*404b540aSrobert }
171*404b540aSrobert }
172*404b540aSrobert #endif
173*404b540aSrobert /* here we have 3 digits in bin, or have used all input digits */
174*404b540aSrobert
175*404b540aSrobert dpd = BIN2DPD[bin];
176*404b540aSrobert
177*404b540aSrobert /* write bunch (bcd) to byte array */
178*404b540aSrobert switch (n & 0x03)
179*404b540aSrobert { /* phase 0-3 */
180*404b540aSrobert case 0:
181*404b540aSrobert *bout = (uByte) dpd; /* [top 2 bits truncated] */
182*404b540aSrobert bout--;
183*404b540aSrobert *bout = (uByte) (dpd >> 8);
184*404b540aSrobert break;
185*404b540aSrobert case 1:
186*404b540aSrobert *bout |= (uByte) (dpd << 2);
187*404b540aSrobert bout--;
188*404b540aSrobert *bout = (uByte) (dpd >> 6);
189*404b540aSrobert break;
190*404b540aSrobert case 2:
191*404b540aSrobert *bout |= (uByte) (dpd << 4);
192*404b540aSrobert bout--;
193*404b540aSrobert *bout = (uByte) (dpd >> 4);
194*404b540aSrobert break;
195*404b540aSrobert case 3:
196*404b540aSrobert *bout |= (uByte) (dpd << 6);
197*404b540aSrobert bout--;
198*404b540aSrobert *bout = (uByte) (dpd >> 2);
199*404b540aSrobert bout--;
200*404b540aSrobert break;
201*404b540aSrobert } /* switch */
202*404b540aSrobert } /* n bunches */
203*404b540aSrobert return;
204*404b540aSrobert }
205*404b540aSrobert
206*404b540aSrobert /* ------------------------------------------------------------------ */
207*404b540aSrobert /* decDenseUnpackCoeff -- unpack a format's coefficient */
208*404b540aSrobert /* */
209*404b540aSrobert /* byte is the source's byte array */
210*404b540aSrobert /* len is length of the source's byte array */
211*404b540aSrobert /* dn is the target number, with 7, 16, or 34-digit space. */
212*404b540aSrobert /* bunches is the count of DPD groups in the decNumber (2, 5, or 11)*/
213*404b540aSrobert /* odd is 1 if there is a non-zero leading 10-bit group containing */
214*404b540aSrobert /* a single digit, 0 otherwise */
215*404b540aSrobert /* */
216*404b540aSrobert /* (This routine works on a copy of the number, if necessary, where */
217*404b540aSrobert /* an extra 10-bit group is prefixed to the coefficient continuation */
218*404b540aSrobert /* to hold the most significant digit if the latter is non-0.) */
219*404b540aSrobert /* */
220*404b540aSrobert /* dn->digits is set, but not the sign or exponent. */
221*404b540aSrobert /* No error is possible [the redundant 888 codes are allowed]. */
222*404b540aSrobert /* ------------------------------------------------------------------ */
223*404b540aSrobert void
decDenseUnpackCoeff(const uByte * bytes,Int len,decNumber * dn,Int bunches,Int odd)224*404b540aSrobert decDenseUnpackCoeff (const uByte * bytes, Int len, decNumber * dn,
225*404b540aSrobert Int bunches, Int odd)
226*404b540aSrobert {
227*404b540aSrobert uInt dpd = 0; /* collector for 10 bits */
228*404b540aSrobert Int n; /* counter */
229*404b540aSrobert const uByte *bin; /* -> current input byte */
230*404b540aSrobert Unit *uout = dn->lsu; /* -> current output unit */
231*404b540aSrobert Unit out = 0; /* accumulator */
232*404b540aSrobert Int cut = 0; /* power of ten in current unit */
233*404b540aSrobert Unit *last = uout; /* will be unit containing msd */
234*404b540aSrobert #if DECDPUN!=3
235*404b540aSrobert uInt bcd; /* BCD result */
236*404b540aSrobert uInt nibble; /* work */
237*404b540aSrobert #endif
238*404b540aSrobert
239*404b540aSrobert /* Expand the densely-packed integer, right to left */
240*404b540aSrobert bin = &bytes[len - 1]; /* next input byte to use */
241*404b540aSrobert for (n = 0; n < bunches + odd; n++)
242*404b540aSrobert { /* N bunches of 10 bits */
243*404b540aSrobert /* assemble the 10 bits */
244*404b540aSrobert switch (n & 0x03)
245*404b540aSrobert { /* phase 0-3 */
246*404b540aSrobert case 0:
247*404b540aSrobert dpd = *bin;
248*404b540aSrobert bin--;
249*404b540aSrobert dpd |= (*bin & 0x03) << 8;
250*404b540aSrobert break;
251*404b540aSrobert case 1:
252*404b540aSrobert dpd = (unsigned) *bin >> 2;
253*404b540aSrobert bin--;
254*404b540aSrobert dpd |= (*bin & 0x0F) << 6;
255*404b540aSrobert break;
256*404b540aSrobert case 2:
257*404b540aSrobert dpd = (unsigned) *bin >> 4;
258*404b540aSrobert bin--;
259*404b540aSrobert dpd |= (*bin & 0x3F) << 4;
260*404b540aSrobert break;
261*404b540aSrobert case 3:
262*404b540aSrobert dpd = (unsigned) *bin >> 6;
263*404b540aSrobert bin--;
264*404b540aSrobert dpd |= (*bin) << 2;
265*404b540aSrobert bin--;
266*404b540aSrobert break;
267*404b540aSrobert } /*switch */
268*404b540aSrobert
269*404b540aSrobert #if DECDPUN==3
270*404b540aSrobert if (dpd == 0)
271*404b540aSrobert *uout = 0;
272*404b540aSrobert else
273*404b540aSrobert {
274*404b540aSrobert *uout = DPD2BIN[dpd]; /* convert 10 bits to binary 0-999 */
275*404b540aSrobert last = uout; /* record most significant unit */
276*404b540aSrobert }
277*404b540aSrobert uout++;
278*404b540aSrobert
279*404b540aSrobert #else /* DECDPUN!=3 */
280*404b540aSrobert if (dpd == 0)
281*404b540aSrobert { /* fastpath [e.g., leading zeros] */
282*404b540aSrobert cut += 3;
283*404b540aSrobert for (; cut >= DECDPUN;)
284*404b540aSrobert {
285*404b540aSrobert cut -= DECDPUN;
286*404b540aSrobert *uout = out;
287*404b540aSrobert uout++;
288*404b540aSrobert out = 0;
289*404b540aSrobert }
290*404b540aSrobert continue;
291*404b540aSrobert }
292*404b540aSrobert bcd = DPD2BCD[dpd]; /* convert 10 bits to 12 bits BCD */
293*404b540aSrobert /* now split the 3 BCD nibbles into bytes, and accumulate into units */
294*404b540aSrobert /* If this is the last bunch and it is an odd one, we only have one */
295*404b540aSrobert /* nibble to handle [extras could overflow a Unit] */
296*404b540aSrobert nibble = bcd & 0x000f;
297*404b540aSrobert if (nibble)
298*404b540aSrobert {
299*404b540aSrobert last = uout;
300*404b540aSrobert out = (Unit) (out + nibble * powers[cut]);
301*404b540aSrobert }
302*404b540aSrobert cut++;
303*404b540aSrobert if (cut == DECDPUN)
304*404b540aSrobert {
305*404b540aSrobert *uout = out;
306*404b540aSrobert uout++;
307*404b540aSrobert cut = 0;
308*404b540aSrobert out = 0;
309*404b540aSrobert }
310*404b540aSrobert if (n < bunches)
311*404b540aSrobert {
312*404b540aSrobert nibble = bcd & 0x00f0;
313*404b540aSrobert if (nibble)
314*404b540aSrobert {
315*404b540aSrobert nibble >>= 4;
316*404b540aSrobert last = uout;
317*404b540aSrobert out = (Unit) (out + nibble * powers[cut]);
318*404b540aSrobert }
319*404b540aSrobert cut++;
320*404b540aSrobert if (cut == DECDPUN)
321*404b540aSrobert {
322*404b540aSrobert *uout = out;
323*404b540aSrobert uout++;
324*404b540aSrobert cut = 0;
325*404b540aSrobert out = 0;
326*404b540aSrobert }
327*404b540aSrobert nibble = bcd & 0x0f00;
328*404b540aSrobert if (nibble)
329*404b540aSrobert {
330*404b540aSrobert nibble >>= 8;
331*404b540aSrobert last = uout;
332*404b540aSrobert out = (Unit) (out + nibble * powers[cut]);
333*404b540aSrobert }
334*404b540aSrobert cut++;
335*404b540aSrobert if (cut == DECDPUN)
336*404b540aSrobert {
337*404b540aSrobert *uout = out;
338*404b540aSrobert uout++;
339*404b540aSrobert cut = 0;
340*404b540aSrobert out = 0;
341*404b540aSrobert }
342*404b540aSrobert }
343*404b540aSrobert #endif
344*404b540aSrobert } /* n */
345*404b540aSrobert if (cut != 0)
346*404b540aSrobert *uout = out; /* write out final unit */
347*404b540aSrobert
348*404b540aSrobert /* here, last points to the most significant unit with digits */
349*404b540aSrobert /* we need to inspect it to get final digits count */
350*404b540aSrobert dn->digits = (last - dn->lsu) * DECDPUN; /* floor of digits */
351*404b540aSrobert for (cut = 0; cut < DECDPUN; cut++)
352*404b540aSrobert {
353*404b540aSrobert if (*last < powers[cut])
354*404b540aSrobert break;
355*404b540aSrobert dn->digits++;
356*404b540aSrobert }
357*404b540aSrobert if (dn->digits == 0)
358*404b540aSrobert dn->digits++; /* zero has one digit */
359*404b540aSrobert return;
360*404b540aSrobert }
361