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