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