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