Home | History | Annotate | Line # | Download | only in gen
compat_ldexp_ieee754.c revision 1.5
      1  1.5  drochner /* $NetBSD: compat_ldexp_ieee754.c,v 1.5 2010/04/23 19:04:54 drochner Exp $ */
      2  1.1  drochner 
      3  1.1  drochner /*-
      4  1.1  drochner  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  drochner  * All rights reserved.
      6  1.1  drochner  *
      7  1.1  drochner  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  drochner  * by Charles M. Hannum.
      9  1.1  drochner  *
     10  1.1  drochner  * Redistribution and use in source and binary forms, with or without
     11  1.1  drochner  * modification, are permitted provided that the following conditions
     12  1.1  drochner  * are met:
     13  1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     14  1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     15  1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  drochner  *    documentation and/or other materials provided with the distribution.
     18  1.1  drochner  *
     19  1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  drochner  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  drochner  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  drochner  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  drochner  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  drochner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  drochner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  drochner  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  drochner  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  drochner  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  drochner  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  drochner  */
     31  1.1  drochner 
     32  1.1  drochner #include <sys/cdefs.h>
     33  1.1  drochner #if defined(LIBC_SCCS) && !defined(lint)
     34  1.5  drochner __RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.5 2010/04/23 19:04:54 drochner Exp $");
     35  1.1  drochner #endif /* LIBC_SCCS and not lint */
     36  1.1  drochner 
     37  1.1  drochner #include <sys/types.h>
     38  1.1  drochner #include <machine/ieee.h>
     39  1.1  drochner #include <errno.h>
     40  1.5  drochner 
     41  1.5  drochner double ldexp(double, int);
     42  1.1  drochner 
     43  1.1  drochner /*
     44  1.1  drochner  * Multiply the given value by 2^expon.
     45  1.1  drochner  */
     46  1.1  drochner double
     47  1.1  drochner ldexp(double val, int expon)
     48  1.1  drochner {
     49  1.1  drochner 	int oldexp, newexp;
     50  1.1  drochner 	union ieee_double_u u, mul;
     51  1.1  drochner 
     52  1.1  drochner 	u.dblu_d = val;
     53  1.1  drochner 	oldexp = u.dblu_dbl.dbl_exp;
     54  1.1  drochner 
     55  1.1  drochner 	/*
     56  1.1  drochner 	 * If input is zero, Inf or NaN, just return it.
     57  1.1  drochner 	 */
     58  1.1  drochner 	if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN)
     59  1.1  drochner 		return (val);
     60  1.1  drochner 
     61  1.1  drochner 	if (oldexp == 0) {
     62  1.1  drochner 		/*
     63  1.1  drochner 		 * u.v is denormal.  We must adjust it so that the exponent
     64  1.1  drochner 		 * arithmetic below will work.
     65  1.1  drochner 		 */
     66  1.1  drochner 		if (expon <= DBL_EXP_BIAS) {
     67  1.1  drochner 			/*
     68  1.1  drochner 			 * Optimization: if the scaling can be done in a single
     69  1.1  drochner 			 * multiply, or underflows, just do it now.
     70  1.1  drochner 			 */
     71  1.1  drochner 			if (expon <= -DBL_FRACBITS) {
     72  1.1  drochner 				errno = ERANGE;
     73  1.1  drochner 				return (val < 0.0 ? -0.0 : 0.0);
     74  1.1  drochner 			}
     75  1.1  drochner 			mul.dblu_d = 0.0;
     76  1.1  drochner 			mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
     77  1.1  drochner 			u.dblu_d *= mul.dblu_d;
     78  1.1  drochner 			if (u.dblu_d == 0.0) {
     79  1.1  drochner 				errno = ERANGE;
     80  1.1  drochner 				return (val < 0.0 ? -0.0 : 0.0);
     81  1.1  drochner 			}
     82  1.1  drochner 			return (u.dblu_d);
     83  1.1  drochner 		} else {
     84  1.1  drochner 			/*
     85  1.1  drochner 			 * We know that expon is very large, and therefore the
     86  1.1  drochner 			 * result cannot be denormal (though it may be Inf).
     87  1.1  drochner 			 * Shift u.v by just enough to make it normal.
     88  1.1  drochner 			 */
     89  1.1  drochner 			mul.dblu_d = 0.0;
     90  1.1  drochner 			mul.dblu_dbl.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS;
     91  1.1  drochner 			u.dblu_d *= mul.dblu_d;
     92  1.1  drochner 			expon -= DBL_FRACBITS;
     93  1.1  drochner 			oldexp = u.dblu_dbl.dbl_exp;
     94  1.1  drochner 		}
     95  1.1  drochner 	}
     96  1.1  drochner 
     97  1.1  drochner 	/*
     98  1.1  drochner 	 * u.v is now normalized and oldexp has been adjusted if necessary.
     99  1.1  drochner 	 * Calculate the new exponent and check for underflow and overflow.
    100  1.1  drochner 	 */
    101  1.1  drochner 	newexp = oldexp + expon;
    102  1.1  drochner 
    103  1.1  drochner 	if (newexp <= 0) {
    104  1.1  drochner 		/*
    105  1.1  drochner 		 * The output number is either denormal or underflows (see
    106  1.1  drochner 		 * comments in machine/ieee.h).
    107  1.1  drochner 		 */
    108  1.1  drochner 		if (newexp <= -DBL_FRACBITS) {
    109  1.1  drochner 			errno = ERANGE;
    110  1.1  drochner 			return (val < 0.0 ? -0.0 : 0.0);
    111  1.1  drochner 		}
    112  1.1  drochner 		/*
    113  1.1  drochner 		 * Denormalize the result.  We do this with a multiply.  If
    114  1.1  drochner 		 * expon is very large, it won't fit in a double, so we have
    115  1.1  drochner 		 * to adjust the exponent first.  This is safe because we know
    116  1.1  drochner 		 * that u.v is normal at this point.
    117  1.1  drochner 		 */
    118  1.1  drochner 		if (expon <= -DBL_EXP_BIAS) {
    119  1.1  drochner 			u.dblu_dbl.dbl_exp = 1;
    120  1.1  drochner 			expon += oldexp - 1;
    121  1.1  drochner 		}
    122  1.1  drochner 		mul.dblu_d = 0.0;
    123  1.1  drochner 		mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
    124  1.1  drochner 		u.dblu_d *= mul.dblu_d;
    125  1.1  drochner 		return (u.dblu_d);
    126  1.1  drochner 	} else if (newexp >= DBL_EXP_INFNAN) {
    127  1.1  drochner 		/*
    128  1.1  drochner 		 * The result overflowed; return +/-Inf.
    129  1.1  drochner 		 */
    130  1.1  drochner 		u.dblu_dbl.dbl_exp = DBL_EXP_INFNAN;
    131  1.1  drochner 		u.dblu_dbl.dbl_frach = 0;
    132  1.1  drochner 		u.dblu_dbl.dbl_fracl = 0;
    133  1.1  drochner 		errno = ERANGE;
    134  1.1  drochner 		return (u.dblu_d);
    135  1.1  drochner 	} else {
    136  1.1  drochner 		/*
    137  1.1  drochner 		 * The result is normal; just replace the old exponent with the
    138  1.1  drochner 		 * new one.
    139  1.1  drochner 		 */
    140  1.1  drochner 		u.dblu_dbl.dbl_exp = newexp;
    141  1.1  drochner 		return (u.dblu_d);
    142  1.1  drochner 	}
    143  1.1  drochner }
    144