common_defines.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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
  28. #ifndef FURI_SWAP
  29. #define FURI_SWAP(x, y) \
  30. do { \
  31. typeof(x) SWAP = x; \
  32. x = y; \
  33. y = SWAP; \
  34. } while(0)
  35. #endif
  36. #ifndef PLACE_IN_SECTION
  37. #define PLACE_IN_SECTION(x) __attribute__((section(x)))
  38. #endif
  39. #ifndef ALIGN
  40. #define ALIGN(n) __attribute__((aligned(n)))
  41. #endif