1 #ifndef _MATH_PRIVATE_H_
2 #error "Never use <math_ldbl.h> directly; include <math_private.h> instead."
8 /* To suit our callers we return *hi64 and *lo64 as if they came from
9 an ieee854 112 bit mantissa, that is, 48 bits in *hi64 (plus one
10 implicit bit) and 64 bits in *lo64. */
13 ldbl_extract_mantissa (int64_t *hi64
, uint64_t *lo64
, int *exp
, long double x
)
15 /* We have 105 bits of mantissa plus one implicit digit. Since
16 106 bits are representable we use the first implicit digit for
17 the number before the decimal point and the second implicit bit
18 as bit 53 of the mantissa. */
20 union ibm_extended_long_double u
;
23 *exp
= u
.d
[0].ieee
.exponent
- IEEE754_DOUBLE_BIAS
;
25 lo
= ((uint64_t) u
.d
[1].ieee
.mantissa0
<< 32) | u
.d
[1].ieee
.mantissa1
;
26 hi
= ((uint64_t) u
.d
[0].ieee
.mantissa0
<< 32) | u
.d
[0].ieee
.mantissa1
;
28 if (u
.d
[0].ieee
.exponent
!= 0)
32 /* If not a denormal or zero then we have an implicit 53rd bit. */
33 hi
|= (uint64_t) 1 << 52;
35 if (u
.d
[1].ieee
.exponent
!= 0)
36 lo
|= (uint64_t) 1 << 52;
38 /* A denormal is to be interpreted as having a biased exponent
42 /* We are going to shift 4 bits out of hi later, because we only
43 want 48 bits in *hi64. That means we want 60 bits in lo, but
44 we currently only have 53. Shift the value up. */
47 /* The lower double is normalized separately from the upper.
48 We may need to adjust the lower mantissa to reflect this.
49 The difference between the exponents can be larger than 53
50 when the low double is much less than 1ULP of the upper
51 (in which case there are significant bits, all 0's or all
52 1's, between the two significands). The difference between
53 the exponents can be less than 53 when the upper double
54 exponent is nearing its minimum value (in which case the low
55 double is denormal ie. has an exponent of zero). */
56 ediff
= u
.d
[0].ieee
.exponent
- u
.d
[1].ieee
.exponent
- 53;
67 if (u
.d
[0].ieee
.negative
!= u
.d
[1].ieee
.negative
71 lo
= ((uint64_t) 1 << 60) - lo
;
72 if (hi
< (uint64_t) 1 << 52)
74 /* We have a borrow from the hidden bit, so shift left 1. */
75 hi
= (hi
<< 1) | (lo
>> 59);
76 lo
= (((uint64_t) 1 << 60) - 1) & (lo
<< 1);
82 /* If the larger magnitude double is denormal then the smaller
86 *lo64
= (hi
<< 60) | lo
;
90 static inline long double
91 ldbl_insert_mantissa (int sign
, int exp
, int64_t hi64
, uint64_t lo64
)
93 union ibm_extended_long_double u
;
97 u
.d
[0].ieee
.negative
= sign
;
98 u
.d
[1].ieee
.negative
= sign
;
99 u
.d
[0].ieee
.exponent
= exp
+ IEEE754_DOUBLE_BIAS
;
100 u
.d
[1].ieee
.exponent
= 0;
101 expnt2
= exp
- 53 + IEEE754_DOUBLE_BIAS
;
103 /* Expect 113 bits (112 bits + hidden) right justified in two longs.
104 The low order 53 bits (52 + hidden) go into the lower double */
105 lo
= (lo64
>> 7) & (((uint64_t) 1 << 53) - 1);
106 /* The high order 53 bits (52 + hidden) go into the upper double */
114 /* hidden bit of low double controls rounding of the high double.
115 If hidden is '1' and either the explicit mantissa is non-zero
116 or hi is odd, then round up hi and adjust lo (2nd mantissa)
117 plus change the sign of the low double to compensate. */
118 if ((lo
& ((uint64_t) 1 << 52)) != 0
119 && ((hi
& 1) != 0 || (lo
& (((uint64_t) 1 << 52) - 1)) != 0))
122 if ((hi
& ((uint64_t) 1 << 53)) != 0)
125 u
.d
[0].ieee
.exponent
++;
127 u
.d
[1].ieee
.negative
= !sign
;
128 lo
= ((uint64_t) 1 << 53) - lo
;
131 /* Normalize the low double. Shift the mantissa left until
132 the hidden bit is '1' and adjust the exponent accordingly. */
134 if (sizeof (lo
) == sizeof (long))
135 lzcount
= __builtin_clzl (lo
);
136 else if ((lo
>> 32) != 0)
137 lzcount
= __builtin_clzl ((long) (lo
>> 32));
139 lzcount
= __builtin_clzl ((long) lo
) + 32;
140 lzcount
= lzcount
- (64 - 53);
146 u
.d
[1].ieee
.exponent
= expnt2
;
149 /* Is denormal. Note that biased exponent of 0 is treated
150 as if it was 1, hence the extra shift. */
158 u
.d
[1].ieee
.negative
= 0;
160 u
.d
[1].ieee
.mantissa1
= lo
;
161 u
.d
[1].ieee
.mantissa0
= lo
>> 32;
162 u
.d
[0].ieee
.mantissa1
= hi
;
163 u
.d
[0].ieee
.mantissa0
= hi
>> 32;
167 /* Handy utility functions to pack/unpack/cononicalize and find the nearbyint
168 of long double implemented as double double. */
169 static inline long double
170 default_ldbl_pack (double a
, double aa
)
172 union ibm_extended_long_double u
;
179 default_ldbl_unpack (long double l
, double *a
, double *aa
)
181 union ibm_extended_long_double u
;
188 # define ldbl_pack default_ldbl_pack
191 # define ldbl_unpack default_ldbl_unpack
194 /* Extract high double. */
195 #define ldbl_high(x) ((double) x)
197 /* Convert a finite long double to canonical form.
198 Does not handle +/-Inf properly. */
200 ldbl_canonicalize (double *a
, double *aa
)
205 xl
= (*a
- xh
) + *aa
;
210 /* Simple inline nearbyint (double) function.
211 Only works in the default rounding mode
212 but is useful in long double rounding functions. */
214 ldbl_nearbyint (double a
)
216 double two52
= 0x1p
52;
218 if (__glibc_likely ((__builtin_fabs (a
) < two52
)))
220 if (__glibc_likely ((a
> 0.0)))
225 else if (__glibc_likely ((a
< 0.0)))