misc.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #ifndef __MISC_H__
  11. #define __MISC_H__
  12. #include <rtdef.h>
  13. #include <cpuport.h>
  14. #ifdef ARCH_CPU_64BIT
  15. #define RT_BITS_PER_LONG 64
  16. #else
  17. #define RT_BITS_PER_LONG 32
  18. #endif
  19. #define RT_BITS_PER_LONG_LONG 64
  20. #define RT_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  21. #define RT_DIV_ROUND_CLOSEST(x, divisor) \
  22. ({ \
  23. typeof(x) __x = x; \
  24. typeof(divisor) __d = divisor; \
  25. (((typeof(x))-1) > 0 || \
  26. ((typeof(divisor))-1) > 0 || \
  27. (((__x) > 0) == ((__d) > 0))) ? \
  28. (((__x) + ((__d) / 2)) / (__d)) : \
  29. (((__x) - ((__d) / 2)) / (__d)); \
  30. })
  31. #define __KEY_PLACEHOLDER_1 0,
  32. #define ____KEY_ENABLED(__ignored, val, ...) val
  33. #define ___KEY_ENABLED(arg1_or_junk) ____KEY_ENABLED(arg1_or_junk 1, 0)
  34. #define __KEY_ENABLED(value) ___KEY_ENABLED(__KEY_PLACEHOLDER_##value)
  35. #define RT_KEY_ENABLED(key) __KEY_ENABLED(key)
  36. #define RT_FIELD_PREP(mask, val) (((rt_uint64_t)(val) << (__rt_ffsl((mask)) - 1)) & (mask))
  37. #define RT_FIELD_GET(mask, val) (((val) & (mask)) >> (__rt_ffsl((mask)) - 1))
  38. #define RT_BIT(n) (1UL << (n))
  39. #define RT_BIT_ULL(n) (1ULL << (n))
  40. #define RT_BIT_MASK(nr) (1UL << ((nr) % RT_BITS_PER_LONG))
  41. #define RT_BIT_WORD(nr) ((nr) / RT_BITS_PER_LONG)
  42. #define RT_BITS_PER_BYTE 8
  43. #define RT_BITS_PER_TYPE(type) (sizeof(type) * RT_BITS_PER_BYTE)
  44. #define RT_BITS_TO_BYTES(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(char))
  45. #define RT_BITS_TO_LONGS(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(long))
  46. #define RT_GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (RT_BITS_PER_LONG - 1 - (h))))
  47. #define RT_GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (RT_BITS_PER_LONG_LONG - 1 - (h))))
  48. #define RT_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  49. #define rt_offsetof(s, field) ((rt_size_t)&((s *)0)->field)
  50. #define rt_err_ptr(err) ((void *)(rt_base_t)(err))
  51. #define rt_ptr_err(ptr) ((rt_err_t)(rt_base_t)(ptr))
  52. #define rt_is_err_value(ptr) ((rt_ubase_t)(void *)(ptr) >= (rt_ubase_t)-4095)
  53. #define rt_is_err(ptr) rt_is_err_value(ptr)
  54. #define rt_is_err_or_null(ptr) (!(ptr) || rt_is_err_value((rt_ubase_t)(ptr)))
  55. #define rt_upper_32_bits(n) ((rt_uint32_t)(((n) >> 16) >> 16))
  56. #define rt_lower_32_bits(n) ((rt_uint32_t)((n) & 0xffffffff))
  57. #define rt_upper_16_bits(n) ((rt_uint16_t)((n) >> 16))
  58. #define rt_lower_16_bits(n) ((rt_uint16_t)((n) & 0xffff))
  59. #define rt_min(x, y) \
  60. ({ \
  61. typeof(x) _x = (x); \
  62. typeof(y) _y = (y); \
  63. (void) (&_x == &_y); \
  64. _x < _y ? _x : _y; \
  65. })
  66. #define rt_max(x, y) \
  67. ({ \
  68. typeof(x) _x = (x); \
  69. typeof(y) _y = (y); \
  70. (void) (&_x == &_y); \
  71. _x > _y ? _x : _y; \
  72. })
  73. #define rt_min_t(type, x, y) \
  74. ({ \
  75. type _x = (x); \
  76. type _y = (y); \
  77. _x < _y ? _x: _y; \
  78. })
  79. #define rt_max_t(type, x, y) \
  80. ({ \
  81. type _x = (x); \
  82. type _y = (y); \
  83. _x > _y ? _x: _y; \
  84. })
  85. #define rt_clamp(val, lo, hi) rt_min((typeof(val))rt_max(val, lo), hi)
  86. #define rt_do_div(n, base) \
  87. ({ \
  88. rt_uint32_t _base = (base), _rem; \
  89. _rem = ((rt_uint64_t)(n)) % _base; \
  90. (n) = ((rt_uint64_t)(n)) / _base; \
  91. if (_rem > _base / 2) \
  92. ++(n); \
  93. _rem; \
  94. })
  95. #define rt_abs(x) \
  96. ({ \
  97. long ret; \
  98. if (sizeof(x) == sizeof(long)) \
  99. { \
  100. long __x = (x); \
  101. ret = (__x < 0) ? -__x : __x; \
  102. } \
  103. else \
  104. { \
  105. int __x = (x); \
  106. ret = (__x < 0) ? -__x : __x; \
  107. } \
  108. ret; \
  109. })
  110. #ifndef rt_ilog2
  111. rt_inline int rt_ilog2(rt_ubase_t v)
  112. {
  113. int l = 0;
  114. while ((1UL << l) < v)
  115. {
  116. l++;
  117. }
  118. return l;
  119. }
  120. #endif /* !rt_ilog2 */
  121. #endif /* __MISC_H__ */