First public contribution.
3 * Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies).
8 /* @(#)s_scalbn.c 5.1 93/09/24 */
10 * ====================================================
11 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
13 * Developed at SunPro, a Sun Microsystems, Inc. business.
14 * Permission to use, copy, modify, and distribute this
15 * software is freely granted, provided that this notice
17 * ====================================================
22 <<scalbn>>, <<scalbnf>>---scale by integer
30 double scalbn(double <[x]>, int <[y]>);
31 float scalbnf(float <[x]>, int <[y]>);
35 double scalbn(<[x]>,<[y]>)
38 float scalbnf(<[x]>,<[y]>)
43 <<scalbn>> and <<scalbnf>> scale <[x]> by <[n]>, returning <[x]> times
44 2 to the power <[n]>. The result is computed by manipulating the
45 exponent, rather than by actually performing an exponentiation or
49 <[x]> times 2 to the power <[n]>.
52 Neither <<scalbn>> nor <<scalbnf>> is required by ANSI C or by the System V
53 Interface Definition (Issue 2).
58 * scalbn (double x, int n)
59 * scalbn(x,n) returns x* 2**n computed by exponent
60 * manipulation rather than by actually performing an
61 * exponentiation or a multiplication.
67 two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
68 twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
73 Scales x by n, returning x times
74 2 to the power n. The result is computed by manipulating the
75 exponent, rather than by actually performing an exponentiation or
77 @return x times 2 to the power n.
78 @param x floating point value
79 @param n integer power
81 EXPORT_C double scalbn (double x, int n) __SOFTFP
84 EXTRACT_WORDS(hx,lx,x);
85 k = (hx&0x7ff00000)>>20; /* extract exponent */
86 if (k==0) { /* 0 or subnormal x */
87 if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
90 k = ((hx&0x7ff00000)>>20) - 54;
91 if (n< -50000) return tiny*x; /*underflow*/
93 if (k==0x7ff) return x+x; /* NaN or Inf */
95 if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
96 if (k > 0) /* normal result */
97 {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
99 if (n > 50000) /* in case integer overflow in n+k */
100 return huge*copysign(huge,x); /*overflow*/
101 else return tiny*copysign(tiny,x); /*underflow*/
103 k += 54; /* subnormal result */
104 SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));