decimal128.c 25 KB

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