Home | History | Annotate | Line # | Download | only in gen
fixunsgen_ieee754.c revision 1.2.2.1
      1  1.2.2.1  yamt /*	$NetBSD: fixunsgen_ieee754.c,v 1.2.2.1 2012/04/17 00:05:18 yamt 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 
     38      1.1  matt #if !defined(FIXUNSNAME) && defined(LIBC_SCCS) && !defined(lint)
     39  1.2.2.1  yamt __RCSID("$NetBSD: fixunsgen_ieee754.c,v 1.2.2.1 2012/04/17 00:05:18 yamt Exp $");
     40      1.1  matt #endif /* LIBC_SCCS and not lint */
     41      1.1  matt 
     42      1.1  matt #include <stddef.h>
     43      1.1  matt #include <stdint.h>
     44      1.1  matt #include <stdbool.h>
     45      1.1  matt #include <float.h>
     46      1.1  matt 
     47      1.1  matt #ifndef FIXUNSNAME
     48      1.1  matt #define	FIXUNSNAME(n)	n##32
     49      1.1  matt #define	UINTXX_T	uint32_t
     50      1.1  matt #endif
     51      1.1  matt 
     52      1.1  matt __dso_hidden UINTXX_T
     53      1.1  matt 	FIXUNSNAME(__fixunsgen)(int, bool, size_t, size_t, const uint32_t *);
     54      1.1  matt 
     55      1.1  matt /*
     56      1.1  matt  * Convert double to (unsigned) int.  All operations are done module 2^32.
     57      1.1  matt  */
     58      1.1  matt UINTXX_T
     59      1.1  matt FIXUNSNAME(__fixunsgen)(int exp, bool sign, size_t mant_dig, size_t fracbits,
     60      1.1  matt 	const uint32_t *frac)
     61      1.1  matt {
     62      1.1  matt 	UINTXX_T tmp;
     63      1.1  matt 
     64      1.1  matt 	/*
     65      1.1  matt 	 * If it's less than 1 (negative exponent), it's going to round
     66      1.1  matt 	 * to zero.  If the exponent is so large that it is a multiple of
     67      1.2  matt 	 * 2^N, then x module 2^N will be 0.  (we use the fact treating a
     68      1.2  matt 	 * negative value as unsigned will be greater than nonnegative value)
     69      1.1  matt 	 */
     70      1.2  matt 	if (__predict_false((size_t)exp >= mant_dig + sizeof(UINTXX_T)*8))
     71      1.1  matt 		return 0;
     72      1.1  matt 
     73      1.1  matt 	/*
     74      1.1  matt 	 * This is simplier than it seems.  Basically we are constructing
     75      1.1  matt 	 * fixed binary representation of the floating point number tossing
     76      1.1  matt 	 * away bits that wont be in the modulis we return.
     77      1.1  matt 	 */
     78      1.1  matt 	tmp = 1;
     79      1.1  matt 	for (size_t ebits = exp;;) {
     80      1.1  matt 		if (ebits <= fracbits) {
     81      1.1  matt 			/*
     82      1.1  matt 			 * The current fraction has more bits than we need.
     83      1.1  matt 			 * Shift the current value over and insert the bits
     84      1.1  matt 			 * we want.  We're done.
     85      1.1  matt 			 */
     86  1.2.2.1  yamt 			tmp <<= (unsigned int)ebits;
     87      1.1  matt 			tmp |= *frac >> (fracbits - ebits);
     88      1.1  matt 			break;
     89      1.1  matt 		}
     90      1.1  matt 		if (fracbits == sizeof(tmp)*4) {
     91      1.1  matt 			/*
     92      1.1  matt 			 * Shifts must be < sizeof(type).  If it's going to be
     93      1.1  matt 			 * sizeof(type), just replace the value.
     94      1.1  matt 			 */
     95      1.1  matt 			tmp = *frac--;
     96      1.1  matt 		} else {
     97  1.2.2.1  yamt 			tmp <<= (unsigned int)fracbits;
     98      1.1  matt 			tmp |= *frac--;
     99      1.1  matt 		}
    100      1.1  matt 		ebits -= fracbits;
    101      1.1  matt 		fracbits = sizeof(frac[0]) * 4;
    102      1.1  matt 	}
    103      1.1  matt 
    104      1.1  matt 	/*
    105      1.1  matt 	 * If the input was negative, make tmp negative module 2^32.
    106      1.1  matt 	 */
    107      1.1  matt 	if (sign)
    108      1.1  matt 		tmp = -tmp;
    109      1.1  matt 
    110      1.1  matt 	return tmp;
    111      1.1  matt }
    112