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