Szabolcs Nagy | 2eaed46 | 2013-09-04 15:52:54 +0000 | [diff] [blame] | 1 | #include <math.h> |
2 | #include <stdint.h> | ||||
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 3 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 4 | float scalbnf(float x, int n) |
5 | { | ||||
Szabolcs Nagy | 2eaed46 | 2013-09-04 15:52:54 +0000 | [diff] [blame] | 6 | union {float f; uint32_t i;} u; |
7 | float_t y = x; | ||||
nsz | 8051e08 | 2012-03-19 10:54:07 +0100 | [diff] [blame] | 8 | |
9 | if (n > 127) { | ||||
Szabolcs Nagy | 2eaed46 | 2013-09-04 15:52:54 +0000 | [diff] [blame] | 10 | y *= 0x1p127f; |
nsz | 8051e08 | 2012-03-19 10:54:07 +0100 | [diff] [blame] | 11 | n -= 127; |
12 | if (n > 127) { | ||||
Szabolcs Nagy | 2eaed46 | 2013-09-04 15:52:54 +0000 | [diff] [blame] | 13 | y *= 0x1p127f; |
nsz | 8051e08 | 2012-03-19 10:54:07 +0100 | [diff] [blame] | 14 | n -= 127; |
Szabolcs Nagy | 1b77b90 | 2013-08-15 10:07:46 +0000 | [diff] [blame] | 15 | if (n > 127) |
16 | n = 127; | ||||
nsz | 8051e08 | 2012-03-19 10:54:07 +0100 | [diff] [blame] | 17 | } |
18 | } else if (n < -126) { | ||||
Szabolcs Nagy | 8c44a06 | 2017-04-03 02:38:13 +0200 | [diff] [blame] | 19 | y *= 0x1p-126f * 0x1p24f; |
20 | n += 126 - 24; | ||||
nsz | 8051e08 | 2012-03-19 10:54:07 +0100 | [diff] [blame] | 21 | if (n < -126) { |
Szabolcs Nagy | 8c44a06 | 2017-04-03 02:38:13 +0200 | [diff] [blame] | 22 | y *= 0x1p-126f * 0x1p24f; |
23 | n += 126 - 24; | ||||
Szabolcs Nagy | 1b77b90 | 2013-08-15 10:07:46 +0000 | [diff] [blame] | 24 | if (n < -126) |
25 | n = -126; | ||||
nsz | 8051e08 | 2012-03-19 10:54:07 +0100 | [diff] [blame] | 26 | } |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 27 | } |
Szabolcs Nagy | 2eaed46 | 2013-09-04 15:52:54 +0000 | [diff] [blame] | 28 | u.i = (uint32_t)(0x7f+n)<<23; |
29 | x = y * u.f; | ||||
Szabolcs Nagy | c4359e0 | 2012-11-13 10:55:35 +0100 | [diff] [blame] | 30 | return x; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 31 | } |