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>>---scale by power of two
24 double scalbn(double <[x]>, int <[y]>);
25 float scalbnf(float <[x]>, int <[y]>);
29 double scalbn(<[x]>,<[y]>)
32 float scalbnf(<[x]>,<[y]>)
37 <<scalbn>> and <<scalbnf>> scale <[x]> by <[n]>, returning <[x]> times
38 2 to the power <[n]>. The result is computed by manipulating the
39 exponent, rather than by actually performing an exponentiation or
43 <[x]> times 2 to the power <[n]>.
46 Neither <<scalbn>> nor <<scalbnf>> is required by ANSI C or by the System V
47 Interface Definition (Issue 2).
52 * scalbn (double x, int n)
53 * scalbn(x,n) returns x* 2**n computed by exponent
54 * manipulation rather than by actually performing an
55 * exponentiation or a multiplication.
60 #ifndef _DOUBLE_IS_32BITS
67 two54
= 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
68 twom54
= 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
73 double scalbn (double x
, int n
)
80 EXTRACT_WORDS(hx
,lx
,x
);
81 k
= (hx
&0x7ff00000)>>20; /* extract exponent */
82 if (k
==0) { /* 0 or subnormal x */
83 if ((lx
|(hx
&0x7fffffff))==0) return x
; /* +-0 */
86 k
= ((hx
&0x7ff00000)>>20) - 54;
87 if (n
< -50000) return tiny
*x
; /*underflow*/
89 if (k
==0x7ff) return x
+x
; /* NaN or Inf */
91 if (k
> 0x7fe) return huge
*copysign(huge
,x
); /* overflow */
92 if (k
> 0) /* normal result */
93 {SET_HIGH_WORD(x
,(hx
&0x800fffff)|(k
<<20)); return x
;}
95 if (n
> 50000) /* in case integer overflow in n+k */
96 return huge
*copysign(huge
,x
); /*overflow*/
97 else return tiny
*copysign(tiny
,x
); /*underflow*/
99 k
+= 54; /* subnormal result */
100 SET_HIGH_WORD(x
,(hx
&0x800fffff)|(k
<<20));
104 #endif /* _DOUBLE_IS_32BITS */