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