From: David Laight <David.Laight@ACULAB.COM>
To: 'Linus Torvalds' <torvalds@linux-foundation.org>,
Sebastian Reichel <sebastian.reichel@collabora.com>
Cc: 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>,
Vasily Gorbik <gor@linux.ibm.com>,
Niklas Schnelle <schnelle@linux.ibm.com>,
"kernel@collabora.com" <kernel@collabora.com>
Subject: RE: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW
Date: Wed, 25 Oct 2023 08:38:39 +0000 [thread overview]
Message-ID: <4c2d36375bd74d94a2e6ef5d2fa0df99@AcuMS.aculab.com> (raw)
In-Reply-To: <CAHk-=wjO5ivM6k7iMiThO9JfxH0dhLe=mcC4TQwReU0nBCnWpg@mail.gmail.com>
From: Linus Torvalds
> Sent: 24 October 2023 23:53
>
> On Tue, 24 Oct 2023 at 09:32, Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
> >
> > I would really prefer to just make our regular DIV_ROUND_UP() DTRT. But:
> >
> > - people do use it with complex first arguments (ie function calls
> > etc) that we don't want to evaluate twice
> >
> > - we can't make it an inline function, because the types aren't fixed
> >
> > - we can't even use a statement expression and __auto_type, because
> > these things are used in type definitions etc and need to be constant
> > expressions
Doesn't min() get around that by using is_constexpr() and
__builtin_choose_exptr() - the same could be done here.
>
> Ok. I have a potential beginning of a solution.
>
> It is unbelievably disgustingly complicated. But it might approach
> being correct.
>
> And by that "it might approach being correct" I obviously mean "this
> is untested, but builds at least some kernel code".
>
> I'm almost certain it will fail on more complex cases, because I
> already found a lot of questionable stuff that was simply hidden by
> the old macro just silently doing the C arithmetic type conversions,
> and this thing does type handling manually.
>
> I'm hoping that somebody will go "Linus, you're just being
> *completely* silly, it's much easier to do XYZ".
> Doing a non-overflowing DIV_ROUND_UP() that is usable in all contexts is
> actually very nasty.
>
> This is a trial balloon.. The signed cases need more thought. The best
> option would be to disallow them (by not listing them in the _Generic()
> rules). But they currently happen, often for bad reasons, ie wireless has
>
> DIV_ROUND_UP(interval, MSEC_PER_SEC);
>
> and while 'interval' is a proper u32, MSEC_PER_SEC is defined to be
> '1000L', so the resulting C arithmetic is done in signed 'long'.
Maybe use some of the 'stuff' from min() and convert compile-time
constant 'd' to signed int to avoid promotions.
Indeed the whole thing really only makes sense for (d > 0 && n >= 0)
so forcing an unsigned divide wouldn't be a bad thing at all.
It will also generate better code when 'd' is a power of 2.
Ignoring the n==0 case I think this always generates an unsigned
divide, never does sign extension and does a 32bit divide
for 32bit arguments.
#define CVT_ULL(x) ((x) + 0u + 0ul + 0ull)
#define DIV_ROUND_UP(n, d) ((CVT_ULL(n) + CVT_ULL(d) - 1) / CVT_ULL(d) + 1)
It should be possible to error if 'd' is a signed variable or
a non-positive constant.
I'd guess most 'd' are constants.
Erroring signed 'n' is possible but might be annoying.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2023-10-25 8:38 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
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 [this message]
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=4c2d36375bd74d94a2e6ef5d2fa0df99@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.