Tor  0.4.7.0-alpha-dev
cmp.h
Go to the documentation of this file.
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 */
5 
6 /**
7  * \file cmp.h
8  *
9  * \brief Macro definitions for MIN, MAX, and CLAMP.
10  **/
11 
12 #ifndef TOR_INTMATH_CMP_H
13 #define TOR_INTMATH_CMP_H
14 
15 /** Macros for MIN/MAX. Never use these when the arguments could have
16  * side-effects.
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
19  * has it.}
20  **/
21 #ifndef MAX
22 #define MAX(a,b) ( ((a)<(b)) ? (b) : (a) )
23 #endif
24 #ifndef MIN
25 #define MIN(a,b) ( ((a)>(b)) ? (b) : (a) )
26 #endif
27 
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>.
31  *
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) : \
37  (v) )
38 
39 /** Give the absolute value of <b>x</b>, independent of its type. */
40 #define ABS(x) ( ((x)<0) ? -(x) : (x) )
41 
42 #endif /* !defined(TOR_INTMATH_CMP_H) */