common_defines.h 691 B

123456789101112131415161718192021222324252627282930313233
  1. #pragma once
  2. #ifndef MAX
  3. #define MAX(a, b) \
  4. ({ \
  5. __typeof__(a) _a = (a); \
  6. __typeof__(b) _b = (b); \
  7. _a > _b ? _a : _b; \
  8. })
  9. #endif
  10. #ifndef MIN
  11. #define MIN(a, b) \
  12. ({ \
  13. __typeof__(a) _a = (a); \
  14. __typeof__(b) _b = (b); \
  15. _a < _b ? _a : _b; \
  16. })
  17. #endif
  18. #ifndef CLAMP
  19. #define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
  20. #endif
  21. // need some common semantics for those two
  22. #ifndef SIZEOF_ARRAY
  23. #define SIZEOF_ARRAY(arr) (sizeof(arr) / sizeof(arr[0]))
  24. #endif
  25. #ifndef COUNT_OF
  26. #define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
  27. #endif