Re: [PATCH] Make shr to divide by power of 2

From: Andi Kleen
Date: Sat Aug 08 2009 - 03:35:46 EST


On Fri, Aug 07, 2009 at 09:09:36PM -0600, Robert Hancock wrote:
> On 08/07/2009 12:50 AM, Andi Kleen wrote:
>> Sergey Senozhatsky<sergey.senozhatsky@xxxxxxxxx> writes:
>>> Of course, compiler supposed to do something like
>>> add $0x7,%eax
>>> shr $0x3,%eax
>>> instead of div. Should we rely on this?
>>
>> Yes. We should rely on this.
>
> It may depend on the selected CPU type that gcc is optimizing for - I
> believe that on some P4s the shift may actually be slower than the divide..

DIV should be always slower than a SHIFT.

But it has nothing really to do with the CPU. The point is that the compiler
always selects a suitable one by itself. Rewriting x / 2 to x >> 1 is
one of the easiest exercises in compiler optimizations.

The only case when the compiler cannot do this easily by itself is
when the dividend is not a constant.

That said -Os sometimes screws us up on this, but it's still not worth
doing this change manually.

-Andi

--
ak@xxxxxxxxxxxxxxx -- Speaking for myself only.
--
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/