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
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_fixsdbitint (UBILtype
*, SItype
, _Decimal32
);
34 __bid_fixsdbitint (UBILtype
*r
, SItype rprec
, _Decimal32 a
)
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
;
43 USItype exp_bits
, mant_bits
;
44 UBILtype
*pow10v
, *resv
;
45 USItype pow10_limbs
, res_limbs
, min_limbs
, mant_limbs
, low_zeros
;
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;
61 if (mantissa
> (USItype
) 9999999)
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
];
103 __bid_pow10bitint (limbs
, 64, -exponent
);
104 #if BIL_TYPE_SIZE == 64
106 #elif BIL_TYPE_SIZE == 32
107 d
= (limbs
[BITINT_END (0, 1)] << 32) | limbs
[BITINT_END (1, 0)];
109 # error Unsupported BIL_TYPE_SIZE
111 rem
= mantissa
% (USItype
) d
;
112 mantissa
/= (USItype
) d
;
114 FP_SET_EXCEPTION (FP_EX_INEXACT
);
120 if (rprec
>= 0 && sgn
)
123 FP_SET_EXCEPTION (FP_EX_INVALID
| FP_EX_INVALID_CVI
);
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)
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;
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
;
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)])
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)
159 min_limbs
= rn
- low_zeros
;
162 min_limbs
= res_limbs
;
164 __builtin_memset (r
+ BITINT_END (rn
- low_zeros
, 0), '\0',
165 low_zeros
* sizeof (UBILtype
));
167 bitint_negate (r
+ BITINT_END (rn
- low_zeros
- 1, low_zeros
),
168 resv
+ BITINT_END (res_limbs
- 1, 0), min_limbs
);
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
)
176 __builtin_memset (r
+ BITINT_END (0, res_limbs
+ low_zeros
), -1,
177 (rn
- res_limbs
- low_zeros
) * sizeof (UBILtype
));
179 __builtin_memset (r
+ BITINT_END (0, res_limbs
+ low_zeros
), '\0',
180 (rn
- res_limbs
- low_zeros
) * sizeof (UBILtype
));
184 if ((r
[BITINT_END (0, rn
- 1)]
185 & ((UBILtype
) 1 << ((arprec
- 1) % BIL_TYPE_SIZE
))) == 0)
189 && (r
[BITINT_END (0, rn
- 1)]
190 & ((UBILtype
) 1 << ((arprec
- 1) % BIL_TYPE_SIZE
))) != 0)
194 FP_HANDLE_EXCEPTIONS
;