decPacked.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Packed decimal conversion module for the decNumber C Library.
  2. Copyright (C) 2007-2013 Free Software Foundation, Inc.
  3. Contributed by IBM Corporation. Author Mike Cowlishaw.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* ------------------------------------------------------------------ */
  21. /* Packed Decimal conversion module */
  22. /* ------------------------------------------------------------------ */
  23. /* This module comprises the routines for Packed Decimal format */
  24. /* numbers. Conversions are supplied to and from decNumber, which in */
  25. /* turn supports: */
  26. /* conversions to and from string */
  27. /* arithmetic routines */
  28. /* utilities. */
  29. /* Conversions from decNumber to and from densely packed decimal */
  30. /* formats are provided by the decimal32 through decimal128 modules. */
  31. /* ------------------------------------------------------------------ */
  32. #include <string.h> /* for NULL */
  33. #include "decNumber.h" /* base number library */
  34. #include "decPacked.h" /* packed decimal */
  35. #include "decNumberLocal.h" /* decNumber local types, etc. */
  36. /* ------------------------------------------------------------------ */
  37. /* decPackedFromNumber -- convert decNumber to BCD Packed Decimal */
  38. /* */
  39. /* bcd is the BCD bytes */
  40. /* length is the length of the BCD array */
  41. /* scale is the scale result */
  42. /* dn is the decNumber */
  43. /* returns bcd, or NULL if error */
  44. /* */
  45. /* The number is converted to a BCD packed decimal byte array, */
  46. /* right aligned in the bcd array, whose length is indicated by the */
  47. /* second parameter. The final 4-bit nibble in the array will be a */
  48. /* sign nibble, C (1100) for + and D (1101) for -. Unused bytes and */
  49. /* nibbles to the left of the number are set to 0. */
  50. /* */
  51. /* scale is set to the scale of the number (this is the exponent, */
  52. /* negated). To force the number to a specified scale, first use the */
  53. /* decNumberRescale routine, which will round and change the exponent */
  54. /* as necessary. */
  55. /* */
  56. /* If there is an error (that is, the decNumber has too many digits */
  57. /* to fit in length bytes, or it is a NaN or Infinity), NULL is */
  58. /* returned and the bcd and scale results are unchanged. Otherwise */
  59. /* bcd is returned. */
  60. /* ------------------------------------------------------------------ */
  61. uByte * decPackedFromNumber(uByte *bcd, Int length, Int *scale,
  62. const decNumber *dn) {
  63. const Unit *up=dn->lsu; /* Unit array pointer */
  64. uByte obyte, *out; /* current output byte, and where it goes */
  65. Int indigs=dn->digits; /* digits processed */
  66. uInt cut=DECDPUN; /* downcounter per Unit */
  67. uInt u=*up; /* work */
  68. uInt nib; /* .. */
  69. #if DECDPUN<=4
  70. uInt temp; /* .. */
  71. #endif
  72. if (dn->digits>length*2-1 /* too long .. */
  73. ||(dn->bits & DECSPECIAL)) return NULL; /* .. or special -- hopeless */
  74. if (dn->bits&DECNEG) obyte=DECPMINUS; /* set the sign .. */
  75. else obyte=DECPPLUS;
  76. *scale=-dn->exponent; /* .. and scale */
  77. /* loop from lowest (rightmost) byte */
  78. out=bcd+length-1; /* -> final byte */
  79. for (; out>=bcd; out--) {
  80. if (indigs>0) {
  81. if (cut==0) {
  82. up++;
  83. u=*up;
  84. cut=DECDPUN;
  85. }
  86. #if DECDPUN<=4
  87. temp=(u*6554)>>16; /* fast /10 */
  88. nib=u-X10(temp);
  89. u=temp;
  90. #else
  91. nib=u%10; /* cannot use *6554 trick :-( */
  92. u=u/10;
  93. #endif
  94. obyte|=(nib<<4);
  95. indigs--;
  96. cut--;
  97. }
  98. *out=obyte;
  99. obyte=0; /* assume 0 */
  100. if (indigs>0) {
  101. if (cut==0) {
  102. up++;
  103. u=*up;
  104. cut=DECDPUN;
  105. }
  106. #if DECDPUN<=4
  107. temp=(u*6554)>>16; /* as above */
  108. obyte=(uByte)(u-X10(temp));
  109. u=temp;
  110. #else
  111. obyte=(uByte)(u%10);
  112. u=u/10;
  113. #endif
  114. indigs--;
  115. cut--;
  116. }
  117. } /* loop */
  118. return bcd;
  119. } /* decPackedFromNumber */
  120. /* ------------------------------------------------------------------ */
  121. /* decPackedToNumber -- convert BCD Packed Decimal to a decNumber */
  122. /* */
  123. /* bcd is the BCD bytes */
  124. /* length is the length of the BCD array */
  125. /* scale is the scale associated with the BCD integer */
  126. /* dn is the decNumber [with space for length*2 digits] */
  127. /* returns dn, or NULL if error */
  128. /* */
  129. /* The BCD packed decimal byte array, together with an associated */
  130. /* scale, is converted to a decNumber. The BCD array is assumed full */
  131. /* of digits, and must be ended by a 4-bit sign nibble in the least */
  132. /* significant four bits of the final byte. */
  133. /* */
  134. /* The scale is used (negated) as the exponent of the decNumber. */
  135. /* Note that zeros may have a sign and/or a scale. */
  136. /* */
  137. /* The decNumber structure is assumed to have sufficient space to */
  138. /* hold the converted number (that is, up to length*2-1 digits), so */
  139. /* no error is possible unless the adjusted exponent is out of range, */
  140. /* no sign nibble was found, or a sign nibble was found before the */
  141. /* final nibble. In these error cases, NULL is returned and the */
  142. /* decNumber will be 0. */
  143. /* ------------------------------------------------------------------ */
  144. decNumber * decPackedToNumber(const uByte *bcd, Int length,
  145. const Int *scale, decNumber *dn) {
  146. const uByte *last=bcd+length-1; /* -> last byte */
  147. const uByte *first; /* -> first non-zero byte */
  148. uInt nib; /* work nibble */
  149. Unit *up=dn->lsu; /* output pointer */
  150. Int digits; /* digits count */
  151. Int cut=0; /* phase of output */
  152. decNumberZero(dn); /* default result */
  153. last=&bcd[length-1];
  154. nib=*last & 0x0f; /* get the sign */
  155. if (nib==DECPMINUS || nib==DECPMINUSALT) dn->bits=DECNEG;
  156. else if (nib<=9) return NULL; /* not a sign nibble */
  157. /* skip leading zero bytes [final byte is always non-zero, due to sign] */
  158. for (first=bcd; *first==0;) first++;
  159. digits=(last-first)*2+1; /* calculate digits .. */
  160. if ((*first & 0xf0)==0) digits--; /* adjust for leading zero nibble */
  161. if (digits!=0) dn->digits=digits; /* count of actual digits [if 0, */
  162. /* leave as 1] */
  163. /* check the adjusted exponent; note that scale could be unbounded */
  164. dn->exponent=-*scale; /* set the exponent */
  165. if (*scale>=0) { /* usual case */
  166. if ((dn->digits-*scale-1)<-DECNUMMAXE) { /* underflow */
  167. decNumberZero(dn);
  168. return NULL;}
  169. }
  170. else { /* -ve scale; +ve exponent */
  171. /* need to be careful to avoid wrap, here, also BADINT case */
  172. if ((*scale<-DECNUMMAXE) /* overflow even without digits */
  173. || ((dn->digits-*scale-1)>DECNUMMAXE)) { /* overflow */
  174. decNumberZero(dn);
  175. return NULL;}
  176. }
  177. if (digits==0) return dn; /* result was zero */
  178. /* copy the digits to the number's units, starting at the lsu */
  179. /* [unrolled] */
  180. for (;;) { /* forever */
  181. /* left nibble first */
  182. nib=(unsigned)(*last & 0xf0)>>4;
  183. /* got a digit, in nib */
  184. if (nib>9) {decNumberZero(dn); return NULL;}
  185. if (cut==0) *up=(Unit)nib;
  186. else *up=(Unit)(*up+nib*DECPOWERS[cut]);
  187. digits--;
  188. if (digits==0) break; /* got them all */
  189. cut++;
  190. if (cut==DECDPUN) {
  191. up++;
  192. cut=0;
  193. }
  194. last--; /* ready for next */
  195. nib=*last & 0x0f; /* get right nibble */
  196. if (nib>9) {decNumberZero(dn); return NULL;}
  197. /* got a digit, in nib */
  198. if (cut==0) *up=(Unit)nib;
  199. else *up=(Unit)(*up+nib*DECPOWERS[cut]);
  200. digits--;
  201. if (digits==0) break; /* got them all */
  202. cut++;
  203. if (cut==DECDPUN) {
  204. up++;
  205. cut=0;
  206. }
  207. } /* forever */
  208. return dn;
  209. } /* decPackedToNumber */