xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/ppc/fixunstfti.c (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1*3cab2bb3Spatrick //===-- lib/builtins/ppc/fixunstfti.c - Convert long double->int128 *-C -*-===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick //
9*3cab2bb3Spatrick // This file implements converting the 128bit IBM/PowerPC long double (double-
10*3cab2bb3Spatrick // double) data type to an unsigned 128 bit integer.
11*3cab2bb3Spatrick //
12*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
13*3cab2bb3Spatrick 
14*3cab2bb3Spatrick #include "../int_math.h"
15*3cab2bb3Spatrick #define BIAS 1023
16*3cab2bb3Spatrick 
17*3cab2bb3Spatrick // Convert long double into an unsigned 128-bit integer.
__fixunstfti(long double input)18*3cab2bb3Spatrick __uint128_t __fixunstfti(long double input) {
19*3cab2bb3Spatrick 
20*3cab2bb3Spatrick   // If we are trying to convert a NaN, return the NaN bit pattern.
21*3cab2bb3Spatrick   if (crt_isnan(input)) {
22*3cab2bb3Spatrick     return ((__uint128_t)0x7FF8000000000000ll) << 64 |
23*3cab2bb3Spatrick            (__uint128_t)0x0000000000000000ll;
24*3cab2bb3Spatrick   }
25*3cab2bb3Spatrick 
26*3cab2bb3Spatrick   __uint128_t result, hiResult, loResult;
27*3cab2bb3Spatrick   int hiExponent, loExponent, shift;
28*3cab2bb3Spatrick   // The long double representation, with the high and low portions of
29*3cab2bb3Spatrick   // the long double, and the corresponding bit patterns of each double.
30*3cab2bb3Spatrick   union {
31*3cab2bb3Spatrick     long double ld;
32*3cab2bb3Spatrick     double d[2];               // [0] is the high double, [1] is the low double.
33*3cab2bb3Spatrick     unsigned long long ull[2]; // High and low doubles as 64-bit integers.
34*3cab2bb3Spatrick   } ldUnion;
35*3cab2bb3Spatrick 
36*3cab2bb3Spatrick   // If the long double is less than 1.0 or negative,
37*3cab2bb3Spatrick   // return 0.
38*3cab2bb3Spatrick   if (input < 1.0)
39*3cab2bb3Spatrick     return 0;
40*3cab2bb3Spatrick 
41*3cab2bb3Spatrick   // Retrieve the 64-bit patterns of high and low doubles.
42*3cab2bb3Spatrick   // Compute the unbiased exponent of both high and low doubles by
43*3cab2bb3Spatrick   // removing the signs, isolating the exponent, and subtracting
44*3cab2bb3Spatrick   // the bias from it.
45*3cab2bb3Spatrick   ldUnion.ld = input;
46*3cab2bb3Spatrick   hiExponent = ((ldUnion.ull[0] & 0x7FFFFFFFFFFFFFFFll) >> 52) - BIAS;
47*3cab2bb3Spatrick   loExponent = ((ldUnion.ull[1] & 0x7FFFFFFFFFFFFFFFll) >> 52) - BIAS;
48*3cab2bb3Spatrick 
49*3cab2bb3Spatrick   // Convert each double into int64; they will be added to the int128 result.
50*3cab2bb3Spatrick   // CASE 1: High or low double fits in int64
51*3cab2bb3Spatrick   //      - Convert the each double normally into int64.
52*3cab2bb3Spatrick   //
53*3cab2bb3Spatrick   // CASE 2: High or low double does not fit in int64
54*3cab2bb3Spatrick   //      - Scale the double to fit within a 64-bit integer
55*3cab2bb3Spatrick   //      - Calculate the shift (amount to scale the double by in the int128)
56*3cab2bb3Spatrick   //      - Clear all the bits of the exponent (with 0x800FFFFFFFFFFFFF)
57*3cab2bb3Spatrick   //      - Add BIAS+53 (0x4350000000000000) to exponent to correct the value
58*3cab2bb3Spatrick   //      - Scale (move) the double to the correct place in the int128
59*3cab2bb3Spatrick   //        (Move it by 2^53 places)
60*3cab2bb3Spatrick   //
61*3cab2bb3Spatrick   // Note: If the high double is assumed to be positive, an unsigned conversion
62*3cab2bb3Spatrick   // from long double to 64-bit integer is needed. The low double can be either
63*3cab2bb3Spatrick   // positive or negative, so a signed conversion is needed to retain the result
64*3cab2bb3Spatrick   // of the low double and to ensure it does not simply get converted to 0.
65*3cab2bb3Spatrick 
66*3cab2bb3Spatrick   // CASE 1 - High double fits in int64.
67*3cab2bb3Spatrick   if (hiExponent < 63) {
68*3cab2bb3Spatrick     hiResult = (unsigned long long)ldUnion.d[0];
69*3cab2bb3Spatrick   } else if (hiExponent < 128) {
70*3cab2bb3Spatrick     // CASE 2 - High double does not fit in int64, scale and convert it.
71*3cab2bb3Spatrick     shift = hiExponent - 54;
72*3cab2bb3Spatrick     ldUnion.ull[0] &= 0x800FFFFFFFFFFFFFll;
73*3cab2bb3Spatrick     ldUnion.ull[0] |= 0x4350000000000000ll;
74*3cab2bb3Spatrick     hiResult = (unsigned long long)ldUnion.d[0];
75*3cab2bb3Spatrick     hiResult <<= shift;
76*3cab2bb3Spatrick   } else {
77*3cab2bb3Spatrick     // Detect cases for overflow. When the exponent of the high
78*3cab2bb3Spatrick     // double is greater than 128 bits and when the long double
79*3cab2bb3Spatrick     // input is positive, return the max 128-bit integer.
80*3cab2bb3Spatrick     // For negative inputs with exponents > 128, return 1, like gcc.
81*3cab2bb3Spatrick     if (ldUnion.d[0] > 0) {
82*3cab2bb3Spatrick       return ((__uint128_t)0xFFFFFFFFFFFFFFFFll) << 64 |
83*3cab2bb3Spatrick              (__uint128_t)0xFFFFFFFFFFFFFFFFll;
84*3cab2bb3Spatrick     } else {
85*3cab2bb3Spatrick       return ((__uint128_t)0x0000000000000000ll) << 64 |
86*3cab2bb3Spatrick              (__uint128_t)0x0000000000000001ll;
87*3cab2bb3Spatrick     }
88*3cab2bb3Spatrick   }
89*3cab2bb3Spatrick 
90*3cab2bb3Spatrick   // CASE 1 - Low double fits in int64.
91*3cab2bb3Spatrick   if (loExponent < 63) {
92*3cab2bb3Spatrick     loResult = (long long)ldUnion.d[1];
93*3cab2bb3Spatrick   } else {
94*3cab2bb3Spatrick     // CASE 2 - Low double does not fit in int64, scale and convert it.
95*3cab2bb3Spatrick     shift = loExponent - 54;
96*3cab2bb3Spatrick     ldUnion.ull[1] &= 0x800FFFFFFFFFFFFFll;
97*3cab2bb3Spatrick     ldUnion.ull[1] |= 0x4350000000000000ll;
98*3cab2bb3Spatrick     loResult = (long long)ldUnion.d[1];
99*3cab2bb3Spatrick     loResult <<= shift;
100*3cab2bb3Spatrick   }
101*3cab2bb3Spatrick 
102*3cab2bb3Spatrick   // If the low double is negative, it may change the integer value of the
103*3cab2bb3Spatrick   // whole number if the absolute value of its fractional part is bigger than
104*3cab2bb3Spatrick   // the fractional part of the high double. Because both doubles cannot
105*3cab2bb3Spatrick   // overlap, this situation only occurs when the high double has no
106*3cab2bb3Spatrick   // fractional part.
107*3cab2bb3Spatrick   ldUnion.ld = input;
108*3cab2bb3Spatrick   if ((ldUnion.d[0] == (double)hiResult) &&
109*3cab2bb3Spatrick       (ldUnion.d[1] < (double)((__int128_t)loResult)))
110*3cab2bb3Spatrick     loResult--;
111*3cab2bb3Spatrick 
112*3cab2bb3Spatrick   // Add the high and low doublewords together to form a 128 bit integer.
113*3cab2bb3Spatrick   result = loResult + hiResult;
114*3cab2bb3Spatrick   return result;
115*3cab2bb3Spatrick }
116