1 1.9 riastrad /* $NetBSD: compat_ldexp_ieee754.c,v 1.9 2024/06/17 18:16:58 riastradh 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.9 riastrad __RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.9 2024/06/17 18:16:58 riastradh 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.8 riastrad 39 1.1 drochner #include <machine/ieee.h> 40 1.8 riastrad 41 1.1 drochner #include <errno.h> 42 1.8 riastrad #include <float.h> 43 1.8 riastrad #include <math.h> 44 1.8 riastrad 45 1.8 riastrad static volatile const double tiny = DBL_MIN, huge = DBL_MAX; 46 1.8 riastrad 47 1.8 riastrad static double 48 1.8 riastrad underflow(double val) 49 1.8 riastrad { 50 1.8 riastrad 51 1.8 riastrad errno = ERANGE; 52 1.8 riastrad return (val < 0 ? -tiny*tiny : tiny*tiny); 53 1.8 riastrad } 54 1.8 riastrad 55 1.8 riastrad static double 56 1.8 riastrad overflow(double val) 57 1.8 riastrad { 58 1.5 drochner 59 1.8 riastrad errno = ERANGE; 60 1.8 riastrad return (val < 0 ? -huge*huge : huge*huge); 61 1.8 riastrad } 62 1.1 drochner 63 1.1 drochner /* 64 1.1 drochner * Multiply the given value by 2^expon. 65 1.1 drochner */ 66 1.1 drochner double 67 1.1 drochner ldexp(double val, int expon) 68 1.1 drochner { 69 1.1 drochner int oldexp, newexp; 70 1.1 drochner union ieee_double_u u, mul; 71 1.1 drochner 72 1.1 drochner u.dblu_d = val; 73 1.1 drochner oldexp = u.dblu_dbl.dbl_exp; 74 1.1 drochner 75 1.1 drochner /* 76 1.8 riastrad * If input is zero, Inf or NaN, just return it, but raise 77 1.8 riastrad * invalid exception if it is a signalling NaN: adding any of 78 1.8 riastrad * these inputs to itself gives itself as output; arithmetic on 79 1.8 riastrad * a signalling NaN additionally raises invalid-operation. 80 1.1 drochner */ 81 1.1 drochner if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN) 82 1.8 riastrad return (val + val); 83 1.1 drochner 84 1.1 drochner if (oldexp == 0) { 85 1.1 drochner /* 86 1.1 drochner * u.v is denormal. We must adjust it so that the exponent 87 1.1 drochner * arithmetic below will work. 88 1.1 drochner */ 89 1.1 drochner if (expon <= DBL_EXP_BIAS) { 90 1.1 drochner /* 91 1.1 drochner * Optimization: if the scaling can be done in a single 92 1.1 drochner * multiply, or underflows, just do it now. 93 1.1 drochner */ 94 1.8 riastrad if (expon <= -DBL_FRACBITS) 95 1.8 riastrad return underflow(val); 96 1.1 drochner mul.dblu_d = 0.0; 97 1.1 drochner mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS; 98 1.1 drochner u.dblu_d *= mul.dblu_d; 99 1.8 riastrad if (u.dblu_d == 0.0) 100 1.8 riastrad return underflow(val); 101 1.1 drochner return (u.dblu_d); 102 1.1 drochner } else { 103 1.1 drochner /* 104 1.1 drochner * We know that expon is very large, and therefore the 105 1.1 drochner * result cannot be denormal (though it may be Inf). 106 1.1 drochner * Shift u.v by just enough to make it normal. 107 1.1 drochner */ 108 1.1 drochner mul.dblu_d = 0.0; 109 1.1 drochner mul.dblu_dbl.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS; 110 1.1 drochner u.dblu_d *= mul.dblu_d; 111 1.1 drochner expon -= DBL_FRACBITS; 112 1.1 drochner oldexp = u.dblu_dbl.dbl_exp; 113 1.1 drochner } 114 1.1 drochner } 115 1.1 drochner 116 1.1 drochner /* 117 1.1 drochner * u.v is now normalized and oldexp has been adjusted if necessary. 118 1.9 riastrad * We have 119 1.9 riastrad * 120 1.9 riastrad * 0 <= oldexp <= DBL_EXP_INFNAN, 121 1.9 riastrad * 122 1.9 riastrad * but 123 1.9 riastrad * 124 1.9 riastrad * INT_MIN <= expon <= INT_MAX. 125 1.9 riastrad * 126 1.9 riastrad * Check for underflow and overflow, and if none, calculate the 127 1.9 riastrad * new exponent. 128 1.1 drochner */ 129 1.9 riastrad if (expon >= DBL_EXP_INFNAN - oldexp) { 130 1.6 christos /* 131 1.6 christos * The result overflowed; return +/-Inf. 132 1.6 christos */ 133 1.8 riastrad return overflow(val); 134 1.9 riastrad } 135 1.9 riastrad 136 1.9 riastrad /* 137 1.9 riastrad * We now have INT_MIN <= oldexp + expon <= DBL_EXP_INFNAN <= INT_MAX, 138 1.9 riastrad * so the arithmetic is safe. 139 1.9 riastrad */ 140 1.9 riastrad newexp = oldexp + expon; 141 1.9 riastrad 142 1.9 riastrad if (newexp <= 0) { 143 1.1 drochner /* 144 1.1 drochner * The output number is either denormal or underflows (see 145 1.1 drochner * comments in machine/ieee.h). 146 1.1 drochner */ 147 1.8 riastrad if (newexp <= -DBL_FRACBITS) 148 1.8 riastrad return underflow(val); 149 1.1 drochner /* 150 1.1 drochner * Denormalize the result. We do this with a multiply. If 151 1.1 drochner * expon is very large, it won't fit in a double, so we have 152 1.1 drochner * to adjust the exponent first. This is safe because we know 153 1.1 drochner * that u.v is normal at this point. 154 1.1 drochner */ 155 1.1 drochner if (expon <= -DBL_EXP_BIAS) { 156 1.1 drochner u.dblu_dbl.dbl_exp = 1; 157 1.1 drochner expon += oldexp - 1; 158 1.1 drochner } 159 1.1 drochner mul.dblu_d = 0.0; 160 1.1 drochner mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS; 161 1.1 drochner u.dblu_d *= mul.dblu_d; 162 1.1 drochner return (u.dblu_d); 163 1.1 drochner } else { 164 1.1 drochner /* 165 1.1 drochner * The result is normal; just replace the old exponent with the 166 1.1 drochner * new one. 167 1.1 drochner */ 168 1.1 drochner u.dblu_dbl.dbl_exp = newexp; 169 1.1 drochner return (u.dblu_d); 170 1.1 drochner } 171 1.1 drochner } 172