xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/ppc/floatunditf.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 // long double __floatunditf(unsigned long long x);
6*0b57cec5SDimitry Andric // This file implements the PowerPC unsigned long long -> long double conversion
7*0b57cec5SDimitry Andric 
8*0b57cec5SDimitry Andric #include "DD.h"
9*0b57cec5SDimitry Andric 
__floatunditf(uint64_t a)10*0b57cec5SDimitry Andric long double __floatunditf(uint64_t a) {
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric   // Begins with an exact copy of the code from __floatundidf
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric   static const double twop52 = 0x1.0p52;
15*0b57cec5SDimitry Andric   static const double twop84 = 0x1.0p84;
16*0b57cec5SDimitry Andric   static const double twop84_plus_twop52 = 0x1.00000001p84;
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric   doublebits high = {.d = twop84};
19*0b57cec5SDimitry Andric   doublebits low = {.d = twop52};
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric   high.x |= a >> 32;                         // 0x1.0p84 + high 32 bits of a
22*0b57cec5SDimitry Andric   low.x |= a & UINT64_C(0x00000000ffffffff); // 0x1.0p52 + low 32 bits of a
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric   const double high_addend = high.d - twop84_plus_twop52;
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric   // At this point, we have two double precision numbers
27*0b57cec5SDimitry Andric   // high_addend and low.d, and we wish to return their sum
28*0b57cec5SDimitry Andric   // as a canonicalized long double:
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric   // This implementation sets the inexact flag spuriously.
31*0b57cec5SDimitry Andric   // This could be avoided, but at some substantial cost.
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric   DD result;
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric   result.s.hi = high_addend + low.d;
36*0b57cec5SDimitry Andric   result.s.lo = (high_addend - result.s.hi) + low.d;
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric   return result.ld;
39*0b57cec5SDimitry Andric }
40