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