OSDN Git Service

83f388665addd870c5bcc7c250f1fd4b33e4e425
[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 (const 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   const 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, *first;     /* work */
87       const Unit *source;       /* work */
88       uInt next = 0;            /* work */
89
90       source = dn->lsu + D2U (digits) - 1;      /* where msu comes from */
91       first = uar + D2U (digits + shift) - 1;   /* where msu will end up */
92       target = uar + D2U (digits) - 1 + D2U (shift);    /* where upper part of first cut goes */
93
94       cut = (DECDPUN - shift % DECDPUN) % DECDPUN;
95       for (; source >= dn->lsu; source--, target--)
96         {
97           /* split the source Unit and accumulate remainder for next */
98           uInt rem = *source % powers[cut];
99           next += *source / powers[cut];
100           if (target <= first)
101             *target = (Unit) next;      /* write to target iff valid */
102           next = rem * powers[DECDPUN - cut];   /* save remainder for next Unit */
103         }
104       /* propagate remainder to one below and clear the rest */
105       for (; target >= uar; target--)
106         {
107           *target = (Unit) next;
108           next = 0;
109         }
110       digits += shift;          /* add count (shift) of zeros added */
111       inu = uar;                /* use units in working array */
112     }
113
114   /* densely pack the coefficient into the byte array, starting from
115      the right (optionally padded) */
116   bout = &bytes[len - 1];       /* rightmost result byte for phase */
117
118 #if DECDPUN!=3                  /* not fast path */
119   in = *inu;                    /* prime */
120   cut = 0;                      /* at lowest digit */
121   bin = 0;                      /* [keep compiler quiet] */
122 #endif
123
124   for (n = 0; digits > 0; n++)
125     {                           /* each output bunch */
126 #if DECDPUN==3                  /* fast path, 3-at-a-time */
127       bin = *inu;               /* 3 ready for convert */
128       digits -= 3;              /* [may go negative] */
129       inu++;                    /* may need another */
130
131 #else /* must collect digit-by-digit */
132       Unit dig;                 /* current digit */
133       Int j;                    /* digit-in-bunch count */
134       for (j = 0; j < 3; j++)
135         {
136 #if DECDPUN<=4
137           Unit temp = (Unit) ((uInt) (in * 6554) >> 16);
138           dig = (Unit) (in - X10 (temp));
139           in = temp;
140 #else
141           dig = in % 10;
142           in = in / 10;
143 #endif
144
145           if (j == 0)
146             bin = dig;
147           else if (j == 1)
148             bin += X10 (dig);
149           else                  /* j==2 */
150             bin += X100 (dig);
151
152           digits--;
153           if (digits == 0)
154             break;              /* [also protects *inu below] */
155           cut++;
156           if (cut == DECDPUN)
157             {
158               inu++;
159               in = *inu;
160               cut = 0;
161             }
162         }
163 #endif
164       /* here we have 3 digits in bin, or have used all input digits */
165
166       dpd = BIN2DPD[bin];
167
168       /* write bunch (bcd) to byte array */
169       switch (n & 0x03)
170         {                       /* phase 0-3 */
171         case 0:
172           *bout = (uByte) dpd;  /* [top 2 bits truncated] */
173           bout--;
174           *bout = (uByte) (dpd >> 8);
175           break;
176         case 1:
177           *bout |= (uByte) (dpd << 2);
178           bout--;
179           *bout = (uByte) (dpd >> 6);
180           break;
181         case 2:
182           *bout |= (uByte) (dpd << 4);
183           bout--;
184           *bout = (uByte) (dpd >> 4);
185           break;
186         case 3:
187           *bout |= (uByte) (dpd << 6);
188           bout--;
189           *bout = (uByte) (dpd >> 2);
190           bout--;
191           break;
192         }                       /* switch */
193     }                           /* n bunches */
194   return;
195 }
196
197 /* ------------------------------------------------------------------ */
198 /* decDenseUnpackCoeff -- unpack a format's coefficient               */
199 /*                                                                    */
200 /*   byte is the source's byte array                                  */
201 /*   len is length of the source's byte array                         */
202 /*   dn is the target number, with 7, 16, or 34-digit space.          */
203 /*   bunches is the count of DPD groups in the decNumber (2, 5, or 11)*/
204 /*   odd is 1 if there is a non-zero leading 10-bit group containing  */
205 /*     a single digit, 0 otherwise                                    */
206 /*                                                                    */
207 /* (This routine works on a copy of the number, if necessary, where   */
208 /* an extra 10-bit group is prefixed to the coefficient continuation  */
209 /* to hold the most significant digit if the latter is non-0.)        */
210 /*                                                                    */
211 /* dn->digits is set, but not the sign or exponent.                   */
212 /* No error is possible [the redundant 888 codes are allowed].        */
213 /* ------------------------------------------------------------------ */
214 void
215 decDenseUnpackCoeff (const uByte * bytes, Int len, decNumber * dn,
216                      Int bunches, Int odd)
217 {
218   uInt dpd = 0;                 /* collector for 10 bits */
219   Int n;                        /* counter */
220   const uByte *bin;             /* -> current input byte */
221   Unit *uout = dn->lsu;         /* -> current output unit */
222   Unit out = 0;                 /* accumulator */
223   Int cut = 0;                  /* power of ten in current unit */
224   Unit *last = uout;            /* will be unit containing msd */
225 #if DECDPUN!=3
226   uInt bcd;                     /* BCD result */
227   uInt nibble;                  /* work */
228 #endif
229
230   /* Expand the densely-packed integer, right to left */
231   bin = &bytes[len - 1];        /* next input byte to use */
232   for (n = 0; n < bunches + odd; n++)
233     {                           /* N bunches of 10 bits */
234       /* assemble the 10 bits */
235       switch (n & 0x03)
236         {                       /* phase 0-3 */
237         case 0:
238           dpd = *bin;
239           bin--;
240           dpd |= (*bin & 0x03) << 8;
241           break;
242         case 1:
243           dpd = (unsigned) *bin >> 2;
244           bin--;
245           dpd |= (*bin & 0x0F) << 6;
246           break;
247         case 2:
248           dpd = (unsigned) *bin >> 4;
249           bin--;
250           dpd |= (*bin & 0x3F) << 4;
251           break;
252         case 3:
253           dpd = (unsigned) *bin >> 6;
254           bin--;
255           dpd |= (*bin) << 2;
256           bin--;
257           break;
258         }                       /*switch */
259
260 #if DECDPUN==3
261       if (dpd == 0)
262         *uout = 0;
263       else
264         {
265           *uout = DPD2BIN[dpd]; /* convert 10 bits to binary 0-999 */
266           last = uout;          /* record most significant unit */
267         }
268       uout++;
269
270 #else /* DECDPUN!=3 */
271       if (dpd == 0)
272         {                       /* fastpath [e.g., leading zeros] */
273           cut += 3;
274           for (; cut >= DECDPUN;)
275             {
276               cut -= DECDPUN;
277               *uout = out;
278               uout++;
279               out = 0;
280             }
281           continue;
282         }
283       bcd = DPD2BCD[dpd];       /* convert 10 bits to 12 bits BCD */
284       /* now split the 3 BCD nibbles into bytes, and accumulate into units */
285       /* If this is the last bunch and it is an odd one, we only have one */
286       /* nibble to handle [extras could overflow a Unit] */
287       nibble = bcd & 0x000f;
288       if (nibble)
289         {
290           last = uout;
291           out = (Unit) (out + nibble * powers[cut]);
292         }
293       cut++;
294       if (cut == DECDPUN)
295         {
296           *uout = out;
297           uout++;
298           cut = 0;
299           out = 0;
300         }
301       if (n < bunches)
302         {
303           nibble = bcd & 0x00f0;
304           if (nibble)
305             {
306               nibble >>= 4;
307               last = uout;
308               out = (Unit) (out + nibble * powers[cut]);
309             }
310           cut++;
311           if (cut == DECDPUN)
312             {
313               *uout = out;
314               uout++;
315               cut = 0;
316               out = 0;
317             }
318           nibble = bcd & 0x0f00;
319           if (nibble)
320             {
321               nibble >>= 8;
322               last = uout;
323               out = (Unit) (out + nibble * powers[cut]);
324             }
325           cut++;
326           if (cut == DECDPUN)
327             {
328               *uout = out;
329               uout++;
330               cut = 0;
331               out = 0;
332             }
333         }
334 #endif
335     }                           /* n */
336   if (cut != 0)
337     *uout = out;                /* write out final unit */
338
339   /* here, last points to the most significant unit with digits */
340   /* we need to inspect it to get final digits count */
341   dn->digits = (last - dn->lsu) * DECDPUN;      /* floor of digits */
342   for (cut = 0; cut < DECDPUN; cut++)
343     {
344       if (*last < powers[cut])
345         break;
346       dn->digits++;
347     }
348   if (dn->digits == 0)
349     dn->digits++;               /* zero has one digit */
350   return;
351 }