Re: [GIT PULL] x86/mm for 6.4

From: Linus Torvalds
Date: Tue May 02 2023 - 12:00:32 EST


On Tue, May 2, 2023 at 8:42 AM Dave Hansen <dave.hansen@xxxxxxxxx> wrote:
>
> Have anyone seen any actual code generation difference between:
>
> return (long)ptr >= 0;
>
> and
>
> return !((unsigned long)ptr & (1UL<<(BITS_PER_LONG-1)));

No, as far as I know, they both generate the same code.

> It's longer, but I'd rather read the explicit "check bit 63" than the
> positive/negative address space thing. I certainly grok both, but have
> to think through the "(long)ptr >= 0" check every time.

I'm very much the other way. I think it's much clearer to say "check
the sign bit".

Doing the explicit bit check means that I have to look at what the bit
number is, and that is a much more complex expression.

In fact, I'd find it easier to read

return !((unsigned long)ptr & (1UL<< 63));

just because then you go "Oh, checking bit 63" without having to parse
the expression.

But even then I find the '!' is easy to miss, so you really have to parse it.

But:

> I guess it also wouldn't matter as much either if we hid it in a helper
> like the attached patch and I didn't have to read it twice. ;)

Yeah, I think that's a good solution.

Linus