Home | History | Annotate | Line # | Download | only in quad
floatunditf_ieee754.c revision 1.2
      1  1.2  matt /*	$NetBSD: floatunditf_ieee754.c,v 1.2 2011/07/09 03:16:40 matt Exp $	*/
      2  1.1  matt 
      3  1.1  matt /*-
      4  1.1  matt  * Copyright (c) 1992, 1993
      5  1.1  matt  *	The Regents of the University of California.  All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This software was developed by the Computer Systems Engineering group
      8  1.1  matt  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1  matt  * contributed to Berkeley.
     10  1.1  matt  *
     11  1.1  matt  * Redistribution and use in source and binary forms, with or without
     12  1.1  matt  * modification, are permitted provided that the following conditions
     13  1.1  matt  * are met:
     14  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     15  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     16  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  matt  *    documentation and/or other materials provided with the distribution.
     19  1.1  matt  * 3. Neither the name of the University nor the names of its contributors
     20  1.1  matt  *    may be used to endorse or promote products derived from this software
     21  1.1  matt  *    without specific prior written permission.
     22  1.1  matt  *
     23  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  matt  * SUCH DAMAGE.
     34  1.1  matt  */
     35  1.1  matt 
     36  1.1  matt #include <sys/cdefs.h>
     37  1.1  matt #if defined(LIBC_SCCS) && !defined(lint)
     38  1.1  matt #if 0
     39  1.1  matt static char sccsid[] = "@(#)floatunsdidf.c	8.1 (Berkeley) 6/4/93";
     40  1.1  matt #else
     41  1.2  matt __RCSID("$NetBSD: floatunditf_ieee754.c,v 1.2 2011/07/09 03:16:40 matt Exp $");
     42  1.1  matt #endif
     43  1.1  matt #endif /* LIBC_SCCS and not lint */
     44  1.1  matt 
     45  1.1  matt #include "quad.h"
     46  1.1  matt #ifdef __vax__
     47  1.1  matt #error vax does not support a distinct long double
     48  1.1  matt #endif
     49  1.1  matt #include <machine/ieee.h>
     50  1.1  matt 
     51  1.1  matt /*
     52  1.1  matt  * Convert (unsigned) quad to long double.
     53  1.1  matt  * This is exactly like floatdidf.c except that negatives never occur.
     54  1.1  matt  */
     55  1.1  matt long double
     56  1.1  matt __floatunditf(u_quad_t x)
     57  1.1  matt {
     58  1.1  matt #if 0
     59  1.1  matt 	long double ld;
     60  1.1  matt 	union uu u;
     61  1.1  matt 
     62  1.1  matt 	u.uq = x;
     63  1.1  matt 	ld = (long double)u.ul[H] * (((int)1 << (INT_BITS - 2)) * 4.0);
     64  1.1  matt 	ld += u.ul[L];
     65  1.1  matt 	return (ld);
     66  1.1  matt #else
     67  1.1  matt 	union ieee_ext_u extu;
     68  1.2  matt 	quad_t tmp = x;		/* must be signed */
     69  1.2  matt 	size_t width = 64;
     70  1.2  matt 	size_t bit = 0;
     71  1.2  matt 	quad_t mask = ~(quad_t)0;
     72  1.1  matt 
     73  1.1  matt 	if (x == 0)
     74  1.1  matt 		return 0.0L;
     75  1.1  matt 	if (x == 1)
     76  1.1  matt 		return 1.0L;
     77  1.1  matt 
     78  1.1  matt 	while (mask != 0 && (tmp >= 0)) {
     79  1.1  matt 		width >>= 1;
     80  1.1  matt 		mask <<= width;
     81  1.1  matt 		if ((tmp & mask) == 0) {
     82  1.1  matt 			tmp <<= width;
     83  1.1  matt 			bit += width;
     84  1.1  matt 		}
     85  1.1  matt 	}
     86  1.1  matt 
     87  1.1  matt 	x <<= (bit + 1);
     88  1.1  matt 	extu.extu_sign = 0;
     89  1.1  matt 	extu.extu_exp = EXT_EXP_BIAS + (64 - (bit + 1));
     90  1.1  matt 	extu.extu_frach = x >> (64 - EXT_FRACHBITS);
     91  1.1  matt 	x <<= EXT_FRACHBITS;
     92  1.1  matt #ifdef EXT_FRACHMBITS
     93  1.1  matt 	extu.extu_frachm = x >> (64 - EXT_FRACHMBITS);
     94  1.1  matt 	x <<= EXT_FRACHMBITS;
     95  1.1  matt #endif
     96  1.1  matt #ifdef EXT_FRACLMBITS
     97  1.1  matt 	extu.extu_fraclm = x >> (64 - EXT_FRACLMBITS);
     98  1.1  matt 	x <<= EXT_FRACLMBITS;
     99  1.1  matt #endif
    100  1.1  matt 	extu.extu_fracl = x >> (64 - EXT_FRACLBITS);
    101  1.1  matt 
    102  1.1  matt 	return extu.extu_ld;
    103  1.1  matt #endif
    104  1.1  matt }
    105