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