Messages in this thread | | | From | David Laight <> | Subject | RE: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW | Date | Thu, 26 Oct 2023 08:41:47 +0000 |
| |
From: Linus Torvalds > Sent: 25 October 2023 18:41 > > On Tue, 24 Oct 2023 at 22:38, David Laight <David.Laight@aculab.com> wrote: > > > > From: Linus Torvalds > > > > - 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. > > Nope. I wanted to do it that way - it would have made things much > simpler and avoid the whole _Generic() thing, but try it - you cannot > use statement expressions in a non-function context even with > __builtin_choose_expr().
_Generic() has exactly the same issues as __builtin_choose_expr(). All the code has to be valid for all types. Makes testing for negative constants a PITA. Something like this worked: (__buitin_choose_expr(__is_constexpr((x) && is_signed_type(__typeof(x)), x, 1) < 0) Although our is_signed_type() isn't constant (enough) for pointer types. So you need another check for (typeof(x))1 being constant.
> And no, min/max have never been usable in that context
I've clearly not tested it and assumed the comment implied that was ok.
It'd probably work in C++ - which is perfectly willing to add functions to initialise static data - even when you'd rather have a compile time error.
David
> > Linus
- Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
| |