]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (C) 1995-2017 Free Software Foundation, Inc. |
377a515b | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
377a515b | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
377a515b UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 RM |
17 | |
18 | #include "gmp.h" | |
19 | #include "gmp-impl.h" | |
20 | #include "longlong.h" | |
981993e1 | 21 | #include <ieee754.h> |
28f540f4 | 22 | #include <float.h> |
76235ed4 | 23 | #include <math.h> |
28f540f4 RM |
24 | #include <stdlib.h> |
25 | ||
26 | /* Convert a `long double' in IEEE854 standard double-precision format to a | |
27 | multi-precision integer representing the significand scaled up by its | |
28 | number of bits (64 for long double) and an integral power of two | |
29 | (MPN frexpl). */ | |
30 | ||
31 | mp_size_t | |
32 | __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size, | |
33 | int *expt, int *is_neg, | |
34 | long double value) | |
35 | { | |
36 | union ieee854_long_double u; | |
37 | u.d = value; | |
38 | ||
39 | *is_neg = u.ieee.negative; | |
40 | *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS; | |
41 | ||
42 | #if BITS_PER_MP_LIMB == 32 | |
43 | res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction. */ | |
44 | res_ptr[1] = u.ieee.mantissa0; /* High-order 32 bits. */ | |
45 | #define N 2 | |
46 | #elif BITS_PER_MP_LIMB == 64 | |
47 | /* Hopefully the compiler will combine the two bitfield extracts | |
48 | and this composition into just the original quadword extract. */ | |
d8a5edc2 | 49 | res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1; |
28f540f4 RM |
50 | #define N 1 |
51 | #else | |
52 | #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for" | |
53 | #endif | |
54 | ||
55 | if (u.ieee.exponent == 0) | |
56 | { | |
57 | /* A biased exponent of zero is a special case. | |
58 | Either it is a zero or it is a denormal number. */ | |
59 | if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2. */ | |
60 | /* It's zero. */ | |
61 | *expt = 0; | |
62 | else | |
63 | { | |
64 | /* It is a denormal number, meaning it has no implicit leading | |
350635a5 | 65 | one bit, and its exponent is in fact the format minimum. */ |
28f540f4 | 66 | int cnt; |
61cd9516 | 67 | |
28f540f4 RM |
68 | if (res_ptr[N - 1] != 0) |
69 | { | |
70 | count_leading_zeros (cnt, res_ptr[N - 1]); | |
71 | if (cnt != 0) | |
72 | { | |
73 | #if N == 2 | |
74 | res_ptr[N - 1] = res_ptr[N - 1] << cnt | |
75 | | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt)); | |
76 | res_ptr[0] <<= cnt; | |
77 | #else | |
78 | res_ptr[N - 1] <<= cnt; | |
79 | #endif | |
80 | } | |
61cd9516 | 81 | *expt = LDBL_MIN_EXP - 1 - cnt; |
28f540f4 RM |
82 | } |
83 | else | |
84 | { | |
85 | count_leading_zeros (cnt, res_ptr[0]); | |
86 | res_ptr[N - 1] = res_ptr[0] << cnt; | |
87 | res_ptr[0] = 0; | |
61cd9516 | 88 | *expt = LDBL_MIN_EXP - 1 - BITS_PER_MP_LIMB - cnt; |
28f540f4 RM |
89 | } |
90 | } | |
91 | } | |
92 | ||
93 | return N; | |
94 | } |