From e9c81477d7003697c7080e6d8dc5191ecd2f4788 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 25 Mar 2026 01:06:45 +0200 Subject: [PATCH] BAN/LibC: Implement remainder This is basically just fmod but with fprem1 instead of fprem --- BAN/include/BAN/Math.h | 18 +++++++++++++++++- userspace/libraries/LibC/math.cpp | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/BAN/include/BAN/Math.h b/BAN/include/BAN/Math.h index 2f5a0745..79383347 100644 --- a/BAN/include/BAN/Math.h +++ b/BAN/include/BAN/Math.h @@ -172,7 +172,23 @@ namespace BAN::Math "jne 1b;" : "+t"(a) : "u"(b) - : "ax" + : "ax", "cc" + ); + return a; + } + + template + inline constexpr T remainder(T a, T b) + { + asm( + "1:" + "fprem1;" + "fnstsw %%ax;" + "testb $4, %%ah;" + "jne 1b;" + : "+t"(a) + : "u"(b) + : "ax", "cc" ); return a; } diff --git a/userspace/libraries/LibC/math.cpp b/userspace/libraries/LibC/math.cpp index 8f1e335b..763cd03e 100644 --- a/userspace/libraries/LibC/math.cpp +++ b/userspace/libraries/LibC/math.cpp @@ -256,7 +256,7 @@ FUNC_EXPR1(nearbyint, BAN::Math::rint(a)) FUNC_EXPR2(nextafter, nextafter_impl(a, b)) FUNC_EXPR2_TYPE(nexttoward, long double, nextafter_impl(a, b)) FUNC_EXPR2(pow, ({ if (isnan(a)) return a; if (isnan(b)) return b; BAN::Math::pow(a, b); })) -// remainder +BAN_FUNC2(remainder) // remquo BAN_FUNC1(rint) FUNC_EXPR1(round, ({ if (!isfinite(a)) return a; BAN::Math::round(a); }))