summaryrefslogtreecommitdiff
path: root/src/commonlib/bsd
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonlib/bsd')
-rw-r--r--src/commonlib/bsd/include/commonlib/bsd/clamp.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/commonlib/bsd/include/commonlib/bsd/clamp.h b/src/commonlib/bsd/include/commonlib/bsd/clamp.h
new file mode 100644
index 0000000000..66513b0337
--- /dev/null
+++ b/src/commonlib/bsd/include/commonlib/bsd/clamp.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later */
+
+#ifndef COMMONLIB_BSD_CLAMP_H
+#define COMMONLIB_BSD_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_BSD_CLAMP_H */