| 1 |
/* |
| 2 |
* Copyright (c) 2005-2020 Rich Felker, et al. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
* Please see https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT |
| 7 |
* for all contributors to musl. |
| 8 |
*/ |
| 9 |
#include <float.h> |
| 10 |
#include <stdint.h> |
| 11 |
|
| 12 |
#include "math_private.h" |
| 13 |
|
| 14 |
double scalbn(double x, int n) |
| 15 |
{ |
| 16 |
union {double f; uint64_t i;} u; |
| 17 |
double_t y = x; |
| 18 |
|
| 19 |
if (n > 1023) { |
| 20 |
y *= 0x1p1023; |
| 21 |
n -= 1023; |
| 22 |
if (n > 1023) { |
| 23 |
y *= 0x1p1023; |
| 24 |
n -= 1023; |
| 25 |
if (n > 1023) |
| 26 |
n = 1023; |
| 27 |
} |
| 28 |
} else if (n < -1022) { |
| 29 |
/* make sure final n < -53 to avoid double |
| 30 |
rounding in the subnormal range */ |
| 31 |
y *= 0x1p-1022 * 0x1p53; |
| 32 |
n += 1022 - 53; |
| 33 |
if (n < -1022) { |
| 34 |
y *= 0x1p-1022 * 0x1p53; |
| 35 |
n += 1022 - 53; |
| 36 |
if (n < -1022) |
| 37 |
n = -1022; |
| 38 |
} |
| 39 |
} |
| 40 |
u.i = (uint64_t)(0x3ff+n)<<52; |
| 41 |
x = y * u.f; |
| 42 |
return x; |
| 43 |
} |