1 | /* Convert a number in multi-precision format to _Float128. |
2 | Copyright (C) 2017-2023 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include "gmp.h" |
20 | #include "gmp-impl.h" |
21 | #include <float.h> |
22 | #include <math.h> |
23 | |
24 | #include "ieee754_float128.h" |
25 | |
26 | /* Convert a multi-precision integer of the needed number of bits (113 for |
27 | binary128) and an integral power of two to a `_Float128' in IEEE854 |
28 | quad-precision format. */ |
29 | |
30 | _Float128 |
31 | __mpn_construct_float128 (mp_srcptr frac_ptr, int expt, int sign) |
32 | { |
33 | union ieee854_float128 u; |
34 | |
35 | u.ieee.negative = sign; |
36 | u.ieee.exponent = expt + IEEE854_FLOAT128_BIAS; |
37 | #if BITS_PER_MP_LIMB == 32 |
38 | u.ieee.mantissa3 = frac_ptr[0]; |
39 | u.ieee.mantissa2 = frac_ptr[1]; |
40 | u.ieee.mantissa1 = frac_ptr[2]; |
41 | u.ieee.mantissa0 = frac_ptr[3] & (((mp_limb_t) 1 |
42 | << (FLT128_MANT_DIG - 96)) - 1); |
43 | #elif BITS_PER_MP_LIMB == 64 |
44 | u.ieee.mantissa3 = frac_ptr[0] & (((mp_limb_t) 1 << 32) - 1); |
45 | u.ieee.mantissa2 = frac_ptr[0] >> 32; |
46 | u.ieee.mantissa1 = frac_ptr[1] & (((mp_limb_t) 1 << 32) - 1); |
47 | u.ieee.mantissa0 = (frac_ptr[1] >> 32) & (((mp_limb_t) 1 |
48 | << (FLT128_MANT_DIG - 96)) - 1); |
49 | #else |
50 | #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for" |
51 | #endif |
52 | |
53 | return u.d; |
54 | } |
55 | |