From d216621d2ad6a3b3de0bf57db2254affaa521cd0 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Mon, 24 Jul 2023 02:23:10 +0200 Subject: [PATCH] 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. --- AK/Math.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/AK/Math.h b/AK/Math.h index 4acf9c041d..6719a4b252 100644 --- a/AK/Math.h +++ b/AK/Math.h @@ -906,6 +906,17 @@ constexpr T round(T x) return ceil(x - .5); } +template +constexpr int clamp_to_int(T value) +{ + if (value >= NumericLimits::max()) { + return NumericLimits::max(); + } else if (value <= NumericLimits::min()) { + return NumericLimits::min(); + } + return value; +} + #undef CONSTEXPR_STATE #undef AARCH64_INSTRUCTION }