decimal128.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* Decimal 128-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 128-bit format module */
  22. /* ------------------------------------------------------------------ */
  23. /* This module comprises the routines for decimal128 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 34 /* make decNumbers with space for 34 */
  35. #include "decNumber.h" /* base number library */
  36. #include "decNumberLocal.h" /* decNumber local types, etc. */
  37. #include "decimal128.h" /* our primary include */
  38. /* Utility routines and tables [in decimal64.c] */
  39. extern const uInt COMBEXP[32], COMBMSD[32];
  40. extern const uShort DPD2BIN[1024];
  41. extern const uShort BIN2DPD[1000]; /* [not used] */
  42. extern const uByte BIN2CHAR[4001];
  43. extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
  44. extern void decDigitsToDPD(const decNumber *, uInt *, Int);
  45. #if DECTRACE || DECCHECK
  46. void decimal128Show(const decimal128 *); /* 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. /* decimal128FromNumber -- convert decNumber to decimal128 */
  54. /* */
  55. /* ds is the target decimal128 */
  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 DECIMAL128_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. decimal128 * decimal128FromNumber(decimal128 *d128, 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 targar[4]={0,0,0,0}; /* target 128-bit */
  78. #define targhi targar[3] /* name the word with the sign */
  79. #define targmh targar[2] /* name the words */
  80. #define targml targar[1] /* .. */
  81. #define targlo targar[0] /* .. */
  82. /* If the number has too many digits, or the exponent could be */
  83. /* out of range then reduce the number under the appropriate */
  84. /* constraints. This could push the number to Infinity or zero, */
  85. /* so this check and rounding must be done before generating the */
  86. /* decimal128] */
  87. ae=dn->exponent+dn->digits-1; /* [0 if special] */
  88. if (dn->digits>DECIMAL128_Pmax /* too many digits */
  89. || ae>DECIMAL128_Emax /* likely overflow */
  90. || ae<DECIMAL128_Emin) { /* likely underflow */
  91. decContextDefault(&dc, DEC_INIT_DECIMAL128); /* [no traps] */
  92. dc.round=set->round; /* use supplied rounding */
  93. decNumberPlus(&dw, dn, &dc); /* (round and check) */
  94. /* [this changes -0 to 0, so enforce the sign...] */
  95. dw.bits|=dn->bits&DECNEG;
  96. status=dc.status; /* save status */
  97. dn=&dw; /* use the work number */
  98. } /* maybe out of range */
  99. if (dn->bits&DECSPECIAL) { /* a special value */
  100. if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
  101. else { /* sNaN or qNaN */
  102. if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
  103. && (dn->digits<DECIMAL128_Pmax)) { /* coefficient fits */
  104. decDigitsToDPD(dn, targar, 0);
  105. }
  106. if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
  107. else targhi|=DECIMAL_sNaN<<24;
  108. } /* a NaN */
  109. } /* special */
  110. else { /* is finite */
  111. if (decNumberIsZero(dn)) { /* is a zero */
  112. /* set and clamp exponent */
  113. if (dn->exponent<-DECIMAL128_Bias) {
  114. exp=0; /* low clamp */
  115. status|=DEC_Clamped;
  116. }
  117. else {
  118. exp=dn->exponent+DECIMAL128_Bias; /* bias exponent */
  119. if (exp>DECIMAL128_Ehigh) { /* top clamp */
  120. exp=DECIMAL128_Ehigh;
  121. status|=DEC_Clamped;
  122. }
  123. }
  124. comb=(exp>>9) & 0x18; /* msd=0, exp top 2 bits .. */
  125. }
  126. else { /* non-zero finite number */
  127. uInt msd; /* work */
  128. Int pad=0; /* coefficient pad digits */
  129. /* the dn is known to fit, but it may need to be padded */
  130. exp=(uInt)(dn->exponent+DECIMAL128_Bias); /* bias exponent */
  131. if (exp>DECIMAL128_Ehigh) { /* fold-down case */
  132. pad=exp-DECIMAL128_Ehigh;
  133. exp=DECIMAL128_Ehigh; /* [to maximum] */
  134. status|=DEC_Clamped;
  135. }
  136. /* [fastpath for common case is not a win, here] */
  137. decDigitsToDPD(dn, targar, pad);
  138. /* save and clear the top digit */
  139. msd=targhi>>14;
  140. targhi&=0x00003fff;
  141. /* create the combination field */
  142. if (msd>=8) comb=0x18 | ((exp>>11) & 0x06) | (msd & 0x01);
  143. else comb=((exp>>9) & 0x18) | msd;
  144. }
  145. targhi|=comb<<26; /* add combination field .. */
  146. targhi|=(exp&0xfff)<<14; /* .. and exponent continuation */
  147. } /* finite */
  148. if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
  149. /* now write to storage; this is endian */
  150. if (DECLITEND) {
  151. /* lo -> hi */
  152. UBFROMUI(d128->bytes, targlo);
  153. UBFROMUI(d128->bytes+4, targml);
  154. UBFROMUI(d128->bytes+8, targmh);
  155. UBFROMUI(d128->bytes+12, targhi);
  156. }
  157. else {
  158. /* hi -> lo */
  159. UBFROMUI(d128->bytes, targhi);
  160. UBFROMUI(d128->bytes+4, targmh);
  161. UBFROMUI(d128->bytes+8, targml);
  162. UBFROMUI(d128->bytes+12, targlo);
  163. }
  164. if (status!=0) decContextSetStatus(set, status); /* pass on status */
  165. /* decimal128Show(d128); */
  166. return d128;
  167. } /* decimal128FromNumber */
  168. /* ------------------------------------------------------------------ */
  169. /* decimal128ToNumber -- convert decimal128 to decNumber */
  170. /* d128 is the source decimal128 */
  171. /* dn is the target number, with appropriate space */
  172. /* No error is possible. */
  173. /* ------------------------------------------------------------------ */
  174. decNumber * decimal128ToNumber(const decimal128 *d128, decNumber *dn) {
  175. uInt msd; /* coefficient MSD */
  176. uInt exp; /* exponent top two bits */
  177. uInt comb; /* combination field */
  178. Int need; /* work */
  179. uInt uiwork; /* for macros */
  180. uInt sourar[4]; /* source 128-bit */
  181. #define sourhi sourar[3] /* name the word with the sign */
  182. #define sourmh sourar[2] /* and the mid-high word */
  183. #define sourml sourar[1] /* and the mod-low word */
  184. #define sourlo sourar[0] /* and the lowest word */
  185. /* load source from storage; this is endian */
  186. if (DECLITEND) {
  187. sourlo=UBTOUI(d128->bytes ); /* directly load the low int */
  188. sourml=UBTOUI(d128->bytes+4 ); /* then the mid-low */
  189. sourmh=UBTOUI(d128->bytes+8 ); /* then the mid-high */
  190. sourhi=UBTOUI(d128->bytes+12); /* then the high int */
  191. }
  192. else {
  193. sourhi=UBTOUI(d128->bytes ); /* directly load the high int */
  194. sourmh=UBTOUI(d128->bytes+4 ); /* then the mid-high */
  195. sourml=UBTOUI(d128->bytes+8 ); /* then the mid-low */
  196. sourlo=UBTOUI(d128->bytes+12); /* then the low int */
  197. }
  198. comb=(sourhi>>26)&0x1f; /* combination field */
  199. decNumberZero(dn); /* clean number */
  200. if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
  201. msd=COMBMSD[comb]; /* decode the combination field */
  202. exp=COMBEXP[comb]; /* .. */
  203. if (exp==3) { /* is a special */
  204. if (msd==0) {
  205. dn->bits|=DECINF;
  206. return dn; /* no coefficient needed */
  207. }
  208. else if (sourhi&0x02000000) dn->bits|=DECSNAN;
  209. else dn->bits|=DECNAN;
  210. msd=0; /* no top digit */
  211. }
  212. else { /* is a finite number */
  213. dn->exponent=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; /* unbiased */
  214. }
  215. /* get the coefficient */
  216. sourhi&=0x00003fff; /* clean coefficient continuation */
  217. if (msd) { /* non-zero msd */
  218. sourhi|=msd<<14; /* prefix to coefficient */
  219. need=12; /* process 12 declets */
  220. }
  221. else { /* msd=0 */
  222. if (sourhi) need=11; /* declets to process */
  223. else if (sourmh) need=10;
  224. else if (sourml) need=7;
  225. else if (sourlo) need=4;
  226. else return dn; /* easy: coefficient is 0 */
  227. } /*msd=0 */
  228. decDigitsFromDPD(dn, sourar, need); /* process declets */
  229. /* decNumberShow(dn); */
  230. return dn;
  231. } /* decimal128ToNumber */
  232. /* ------------------------------------------------------------------ */
  233. /* to-scientific-string -- conversion to numeric string */
  234. /* to-engineering-string -- conversion to numeric string */
  235. /* */
  236. /* decimal128ToString(d128, string); */
  237. /* decimal128ToEngString(d128, string); */
  238. /* */
  239. /* d128 is the decimal128 format number to convert */
  240. /* string is the string where the result will be laid out */
  241. /* */
  242. /* string must be at least 24 characters */
  243. /* */
  244. /* No error is possible, and no status can be set. */
  245. /* ------------------------------------------------------------------ */
  246. char * decimal128ToEngString(const decimal128 *d128, char *string){
  247. decNumber dn; /* work */
  248. decimal128ToNumber(d128, &dn);
  249. decNumberToEngString(&dn, string);
  250. return string;
  251. } /* decimal128ToEngString */
  252. char * decimal128ToString(const decimal128 *d128, char *string){
  253. uInt msd; /* coefficient MSD */
  254. Int exp; /* exponent top two bits or full */
  255. uInt comb; /* combination field */
  256. char *cstart; /* coefficient start */
  257. char *c; /* output pointer in string */
  258. const uByte *u; /* work */
  259. char *s, *t; /* .. (source, target) */
  260. Int dpd; /* .. */
  261. Int pre, e; /* .. */
  262. uInt uiwork; /* for macros */
  263. uInt sourar[4]; /* source 128-bit */
  264. #define sourhi sourar[3] /* name the word with the sign */
  265. #define sourmh sourar[2] /* and the mid-high word */
  266. #define sourml sourar[1] /* and the mod-low word */
  267. #define sourlo sourar[0] /* and the lowest word */
  268. /* load source from storage; this is endian */
  269. if (DECLITEND) {
  270. sourlo=UBTOUI(d128->bytes ); /* directly load the low int */
  271. sourml=UBTOUI(d128->bytes+4 ); /* then the mid-low */
  272. sourmh=UBTOUI(d128->bytes+8 ); /* then the mid-high */
  273. sourhi=UBTOUI(d128->bytes+12); /* then the high int */
  274. }
  275. else {
  276. sourhi=UBTOUI(d128->bytes ); /* directly load the high int */
  277. sourmh=UBTOUI(d128->bytes+4 ); /* then the mid-high */
  278. sourml=UBTOUI(d128->bytes+8 ); /* then the mid-low */
  279. sourlo=UBTOUI(d128->bytes+12); /* then the low int */
  280. }
  281. c=string; /* where result will go */
  282. if (((Int)sourhi)<0) *c++='-'; /* handle sign */
  283. comb=(sourhi>>26)&0x1f; /* combination field */
  284. msd=COMBMSD[comb]; /* decode the combination field */
  285. exp=COMBEXP[comb]; /* .. */
  286. if (exp==3) {
  287. if (msd==0) { /* infinity */
  288. strcpy(c, "Inf");
  289. strcpy(c+3, "inity");
  290. return string; /* easy */
  291. }
  292. if (sourhi&0x02000000) *c++='s'; /* sNaN */
  293. strcpy(c, "NaN"); /* complete word */
  294. c+=3; /* step past */
  295. if (sourlo==0 && sourml==0 && sourmh==0
  296. && (sourhi&0x0003ffff)==0) return string; /* zero payload */
  297. /* otherwise drop through to add integer; set correct exp */
  298. exp=0; msd=0; /* setup for following code */
  299. }
  300. else exp=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; /* unbiased */
  301. /* convert 34 digits of significand to characters */
  302. cstart=c; /* save start of coefficient */
  303. if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
  304. /* Now decode the declets. After extracting each one, it is */
  305. /* decoded to binary and then to a 4-char sequence by table lookup; */
  306. /* the 4-chars are a 1-char length (significant digits, except 000 */
  307. /* has length 0). This allows us to left-align the first declet */
  308. /* with non-zero content, then remaining ones are full 3-char */
  309. /* length. We use fixed-length memcpys because variable-length */
  310. /* causes a subroutine call in GCC. (These are length 4 for speed */
  311. /* and are safe because the array has an extra terminator byte.) */
  312. #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
  313. if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
  314. else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
  315. dpd=(sourhi>>4)&0x3ff; /* declet 1 */
  316. dpd2char;
  317. dpd=((sourhi&0xf)<<6) | (sourmh>>26); /* declet 2 */
  318. dpd2char;
  319. dpd=(sourmh>>16)&0x3ff; /* declet 3 */
  320. dpd2char;
  321. dpd=(sourmh>>6)&0x3ff; /* declet 4 */
  322. dpd2char;
  323. dpd=((sourmh&0x3f)<<4) | (sourml>>28); /* declet 5 */
  324. dpd2char;
  325. dpd=(sourml>>18)&0x3ff; /* declet 6 */
  326. dpd2char;
  327. dpd=(sourml>>8)&0x3ff; /* declet 7 */
  328. dpd2char;
  329. dpd=((sourml&0xff)<<2) | (sourlo>>30); /* declet 8 */
  330. dpd2char;
  331. dpd=(sourlo>>20)&0x3ff; /* declet 9 */
  332. dpd2char;
  333. dpd=(sourlo>>10)&0x3ff; /* declet 10 */
  334. dpd2char;
  335. dpd=(sourlo)&0x3ff; /* declet 11 */
  336. dpd2char;
  337. if (c==cstart) *c++='0'; /* all zeros -- make 0 */
  338. if (exp==0) { /* integer or NaN case -- easy */
  339. *c='\0'; /* terminate */
  340. return string;
  341. }
  342. /* non-0 exponent */
  343. e=0; /* assume no E */
  344. pre=c-cstart+exp;
  345. /* [here, pre-exp is the digits count (==1 for zero)] */
  346. if (exp>0 || pre<-5) { /* need exponential form */
  347. e=pre-1; /* calculate E value */
  348. pre=1; /* assume one digit before '.' */
  349. } /* exponential form */
  350. /* modify the coefficient, adding 0s, '.', and E+nn as needed */
  351. s=c-1; /* source (LSD) */
  352. if (pre>0) { /* ddd.ddd (plain), perhaps with E */
  353. char *dotat=cstart+pre;
  354. if (dotat<c) { /* if embedded dot needed... */
  355. t=c; /* target */
  356. for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
  357. *t='.'; /* insert the dot */
  358. c++; /* length increased by one */
  359. }
  360. /* finally add the E-part, if needed; it will never be 0, and has */
  361. /* a maximum length of 4 digits */
  362. if (e!=0) {
  363. *c++='E'; /* starts with E */
  364. *c++='+'; /* assume positive */
  365. if (e<0) {
  366. *(c-1)='-'; /* oops, need '-' */
  367. e=-e; /* uInt, please */
  368. }
  369. if (e<1000) { /* 3 (or fewer) digits case */
  370. u=&BIN2CHAR[e*4]; /* -> length byte */
  371. memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
  372. c+=*u; /* bump pointer appropriately */
  373. }
  374. else { /* 4-digits */
  375. Int thou=((e>>3)*1049)>>17; /* e/1000 */
  376. Int rem=e-(1000*thou); /* e%1000 */
  377. *c++='0'+(char)thou;
  378. u=&BIN2CHAR[rem*4]; /* -> length byte */
  379. memcpy(c, u+1, 4); /* copy fixed 3+1 characters [is safe] */
  380. c+=3; /* bump pointer, always 3 digits */
  381. }
  382. }
  383. *c='\0'; /* add terminator */
  384. /*printf("res %s\n", string); */
  385. return string;
  386. } /* pre>0 */
  387. /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
  388. t=c+1-pre;
  389. *(t+1)='\0'; /* can add terminator now */
  390. for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
  391. c=cstart;
  392. *c++='0'; /* always starts with 0. */
  393. *c++='.';
  394. for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
  395. /*printf("res %s\n", string); */
  396. return string;
  397. } /* decimal128ToString */
  398. /* ------------------------------------------------------------------ */
  399. /* to-number -- conversion from numeric string */
  400. /* */
  401. /* decimal128FromString(result, string, set); */
  402. /* */
  403. /* result is the decimal128 format number which gets the result of */
  404. /* the conversion */
  405. /* *string is the character string which should contain a valid */
  406. /* number (which may be a special value) */
  407. /* set is the context */
  408. /* */
  409. /* The context is supplied to this routine is used for error handling */
  410. /* (setting of status and traps) and for the rounding mode, only. */
  411. /* If an error occurs, the result will be a valid decimal128 NaN. */
  412. /* ------------------------------------------------------------------ */
  413. decimal128 * decimal128FromString(decimal128 *result, const char *string,
  414. decContext *set) {
  415. decContext dc; /* work */
  416. decNumber dn; /* .. */
  417. decContextDefault(&dc, DEC_INIT_DECIMAL128); /* no traps, please */
  418. dc.round=set->round; /* use supplied rounding */
  419. decNumberFromString(&dn, string, &dc); /* will round if needed */
  420. decimal128FromNumber(result, &dn, &dc);
  421. if (dc.status!=0) { /* something happened */
  422. decContextSetStatus(set, dc.status); /* .. pass it on */
  423. }
  424. return result;
  425. } /* decimal128FromString */
  426. /* ------------------------------------------------------------------ */
  427. /* decimal128IsCanonical -- test whether encoding is canonical */
  428. /* d128 is the source decimal128 */
  429. /* returns 1 if the encoding of d128 is canonical, 0 otherwise */
  430. /* No error is possible. */
  431. /* ------------------------------------------------------------------ */
  432. uInt decimal128IsCanonical(const decimal128 *d128) {
  433. decNumber dn; /* work */
  434. decimal128 canon; /* .. */
  435. decContext dc; /* .. */
  436. decContextDefault(&dc, DEC_INIT_DECIMAL128);
  437. decimal128ToNumber(d128, &dn);
  438. decimal128FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
  439. return memcmp(d128, &canon, DECIMAL128_Bytes)==0;
  440. } /* decimal128IsCanonical */
  441. /* ------------------------------------------------------------------ */
  442. /* decimal128Canonical -- copy an encoding, ensuring it is canonical */
  443. /* d128 is the source decimal128 */
  444. /* result is the target (may be the same decimal128) */
  445. /* returns result */
  446. /* No error is possible. */
  447. /* ------------------------------------------------------------------ */
  448. decimal128 * decimal128Canonical(decimal128 *result, const decimal128 *d128) {
  449. decNumber dn; /* work */
  450. decContext dc; /* .. */
  451. decContextDefault(&dc, DEC_INIT_DECIMAL128);
  452. decimal128ToNumber(d128, &dn);
  453. decimal128FromNumber(result, &dn, &dc);/* result will now be canonical */
  454. return result;
  455. } /* decimal128Canonical */
  456. #if DECTRACE || DECCHECK
  457. /* Macros for accessing decimal128 fields. These assume the argument
  458. is a reference (pointer) to the decimal128 structure, and the
  459. decimal128 is in network byte order (big-endian) */
  460. /* Get sign */
  461. #define decimal128Sign(d) ((unsigned)(d)->bytes[0]>>7)
  462. /* Get combination field */
  463. #define decimal128Comb(d) (((d)->bytes[0] & 0x7c)>>2)
  464. /* Get exponent continuation [does not remove bias] */
  465. #define decimal128ExpCon(d) ((((d)->bytes[0] & 0x03)<<10) \
  466. | ((unsigned)(d)->bytes[1]<<2) \
  467. | ((unsigned)(d)->bytes[2]>>6))
  468. /* Set sign [this assumes sign previously 0] */
  469. #define decimal128SetSign(d, b) { \
  470. (d)->bytes[0]|=((unsigned)(b)<<7);}
  471. /* Set exponent continuation [does not apply bias] */
  472. /* This assumes range has been checked and exponent previously 0; */
  473. /* type of exponent must be unsigned */
  474. #define decimal128SetExpCon(d, e) { \
  475. (d)->bytes[0]|=(uByte)((e)>>10); \
  476. (d)->bytes[1] =(uByte)(((e)&0x3fc)>>2); \
  477. (d)->bytes[2]|=(uByte)(((e)&0x03)<<6);}
  478. /* ------------------------------------------------------------------ */
  479. /* decimal128Show -- display a decimal128 in hexadecimal [debug aid] */
  480. /* d128 -- the number to show */
  481. /* ------------------------------------------------------------------ */
  482. /* Also shows sign/cob/expconfields extracted */
  483. void decimal128Show(const decimal128 *d128) {
  484. char buf[DECIMAL128_Bytes*2+1];
  485. Int i, j=0;
  486. if (DECLITEND) {
  487. for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
  488. sprintf(&buf[j], "%02x", d128->bytes[15-i]);
  489. }
  490. printf(" D128> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
  491. d128->bytes[15]>>7, (d128->bytes[15]>>2)&0x1f,
  492. ((d128->bytes[15]&0x3)<<10)|(d128->bytes[14]<<2)|
  493. (d128->bytes[13]>>6));
  494. }
  495. else {
  496. for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
  497. sprintf(&buf[j], "%02x", d128->bytes[i]);
  498. }
  499. printf(" D128> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
  500. decimal128Sign(d128), decimal128Comb(d128),
  501. decimal128ExpCon(d128));
  502. }
  503. } /* decimal128Show */
  504. #endif