core_defines.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. #define FURI_RETURNS_NONNULL __attribute__((returns_nonnull))
  6. #ifndef MAX
  7. #define MAX(a, b) \
  8. ({ \
  9. __typeof__(a) _a = (a); \
  10. __typeof__(b) _b = (b); \
  11. _a > _b ? _a : _b; \
  12. })
  13. #endif
  14. #ifndef MIN
  15. #define MIN(a, b) \
  16. ({ \
  17. __typeof__(a) _a = (a); \
  18. __typeof__(b) _b = (b); \
  19. _a < _b ? _a : _b; \
  20. })
  21. #endif
  22. #ifndef ABS
  23. #define ABS(a) ({ (a) < 0 ? -(a) : (a); })
  24. #endif
  25. #ifndef ROUND_UP_TO
  26. #define ROUND_UP_TO(a, b) \
  27. ({ \
  28. __typeof__(a) _a = (a); \
  29. __typeof__(b) _b = (b); \
  30. _a / _b + !!(_a % _b); \
  31. })
  32. #endif
  33. #ifndef CLAMP
  34. #define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
  35. #endif
  36. #ifndef COUNT_OF
  37. #define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
  38. #endif
  39. #ifndef FURI_SWAP
  40. #define FURI_SWAP(x, y) \
  41. do { \
  42. typeof(x) SWAP = x; \
  43. x = y; \
  44. y = SWAP; \
  45. } while(0)
  46. #endif
  47. #ifndef PLACE_IN_SECTION
  48. #define PLACE_IN_SECTION(x) __attribute__((section(x)))
  49. #endif
  50. #ifndef ALIGN
  51. #define ALIGN(n) __attribute__((aligned(n)))
  52. #endif
  53. #ifndef __weak
  54. #define __weak __attribute__((weak))
  55. #endif
  56. #ifndef UNUSED
  57. #define UNUSED(X) (void)(X)
  58. #endif
  59. #ifndef STRINGIFY
  60. #define STRINGIFY(x) #x
  61. #endif
  62. #ifndef TOSTRING
  63. #define TOSTRING(x) STRINGIFY(x)
  64. #endif
  65. #ifndef REVERSE_BYTES_U32
  66. #define REVERSE_BYTES_U32(x) \
  67. ((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
  68. (((x)&0xFF000000) >> 24))
  69. #endif
  70. #ifndef FURI_BIT
  71. #define FURI_BIT(x, n) (((x) >> (n)) & 1)
  72. #endif
  73. #ifndef FURI_BIT_SET
  74. #define FURI_BIT_SET(x, n) \
  75. ({ \
  76. __typeof__(x) _x = (1); \
  77. (x) |= (_x << (n)); \
  78. })
  79. #endif
  80. #ifndef FURI_BIT_CLEAR
  81. #define FURI_BIT_CLEAR(x, n) \
  82. ({ \
  83. __typeof__(x) _x = (1); \
  84. (x) &= ~(_x << (n)); \
  85. })
  86. #endif
  87. #define FURI_SW_MEMBARRIER() asm volatile("" : : : "memory")
  88. #ifdef __cplusplus
  89. }
  90. #endif