xref: /freebsd/lib/msun/src/s_scalbnl.c (revision 3550a49f6814af38c21b0033ef8746953451dade)
1*3550a49fSWarner Losh /*
2*3550a49fSWarner Losh  * Copyright (c) 2005-2020 Rich Felker, et al.
3*3550a49fSWarner Losh  *
4*3550a49fSWarner Losh  * SPDX-License-Identifier: MIT
5*3550a49fSWarner Losh  *
6*3550a49fSWarner Losh  * Please see https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
7*3550a49fSWarner Losh  * for all contributors to musl.
8*3550a49fSWarner Losh  */
9760b2ffcSAlex Richardson #include <math.h>
10760b2ffcSAlex Richardson #include <float.h>
11760b2ffcSAlex Richardson #include "math_private.h"
12760b2ffcSAlex Richardson #include "fpmath.h"
13cd7d05b5SDavid Schultz /*
14cd7d05b5SDavid Schultz  * scalbnl (long double x, int n)
15cd7d05b5SDavid Schultz  * scalbnl(x,n) returns x* 2**n  computed by  exponent
16cd7d05b5SDavid Schultz  * manipulation rather than by actually performing an
17cd7d05b5SDavid Schultz  * exponentiation or a multiplication.
18cd7d05b5SDavid Schultz  */
19f5542795SAlex Richardson #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
scalbnl(long double x,int n)20760b2ffcSAlex Richardson long double scalbnl(long double x, int n)
21cd7d05b5SDavid Schultz {
22cd7d05b5SDavid Schultz 	union IEEEl2bits u;
23760b2ffcSAlex Richardson 
24760b2ffcSAlex Richardson 	if (n > 16383) {
25760b2ffcSAlex Richardson 		x *= 0x1p16383L;
26760b2ffcSAlex Richardson 		n -= 16383;
27760b2ffcSAlex Richardson 		if (n > 16383) {
28760b2ffcSAlex Richardson 			x *= 0x1p16383L;
29760b2ffcSAlex Richardson 			n -= 16383;
30760b2ffcSAlex Richardson 			if (n > 16383)
31760b2ffcSAlex Richardson 				n = 16383;
32cd7d05b5SDavid Schultz 		}
33760b2ffcSAlex Richardson 	} else if (n < -16382) {
34760b2ffcSAlex Richardson 		x *= 0x1p-16382L * 0x1p113L;
35760b2ffcSAlex Richardson 		n += 16382 - 113;
36760b2ffcSAlex Richardson 		if (n < -16382) {
37760b2ffcSAlex Richardson 			x *= 0x1p-16382L * 0x1p113L;
38760b2ffcSAlex Richardson 			n += 16382 - 113;
39760b2ffcSAlex Richardson 			if (n < -16382)
40760b2ffcSAlex Richardson 				n = -16382;
41dcdfa506SEd Maste 		}
42cd7d05b5SDavid Schultz 	}
43760b2ffcSAlex Richardson 	u.e = 1.0;
44760b2ffcSAlex Richardson 	u.xbits.expsign = 0x3fff + n;
45760b2ffcSAlex Richardson 	return x * u.e;
46760b2ffcSAlex Richardson }
47f5542795SAlex Richardson __strong_reference(scalbnl, ldexpl);
48760b2ffcSAlex Richardson #endif
49cd7d05b5SDavid Schultz 
50