mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK+LibWeb: Make clamp_to_int generic over all integrals
This commit is contained in:
parent
cf7b13c708
commit
da3cfd5bbc
Notes:
sideshowbarker
2024-07-16 20:44:03 +09:00
Author: https://github.com/timmot Commit: https://github.com/SerenityOS/serenity/commit/da3cfd5bbc Pull-request: https://github.com/SerenityOS/serenity/pull/21980 Reviewed-by: https://github.com/MacDue ✅
2 changed files with 12 additions and 12 deletions
14
AK/Math.h
14
AK/Math.h
|
@ -1026,17 +1026,17 @@ constexpr T pow(T x, T y)
|
||||||
return exp2<T>(y * log2<T>(x));
|
return exp2<T>(y * log2<T>(x));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<Integral I, typename T>
|
||||||
constexpr int clamp_to_int(T value)
|
constexpr I clamp_to(T value)
|
||||||
{
|
{
|
||||||
if (value >= static_cast<T>(NumericLimits<int>::max()))
|
if (value >= static_cast<T>(NumericLimits<I>::max()))
|
||||||
return NumericLimits<int>::max();
|
return NumericLimits<I>::max();
|
||||||
|
|
||||||
if (value <= static_cast<T>(NumericLimits<int>::min()))
|
if (value <= static_cast<T>(NumericLimits<I>::min()))
|
||||||
return NumericLimits<int>::min();
|
return NumericLimits<I>::min();
|
||||||
|
|
||||||
if constexpr (IsFloatingPoint<T>)
|
if constexpr (IsFloatingPoint<T>)
|
||||||
return round_to<int>(value);
|
return round_to<I>(value);
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ public:
|
||||||
{
|
{
|
||||||
i32 raw_value = 0;
|
i32 raw_value = 0;
|
||||||
if (!isnan(value))
|
if (!isnan(value))
|
||||||
raw_value = AK::clamp_to_int(value * fixed_point_denominator);
|
raw_value = AK::clamp_to<int>(value * fixed_point_denominator);
|
||||||
// Note: The resolution of CSSPixels is 0.015625, so care must be taken when converting
|
// Note: The resolution of CSSPixels is 0.015625, so care must be taken when converting
|
||||||
// floats/doubles to CSSPixels as small values (such as scale factors) can underflow to zero,
|
// floats/doubles to CSSPixels as small values (such as scale factors) can underflow to zero,
|
||||||
// or otherwise produce inaccurate results (when scaled back up).
|
// or otherwise produce inaccurate results (when scaled back up).
|
||||||
|
@ -181,7 +181,7 @@ public:
|
||||||
i64 value = raw_value();
|
i64 value = raw_value();
|
||||||
value *= other.raw_value();
|
value *= other.raw_value();
|
||||||
|
|
||||||
int int_value = AK::clamp_to_int(value >> fractional_bits);
|
int int_value = AK::clamp_to<int>(value >> fractional_bits);
|
||||||
|
|
||||||
// Rounding:
|
// Rounding:
|
||||||
// If last bit cut off was 1:
|
// If last bit cut off was 1:
|
||||||
|
@ -321,7 +321,7 @@ public:
|
||||||
i64 wide_value = m_numerator.raw_value();
|
i64 wide_value = m_numerator.raw_value();
|
||||||
wide_value <<= CSSPixels::fractional_bits;
|
wide_value <<= CSSPixels::fractional_bits;
|
||||||
wide_value = rounding_divide<i64>(wide_value, m_denominator.raw_value());
|
wide_value = rounding_divide<i64>(wide_value, m_denominator.raw_value());
|
||||||
return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
|
return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr CSSPixels operator-(CSSPixels const& other) const
|
constexpr CSSPixels operator-(CSSPixels const& other) const
|
||||||
|
@ -373,7 +373,7 @@ constexpr CSSPixels CSSPixels::operator*(CSSPixelFraction const& other) const
|
||||||
i64 wide_value = raw_value();
|
i64 wide_value = raw_value();
|
||||||
wide_value *= other.numerator().raw_value();
|
wide_value *= other.numerator().raw_value();
|
||||||
wide_value = rounding_divide<i64>(wide_value, other.denominator().raw_value());
|
wide_value = rounding_divide<i64>(wide_value, other.denominator().raw_value());
|
||||||
return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
|
return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr CSSPixelFraction CSSPixels::operator/(CSSPixels const& other) const
|
constexpr CSSPixelFraction CSSPixels::operator/(CSSPixels const& other) const
|
||||||
|
@ -385,7 +385,7 @@ constexpr CSSPixels CSSPixels::operator/(CSSPixelFraction const& other) const
|
||||||
i64 wide_value = raw_value();
|
i64 wide_value = raw_value();
|
||||||
wide_value *= other.denominator().raw_value();
|
wide_value *= other.denominator().raw_value();
|
||||||
wide_value = rounding_divide<i64>(wide_value, other.numerator().raw_value());
|
wide_value = rounding_divide<i64>(wide_value, other.numerator().raw_value());
|
||||||
return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
|
return CSSPixels::from_raw(AK::clamp_to<int>(wide_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Integral T>
|
template<Integral T>
|
||||||
|
|
Loading…
Reference in a new issue