2 /* @(#)s_scalbn.c 5.1 93/09/24 */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
16 <<scalbn>>, <<scalbnf>>, <<scalbln>>, <<scalblnf>>---scale by power of FLT_RADIX (=2)
28 double scalbn(double <[x]>, int <[n]>);
29 float scalbnf(float <[x]>, int <[n]>);
30 double scalbln(double <[x]>, long int <[n]>);
31 float scalblnf(float <[x]>, long int <[n]>);
34 The <<scalbn>> and <<scalbln>> functions compute
36 <[x]> times FLT_RADIX to the power <[n]>.
39 $x \cdot FLT\_RADIX^n$.
41 efficiently. The result is computed by manipulating the exponent, rather than
42 by actually performing an exponentiation or multiplication. In this
43 floating-point implementation FLT_RADIX=2, which makes the <<scalbn>>
44 functions equivalent to the <<ldexp>> functions.
47 <[x]> times 2 to the power <[n]>. A range error may occur.
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.
66 #ifndef _DOUBLE_IS_32BITS
73 two54
= 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
74 twom54
= 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
79 double scalbn (double x
, int n
)
86 EXTRACT_WORDS(hx
,lx
,x
);
87 k
= (hx
&0x7ff00000)>>20; /* extract exponent */
88 if (k
==0) { /* 0 or subnormal x */
89 if ((lx
|(hx
&0x7fffffff))==0) return x
; /* +-0 */
92 k
= ((hx
&0x7ff00000)>>20) - 54;
93 if (n
< -50000) return tiny
*x
; /*underflow*/
95 if (k
==0x7ff) return x
+x
; /* NaN or Inf */
96 if (n
> 50000) /* in case integer overflow in n+k */
97 return huge
*copysign(huge
,x
); /*overflow*/
99 if (k
> 0x7fe) return huge
*copysign(huge
,x
); /* overflow */
100 if (k
> 0) /* normal result */
101 {SET_HIGH_WORD(x
,(hx
&0x800fffff)|(k
<<20)); return x
;}
103 return tiny
*copysign(tiny
,x
); /*underflow*/
104 k
+= 54; /* subnormal result */
105 SET_HIGH_WORD(x
,(hx
&0x800fffff)|(k
<<20));
109 #endif /* _DOUBLE_IS_32BITS */