Skip to content

Commit

Permalink
Added fmodf function #1875
Browse files Browse the repository at this point in the history
  • Loading branch information
Adversing authored and lerno committed Jan 31, 2025
1 parent bda33ca commit f3afec6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
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;
}
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- Added channels for threads.
- New `std::core::test` module for unit testing machinery.
- New unit test default runner.
- Added weakly linked `fmodf`.

## 0.6.6 Change list

Expand Down

0 comments on commit f3afec6

Please sign in to comment.