linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v16 1/2] math64.h: provide rounddown_u64 variant for rounddown macro
@ 2025-06-25  0:00 Christian Marangi
  2025-06-25  0:00 ` [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC Christian Marangi
  2025-06-25 11:39 ` [PATCH v16 1/2] math64.h: provide rounddown_u64 variant for rounddown macro Andy Shevchenko
  0 siblings, 2 replies; 9+ messages in thread
From: Christian Marangi @ 2025-06-25  0:00 UTC (permalink / raw)
  To: Uwe Kleine-König, Christian Marangi, linux-kernel, linux-pwm,
	Andy Shevchenko

There is currently a problem with the usage of rounddown() macro with
u64 dividends. This causes compilation error on specific arch where
64-bit division is done on 32-bit system.

To be more specific GCC try to optimize the function and replace it
with __umoddi3() but this is actually not compiled in the kernel.

Example:
pwm-airoha.c:(.text+0x8f8): undefined reference to `__umoddi3'

To better handle this, introduce a variant of rounddown() macro,
rounddown_u64() that can be used exactly for this scenario.

The new rounddown_u64() in math64.h uses do_div() to do the heavy work
of handling internally all the magic for the 64-bit division on 32-bit
(and indirectly fix the compilation error).

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
Changes v16:
- Move to math64.h
- Fix spelling mistake
- Use inline function (to follow roundup_u64 pattern)
Changes v15:
- Add this patch

 include/linux/math64.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/linux/math64.h b/include/linux/math64.h
index 6aaccc1626ab..60cf79d0d7d6 100644
--- a/include/linux/math64.h
+++ b/include/linux/math64.h
@@ -370,4 +370,20 @@ static inline u64 roundup_u64(u64 x, u32 y)
 {
 	return DIV_U64_ROUND_UP(x, y) * y;
 }
+
+/**
+ * rounddown_u64 - Round down a 64bit value to the next specified 32bit multiple
+ * @x: the value to round
+ * @y: 32bit multiple to round down to
+ *
+ * Rounds @x down to the next multiple of @y. For 32bit @x values, see rounddown
+ * and the faster round_down() for powers of 2.
+ *
+ * Return: rounded up value.
+ */
+static inline u64 rounddown_u64(u64 x, u32 y)
+{
+	u64 tmp = x;
+	return x - do_div(tmp, y);
+}
 #endif /* _LINUX_MATH64_H */
-- 
2.48.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-06-25 14:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-25  0:00 [PATCH v16 1/2] math64.h: provide rounddown_u64 variant for rounddown macro Christian Marangi
2025-06-25  0:00 ` [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC Christian Marangi
2025-06-25  7:24   ` Uwe Kleine-König
2025-06-25  7:40     ` Christian Marangi
2025-06-25 11:18       ` Andy Shevchenko
2025-06-25 12:01         ` Christian Marangi
2025-06-25 14:05       ` Uwe Kleine-König
2025-06-25 11:39 ` [PATCH v16 1/2] math64.h: provide rounddown_u64 variant for rounddown macro Andy Shevchenko
2025-06-25 13:16   ` Christian Marangi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).