xref: /netbsd-src/lib/libc/gen/floatunditf_ieee754.c (revision 074e94778cee9df44e26f51689a4b1312bc93604)
1*074e9477Sjoerg /*	$NetBSD: floatunditf_ieee754.c,v 1.1 2014/01/30 15:06:18 joerg Exp $	*/
2*074e9477Sjoerg 
3*074e9477Sjoerg /*-
4*074e9477Sjoerg  * Copyright (c) 2014 Joerg Sonnenberger <joerg@NetBSD.org>.
5*074e9477Sjoerg  * All rights reserved.
6*074e9477Sjoerg  *
7*074e9477Sjoerg  * Redistribution and use in source and binary forms, with or without
8*074e9477Sjoerg  * modification, are permitted provided that the following conditions
9*074e9477Sjoerg  * are met:
10*074e9477Sjoerg  *
11*074e9477Sjoerg  * 1. Redistributions of source code must retain the above copyright
12*074e9477Sjoerg  *    notice, this list of conditions and the following disclaimer.
13*074e9477Sjoerg  * 2. Redistributions in binary form must reproduce the above copyright
14*074e9477Sjoerg  *    notice, this list of conditions and the following disclaimer in
15*074e9477Sjoerg  *    the documentation and/or other materials provided with the
16*074e9477Sjoerg  *    distribution.
17*074e9477Sjoerg  *
18*074e9477Sjoerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*074e9477Sjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*074e9477Sjoerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*074e9477Sjoerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22*074e9477Sjoerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*074e9477Sjoerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*074e9477Sjoerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*074e9477Sjoerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*074e9477Sjoerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27*074e9477Sjoerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28*074e9477Sjoerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*074e9477Sjoerg  * SUCH DAMAGE.
30*074e9477Sjoerg  */
31*074e9477Sjoerg 
32*074e9477Sjoerg #include <sys/cdefs.h>
33*074e9477Sjoerg __RCSID("$NetBSD: floatunditf_ieee754.c,v 1.1 2014/01/30 15:06:18 joerg Exp $");
34*074e9477Sjoerg 
35*074e9477Sjoerg #include <math.h>
36*074e9477Sjoerg #include <machine/ieee.h>
37*074e9477Sjoerg #include <limits.h>
38*074e9477Sjoerg 
39*074e9477Sjoerg #ifdef __HAVE_LONG_DOUBLE
40*074e9477Sjoerg 
41*074e9477Sjoerg long double __floatunditf(uint64_t x);
42*074e9477Sjoerg 
43*074e9477Sjoerg /*
44*074e9477Sjoerg  * Convert uint64_t to long double.
45*074e9477Sjoerg  */
46*074e9477Sjoerg long double
__floatunditf(uint64_t x)47*074e9477Sjoerg __floatunditf(uint64_t x)
48*074e9477Sjoerg {
49*074e9477Sjoerg 	int exponent, zeros;
50*074e9477Sjoerg 	union ieee_ext_u ux;
51*074e9477Sjoerg 
52*074e9477Sjoerg 	/* Use the easier conversion routine for uint32_t if possible. */
53*074e9477Sjoerg 	if (x <= UINT32_MAX)
54*074e9477Sjoerg 		return (long double)(uint32_t)x;
55*074e9477Sjoerg 
56*074e9477Sjoerg 	zeros = __builtin_clzll(x);
57*074e9477Sjoerg #ifdef LDBL_IMPLICIT_NBIT
58*074e9477Sjoerg 	exponent = 64 - zeros;
59*074e9477Sjoerg 	x <<= zeros + 1;
60*074e9477Sjoerg #else
61*074e9477Sjoerg 	exponent = 63 - zeros;
62*074e9477Sjoerg 	x <<= zeros;
63*074e9477Sjoerg #endif
64*074e9477Sjoerg 
65*074e9477Sjoerg 	ux.extu_exp = EXT_EXP_BIAS + exponent;
66*074e9477Sjoerg 	ux.extu_frach = (x >> (64 - EXT_FRACHBITS));
67*074e9477Sjoerg 	x <<= EXT_FRACHBITS;
68*074e9477Sjoerg #ifdef EXT_FRACHMBITS
69*074e9477Sjoerg 	ux.extu_frachm = (x >> (64 - EXT_FRACHMBITS));
70*074e9477Sjoerg 	x <<= EXT_FRACHMBITS;
71*074e9477Sjoerg #endif
72*074e9477Sjoerg #ifdef EXT_FRACLMBITS
73*074e9477Sjoerg 	ux.extu_fraclm = (x >> (64 - EXT_FRACLMBITS));
74*074e9477Sjoerg 	x <<= EXT_FRACLMBITS;
75*074e9477Sjoerg #endif
76*074e9477Sjoerg 	ux.extu_fracl = (x >> (64 - EXT_FRACLBITS));
77*074e9477Sjoerg 	ux.extu_sign = 0;
78*074e9477Sjoerg 
79*074e9477Sjoerg 	return ux.extu_ld;
80*074e9477Sjoerg }
81*074e9477Sjoerg #endif /* __HAVE_LONG_DOUBLE */
82