xref: /aosp_15_r20/external/compiler-rt/lib/builtins/ppc/fixtfdi.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* This file is distributed under the University of Illinois Open Source
2*7c3d14c8STreehugger Robot  * License. See LICENSE.TXT for details.
3*7c3d14c8STreehugger Robot  */
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot /* int64_t __fixunstfdi(long double x);
6*7c3d14c8STreehugger Robot  * This file implements the PowerPC 128-bit double-double -> int64_t conversion
7*7c3d14c8STreehugger Robot  */
8*7c3d14c8STreehugger Robot 
9*7c3d14c8STreehugger Robot #include "DD.h"
10*7c3d14c8STreehugger Robot #include "../int_math.h"
11*7c3d14c8STreehugger Robot 
__fixtfdi(long double input)12*7c3d14c8STreehugger Robot uint64_t __fixtfdi(long double input)
13*7c3d14c8STreehugger Robot {
14*7c3d14c8STreehugger Robot 	const DD x = { .ld = input };
15*7c3d14c8STreehugger Robot 	const doublebits hibits = { .d = x.s.hi };
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot 	const uint32_t absHighWord = (uint32_t)(hibits.x >> 32) & UINT32_C(0x7fffffff);
18*7c3d14c8STreehugger Robot 	const uint32_t absHighWordMinusOne = absHighWord - UINT32_C(0x3ff00000);
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot 	/* If (1.0 - tiny) <= input < 0x1.0p63: */
21*7c3d14c8STreehugger Robot 	if (UINT32_C(0x03f00000) > absHighWordMinusOne)
22*7c3d14c8STreehugger Robot 	{
23*7c3d14c8STreehugger Robot 		/* Do an unsigned conversion of the absolute value, then restore the sign. */
24*7c3d14c8STreehugger Robot 		const int unbiasedHeadExponent = absHighWordMinusOne >> 20;
25*7c3d14c8STreehugger Robot 
26*7c3d14c8STreehugger Robot 		int64_t result = hibits.x & INT64_C(0x000fffffffffffff); /* mantissa(hi) */
27*7c3d14c8STreehugger Robot 		result |= INT64_C(0x0010000000000000); /* matissa(hi) with implicit bit */
28*7c3d14c8STreehugger Robot 		result <<= 10; /* mantissa(hi) with one zero preceding bit. */
29*7c3d14c8STreehugger Robot 
30*7c3d14c8STreehugger Robot 		const int64_t hiNegationMask = ((int64_t)(hibits.x)) >> 63;
31*7c3d14c8STreehugger Robot 
32*7c3d14c8STreehugger Robot 		/* If the tail is non-zero, we need to patch in the tail bits. */
33*7c3d14c8STreehugger Robot 		if (0.0 != x.s.lo)
34*7c3d14c8STreehugger Robot 		{
35*7c3d14c8STreehugger Robot 			const doublebits lobits = { .d = x.s.lo };
36*7c3d14c8STreehugger Robot 			int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
37*7c3d14c8STreehugger Robot 			tailMantissa |= INT64_C(0x0010000000000000);
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot 			/* At this point we have the mantissa of |tail| */
40*7c3d14c8STreehugger Robot 			/* We need to negate it if head and tail have different signs. */
41*7c3d14c8STreehugger Robot 			const int64_t loNegationMask = ((int64_t)(lobits.x)) >> 63;
42*7c3d14c8STreehugger Robot 			const int64_t negationMask = loNegationMask ^ hiNegationMask;
43*7c3d14c8STreehugger Robot 			tailMantissa = (tailMantissa ^ negationMask) - negationMask;
44*7c3d14c8STreehugger Robot 
45*7c3d14c8STreehugger Robot 			/* Now we have the mantissa of tail as a signed 2s-complement integer */
46*7c3d14c8STreehugger Robot 
47*7c3d14c8STreehugger Robot 			const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot 			/* Shift the tail mantissa into the right position, accounting for the
50*7c3d14c8STreehugger Robot 			 * bias of 10 that we shifted the head mantissa by.
51*7c3d14c8STreehugger Robot 			 */
52*7c3d14c8STreehugger Robot 			tailMantissa >>= (unbiasedHeadExponent - (biasedTailExponent - (1023 - 10)));
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot 			result += tailMantissa;
55*7c3d14c8STreehugger Robot 		}
56*7c3d14c8STreehugger Robot 
57*7c3d14c8STreehugger Robot 		result >>= (62 - unbiasedHeadExponent);
58*7c3d14c8STreehugger Robot 
59*7c3d14c8STreehugger Robot 		/* Restore the sign of the result and return */
60*7c3d14c8STreehugger Robot 		result = (result ^ hiNegationMask) - hiNegationMask;
61*7c3d14c8STreehugger Robot 		return result;
62*7c3d14c8STreehugger Robot 
63*7c3d14c8STreehugger Robot 	}
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot 	/* Edge cases handled here: */
66*7c3d14c8STreehugger Robot 
67*7c3d14c8STreehugger Robot 	/* |x| < 1, result is zero. */
68*7c3d14c8STreehugger Robot 	if (1.0 > crt_fabs(x.s.hi))
69*7c3d14c8STreehugger Robot 		return INT64_C(0);
70*7c3d14c8STreehugger Robot 
71*7c3d14c8STreehugger Robot 	/* x very close to INT64_MIN, care must be taken to see which side we are on. */
72*7c3d14c8STreehugger Robot 	if (x.s.hi == -0x1.0p63) {
73*7c3d14c8STreehugger Robot 
74*7c3d14c8STreehugger Robot 		int64_t result = INT64_MIN;
75*7c3d14c8STreehugger Robot 
76*7c3d14c8STreehugger Robot 		if (0.0 < x.s.lo)
77*7c3d14c8STreehugger Robot 		{
78*7c3d14c8STreehugger Robot 			/* If the tail is positive, the correct result is something other than INT64_MIN.
79*7c3d14c8STreehugger Robot 			 * we'll need to figure out what it is.
80*7c3d14c8STreehugger Robot 			 */
81*7c3d14c8STreehugger Robot 
82*7c3d14c8STreehugger Robot 			const doublebits lobits = { .d = x.s.lo };
83*7c3d14c8STreehugger Robot 			int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
84*7c3d14c8STreehugger Robot 			tailMantissa |= INT64_C(0x0010000000000000);
85*7c3d14c8STreehugger Robot 
86*7c3d14c8STreehugger Robot 			/* Now we negate the tailMantissa */
87*7c3d14c8STreehugger Robot 			tailMantissa = (tailMantissa ^ INT64_C(-1)) + INT64_C(1);
88*7c3d14c8STreehugger Robot 
89*7c3d14c8STreehugger Robot 			/* And shift it by the appropriate amount */
90*7c3d14c8STreehugger Robot 			const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
91*7c3d14c8STreehugger Robot 			tailMantissa >>= 1075 - biasedTailExponent;
92*7c3d14c8STreehugger Robot 
93*7c3d14c8STreehugger Robot 			result -= tailMantissa;
94*7c3d14c8STreehugger Robot 		}
95*7c3d14c8STreehugger Robot 
96*7c3d14c8STreehugger Robot 		return result;
97*7c3d14c8STreehugger Robot 	}
98*7c3d14c8STreehugger Robot 
99*7c3d14c8STreehugger Robot 	/* Signed overflows, infinities, and NaNs */
100*7c3d14c8STreehugger Robot 	if (x.s.hi > 0.0)
101*7c3d14c8STreehugger Robot 		return INT64_MAX;
102*7c3d14c8STreehugger Robot 	else
103*7c3d14c8STreehugger Robot 		return INT64_MIN;
104*7c3d14c8STreehugger Robot }
105