xref: /relibc/openlibm/src/s_scalbnl.c (revision 232ba9db6e4b36dd94ff2ca1022f9ba09970a2ec)
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 #include "cdefs-compat.h"
27 
28 #include <float.h>
29 #include <openlibm_math.h>
30 
31 #include "fpmath.h"
32 #include "math_private.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 OLM_DLLEXPORT 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         }
65 	k += 128;				/* subnormal result */
66 	u.bits.exp = k;
67         return u.e*0x1p-128;
68 }
69 
70 __strong_reference(scalbnl, ldexpl);
71