diff options
author | Pali Rohár <pali@kernel.org> | 2022-02-19 16:28:13 +0100 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2022-02-25 10:27:57 +0100 |
commit | 2c861b73a23b1237bdd054b6023bb27f6747c2b9 (patch) | |
tree | e9844b25100551469ea95c36d6fd59130506fcac | |
parent | 679875d1d8802669590ef4d69b0e7d13207ebd61 (diff) | |
download | lwn-2c861b73a23b1237bdd054b6023bb27f6747c2b9.tar.gz lwn-2c861b73a23b1237bdd054b6023bb27f6747c2b9.zip |
math64: New DIV_U64_ROUND_CLOSEST helper
Provide DIV_U64_ROUND_CLOSEST helper which uses div_u64 to perform
division rounded to the closest integer using unsigned 64bit
dividend and unsigned 32bit divisor.
Reviewed-by: Marek Behún <kabel@kernel.org>
Signed-off-by: Pali Rohár <pali@kernel.org>
Signed-off-by: Marek Behún <kabel@kernel.org>
Link: https://lore.kernel.org/r/20220219152818.4319-2-kabel@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | include/linux/math64.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/include/linux/math64.h b/include/linux/math64.h index 2928f03d6d46..a14f40de1dca 100644 --- a/include/linux/math64.h +++ b/include/linux/math64.h @@ -301,6 +301,19 @@ u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div); ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); }) /* + * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor + * + * Divide unsigned 64bit dividend by unsigned 32bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV_U64_ROUND_CLOSEST(dividend, divisor) \ + ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); }) + +/* * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer * @dividend: signed 64bit dividend * @divisor: signed 32bit divisor |