mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +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
Notes:
sideshowbarker
2024-07-17 06:54:15 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/07e4358c63 Pull-request: https://github.com/SerenityOS/serenity/pull/18998 Reviewed-by: https://github.com/kleinesfilmroellchen ✅ Reviewed-by: https://github.com/linusg ✅
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));
|
||||
} while (fpu_status & 0x400);
|
||||
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
|
||||
# if defined(AK_OS_SERENITY)
|
||||
// TODO: Add implementation for this function.
|
||||
TODO();
|
||||
# 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
|
||||
}
|
||||
|
||||
|
@ -420,7 +445,12 @@ constexpr T remainder(T x, T y)
|
|||
// TODO: Add implementation for this function.
|
||||
TODO();
|
||||
# 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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue