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