Re: [PATCH] Remove some divide instructions

From: Geert Uytterhoeven
Date: Fri Oct 29 2004 - 20:38:33 EST


On Thu, 28 Oct 2004, Linus Torvalds wrote:
> On Thu, 28 Oct 2004, Zachary Amsden wrote:
> #define __constant_log2(y) ((y==1) ? 0 : \
> (y==2) ? 1 : \
> (y==4) ? 2 : \
> (y==8) ? 3 : -1) /* We could go on ... */
>
> #define do_div(x,y) ({ \
> unsigned long __mod; \
> int __log2; \
> if (__builtin_constant_p(y) && \
> !((y) & ((y)-1)) && \
> (__log2 = __constant_log2((y))) >= 0) { \
> mod = x & ((y)-1); \
> (x) >>= __log2; \
> } else { \
> .. inline asm case .. \
> } \
> __mod; })
>
> which looks like it should work, but it's getting so ugly that I suspect I
> should be committed for even thinking about it.
>
> (And no, I didn't test the above. It is all trivially optimizable by a
> compiler, and I can't see how gcc could _fail_ to get it right, but hey, I
> thought the previous thing would work too, so I'm clearly not competent to
> make that judgement... ;)

Perhaps this is why you see in many header files code like this:

static inline xxx_const_case(...) { ... }
static inline xxx_nonconst_case(...) { ... }

#define xxx(...) __builtin_constant_p(...) ? xxx_const_case(...) \
: xxx_nonconst_case(...)

i.e. gcc is better in optimizing away calls to non-called functions?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
-
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/