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