mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:27:43 +00:00
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:
parent
c66dbc99ee
commit
d216621d2a
1 changed files with 11 additions and 0 deletions
11
AK/Math.h
11
AK/Math.h
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue