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