xref: /aosp_15_r20/external/compiler-rt/lib/builtins/ppc/floatditf.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* This file is distributed under the University of Illinois Open Source
2*7c3d14c8STreehugger Robot  * License. See LICENSE.TXT for details.
3*7c3d14c8STreehugger Robot  */
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot /* long double __floatditf(long long x); */
6*7c3d14c8STreehugger Robot /* This file implements the PowerPC long long -> long double conversion */
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot #include "DD.h"
9*7c3d14c8STreehugger Robot 
__floatditf(int64_t a)10*7c3d14c8STreehugger Robot long double __floatditf(int64_t a) {
11*7c3d14c8STreehugger Robot 
12*7c3d14c8STreehugger Robot 	static const double twop32 = 0x1.0p32;
13*7c3d14c8STreehugger Robot 	static const double twop52 = 0x1.0p52;
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot 	doublebits low  = { .d = twop52 };
16*7c3d14c8STreehugger Robot 	low.x |= a & UINT64_C(0x00000000ffffffff);	/* 0x1.0p52 + low 32 bits of a. */
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot 	const double high_addend = (double)((int32_t)(a >> 32))*twop32 - twop52;
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot 	/* At this point, we have two double precision numbers
21*7c3d14c8STreehugger Robot 	 * high_addend and low.d, and we wish to return their sum
22*7c3d14c8STreehugger Robot 	 * as a canonicalized long double:
23*7c3d14c8STreehugger Robot 	 */
24*7c3d14c8STreehugger Robot 
25*7c3d14c8STreehugger Robot 	/* This implementation sets the inexact flag spuriously.
26*7c3d14c8STreehugger Robot 	 * This could be avoided, but at some substantial cost.
27*7c3d14c8STreehugger Robot 	*/
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot 	DD result;
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot 	result.s.hi = high_addend + low.d;
32*7c3d14c8STreehugger Robot 	result.s.lo = (high_addend - result.s.hi) + low.d;
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot 	return result.ld;
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot }
37