core_defines.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 ROUND_UP_TO
  23. #define ROUND_UP_TO(a, b) \
  24. ({ \
  25. __typeof__(a) _a = (a); \
  26. __typeof__(b) _b = (b); \
  27. _a / _b + !!(_a % _b); \
  28. })
  29. #endif
  30. #ifndef CLAMP
  31. #define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
  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 __weak
  51. #define __weak __attribute__((weak))
  52. #endif
  53. #ifndef UNUSED
  54. #define UNUSED(X) (void)(X)
  55. #endif
  56. #ifndef STRINGIFY
  57. #define STRINGIFY(x) #x
  58. #endif
  59. #ifndef TOSTRING
  60. #define TOSTRING(x) STRINGIFY(x)
  61. #endif
  62. #ifndef REVERSE_BYTES_U32
  63. #define REVERSE_BYTES_U32(x) \
  64. ((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
  65. (((x)&0xFF000000) >> 24))
  66. #endif
  67. #ifndef FURI_BIT
  68. #define FURI_BIT(x, n) (((x) >> (n)) & 1)
  69. #endif
  70. #ifndef FURI_BIT_SET
  71. #define FURI_BIT_SET(x, n) \
  72. ({ \
  73. __typeof__(x) _x = (1); \
  74. (x) |= (_x << (n)); \
  75. })
  76. #endif
  77. #ifndef FURI_BIT_CLEAR
  78. #define FURI_BIT_CLEAR(x, n) ((x) &= ~(1 << (n)))
  79. #endif
  80. #define FURI_SW_MEMBARRIER() asm volatile("" : : : "memory")
  81. #ifdef __cplusplus
  82. }
  83. #endif