blob: a5ad208b69929f24336fae40e6af37101c99a72f [file] [log] [blame]
Szabolcs Nagy2eaed462013-09-04 15:52:54 +00001#include <math.h>
2#include <stdint.h>
Rich Felkerb69f6952012-03-13 01:17:53 -04003
Rich Felkerb69f6952012-03-13 01:17:53 -04004float scalbnf(float x, int n)
5{
Szabolcs Nagy2eaed462013-09-04 15:52:54 +00006 union {float f; uint32_t i;} u;
7 float_t y = x;
nsz8051e082012-03-19 10:54:07 +01008
9 if (n > 127) {
Szabolcs Nagy2eaed462013-09-04 15:52:54 +000010 y *= 0x1p127f;
nsz8051e082012-03-19 10:54:07 +010011 n -= 127;
12 if (n > 127) {
Szabolcs Nagy2eaed462013-09-04 15:52:54 +000013 y *= 0x1p127f;
nsz8051e082012-03-19 10:54:07 +010014 n -= 127;
Szabolcs Nagy1b77b902013-08-15 10:07:46 +000015 if (n > 127)
16 n = 127;
nsz8051e082012-03-19 10:54:07 +010017 }
18 } else if (n < -126) {
Szabolcs Nagy8c44a062017-04-03 02:38:13 +020019 y *= 0x1p-126f * 0x1p24f;
20 n += 126 - 24;
nsz8051e082012-03-19 10:54:07 +010021 if (n < -126) {
Szabolcs Nagy8c44a062017-04-03 02:38:13 +020022 y *= 0x1p-126f * 0x1p24f;
23 n += 126 - 24;
Szabolcs Nagy1b77b902013-08-15 10:07:46 +000024 if (n < -126)
25 n = -126;
nsz8051e082012-03-19 10:54:07 +010026 }
Rich Felkerb69f6952012-03-13 01:17:53 -040027 }
Szabolcs Nagy2eaed462013-09-04 15:52:54 +000028 u.i = (uint32_t)(0x7f+n)<<23;
29 x = y * u.f;
Szabolcs Nagyc4359e02012-11-13 10:55:35 +010030 return x;
Rich Felkerb69f6952012-03-13 01:17:53 -040031}