1 /* This file is distributed under the University of Illinois Open Source
2 * License. See LICENSE.TXT for details.
5 /* int64_t __fixunstfdi(long double x);
6 * This file implements the PowerPC 128-bit double-double -> int64_t conversion
10 #include "../int_math.h"
12 uint64_t __fixtfdi(long double input
)
14 const DD x
= { .ld
= input
};
15 const doublebits hibits
= { .d
= x
.s
.hi
};
17 const uint32_t absHighWord
= (uint32_t)(hibits
.x
>> 32) & UINT32_C(0x7fffffff);
18 const uint32_t absHighWordMinusOne
= absHighWord
- UINT32_C(0x3ff00000);
20 /* If (1.0 - tiny) <= input < 0x1.0p63: */
21 if (UINT32_C(0x03f00000) > absHighWordMinusOne
)
23 /* Do an unsigned conversion of the absolute value, then restore the sign. */
24 const int unbiasedHeadExponent
= absHighWordMinusOne
>> 20;
26 int64_t result
= hibits
.x
& INT64_C(0x000fffffffffffff); /* mantissa(hi) */
27 result
|= INT64_C(0x0010000000000000); /* matissa(hi) with implicit bit */
28 result
<<= 10; /* mantissa(hi) with one zero preceding bit. */
30 const int64_t hiNegationMask
= ((int64_t)(hibits
.x
)) >> 63;
32 /* If the tail is non-zero, we need to patch in the tail bits. */
35 const doublebits lobits
= { .d
= x
.s
.lo
};
36 int64_t tailMantissa
= lobits
.x
& INT64_C(0x000fffffffffffff);
37 tailMantissa
|= INT64_C(0x0010000000000000);
39 /* At this point we have the mantissa of |tail| */
40 /* We need to negate it if head and tail have different signs. */
41 const int64_t loNegationMask
= ((int64_t)(lobits
.x
)) >> 63;
42 const int64_t negationMask
= loNegationMask
^ hiNegationMask
;
43 tailMantissa
= (tailMantissa
^ negationMask
) - negationMask
;
45 /* Now we have the mantissa of tail as a signed 2s-complement integer */
47 const int biasedTailExponent
= (int)(lobits
.x
>> 52) & 0x7ff;
49 /* Shift the tail mantissa into the right position, accounting for the
50 * bias of 10 that we shifted the head mantissa by.
52 tailMantissa
>>= (unbiasedHeadExponent
- (biasedTailExponent
- (1023 - 10)));
54 result
+= tailMantissa
;
57 result
>>= (62 - unbiasedHeadExponent
);
59 /* Restore the sign of the result and return */
60 result
= (result
^ hiNegationMask
) - hiNegationMask
;
65 /* Edge cases handled here: */
67 /* |x| < 1, result is zero. */
68 if (1.0 > crt_fabs(x
.s
.hi
))
71 /* x very close to INT64_MIN, care must be taken to see which side we are on. */
72 if (x
.s
.hi
== -0x1.0p63
) {
74 int64_t result
= INT64_MIN
;
78 /* If the tail is positive, the correct result is something other than INT64_MIN.
79 * we'll need to figure out what it is.
82 const doublebits lobits
= { .d
= x
.s
.lo
};
83 int64_t tailMantissa
= lobits
.x
& INT64_C(0x000fffffffffffff);
84 tailMantissa
|= INT64_C(0x0010000000000000);
86 /* Now we negate the tailMantissa */
87 tailMantissa
= (tailMantissa
^ INT64_C(-1)) + INT64_C(1);
89 /* And shift it by the appropriate amount */
90 const int biasedTailExponent
= (int)(lobits
.x
>> 52) & 0x7ff;
91 tailMantissa
>>= 1075 - biasedTailExponent
;
93 result
-= tailMantissa
;
99 /* Signed overflows, infinities, and NaNs */