OSDN Git Service

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