From: David Laight <David.Laight@ACULAB.COM>
To: 'Sebastian Reichel' <sebastian.reichel@collabora.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>,
"linux-clk@vger.kernel.org" <linux-clk@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: Vasily Gorbik <gor@linux.ibm.com>,
Niklas Schnelle <schnelle@linux.ibm.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
"kernel@collabora.com" <kernel@collabora.com>
Subject: RE: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW
Date: Tue, 24 Oct 2023 16:26:55 +0000 [thread overview]
Message-ID: <488098703d6947ee8d7b19312c7097a6@AcuMS.aculab.com> (raw)
In-Reply-To: <20231024161931.78567-2-sebastian.reichel@collabora.com>
From: Sebastian Reichel
> Sent: 24 October 2023 17:18
>
> Add a new DIV_ROUND_UP helper, which cannot overflow when
> big numbers are being used.
For non-zero you can use (n - 1)/d + 1 instead of (n + d - 1)/d
So maybe add:
#define DIV_ROUND_UP_NON_ZERO(n, d) (((n) - 1)/(d) + 1)
Saves the compiler having to get the remainder (if not
generated by a divide instruction.
David
>
> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
> ---
> include/linux/math.h | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/include/linux/math.h b/include/linux/math.h
> index dd4152711de7..f80bfb375ab9 100644
> --- a/include/linux/math.h
> +++ b/include/linux/math.h
> @@ -36,6 +36,17 @@
>
> #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
>
> +/**
> + * DIV_ROUND_UP_NO_OVERFLOW - divide two numbers and always round up
> + * @n: numerator / dividend
> + * @d: denominator / divisor
> + *
> + * This functions does the same as DIV_ROUND_UP, but internally uses a
> + * division and a modulo operation instead of math tricks. This way it
> + * avoids overflowing when handling big numbers.
> + */
> +#define DIV_ROUND_UP_NO_OVERFLOW(n, d) (((n) / (d)) + !!((n) % (d)))
> +
> #define DIV_ROUND_DOWN_ULL(ll, d) \
> ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
>
> --
> 2.42.0
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2023-10-24 16:27 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-24 16:18 [PATCH v4 0/3] Fix clock division overflow problem Sebastian Reichel
2023-10-24 16:18 ` [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW Sebastian Reichel
2023-10-24 16:26 ` David Laight [this message]
2023-10-24 19:32 ` Linus Torvalds
2023-10-24 22:53 ` Linus Torvalds
2023-10-25 8:03 ` Rasmus Villemoes
2023-10-25 17:37 ` Linus Torvalds
2023-10-25 8:38 ` David Laight
2023-10-25 17:41 ` Linus Torvalds
2023-10-26 8:41 ` David Laight
2023-10-25 15:05 ` Vasily Gorbik
2023-10-25 17:43 ` Linus Torvalds
2023-10-26 8:57 ` David Laight
2023-10-26 16:54 ` Linus Torvalds
2023-10-27 7:24 ` David Laight
2023-10-25 17:36 ` Sebastian Reichel
2023-10-25 17:28 ` Sebastian Reichel
2023-10-24 16:18 ` [PATCH v4 2/3] clk: divider: Fix divisor masking on 64 bit platforms Sebastian Reichel
2023-10-24 16:18 ` [PATCH v4 3/3] clk: composite: replace open-coded abs_diff() Sebastian Reichel
2023-10-24 16:23 ` Andy Shevchenko
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=488098703d6947ee8d7b19312c7097a6@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=gor@linux.ibm.com \
--cc=kernel@collabora.com \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mturquette@baylibre.com \
--cc=sboyd@kernel.org \
--cc=schnelle@linux.ibm.com \
--cc=sebastian.reichel@collabora.com \
--cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.