Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fmodf symbol missing in WASM #1882

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/std/core/runtime.c3
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ fn void wasm_initialize() @extern("_initialize") @wasm
{
// The linker synthesizes this to call constructors.
__wasm_call_ctors();
}
}

extern fn float fmodf(float x, float y) @wasm;
78 changes: 77 additions & 1 deletion lib/std/math/math_nolibc/__fmod.c3
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ union DoubleInternal
ulong i;
}

union FloatInternal
{
float f;
uint i;
}

// Based on the musl implementation
fn double fmod(double x, double y) @extern("fmod") @weak @nostrip
{
Expand Down Expand Up @@ -75,4 +81,74 @@ fn double fmod(double x, double y) @extern("fmod") @weak @nostrip
uxi |= (ulong)sx << 63;
ux.i = uxi;
return ux.f;
}
}

fn float fmodf(float x, float y) @extern("fmodf") @weak @nostrip
{
FloatInternal ux = { .f = x };
FloatInternal uy = { .f = y };
int ex = (int)((ux.i >> 23) & 0xff);
int ey = (int)((uy.i >> 23) & 0xff);
int sx = (int)(ux.i >> 31);
uint uxi = ux.i;
if (uy.i << 1 == 0 || math::is_nan(y) || ex == 0xff) 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 (uint i = uxi << 9; i >> 31 == 0; ex--, i <<= 1);
uxi <<= -ex + 1;
}
else
{
uxi &= -1U >> 9;
uxi |= 1U << 23;
}
if (!ey)
{
for (uint i = uy.i << 9; i >> 31 == 0; ey--, i <<= 1);
uy.i <<= -ey + 1;
}
else
{
uy.i &= -1U >> 9;
uy.i |= 1U << 23;
}

/* x mod y */
for (; ex > ey; ex--)
{
uint i = uxi - uy.i;
if (i >> 31 == 0)
{
if (i == 0) return 0 * x;
uxi = i;
}
uxi <<= 1;
}
uint i = uxi - uy.i;
if (i >> 31 == 0)
{
if (i == 0) return 0*x;
uxi = i;
}
for (; uxi>>23 == 0; uxi <<= 1, ex--);

/* scale result */
if (ex > 0)
{
uxi -= 1U << 23;
uxi |= (uint)ex << 23;
}
else
{
uxi >>= -ex + 1;
}
uxi |= (uint)sx << 31;
ux.i = uxi;
return ux.f;
}
76 changes: 0 additions & 76 deletions src/compiler/abi/c_abi_wasm.c

This file was deleted.

Loading
Loading