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