Skip to content
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

[libc++] Fix acceptance of convertible-to-{float,double,long double} in std::isfinite() #98841

Merged
merged 9 commits into from
Jul 18, 2024
12 changes: 12 additions & 0 deletions libcxx/include/__math/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ _LIBCPP_NODISCARD _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfin
return true;
}

_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(float __x) _NOEXCEPT {
return __builtin_isfinite(__x);
}

_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(double __x) _NOEXCEPT {
return __builtin_isfinite(__x);
}

_LIBCPP_NODISCARD inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool isfinite(long double __x) _NOEXCEPT {
return __builtin_isfinite(__x);
}

// isinf

template <class _A1, __enable_if_t<is_arithmetic<_A1>::value && numeric_limits<_A1>::has_infinity, int> = 0>
Expand Down
21 changes: 21 additions & 0 deletions libcxx/test/std/numerics/c.math/isfinite.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,30 @@ struct TestInt {
}
};

struct ConvertibleFloat {
int value;
ConvertibleFloat(int v) : value(v) {}
operator float() const { return static_cast<float>(value); }
};

struct ConvertibleDouble {
int value;
ConvertibleDouble(int v) : value(v) {}
operator double() const { return static_cast<double>(value); }
};

struct ConvertibleLongDouble {
int value;
ConvertibleLongDouble(int v) : value(v) {}
operator long double() const { return static_cast<long double>(value); }
};

robincaloudis marked this conversation as resolved.
Show resolved Hide resolved
int main(int, char**) {
types::for_each(types::floating_point_types(), TestFloat());
types::for_each(types::integral_types(), TestInt());
assert(std::isfinite(ConvertibleFloat(0)));
assert(std::isfinite(ConvertibleDouble(0)));
assert(std::isfinite(ConvertibleLongDouble(0)));
robincaloudis marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}
Loading