[PATCH v3 02/13] arm64: cpufeatures: Correctly handle signed values

Will Deacon will at kernel.org
Tue Jan 30 03:34:00 PST 2024


On Tue, Jan 09, 2024 at 11:40:19AM +0000, Marc Zyngier wrote:
> On Mon, 08 Jan 2024 17:46:12 +0000,
> Marc Zyngier <maz at kernel.org> wrote:
> > 
> > On Mon, 11 Dec 2023 12:24:16 +0000,
> > Will Deacon <will at kernel.org> wrote:
> 
> [...]
> 
> > > > diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
> > > > index 646591c67e7a..bc8787f28ffd 100644
> > > > --- a/arch/arm64/kernel/cpufeature.c
> > > > +++ b/arch/arm64/kernel/cpufeature.c
> > > > @@ -140,12 +140,42 @@ void dump_cpu_features(void)
> > > >  	pr_emerg("0x%*pb\n", ARM64_NCAPS, &system_cpucaps);
> > > >  }
> > > >  
> > > > +#define __ARM64_MAX_POSITIVE(reg, field)				\
> > > > +		((reg##_##field##_SIGNED ?				\
> > > > +		  BIT(reg##_##field##_WIDTH - 1) :			\
> > > > +		  BIT(reg##_##field##_WIDTH)) - 1)
> > > > +
> > > > +#define __ARM64_MIN_NEGATIVE(reg, field)  BIT(reg##_##field##_WIDTH - 1)
> > > 
> > > I'm struggling to grok these two macros. For example, let's say I have a
> > > 4-bit signed field. In that case, the maximum positive value is 7 (0b0111)
> > > and the minimum negative value is -8 (0b1000), but the macros above appear
> > > to give 0b1000 for both.
> > 
> > Crap. Well spotted. The signed maximum needs to be further adjusted
> > like this:
> 
> Actually, scratch that. The original code is correct and does return
> 0b0111 for the max of a signed 4 bit value, and 0b1000 for the min.
> The clue is in the bracketing (the '- 1' is applied to the result of
> the conditional expression in the max case).

Gah, sorry, that trailing '- 1' really caught me out with the formatting!

Will



More information about the linux-arm-kernel mailing list