misc.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_DOWN_ULL(ll, d) ({ rt_uint64_t _tmp = (ll); rt_do_div(_tmp, d); _tmp; })
  22. #define RT_DIV_ROUND_UP_ULL(ll, d) RT_DIV_ROUND_DOWN_ULL((rt_uint64_t)(ll) + (d) - 1, (d))
  23. #define RT_DIV_ROUND_CLOSEST(x, divisor) \
  24. ({ \
  25. typeof(x) __x = x; \
  26. typeof(divisor) __d = divisor; \
  27. (((typeof(x))-1) > 0 || \
  28. ((typeof(divisor))-1) > 0 || \
  29. (((__x) > 0) == ((__d) > 0))) ? \
  30. (((__x) + ((__d) / 2)) / (__d)) : \
  31. (((__x) - ((__d) / 2)) / (__d)); \
  32. })
  33. #define RT_DIV_ROUND_CLOSEST_ULL(x, divisor) \
  34. ({ \
  35. typeof(divisor) __d = divisor; \
  36. rt_uint64_t _tmp = (x) + (__d) / 2; \
  37. rt_do_div(_tmp, __d); \
  38. _tmp; \
  39. })
  40. #define __KEY_PLACEHOLDER_1 0,
  41. #define ____KEY_ENABLED(__ignored, val, ...) val
  42. #define ___KEY_ENABLED(arg1_or_junk) ____KEY_ENABLED(arg1_or_junk 1, 0)
  43. #define __KEY_ENABLED(value) ___KEY_ENABLED(__KEY_PLACEHOLDER_##value)
  44. #define RT_KEY_ENABLED(key) __KEY_ENABLED(key)
  45. #define RT_FIELD_PREP(mask, val) (((rt_uint64_t)(val) << (__rt_ffsl((mask)) - 1)) & (mask))
  46. #define RT_FIELD_GET(mask, val) (((val) & (mask)) >> (__rt_ffsl((mask)) - 1))
  47. #define RT_BIT(n) (1UL << (n))
  48. #define RT_BIT_ULL(n) (1ULL << (n))
  49. #define RT_BIT_MASK(nr) (1UL << ((nr) % RT_BITS_PER_LONG))
  50. #define RT_BIT_WORD(nr) ((nr) / RT_BITS_PER_LONG)
  51. #define RT_BITS_PER_BYTE 8
  52. #define RT_BITS_PER_TYPE(type) (sizeof(type) * RT_BITS_PER_BYTE)
  53. #define RT_BITS_TO_BYTES(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(char))
  54. #define RT_BITS_TO_LONGS(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(long))
  55. #define RT_GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (RT_BITS_PER_LONG - 1 - (h))))
  56. #define RT_GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (RT_BITS_PER_LONG_LONG - 1 - (h))))
  57. #define RT_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  58. #define rt_offsetof(s, field) ((rt_size_t)&((s *)0)->field)
  59. #define rt_err_ptr(err) ((void *)(rt_base_t)(err))
  60. #define rt_ptr_err(ptr) ((rt_err_t)(rt_base_t)(ptr))
  61. #define rt_is_err_value(ptr) ((rt_ubase_t)(void *)(ptr) >= (rt_ubase_t)-4095)
  62. #define rt_is_err(ptr) rt_is_err_value(ptr)
  63. #define rt_is_err_or_null(ptr) (!(ptr) || rt_is_err_value((rt_ubase_t)(ptr)))
  64. #define rt_upper_32_bits(n) ((rt_uint32_t)(((n) >> 16) >> 16))
  65. #define rt_lower_32_bits(n) ((rt_uint32_t)((n) & 0xffffffff))
  66. #define rt_upper_16_bits(n) ((rt_uint16_t)((n) >> 16))
  67. #define rt_lower_16_bits(n) ((rt_uint16_t)((n) & 0xffff))
  68. #define rt_min(x, y) \
  69. ({ \
  70. typeof(x) _x = (x); \
  71. typeof(y) _y = (y); \
  72. (void) (&_x == &_y); \
  73. _x < _y ? _x : _y; \
  74. })
  75. #define rt_max(x, y) \
  76. ({ \
  77. typeof(x) _x = (x); \
  78. typeof(y) _y = (y); \
  79. (void) (&_x == &_y); \
  80. _x > _y ? _x : _y; \
  81. })
  82. #define rt_min_t(type, x, y) \
  83. ({ \
  84. type _x = (x); \
  85. type _y = (y); \
  86. _x < _y ? _x: _y; \
  87. })
  88. #define rt_max_t(type, x, y) \
  89. ({ \
  90. type _x = (x); \
  91. type _y = (y); \
  92. _x > _y ? _x: _y; \
  93. })
  94. #define rt_clamp(val, lo, hi) rt_min((typeof(val))rt_max(val, lo), hi)
  95. #define rt_do_div(n, base) \
  96. ({ \
  97. rt_uint32_t _base = (base); \
  98. rt_uint32_t _rem = (rt_uint64_t)(n) % _base; \
  99. (n) = (rt_uint64_t)(n) / _base; \
  100. _rem; \
  101. })
  102. #define rt_abs(x) \
  103. ({ \
  104. long ret; \
  105. if (sizeof(x) == sizeof(long)) \
  106. { \
  107. long __x = (x); \
  108. ret = (__x < 0) ? -__x : __x; \
  109. } \
  110. else \
  111. { \
  112. int __x = (x); \
  113. ret = (__x < 0) ? -__x : __x; \
  114. } \
  115. ret; \
  116. })
  117. #define rt_roundup(x, y) \
  118. ({ \
  119. typeof(y) __y = y; \
  120. (((x) + (__y - 1)) / __y) * __y; \
  121. })
  122. #define rt_rounddown(x, y) \
  123. ({ \
  124. typeof(x) __x = (x); \
  125. __x - (__x % (y)); \
  126. })
  127. #ifndef rt_ilog2
  128. rt_inline int rt_ilog2(rt_ubase_t v)
  129. {
  130. int l = 0;
  131. while ((1UL << l) < v)
  132. {
  133. l++;
  134. }
  135. return l;
  136. }
  137. #endif /* !rt_ilog2 */
  138. #ifndef rt_bcd2bin
  139. rt_inline rt_ubase_t rt_bcd2bin(rt_uint8_t val)
  140. {
  141. return (val & 0x0f) + (val >> 4) * 10;
  142. }
  143. #endif /* !rt_bcd2bin */
  144. #ifndef rt_bin2bcd
  145. rt_inline rt_uint8_t rt_bin2bcd(rt_ubase_t val)
  146. {
  147. return ((val / 10) << 4) + val % 10;
  148. }
  149. #endif /* !rt_bin2bcd */
  150. #ifndef rt_div_u64_rem
  151. rt_inline rt_uint64_t rt_div_u64_rem(rt_uint64_t dividend, rt_uint32_t divisor,
  152. rt_uint32_t *remainder)
  153. {
  154. *remainder = dividend % divisor;
  155. return dividend / divisor;
  156. }
  157. #endif /* !rt_div_u64_rem */
  158. #ifndef rt_div_u64
  159. rt_inline rt_uint64_t rt_div_u64(rt_uint64_t dividend, rt_uint32_t divisor)
  160. {
  161. rt_uint32_t remainder;
  162. return rt_div_u64_rem(dividend, divisor, &remainder);
  163. }
  164. #endif /* !rt_div_u64 */
  165. #endif /* __MISC_H__ */