Skip to content

Commit af60459

Browse files
Chunyan Zhangsre
authored andcommitted
math64: New DIV_S64_ROUND_CLOSEST helper
Provide DIV_S64_ROUND_CLOSEST helper which uses div_s64 to perform division rounded to the closest integer using signed 64bit dividend and signed 32bit divisor. Signed-off-by: Chunyan Zhang <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]>
1 parent ab92ffd commit af60459

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

include/linux/math64.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,23 @@ static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
279279
#define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \
280280
({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); })
281281

282+
/*
283+
* DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer
284+
* @dividend: signed 64bit dividend
285+
* @divisor: signed 32bit divisor
286+
*
287+
* Divide signed 64bit dividend by signed 32bit divisor
288+
* and round to closest integer.
289+
*
290+
* Return: dividend / divisor rounded to nearest integer
291+
*/
292+
#define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \
293+
{ \
294+
s64 __x = (dividend); \
295+
s32 __d = (divisor); \
296+
((__x > 0) == (__d > 0)) ? \
297+
div_s64((__x + (__d / 2)), __d) : \
298+
div_s64((__x - (__d / 2)), __d); \
299+
} \
300+
)
282301
#endif /* _LINUX_MATH64_H */

0 commit comments

Comments
 (0)