From e9f45798d06983ef1dd3fffb60524111bb7f6335 Mon Sep 17 00:00:00 2001 From: Ell Date: Sun, 19 Nov 2017 07:07:20 -0500 Subject: [PATCH] libgimpmath: add SAFE_CLAMP() macro The SAFE_CLAMP() macro is similar to CLAMP(), however, its result is always within the specified range, even if the input is NaN. --- libgimpmath/gimpmath.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libgimpmath/gimpmath.h b/libgimpmath/gimpmath.h index b7f5d6a81a..c26acc3710 100644 --- a/libgimpmath/gimpmath.h +++ b/libgimpmath/gimpmath.h @@ -117,6 +117,20 @@ G_BEGIN_DECLS **/ #define CLAMP0255(a) CLAMP(a,0,255) +/** + * SAFE_CLAMP: + * @x: the value to be limited. + * @low: the lower limit. + * @high: the upper limit. + * + * Ensures that @x is between the limits set by @low and @high, + * even if @x is NaN. If @low is greater than @high, or if either + * of them is NaN, the result is undefined. + * + * Since: 2.10 + **/ +#define SAFE_CLAMP(x, low, high) ((x) > (low) ? (x) < (high) ? (x) : (high) : (low)) + /** * gimp_deg_to_rad: * @angle: the angle to be converted.