mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:37:44 +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
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 T fmod(T x, T y)
|
||||||
{
|
{
|
||||||
CONSTEXPR_STATE(fmod, x, y);
|
CONSTEXPR_STATE(fmod, x, y);
|
||||||
T res;
|
u16 fpu_status;
|
||||||
asm(
|
do {
|
||||||
"fprem"
|
asm(
|
||||||
: "=t"(res)
|
"fprem\n"
|
||||||
: "0"(x), "u"(y));
|
"fnstsw %%ax\n"
|
||||||
return res;
|
: "+t"(x), "=a"(fpu_status)
|
||||||
|
: "u"(y));
|
||||||
|
} while (fpu_status & 0x400);
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
template<FloatingPoint T>
|
template<FloatingPoint T>
|
||||||
constexpr T remainder(T x, T y)
|
constexpr T remainder(T x, T y)
|
||||||
{
|
{
|
||||||
CONSTEXPR_STATE(remainder, x, y);
|
CONSTEXPR_STATE(remainder, x, y);
|
||||||
T res;
|
u16 fpu_status;
|
||||||
asm(
|
do {
|
||||||
"fprem1"
|
asm(
|
||||||
: "=t"(res)
|
"fprem1\n"
|
||||||
: "0"(x), "u"(y));
|
"fnstsw %%ax\n"
|
||||||
return res;
|
: "+t"(x), "=a"(fpu_status)
|
||||||
|
: "u"(y));
|
||||||
|
} while (fpu_status & 0x400);
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue