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