1 /* Copyright (C) 2007-2015 Free Software Foundation, Inc.
3 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
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
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.
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/>. */
24 /*****************************************************************************
26 ****************************************************************************/
30 #include "bid_internal.h"
31 #include "bid128_2_str.h"
32 #include "bid128_2_str_macros.h"
34 extern int bid128_coeff_2_string (UINT64 X_hi
, UINT64 X_lo
,
37 #if DECIMAL_CALL_BY_REFERENCE
40 bid128_to_string (char *str
,
42 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
48 bid128_to_string (char *str
, UINT128 x
49 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM
) {
53 int exp
; // unbiased exponent
54 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
57 unsigned int k
= 0; // pointer in the string
58 unsigned int d0
, d123
;
59 UINT64 HI_18Dig
, LO_18Dig
, Tmp
;
60 UINT32 MiDi
[12], *ptr
;
61 char *c_ptr_start
, *c_ptr
;
62 int midi_ind
, k_lcv
, len
;
64 #if DECIMAL_CALL_BY_REFERENCE
69 // check for NaN or Infinity
70 if ((x
.w
[1] & MASK_SPECIAL
) == MASK_SPECIAL
) {
72 if ((x
.w
[1] & MASK_NAN
) == MASK_NAN
) { // x is NAN
73 if ((x
.w
[1] & MASK_SNAN
) == MASK_SNAN
) { // x is SNAN
75 str
[0] = ((SINT64
)x
.w
[1]<0)? '-':'+';
82 str
[0] = ((SINT64
)x
.w
[1]<0)? '-':'+';
89 } else { // x is not a NaN, so it must be infinity
90 if ((x
.w
[1] & MASK_SIGN
) == 0x0ull
) { // x is +inf
105 } else if (((x
.w
[1] & MASK_COEFF
) == 0x0ull
) && (x
.w
[0] == 0x0ull
)) {
110 if (x
.w
[1] & MASK_SIGN
)
117 // extract the exponent and print
118 exp
= (int) (((x
.w
[1] & MASK_EXP
) >> 49) - 6176);
119 if(exp
> (((0x5ffe)>>1) - (6176))) {
120 exp
= (int) ((((x
.w
[1]<<2) & MASK_EXP
) >> 49) - 6176);
124 len
+= sprintf (str
+ len
, "%u", exp
);// should not use sprintf (should
125 // use sophisticated algorithm, since we know range of exp is limited)
128 len
+= sprintf (str
+ len
, "%d", exp
);// should not use sprintf (should
129 // use sophisticated algorithm, since we know range of exp is limited)
133 } else { // x is not special and is not zero
135 x_sign
= x
.w
[1] & MASK_SIGN
;// 0 for positive, MASK_SIGN for negative
136 x_exp
= x
.w
[1] & MASK_EXP
;// biased and shifted left 49 bit positions
137 if ((x
.w
[1] & 0x6000000000000000ull
) == 0x6000000000000000ull
)
138 x_exp
= (x
.w
[1]<<2) & MASK_EXP
;// biased and shifted left 49 bit positions
139 C1
.w
[1] = x
.w
[1] & MASK_COEFF
;
141 exp
= (x_exp
>> 49) - 6176;
143 // determine sign's representation as a char
145 str
[k
++] = '-';// negative number
147 str
[k
++] = '+';// positive number
149 // determine coefficient's representation as a decimal string
151 // if zero or non-canonical, set coefficient to '0'
152 if ((C1
.w
[1] > 0x0001ed09bead87c0ull
) ||
153 (C1
.w
[1] == 0x0001ed09bead87c0ull
&&
154 (C1
.w
[0] > 0x378d8e63ffffffffull
)) ||
155 ((x
.w
[1] & 0x6000000000000000ull
) == 0x6000000000000000ull
) ||
156 ((C1
.w
[1] == 0) && (C1
.w
[0] == 0))) {
159 /* ****************************************************
160 This takes a bid coefficient in C1.w[1],C1.w[0]
161 and put the converted character sequence at location
162 starting at &(str[k]). The function returns the number
163 of MiDi returned. Note that the character sequence
164 does not have leading zeros EXCEPT when the input is of
165 zero value. It will then output 1 character '0'
166 The algorithm essentailly tries first to get a sequence of
167 Millenial Digits "MiDi" and then uses table lookup to get the
168 character strings of these MiDis.
169 **************************************************** */
170 /* Algorithm first decompose possibly 34 digits in hi and lo
171 18 digits. (The high can have at most 16 digits). It then
172 uses macro that handle 18 digit portions.
173 The first step is to get hi and lo such that
174 2^(64) C1.w[1] + C1.w[0] = hi * 10^18 + lo, 0 <= lo < 10^18.
175 We use a table lookup method to obtain the hi and lo 18 digits.
176 [C1.w[1],C1.w[0]] = c_8 2^(107) + c_7 2^(101) + ... + c_0 2^(59) + d
177 where 0 <= d < 2^59 and each c_j has 6 bits. Because d fits in
178 18 digits, we set hi = 0, and lo = d to begin with.
179 We then retrieve from a table, for j = 0, 1, ..., 8
180 that gives us A and B where c_j 2^(59+6j) = A * 10^18 + B.
181 hi += A ; lo += B; After each accumulation into lo, we normalize
182 immediately. So at the end, we have the decomposition as we need. */
185 LO_18Dig
= (C1
.w
[0] << 5) >> 5;
186 Tmp
+= (C1
.w
[1] << 5);
189 // Tmp = {C1.w[1]{49:0}, C1.w[0]{63:59}}
190 // Lo_18Dig = {C1.w[0]{58:0}}
193 midi_ind
= (int) (Tmp
& 0x000000000000003FLL
);
196 HI_18Dig
+= mod10_18_tbl
[k_lcv
][midi_ind
++];
197 LO_18Dig
+= mod10_18_tbl
[k_lcv
++][midi_ind
];
198 __L0_Normalize_10to18 (HI_18Dig
, LO_18Dig
);
201 if (HI_18Dig
== 0LL) {
202 __L1_Split_MiDi_6_Lead (LO_18Dig
, ptr
);
204 __L1_Split_MiDi_6_Lead (HI_18Dig
, ptr
);
205 __L1_Split_MiDi_6 (LO_18Dig
, ptr
);
208 c_ptr_start
= &(str
[k
]);
211 /* now convert the MiDi into character strings */
212 __L0_MiDi2Str_Lead (MiDi
[0], c_ptr
);
213 for (k_lcv
= 1; k_lcv
< len
; k_lcv
++) {
214 __L0_MiDi2Str (MiDi
[k_lcv
], c_ptr
);
216 k
= k
+ (c_ptr
- c_ptr_start
);
219 // print E and sign of exponent
228 // determine exponent's representation as a decimal string
231 d0
= (exp
* 0x418a) >> 24;// 0x418a * 2^-24 = (10^(-3))RP,15
232 d123
= exp
- 1000 * d0
;
234 if (d0
) { // 1000 <= exp <= 6144 => 4 digits to return
235 str
[k
++] = d0
+ 0x30;// ASCII for decimal digit d0
237 str
[k
++] = char_table3
[ind
];
238 str
[k
++] = char_table3
[ind
+ 1];
239 str
[k
++] = char_table3
[ind
+ 2];
240 } else { // 0 <= exp <= 999 => d0 = 0
241 if (d123
< 10) { // 0 <= exp <= 9 => 1 digit to return
242 str
[k
++] = d123
+ 0x30;// ASCII
243 } else if (d123
< 100) { // 10 <= exp <= 99 => 2 digits to return
244 ind
= 2 * (d123
- 10);
245 str
[k
++] = char_table2
[ind
];
246 str
[k
++] = char_table2
[ind
+ 1];
247 } else { // 100 <= exp <= 999 => 3 digits to return
249 str
[k
++] = char_table3
[ind
];
250 str
[k
++] = char_table3
[ind
+ 1];
251 str
[k
++] = char_table3
[ind
+ 2];
262 #define MAX_FORMAT_DIGITS_128 34
263 #define MAX_STRING_DIGITS_128 100
264 #define MAX_SEARCH MAX_STRING_DIGITS_128-MAX_FORMAT_DIGITS_128-1
267 #if DECIMAL_CALL_BY_REFERENCE
270 bid128_from_string (UINT128
* pres
,
271 char *ps _RND_MODE_PARAM _EXC_FLAGS_PARAM
272 _EXC_MASKS_PARAM _EXC_INFO_PARAM
) {
276 bid128_from_string (char *ps _RND_MODE_PARAM _EXC_FLAGS_PARAM
277 _EXC_MASKS_PARAM _EXC_INFO_PARAM
) {
280 UINT64 sign_x
, coeff_high
, coeff_low
, coeff2
, coeff_l2
, carry
= 0x0ull
,
281 scale_high
, right_radix_leading_zeros
;
282 int ndigits_before
, ndigits_after
, ndigits_total
, dec_expon
, sgn_exp
,
284 char c
, buffer
[MAX_STRING_DIGITS_128
];
288 #if DECIMAL_CALL_BY_REFERENCE
289 #if !DECIMAL_GLOBAL_ROUNDING
290 _IDEC_round rnd_mode
= *prnd_mode
;
294 save_rnd_mode
= rnd_mode
; // dummy
295 save_fpsf
= *pfpsf
; // dummy
297 right_radix_leading_zeros
= rdx_pt_enc
= 0;
299 // if null string, return NaN
301 res
.w
[1] = 0x7c00000000000000ull
;
305 // eliminate leading white space
306 while ((*ps
== ' ') || (*ps
== '\t'))
309 // c gets first character
313 // if c is null or not equal to a (radix point, negative sign,
314 // positive sign, or number) it might be SNaN, sNaN, Infinity
316 || (c
!= '.' && c
!= '-' && c
!= '+'
317 && ((unsigned) (c
- '0') > 9))) {
320 if ((tolower_macro (ps
[0]) == 'i' && tolower_macro (ps
[1]) == 'n'
321 && tolower_macro (ps
[2]) == 'f')
323 || (tolower_macro (ps
[3]) == 'i'
324 && tolower_macro (ps
[4]) == 'n'
325 && tolower_macro (ps
[5]) == 'i'
326 && tolower_macro (ps
[6]) == 't'
327 && tolower_macro (ps
[7]) == 'y' && !ps
[8])
329 res
.w
[1] = 0x7800000000000000ull
;
333 if (tolower_macro (ps
[0]) == 's' && tolower_macro (ps
[1]) == 'n' &&
334 tolower_macro (ps
[2]) == 'a' && tolower_macro (ps
[3]) == 'n') {
335 // case insensitive check for snan
336 res
.w
[1] = 0x7e00000000000000ull
;
340 res
.w
[1] = 0x7c00000000000000ull
;
344 // if +Inf, -Inf, +Infinity, or -Infinity (case insensitive check for inf)
345 if ((tolower_macro (ps
[1]) == 'i' && tolower_macro (ps
[2]) == 'n' &&
346 tolower_macro (ps
[3]) == 'f') && (!ps
[4] ||
347 (tolower_macro (ps
[4]) == 'i' && tolower_macro (ps
[5]) == 'n' &&
348 tolower_macro (ps
[6]) == 'i' && tolower_macro (ps
[7]) == 't' &&
349 tolower_macro (ps
[8]) == 'y' && !ps
[9]))) { // ci check for infinity
353 res
.w
[1] = 0x7800000000000000ull
;
355 res
.w
[1] = 0xf800000000000000ull
;
357 res
.w
[1] = 0x7c00000000000000ull
;
361 // if +sNaN, +SNaN, -sNaN, or -SNaN
362 if (tolower_macro (ps
[1]) == 's' && tolower_macro (ps
[2]) == 'n'
363 && tolower_macro (ps
[3]) == 'a' && tolower_macro (ps
[4]) == 'n') {
366 res
.w
[1] = 0xfe00000000000000ull
;
368 res
.w
[1] = 0x7e00000000000000ull
;
371 // set up sign_x to be OR'ed with the upper word later
373 sign_x
= 0x8000000000000000ull
;
377 // go to next character if leading sign
378 if (c
== '-' || c
== '+')
383 // if c isn't a decimal point or a decimal digit, return NaN
384 if (c
!= '.' && ((unsigned) (c
- '0') > 9)) {
385 res
.w
[1] = 0x7c00000000000000ull
| sign_x
;
389 // detect zero (and eliminate/ignore leading zeros)
392 // if all numbers are zeros (with possibly 1 radix point, the number is zero
393 // should catch cases such as: 000.0
398 // for numbers such as 0.0000000000000000000000000000000000001001,
399 // we want to count the leading zeros
401 right_radix_leading_zeros
++;
403 // if this character is a radix point, make sure we haven't already
406 if (rdx_pt_enc
== 0) {
408 // if this is the first radix point, and the next character is NULL,
412 (0x3040000000000000ull
-
413 (right_radix_leading_zeros
<< 49)) | sign_x
;
419 // if 2 radix points, return NaN
420 res
.w
[1] = 0x7c00000000000000ull
| sign_x
;
425 //res.w[1] = 0x3040000000000000ull | sign_x;
427 (0x3040000000000000ull
-
428 (right_radix_leading_zeros
<< 49)) | sign_x
;
437 // initialize local variables
438 ndigits_before
= ndigits_after
= ndigits_total
= 0;
440 // pstart_coefficient = ps;
443 // investigate string (before radix point)
444 while ((unsigned) (c
- '0') <= 9
445 && ndigits_before
< MAX_STRING_DIGITS_128
) {
446 buffer
[ndigits_before
] = c
;
452 ndigits_total
= ndigits_before
;
457 // investigate string (after radix point)
458 while ((unsigned) (c
- '0') <= 9
459 && ndigits_total
< MAX_STRING_DIGITS_128
) {
460 buffer
[ndigits_total
] = c
;
465 ndigits_after
= ndigits_total
- ndigits_before
;
469 // we encountered a radix point while detecting zeros
474 // investigate string (after radix point)
475 while ((unsigned) (c
- '0') <= 9
476 && ndigits_total
< MAX_STRING_DIGITS_128
) {
477 buffer
[ndigits_total
] = c
;
482 ndigits_after
= ndigits_total
- ndigits_before
;
487 if (ndigits_total
< MAX_STRING_DIGITS_128
) {
489 if (c
!= 'e' && c
!= 'E') {
491 res
.w
[1] = 0x7c00000000000000ull
;
498 if (((unsigned) (c
- '0') > 9)
499 && ((c
!= '+' && c
!= '-') || (unsigned) (ps
[1] - '0') > 9)) {
501 res
.w
[1] = 0x7c00000000000000ull
;
510 } else if (c
== '+') {
519 while (((unsigned) c
) <= 9 && i
< 7) {
520 d2
= dec_expon
+ dec_expon
;
521 dec_expon
= (d2
<< 2) + d2
+ c
;
528 dec_expon
= (dec_expon
+ sgn_exp
) ^ sgn_exp
;
532 if (ndigits_total
<= MAX_FORMAT_DIGITS_128
) {
534 DECIMAL_EXPONENT_BIAS_128
- ndigits_after
-
535 right_radix_leading_zeros
;
537 res
.w
[1] = 0 | sign_x
;
540 if (ndigits_total
== 0) {
543 } else if (ndigits_total
<= 19) {
544 coeff_high
= buffer
[0] - '0';
545 for (i
= 1; i
< ndigits_total
; i
++) {
546 coeff2
= coeff_high
+ coeff_high
;
547 coeff_high
= (coeff2
<< 2) + coeff2
+ buffer
[i
] - '0';
549 CX
.w
[0] = coeff_high
;
552 coeff_high
= buffer
[0] - '0';
553 for (i
= 1; i
< ndigits_total
- 17; i
++) {
554 coeff2
= coeff_high
+ coeff_high
;
555 coeff_high
= (coeff2
<< 2) + coeff2
+ buffer
[i
] - '0';
557 coeff_low
= buffer
[i
] - '0';
559 for (; i
< ndigits_total
; i
++) {
560 coeff_l2
= coeff_low
+ coeff_low
;
561 coeff_low
= (coeff_l2
<< 2) + coeff_l2
+ buffer
[i
] - '0';
563 // now form the coefficient as coeff_high*10^19+coeff_low+carry
564 scale_high
= 100000000000000000ull;
565 __mul_64x64_to_128_fast (CX
, coeff_high
, scale_high
);
567 CX
.w
[0] += coeff_low
;
568 if (CX
.w
[0] < coeff_low
)
571 get_BID128 (&res
, sign_x
, dec_expon
, CX
,&rnd_mode
,pfpsf
);
574 // simply round using the digits that were read
577 ndigits_before
+ DECIMAL_EXPONENT_BIAS_128
-
578 MAX_FORMAT_DIGITS_128
- right_radix_leading_zeros
;
581 res
.w
[1] = 0 | sign_x
;
585 coeff_high
= buffer
[0] - '0';
586 for (i
= 1; i
< MAX_FORMAT_DIGITS_128
- 17; i
++) {
587 coeff2
= coeff_high
+ coeff_high
;
588 coeff_high
= (coeff2
<< 2) + coeff2
+ buffer
[i
] - '0';
590 coeff_low
= buffer
[i
] - '0';
592 for (; i
< MAX_FORMAT_DIGITS_128
; i
++) {
593 coeff_l2
= coeff_low
+ coeff_low
;
594 coeff_low
= (coeff_l2
<< 2) + coeff_l2
+ buffer
[i
] - '0';
597 case ROUNDING_TO_NEAREST
:
598 carry
= ((unsigned) ('4' - buffer
[i
])) >> 31;
599 if ((buffer
[i
] == '5' && !(coeff_low
& 1)) || dec_expon
< 0) {
600 if (dec_expon
>= 0) {
604 for (; i
< ndigits_total
; i
++) {
605 if (buffer
[i
] > '0') {
615 for (; i
< ndigits_total
; i
++) {
616 if (buffer
[i
] > '0') {
624 for (; i
< ndigits_total
; i
++) {
625 if (buffer
[i
] > '0') {
631 case ROUNDING_TO_ZERO
:
634 case ROUNDING_TIES_AWAY
:
635 carry
= ((unsigned) ('4' - buffer
[i
])) >> 31;
637 for (; i
< ndigits_total
; i
++) {
638 if (buffer
[i
] > '0') {
648 // now form the coefficient as coeff_high*10^17+coeff_low+carry
649 scale_high
= 100000000000000000ull;
651 if (dec_expon
> -MAX_FORMAT_DIGITS_128
) {
652 scale_high
= 1000000000000000000ull;
653 coeff_low
= (coeff_low
<< 3) + (coeff_low
<< 1);
656 if (dec_expon
== -MAX_FORMAT_DIGITS_128
657 && coeff_high
> 50000000000000000ull)
661 __mul_64x64_to_128_fast (CX
, coeff_high
, scale_high
);
664 CX
.w
[0] += coeff_low
;
665 if (CX
.w
[0] < coeff_low
)
669 get_BID128(&res
, sign_x
, dec_expon
, CX
, &rnd_mode
, pfpsf
);