[CRT:MATH] Import scalbn/scalbnf from musl

This commit is contained in:
Timo Kreuzer 2024-11-20 16:24:27 +02:00
parent 3f71ab80ad
commit 5c26ccdb29
4 changed files with 88 additions and 2 deletions

View file

@ -16,6 +16,8 @@ list(APPEND LIBCNTPR_MATH_SOURCE
math/labs.c
math/round.c
math/roundf.c
math/scalbn.c
math/scalbnf.c
math/sincos.c
)

43
sdk/lib/crt/math/scalbn.c Normal file
View file

@ -0,0 +1,43 @@
/*
* PROJECT: ReactOS CRT
* LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of scalbn.
* COPYRIGHT: Imported from musl libc
* https://git.musl-libc.org/cgit/musl/tree/src/math/scalbn.c
* blob: 182f561068fda7bc8321dfc7991df8b6d58e1b56
* See https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
*/
#include <math.h>
#include <stdint.h>
double scalbn(double x, int n)
{
union {double f; uint64_t i;} u;
double_t y = x;
if (n > 1023) {
y *= 0x1p1023;
n -= 1023;
if (n > 1023) {
y *= 0x1p1023;
n -= 1023;
if (n > 1023)
n = 1023;
}
} else if (n < -1022) {
/* make sure final n < -53 to avoid double
rounding in the subnormal range */
y *= 0x1p-1022 * 0x1p53;
n += 1022 - 53;
if (n < -1022) {
y *= 0x1p-1022 * 0x1p53;
n += 1022 - 53;
if (n < -1022)
n = -1022;
}
}
u.i = (uint64_t)(0x3ff+n)<<52;
x = y * u.f;
return x;
}

View file

@ -0,0 +1,41 @@
/*
* PROJECT: ReactOS CRT
* LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of scalbnf.
* COPYRIGHT: Imported from musl libc
* https://git.musl-libc.org/cgit/musl/tree/src/math/scalbnf.c
* blob: a5ad208b69929f24336fae40e6af37101c99a72f
* See https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
*/
#include <math.h>
#include <stdint.h>
float scalbnf(float x, int n)
{
union {float f; uint32_t i;} u;
float_t y = x;
if (n > 127) {
y *= 0x1p127f;
n -= 127;
if (n > 127) {
y *= 0x1p127f;
n -= 127;
if (n > 127)
n = 127;
}
} else if (n < -126) {
y *= 0x1p-126f * 0x1p24f;
n += 126 - 24;
if (n < -126) {
y *= 0x1p-126f * 0x1p24f;
n += 126 - 24;
if (n < -126)
n = -126;
}
}
u.i = (uint32_t)(0x7f+n)<<23;
x = y * u.f;
return x;
}