Skip to content

[libc][math] Ensure fputil::nextafter is called with correct type args #75009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions libc/src/__support/FPUtil/ManipulationFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ LIBC_INLINE T ldexp(T x, int exp) {
return normal;
}

template <
typename T, typename U,
cpp::enable_if_t<cpp::is_floating_point_v<T> && cpp::is_floating_point_v<U>,
int> = 0>
template <typename T, typename U,
cpp::enable_if_t<cpp::is_floating_point_v<T> &&
cpp::is_floating_point_v<U> &&
(sizeof(T) <= sizeof(U)),
int> = 0>
LIBC_INLINE T nextafter(T from, U to) {
FPBits<T> from_bits(from);
if (from_bits.is_nan())
Expand All @@ -157,6 +158,9 @@ LIBC_INLINE T nextafter(T from, U to) {
if (to_bits.is_nan())
return static_cast<T>(to);

// NOTE: This would work only if `U` has a greater or equal precision than
// `T`. Otherwise `from` could loose its precision and the following statement
// could incorrectly evaluate to `true`.
if (static_cast<U>(from) == to)
return static_cast<T>(to);

Expand Down