cmp.h 1.3 KB

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