1 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 // See https://llvm.org/LICENSE.txt for license information.
3 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 // int64_t __fixunstfdi(long double x);
6 // This file implements the PowerPC 128-bit double-double -> int64_t conversion
8 #include "../int_math.h"
11 uint64_t __fixtfdi(long double input
) {
12 const DD x
= {.ld
= input
};
13 const doublebits hibits
= {.d
= x
.s
.hi
};
15 const uint32_t absHighWord
=
16 (uint32_t)(hibits
.x
>> 32) & UINT32_C(0x7fffffff);
17 const uint32_t absHighWordMinusOne
= absHighWord
- UINT32_C(0x3ff00000);
19 // If (1.0 - tiny) <= input < 0x1.0p63:
20 if (UINT32_C(0x03f00000) > absHighWordMinusOne
) {
21 // Do an unsigned conversion of the absolute value, then restore the sign.
22 const int unbiasedHeadExponent
= absHighWordMinusOne
>> 20;
24 int64_t result
= hibits
.x
& INT64_C(0x000fffffffffffff); // mantissa(hi)
25 result
|= INT64_C(0x0010000000000000); // matissa(hi) with implicit bit
26 result
<<= 10; // mantissa(hi) with one zero preceding bit.
28 const int64_t hiNegationMask
= ((int64_t)(hibits
.x
)) >> 63;
30 // If the tail is non-zero, we need to patch in the tail bits.
32 const doublebits lobits
= {.d
= x
.s
.lo
};
33 int64_t tailMantissa
= lobits
.x
& INT64_C(0x000fffffffffffff);
34 tailMantissa
|= INT64_C(0x0010000000000000);
36 // At this point we have the mantissa of |tail|
37 // We need to negate it if head and tail have different signs.
38 const int64_t loNegationMask
= ((int64_t)(lobits
.x
)) >> 63;
39 const int64_t negationMask
= loNegationMask
^ hiNegationMask
;
40 tailMantissa
= (tailMantissa
^ negationMask
) - negationMask
;
42 // Now we have the mantissa of tail as a signed 2s-complement integer
44 const int biasedTailExponent
= (int)(lobits
.x
>> 52) & 0x7ff;
46 // Shift the tail mantissa into the right position, accounting for the
47 // bias of 10 that we shifted the head mantissa by.
49 (unbiasedHeadExponent
- (biasedTailExponent
- (1023 - 10)));
51 result
+= tailMantissa
;
54 result
>>= (62 - unbiasedHeadExponent
);
56 // Restore the sign of the result and return
57 result
= (result
^ hiNegationMask
) - hiNegationMask
;
61 // Edge cases handled here:
63 // |x| < 1, result is zero.
64 if (1.0 > crt_fabs(x
.s
.hi
))
67 // x very close to INT64_MIN, care must be taken to see which side we are on.
68 if (x
.s
.hi
== -0x1.0p63
) {
70 int64_t result
= INT64_MIN
;
73 // If the tail is positive, the correct result is something other than
74 // INT64_MIN. we'll need to figure out what it is.
76 const doublebits lobits
= {.d
= x
.s
.lo
};
77 int64_t tailMantissa
= lobits
.x
& INT64_C(0x000fffffffffffff);
78 tailMantissa
|= INT64_C(0x0010000000000000);
80 // Now we negate the tailMantissa
81 tailMantissa
= (tailMantissa
^ INT64_C(-1)) + INT64_C(1);
83 // And shift it by the appropriate amount
84 const int biasedTailExponent
= (int)(lobits
.x
>> 52) & 0x7ff;
85 tailMantissa
>>= 1075 - biasedTailExponent
;
87 result
-= tailMantissa
;
93 // Signed overflows, infinities, and NaNs