math: fix scalbn and scalbnf on overflow/underflow
[musl] / src / math / scalbnf.c
1 #include "libm.h"
2
3 float scalbnf(float x, int n)
4 {
5         /* make sure result is stored as double on overflow or underflow */
6         volatile float z;
7         float scale;
8
9         if (n > 127) {
10                 x *= 0x1p127f;
11                 n -= 127;
12                 if (n > 127) {
13                         x *= 0x1p127f;
14                         n -= 127;
15                         if (n > 127) {
16                                 z = x * 0x1p127f;
17                                 return z;
18                         }
19                 }
20         } else if (n < -126) {
21                 x *= 0x1p-126f;
22                 n += 126;
23                 if (n < -126) {
24                         x *= 0x1p-126f;
25                         n += 126;
26                         if (n < -126) {
27                                 z = x * 0x1p-126f;
28                                 return z;
29                         }
30                 }
31         }
32         SET_FLOAT_WORD(scale, (uint32_t)(0x7f+n)<<23);
33         z = x * scale;
34         return z;
35 }