first commit of the new libm!
[musl] / src / math / scalbnl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_scalbnl.c */
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  * scalbnl (long double x, int n)
14  * scalbnl(x,n) returns x* 2**n  computed by  exponent
15  * manipulation rather than by actually performing an
16  * exponentiation or a multiplication.
17  */
18
19 #include "libm.h"
20
21 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
22 long double scalbnl(long double x, int n)
23 {
24         return scalbn(x, n);
25 }
26 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
27 static const long double
28 huge = 0x1p16000L,
29 tiny = 0x1p-16000L;
30
31 long double scalbnl(long double x, int n)
32 {
33         union IEEEl2bits u;
34         int k;
35
36         u.e = x;
37         k = u.bits.exp;                    /* extract exponent */
38         if (k == 0) {                      /* 0 or subnormal x */
39                 if ((u.bits.manh|u.bits.manl) == 0)  /* +-0 */
40                         return x;
41                 u.e *= 0x1p128;
42                 k = u.bits.exp - 128;
43                 if (n < -50000)
44                         return tiny*x;  /*underflow*/
45         }
46         if (k == 0x7fff)                   /* NaN or Inf */
47                 return x + x;
48         k = k + n;
49         if (k >= 0x7fff)
50                 return huge*copysignl(huge, x);  /* overflow  */
51         if (k > 0) {                       /* normal result */
52                 u.bits.exp = k;
53                 return u.e;
54         }
55         if (k <= -128)
56                 if (n > 50000)  /* in case integer overflow in n+k */
57                         return huge*copysign(huge, x);  /*overflow*/
58                 return tiny*copysign(tiny, x);  /*underflow*/
59         k += 128;                          /* subnormal result */
60         u.bits.exp = k;
61         return u.e*0x1p-128;
62 }
63 #endif