10b57cec5SDimitry Andric //===-- lib/builtins/ppc/fixunstfti.c - Convert long double->int128 *-C -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements converting the 128bit IBM/PowerPC long double (double-
100b57cec5SDimitry Andric // double) data type to an unsigned 128 bit integer.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "../int_math.h"
150b57cec5SDimitry Andric #define BIAS 1023
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric // Convert long double into an unsigned 128-bit integer.
__fixunstfti(long double input)180b57cec5SDimitry Andric __uint128_t __fixunstfti(long double input) {
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric // If we are trying to convert a NaN, return the NaN bit pattern.
210b57cec5SDimitry Andric if (crt_isnan(input)) {
220b57cec5SDimitry Andric return ((__uint128_t)0x7FF8000000000000ll) << 64 |
230b57cec5SDimitry Andric (__uint128_t)0x0000000000000000ll;
240b57cec5SDimitry Andric }
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric __uint128_t result, hiResult, loResult;
270b57cec5SDimitry Andric int hiExponent, loExponent, shift;
280b57cec5SDimitry Andric // The long double representation, with the high and low portions of
290b57cec5SDimitry Andric // the long double, and the corresponding bit patterns of each double.
300b57cec5SDimitry Andric union {
310b57cec5SDimitry Andric long double ld;
320b57cec5SDimitry Andric double d[2]; // [0] is the high double, [1] is the low double.
330b57cec5SDimitry Andric unsigned long long ull[2]; // High and low doubles as 64-bit integers.
340b57cec5SDimitry Andric } ldUnion;
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric // If the long double is less than 1.0 or negative,
37*480093f4SDimitry Andric // return 0.
380b57cec5SDimitry Andric if (input < 1.0)
39*480093f4SDimitry Andric return 0;
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric // Retrieve the 64-bit patterns of high and low doubles.
420b57cec5SDimitry Andric // Compute the unbiased exponent of both high and low doubles by
430b57cec5SDimitry Andric // removing the signs, isolating the exponent, and subtracting
440b57cec5SDimitry Andric // the bias from it.
450b57cec5SDimitry Andric ldUnion.ld = input;
460b57cec5SDimitry Andric hiExponent = ((ldUnion.ull[0] & 0x7FFFFFFFFFFFFFFFll) >> 52) - BIAS;
470b57cec5SDimitry Andric loExponent = ((ldUnion.ull[1] & 0x7FFFFFFFFFFFFFFFll) >> 52) - BIAS;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric // Convert each double into int64; they will be added to the int128 result.
500b57cec5SDimitry Andric // CASE 1: High or low double fits in int64
510b57cec5SDimitry Andric // - Convert the each double normally into int64.
520b57cec5SDimitry Andric //
530b57cec5SDimitry Andric // CASE 2: High or low double does not fit in int64
540b57cec5SDimitry Andric // - Scale the double to fit within a 64-bit integer
550b57cec5SDimitry Andric // - Calculate the shift (amount to scale the double by in the int128)
560b57cec5SDimitry Andric // - Clear all the bits of the exponent (with 0x800FFFFFFFFFFFFF)
570b57cec5SDimitry Andric // - Add BIAS+53 (0x4350000000000000) to exponent to correct the value
580b57cec5SDimitry Andric // - Scale (move) the double to the correct place in the int128
590b57cec5SDimitry Andric // (Move it by 2^53 places)
600b57cec5SDimitry Andric //
610b57cec5SDimitry Andric // Note: If the high double is assumed to be positive, an unsigned conversion
620b57cec5SDimitry Andric // from long double to 64-bit integer is needed. The low double can be either
630b57cec5SDimitry Andric // positive or negative, so a signed conversion is needed to retain the result
640b57cec5SDimitry Andric // of the low double and to ensure it does not simply get converted to 0.
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric // CASE 1 - High double fits in int64.
670b57cec5SDimitry Andric if (hiExponent < 63) {
680b57cec5SDimitry Andric hiResult = (unsigned long long)ldUnion.d[0];
690b57cec5SDimitry Andric } else if (hiExponent < 128) {
700b57cec5SDimitry Andric // CASE 2 - High double does not fit in int64, scale and convert it.
710b57cec5SDimitry Andric shift = hiExponent - 54;
720b57cec5SDimitry Andric ldUnion.ull[0] &= 0x800FFFFFFFFFFFFFll;
730b57cec5SDimitry Andric ldUnion.ull[0] |= 0x4350000000000000ll;
740b57cec5SDimitry Andric hiResult = (unsigned long long)ldUnion.d[0];
750b57cec5SDimitry Andric hiResult <<= shift;
760b57cec5SDimitry Andric } else {
770b57cec5SDimitry Andric // Detect cases for overflow. When the exponent of the high
780b57cec5SDimitry Andric // double is greater than 128 bits and when the long double
790b57cec5SDimitry Andric // input is positive, return the max 128-bit integer.
800b57cec5SDimitry Andric // For negative inputs with exponents > 128, return 1, like gcc.
810b57cec5SDimitry Andric if (ldUnion.d[0] > 0) {
820b57cec5SDimitry Andric return ((__uint128_t)0xFFFFFFFFFFFFFFFFll) << 64 |
830b57cec5SDimitry Andric (__uint128_t)0xFFFFFFFFFFFFFFFFll;
840b57cec5SDimitry Andric } else {
850b57cec5SDimitry Andric return ((__uint128_t)0x0000000000000000ll) << 64 |
860b57cec5SDimitry Andric (__uint128_t)0x0000000000000001ll;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // CASE 1 - Low double fits in int64.
910b57cec5SDimitry Andric if (loExponent < 63) {
920b57cec5SDimitry Andric loResult = (long long)ldUnion.d[1];
930b57cec5SDimitry Andric } else {
940b57cec5SDimitry Andric // CASE 2 - Low double does not fit in int64, scale and convert it.
950b57cec5SDimitry Andric shift = loExponent - 54;
960b57cec5SDimitry Andric ldUnion.ull[1] &= 0x800FFFFFFFFFFFFFll;
970b57cec5SDimitry Andric ldUnion.ull[1] |= 0x4350000000000000ll;
980b57cec5SDimitry Andric loResult = (long long)ldUnion.d[1];
990b57cec5SDimitry Andric loResult <<= shift;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric
102*480093f4SDimitry Andric // If the low double is negative, it may change the integer value of the
103*480093f4SDimitry Andric // whole number if the absolute value of its fractional part is bigger than
104*480093f4SDimitry Andric // the fractional part of the high double. Because both doubles cannot
105*480093f4SDimitry Andric // overlap, this situation only occurs when the high double has no
106*480093f4SDimitry Andric // fractional part.
107*480093f4SDimitry Andric ldUnion.ld = input;
108*480093f4SDimitry Andric if ((ldUnion.d[0] == (double)hiResult) &&
109*480093f4SDimitry Andric (ldUnion.d[1] < (double)((__int128_t)loResult)))
110*480093f4SDimitry Andric loResult--;
111*480093f4SDimitry Andric
1120b57cec5SDimitry Andric // Add the high and low doublewords together to form a 128 bit integer.
1130b57cec5SDimitry Andric result = loResult + hiResult;
1140b57cec5SDimitry Andric return result;
1150b57cec5SDimitry Andric }
116