mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] include/linux/kernel.h: fix mult_frac() multiple argument evaluation bug
Date: Wed, 15 May 2024 13:27:03 +0200	[thread overview]
Message-ID: <20240515112703.290968-1-a.fatoum@pengutronix.de> (raw)

This is a port of Linux commit 048a9883267f9b8f8e05dca9e9e8e6f991eea61e:

| Author:     Alexey Dobriyan <adobriyan@gmail.com>
| AuthorDate: Sat May 20 21:25:19 2023 +0300
|
| include/linux/math.h: fix mult_frac() multiple argument evaluation bug
|
| mult_frac() evaluates _all_ arguments multiple times in the body.
|
| Clarify comment while I'm at it.
|
| Link: https://lkml.kernel.org/r/f9f9fdbb-ec8e-4f5e-a998-2a58627a1a43@p183
| Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
| Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/linux/kernel.h | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index c411ac0860dd..cd7dac73f93a 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -107,17 +107,17 @@ extern long long simple_strtoll(const char *,char **,unsigned int);
 }							\
 )
 
-/*
- * Multiplies an integer by a fraction, while avoiding unnecessary
- * overflow or loss of precision.
- */
-#define mult_frac(x, numer, denom)(			\
-{							\
-	typeof(x) quot = (x) / (denom);			\
-	typeof(x) rem  = (x) % (denom);			\
-	(quot * (numer)) + ((rem * (numer)) / (denom));	\
-}							\
-)
+/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
+#define mult_frac(x, n, d)	\
+({				\
+	typeof(x) x_ = (x);	\
+	typeof(n) n_ = (n);	\
+	typeof(d) d_ = (d);	\
+				\
+	typeof(x_) q = x_ / d_;	\
+	typeof(x_) r = x_ % d_;	\
+	q * n_ + r * n_ / d_;	\
+})
 
 extern const char hex_asc[];
 #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
-- 
2.39.2




             reply	other threads:[~2024-05-15 11:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-15 11:27 Ahmad Fatoum [this message]
2024-05-16  5:58 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240515112703.290968-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox