| 1 |
/* Software floating-point emulation. |
| 2 |
Convert _Decimal64 to signed or unsigned _BitInt. |
| 3 |
|
| 4 |
Copyright (C) 2023 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GCC. |
| 7 |
|
| 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 |
| 11 |
version. |
| 12 |
|
| 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 |
| 16 |
for more details. |
| 17 |
|
| 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. |
| 21 |
|
| 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/>. */ |
| 26 |
|
| 27 |
#include "soft-fp.h" |
| 28 |
#include "bitint.h" |
| 29 |
|
| 30 |
#ifdef __BITINT_MAXWIDTH__ |
| 31 |
extern void __bid_fixddbitint (UBILtype *, SItype, _Decimal64); |
| 32 |
|
| 33 |
void |
| 34 |
__bid_fixddbitint (UBILtype *r, SItype rprec, _Decimal64 a) |
| 35 |
{ |
| 36 |
FP_DECL_EX; |
| 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; |
| 40 |
UDItype mantissa, t; |
| 41 |
SItype sgn; |
| 42 |
SItype exponent; |
| 43 |
USItype exp_bits, mant_bits; |
| 44 |
UBILtype *pow10v, *resv; |
| 45 |
USItype pow10_limbs, res_limbs, min_limbs, mant_limbs, low_zeros; |
| 46 |
|
| 47 |
FP_INIT_EXCEPTIONS; |
| 48 |
u.d = a; |
| 49 |
t = u.u >> 51; |
| 50 |
sgn = (DItype) u.u < 0; |
| 51 |
if ((t & (3 << 10)) != (3 << 10)) |
| 52 |
{ |
| 53 |
mantissa = u.u & ((((UDItype) 1) << 53) - 1); |
| 54 |
exponent = (t >> 2) & 0x3ff; |
| 55 |
} |
| 56 |
else if ((t & (3 << 8)) != (3 << 8)) |
| 57 |
{ |
| 58 |
mantissa = u.u & ((((UDItype) 1) << 51) - 1); |
| 59 |
mantissa |= ((UDItype) 1) << 53; |
| 60 |
exponent = t & 0x3ff; |
| 61 |
if (mantissa > (UDItype) 9999999999999999) |
| 62 |
mantissa = 0; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
FP_SET_EXCEPTION (FP_EX_INVALID |
| 67 |
| FP_EX_INVALID_CVI |
| 68 |
| ((FP_EX_INVALID_SNAN |
| 69 |
&& ((t & 0x80)) != 0) |
| 70 |
? FP_EX_INVALID_SNAN : 0)); |
| 71 |
ovf: |
| 72 |
if (!sgn) |
| 73 |
__builtin_memset (r, -1, rn * sizeof (UBILtype)); |
| 74 |
else |
| 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); |
| 79 |
else |
| 80 |
r[BITINT_END (0, rn - 1)] |
| 81 |
&= ~((UBILtype) -1 << ((arprec - 1) % BIL_TYPE_SIZE)); |
| 82 |
goto done; |
| 83 |
} |
| 84 |
exponent -= 398; |
| 85 |
|
| 86 |
if (mantissa == 0) |
| 87 |
{ |
| 88 |
/* Zero (with any exponent). */ |
| 89 |
zero: |
| 90 |
__builtin_memset (r, 0, rn * sizeof (UBILtype)); |
| 91 |
goto done; |
| 92 |
} |
| 93 |
if (exponent <= -16) |
| 94 |
{ |
| 95 |
FP_SET_EXCEPTION (FP_EX_INEXACT); |
| 96 |
goto zero; |
| 97 |
} |
| 98 |
else if (exponent < 0) |
| 99 |
{ |
| 100 |
UBILtype limbs[64 / BIL_TYPE_SIZE]; |
| 101 |
UDItype d, rem; |
| 102 |
__bid_pow10bitint (limbs, 64, -exponent); |
| 103 |
#if BIL_TYPE_SIZE == 64 |
| 104 |
d = limbs[0]; |
| 105 |
#elif BIL_TYPE_SIZE == 32 |
| 106 |
d = (UDItype) limbs[BITINT_END (0, 1)] << 32 | limbs[BITINT_END (1, 0)]; |
| 107 |
#else |
| 108 |
# error Unsupported BIL_TYPE_SIZE |
| 109 |
#endif |
| 110 |
rem = mantissa % d; |
| 111 |
mantissa /= d; |
| 112 |
if (rem) |
| 113 |
FP_SET_EXCEPTION (FP_EX_INEXACT); |
| 114 |
if (mantissa == 0) |
| 115 |
goto zero; |
| 116 |
exponent = 0; |
| 117 |
} |
| 118 |
|
| 119 |
if (rprec >= 0 && sgn) |
| 120 |
{ |
| 121 |
ovf_ex: |
| 122 |
FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); |
| 123 |
goto ovf; |
| 124 |
} |
| 125 |
|
| 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) |
| 131 |
goto ovf_ex; |
| 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; |
| 135 |
if (exp_bits == 0) |
| 136 |
exp_bits = 1; |
| 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); |
| 140 |
|
| 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; |
| 147 |
#else |
| 148 |
if (mant_limbs == 1) |
| 149 |
resv[res_limbs] = mantissa; |
| 150 |
else |
| 151 |
{ |
| 152 |
resv[res_limbs + BITINT_END (1, 0)] = mantissa; |
| 153 |
resv[res_limbs + BITINT_END (0, 1)] = mantissa >> 32; |
| 154 |
} |
| 155 |
#endif |
| 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) |
| 161 |
{ |
| 162 |
if (res_limbs + low_zeros > rn && resv[BITINT_END (0, res_limbs - 1)]) |
| 163 |
goto ovf_ex; |
| 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) |
| 167 |
goto ovf_ex; |
| 168 |
min_limbs = rn - low_zeros; |
| 169 |
} |
| 170 |
else |
| 171 |
min_limbs = res_limbs; |
| 172 |
if (low_zeros) |
| 173 |
__builtin_memset (r + BITINT_END (rn - low_zeros, 0), '\0', |
| 174 |
low_zeros * sizeof (UBILtype)); |
| 175 |
if (sgn) |
| 176 |
bitint_negate (r + BITINT_END (rn - low_zeros - 1, low_zeros), |
| 177 |
resv + BITINT_END (res_limbs - 1, 0), min_limbs); |
| 178 |
else |
| 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) |
| 183 |
{ |
| 184 |
if (sgn) |
| 185 |
__builtin_memset (r + BITINT_END (0, res_limbs + low_zeros), -1, |
| 186 |
(rn - res_limbs - low_zeros) * sizeof (UBILtype)); |
| 187 |
else |
| 188 |
__builtin_memset (r + BITINT_END (0, res_limbs + low_zeros), '\0', |
| 189 |
(rn - res_limbs - low_zeros) * sizeof (UBILtype)); |
| 190 |
} |
| 191 |
else if (sgn) |
| 192 |
{ |
| 193 |
if ((r[BITINT_END (0, rn - 1)] |
| 194 |
& ((UBILtype) 1 << ((arprec - 1) % BIL_TYPE_SIZE))) == 0) |
| 195 |
goto ovf_ex; |
| 196 |
} |
| 197 |
else if (rprec < 0 |
| 198 |
&& (r[BITINT_END (0, rn - 1)] |
| 199 |
& ((UBILtype) 1 << ((arprec - 1) % BIL_TYPE_SIZE))) != 0) |
| 200 |
goto ovf_ex; |
| 201 |
|
| 202 |
done: |
| 203 |
FP_HANDLE_EXCEPTIONS; |
| 204 |
} |
| 205 |
#endif |