On Tue, Nov 3, 2026 at 7:39 PM Jakub Jelinek <[email protected]> wrote:

> On Tue, Nov 03, 2026 at 06:23:19PM +0000, Jonathan Wakely wrote:
> > On Tue, 3 Nov 2026 at 15:38, Jakub Jelinek <[email protected]> wrote:
> > > +  template<typename _Tp>
> > > +    class wide_result {
> > > +    public:
> > > +      constexpr wide_result(_Tp __high, _Tp __low) noexcept
> > > +       : _M_high(__high), _M_low(__low) {}
> > > +
> > > +      constexpr _Tp high() const noexcept { return _M_high; }
> > > +      constexpr _Tp low() const noexcept { return _M_low; }
> > > +
> > > +      friend constexpr bool operator==(const wide_result&,
> > > +                                      const wide_result&) = default;
> > > +      friend constexpr strong_ordering operator<=>(const wide_result&,
> > > +                                                  const wide_result&)
> > > +       noexcept = default;
> > > +    private:
> > > +      _Tp _M_high, _M_low;
> >
> > LWG wanted this to be a class with private members rather than an
> > aggregate with public members so that the high and low parts could be
> > ordered optimally according to the target endianness, e.g. so that
> > wide_result<uint64_t> would have the same layout as unsigned int128.
>
> I don't see any advantages in doing that.

Sure, having the private bits actually be the twice as wide unsigned
> type is an implementation option, but one then needs to specialize
> those just for types that do have such a twice as wide type at all.
> Also, given the way I've introduced __builtin_clmulg which returns
> the low part and sets what optional third argument points at to the high
> part, using that strategy actually would be pessimization, especially for
> say unsigned _BitInt(31), having two unsigned _BitInt(31) parts is
> then better over having to combine it into unsigned _BitInt(62).
> And, without _BitInt there is no 2x wider type for unsigned __int128,
> on 32-bit targets no 2x wider type for unsigned long long, on _BitInt
> targets no 2x wider type for unsigned _BitInt(__BITINT_MAXWIDTH__ - 1)
> etc.
> I think it is fine if the standard allows the implementations to choose
> what they want though.
>
The context here, is that the same layout was originally suggested to allow
bitcasting/memcpy into a wider integer type. We didn't want to guarantee
using a,
and instead suggested adding a member function that would allow extraction
of low and high combined in single type. I would imagine something like:
   template<typename W = implementation-defined>
   W wide() { };
W should have a default argument only if a type at least twice as wide as T
exists.

I think this function could compile to fewer instructions for some
combination of W and T,
if we order the _M_high and _M_low members according to endianess of target,
without combining them into a single member.

>
> >
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3642r7.html#aggregate-type-std::wide_result
> >
> > Do we want to take advantage of that?
> >
> > I think we need static_assert(__is_unsigned_integer<_Tp>::value);
>
> Ah, I've missed
> "A specialization wide_result<T> is ill-formed unless T is an unsigned
> integer
> type"
>
> Should that be
> static_assert(__is_unsigned_integer<_Tp>::value);
>
static_assert.

> or instead
> template<__unsigned_integer _Tp>
> class wide_result ?
>
>         Jakub
>
>

Reply via email to