1 /* Manipulation of the bit representation of 'long double' quantities.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
20 #define _MATH_LDBL_H_ 1
25 /* To suit our callers we return *hi64 and *lo64 as if they came from
26 an ieee854 112 bit mantissa, that is, 48 bits in *hi64 (plus one
27 implicit bit) and 64 bits in *lo64. */
30 ldbl_extract_mantissa (int64_t *hi64
, uint64_t *lo64
, int *exp
, long double x
)
32 /* We have 105 bits of mantissa plus one implicit digit. Since
33 106 bits are representable we use the first implicit digit for
34 the number before the decimal point and the second implicit bit
35 as bit 53 of the mantissa. */
37 union ibm_extended_long_double u
;
40 *exp
= u
.d
[0].ieee
.exponent
- IEEE754_DOUBLE_BIAS
;
42 lo
= ((uint64_t) u
.d
[1].ieee
.mantissa0
<< 32) | u
.d
[1].ieee
.mantissa1
;
43 hi
= ((uint64_t) u
.d
[0].ieee
.mantissa0
<< 32) | u
.d
[0].ieee
.mantissa1
;
45 if (u
.d
[0].ieee
.exponent
!= 0)
49 /* If not a denormal or zero then we have an implicit 53rd bit. */
50 hi
|= (uint64_t) 1 << 52;
52 if (u
.d
[1].ieee
.exponent
!= 0)
53 lo
|= (uint64_t) 1 << 52;
55 /* A denormal is to be interpreted as having a biased exponent
59 /* We are going to shift 4 bits out of hi later, because we only
60 want 48 bits in *hi64. That means we want 60 bits in lo, but
61 we currently only have 53. Shift the value up. */
64 /* The lower double is normalized separately from the upper.
65 We may need to adjust the lower mantissa to reflect this.
66 The difference between the exponents can be larger than 53
67 when the low double is much less than 1ULP of the upper
68 (in which case there are significant bits, all 0's or all
69 1's, between the two significands). The difference between
70 the exponents can be less than 53 when the upper double
71 exponent is nearing its minimum value (in which case the low
72 double is denormal ie. has an exponent of zero). */
73 ediff
= u
.d
[0].ieee
.exponent
- u
.d
[1].ieee
.exponent
- 53;
84 if (u
.d
[0].ieee
.negative
!= u
.d
[1].ieee
.negative
88 lo
= ((uint64_t) 1 << 60) - lo
;
89 if (hi
< (uint64_t) 1 << 52)
91 /* We have a borrow from the hidden bit, so shift left 1. */
92 hi
= (hi
<< 1) | (lo
>> 59);
93 lo
= (((uint64_t) 1 << 60) - 1) & (lo
<< 1);
99 /* If the larger magnitude double is denormal then the smaller
103 *lo64
= (hi
<< 60) | lo
;
107 static inline long double
108 ldbl_insert_mantissa (int sign
, int exp
, int64_t hi64
, uint64_t lo64
)
110 union ibm_extended_long_double u
;
114 u
.d
[0].ieee
.negative
= sign
;
115 u
.d
[1].ieee
.negative
= sign
;
116 u
.d
[0].ieee
.exponent
= exp
+ IEEE754_DOUBLE_BIAS
;
117 u
.d
[1].ieee
.exponent
= 0;
118 expnt2
= exp
- 53 + IEEE754_DOUBLE_BIAS
;
120 /* Expect 113 bits (112 bits + hidden) right justified in two longs.
121 The low order 53 bits (52 + hidden) go into the lower double */
122 lo
= (lo64
>> 7) & (((uint64_t) 1 << 53) - 1);
123 /* The high order 53 bits (52 + hidden) go into the upper double */
131 /* hidden bit of low double controls rounding of the high double.
132 If hidden is '1' and either the explicit mantissa is non-zero
133 or hi is odd, then round up hi and adjust lo (2nd mantissa)
134 plus change the sign of the low double to compensate. */
135 if ((lo
& ((uint64_t) 1 << 52)) != 0
136 && ((hi
& 1) != 0 || (lo
& (((uint64_t) 1 << 52) - 1)) != 0))
139 if ((hi
& ((uint64_t) 1 << 53)) != 0)
142 u
.d
[0].ieee
.exponent
++;
144 u
.d
[1].ieee
.negative
= !sign
;
145 lo
= ((uint64_t) 1 << 53) - lo
;
148 /* Normalize the low double. Shift the mantissa left until
149 the hidden bit is '1' and adjust the exponent accordingly. */
151 if (sizeof (lo
) == sizeof (long))
152 lzcount
= __builtin_clzl (lo
);
153 else if ((lo
>> 32) != 0)
154 lzcount
= __builtin_clzl ((long) (lo
>> 32));
156 lzcount
= __builtin_clzl ((long) lo
) + 32;
157 lzcount
= lzcount
- (64 - 53);
163 u
.d
[1].ieee
.exponent
= expnt2
;
166 /* Is denormal. Note that biased exponent of 0 is treated
167 as if it was 1, hence the extra shift. */
175 u
.d
[1].ieee
.negative
= 0;
177 u
.d
[1].ieee
.mantissa1
= lo
;
178 u
.d
[1].ieee
.mantissa0
= lo
>> 32;
179 u
.d
[0].ieee
.mantissa1
= hi
;
180 u
.d
[0].ieee
.mantissa0
= hi
>> 32;
184 /* Handy utility functions to pack/unpack/cononicalize and find the nearbyint
185 of long double implemented as double double. */
186 static inline long double
187 default_ldbl_pack (double a
, double aa
)
189 union ibm_extended_long_double u
;
196 default_ldbl_unpack (long double l
, double *a
, double *aa
)
198 union ibm_extended_long_double u
;
205 # define ldbl_pack default_ldbl_pack
208 # define ldbl_unpack default_ldbl_unpack
211 /* Extract high double. */
212 #define ldbl_high(x) ((double) x)
214 /* Convert a finite long double to canonical form.
215 Does not handle +/-Inf properly. */
217 ldbl_canonicalize (double *a
, double *aa
)
222 xl
= (*a
- xh
) + *aa
;
227 /* Simple inline nearbyint (double) function.
228 Only works in the default rounding mode
229 but is useful in long double rounding functions. */
231 ldbl_nearbyint (double a
)
233 double two52
= 0x1p
52;
235 if (__glibc_likely ((__builtin_fabs (a
) < two52
)))
237 if (__glibc_likely ((a
> 0.0)))
242 else if (__glibc_likely ((a
< 0.0)))
251 /* Canonicalize a result from an integer rounding function, in any
252 rounding mode. *A and *AA are finite and integers, with *A being
253 nonzero; if the result is not already canonical, *AA is plus or
254 minus a power of 2 that does not exceed the least set bit in
257 ldbl_canonicalize_int (double *a
, double *aa
)
259 /* Previously we used EXTRACT_WORDS64 from math_private.h, but in order
260 to avoid including internal headers we duplicate that code here. */
262 union { double value
; uint64_t word
; } extractor
;
263 extractor
.value
= *a
;
265 extractor
.value
= *aa
;
266 aax
= extractor
.word
;
268 int expdiff
= ((ax
>> 52) & 0x7ff) - ((aax
>> 52) & 0x7ff);
273 /* Half way between two double values; noncanonical iff the
274 low bit of A's mantissa is 1. */
283 /* The sum can be represented in a single double. */
290 #endif /* math_ldbl.h */