1 /* s_scalbnl.c -- long double version of s_scalbn.c.
4 /* @(#)s_scalbn.c 5.1 93/09/24 */
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
13 * ====================================================
16 #if defined(LIBM_SCCS) && !defined(lint)
17 static char rcsid
[] = "$NetBSD: $";
21 * scalbnl (long double x, int n)
22 * scalbnl(x,n) returns x* 2**n computed by exponent
23 * manipulation rather than by actually performing an
24 * exponentiation or a multiplication.
28 #include <math_private.h>
29 #include <math_ldbl_opt.h>
31 static const long double
32 twolm54
= 5.55111512312578270212e-17, /* 0x3C90000000000000, 0 */
36 two54
= 1.80143985094819840000e+16, /* 0x4350000000000000 */
37 twom54
= 5.55111512312578270212e-17; /* 0x3C90000000000000 */
39 long double __scalbnl (long double x
, int n
)
42 union { int64_t i
; double d
; } u
;
45 ldbl_unpack (x
, &xhi
, &xlo
);
46 EXTRACT_WORDS64 (hx
, xhi
);
47 EXTRACT_WORDS64 (lx
, xlo
);
48 k
= (hx
>>52)&0x7ff; /* extract exponent */
50 if (k
==0) { /* 0 or subnormal x */
51 if ((hx
&0x7fffffffffffffffULL
)==0) return x
; /* +-0 */
55 k
= ((hx
>>52)&0x7ff) - 54;
57 else if (k
==0x7ff) return x
+x
; /* NaN or Inf */
58 if (n
< -50000) return tiny
*copysignl(tiny
,x
); /*underflow */
59 if (n
> 50000 || k
+n
> 0x7fe)
60 return huge
*copysignl(huge
,x
); /* overflow */
61 /* Now k and n are bounded we know that k = k+n does not
64 if (k
> 0) { /* normal result */
65 hx
= (hx
&0x800fffffffffffffULL
)|(k
<<52);
66 if ((lx
& 0x7fffffffffffffffULL
) == 0) { /* low part +-0 */
67 INSERT_WORDS64 (xhi
, hx
);
68 INSERT_WORDS64 (xlo
, lx
);
69 x
= ldbl_pack (xhi
, xlo
);
72 if (l
== 0) { /* low part subnormal */
76 l
= ((lx
>>52)&0x7ff) - 54;
80 lx
= (lx
&0x800fffffffffffffULL
)|(l
<<52);
82 lx
= (lx
&0x8000000000000000ULL
);
85 u
.i
= (lx
&0x800fffffffffffffULL
)|(l
<<52);
89 INSERT_WORDS64 (xhi
, hx
);
90 INSERT_WORDS64 (xlo
, lx
);
91 x
= ldbl_pack (xhi
, xlo
);
95 return tiny
*copysignl(tiny
,x
); /*underflow*/
96 k
+= 54; /* subnormal result */
97 lx
&= 0x8000000000000000ULL
;
98 hx
&= 0x800fffffffffffffULL
;
99 INSERT_WORDS64 (xhi
, hx
|(k
<<52));
100 INSERT_WORDS64 (xlo
, lx
);
101 x
= ldbl_pack (xhi
, xlo
);