1 /* $NetBSD: ldexp.c,v 1.1 1995/02/10 17:50:24 cgd Exp $ */
2 /* $FreeBSD: src/lib/libc/alpha/gen/ldexp.c,v 1.1.1.1.6.1 2000/08/21 21:09:29 jhb Exp $ */
5 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
8 * Author: Chris G. Demetriou
10 * Permission to use, copy, modify and distribute this software and
11 * its documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 * Carnegie Mellon requests users of this software to return to
22 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
23 * School of Computer Science
24 * Carnegie Mellon University
25 * Pittsburgh PA 15213-3890
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
31 #include <sys/types.h>
32 #include <machine/ieee.h>
37 * double ldexp(double val, int exp)
38 * returns: val * (2**exp)
45 register int oldexp, newexp, mulexp;
52 * If input is zero, or no change, just return input.
53 * Likewise, if input is Inf or NaN, just return it.
57 if (val == 0 || exp == 0 || oldexp == DBL_EXP_INFNAN)
61 * Compute new exponent and check for over/under flow.
62 * Underflow, unfortunately, could mean switching to denormal.
63 * If result out of range, set ERANGE and return 0 if too small
64 * or Inf if too big, with the same sign as the input value.
66 newexp = oldexp + exp;
67 if (newexp >= DBL_EXP_INFNAN) {
68 /* u.s.dbl_sign = val < 0; -- already set */
69 u.s.dbl_exp = DBL_EXP_INFNAN;
70 u.s.dbl_frach = u.s.dbl_fracl = 0;
72 return (u.v); /* Inf */
76 * The output number is either a denormal or underflows
77 * (see comments in machine/ieee.h).
79 if (newexp <= -DBL_FRACBITS) {
80 /* u.s.dbl_sign = val < 0; -- already set */
82 u.s.dbl_frach = u.s.dbl_fracl = 0;
84 return (u.v); /* zero */
87 * We are going to produce a denorm. Our `exp' argument
88 * might be as small as -2097, and we cannot compute
89 * 2^-2097, so we may have to do this as many as three
90 * steps (not just two, as for positive `exp's below).
93 while (exp <= -DBL_EXP_BIAS) {
96 exp += DBL_EXP_BIAS - 1;
98 mul.s.dbl_exp = exp + DBL_EXP_BIAS;
104 * Newexp is positive.
106 * If oldexp is zero, we are starting with a denorm, and simply
107 * adjusting the exponent will produce bogus answers. We need
112 * Multiply by 2^mulexp to make the number normalizable.
113 * We cannot multiply by more than 2^1023, but `exp'
114 * argument might be as large as 2046. A single
115 * adjustment, however, will normalize the number even
116 * for huge `exp's, and then we can use exponent
117 * arithmetic just as for normal `double's.
119 mulexp = exp <= DBL_EXP_BIAS ? exp : DBL_EXP_BIAS;
121 mul.s.dbl_exp = mulexp + DBL_EXP_BIAS;
130 * Both oldexp and newexp are positive; just replace the
131 * old exponent with the new one.
133 u.s.dbl_exp = newexp;