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