1 /* @(#)s_scalbn.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
13 /*
14 * scalbnl (long double x, int n)
15 * scalbnl(x,n) returns x* 2**n computed by exponent
16 * manipulation rather than by actually performing an
17 * exponentiation or a multiplication.
18 */
19
20 /*
21 * We assume that a long double has a 15-bit exponent. On systems
22 * where long double is the same as double, scalbnl() is an alias
23 * for scalbn(), so we don't use this routine.
24 */
25
26
27 #if LDBL_MAX_EXP == 0x4000
28
29 long double
scalbnl(long double x,int n)30 scalbnl (long double x, int n)
31 {
32 union IEEEl2bits u;
33 __int32_t k;
34 u.e = x;
35 k = u.bits.exp; /* extract exponent */
36 if (k==0) { /* 0 or subnormal x */
37 if ((u.bits.manh|u.bits.manl)==0) return x; /* +-0 */
38 u.e *= 0x1p+128L;
39 k = u.bits.exp - 128;
40 if (n< -50000) return __math_uflowl(u.bits.sign);
41 }
42 if (k==0x7fff) return x+x; /* NaN or Inf */
43 #if __SIZEOF_INT__ > 2
44 if (n > 50000) /* in case integer overflow in n+k */
45 return __math_oflowl(u.bits.sign); /*overflow*/
46 #endif
47 k = k+n;
48 if (k >= 0x7fff) return __math_oflowl(u.bits.sign); /* overflow */
49 if (k > 0) /* normal result */
50 {u.bits.exp = k; return u.e;}
51 if (k <= -128)
52 return __math_uflowl(u.bits.sign); /*underflow*/
53 k += 128; /* subnormal result */
54 u.bits.exp = k;
55 return check_uflowl(u.e*0x1p-128L);
56 }
57
58 #elif defined(_DOUBLE_DOUBLE_FLOAT)
59
60 long double
scalbnl(long double x,int n)61 scalbnl(long double x, int n)
62 {
63 union IEEEl2bits u;
64 double dh, dl;
65
66 u.e = x;
67 dh = scalbn(u.dbits.dh, n);
68 dl = scalbn(u.dbits.dl, n);
69 return (long double) dh + (long double) dl;
70 }
71
72 #endif
73
74 #ifdef _HAVE_ALIAS_ATTRIBUTE
75 __strong_reference(scalbnl, ldexpl);
76 #else
77 long double
ldexpl(long double x,int n)78 ldexpl(long double x, int n)
79 {
80 return scalbnl(x, n);
81 }
82 #endif
83