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