Re: Re: [PATCH v3 1/3] bits: introduce fixed-type genmasks

From: Yury Norov
Date: Thu Feb 22 2024 - 09:50:22 EST


On Wed, Feb 21, 2024 at 03:59:06PM -0600, Lucas De Marchi wrote:
> On Wed, Feb 21, 2024 at 11:04:22PM +0200, Andy Shevchenko wrote:
> > On Wed, Feb 21, 2024 at 10:30:02PM +0200, Dmitry Baryshkov wrote:
> > > On Thu, 8 Feb 2024 at 09:45, Lucas De Marchi <lucas.demarchi@xxxxxxxxx> wrote:
> >
> > ...
> >
> > > > +#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
> >
> > Can sizeof() be used in assembly?
> >
> > ...
> >
> > > > -#define __GENMASK(h, l) \
> > > > - (((~UL(0)) - (UL(1) << (l)) + 1) & \
> > > > - (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
> > > > -#define GENMASK(h, l) \
> > > > - (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
> >
> > > > +#define __GENMASK(t, h, l) \
> > > > + (GENMASK_INPUT_CHECK(h, l) + \
> > > > + (((t)~0ULL - ((t)(1) << (l)) + 1) & \
> > > > + ((t)~0ULL >> (BITS_PER_TYPE(t) - 1 - (h)))))
> >
> > Nevertheless, the use ~0ULL is not proper assembly, this broke initial
> > implementation using UL() / ULL().
>
> indeed.
>
> >
> >
> > > > -#define __GENMASK_ULL(h, l) \
> > > > - (((~ULL(0)) - (ULL(1) << (l)) + 1) & \
> > > > - (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
> > > > -#define GENMASK_ULL(h, l) \
> > > > - (GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
> >
> > Ditto.
>
> problem here seems actually because of the cast to the final type. My
> previous impl was avoiding that, but was too verbose compared to this.
>
> I will look at reverting this.
>
> Lucas De Marchi

The fix is quite straightforward. Can you consider the following
patch? I tested it for C and x86_64 asm parts, and it compiles well.

Thanks,
Yury