| 1 | /* Copyright (C) 1995-2020 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <https://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include "gmp.h" |
| 19 | #include "gmp-impl.h" |
| 20 | #include "longlong.h" |
| 21 | #include <ieee754.h> |
| 22 | #include <float.h> |
| 23 | #include <math.h> |
| 24 | #include <math_private.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | /* Convert a `long double' in IEEE854 quad-precision format to a |
| 28 | multi-precision integer representing the significand scaled up by its |
| 29 | number of bits (113 for long double) and an integral power of two |
| 30 | (MPN frexpl). */ |
| 31 | |
| 32 | mp_size_t |
| 33 | __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size, |
| 34 | int *expt, int *is_neg, |
| 35 | _Float128 value) |
| 36 | { |
| 37 | union ieee854_long_double u; |
| 38 | u.d = value; |
| 39 | |
| 40 | *is_neg = u.ieee.negative; |
| 41 | *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS; |
| 42 | |
| 43 | #if BITS_PER_MP_LIMB == 32 |
| 44 | res_ptr[0] = u.ieee.mantissa3; /* Low-order 32 bits of fraction. */ |
| 45 | res_ptr[1] = u.ieee.mantissa2; |
| 46 | res_ptr[2] = u.ieee.mantissa1; |
| 47 | res_ptr[3] = u.ieee.mantissa0; /* High-order 32 bits. */ |
| 48 | #define N 4 |
| 49 | #elif BITS_PER_MP_LIMB == 64 |
| 50 | /* Hopefully the compiler will combine the two bitfield extracts |
| 51 | and this composition into just the original quadword extract. */ |
| 52 | res_ptr[0] = ((mp_limb_t) u.ieee.mantissa2 << 32) | u.ieee.mantissa3; |
| 53 | res_ptr[1] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1; |
| 54 | #define N 2 |
| 55 | #else |
| 56 | #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for" |
| 57 | #endif |
| 58 | /* The format does not fill the last limb. There are some zeros. */ |
| 59 | #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \ |
| 60 | - (LDBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB))) |
| 61 | |
| 62 | if (u.ieee.exponent == 0) |
| 63 | { |
| 64 | /* A biased exponent of zero is a special case. |
| 65 | Either it is a zero or it is a denormal number. */ |
| 66 | if (res_ptr[0] == 0 && res_ptr[1] == 0 |
| 67 | && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4. */ |
| 68 | /* It's zero. */ |
| 69 | *expt = 0; |
| 70 | else |
| 71 | { |
| 72 | /* It is a denormal number, meaning it has no implicit leading |
| 73 | one bit, and its exponent is in fact the format minimum. */ |
| 74 | int cnt; |
| 75 | |
| 76 | #if N == 2 |
| 77 | if (res_ptr[N - 1] != 0) |
| 78 | { |
| 79 | count_leading_zeros (cnt, res_ptr[N - 1]); |
| 80 | cnt -= NUM_LEADING_ZEROS; |
| 81 | res_ptr[N - 1] = res_ptr[N - 1] << cnt |
| 82 | | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt)); |
| 83 | res_ptr[0] <<= cnt; |
| 84 | *expt = LDBL_MIN_EXP - 1 - cnt; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | count_leading_zeros (cnt, res_ptr[0]); |
| 89 | if (cnt >= NUM_LEADING_ZEROS) |
| 90 | { |
| 91 | res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS); |
| 92 | res_ptr[0] = 0; |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt); |
| 97 | res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt); |
| 98 | } |
| 99 | *expt = LDBL_MIN_EXP - 1 |
| 100 | - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt; |
| 101 | } |
| 102 | #else |
| 103 | int j, k, l; |
| 104 | |
| 105 | for (j = N - 1; j > 0; j--) |
| 106 | if (res_ptr[j] != 0) |
| 107 | break; |
| 108 | |
| 109 | count_leading_zeros (cnt, res_ptr[j]); |
| 110 | cnt -= NUM_LEADING_ZEROS; |
| 111 | l = N - 1 - j; |
| 112 | if (cnt < 0) |
| 113 | { |
| 114 | cnt += BITS_PER_MP_LIMB; |
| 115 | l--; |
| 116 | } |
| 117 | if (!cnt) |
| 118 | for (k = N - 1; k >= l; k--) |
| 119 | res_ptr[k] = res_ptr[k-l]; |
| 120 | else |
| 121 | { |
| 122 | for (k = N - 1; k > l; k--) |
| 123 | res_ptr[k] = res_ptr[k-l] << cnt |
| 124 | | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt); |
| 125 | res_ptr[k--] = res_ptr[0] << cnt; |
| 126 | } |
| 127 | |
| 128 | for (; k >= 0; k--) |
| 129 | res_ptr[k] = 0; |
| 130 | *expt = LDBL_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt; |
| 131 | #endif |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | /* Add the implicit leading one bit for a normalized number. */ |
| 136 | res_ptr[N - 1] |= (mp_limb_t) 1 << (LDBL_MANT_DIG - 1 |
| 137 | - ((N - 1) * BITS_PER_MP_LIMB)); |
| 138 | |
| 139 | return N; |
| 140 | } |
| 141 | |