mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Handle partial remainders
On x86, the `fprem` and `fmprem1` instructions may produce a 'partial remainder', for which we should check by reading a FPU flag. If we don't check for it, we may end up using values that are outside the expected range of values.
This commit is contained in:
parent
653d22e21f
commit
5d32f543ec
Notes:
sideshowbarker
2024-07-18 07:15:38 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/5d32f543ecb Pull-request: https://github.com/SerenityOS/serenity/pull/8718 Issue: https://github.com/SerenityOS/serenity/issues/363 Reviewed-by: https://github.com/gunnarbeutner ✅ Reviewed-by: https://github.com/nico
1 changed files with 18 additions and 12 deletions
30
AK/Math.h
30
AK/Math.h
|
@ -65,23 +65,29 @@ template<FloatingPoint T>
|
|||
constexpr T fmod(T x, T y)
|
||||
{
|
||||
CONSTEXPR_STATE(fmod, x, y);
|
||||
T res;
|
||||
asm(
|
||||
"fprem"
|
||||
: "=t"(res)
|
||||
: "0"(x), "u"(y));
|
||||
return res;
|
||||
u16 fpu_status;
|
||||
do {
|
||||
asm(
|
||||
"fprem\n"
|
||||
"fnstsw %%ax\n"
|
||||
: "+t"(x), "=a"(fpu_status)
|
||||
: "u"(y));
|
||||
} while (fpu_status & 0x400);
|
||||
return x;
|
||||
}
|
||||
template<FloatingPoint T>
|
||||
constexpr T remainder(T x, T y)
|
||||
{
|
||||
CONSTEXPR_STATE(remainder, x, y);
|
||||
T res;
|
||||
asm(
|
||||
"fprem1"
|
||||
: "=t"(res)
|
||||
: "0"(x), "u"(y));
|
||||
return res;
|
||||
u16 fpu_status;
|
||||
do {
|
||||
asm(
|
||||
"fprem1\n"
|
||||
"fnstsw %%ax\n"
|
||||
: "+t"(x), "=a"(fpu_status)
|
||||
: "u"(y));
|
||||
} while (fpu_status & 0x400);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue