Home | History | Annotate | Line # | Download | only in gen
compat_ldexp_ieee754.c revision 1.6
      1  1.6  christos /* $NetBSD: compat_ldexp_ieee754.c,v 1.6 2016/08/27 09:11:56 christos 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.6  christos __RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.6 2016/08/27 09:11:56 christos 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.6  christos #include <stdio.h>
     44  1.1  drochner /*
     45  1.1  drochner  * Multiply the given value by 2^expon.
     46  1.1  drochner  */
     47  1.1  drochner double
     48  1.1  drochner ldexp(double val, int expon)
     49  1.1  drochner {
     50  1.1  drochner 	int oldexp, newexp;
     51  1.1  drochner 	union ieee_double_u u, mul;
     52  1.1  drochner 
     53  1.1  drochner 	u.dblu_d = val;
     54  1.1  drochner 	oldexp = u.dblu_dbl.dbl_exp;
     55  1.1  drochner 
     56  1.1  drochner 	/*
     57  1.1  drochner 	 * If input is zero, Inf or NaN, just return it.
     58  1.1  drochner 	 */
     59  1.1  drochner 	if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN)
     60  1.1  drochner 		return (val);
     61  1.1  drochner 
     62  1.1  drochner 	if (oldexp == 0) {
     63  1.1  drochner 		/*
     64  1.1  drochner 		 * u.v is denormal.  We must adjust it so that the exponent
     65  1.1  drochner 		 * arithmetic below will work.
     66  1.1  drochner 		 */
     67  1.1  drochner 		if (expon <= DBL_EXP_BIAS) {
     68  1.1  drochner 			/*
     69  1.1  drochner 			 * Optimization: if the scaling can be done in a single
     70  1.1  drochner 			 * multiply, or underflows, just do it now.
     71  1.1  drochner 			 */
     72  1.1  drochner 			if (expon <= -DBL_FRACBITS) {
     73  1.1  drochner 				errno = ERANGE;
     74  1.1  drochner 				return (val < 0.0 ? -0.0 : 0.0);
     75  1.1  drochner 			}
     76  1.1  drochner 			mul.dblu_d = 0.0;
     77  1.1  drochner 			mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
     78  1.1  drochner 			u.dblu_d *= mul.dblu_d;
     79  1.1  drochner 			if (u.dblu_d == 0.0) {
     80  1.1  drochner 				errno = ERANGE;
     81  1.1  drochner 				return (val < 0.0 ? -0.0 : 0.0);
     82  1.1  drochner 			}
     83  1.1  drochner 			return (u.dblu_d);
     84  1.1  drochner 		} else {
     85  1.1  drochner 			/*
     86  1.1  drochner 			 * We know that expon is very large, and therefore the
     87  1.1  drochner 			 * result cannot be denormal (though it may be Inf).
     88  1.1  drochner 			 * Shift u.v by just enough to make it normal.
     89  1.1  drochner 			 */
     90  1.1  drochner 			mul.dblu_d = 0.0;
     91  1.1  drochner 			mul.dblu_dbl.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS;
     92  1.1  drochner 			u.dblu_d *= mul.dblu_d;
     93  1.1  drochner 			expon -= DBL_FRACBITS;
     94  1.1  drochner 			oldexp = u.dblu_dbl.dbl_exp;
     95  1.1  drochner 		}
     96  1.1  drochner 	}
     97  1.1  drochner 
     98  1.1  drochner 	/*
     99  1.1  drochner 	 * u.v is now normalized and oldexp has been adjusted if necessary.
    100  1.1  drochner 	 * Calculate the new exponent and check for underflow and overflow.
    101  1.1  drochner 	 */
    102  1.1  drochner 	newexp = oldexp + expon;
    103  1.6  christos 	printf("ee %#x oldexp %#x\n", oldexp, expon);
    104  1.1  drochner 
    105  1.6  christos 	if (newexp >= DBL_EXP_INFNAN ||
    106  1.6  christos 	    (oldexp >= 0 && expon >= DBL_EXP_INFNAN)) {
    107  1.6  christos 		/*
    108  1.6  christos 		 * The result overflowed; return +/-Inf.
    109  1.6  christos 		 */
    110  1.6  christos 		u.dblu_dbl.dbl_exp = DBL_EXP_INFNAN;
    111  1.6  christos 		u.dblu_dbl.dbl_frach = 0;
    112  1.6  christos 		u.dblu_dbl.dbl_fracl = 0;
    113  1.6  christos 		errno = ERANGE;
    114  1.6  christos 		return (u.dblu_d);
    115  1.6  christos 	} else if (newexp <= 0) {
    116  1.1  drochner 		/*
    117  1.1  drochner 		 * The output number is either denormal or underflows (see
    118  1.1  drochner 		 * comments in machine/ieee.h).
    119  1.1  drochner 		 */
    120  1.1  drochner 		if (newexp <= -DBL_FRACBITS) {
    121  1.1  drochner 			errno = ERANGE;
    122  1.1  drochner 			return (val < 0.0 ? -0.0 : 0.0);
    123  1.1  drochner 		}
    124  1.1  drochner 		/*
    125  1.1  drochner 		 * Denormalize the result.  We do this with a multiply.  If
    126  1.1  drochner 		 * expon is very large, it won't fit in a double, so we have
    127  1.1  drochner 		 * to adjust the exponent first.  This is safe because we know
    128  1.1  drochner 		 * that u.v is normal at this point.
    129  1.1  drochner 		 */
    130  1.1  drochner 		if (expon <= -DBL_EXP_BIAS) {
    131  1.1  drochner 			u.dblu_dbl.dbl_exp = 1;
    132  1.1  drochner 			expon += oldexp - 1;
    133  1.1  drochner 		}
    134  1.1  drochner 		mul.dblu_d = 0.0;
    135  1.1  drochner 		mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
    136  1.1  drochner 		u.dblu_d *= mul.dblu_d;
    137  1.1  drochner 		return (u.dblu_d);
    138  1.1  drochner 	} else {
    139  1.1  drochner 		/*
    140  1.1  drochner 		 * The result is normal; just replace the old exponent with the
    141  1.1  drochner 		 * new one.
    142  1.1  drochner 		 */
    143  1.1  drochner 		u.dblu_dbl.dbl_exp = newexp;
    144  1.1  drochner 		return (u.dblu_d);
    145  1.1  drochner 	}
    146  1.1  drochner }
    147