compat_ldexp_ieee754.c revision 1.8 1 1.8 riastrad /* $NetBSD: compat_ldexp_ieee754.c,v 1.8 2020/05/21 05:56:31 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.8 riastrad __RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.8 2020/05/21 05:56:31 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.1 drochner * Calculate the new exponent and check for underflow and overflow.
119 1.1 drochner */
120 1.1 drochner newexp = oldexp + expon;
121 1.1 drochner
122 1.6 christos if (newexp >= DBL_EXP_INFNAN ||
123 1.6 christos (oldexp >= 0 && expon >= DBL_EXP_INFNAN)) {
124 1.6 christos /*
125 1.6 christos * The result overflowed; return +/-Inf.
126 1.6 christos */
127 1.8 riastrad return overflow(val);
128 1.6 christos } else if (newexp <= 0) {
129 1.1 drochner /*
130 1.1 drochner * The output number is either denormal or underflows (see
131 1.1 drochner * comments in machine/ieee.h).
132 1.1 drochner */
133 1.8 riastrad if (newexp <= -DBL_FRACBITS)
134 1.8 riastrad return underflow(val);
135 1.1 drochner /*
136 1.1 drochner * Denormalize the result. We do this with a multiply. If
137 1.1 drochner * expon is very large, it won't fit in a double, so we have
138 1.1 drochner * to adjust the exponent first. This is safe because we know
139 1.1 drochner * that u.v is normal at this point.
140 1.1 drochner */
141 1.1 drochner if (expon <= -DBL_EXP_BIAS) {
142 1.1 drochner u.dblu_dbl.dbl_exp = 1;
143 1.1 drochner expon += oldexp - 1;
144 1.1 drochner }
145 1.1 drochner mul.dblu_d = 0.0;
146 1.1 drochner mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
147 1.1 drochner u.dblu_d *= mul.dblu_d;
148 1.1 drochner return (u.dblu_d);
149 1.1 drochner } else {
150 1.1 drochner /*
151 1.1 drochner * The result is normal; just replace the old exponent with the
152 1.1 drochner * new one.
153 1.1 drochner */
154 1.1 drochner u.dblu_dbl.dbl_exp = newexp;
155 1.1 drochner return (u.dblu_d);
156 1.1 drochner }
157 1.1 drochner }
158