xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/scalbnq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* s_scalbnl.c -- long double version of s_scalbn.c.
2*181254a7Smrg  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3*181254a7Smrg  */
4*181254a7Smrg 
5*181254a7Smrg /* @(#)s_scalbn.c 5.1 93/09/24 */
6*181254a7Smrg /*
7*181254a7Smrg  * ====================================================
8*181254a7Smrg  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9*181254a7Smrg  *
10*181254a7Smrg  * Developed at SunPro, a Sun Microsystems, Inc. business.
11*181254a7Smrg  * Permission to use, copy, modify, and distribute this
12*181254a7Smrg  * software is freely granted, provided that this notice
13*181254a7Smrg  * is preserved.
14*181254a7Smrg  * ====================================================
15*181254a7Smrg  */
16*181254a7Smrg 
17*181254a7Smrg #if defined(LIBM_SCCS) && !defined(lint)
18*181254a7Smrg static char rcsid[] = "NetBSD: ";
19*181254a7Smrg #endif
20*181254a7Smrg 
21*181254a7Smrg /*
22*181254a7Smrg  * scalbnq (long double x, int n)
23*181254a7Smrg  * scalbnq(x,n) returns x* 2**n  computed by  exponent
24*181254a7Smrg  * manipulation rather than by actually performing an
25*181254a7Smrg  * exponentiation or a multiplication.
26*181254a7Smrg  */
27*181254a7Smrg 
28*181254a7Smrg #include "quadmath-imp.h"
29*181254a7Smrg 
30*181254a7Smrg static const __float128
31*181254a7Smrg two114 = 2.0769187434139310514121985316880384E+34Q, /* 0x4071000000000000, 0 */
32*181254a7Smrg twom114 = 4.8148248609680896326399448564623183E-35Q, /* 0x3F8D000000000000, 0 */
33*181254a7Smrg huge   = 1.0E+4900Q,
34*181254a7Smrg tiny   = 1.0E-4900Q;
35*181254a7Smrg 
scalbnq(__float128 x,int n)36*181254a7Smrg __float128 scalbnq (__float128 x, int n)
37*181254a7Smrg {
38*181254a7Smrg 	int64_t k,hx,lx;
39*181254a7Smrg 	GET_FLT128_WORDS64(hx,lx,x);
40*181254a7Smrg         k = (hx>>48)&0x7fff;		/* extract exponent */
41*181254a7Smrg         if (k==0) {				/* 0 or subnormal x */
42*181254a7Smrg             if ((lx|(hx&0x7fffffffffffffffULL))==0) return x; /* +-0 */
43*181254a7Smrg 	    x *= two114;
44*181254a7Smrg 	    GET_FLT128_MSW64(hx,x);
45*181254a7Smrg 	    k = ((hx>>48)&0x7fff) - 114;
46*181254a7Smrg 	}
47*181254a7Smrg         if (k==0x7fff) return x+x;		/* NaN or Inf */
48*181254a7Smrg 	if (n< -50000) return tiny*copysignq(tiny,x); /*underflow*/
49*181254a7Smrg         if (n> 50000 || k+n > 0x7ffe)
50*181254a7Smrg 	  return huge*copysignq(huge,x); /* overflow  */
51*181254a7Smrg 	/* Now k and n are bounded we know that k = k+n does not
52*181254a7Smrg 	   overflow.  */
53*181254a7Smrg         k = k+n;
54*181254a7Smrg         if (k > 0) 				/* normal result */
55*181254a7Smrg 	    {SET_FLT128_MSW64(x,(hx&0x8000ffffffffffffULL)|(k<<48)); return x;}
56*181254a7Smrg         if (k <= -114)
57*181254a7Smrg 	  return tiny*copysignq(tiny,x); 	/*underflow*/
58*181254a7Smrg         k += 114;				/* subnormal result */
59*181254a7Smrg 	SET_FLT128_MSW64(x,(hx&0x8000ffffffffffffULL)|(k<<48));
60*181254a7Smrg         return x*twom114;
61*181254a7Smrg }
62