Re: using gcc built-ins for bitops?

From: Linus Torvalds
Date: Thu Jun 24 2004 - 10:29:47 EST




On Thu, 24 Jun 2004, Jakub Jelinek wrote:
> >
> > As to your primary question - is this worth doing - I don't have
> > an answer.
>
> It is, for 2 reasons:
> 1) unlike __asm, GCC knows how to schedule the instructions in the builtins
> 2) GCC will handle stuff like ffz (16) at compile time rather than runtime

I'd argue that unless the gcc code can be shown to be clearly better, it
is NOT worth doing.

Adding support for built-ins generates a noticeable maintenance burden,
since we'll still have to support older gcc's and architectures where gcc
does WORSE than we do. And quite frankly, I doubt you'll find any cases
where gcc does any better in any measurable way.

In other words, the rule about gcc builtins should be:

- use them only if they are old enough that the huge majority (possibly
all) of users get them. This is partly because gcc has frankly been
buggy, and often makes assumptions that simply may not be true for the
kernel (ie "it's ok to use library routines").

- only use them if you can show a measurable improvement. Theoretical
arguments simply don't count. Theoretical arguments is why gcc-3.x is
a lot slower than 2.95 and is apparently still not generating
appreciably better code for the kernel (and no, don't bother pointing
me to spec runs, I just don't care. The kernel is what I care about).

So far, the only case where they have been worth it is likely the
"memcpy()" stuff, and even there our previous macros were doing an almost
equivalent job (but were arguably so ugly that it was worth making the
change).

For something like ffs/popcount/etc, I do not see _any_ point to compiler
support. There just aren't any kernel uses that make it worth it. Sounds
like a total micro-optimization for some spec benchmark.

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/