mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
* C3_MATH feature This feature allows the usage of noclib math files even when libc is in use. If a nolibc symbol exists, it will be used in place of libc, otherwise it will default to libc. * Added MIT License notices to atan.c3
78 lines
1.3 KiB
Plaintext
78 lines
1.3 KiB
Plaintext
module std::math::nolibc @if(env::NO_LIBC || $feature(C3_MATH));
|
|
|
|
union DoubleInternal
|
|
{
|
|
double f;
|
|
ulong i;
|
|
}
|
|
|
|
// Based on the musl implementation
|
|
fn double fmod(double x, double y) @extern("fmod") @weak @nostrip
|
|
{
|
|
DoubleInternal ux = { .f = x };
|
|
DoubleInternal uy = { .f = y };
|
|
int ex = (int)((ux.i >> 52) & 0x7ff);
|
|
int ey = (int)((uy.i >> 52) & 0x7ff);
|
|
int sx = (int)(ux.i >> 63);
|
|
ulong uxi = ux.i;
|
|
if (uy.i << 1 == 0 || math::is_nan(y) || ex == 0x7ff) return (x * y)/(x * y);
|
|
if (uxi << 1 <= uy.i << 1)
|
|
{
|
|
if (uxi << 1 == uy.i << 1) return 0 * x;
|
|
return x;
|
|
}
|
|
|
|
if (!ex)
|
|
{
|
|
for (ulong i = uxi << 12; i >> 63 == 0; ex--, i <<= 1);
|
|
uxi <<= -ex + 1;
|
|
}
|
|
else
|
|
{
|
|
uxi &= -1UL >> 12;
|
|
uxi |= 1UL << 52;
|
|
}
|
|
if (!ey)
|
|
{
|
|
for (ulong i = uy.i << 12; i >> 63 == 0; ey--, i <<= 1);
|
|
uy.i <<= -ey + 1;
|
|
}
|
|
else
|
|
{
|
|
uy.i &= -1UL >> 12;
|
|
uy.i |= 1UL << 52;
|
|
}
|
|
|
|
/* x mod y */
|
|
for (; ex > ey; ex--)
|
|
{
|
|
ulong i = uxi - uy.i;
|
|
if (i >> 63 == 0)
|
|
{
|
|
if (i == 0) return 0 * x;
|
|
uxi = i;
|
|
}
|
|
uxi <<= 1;
|
|
}
|
|
ulong i = uxi - uy.i;
|
|
if (i >> 63 == 0)
|
|
{
|
|
if (i == 0) return 0*x;
|
|
uxi = i;
|
|
}
|
|
for (; uxi>>52 == 0; uxi <<= 1, ex--);
|
|
|
|
/* scale result */
|
|
if (ex > 0)
|
|
{
|
|
uxi -= 1UL << 52;
|
|
uxi |= (ulong)ex << 52;
|
|
}
|
|
else
|
|
{
|
|
uxi >>= -ex + 1;
|
|
}
|
|
uxi |= (ulong)sx << 63;
|
|
ux.i = uxi;
|
|
return ux.f;
|
|
} |