4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1988 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI" /* from S5R2 2.7 */
34 * double ldexp (value, exp)
38 * Ldexp returns value * 2**exp, if that result is in range.
39 * If underflow occurs, it returns zero. If overflow occurs,
40 * it returns a value of appropriate sign and largest single-
41 * precision magnitude. In case of underflow or overflow,
42 * the external int "errno" is set to ERANGE. Note that errno is
43 * not modified if no error occurs, so if you intend to test it
44 * after you use ldexp, you had better set it to something
45 * other than ERANGE first (zero is a reasonable value to use).
50 /* Largest signed long int power of 2 */
51 #define MAXSHIFT (BITSPERBYTE * sizeof(long) - 2)
53 extern double frexp();
57 register double value
;
62 if (exp
== 0 || value
== 0.0) /* nothing to do for zero */
64 #if !(pdp11 || u3b5) /* pdp11 "cc" can't handle cast of
65 double to void on pdp11 or 3b5 */
68 frexp(value
, &old_exp
);
70 if (exp
+ old_exp
> MAXBEXP
) { /* overflow */
72 return ((double)(value
< 0 ? MINDOUBLE
: MAXDOUBLE
));
74 return ((double)(value < 0 ? -1.0e999 : 1.0e999));
77 for ( ; exp
> MAXSHIFT
; exp
-= MAXSHIFT
)
78 value
*= (1L << MAXSHIFT
);
79 return (value
* (1L << exp
));
81 if (exp
+ old_exp
< MINBEXP
) { /* underflow */
85 for ( ; exp
< -MAXSHIFT
; exp
+= MAXSHIFT
)
86 value
*= 1.0/(1L << MAXSHIFT
); /* mult faster than div */
87 return (value
/ (1L << -exp
));