fixtfdi.c revision 256281
1151497Sru/* This file is distributed under the University of Illinois Open Source 2151497Sru * License. See LICENSE.TXT for details. 3151497Sru */ 4151497Sru 5151497Sru/* int64_t __fixunstfdi(long double x); 6151497Sru * This file implements the PowerPC 128-bit double-double -> int64_t conversion 7151497Sru */ 8151497Sru 9151497Sru#include "DD.h" 10151497Sru#include "../int_math.h" 11151497Sru 12151497Sruuint64_t __fixtfdi(long double input) 13151497Sru{ 14151497Sru const DD x = { .ld = input }; 15151497Sru const doublebits hibits = { .d = x.s.hi }; 16151497Sru 17151497Sru const uint32_t absHighWord = (uint32_t)(hibits.x >> 32) & UINT32_C(0x7fffffff); 18151497Sru const uint32_t absHighWordMinusOne = absHighWord - UINT32_C(0x3ff00000); 19151497Sru 20151497Sru /* If (1.0 - tiny) <= input < 0x1.0p63: */ 21151497Sru if (UINT32_C(0x03f00000) > absHighWordMinusOne) 22151497Sru { 23151497Sru /* Do an unsigned conversion of the absolute value, then restore the sign. */ 24151497Sru const int unbiasedHeadExponent = absHighWordMinusOne >> 20; 25151497Sru 26151497Sru int64_t result = hibits.x & INT64_C(0x000fffffffffffff); /* mantissa(hi) */ 27151497Sru result |= INT64_C(0x0010000000000000); /* matissa(hi) with implicit bit */ 28151497Sru result <<= 10; /* mantissa(hi) with one zero preceeding bit. */ 29151497Sru 30151497Sru const int64_t hiNegationMask = ((int64_t)(hibits.x)) >> 63; 31151497Sru 32151497Sru /* If the tail is non-zero, we need to patch in the tail bits. */ 33151497Sru if (0.0 != x.s.lo) 34151497Sru { 35151497Sru const doublebits lobits = { .d = x.s.lo }; 36151497Sru int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff); 37151497Sru tailMantissa |= INT64_C(0x0010000000000000); 38151497Sru 39151497Sru /* At this point we have the mantissa of |tail| */ 40151497Sru /* We need to negate it if head and tail have different signs. */ 41151497Sru const int64_t loNegationMask = ((int64_t)(lobits.x)) >> 63; 42151497Sru const int64_t negationMask = loNegationMask ^ hiNegationMask; 43151497Sru tailMantissa = (tailMantissa ^ negationMask) - negationMask; 44151497Sru 45151497Sru /* Now we have the mantissa of tail as a signed 2s-complement integer */ 46151497Sru 47151497Sru const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff; 48151497Sru 49151497Sru /* Shift the tail mantissa into the right position, accounting for the 50151497Sru * bias of 10 that we shifted the head mantissa by. 51151497Sru */ 52151497Sru tailMantissa >>= (unbiasedHeadExponent - (biasedTailExponent - (1023 - 10))); 53151497Sru 54151497Sru result += tailMantissa; 55151497Sru } 56151497Sru 57151497Sru result >>= (62 - unbiasedHeadExponent); 58151497Sru 59151497Sru /* Restore the sign of the result and return */ 60151497Sru result = (result ^ hiNegationMask) - hiNegationMask; 61151497Sru return result; 62151497Sru 63151497Sru } 64151497Sru 65151497Sru /* Edge cases handled here: */ 66151497Sru 67151497Sru /* |x| < 1, result is zero. */ 68151497Sru if (1.0 > crt_fabs(x.s.hi)) 69151497Sru return INT64_C(0); 70151497Sru 71151497Sru /* x very close to INT64_MIN, care must be taken to see which side we are on. */ 72151497Sru if (x.s.hi == -0x1.0p63) { 73151497Sru 74151497Sru int64_t result = INT64_MIN; 75151497Sru 76151497Sru if (0.0 < x.s.lo) 77151497Sru { 78151497Sru /* If the tail is positive, the correct result is something other than INT64_MIN. 79151497Sru * we'll need to figure out what it is. 80151497Sru */ 81151497Sru 82151497Sru const doublebits lobits = { .d = x.s.lo }; 83151497Sru int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff); 84151497Sru tailMantissa |= INT64_C(0x0010000000000000); 85151497Sru 86151497Sru /* Now we negate the tailMantissa */ 87151497Sru tailMantissa = (tailMantissa ^ INT64_C(-1)) + INT64_C(1); 88151497Sru 89151497Sru /* And shift it by the appropriate amount */ 90151497Sru const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff; 91151497Sru tailMantissa >>= 1075 - biasedTailExponent; 92151497Sru 93151497Sru result -= tailMantissa; 94151497Sru } 95151497Sru 96151497Sru return result; 97151497Sru } 98151497Sru 99151497Sru /* Signed overflows, infinities, and NaNs */ 100151497Sru if (x.s.hi > 0.0) 101151497Sru return INT64_MAX; 102151497Sru else 103151497Sru return INT64_MIN; 104151497Sru} 105151497Sru