AK: Fix is_within_range when converting from float

Within range now uses the max capacity of a type rather than its size.
This fixes some subtests in
https://wpt.fyi/results/wasm/core/conversions.wast.js.html?product=ladybird
This commit is contained in:
me-it-is 2025-09-13 16:41:10 -07:00 committed by Sam Atkins
parent 193163be2f
commit b76f1fb011
Notes: github-actions[bot] 2025-09-24 09:41:40 +00:00
2 changed files with 19 additions and 2 deletions

View file

@ -34,7 +34,7 @@
namespace AK {
template<typename Destination, typename Source, bool destination_is_wider = (sizeof(Destination) >= sizeof(Source)), bool destination_is_signed = NumericLimits<Destination>::is_signed(), bool source_is_signed = NumericLimits<Source>::is_signed()>
template<typename Destination, typename Source, bool destination_is_wider = (NumericLimits<Destination>::max() >= NumericLimits<Source>::max()), bool destination_is_signed = NumericLimits<Destination>::is_signed(), bool source_is_signed = NumericLimits<Source>::is_signed()>
struct TypeBoundsChecker;
template<typename Destination, typename Source>
@ -98,7 +98,7 @@ template<typename Destination, typename Source>
struct TypeBoundsChecker<Destination, Source, true, true, false> {
static constexpr bool is_within_range(Source value)
{
if (sizeof(Destination) > sizeof(Source))
if (NumericLimits<Destination>::max() > NumericLimits<Source>::max())
return true;
return value <= static_cast<Source>(NumericLimits<Destination>::max());
}

View file

@ -445,3 +445,20 @@ TEST_CASE(should_constexpr_make_via_factory)
{
[[maybe_unused]] constexpr auto value = make_checked(42);
}
TEST_CASE(is_within_range_float_to_int)
{
static_assert(AK::is_within_range<int>(0.0));
static_assert(AK::is_within_range<int>(0.0f));
static_assert(AK::is_within_range<int>(((long double)0.0)));
static_assert(!AK::is_within_range<int>(NumericLimits<float>::max()));
static_assert(!AK::is_within_range<int>(NumericLimits<double>::max()));
static_assert(!AK::is_within_range<int>(NumericLimits<long double>::max()));
static_assert(!AK::is_within_range<int>(NumericLimits<float>::lowest()));
static_assert(AK::is_within_range<long>(0.0));
static_assert(AK::is_within_range<long long>(0.0));
static_assert(!AK::is_within_range<int>(NAN));
}