AK: Add clamp_to_int(value) in Math.h

clamp_to_int clamps value to valid range of int values so resulting
value does not overflow.

It is going to be used to clamp float or double values to int that
represents fixed-point value of CSSPixels.
This commit is contained in:
Aliaksandr Kalenik 2023-07-24 02:23:10 +02:00 committed by Andreas Kling
parent c66dbc99ee
commit d216621d2a
Notes: sideshowbarker 2024-07-17 09:48:50 +09:00

View file

@ -906,6 +906,17 @@ constexpr T round(T x)
return ceil(x - .5);
}
template<typename T>
constexpr int clamp_to_int(T value)
{
if (value >= NumericLimits<int>::max()) {
return NumericLimits<int>::max();
} else if (value <= NumericLimits<int>::min()) {
return NumericLimits<int>::min();
}
return value;
}
#undef CONSTEXPR_STATE
#undef AARCH64_INSTRUCTION
}