misc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 RT_FIELD_PREP(mask, val) (((rt_uint64_t)(val) << (__rt_ffsl((mask)) - 1)) & (mask))
  32. #define RT_FIELD_GET(mask, val) (((val) & (mask)) >> (__rt_ffsl((mask)) - 1))
  33. #define RT_BIT(n) (1UL << (n))
  34. #define RT_BIT_ULL(n) (1ULL << (n))
  35. #define RT_BIT_MASK(nr) (1UL << ((nr) % RT_BITS_PER_LONG))
  36. #define RT_BIT_WORD(nr) ((nr) / RT_BITS_PER_LONG)
  37. #define RT_BITS_PER_BYTE 8
  38. #define RT_BITS_PER_TYPE(type) (sizeof(type) * RT_BITS_PER_BYTE)
  39. #define RT_BITS_TO_BYTES(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(char))
  40. #define RT_BITS_TO_LONGS(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(long))
  41. #define RT_GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (RT_BITS_PER_LONG - 1 - (h))))
  42. #define RT_GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (RT_BITS_PER_LONG_LONG - 1 - (h))))
  43. #define RT_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  44. #define rt_offsetof(s, field) ((rt_size_t)&((s *)0)->field)
  45. #define rt_err_ptr(err) ((void *)(rt_base_t)(err))
  46. #define rt_ptr_err(ptr) ((rt_err_t)(rt_base_t)(ptr))
  47. #define rt_is_err_value(ptr) ((rt_ubase_t)(void *)(ptr) >= (rt_ubase_t)-4095)
  48. #define rt_is_err(ptr) rt_is_err_value(ptr)
  49. #define rt_is_err_or_null(ptr) (!(ptr) || rt_is_err_value((rt_ubase_t)(ptr)))
  50. #define rt_upper_32_bits(n) ((rt_uint32_t)(((n) >> 16) >> 16))
  51. #define rt_lower_32_bits(n) ((rt_uint32_t)((n) & 0xffffffff))
  52. #define rt_upper_16_bits(n) ((rt_uint16_t)((n) >> 16))
  53. #define rt_lower_16_bits(n) ((rt_uint16_t)((n) & 0xffff))
  54. #define rt_min(x, y) \
  55. ({ \
  56. typeof(x) _x = (x); \
  57. typeof(y) _y = (y); \
  58. (void) (&_x == &_y); \
  59. _x < _y ? _x : _y; \
  60. })
  61. #define rt_max(x, y) \
  62. ({ \
  63. typeof(x) _x = (x); \
  64. typeof(y) _y = (y); \
  65. (void) (&_x == &_y); \
  66. _x > _y ? _x : _y; \
  67. })
  68. #define rt_min_t(type, x, y) \
  69. ({ \
  70. type _x = (x); \
  71. type _y = (y); \
  72. _x < _y ? _x: _y; \
  73. })
  74. #define rt_max_t(type, x, y) \
  75. ({ \
  76. type _x = (x); \
  77. type _y = (y); \
  78. _x > _y ? _x: _y; \
  79. })
  80. #define rt_clamp(val, lo, hi) rt_min((typeof(val))rt_max(val, lo), hi)
  81. #define rt_do_div(n, base) \
  82. ({ \
  83. rt_uint32_t _base = (base), _rem; \
  84. _rem = ((rt_uint64_t)(n)) % _base; \
  85. (n) = ((rt_uint64_t)(n)) / _base; \
  86. if (_rem > _base / 2) \
  87. ++(n); \
  88. _rem; \
  89. })
  90. #ifndef rt_ilog2
  91. rt_inline int rt_ilog2(rt_ubase_t v)
  92. {
  93. int l = 0;
  94. while ((1UL << l) < v)
  95. {
  96. l++;
  97. }
  98. return l;
  99. }
  100. #endif /* !rt_ilog2 */
  101. #endif /* __MISC_H__ */