1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
9 * \brief Macro definitions for MIN, MAX, and CLAMP.
12 #ifndef TOR_INTMATH_CMP_H
13 #define TOR_INTMATH_CMP_H
15 /** Macros for MIN/MAX. Never use these when the arguments could have
17 * {With GCC extensions we could probably define a safer MIN/MAX. But
18 * depending on that safety would be dangerous, since not every platform
22 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
25 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
28 /* Return <b>v</b> if it's between <b>min</b> and <b>max</b>. Otherwise
29 * return <b>min</b> if <b>v</b> is smaller than <b>min</b>, or <b>max</b> if
30 * <b>b</b> is larger than <b>max</b>.
32 * Requires that <b>min</b> is no more than <b>max</b>. May evaluate any of
33 * its arguments more than once! */
34 #define CLAMP(min,v,max) \
35 ( ((v) < (min)) ? (min) : \
36 ((v) > (max)) ? (max) : \
39 /** Give the absolute value of <b>x</b>, independent of its type. */
40 #define ABS(x) ( ((x)<0) ? -(x) : (x) )
42 #endif /* !defined(TOR_INTMATH_CMP_H) */