decimal32.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* Decimal 32-bit format module for the decNumber C Library.
  2. Copyright (C) 2005-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. /* Decimal 32-bit format module */
  22. /* ------------------------------------------------------------------ */
  23. /* This module comprises the routines for decimal32 format numbers. */
  24. /* Conversions are supplied to and from decNumber and String. */
  25. /* */
  26. /* This is used when decNumber provides operations, either for all */
  27. /* operations or as a proxy between decNumber and decSingle. */
  28. /* */
  29. /* Error handling is the same as decNumber (qv.). */
  30. /* ------------------------------------------------------------------ */
  31. #include <string.h> /* [for memset/memcpy] */
  32. #include <stdio.h> /* [for printf] */
  33. #include "dconfig.h" /* GCC definitions */
  34. #define DECNUMDIGITS 7 /* make decNumbers with space for 7 */
  35. #include "decNumber.h" /* base number library */
  36. #include "decNumberLocal.h" /* decNumber local types, etc. */
  37. #include "decimal32.h" /* our primary include */
  38. /* Utility tables and routines [in decimal64.c] */
  39. extern const uInt COMBEXP[32], COMBMSD[32];
  40. extern const uShort DPD2BIN[1024];
  41. extern const uShort BIN2DPD[1000];
  42. extern const uByte BIN2CHAR[4001];
  43. extern void decDigitsToDPD(const decNumber *, uInt *, Int);
  44. extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
  45. #if DECTRACE || DECCHECK
  46. void decimal32Show(const decimal32 *); /* for debug */
  47. extern void decNumberShow(const decNumber *); /* .. */
  48. #endif
  49. /* Useful macro */
  50. /* Clear a structure (e.g., a decNumber) */
  51. #define DEC_clear(d) memset(d, 0, sizeof(*d))
  52. /* ------------------------------------------------------------------ */
  53. /* decimal32FromNumber -- convert decNumber to decimal32 */
  54. /* */
  55. /* ds is the target decimal32 */
  56. /* dn is the source number (assumed valid) */
  57. /* set is the context, used only for reporting errors */
  58. /* */
  59. /* The set argument is used only for status reporting and for the */
  60. /* rounding mode (used if the coefficient is more than DECIMAL32_Pmax */
  61. /* digits or an overflow is detected). If the exponent is out of the */
  62. /* valid range then Overflow or Underflow will be raised. */
  63. /* After Underflow a subnormal result is possible. */
  64. /* */
  65. /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
  66. /* by reducing its exponent and multiplying the coefficient by a */
  67. /* power of ten, or if the exponent on a zero had to be clamped. */
  68. /* ------------------------------------------------------------------ */
  69. decimal32 * decimal32FromNumber(decimal32 *d32, const decNumber *dn,
  70. decContext *set) {
  71. uInt status=0; /* status accumulator */
  72. Int ae; /* adjusted exponent */
  73. decNumber dw; /* work */
  74. decContext dc; /* .. */
  75. uInt comb, exp; /* .. */
  76. uInt uiwork; /* for macros */
  77. uInt targ=0; /* target 32-bit */
  78. /* If the number has too many digits, or the exponent could be */
  79. /* out of range then reduce the number under the appropriate */
  80. /* constraints. This could push the number to Infinity or zero, */
  81. /* so this check and rounding must be done before generating the */
  82. /* decimal32] */
  83. ae=dn->exponent+dn->digits-1; /* [0 if special] */
  84. if (dn->digits>DECIMAL32_Pmax /* too many digits */
  85. || ae>DECIMAL32_Emax /* likely overflow */
  86. || ae<DECIMAL32_Emin) { /* likely underflow */
  87. decContextDefault(&dc, DEC_INIT_DECIMAL32); /* [no traps] */
  88. dc.round=set->round; /* use supplied rounding */
  89. decNumberPlus(&dw, dn, &dc); /* (round and check) */
  90. /* [this changes -0 to 0, so enforce the sign...] */
  91. dw.bits|=dn->bits&DECNEG;
  92. status=dc.status; /* save status */
  93. dn=&dw; /* use the work number */
  94. } /* maybe out of range */
  95. if (dn->bits&DECSPECIAL) { /* a special value */
  96. if (dn->bits&DECINF) targ=DECIMAL_Inf<<24;
  97. else { /* sNaN or qNaN */
  98. if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
  99. && (dn->digits<DECIMAL32_Pmax)) { /* coefficient fits */
  100. decDigitsToDPD(dn, &targ, 0);
  101. }
  102. if (dn->bits&DECNAN) targ|=DECIMAL_NaN<<24;
  103. else targ|=DECIMAL_sNaN<<24;
  104. } /* a NaN */
  105. } /* special */
  106. else { /* is finite */
  107. if (decNumberIsZero(dn)) { /* is a zero */
  108. /* set and clamp exponent */
  109. if (dn->exponent<-DECIMAL32_Bias) {
  110. exp=0; /* low clamp */
  111. status|=DEC_Clamped;
  112. }
  113. else {
  114. exp=dn->exponent+DECIMAL32_Bias; /* bias exponent */
  115. if (exp>DECIMAL32_Ehigh) { /* top clamp */
  116. exp=DECIMAL32_Ehigh;
  117. status|=DEC_Clamped;
  118. }
  119. }
  120. comb=(exp>>3) & 0x18; /* msd=0, exp top 2 bits .. */
  121. }
  122. else { /* non-zero finite number */
  123. uInt msd; /* work */
  124. Int pad=0; /* coefficient pad digits */
  125. /* the dn is known to fit, but it may need to be padded */
  126. exp=(uInt)(dn->exponent+DECIMAL32_Bias); /* bias exponent */
  127. if (exp>DECIMAL32_Ehigh) { /* fold-down case */
  128. pad=exp-DECIMAL32_Ehigh;
  129. exp=DECIMAL32_Ehigh; /* [to maximum] */
  130. status|=DEC_Clamped;
  131. }
  132. /* fastpath common case */
  133. if (DECDPUN==3 && pad==0) {
  134. targ=BIN2DPD[dn->lsu[0]];
  135. if (dn->digits>3) targ|=(uInt)(BIN2DPD[dn->lsu[1]])<<10;
  136. msd=(dn->digits==7 ? dn->lsu[2] : 0);
  137. }
  138. else { /* general case */
  139. decDigitsToDPD(dn, &targ, pad);
  140. /* save and clear the top digit */
  141. msd=targ>>20;
  142. targ&=0x000fffff;
  143. }
  144. /* create the combination field */
  145. if (msd>=8) comb=0x18 | ((exp>>5) & 0x06) | (msd & 0x01);
  146. else comb=((exp>>3) & 0x18) | msd;
  147. }
  148. targ|=comb<<26; /* add combination field .. */
  149. targ|=(exp&0x3f)<<20; /* .. and exponent continuation */
  150. } /* finite */
  151. if (dn->bits&DECNEG) targ|=0x80000000; /* add sign bit */
  152. /* now write to storage; this is endian */
  153. UBFROMUI(d32->bytes, targ); /* directly store the int */
  154. if (status!=0) decContextSetStatus(set, status); /* pass on status */
  155. /* decimal32Show(d32); */
  156. return d32;
  157. } /* decimal32FromNumber */
  158. /* ------------------------------------------------------------------ */
  159. /* decimal32ToNumber -- convert decimal32 to decNumber */
  160. /* d32 is the source decimal32 */
  161. /* dn is the target number, with appropriate space */
  162. /* No error is possible. */
  163. /* ------------------------------------------------------------------ */
  164. decNumber * decimal32ToNumber(const decimal32 *d32, decNumber *dn) {
  165. uInt msd; /* coefficient MSD */
  166. uInt exp; /* exponent top two bits */
  167. uInt comb; /* combination field */
  168. uInt sour; /* source 32-bit */
  169. uInt uiwork; /* for macros */
  170. /* load source from storage; this is endian */
  171. sour=UBTOUI(d32->bytes); /* directly load the int */
  172. comb=(sour>>26)&0x1f; /* combination field */
  173. decNumberZero(dn); /* clean number */
  174. if (sour&0x80000000) dn->bits=DECNEG; /* set sign if negative */
  175. msd=COMBMSD[comb]; /* decode the combination field */
  176. exp=COMBEXP[comb]; /* .. */
  177. if (exp==3) { /* is a special */
  178. if (msd==0) {
  179. dn->bits|=DECINF;
  180. return dn; /* no coefficient needed */
  181. }
  182. else if (sour&0x02000000) dn->bits|=DECSNAN;
  183. else dn->bits|=DECNAN;
  184. msd=0; /* no top digit */
  185. }
  186. else { /* is a finite number */
  187. dn->exponent=(exp<<6)+((sour>>20)&0x3f)-DECIMAL32_Bias; /* unbiased */
  188. }
  189. /* get the coefficient */
  190. sour&=0x000fffff; /* clean coefficient continuation */
  191. if (msd) { /* non-zero msd */
  192. sour|=msd<<20; /* prefix to coefficient */
  193. decDigitsFromDPD(dn, &sour, 3); /* process 3 declets */
  194. return dn;
  195. }
  196. /* msd=0 */
  197. if (!sour) return dn; /* easy: coefficient is 0 */
  198. if (sour&0x000ffc00) /* need 2 declets? */
  199. decDigitsFromDPD(dn, &sour, 2); /* process 2 declets */
  200. else
  201. decDigitsFromDPD(dn, &sour, 1); /* process 1 declet */
  202. return dn;
  203. } /* decimal32ToNumber */
  204. /* ------------------------------------------------------------------ */
  205. /* to-scientific-string -- conversion to numeric string */
  206. /* to-engineering-string -- conversion to numeric string */
  207. /* */
  208. /* decimal32ToString(d32, string); */
  209. /* decimal32ToEngString(d32, string); */
  210. /* */
  211. /* d32 is the decimal32 format number to convert */
  212. /* string is the string where the result will be laid out */
  213. /* */
  214. /* string must be at least 24 characters */
  215. /* */
  216. /* No error is possible, and no status can be set. */
  217. /* ------------------------------------------------------------------ */
  218. char * decimal32ToEngString(const decimal32 *d32, char *string){
  219. decNumber dn; /* work */
  220. decimal32ToNumber(d32, &dn);
  221. decNumberToEngString(&dn, string);
  222. return string;
  223. } /* decimal32ToEngString */
  224. char * decimal32ToString(const decimal32 *d32, char *string){
  225. uInt msd; /* coefficient MSD */
  226. Int exp; /* exponent top two bits or full */
  227. uInt comb; /* combination field */
  228. char *cstart; /* coefficient start */
  229. char *c; /* output pointer in string */
  230. const uByte *u; /* work */
  231. char *s, *t; /* .. (source, target) */
  232. Int dpd; /* .. */
  233. Int pre, e; /* .. */
  234. uInt uiwork; /* for macros */
  235. uInt sour; /* source 32-bit */
  236. /* load source from storage; this is endian */
  237. sour=UBTOUI(d32->bytes); /* directly load the int */
  238. c=string; /* where result will go */
  239. if (((Int)sour)<0) *c++='-'; /* handle sign */
  240. comb=(sour>>26)&0x1f; /* combination field */
  241. msd=COMBMSD[comb]; /* decode the combination field */
  242. exp=COMBEXP[comb]; /* .. */
  243. if (exp==3) {
  244. if (msd==0) { /* infinity */
  245. strcpy(c, "Inf");
  246. strcpy(c+3, "inity");
  247. return string; /* easy */
  248. }
  249. if (sour&0x02000000) *c++='s'; /* sNaN */
  250. strcpy(c, "NaN"); /* complete word */
  251. c+=3; /* step past */
  252. if ((sour&0x000fffff)==0) return string; /* zero payload */
  253. /* otherwise drop through to add integer; set correct exp */
  254. exp=0; msd=0; /* setup for following code */
  255. }
  256. else exp=(exp<<6)+((sour>>20)&0x3f)-DECIMAL32_Bias; /* unbiased */
  257. /* convert 7 digits of significand to characters */
  258. cstart=c; /* save start of coefficient */
  259. if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
  260. /* Now decode the declets. After extracting each one, it is */
  261. /* decoded to binary and then to a 4-char sequence by table lookup; */
  262. /* the 4-chars are a 1-char length (significant digits, except 000 */
  263. /* has length 0). This allows us to left-align the first declet */
  264. /* with non-zero content, then remaining ones are full 3-char */
  265. /* length. We use fixed-length memcpys because variable-length */
  266. /* causes a subroutine call in GCC. (These are length 4 for speed */
  267. /* and are safe because the array has an extra terminator byte.) */
  268. #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
  269. if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
  270. else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
  271. dpd=(sour>>10)&0x3ff; /* declet 1 */
  272. dpd2char;
  273. dpd=(sour)&0x3ff; /* declet 2 */
  274. dpd2char;
  275. if (c==cstart) *c++='0'; /* all zeros -- make 0 */
  276. if (exp==0) { /* integer or NaN case -- easy */
  277. *c='\0'; /* terminate */
  278. return string;
  279. }
  280. /* non-0 exponent */
  281. e=0; /* assume no E */
  282. pre=c-cstart+exp;
  283. /* [here, pre-exp is the digits count (==1 for zero)] */
  284. if (exp>0 || pre<-5) { /* need exponential form */
  285. e=pre-1; /* calculate E value */
  286. pre=1; /* assume one digit before '.' */
  287. } /* exponential form */
  288. /* modify the coefficient, adding 0s, '.', and E+nn as needed */
  289. s=c-1; /* source (LSD) */
  290. if (pre>0) { /* ddd.ddd (plain), perhaps with E */
  291. char *dotat=cstart+pre;
  292. if (dotat<c) { /* if embedded dot needed... */
  293. t=c; /* target */
  294. for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
  295. *t='.'; /* insert the dot */
  296. c++; /* length increased by one */
  297. }
  298. /* finally add the E-part, if needed; it will never be 0, and has */
  299. /* a maximum length of 3 digits (E-101 case) */
  300. if (e!=0) {
  301. *c++='E'; /* starts with E */
  302. *c++='+'; /* assume positive */
  303. if (e<0) {
  304. *(c-1)='-'; /* oops, need '-' */
  305. e=-e; /* uInt, please */
  306. }
  307. u=&BIN2CHAR[e*4]; /* -> length byte */
  308. memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
  309. c+=*u; /* bump pointer appropriately */
  310. }
  311. *c='\0'; /* add terminator */
  312. /*printf("res %s\n", string); */
  313. return string;
  314. } /* pre>0 */
  315. /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
  316. t=c+1-pre;
  317. *(t+1)='\0'; /* can add terminator now */
  318. for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
  319. c=cstart;
  320. *c++='0'; /* always starts with 0. */
  321. *c++='.';
  322. for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
  323. /*printf("res %s\n", string); */
  324. return string;
  325. } /* decimal32ToString */
  326. /* ------------------------------------------------------------------ */
  327. /* to-number -- conversion from numeric string */
  328. /* */
  329. /* decimal32FromString(result, string, set); */
  330. /* */
  331. /* result is the decimal32 format number which gets the result of */
  332. /* the conversion */
  333. /* *string is the character string which should contain a valid */
  334. /* number (which may be a special value) */
  335. /* set is the context */
  336. /* */
  337. /* The context is supplied to this routine is used for error handling */
  338. /* (setting of status and traps) and for the rounding mode, only. */
  339. /* If an error occurs, the result will be a valid decimal32 NaN. */
  340. /* ------------------------------------------------------------------ */
  341. decimal32 * decimal32FromString(decimal32 *result, const char *string,
  342. decContext *set) {
  343. decContext dc; /* work */
  344. decNumber dn; /* .. */
  345. decContextDefault(&dc, DEC_INIT_DECIMAL32); /* no traps, please */
  346. dc.round=set->round; /* use supplied rounding */
  347. decNumberFromString(&dn, string, &dc); /* will round if needed */
  348. decimal32FromNumber(result, &dn, &dc);
  349. if (dc.status!=0) { /* something happened */
  350. decContextSetStatus(set, dc.status); /* .. pass it on */
  351. }
  352. return result;
  353. } /* decimal32FromString */
  354. /* ------------------------------------------------------------------ */
  355. /* decimal32IsCanonical -- test whether encoding is canonical */
  356. /* d32 is the source decimal32 */
  357. /* returns 1 if the encoding of d32 is canonical, 0 otherwise */
  358. /* No error is possible. */
  359. /* ------------------------------------------------------------------ */
  360. uInt decimal32IsCanonical(const decimal32 *d32) {
  361. decNumber dn; /* work */
  362. decimal32 canon; /* .. */
  363. decContext dc; /* .. */
  364. decContextDefault(&dc, DEC_INIT_DECIMAL32);
  365. decimal32ToNumber(d32, &dn);
  366. decimal32FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
  367. return memcmp(d32, &canon, DECIMAL32_Bytes)==0;
  368. } /* decimal32IsCanonical */
  369. /* ------------------------------------------------------------------ */
  370. /* decimal32Canonical -- copy an encoding, ensuring it is canonical */
  371. /* d32 is the source decimal32 */
  372. /* result is the target (may be the same decimal32) */
  373. /* returns result */
  374. /* No error is possible. */
  375. /* ------------------------------------------------------------------ */
  376. decimal32 * decimal32Canonical(decimal32 *result, const decimal32 *d32) {
  377. decNumber dn; /* work */
  378. decContext dc; /* .. */
  379. decContextDefault(&dc, DEC_INIT_DECIMAL32);
  380. decimal32ToNumber(d32, &dn);
  381. decimal32FromNumber(result, &dn, &dc);/* result will now be canonical */
  382. return result;
  383. } /* decimal32Canonical */
  384. #if DECTRACE || DECCHECK
  385. /* Macros for accessing decimal32 fields. These assume the argument
  386. is a reference (pointer) to the decimal32 structure, and the
  387. decimal32 is in network byte order (big-endian) */
  388. /* Get sign */
  389. #define decimal32Sign(d) ((unsigned)(d)->bytes[0]>>7)
  390. /* Get combination field */
  391. #define decimal32Comb(d) (((d)->bytes[0] & 0x7c)>>2)
  392. /* Get exponent continuation [does not remove bias] */
  393. #define decimal32ExpCon(d) ((((d)->bytes[0] & 0x03)<<4) \
  394. | ((unsigned)(d)->bytes[1]>>4))
  395. /* Set sign [this assumes sign previously 0] */
  396. #define decimal32SetSign(d, b) { \
  397. (d)->bytes[0]|=((unsigned)(b)<<7);}
  398. /* Set exponent continuation [does not apply bias] */
  399. /* This assumes range has been checked and exponent previously 0; */
  400. /* type of exponent must be unsigned */
  401. #define decimal32SetExpCon(d, e) { \
  402. (d)->bytes[0]|=(uByte)((e)>>4); \
  403. (d)->bytes[1]|=(uByte)(((e)&0x0F)<<4);}
  404. /* ------------------------------------------------------------------ */
  405. /* decimal32Show -- display a decimal32 in hexadecimal [debug aid] */
  406. /* d32 -- the number to show */
  407. /* ------------------------------------------------------------------ */
  408. /* Also shows sign/cob/expconfields extracted - valid bigendian only */
  409. void decimal32Show(const decimal32 *d32) {
  410. char buf[DECIMAL32_Bytes*2+1];
  411. Int i, j=0;
  412. if (DECLITEND) {
  413. for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
  414. sprintf(&buf[j], "%02x", d32->bytes[3-i]);
  415. }
  416. printf(" D32> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
  417. d32->bytes[3]>>7, (d32->bytes[3]>>2)&0x1f,
  418. ((d32->bytes[3]&0x3)<<4)| (d32->bytes[2]>>4));
  419. }
  420. else {
  421. for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
  422. sprintf(&buf[j], "%02x", d32->bytes[i]);
  423. }
  424. printf(" D32> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
  425. decimal32Sign(d32), decimal32Comb(d32), decimal32ExpCon(d32));
  426. }
  427. } /* decimal32Show */
  428. #endif