Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions components/drivers/include/drivers/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

#define RT_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))

#define RT_DIV_ROUND_DOWN_ULL(ll, d) ({ rt_uint64_t _tmp = (ll); rt_do_div(_tmp, d); _tmp; })

#define RT_DIV_ROUND_UP_ULL(ll, d) RT_DIV_ROUND_DOWN_ULL((rt_uint64_t)(ll) + (d) - 1, (d))

#define RT_DIV_ROUND_CLOSEST(x, divisor) \
({ \
typeof(x) __x = x; \
Expand All @@ -34,6 +38,14 @@
(((__x) - ((__d) / 2)) / (__d)); \
})

#define RT_DIV_ROUND_CLOSEST_ULL(x, divisor) \
({ \
typeof(divisor) __d = divisor; \
rt_uint64_t _tmp = (x) + (__d) / 2; \
rt_do_div(_tmp, __d); \
_tmp; \
})

#define __KEY_PLACEHOLDER_1 0,
#define ____KEY_ENABLED(__ignored, val, ...) val
#define ___KEY_ENABLED(arg1_or_junk) ____KEY_ENABLED(arg1_or_junk 1, 0)
Expand Down Expand Up @@ -103,14 +115,12 @@

#define rt_clamp(val, lo, hi) rt_min((typeof(val))rt_max(val, lo), hi)

#define rt_do_div(n, base) \
({ \
rt_uint32_t _base = (base), _rem; \
_rem = ((rt_uint64_t)(n)) % _base; \
(n) = ((rt_uint64_t)(n)) / _base; \
if (_rem > _base / 2) \
++(n); \
_rem; \
#define rt_do_div(n, base) \
({ \
rt_uint32_t _base = (base); \
rt_uint32_t _rem = (rt_uint64_t)(n) % _base; \
(n) = (rt_uint64_t)(n) / _base; \
_rem; \
})

#define rt_abs(x) \
Expand All @@ -129,6 +139,18 @@
ret; \
})

#define rt_roundup(x, y) \
({ \
typeof(y) __y = y; \
(((x) + (__y - 1)) / __y) * __y; \
})
Comment on lines +142 to +146
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[maintainability/可维护性]: Missing code comment for new macro / 新增宏缺少代码注释

English: The new rt_roundup macro lacks a comment explaining its purpose and usage. Consider adding a brief comment explaining that this macro rounds up a value to the nearest multiple of y.

中文: 新增的 rt_roundup 宏缺少注释说明其用途和使用方法。建议添加简短注释说明该宏将值向上舍入到 y 的最接近倍数。

Example/示例:

/* Round up x to the nearest multiple of y */
#define rt_roundup(x, y)                \
({                                      \
    typeof(y) __y = y;                  \
    (((x) + (__y - 1)) / __y) * __y;    \
})

Copilot uses AI. Check for mistakes.

#define rt_rounddown(x, y) \
({ \
typeof(x) __x = (x); \
__x - (__x % (y)); \
})

#ifndef rt_ilog2
rt_inline int rt_ilog2(rt_ubase_t v)
{
Expand All @@ -143,4 +165,37 @@ rt_inline int rt_ilog2(rt_ubase_t v)
}
#endif /* !rt_ilog2 */

#ifndef rt_bcd2bin
rt_inline rt_ubase_t rt_bcd2bin(rt_uint8_t val)
{
return (val & 0x0f) + (val >> 4) * 10;
}
#endif /* !rt_bcd2bin */

#ifndef rt_bin2bcd
rt_inline rt_uint8_t rt_bin2bcd(rt_ubase_t val)
{
return ((val / 10) << 4) + val % 10;
}
#endif /* !rt_bin2bcd */

#ifndef rt_div_u64_rem
rt_inline rt_uint64_t rt_div_u64_rem(rt_uint64_t dividend, rt_uint32_t divisor,
rt_uint32_t *remainder)
{
*remainder = dividend % divisor;

return dividend / divisor;
}
#endif /* !rt_div_u64_rem */

#ifndef rt_div_u64
rt_inline rt_uint64_t rt_div_u64(rt_uint64_t dividend, rt_uint32_t divisor)
{
rt_uint32_t remainder;

return rt_div_u64_rem(dividend, divisor, &remainder);
}
#endif /* !rt_div_u64 */

#endif /* __MISC_H__ */