common_defines.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ROUND_UP_TO
  19. #define ROUND_UP_TO(a, b) \
  20. ({ \
  21. __typeof__(a) _a = (a); \
  22. __typeof__(b) _b = (b); \
  23. _a / _b + !!(_a % _b); \
  24. })
  25. #endif
  26. #ifndef CLAMP
  27. #define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
  28. #endif
  29. // need some common semantics for those two
  30. #ifndef SIZEOF_ARRAY
  31. #define SIZEOF_ARRAY(arr) (sizeof(arr) / sizeof(arr[0]))
  32. #endif
  33. #ifndef COUNT_OF
  34. #define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
  35. #endif
  36. #ifndef FURI_SWAP
  37. #define FURI_SWAP(x, y) \
  38. do { \
  39. typeof(x) SWAP = x; \
  40. x = y; \
  41. y = SWAP; \
  42. } while(0)
  43. #endif
  44. #ifndef PLACE_IN_SECTION
  45. #define PLACE_IN_SECTION(x) __attribute__((section(x)))
  46. #endif
  47. #ifndef ALIGN
  48. #define ALIGN(n) __attribute__((aligned(n)))
  49. #endif
  50. #ifndef STRINGIFY
  51. #define STRINGIFY(x) #x
  52. #endif
  53. #ifndef TOSTRING
  54. #define TOSTRING(x) STRINGIFY(x)
  55. #endif
  56. #ifndef REVERSE_BYTES_U32
  57. #define REVERSE_BYTES_U32(x) \
  58. ((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
  59. (((x)&0xFF000000) >> 24))
  60. #endif