From 35938c15e22e3021f7693425f0d2134845c81f6b Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 14 May 2020 12:34:35 -0400 Subject: [PATCH] util: Add SATURATE macro Equivalent to clamp(x, 0.0, 1.0) or fsat in NIR. Useful for format packing, among other uses given the variety of substituions in-tree. v2: Drop brackets (Eric). Signed-off-by: Alyssa Rosenzweig Reviewed-by: Eric Engestrom Part-of: --- src/util/macros.h | 3 +++ src/util/u_math.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/util/macros.h b/src/util/macros.h index fcace4e351d..57beaf72fae 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -291,6 +291,9 @@ do { \ /** Clamp X to [MIN,MAX]. Turn NaN into MIN, arbitrarily. */ #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) ) +/* Syntax sugar occuring frequently in graphics code */ +#define SATURATE( X ) CLAMP(X, 0.0f, 1.0f) + /** Minimum of two values: */ #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) diff --git a/src/util/u_math.h b/src/util/u_math.h index 59266c16922..b725489b520 100644 --- a/src/util/u_math.h +++ b/src/util/u_math.h @@ -667,6 +667,9 @@ util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t */ #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) ) +/* Syntax sugar occuring frequently in graphics code */ +#define SATURATE( X ) CLAMP(X, 0.0f, 1.0f) + #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) -- 2.30.2