Thread (67 messages) flat view 67 messages, 8 authors, 2021-09-21

Re: [PATCH v3 1/5] math64: New DIV_U64_ROUND_CLOSEST helper

From: Pali Rohár <pali@kernel.org>
Date: 2021-07-22 21:57:48
Also in: linux-clk, linux-serial, lkml

On Monday 19 July 2021 15:47:07 Andy Shevchenko wrote:
On Sat, Jul 17, 2021 at 3:39 PM Pali Rohár [off-list ref] wrote:
quoted
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.
...
quoted
+/*
+ * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer
quoted
+ * @dividend: unsigned 64bit dividend
Here you insist users to provide a u64 (or compatible) type.
quoted
+ * @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)       \
quoted
+       ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); })
Here is the casting to u64. Why? (Yes, I have read v1 discussion and I
just want to continue it here).
See also Willy's response: https://lore.kernel.org/lkml/20210625155008.GB16901@1wt.eu/ (local)

Macro does not enforce type as opposite to function.

There is no compile time check for correct type and neither compile time
warning if smaller typed value is passed.

And e.g. passing constant with explicit ULL suffix or casting external
constant to 64bit type is impractical.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help