arc: Fix -Wincompatible-pointer-types warning during libgcc build
[official-gcc.git] / libgcc / soft-fp / fixsdbitint.c
blob98e3ab9f13a3ccbe3e9e2cc3c826c23f15219b46
1 /* Software floating-point emulation.
2 Convert _Decimal32 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
11 version.
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.
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/>. */
27 #include "soft-fp.h"
28 #include "bitint.h"
30 #ifdef __BITINT_MAXWIDTH__
31 extern void __bid_fixsdbitint (UBILtype *, SItype, _Decimal32);
33 void
34 __bid_fixsdbitint (UBILtype *r, SItype rprec, _Decimal32 a)
36 FP_DECL_EX;
37 USItype arprec = rprec < 0 ? -rprec : rprec;
38 USItype rn = (arprec + BIL_TYPE_SIZE - 1) / BIL_TYPE_SIZE;
39 union { _Decimal32 d; USItype u; } u;
40 USItype 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;
47 FP_INIT_EXCEPTIONS;
48 u.d = a;
49 t = u.u >> 21;
50 sgn = (SItype) u.u < 0;
51 if ((t & (3 << 8)) != (3 << 8))
53 mantissa = u.u & ((((USItype) 1) << 23) - 1);
54 exponent = (t >> 2) & 0xff;
56 else if ((t & (3 << 6)) != (3 << 6))
58 mantissa = u.u & ((((USItype) 1) << 21) - 1);
59 mantissa |= ((USItype) 1) << 23;
60 exponent = t & 0xff;
61 if (mantissa > (USItype) 9999999)
62 mantissa = 0;
64 else
66 FP_SET_EXCEPTION (FP_EX_INVALID
67 | FP_EX_INVALID_CVI
68 | ((FP_EX_INVALID_SNAN
69 && ((t & 0x20)) != 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;
84 exponent -= 101;
86 if (mantissa == 0)
88 /* Zero (with any exponent). */
89 zero:
90 __builtin_memset (r, 0, rn * sizeof (UBILtype));
91 goto done;
93 if (exponent <= -7)
95 FP_SET_EXCEPTION (FP_EX_INEXACT);
96 goto zero;
98 else if (exponent < 0)
100 UBILtype limbs[64 / BIL_TYPE_SIZE];
101 USItype rem;
102 UDItype d;
103 __bid_pow10bitint (limbs, 64, -exponent);
104 #if BIL_TYPE_SIZE == 64
105 d = limbs[0];
106 #elif BIL_TYPE_SIZE == 32
107 d = (limbs[BITINT_END (0, 1)] << 32) | limbs[BITINT_END (1, 0)];
108 #else
109 # error Unsupported BIL_TYPE_SIZE
110 #endif
111 rem = mantissa % (USItype) d;
112 mantissa /= (USItype) d;
113 if (rem)
114 FP_SET_EXCEPTION (FP_EX_INEXACT);
115 if (mantissa == 0)
116 goto zero;
117 exponent = 0;
120 if (rprec >= 0 && sgn)
122 ovf_ex:
123 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI);
124 goto ovf;
127 /* Lower estimate for number of bits needed for pow10 (exponent). */
128 exp_bits = exponent / 3;
129 exp_bits = exp_bits * 10 - exp_bits / 29;
130 mant_bits = sizeof (0ULL) * __CHAR_BIT__ - __builtin_clzll (mantissa);
131 if (exp_bits + mant_bits > arprec + 1)
132 goto ovf_ex;
133 /* Upper estimate for number of bits needed for pow10 (exponent). */
134 exp_bits = (exponent + 2) / 3;
135 exp_bits = exp_bits * 10 - exp_bits / 30;
136 if (exp_bits == 0)
137 exp_bits = 1;
138 pow10_limbs = (exp_bits + BIL_TYPE_SIZE - 1) / BIL_TYPE_SIZE;
139 pow10v = __builtin_alloca (pow10_limbs * sizeof (UBILtype));
140 low_zeros = __bid_pow10bitint (pow10v, exp_bits, exponent);
142 res_limbs = ((exp_bits + mant_bits + BIL_TYPE_SIZE - 1)
143 / BIL_TYPE_SIZE) - low_zeros;
144 mant_limbs = 1;
145 resv = __builtin_alloca ((res_limbs + mant_limbs) * sizeof (UBILtype));
146 resv[res_limbs] = mantissa;
147 __mulbitint3 (resv, exp_bits + mant_bits - low_zeros * BIL_TYPE_SIZE,
148 resv + res_limbs, mant_bits,
149 pow10v + BITINT_END (0, low_zeros),
150 exp_bits - low_zeros * BIL_TYPE_SIZE);
151 if (res_limbs + low_zeros >= rn)
153 if (res_limbs + low_zeros > rn && resv[BITINT_END (0, res_limbs - 1)])
154 goto ovf_ex;
155 if ((arprec % BIL_TYPE_SIZE) != 0
156 && (resv[BITINT_END (rn - res_limbs, rn - 1) - low_zeros]
157 & ((UBILtype) -1 << (arprec % BIL_TYPE_SIZE))) != 0)
158 goto ovf_ex;
159 min_limbs = rn - low_zeros;
161 else
162 min_limbs = res_limbs;
163 if (low_zeros)
164 __builtin_memset (r + BITINT_END (rn - low_zeros, 0), '\0',
165 low_zeros * sizeof (UBILtype));
166 if (sgn)
167 bitint_negate (r + BITINT_END (rn - low_zeros - 1, low_zeros),
168 resv + BITINT_END (res_limbs - 1, 0), min_limbs);
169 else
170 __builtin_memcpy (r + BITINT_END (rn - low_zeros - min_limbs, low_zeros),
171 resv + BITINT_END (res_limbs - min_limbs, 0),
172 min_limbs * sizeof (UBILtype));
173 if (res_limbs + low_zeros < rn)
175 if (sgn)
176 __builtin_memset (r + BITINT_END (0, res_limbs + low_zeros), -1,
177 (rn - res_limbs - low_zeros) * sizeof (UBILtype));
178 else
179 __builtin_memset (r + BITINT_END (0, res_limbs + low_zeros), '\0',
180 (rn - res_limbs - low_zeros) * sizeof (UBILtype));
182 else if (sgn)
184 if ((r[BITINT_END (0, rn - 1)]
185 & ((UBILtype) 1 << ((arprec - 1) % BIL_TYPE_SIZE))) == 0)
186 goto ovf_ex;
188 else if (rprec < 0
189 && (r[BITINT_END (0, rn - 1)]
190 & ((UBILtype) 1 << ((arprec - 1) % BIL_TYPE_SIZE))) != 0)
191 goto ovf_ex;
193 done:
194 FP_HANDLE_EXCEPTIONS;
196 #endif