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