xref: /freebsd/lib/msun/src/s_scalbnf.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 <stdint.h>
113a8617a8SJordan K. Hubbard 
scalbnf(float x,int n)12760b2ffcSAlex Richardson float scalbnf(float x, int n)
133a8617a8SJordan K. Hubbard {
14760b2ffcSAlex Richardson 	union {float f; uint32_t i;} u;
15760b2ffcSAlex Richardson 	float_t y = x;
16760b2ffcSAlex Richardson 
17760b2ffcSAlex Richardson 	if (n > 127) {
18760b2ffcSAlex Richardson 		y *= 0x1p127f;
19760b2ffcSAlex Richardson 		n -= 127;
20760b2ffcSAlex Richardson 		if (n > 127) {
21760b2ffcSAlex Richardson 			y *= 0x1p127f;
22760b2ffcSAlex Richardson 			n -= 127;
23760b2ffcSAlex Richardson 			if (n > 127)
24760b2ffcSAlex Richardson 				n = 127;
253a8617a8SJordan K. Hubbard 		}
26760b2ffcSAlex Richardson 	} else if (n < -126) {
27760b2ffcSAlex Richardson 		y *= 0x1p-126f * 0x1p24f;
28760b2ffcSAlex Richardson 		n += 126 - 24;
29760b2ffcSAlex Richardson 		if (n < -126) {
30760b2ffcSAlex Richardson 			y *= 0x1p-126f * 0x1p24f;
31760b2ffcSAlex Richardson 			n += 126 - 24;
32760b2ffcSAlex Richardson 			if (n < -126)
33760b2ffcSAlex Richardson 				n = -126;
34dcdfa506SEd Maste 		}
35760b2ffcSAlex Richardson 	}
36760b2ffcSAlex Richardson 	u.i = (uint32_t)(0x7f+n)<<23;
37760b2ffcSAlex Richardson 	x = y * u.f;
38760b2ffcSAlex Richardson 	return x;
393a8617a8SJordan K. Hubbard }
404b201130SDavid Schultz 
414b201130SDavid Schultz __strong_reference(scalbnf, ldexpf);
42