decPacked.c 9.9 KB

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