Home | History | Annotate | Download | only in ppc
      1 /* This file is distributed under the University of Illinois Open Source
      2  * License. See LICENSE.TXT for details.
      3  */
      4 
      5 /* int64_t __fixunstfdi(long double x);
      6  * This file implements the PowerPC 128-bit double-double -> int64_t conversion
      7  */
      8 
      9 #include "DD.h"
     10 #include "../int_math.h"
     11 
     12 uint64_t __fixtfdi(long double input)
     13 {
     14 	const DD x = { .ld = input };
     15 	const doublebits hibits = { .d = x.s.hi };
     16 
     17 	const uint32_t absHighWord = (uint32_t)(hibits.x >> 32) & UINT32_C(0x7fffffff);
     18 	const uint32_t absHighWordMinusOne = absHighWord - UINT32_C(0x3ff00000);
     19 
     20 	/* If (1.0 - tiny) <= input < 0x1.0p63: */
     21 	if (UINT32_C(0x03f00000) > absHighWordMinusOne)
     22 	{
     23 		/* Do an unsigned conversion of the absolute value, then restore the sign. */
     24 		const int unbiasedHeadExponent = absHighWordMinusOne >> 20;
     25 
     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 preceeding bit. */
     29 
     30 		const int64_t hiNegationMask = ((int64_t)(hibits.x)) >> 63;
     31 
     32 		/* If the tail is non-zero, we need to patch in the tail bits. */
     33 		if (0.0 != x.s.lo)
     34 		{
     35 			const doublebits lobits = { .d = x.s.lo };
     36 			int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
     37 			tailMantissa |= INT64_C(0x0010000000000000);
     38 
     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;
     44 
     45 			/* Now we have the mantissa of tail as a signed 2s-complement integer */
     46 
     47 			const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
     48 
     49 			/* Shift the tail mantissa into the right position, accounting for the
     50 			 * bias of 10 that we shifted the head mantissa by.
     51 			 */
     52 			tailMantissa >>= (unbiasedHeadExponent - (biasedTailExponent - (1023 - 10)));
     53 
     54 			result += tailMantissa;
     55 		}
     56 
     57 		result >>= (62 - unbiasedHeadExponent);
     58 
     59 		/* Restore the sign of the result and return */
     60 		result = (result ^ hiNegationMask) - hiNegationMask;
     61 		return result;
     62 
     63 	}
     64 
     65 	/* Edge cases handled here: */
     66 
     67 	/* |x| < 1, result is zero. */
     68 	if (1.0 > crt_fabs(x.s.hi))
     69 		return INT64_C(0);
     70 
     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) {
     73 
     74 		int64_t result = INT64_MIN;
     75 
     76 		if (0.0 < x.s.lo)
     77 		{
     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.
     80 			 */
     81 
     82 			const doublebits lobits = { .d = x.s.lo };
     83 			int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
     84 			tailMantissa |= INT64_C(0x0010000000000000);
     85 
     86 			/* Now we negate the tailMantissa */
     87 			tailMantissa = (tailMantissa ^ INT64_C(-1)) + INT64_C(1);
     88 
     89 			/* And shift it by the appropriate amount */
     90 			const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
     91 			tailMantissa >>= 1075 - biasedTailExponent;
     92 
     93 			result -= tailMantissa;
     94 		}
     95 
     96 		return result;
     97 	}
     98 
     99 	/* Signed overflows, infinities, and NaNs */
    100 	if (x.s.hi > 0.0)
    101 		return INT64_MAX;
    102 	else
    103 		return INT64_MIN;
    104 }
    105