summaryrefslogtreecommitdiff
path: root/libdecnumber/decPacked.c
blob: 3e77bad13e8e6aa1ef67ddee2daf53c1d656be41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Packed decimal conversion module for the decNumber C Library.
   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
   Contributed by IBM Corporation.  Author Mike Cowlishaw.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   version.

   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */

/* ------------------------------------------------------------------ */
/* Packed Decimal conversion module				      */
/* ------------------------------------------------------------------ */
/* This module comprises the routines for Packed Decimal format       */
/* numbers.  Conversions are supplied to and from decNumber, which in */
/* turn supports:						      */
/*   conversions to and from string				      */
/*   arithmetic routines					      */
/*   utilities. 						      */
/* Conversions from decNumber to and from densely packed decimal      */
/* formats are provided by the decimal32 through decimal128 modules.  */
/* ------------------------------------------------------------------ */

#include <string.h>	      /* for NULL */
#include "decNumber.h"	      /* base number library */
#include "decPacked.h"	      /* packed decimal */
#include "decNumberLocal.h"   /* decNumber local types, etc. */

/* ------------------------------------------------------------------ */
/* decPackedFromNumber -- convert decNumber to BCD Packed Decimal     */
/*								      */
/*   bcd    is the BCD bytes					      */
/*   length is the length of the BCD array			      */
/*   scale  is the scale result 				      */
/*   dn     is the decNumber					      */
/*   returns bcd, or NULL if error				      */
/*								      */
/* The number is converted to a BCD packed decimal byte array,	      */
/* right aligned in the bcd array, whose length is indicated by the   */
/* second parameter.  The final 4-bit nibble in the array will be a   */
/* sign nibble, C (1100) for + and D (1101) for -.  Unused bytes and  */
/* nibbles to the left of the number are set to 0.		      */
/*								      */
/* scale is set to the scale of the number (this is the exponent,     */
/* negated).  To force the number to a specified scale, first use the */
/* decNumberRescale routine, which will round and change the exponent */
/* as necessary.						      */
/*								      */
/* If there is an error (that is, the decNumber has too many digits   */
/* to fit in length bytes, or it is a NaN or Infinity), NULL is       */
/* returned and the bcd and scale results are unchanged.  Otherwise   */
/* bcd is returned.						      */
/* ------------------------------------------------------------------ */
uByte * decPackedFromNumber(uByte *bcd, Int length, Int *scale,
			    const decNumber *dn) {
  const Unit *up=dn->lsu;     /* Unit array pointer */
  uByte obyte, *out;	      /* current output byte, and where it goes */
  Int indigs=dn->digits;      /* digits processed */
  uInt cut=DECDPUN;	      /* downcounter per Unit */
  uInt u=*up;		      /* work */
  uInt nib;		      /* .. */
  #if DECDPUN<=4
  uInt temp;		      /* .. */
  #endif

  if (dn->digits>length*2-1		     /* too long .. */
   ||(dn->bits & DECSPECIAL)) return NULL;   /* .. or special -- hopeless */

  if (dn->bits&DECNEG) obyte=DECPMINUS;      /* set the sign .. */
   else 	       obyte=DECPPLUS;
  *scale=-dn->exponent; 		     /* .. and scale */

  /* loop from lowest (rightmost) byte */
  out=bcd+length-1;			     /* -> final byte */
  for (; out>=bcd; out--) {
    if (indigs>0) {
      if (cut==0) {
	up++;
	u=*up;
	cut=DECDPUN;
	}
      #if DECDPUN<=4
	temp=(u*6554)>>16;	   /* fast /10 */
	nib=u-X10(temp);
	u=temp;
      #else
	nib=u%10;		   /* cannot use *6554 trick :-( */
	u=u/10;
      #endif
      obyte|=(nib<<4);
      indigs--;
      cut--;
      }
    *out=obyte;
    obyte=0;			   /* assume 0 */
    if (indigs>0) {
      if (cut==0) {
	up++;
	u=*up;
	cut=DECDPUN;
	}
      #if DECDPUN<=4
	temp=(u*6554)>>16;	   /* as above */
	obyte=(uByte)(u-X10(temp));
	u=temp;
      #else
	obyte=(uByte)(u%10);
	u=u/10;
      #endif
      indigs--;
      cut--;
      }
    } /* loop */

  return bcd;
  } /* decPackedFromNumber */

/* ------------------------------------------------------------------ */
/* decPackedToNumber -- convert BCD Packed Decimal to a decNumber     */
/*								      */
/*   bcd    is the BCD bytes					      */
/*   length is the length of the BCD array			      */
/*   scale  is the scale associated with the BCD integer	      */
/*   dn     is the decNumber [with space for length*2 digits]	      */
/*   returns dn, or NULL if error				      */
/*								      */
/* The BCD packed decimal byte array, together with an associated     */
/* scale, is converted to a decNumber.	The BCD array is assumed full */
/* of digits, and must be ended by a 4-bit sign nibble in the least   */
/* significant four bits of the final byte.			      */
/*								      */
/* The scale is used (negated) as the exponent of the decNumber.      */
/* Note that zeros may have a sign and/or a scale.		      */
/*								      */
/* The decNumber structure is assumed to have sufficient space to     */
/* hold the converted number (that is, up to length*2-1 digits), so   */
/* no error is possible unless the adjusted exponent is out of range, */
/* no sign nibble was found, or a sign nibble was found before the    */
/* final nibble.  In these error cases, NULL is returned and the      */
/* decNumber will be 0. 					      */
/* ------------------------------------------------------------------ */
decNumber * decPackedToNumber(const uByte *bcd, Int length,
			      const Int *scale, decNumber *dn) {
  const uByte *last=bcd+length-1;  /* -> last byte */
  const uByte *first;		   /* -> first non-zero byte */
  uInt	nib;			   /* work nibble */
  Unit	*up=dn->lsu;		   /* output pointer */
  Int	digits; 		   /* digits count */
  Int	cut=0;			   /* phase of output */

  decNumberZero(dn);		   /* default result */
  last=&bcd[length-1];
  nib=*last & 0x0f;		   /* get the sign */
  if (nib==DECPMINUS || nib==DECPMINUSALT) dn->bits=DECNEG;
   else if (nib<=9) return NULL;   /* not a sign nibble */

  /* skip leading zero bytes [final byte is always non-zero, due to sign] */
  for (first=bcd; *first==0;) first++;
  digits=(last-first)*2+1;		/* calculate digits .. */
  if ((*first & 0xf0)==0) digits--;	/* adjust for leading zero nibble */
  if (digits!=0) dn->digits=digits;	/* count of actual digits [if 0, */
					/* leave as 1] */

  /* check the adjusted exponent; note that scale could be unbounded */
  dn->exponent=-*scale; 		/* set the exponent */
  if (*scale>=0) {			/* usual case */
    if ((dn->digits-*scale-1)<-DECNUMMAXE) {	  /* underflow */
      decNumberZero(dn);
      return NULL;}
    }
   else { /* -ve scale; +ve exponent */
    /* need to be careful to avoid wrap, here, also BADINT case */
    if ((*scale<-DECNUMMAXE)		/* overflow even without digits */
	 || ((dn->digits-*scale-1)>DECNUMMAXE)) { /* overflow */
      decNumberZero(dn);
      return NULL;}
    }
  if (digits==0) return dn;		/* result was zero */

  /* copy the digits to the number's units, starting at the lsu */
  /* [unrolled] */
  for (;;) {				/* forever */
    /* left nibble first */
    nib=(unsigned)(*last & 0xf0)>>4;
    /* got a digit, in nib */
    if (nib>9) {decNumberZero(dn); return NULL;}

    if (cut==0) *up=(Unit)nib;
     else *up=(Unit)(*up+nib*DECPOWERS[cut]);
    digits--;
    if (digits==0) break;		/* got them all */
    cut++;
    if (cut==DECDPUN) {
      up++;
      cut=0;
      }
    last--;				/* ready for next */
    nib=*last & 0x0f;			/* get right nibble */
    if (nib>9) {decNumberZero(dn); return NULL;}

    /* got a digit, in nib */
    if (cut==0) *up=(Unit)nib;
     else *up=(Unit)(*up+nib*DECPOWERS[cut]);
    digits--;
    if (digits==0) break;		/* got them all */
    cut++;
    if (cut==DECDPUN) {
      up++;
      cut=0;
      }
    } /* forever */

  return dn;
  } /* decPackedToNumber */