From c000a3f70b74752a307d533c3cd7432ce7ebff83 Mon Sep 17 00:00:00 2001 From: Qiang Zhang Date: Thu, 31 Aug 2023 13:58:37 +0800 Subject: [PATCH] hv: add clamp macro for convenience Add clamp macro to clamp a value within a range. Tracked-On: #8500 Reviewed-by: Junjie Mao Signed-off-by: Qiang Zhang --- hypervisor/include/lib/util.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hypervisor/include/lib/util.h b/hypervisor/include/lib/util.h index 176455a3e..c7e3604d2 100644 --- a/hypervisor/include/lib/util.h +++ b/hypervisor/include/lib/util.h @@ -22,6 +22,9 @@ #define max(x, y) (((x) < (y)) ? (y) : (x)) +/** return a value of v clamped to the range [l, h] */ +#define clamp(v, l, h) (max(min((v), (h)), (l))) + /** Replaces 'x' by the string "x". */ #define STRINGIFY(x) #x