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 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD: head/lib/msun/src/s_scalbnl.c 306409 2016-09-28 14:48:34Z emaste $");
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
scalbnl(long double x,int n)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
64 return tiny*copysign(tiny,x); /*underflow*/
65 }
66 k += 128; /* subnormal result */
67 u.bits.exp = k;
68 return u.e*0x1p-128;
69 }
70
71 __strong_reference(scalbnl, ldexpl);
72