Skip to content

Commit 349d592

Browse files
committed
[DM/MISC] Update MISC API
1. Fixup RT_DIV_ROUND_DOWN_ULL and RT_DIV_ROUND_UP_ULL. 2. Support RT_DIV_ROUND_CLOSEST_ULL. 3. Make new DIV API. Signed-off-by: GuEe-GUI <2991707448@qq.com>
1 parent 2fb53c8 commit 349d592

File tree

1 file changed

+63
-8
lines changed
  • components/drivers/include/drivers

1 file changed

+63
-8
lines changed

components/drivers/include/drivers/misc.h

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

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

26+
#define RT_DIV_ROUND_DOWN_ULL(ll, d) ({ rt_uint64_t _tmp = (ll); rt_do_div(_tmp, d); _tmp; })
27+
28+
#define RT_DIV_ROUND_UP_ULL(ll, d) RT_DIV_ROUND_DOWN_ULL((rt_uint64_t)(ll) + (d) - 1, (d))
29+
2630
#define RT_DIV_ROUND_CLOSEST(x, divisor) \
2731
({ \
2832
typeof(x) __x = x; \
@@ -34,6 +38,14 @@
3438
(((__x) - ((__d) / 2)) / (__d)); \
3539
})
3640

41+
#define RT_DIV_ROUND_CLOSEST_ULL(x, divisor) \
42+
({ \
43+
typeof(divisor) __d = divisor; \
44+
rt_uint64_t _tmp = (x) + (__d) / 2; \
45+
rt_do_div(_tmp, __d); \
46+
_tmp; \
47+
})
48+
3749
#define __KEY_PLACEHOLDER_1 0,
3850
#define ____KEY_ENABLED(__ignored, val, ...) val
3951
#define ___KEY_ENABLED(arg1_or_junk) ____KEY_ENABLED(arg1_or_junk 1, 0)
@@ -103,14 +115,12 @@
103115

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

106-
#define rt_do_div(n, base) \
107-
({ \
108-
rt_uint32_t _base = (base), _rem; \
109-
_rem = ((rt_uint64_t)(n)) % _base; \
110-
(n) = ((rt_uint64_t)(n)) / _base; \
111-
if (_rem > _base / 2) \
112-
++(n); \
113-
_rem; \
118+
#define rt_do_div(n, base) \
119+
({ \
120+
rt_uint32_t _base = (base); \
121+
rt_uint32_t _rem = (rt_uint64_t)(n) % _base; \
122+
(n) = (rt_uint64_t)(n) / _base; \
123+
_rem; \
114124
})
115125

116126
#define rt_abs(x) \
@@ -129,6 +139,18 @@
129139
ret; \
130140
})
131141

142+
#define rt_roundup(x, y) \
143+
({ \
144+
typeof(y) __y = y; \
145+
(((x) + (__y - 1)) / __y) * __y; \
146+
})
147+
148+
#define rt_rounddown(x, y) \
149+
({ \
150+
typeof(x) __x = (x); \
151+
__x - (__x % (y)); \
152+
})
153+
132154
#ifndef rt_ilog2
133155
rt_inline int rt_ilog2(rt_ubase_t v)
134156
{
@@ -143,4 +165,37 @@ rt_inline int rt_ilog2(rt_ubase_t v)
143165
}
144166
#endif /* !rt_ilog2 */
145167

168+
#ifndef rt_bcd2bin
169+
rt_inline rt_ubase_t rt_bcd2bin(rt_uint8_t val)
170+
{
171+
return (val & 0x0f) + (val >> 4) * 10;
172+
}
173+
#endif /* !rt_bcd2bin */
174+
175+
#ifndef rt_bin2bcd
176+
rt_inline rt_uint8_t rt_bin2bcd(rt_ubase_t val)
177+
{
178+
return ((val / 10) << 4) + val % 10;
179+
}
180+
#endif /* !rt_bin2bcd */
181+
182+
#ifndef rt_div_u64_rem
183+
rt_inline rt_uint64_t rt_div_u64_rem(rt_uint64_t dividend, rt_uint32_t divisor,
184+
rt_uint32_t *remainder)
185+
{
186+
*remainder = dividend % divisor;
187+
188+
return dividend / divisor;
189+
}
190+
#endif /* !rt_div_u64_rem */
191+
192+
#ifndef rt_div_u64
193+
rt_inline rt_uint64_t rt_div_u64(rt_uint64_t dividend, rt_uint32_t divisor)
194+
{
195+
rt_uint32_t remainder;
196+
197+
return rt_div_u64_rem(dividend, divisor, &remainder);
198+
}
199+
#endif /* !rt_div_u64 */
200+
146201
#endif /* __MISC_H__ */

0 commit comments

Comments
 (0)