Re: The type of bitops

From: Linus Torvalds
Date: Mon May 06 2013 - 22:33:18 EST


On Mon, May 6, 2013 at 6:36 PM, H. Peter Anvin <hpa@xxxxxxxxx> wrote:
>>
>> That sounds insane. Just *how* could the size of the argument even
>> matter? Seriously, there's no *difference* between a 32-bit or a
>> 64-bit index. The code is the same, there's no possible reason to make
>> it be different.
>>
>
> The only reason on x86 would be to avoid the REX prefix. Hardly worth
> it, though.

Hmm. You *might* be able to do it by simply turning the inline
functions into statement expressions, and letting the compiler do the
types as it will. Use "(nr)+0" to make sure that the type is at least
"int", and then "Ir" as the asm constraint.

A quick test seems to imply that this works:

#define test_bit(nr, addr) \
({ int oldbit; \
asm volatile("bt %2,%1\n\t" \
"sbb %0,%0" \
: "=r" (oldbit) \
: "m" (*(unsigned long *)(addr)), \
"Ir" ((nr)+0)); \
oldbit; })

int test(int idx1, long idx2, void *addr)
{
return test_bit(idx1, addr) + test_bit(idx2, addr);
}

and I get

...
bt %edi,(%rdx)
...
bt %rsi,(%rdx)
...

for the int/long index respectively. So if the argument is an 'int',
it will avoid the rex prefix.

And I tested that using a "char" index does indeed do the right thing too:

movsbl %dil,%eax
bt %eax,(%rcx)

so that all seems surmountable.Whether it really matters, I dunno.

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/