mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
AK: Use correct builtins for fmod and remainder
Similar to floor and ceil, we were forcing the values to be doubles here Also adds a big FIXME about my findings trying to add a general implementation for these
This commit is contained in:
parent
c9808f0d4a
commit
07e4358c63
1 changed files with 32 additions and 2 deletions
34
AK/Math.h
34
AK/Math.h
|
@ -391,12 +391,37 @@ constexpr T fmod(T x, T y)
|
||||||
: "u"(y));
|
: "u"(y));
|
||||||
} while (fpu_status & 0x400);
|
} while (fpu_status & 0x400);
|
||||||
return x;
|
return x;
|
||||||
|
// FIXME: Add a generic implementation of this
|
||||||
|
// Neither
|
||||||
|
// ```
|
||||||
|
// return x - (y * trunc(x/y))
|
||||||
|
// ```
|
||||||
|
// nor
|
||||||
|
// ```
|
||||||
|
// double result = remainder(std::fabs(x), y = std::fabs(y));
|
||||||
|
// if (std::signbit(result))
|
||||||
|
// result += y;
|
||||||
|
// return std::copysign(result, x);
|
||||||
|
// ``` from (https://en.cppreference.com/w/cpp/numeric/math/fmod)
|
||||||
|
// provide enough precision for all cases
|
||||||
|
// other implementations seem to do this by hand with some fixed point steps in between
|
||||||
|
// For `remainder` the trivial solution of
|
||||||
|
// ```
|
||||||
|
// return x - (y * rint(x/y))
|
||||||
|
// ```
|
||||||
|
// might work
|
||||||
|
|
||||||
#else
|
#else
|
||||||
# if defined(AK_OS_SERENITY)
|
# if defined(AK_OS_SERENITY)
|
||||||
// TODO: Add implementation for this function.
|
// TODO: Add implementation for this function.
|
||||||
TODO();
|
TODO();
|
||||||
# endif
|
# endif
|
||||||
return __builtin_fmod(x, y);
|
if constexpr (IsSame<T, long double>)
|
||||||
|
return __builtin_fmodl(x, y);
|
||||||
|
if constexpr (IsSame<T, double>)
|
||||||
|
return __builtin_fmod(x, y);
|
||||||
|
if constexpr (IsSame<T, float>)
|
||||||
|
return __builtin_fmodf(x, y);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,7 +445,12 @@ constexpr T remainder(T x, T y)
|
||||||
// TODO: Add implementation for this function.
|
// TODO: Add implementation for this function.
|
||||||
TODO();
|
TODO();
|
||||||
# endif
|
# endif
|
||||||
return __builtin_fmod(x, y);
|
if constexpr (IsSame<T, long double>)
|
||||||
|
return __builtin_remainderl(x, y);
|
||||||
|
if constexpr (IsSame<T, double>)
|
||||||
|
return __builtin_remainder(x, y);
|
||||||
|
if constexpr (IsSame<T, float>)
|
||||||
|
return __builtin_remainderf(x, y);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue