aboutsummaryrefslogtreecommitdiff
path: root/src/commonlib
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonlib')
-rw-r--r--src/commonlib/include/commonlib/clamp.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/commonlib/include/commonlib/clamp.h b/src/commonlib/include/commonlib/clamp.h
new file mode 100644
index 0000000000..e01a107ed4
--- /dev/null
+++ b/src/commonlib/include/commonlib/clamp.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef COMMONLIB_CLAMP_H
+#define COMMONLIB_CLAMP_H
+
+#include <stdint.h>
+
+/*
+ * Clamp a value, so that it is between a lower and an upper bound.
+ */
+static inline u32 clamp_u32(const u32 min, const u32 val, const u32 max)
+{
+ if (val > max)
+ return max;
+
+ if (val < min)
+ return min;
+
+ return val;
+}
+
+#endif /* COMMONLIB_CLAMP_H */