Home | History | Annotate | Line # | Download | only in ppc
      1  1.1  joerg /* This file is distributed under the University of Illinois Open Source
      2  1.1  joerg  * License. See LICENSE.TXT for details.
      3  1.1  joerg  */
      4  1.1  joerg 
      5  1.1  joerg /* uint64_t __fixunstfdi(long double x); */
      6  1.1  joerg /* This file implements the PowerPC 128-bit double-double -> uint64_t conversion */
      7  1.1  joerg 
      8  1.1  joerg #include "DD.h"
      9  1.1  joerg 
     10  1.1  joerg uint64_t __fixunstfdi(long double input)
     11  1.1  joerg {
     12  1.1  joerg 	const DD x = { .ld = input };
     13  1.1  joerg 	const doublebits hibits = { .d = x.s.hi };
     14  1.1  joerg 
     15  1.1  joerg 	const uint32_t highWordMinusOne = (uint32_t)(hibits.x >> 32) - UINT32_C(0x3ff00000);
     16  1.1  joerg 
     17  1.1  joerg 	/* If (1.0 - tiny) <= input < 0x1.0p64: */
     18  1.1  joerg 	if (UINT32_C(0x04000000) > highWordMinusOne)
     19  1.1  joerg 	{
     20  1.1  joerg 		const int unbiasedHeadExponent = highWordMinusOne >> 20;
     21  1.1  joerg 
     22  1.1  joerg 		uint64_t result = hibits.x & UINT64_C(0x000fffffffffffff); /* mantissa(hi) */
     23  1.1  joerg 		result |= UINT64_C(0x0010000000000000); /* matissa(hi) with implicit bit */
     24  1.1  joerg 		result <<= 11; /* mantissa(hi) left aligned in the int64 field. */
     25  1.1  joerg 
     26  1.1  joerg 		/* If the tail is non-zero, we need to patch in the tail bits. */
     27  1.1  joerg 		if (0.0 != x.s.lo)
     28  1.1  joerg 		{
     29  1.1  joerg 			const doublebits lobits = { .d = x.s.lo };
     30  1.1  joerg 			int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
     31  1.1  joerg 			tailMantissa |= INT64_C(0x0010000000000000);
     32  1.1  joerg 
     33  1.1  joerg 			/* At this point we have the mantissa of |tail| */
     34  1.1  joerg 
     35  1.1  joerg 			const int64_t negationMask = ((int64_t)(lobits.x)) >> 63;
     36  1.1  joerg 			tailMantissa = (tailMantissa ^ negationMask) - negationMask;
     37  1.1  joerg 
     38  1.1  joerg 			/* Now we have the mantissa of tail as a signed 2s-complement integer */
     39  1.1  joerg 
     40  1.1  joerg 			const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
     41  1.1  joerg 
     42  1.1  joerg 			/* Shift the tail mantissa into the right position, accounting for the
     43  1.1  joerg 			 * bias of 11 that we shifted the head mantissa by.
     44  1.1  joerg 			 */
     45  1.1  joerg 			tailMantissa >>= (unbiasedHeadExponent - (biasedTailExponent - (1023 - 11)));
     46  1.1  joerg 
     47  1.1  joerg 			result += tailMantissa;
     48  1.1  joerg 		}
     49  1.1  joerg 
     50  1.1  joerg 		result >>= (63 - unbiasedHeadExponent);
     51  1.1  joerg 		return result;
     52  1.1  joerg 	}
     53  1.1  joerg 
     54  1.1  joerg 	/* Edge cases are handled here, with saturation. */
     55  1.1  joerg 	if (1.0 > x.s.hi)
     56  1.1  joerg 		return UINT64_C(0);
     57  1.1  joerg 	else
     58  1.1  joerg 		return UINT64_MAX;
     59  1.1  joerg }
     60