common_defines.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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
  61. #ifndef FURI_CRITICAL_ENTER
  62. #define FURI_CRITICAL_ENTER() \
  63. uint32_t primask_bit = __get_PRIMASK(); \
  64. __disable_irq()
  65. #endif
  66. #ifndef FURI_CRITICAL_EXIT
  67. #define FURI_CRITICAL_EXIT() __set_PRIMASK(primask_bit)
  68. #endif