xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/ppc/fixunstfdi.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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 // uint64_t __fixunstfdi(long double x);
6*0b57cec5SDimitry Andric // This file implements the PowerPC 128-bit double-double -> uint64_t conversion
7*0b57cec5SDimitry Andric 
8*0b57cec5SDimitry Andric #include "DD.h"
9*0b57cec5SDimitry Andric 
__fixunstfdi(long double input)10*0b57cec5SDimitry Andric uint64_t __fixunstfdi(long double input) {
11*0b57cec5SDimitry Andric   const DD x = {.ld = input};
12*0b57cec5SDimitry Andric   const doublebits hibits = {.d = x.s.hi};
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric   const uint32_t highWordMinusOne =
15*0b57cec5SDimitry Andric       (uint32_t)(hibits.x >> 32) - UINT32_C(0x3ff00000);
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric   // If (1.0 - tiny) <= input < 0x1.0p64:
18*0b57cec5SDimitry Andric   if (UINT32_C(0x04000000) > highWordMinusOne) {
19*0b57cec5SDimitry Andric     const int unbiasedHeadExponent = highWordMinusOne >> 20;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric     uint64_t result = hibits.x & UINT64_C(0x000fffffffffffff); // mantissa(hi)
22*0b57cec5SDimitry Andric     result |= UINT64_C(0x0010000000000000); // matissa(hi) with implicit bit
23*0b57cec5SDimitry Andric     result <<= 11; // mantissa(hi) left aligned in the int64 field.
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric     // If the tail is non-zero, we need to patch in the tail bits.
26*0b57cec5SDimitry Andric     if (0.0 != x.s.lo) {
27*0b57cec5SDimitry Andric       const doublebits lobits = {.d = x.s.lo};
28*0b57cec5SDimitry Andric       int64_t tailMantissa = lobits.x & INT64_C(0x000fffffffffffff);
29*0b57cec5SDimitry Andric       tailMantissa |= INT64_C(0x0010000000000000);
30*0b57cec5SDimitry Andric 
31*0b57cec5SDimitry Andric       // At this point we have the mantissa of |tail|
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric       const int64_t negationMask = ((int64_t)(lobits.x)) >> 63;
34*0b57cec5SDimitry Andric       tailMantissa = (tailMantissa ^ negationMask) - negationMask;
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric       // Now we have the mantissa of tail as a signed 2s-complement integer
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric       const int biasedTailExponent = (int)(lobits.x >> 52) & 0x7ff;
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric       // Shift the tail mantissa into the right position, accounting for the
41*0b57cec5SDimitry Andric       // bias of 11 that we shifted the head mantissa by.
42*0b57cec5SDimitry Andric       tailMantissa >>=
43*0b57cec5SDimitry Andric           (unbiasedHeadExponent - (biasedTailExponent - (1023 - 11)));
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric       result += tailMantissa;
46*0b57cec5SDimitry Andric     }
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric     result >>= (63 - unbiasedHeadExponent);
49*0b57cec5SDimitry Andric     return result;
50*0b57cec5SDimitry Andric   }
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   // Edge cases are handled here, with saturation.
53*0b57cec5SDimitry Andric   if (1.0 > x.s.hi)
54*0b57cec5SDimitry Andric     return UINT64_C(0);
55*0b57cec5SDimitry Andric   else
56*0b57cec5SDimitry Andric     return UINT64_MAX;
57*0b57cec5SDimitry Andric }
58