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