1 /* Software floating-point emulation.
2 Convert _Decimal64 to signed or unsigned _BitInt.
4 Copyright (C) 2023 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
30 #ifdef __BITINT_MAXWIDTH__
31 extern void __bid_fixddbitint (UBILtype
*, SItype
, _Decimal64
);
34 __bid_fixddbitint (UBILtype
*r
, SItype rprec
, _Decimal64 a
)
37 USItype arprec
= rprec
< 0 ? -rprec
: rprec
;
38 USItype rn
= (arprec
+ BIL_TYPE_SIZE
- 1) / BIL_TYPE_SIZE
;
39 union { _Decimal64 d
; UDItype u
; } u
;
43 USItype exp_bits
, mant_bits
;
44 UBILtype
*pow10v
, *resv
;
45 USItype pow10_limbs
, res_limbs
, min_limbs
, mant_limbs
, low_zeros
;
50 sgn
= (DItype
) u
.u
< 0;
51 if ((t
& (3 << 10)) != (3 << 10))
53 mantissa
= u
.u
& ((((UDItype
) 1) << 53) - 1);
54 exponent
= (t
>> 2) & 0x3ff;
56 else if ((t
& (3 << 8)) != (3 << 8))
58 mantissa
= u
.u
& ((((UDItype
) 1) << 51) - 1);
59 mantissa
|= ((UDItype
) 1) << 53;
61 if (mantissa
> (UDItype
) 9999999999999999)
66 FP_SET_EXCEPTION (FP_EX_INVALID
68 | ((FP_EX_INVALID_SNAN
70 ? FP_EX_INVALID_SNAN
: 0));
73 __builtin_memset (r
, -1, rn
* sizeof (UBILtype
));
75 __builtin_memset (r
, 0, rn
* sizeof (UBILtype
));
76 if (sgn
^ (rprec
>= 0))
77 r
[BITINT_END (0, rn
- 1)]
78 |= (UBILtype
) -1 << ((arprec
- 1) % BIL_TYPE_SIZE
);
80 r
[BITINT_END (0, rn
- 1)]
81 &= ~((UBILtype
) -1 << ((arprec
- 1) % BIL_TYPE_SIZE
));
88 /* Zero (with any exponent). */
90 __builtin_memset (r
, 0, rn
* sizeof (UBILtype
));
95 FP_SET_EXCEPTION (FP_EX_INEXACT
);
98 else if (exponent
< 0)
100 UBILtype limbs
[64 / BIL_TYPE_SIZE
];
102 __bid_pow10bitint (limbs
, 64, -exponent
);
103 #if BIL_TYPE_SIZE == 64
105 #elif BIL_TYPE_SIZE == 32
106 d
= (limbs
[BITINT_END (0, 1)] << 32) | limbs
[BITINT_END (1, 0)];
108 # error Unsupported BIL_TYPE_SIZE
113 FP_SET_EXCEPTION (FP_EX_INEXACT
);
119 if (rprec
>= 0 && sgn
)
122 FP_SET_EXCEPTION (FP_EX_INVALID
| FP_EX_INVALID_CVI
);
126 /* Lower estimate for number of bits needed for pow10 (exponent). */
127 exp_bits
= exponent
/ 3;
128 exp_bits
= exp_bits
* 10 - exp_bits
/ 29;
129 mant_bits
= sizeof (0ULL) * __CHAR_BIT__
- __builtin_clzll (mantissa
);
130 if (exp_bits
+ mant_bits
> arprec
+ 1)
132 /* Upper estimate for number of bits needed for pow10 (exponent). */
133 exp_bits
= (exponent
+ 2) / 3;
134 exp_bits
= exp_bits
* 10 - exp_bits
/ 30;
137 pow10_limbs
= (exp_bits
+ BIL_TYPE_SIZE
- 1) / BIL_TYPE_SIZE
;
138 pow10v
= __builtin_alloca (pow10_limbs
* sizeof (UBILtype
));
139 low_zeros
= __bid_pow10bitint (pow10v
, exp_bits
, exponent
);
141 res_limbs
= ((exp_bits
+ mant_bits
+ BIL_TYPE_SIZE
- 1)
142 / BIL_TYPE_SIZE
) - low_zeros
;
143 mant_limbs
= (mant_bits
+ BIL_TYPE_SIZE
- 1) / BIL_TYPE_SIZE
;
144 resv
= __builtin_alloca ((res_limbs
+ mant_limbs
) * sizeof (UBILtype
));
145 #if BIL_TYPE_SIZE >= 64
146 resv
[res_limbs
] = mantissa
;
149 resv
[res_limbs
] = mantissa
;
152 resv
[res_limbs
+ BITINT_END (1, 0)] = mantissa
;
153 resv
[res_limbs
+ BITINT_END (0, 1)] = mantissa
>> 32;
156 __mulbitint3 (resv
, exp_bits
+ mant_bits
- low_zeros
* BIL_TYPE_SIZE
,
157 resv
+ res_limbs
, mant_bits
,
158 pow10v
+ BITINT_END (0, low_zeros
),
159 exp_bits
- low_zeros
* BIL_TYPE_SIZE
);
160 if (res_limbs
+ low_zeros
>= rn
)
162 if (res_limbs
+ low_zeros
> rn
&& resv
[BITINT_END (0, res_limbs
- 1)])
164 if ((arprec
% BIL_TYPE_SIZE
) != 0
165 && (resv
[BITINT_END (rn
- res_limbs
, rn
- 1) - low_zeros
]
166 & ((UBILtype
) -1 << (arprec
% BIL_TYPE_SIZE
))) != 0)
168 min_limbs
= rn
- low_zeros
;
171 min_limbs
= res_limbs
;
173 __builtin_memset (r
+ BITINT_END (rn
- low_zeros
, 0), '\0',
174 low_zeros
* sizeof (UBILtype
));
176 bitint_negate (r
+ BITINT_END (rn
- low_zeros
- 1, low_zeros
),
177 resv
+ BITINT_END (res_limbs
- 1, 0), min_limbs
);
179 __builtin_memcpy (r
+ BITINT_END (rn
- low_zeros
- min_limbs
, low_zeros
),
180 resv
+ BITINT_END (res_limbs
- min_limbs
, 0),
181 min_limbs
* sizeof (UBILtype
));
182 if (res_limbs
+ low_zeros
< rn
)
185 __builtin_memset (r
+ BITINT_END (0, res_limbs
+ low_zeros
), -1,
186 (rn
- res_limbs
- low_zeros
) * sizeof (UBILtype
));
188 __builtin_memset (r
+ BITINT_END (0, res_limbs
+ low_zeros
), '\0',
189 (rn
- res_limbs
- low_zeros
) * sizeof (UBILtype
));
193 if ((r
[BITINT_END (0, rn
- 1)]
194 & ((UBILtype
) 1 << ((arprec
- 1) % BIL_TYPE_SIZE
))) == 0)
198 && (r
[BITINT_END (0, rn
- 1)]
199 & ((UBILtype
) 1 << ((arprec
- 1) % BIL_TYPE_SIZE
))) != 0)
203 FP_HANDLE_EXCEPTIONS
;