xref: /openbsd-src/gnu/llvm/compiler-rt/lib/builtins/ppc/floatditf.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 // long double __floatditf(long long x);
6*3cab2bb3Spatrick // This file implements the PowerPC long long -> long double conversion
7*3cab2bb3Spatrick 
8*3cab2bb3Spatrick #include "DD.h"
9*3cab2bb3Spatrick 
__floatditf(int64_t a)10*3cab2bb3Spatrick long double __floatditf(int64_t a) {
11*3cab2bb3Spatrick 
12*3cab2bb3Spatrick   static const double twop32 = 0x1.0p32;
13*3cab2bb3Spatrick   static const double twop52 = 0x1.0p52;
14*3cab2bb3Spatrick 
15*3cab2bb3Spatrick   doublebits low = {.d = twop52};
16*3cab2bb3Spatrick   low.x |= a & UINT64_C(0x00000000ffffffff); // 0x1.0p52 + low 32 bits of a.
17*3cab2bb3Spatrick 
18*3cab2bb3Spatrick   const double high_addend = (double)((int32_t)(a >> 32)) * twop32 - twop52;
19*3cab2bb3Spatrick 
20*3cab2bb3Spatrick   // At this point, we have two double precision numbers
21*3cab2bb3Spatrick   // high_addend and low.d, and we wish to return their sum
22*3cab2bb3Spatrick   // as a canonicalized long double:
23*3cab2bb3Spatrick 
24*3cab2bb3Spatrick   // This implementation sets the inexact flag spuriously.
25*3cab2bb3Spatrick   // This could be avoided, but at some substantial cost.
26*3cab2bb3Spatrick 
27*3cab2bb3Spatrick   DD result;
28*3cab2bb3Spatrick 
29*3cab2bb3Spatrick   result.s.hi = high_addend + low.d;
30*3cab2bb3Spatrick   result.s.lo = (high_addend - result.s.hi) + low.d;
31*3cab2bb3Spatrick 
32*3cab2bb3Spatrick   return result.ld;
33*3cab2bb3Spatrick }
34