1/* Copyright (C) 1993-2022 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 <stdlib.h>
24
25/* Convert a `double' in IEEE754 standard double-precision format to a
26 multi-precision integer representing the significand scaled up by its
27 number of bits (52 for double) and an integral power of two (MPN frexp). */
28
29mp_size_t
30__mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
31 int *expt, int *is_neg,
32 double value)
33{
34 union ieee754_double u;
35 u.d = value;
36
37 *is_neg = u.ieee.negative;
38 *expt = (int) u.ieee.exponent - IEEE754_DOUBLE_BIAS;
39
40#if BITS_PER_MP_LIMB == 32
41 res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction. */
42 res_ptr[1] = u.ieee.mantissa0; /* High-order 20 bits. */
43 # define N 2
44#elif BITS_PER_MP_LIMB == 64
45 /* Hopefully the compiler will combine the two bitfield extracts
46 and this composition into just the original quadword extract. */
47 res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
48 # define N 1
49#else
50 # error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
51#endif
52/* The format does not fill the last limb. There are some zeros. */
53#define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
54 - (DBL_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
55
56 if (u.ieee.exponent == 0)
57 {
58 /* A biased exponent of zero is a special case.
59 Either it is a zero or it is a denormal number. */
60 if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2. */
61 /* It's zero. */
62 *expt = 0;
63 else
64 {
65 /* It is a denormal number, meaning it has no implicit leading
66 one bit, and its exponent is in fact the format minimum. */
67 int cnt;
68
69 if (res_ptr[N - 1] != 0)
70 {
71 count_leading_zeros (cnt, res_ptr[N - 1]);
72 cnt -= NUM_LEADING_ZEROS;
73#if N == 2
74 res_ptr[N - 1] = res_ptr[1] << cnt
75 | (N - 1)
76 * (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
77 res_ptr[0] <<= cnt;
78#else
79 res_ptr[N - 1] <<= cnt;
80#endif
81 *expt = DBL_MIN_EXP - 1 - cnt;
82 }
83 else
84 {
85 count_leading_zeros (cnt, res_ptr[0]);
86 if (cnt >= NUM_LEADING_ZEROS)
87 {
88 res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
89 res_ptr[0] = 0;
90 }
91 else
92 {
93 res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
94 res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
95 }
96 *expt = DBL_MIN_EXP - 1
97 - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
98 }
99 }
100 }
101 else
102 /* Add the implicit leading one bit for a normalized number. */
103 res_ptr[N - 1] |= (mp_limb_t) 1 << (DBL_MANT_DIG - 1
104 - ((N - 1) * BITS_PER_MP_LIMB));
105
106 return N;
107}
108

source code of glibc/sysdeps/ieee754/dbl-64/dbl2mpn.c