| 1 |
/* Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
This file is part of GCC. |
| 4 |
|
| 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 |
|
| 10 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
Under Section 7 of GPL version 3, you are granted additional |
| 16 |
permissions described in the GCC Runtime Library Exception, version |
| 17 |
3.1, as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License and |
| 20 |
a copy of the GCC Runtime Library Exception along with this program; |
| 21 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 |
<http://www.gnu.org/licenses/>. */ |
| 23 |
|
| 24 |
/***************************************************************************** |
| 25 |
* BID128_to_string |
| 26 |
****************************************************************************/ |
| 27 |
|
| 28 |
#define BID_128RES |
| 29 |
#include <stdio.h> |
| 30 |
#include "bid_internal.h" |
| 31 |
#include "bid128_2_str.h" |
| 32 |
#include "bid128_2_str_macros.h" |
| 33 |
|
| 34 |
extern int bid128_coeff_2_string (UINT64 X_hi, UINT64 X_lo, |
| 35 |
char *char_ptr); |
| 36 |
|
| 37 |
#if DECIMAL_CALL_BY_REFERENCE |
| 38 |
|
| 39 |
void |
| 40 |
bid128_to_string (char *str, |
| 41 |
UINT128 * |
| 42 |
px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM |
| 43 |
_EXC_INFO_PARAM) { |
| 44 |
UINT128 x; |
| 45 |
#else |
| 46 |
|
| 47 |
void |
| 48 |
bid128_to_string (char *str, UINT128 x |
| 49 |
_EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM) { |
| 50 |
#endif |
| 51 |
UINT64 x_sign; |
| 52 |
UINT64 x_exp; |
| 53 |
int exp; // unbiased exponent |
| 54 |
// Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64) |
| 55 |
int ind; |
| 56 |
UINT128 C1; |
| 57 |
unsigned int k = 0; // pointer in the string |
| 58 |
unsigned int d0, d123; |
| 59 |
unsigned int zero_digit = (unsigned int) '0'; |
| 60 |
UINT64 HI_18Dig, LO_18Dig, Tmp; |
| 61 |
UINT32 MiDi[12], *ptr; |
| 62 |
char *c_ptr_start, *c_ptr; |
| 63 |
int midi_ind, k_lcv, len; |
| 64 |
|
| 65 |
#if DECIMAL_CALL_BY_REFERENCE |
| 66 |
x = *px; |
| 67 |
#endif |
| 68 |
|
| 69 |
BID_SWAP128(x); |
| 70 |
// check for NaN or Infinity |
| 71 |
if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) { |
| 72 |
// x is special |
| 73 |
if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN |
| 74 |
if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN |
| 75 |
// set invalid flag |
| 76 |
str[0] = ((SINT64)x.w[1]<0)? '-':'+'; |
| 77 |
str[1] = 'S'; |
| 78 |
str[2] = 'N'; |
| 79 |
str[3] = 'a'; |
| 80 |
str[4] = 'N'; |
| 81 |
str[5] = '\0'; |
| 82 |
} else { // x is QNaN |
| 83 |
str[0] = ((SINT64)x.w[1]<0)? '-':'+'; |
| 84 |
str[1] = 'Q'; |
| 85 |
str[2] = 'N'; |
| 86 |
str[3] = 'a'; |
| 87 |
str[4] = 'N'; |
| 88 |
str[5] = '\0'; |
| 89 |
} |
| 90 |
} else { // x is not a NaN, so it must be infinity |
| 91 |
if ((x.w[1] & MASK_SIGN) == 0x0ull) { // x is +inf |
| 92 |
str[0] = '+'; |
| 93 |
str[1] = 'I'; |
| 94 |
str[2] = 'n'; |
| 95 |
str[3] = 'f'; |
| 96 |
str[4] = '\0'; |
| 97 |
} else { // x is -inf |
| 98 |
str[0] = '-'; |
| 99 |
str[1] = 'I'; |
| 100 |
str[2] = 'n'; |
| 101 |
str[3] = 'f'; |
| 102 |
str[4] = '\0'; |
| 103 |
} |
| 104 |
} |
| 105 |
return; |
| 106 |
} else if (((x.w[1] & MASK_COEFF) == 0x0ull) && (x.w[0] == 0x0ull)) { |
| 107 |
// x is 0 |
| 108 |
len = 0; |
| 109 |
|
| 110 |
//determine if +/- |
| 111 |
if (x.w[1] & MASK_SIGN) |
| 112 |
str[len++] = '-'; |
| 113 |
else |
| 114 |
str[len++] = '+'; |
| 115 |
str[len++] = '0'; |
| 116 |
str[len++] = 'E'; |
| 117 |
|
| 118 |
// extract the exponent and print |
| 119 |
exp = (int) (((x.w[1] & MASK_EXP) >> 49) - 6176); |
| 120 |
if(exp > (((0x5ffe)>>1) - (6176))) { |
| 121 |
exp = (int) ((((x.w[1]<<2) & MASK_EXP) >> 49) - 6176); |
| 122 |
} |
| 123 |
if (exp >= 0) { |
| 124 |
str[len++] = '+'; |
| 125 |
len += sprintf (str + len, "%u", exp);// should not use sprintf (should |
| 126 |
// use sophisticated algorithm, since we know range of exp is limited) |
| 127 |
str[len++] = '\0'; |
| 128 |
} else { |
| 129 |
len += sprintf (str + len, "%d", exp);// should not use sprintf (should |
| 130 |
// use sophisticated algorithm, since we know range of exp is limited) |
| 131 |
str[len++] = '\0'; |
| 132 |
} |
| 133 |
return; |
| 134 |
} else { // x is not special and is not zero |
| 135 |
// unpack x |
| 136 |
x_sign = x.w[1] & MASK_SIGN;// 0 for positive, MASK_SIGN for negative |
| 137 |
x_exp = x.w[1] & MASK_EXP;// biased and shifted left 49 bit positions |
| 138 |
if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) |
| 139 |
x_exp = (x.w[1]<<2) & MASK_EXP;// biased and shifted left 49 bit positions |
| 140 |
C1.w[1] = x.w[1] & MASK_COEFF; |
| 141 |
C1.w[0] = x.w[0]; |
| 142 |
exp = (x_exp >> 49) - 6176; |
| 143 |
|
| 144 |
// determine sign's representation as a char |
| 145 |
if (x_sign) |
| 146 |
str[k++] = '-';// negative number |
| 147 |
else |
| 148 |
str[k++] = '+';// positive number |
| 149 |
|
| 150 |
// determine coefficient's representation as a decimal string |
| 151 |
|
| 152 |
// if zero or non-canonical, set coefficient to '0' |
| 153 |
if ((C1.w[1] > 0x0001ed09bead87c0ull) || |
| 154 |
(C1.w[1] == 0x0001ed09bead87c0ull && |
| 155 |
(C1.w[0] > 0x378d8e63ffffffffull)) || |
| 156 |
((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) || |
| 157 |
((C1.w[1] == 0) && (C1.w[0] == 0))) { |
| 158 |
str[k++] = '0'; |
| 159 |
} else { |
| 160 |
/* **************************************************** |
| 161 |
This takes a bid coefficient in C1.w[1],C1.w[0] |
| 162 |
and put the converted character sequence at location |
| 163 |
starting at &(str[k]). The function returns the number |
| 164 |
of MiDi returned. Note that the character sequence |
| 165 |
does not have leading zeros EXCEPT when the input is of |
| 166 |
zero value. It will then output 1 character '0' |
| 167 |
The algorithm essentailly tries first to get a sequence of |
| 168 |
Millenial Digits "MiDi" and then uses table lookup to get the |
| 169 |
character strings of these MiDis. |
| 170 |
**************************************************** */ |
| 171 |
/* Algorithm first decompose possibly 34 digits in hi and lo |
| 172 |
18 digits. (The high can have at most 16 digits). It then |
| 173 |
uses macro that handle 18 digit portions. |
| 174 |
The first step is to get hi and lo such that |
| 175 |
2^(64) C1.w[1] + C1.w[0] = hi * 10^18 + lo, 0 <= lo < 10^18. |
| 176 |
We use a table lookup method to obtain the hi and lo 18 digits. |
| 177 |
[C1.w[1],C1.w[0]] = c_8 2^(107) + c_7 2^(101) + ... + c_0 2^(59) + d |
| 178 |
where 0 <= d < 2^59 and each c_j has 6 bits. Because d fits in |
| 179 |
18 digits, we set hi = 0, and lo = d to begin with. |
| 180 |
We then retrieve from a table, for j = 0, 1, ..., 8 |
| 181 |
that gives us A and B where c_j 2^(59+6j) = A * 10^18 + B. |
| 182 |
hi += A ; lo += B; After each accumulation into lo, we normalize |
| 183 |
immediately. So at the end, we have the decomposition as we need. */ |
| 184 |
|
| 185 |
Tmp = C1.w[0] >> 59; |
| 186 |
LO_18Dig = (C1.w[0] << 5) >> 5; |
| 187 |
Tmp += (C1.w[1] << 5); |
| 188 |
HI_18Dig = 0; |
| 189 |
k_lcv = 0; |
| 190 |
// Tmp = {C1.w[1]{49:0}, C1.w[0]{63:59}} |
| 191 |
// Lo_18Dig = {C1.w[0]{58:0}} |
| 192 |
|
| 193 |
while (Tmp) { |
| 194 |
midi_ind = (int) (Tmp & 0x000000000000003FLL); |
| 195 |
midi_ind <<= 1; |
| 196 |
Tmp >>= 6; |
| 197 |
HI_18Dig += mod10_18_tbl[k_lcv][midi_ind++]; |
| 198 |
LO_18Dig += mod10_18_tbl[k_lcv++][midi_ind]; |
| 199 |
__L0_Normalize_10to18 (HI_18Dig, LO_18Dig); |
| 200 |
} |
| 201 |
ptr = MiDi; |
| 202 |
if (HI_18Dig == 0LL) { |
| 203 |
__L1_Split_MiDi_6_Lead (LO_18Dig, ptr); |
| 204 |
} else { |
| 205 |
__L1_Split_MiDi_6_Lead (HI_18Dig, ptr); |
| 206 |
__L1_Split_MiDi_6 (LO_18Dig, ptr); |
| 207 |
} |
| 208 |
len = ptr - MiDi; |
| 209 |
c_ptr_start = &(str[k]); |
| 210 |
c_ptr = c_ptr_start; |
| 211 |
|
| 212 |
/* now convert the MiDi into character strings */ |
| 213 |
__L0_MiDi2Str_Lead (MiDi[0], c_ptr); |
| 214 |
for (k_lcv = 1; k_lcv < len; k_lcv++) { |
| 215 |
__L0_MiDi2Str (MiDi[k_lcv], c_ptr); |
| 216 |
} |
| 217 |
k = k + (c_ptr - c_ptr_start); |
| 218 |
} |
| 219 |
|
| 220 |
// print E and sign of exponent |
| 221 |
str[k++] = 'E'; |
| 222 |
if (exp < 0) { |
| 223 |
exp = -exp; |
| 224 |
str[k++] = '-'; |
| 225 |
} else { |
| 226 |
str[k++] = '+'; |
| 227 |
} |
| 228 |
|
| 229 |
// determine exponent's representation as a decimal string |
| 230 |
// d0 = exp / 1000; |
| 231 |
// Use Property 1 |
| 232 |
d0 = (exp * 0x418a) >> 24;// 0x418a * 2^-24 = (10^(-3))RP,15 |
| 233 |
d123 = exp - 1000 * d0; |
| 234 |
|
| 235 |
if (d0) { // 1000 <= exp <= 6144 => 4 digits to return |
| 236 |
str[k++] = d0 + zero_digit; // ASCII for decimal digit d0 |
| 237 |
ind = 3 * d123; |
| 238 |
str[k++] = char_table3[ind]; |
| 239 |
str[k++] = char_table3[ind + 1]; |
| 240 |
str[k++] = char_table3[ind + 2]; |
| 241 |
} else { // 0 <= exp <= 999 => d0 = 0 |
| 242 |
if (d123 < 10) { // 0 <= exp <= 9 => 1 digit to return |
| 243 |
str[k++] = d123 + zero_digit; // ASCII |
| 244 |
} else if (d123 < 100) { // 10 <= exp <= 99 => 2 digits to return |
| 245 |
ind = 2 * (d123 - 10); |
| 246 |
str[k++] = char_table2[ind]; |
| 247 |
str[k++] = char_table2[ind + 1]; |
| 248 |
} else { // 100 <= exp <= 999 => 3 digits to return |
| 249 |
ind = 3 * d123; |
| 250 |
str[k++] = char_table3[ind]; |
| 251 |
str[k++] = char_table3[ind + 1]; |
| 252 |
str[k++] = char_table3[ind + 2]; |
| 253 |
} |
| 254 |
} |
| 255 |
str[k] = '\0'; |
| 256 |
|
| 257 |
} |
| 258 |
return; |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
#define MAX_FORMAT_DIGITS_128 34 |
| 264 |
#define MAX_STRING_DIGITS_128 100 |
| 265 |
#define MAX_SEARCH MAX_STRING_DIGITS_128-MAX_FORMAT_DIGITS_128-1 |
| 266 |
|
| 267 |
|
| 268 |
#if DECIMAL_CALL_BY_REFERENCE |
| 269 |
|
| 270 |
void |
| 271 |
bid128_from_string (UINT128 * pres, |
| 272 |
char *ps _RND_MODE_PARAM _EXC_FLAGS_PARAM |
| 273 |
_EXC_MASKS_PARAM _EXC_INFO_PARAM) { |
| 274 |
#else |
| 275 |
|
| 276 |
UINT128 |
| 277 |
bid128_from_string (char *ps _RND_MODE_PARAM _EXC_FLAGS_PARAM |
| 278 |
_EXC_MASKS_PARAM _EXC_INFO_PARAM) { |
| 279 |
#endif |
| 280 |
UINT128 CX, res; |
| 281 |
UINT64 sign_x, coeff_high, coeff_low, coeff2, coeff_l2, carry = 0x0ull, |
| 282 |
scale_high, right_radix_leading_zeros; |
| 283 |
int ndigits_before, ndigits_after, ndigits_total, dec_expon, sgn_exp, |
| 284 |
i, d2, rdx_pt_enc; |
| 285 |
char c, buffer[MAX_STRING_DIGITS_128]; |
| 286 |
int save_rnd_mode; |
| 287 |
int save_fpsf; |
| 288 |
|
| 289 |
#if DECIMAL_CALL_BY_REFERENCE |
| 290 |
#if !DECIMAL_GLOBAL_ROUNDING |
| 291 |
_IDEC_round rnd_mode = *prnd_mode; |
| 292 |
#endif |
| 293 |
#endif |
| 294 |
|
| 295 |
save_rnd_mode = rnd_mode; // dummy |
| 296 |
save_fpsf = *pfpsf; // dummy |
| 297 |
|
| 298 |
right_radix_leading_zeros = rdx_pt_enc = 0; |
| 299 |
|
| 300 |
// if null string, return NaN |
| 301 |
if (!ps) { |
| 302 |
res.w[1] = 0x7c00000000000000ull; |
| 303 |
res.w[0] = 0; |
| 304 |
BID_RETURN (res); |
| 305 |
} |
| 306 |
// eliminate leading white space |
| 307 |
while ((*ps == ' ') || (*ps == '\t')) |
| 308 |
ps++; |
| 309 |
|
| 310 |
// c gets first character |
| 311 |
c = *ps; |
| 312 |
|
| 313 |
|
| 314 |
// if c is null or not equal to a (radix point, negative sign, |
| 315 |
// positive sign, or number) it might be SNaN, sNaN, Infinity |
| 316 |
if (!c |
| 317 |
|| (c != '.' && c != '-' && c != '+' |
| 318 |
&& ((unsigned) (c - '0') > 9))) { |
| 319 |
res.w[0] = 0; |
| 320 |
// Infinity? |
| 321 |
if ((tolower_macro (ps[0]) == 'i' && tolower_macro (ps[1]) == 'n' |
| 322 |
&& tolower_macro (ps[2]) == 'f') |
| 323 |
&& (!ps[3] |
| 324 |
|| (tolower_macro (ps[3]) == 'i' |
| 325 |
&& tolower_macro (ps[4]) == 'n' |
| 326 |
&& tolower_macro (ps[5]) == 'i' |
| 327 |
&& tolower_macro (ps[6]) == 't' |
| 328 |
&& tolower_macro (ps[7]) == 'y' && !ps[8]) |
| 329 |
)) { |
| 330 |
res.w[1] = 0x7800000000000000ull; |
| 331 |
BID_RETURN (res); |
| 332 |
} |
| 333 |
// return sNaN |
| 334 |
if (tolower_macro (ps[0]) == 's' && tolower_macro (ps[1]) == 'n' && |
| 335 |
tolower_macro (ps[2]) == 'a' && tolower_macro (ps[3]) == 'n') { |
| 336 |
// case insensitive check for snan |
| 337 |
res.w[1] = 0x7e00000000000000ull; |
| 338 |
BID_RETURN (res); |
| 339 |
} else { |
| 340 |
// return qNaN |
| 341 |
res.w[1] = 0x7c00000000000000ull; |
| 342 |
BID_RETURN (res); |
| 343 |
} |
| 344 |
} |
| 345 |
// if +Inf, -Inf, +Infinity, or -Infinity (case insensitive check for inf) |
| 346 |
if ((tolower_macro (ps[1]) == 'i' && tolower_macro (ps[2]) == 'n' && |
| 347 |
tolower_macro (ps[3]) == 'f') && (!ps[4] || |
| 348 |
(tolower_macro (ps[4]) == 'i' && tolower_macro (ps[5]) == 'n' && |
| 349 |
tolower_macro (ps[6]) == 'i' && tolower_macro (ps[7]) == 't' && |
| 350 |
tolower_macro (ps[8]) == 'y' && !ps[9]))) { // ci check for infinity |
| 351 |
res.w[0] = 0; |
| 352 |
|
| 353 |
if (c == '+') |
| 354 |
res.w[1] = 0x7800000000000000ull; |
| 355 |
else if (c == '-') |
| 356 |
res.w[1] = 0xf800000000000000ull; |
| 357 |
else |
| 358 |
res.w[1] = 0x7c00000000000000ull; |
| 359 |
|
| 360 |
BID_RETURN (res); |
| 361 |
} |
| 362 |
// if +sNaN, +SNaN, -sNaN, or -SNaN |
| 363 |
if (tolower_macro (ps[1]) == 's' && tolower_macro (ps[2]) == 'n' |
| 364 |
&& tolower_macro (ps[3]) == 'a' && tolower_macro (ps[4]) == 'n') { |
| 365 |
res.w[0] = 0; |
| 366 |
if (c == '-') |
| 367 |
res.w[1] = 0xfe00000000000000ull; |
| 368 |
else |
| 369 |
res.w[1] = 0x7e00000000000000ull; |
| 370 |
BID_RETURN (res); |
| 371 |
} |
| 372 |
// set up sign_x to be OR'ed with the upper word later |
| 373 |
if (c == '-') |
| 374 |
sign_x = 0x8000000000000000ull; |
| 375 |
else |
| 376 |
sign_x = 0; |
| 377 |
|
| 378 |
// go to next character if leading sign |
| 379 |
if (c == '-' || c == '+') |
| 380 |
ps++; |
| 381 |
|
| 382 |
c = *ps; |
| 383 |
|
| 384 |
// if c isn't a decimal point or a decimal digit, return NaN |
| 385 |
if (c != '.' && ((unsigned) (c - '0') > 9)) { |
| 386 |
res.w[1] = 0x7c00000000000000ull | sign_x; |
| 387 |
res.w[0] = 0; |
| 388 |
BID_RETURN (res); |
| 389 |
} |
| 390 |
// detect zero (and eliminate/ignore leading zeros) |
| 391 |
if (*(ps) == '0') { |
| 392 |
|
| 393 |
// if all numbers are zeros (with possibly 1 radix point, the number is zero |
| 394 |
// should catch cases such as: 000.0 |
| 395 |
while (*ps == '0') { |
| 396 |
|
| 397 |
ps++; |
| 398 |
|
| 399 |
// for numbers such as 0.0000000000000000000000000000000000001001, |
| 400 |
// we want to count the leading zeros |
| 401 |
if (rdx_pt_enc) { |
| 402 |
right_radix_leading_zeros++; |
| 403 |
} |
| 404 |
// if this character is a radix point, make sure we haven't already |
| 405 |
// encountered one |
| 406 |
if (*(ps) == '.') { |
| 407 |
if (rdx_pt_enc == 0) { |
| 408 |
rdx_pt_enc = 1; |
| 409 |
// if this is the first radix point, and the next character is NULL, |
| 410 |
// we have a zero |
| 411 |
if (!*(ps + 1)) { |
| 412 |
res.w[1] = |
| 413 |
(0x3040000000000000ull - |
| 414 |
(right_radix_leading_zeros << 49)) | sign_x; |
| 415 |
res.w[0] = 0; |
| 416 |
BID_RETURN (res); |
| 417 |
} |
| 418 |
ps = ps + 1; |
| 419 |
} else { |
| 420 |
// if 2 radix points, return NaN |
| 421 |
res.w[1] = 0x7c00000000000000ull | sign_x; |
| 422 |
res.w[0] = 0; |
| 423 |
BID_RETURN (res); |
| 424 |
} |
| 425 |
} else if (!*(ps)) { |
| 426 |
//res.w[1] = 0x3040000000000000ull | sign_x; |
| 427 |
res.w[1] = |
| 428 |
(0x3040000000000000ull - |
| 429 |
(right_radix_leading_zeros << 49)) | sign_x; |
| 430 |
res.w[0] = 0; |
| 431 |
BID_RETURN (res); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
c = *ps; |
| 437 |
|
| 438 |
// initialize local variables |
| 439 |
ndigits_before = ndigits_after = ndigits_total = 0; |
| 440 |
sgn_exp = 0; |
| 441 |
// pstart_coefficient = ps; |
| 442 |
|
| 443 |
if (!rdx_pt_enc) { |
| 444 |
// investigate string (before radix point) |
| 445 |
while ((unsigned) (c - '0') <= 9 |
| 446 |
&& ndigits_before < MAX_STRING_DIGITS_128) { |
| 447 |
buffer[ndigits_before] = c; |
| 448 |
ps++; |
| 449 |
c = *ps; |
| 450 |
ndigits_before++; |
| 451 |
} |
| 452 |
|
| 453 |
ndigits_total = ndigits_before; |
| 454 |
if (c == '.') { |
| 455 |
ps++; |
| 456 |
if ((c = *ps)) { |
| 457 |
|
| 458 |
// investigate string (after radix point) |
| 459 |
while ((unsigned) (c - '0') <= 9 |
| 460 |
&& ndigits_total < MAX_STRING_DIGITS_128) { |
| 461 |
buffer[ndigits_total] = c; |
| 462 |
ps++; |
| 463 |
c = *ps; |
| 464 |
ndigits_total++; |
| 465 |
} |
| 466 |
ndigits_after = ndigits_total - ndigits_before; |
| 467 |
} |
| 468 |
} |
| 469 |
} else { |
| 470 |
// we encountered a radix point while detecting zeros |
| 471 |
//if (c = *ps){ |
| 472 |
|
| 473 |
c = *ps; |
| 474 |
ndigits_total = 0; |
| 475 |
// investigate string (after radix point) |
| 476 |
while ((unsigned) (c - '0') <= 9 |
| 477 |
&& ndigits_total < MAX_STRING_DIGITS_128) { |
| 478 |
buffer[ndigits_total] = c; |
| 479 |
ps++; |
| 480 |
c = *ps; |
| 481 |
ndigits_total++; |
| 482 |
} |
| 483 |
ndigits_after = ndigits_total - ndigits_before; |
| 484 |
} |
| 485 |
|
| 486 |
// get exponent |
| 487 |
dec_expon = 0; |
| 488 |
if (ndigits_total < MAX_STRING_DIGITS_128) { |
| 489 |
if (c) { |
| 490 |
if (c != 'e' && c != 'E') { |
| 491 |
// return NaN |
| 492 |
res.w[1] = 0x7c00000000000000ull; |
| 493 |
res.w[0] = 0; |
| 494 |
BID_RETURN (res); |
| 495 |
} |
| 496 |
ps++; |
| 497 |
c = *ps; |
| 498 |
|
| 499 |
if (((unsigned) (c - '0') > 9) |
| 500 |
&& ((c != '+' && c != '-') || (unsigned) (ps[1] - '0') > 9)) { |
| 501 |
// return NaN |
| 502 |
res.w[1] = 0x7c00000000000000ull; |
| 503 |
res.w[0] = 0; |
| 504 |
BID_RETURN (res); |
| 505 |
} |
| 506 |
|
| 507 |
if (c == '-') { |
| 508 |
sgn_exp = -1; |
| 509 |
ps++; |
| 510 |
c = *ps; |
| 511 |
} else if (c == '+') { |
| 512 |
ps++; |
| 513 |
c = *ps; |
| 514 |
} |
| 515 |
|
| 516 |
dec_expon = c - '0'; |
| 517 |
i = 1; |
| 518 |
ps++; |
| 519 |
c = *ps - '0'; |
| 520 |
while (((unsigned) c) <= 9 && i < 7) { |
| 521 |
d2 = dec_expon + dec_expon; |
| 522 |
dec_expon = (d2 << 2) + d2 + c; |
| 523 |
ps++; |
| 524 |
c = *ps - '0'; |
| 525 |
i++; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
dec_expon = (dec_expon + sgn_exp) ^ sgn_exp; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
if (ndigits_total <= MAX_FORMAT_DIGITS_128) { |
| 534 |
dec_expon += |
| 535 |
DECIMAL_EXPONENT_BIAS_128 - ndigits_after - |
| 536 |
right_radix_leading_zeros; |
| 537 |
if (dec_expon < 0) { |
| 538 |
res.w[1] = 0 | sign_x; |
| 539 |
res.w[0] = 0; |
| 540 |
} |
| 541 |
if (ndigits_total == 0) { |
| 542 |
CX.w[0] = 0; |
| 543 |
CX.w[1] = 0; |
| 544 |
} else if (ndigits_total <= 19) { |
| 545 |
coeff_high = buffer[0] - '0'; |
| 546 |
for (i = 1; i < ndigits_total; i++) { |
| 547 |
coeff2 = coeff_high + coeff_high; |
| 548 |
coeff_high = (coeff2 << 2) + coeff2 + buffer[i] - '0'; |
| 549 |
} |
| 550 |
CX.w[0] = coeff_high; |
| 551 |
CX.w[1] = 0; |
| 552 |
} else { |
| 553 |
coeff_high = buffer[0] - '0'; |
| 554 |
for (i = 1; i < ndigits_total - 17; i++) { |
| 555 |
coeff2 = coeff_high + coeff_high; |
| 556 |
coeff_high = (coeff2 << 2) + coeff2 + buffer[i] - '0'; |
| 557 |
} |
| 558 |
coeff_low = buffer[i] - '0'; |
| 559 |
i++; |
| 560 |
for (; i < ndigits_total; i++) { |
| 561 |
coeff_l2 = coeff_low + coeff_low; |
| 562 |
coeff_low = (coeff_l2 << 2) + coeff_l2 + buffer[i] - '0'; |
| 563 |
} |
| 564 |
// now form the coefficient as coeff_high*10^19+coeff_low+carry |
| 565 |
scale_high = 100000000000000000ull; |
| 566 |
__mul_64x64_to_128_fast (CX, coeff_high, scale_high); |
| 567 |
|
| 568 |
CX.w[0] += coeff_low; |
| 569 |
if (CX.w[0] < coeff_low) |
| 570 |
CX.w[1]++; |
| 571 |
} |
| 572 |
get_BID128 (&res, sign_x, dec_expon, CX,&rnd_mode,pfpsf); |
| 573 |
BID_RETURN (res); |
| 574 |
} else { |
| 575 |
// simply round using the digits that were read |
| 576 |
|
| 577 |
dec_expon += |
| 578 |
ndigits_before + DECIMAL_EXPONENT_BIAS_128 - |
| 579 |
MAX_FORMAT_DIGITS_128 - right_radix_leading_zeros; |
| 580 |
|
| 581 |
if (dec_expon < 0) { |
| 582 |
res.w[1] = 0 | sign_x; |
| 583 |
res.w[0] = 0; |
| 584 |
} |
| 585 |
|
| 586 |
coeff_high = buffer[0] - '0'; |
| 587 |
for (i = 1; i < MAX_FORMAT_DIGITS_128 - 17; i++) { |
| 588 |
coeff2 = coeff_high + coeff_high; |
| 589 |
coeff_high = (coeff2 << 2) + coeff2 + buffer[i] - '0'; |
| 590 |
} |
| 591 |
coeff_low = buffer[i] - '0'; |
| 592 |
i++; |
| 593 |
for (; i < MAX_FORMAT_DIGITS_128; i++) { |
| 594 |
coeff_l2 = coeff_low + coeff_low; |
| 595 |
coeff_low = (coeff_l2 << 2) + coeff_l2 + buffer[i] - '0'; |
| 596 |
} |
| 597 |
switch(rnd_mode) { |
| 598 |
case ROUNDING_TO_NEAREST: |
| 599 |
carry = ((unsigned) ('4' - buffer[i])) >> 31; |
| 600 |
if ((buffer[i] == '5' && !(coeff_low & 1)) || dec_expon < 0) { |
| 601 |
if (dec_expon >= 0) { |
| 602 |
carry = 0; |
| 603 |
i++; |
| 604 |
} |
| 605 |
for (; i < ndigits_total; i++) { |
| 606 |
if (buffer[i] > '0') { |
| 607 |
carry = 1; |
| 608 |
break; |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
break; |
| 613 |
|
| 614 |
case ROUNDING_DOWN: |
| 615 |
if(sign_x) |
| 616 |
for (; i < ndigits_total; i++) { |
| 617 |
if (buffer[i] > '0') { |
| 618 |
carry = 1; |
| 619 |
break; |
| 620 |
} |
| 621 |
} |
| 622 |
break; |
| 623 |
case ROUNDING_UP: |
| 624 |
if(!sign_x) |
| 625 |
for (; i < ndigits_total; i++) { |
| 626 |
if (buffer[i] > '0') { |
| 627 |
carry = 1; |
| 628 |
break; |
| 629 |
} |
| 630 |
} |
| 631 |
break; |
| 632 |
case ROUNDING_TO_ZERO: |
| 633 |
carry=0; |
| 634 |
break; |
| 635 |
case ROUNDING_TIES_AWAY: |
| 636 |
carry = ((unsigned) ('4' - buffer[i])) >> 31; |
| 637 |
if (dec_expon < 0) { |
| 638 |
for (; i < ndigits_total; i++) { |
| 639 |
if (buffer[i] > '0') { |
| 640 |
carry = 1; |
| 641 |
break; |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
break; |
| 646 |
|
| 647 |
default: break; // default added to avoid compiler warning |
| 648 |
} |
| 649 |
// now form the coefficient as coeff_high*10^17+coeff_low+carry |
| 650 |
scale_high = 100000000000000000ull; |
| 651 |
if (dec_expon < 0) { |
| 652 |
if (dec_expon > -MAX_FORMAT_DIGITS_128) { |
| 653 |
scale_high = 1000000000000000000ull; |
| 654 |
coeff_low = (coeff_low << 3) + (coeff_low << 1); |
| 655 |
dec_expon--; |
| 656 |
} |
| 657 |
if (dec_expon == -MAX_FORMAT_DIGITS_128 |
| 658 |
&& coeff_high > 50000000000000000ull) |
| 659 |
carry = 0; |
| 660 |
} |
| 661 |
|
| 662 |
__mul_64x64_to_128_fast (CX, coeff_high, scale_high); |
| 663 |
|
| 664 |
coeff_low += carry; |
| 665 |
CX.w[0] += coeff_low; |
| 666 |
if (CX.w[0] < coeff_low) |
| 667 |
CX.w[1]++; |
| 668 |
|
| 669 |
|
| 670 |
get_BID128(&res, sign_x, dec_expon, CX, &rnd_mode, pfpsf); |
| 671 |
BID_RETURN (res); |
| 672 |
} |
| 673 |
} |