OSDN Git Service

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