libm: Sync with FreeBSD (gains 6 long double functions)
[dragonfly.git] / lib / libm / src / s_scalbnl.c
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  * $FreeBSD: head/lib/msun/src/s_scalbnl.c 143206 2005-03-07 04:52:58Z das
13  */
14
15
16 /*
17  * scalbnl (long double x, int n)
18  * scalbnl(x,n) returns x* 2**n  computed by  exponent
19  * manipulation rather than by actually performing an
20  * exponentiation or a multiplication.
21  */
22
23 /*
24  * We assume that a long double has a 15-bit exponent.  On systems
25  * where long double is the same as double, scalbnl() is an alias
26  * for scalbn(), so we don't use this routine.
27  */
28
29 #include <float.h>
30 #include <math.h>
31
32 #include "fpmath.h"
33
34 #if LDBL_MAX_EXP != 0x4000
35 #error "Unsupported long double format"
36 #endif
37
38 static const long double
39 huge = 0x1p16000L,
40 tiny = 0x1p-16000L;
41
42 long double
43 scalbnl (long double x, int n)
44 {
45         union IEEEl2bits u;
46         int k;
47         u.e = x;
48         k = u.bits.exp;                         /* extract exponent */
49         if (k==0) {                             /* 0 or subnormal x */
50             if ((u.bits.manh|u.bits.manl)==0) return x; /* +-0 */
51             u.e *= 0x1p+128;
52             k = u.bits.exp - 128;
53             if (n< -50000) return tiny*x;       /*underflow*/
54             }
55         if (k==0x7fff) return x+x;              /* NaN or Inf */
56         k = k+n;
57         if (k >= 0x7fff) return huge*copysignl(huge,x); /* overflow  */
58         if (k > 0)                              /* normal result */
59             {u.bits.exp = k; return u.e;}
60         if (k <= -128)
61             if (n > 50000)      /* in case integer overflow in n+k */
62                 return huge*copysign(huge,x);   /*overflow*/
63             else return tiny*copysign(tiny,x);  /*underflow*/
64         k += 128;                               /* subnormal result */
65         u.bits.exp = k;
66         return u.e*0x1p-128;
67 }
68
69 __strong_reference(scalbnl, ldexpl);