.../asm-i386/bitops.h performance improvements
In find_first_bit() there exists this the sequence:
shll $3,%%edi
addl %%edi,%%eax
LEA knows how to multiply by small powers of 2 and add all in one shot very efficiently:
leal (%%eax,%%edi,8),%%eax
In find_first_zero_bit() the sequence:
shll $3,%%edi
addl %%edi,%%edx
could similarly become:
leal (%%edx,%%edi,8),%%edx
Quoted 11 lines · On Wednesday 15 June 2005 04:53, [email protected] wrote:
In
find_first_bit()there exists this the sequence:shll $3,%%edi
addl %%edi,%%eaxLEA knows how to multiply by small powers of 2 and add all in one shot very efficiently:
leal (%%eax,%%edi,8),%%eax
Quoted 9 lines
In
find_first_zero_bit()the sequence:shll $3,%%edi
addl %%edi,%%edxcould similarly become:
leal (%%edx,%%edi,8),%%edx
To what cpu families does this apply? eg, this may be true for intel, but what about amd, via etc?
Quoted 7 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 8 lines
Cheers, Gene "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) 99.35% setiathome rank, not too shabby for a WV hillbilly Yahoo.com and AOL/TW attorneys please note, additions to the above message by Gene Heskett are: Copyright 2005 by Maurice Eugene Heskett, all rights reserved.
LEA was designed for address calculation on ix86 processors. If it is used to ready the value of an index register for the next memory access, it can run in parallel with the next operations. However, if it is just used to put a value into a register, where the CPU can't proceed until that value is finalized, it does nothing more useful than shifts and adds.
In other words, don't substitute LEA for INC or ADD just because you can.
leal 0x04(%ebx), %ebx ... and addl $0x04, %ebx
... are functionally the same if the CPU needs the value in ebx immediately. In the code sequence....
movl (%ebx), %eax leal 0x04(%ebx), %ebx # Next address xorl %ecx, %eax movl %eax, (%ebx)
... the address calculation for the marked next address can proceed in parallel with the xorl operation that follows. This makes LEA helpful. However, in the following...
Quoted 1 line
Quoted 1 line
leal (%%eax,%%edi,8),%%eax
... the CPU needs to complete the whole operation before proceeding. If you measure this, LEA with two index registers, you will find that the shift and add is faster, guaranteed.
Quoted 25 lines · Gene Heskett
Quoted 11 lines · On Wednesday 15 June 2005 04:53, [email protected] wrote:
In
find_first_bit()there exists this the sequence:shll $3,%%edi
addl %%edi,%%eaxLEA knows how to multiply by small powers of 2 and add all in one shot very efficiently:
leal (%%eax,%%edi,8),%%eax
Quoted 9 lines
In
find_first_zero_bit()the sequence:shll $3,%%edi
addl %%edi,%%edxcould similarly become:
leal (%%edx,%%edi,8),%%edx
To what cpu families does this apply? eg, this may be true for intel, but what about amd, via etc?
Quoted 2 lines
Quoted 21 lines
Quoted 5 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/Signature · 13 lines
Cheers, Gene "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) 99.35% setiathome rank, not too shabby for a WV hillbilly Yahoo.com and AOL/TW attorneys please note, additions to the above message by Gene Heskett are: Copyright 2005 by Maurice Eugene Heskett, all rights reserved. - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Cheers,
Dick Johnson
Penguin : Linux version 2.6.11.9 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.
----- Original Message -----
From: "Gene Heskett" <[email protected]>
To: <[email protected]>
Quoted 3 lines
Quoted 1 line
To what cpu families does this apply? eg, this may be true for intel, but what about amd, via etc?
You tell me -- I've included below a small benchmark that compares them.
These are the results I've gotten so far:
LEA SHL/ADD
---------------------------------------
Pentium Pro 200 88sec 96sec AMD K6/2-500 29sec 48sec
386SLC(386SX core) 2966sec 4932sec
If LEA isn't fast, those CPU's you mentioned have much bigger problems than these two inline functions because GCC always generates (with the kernel default -O2 at least) an LEA for things like this:
unsigned int foo(unsigned int bar)
{
return ((bar<<3)+bar);
}
----------- LEA vs SHL/ADD ----------
#include <stdio.h>
#include <time.h>
#define ITERATIONS 2000000L
#define START start = time(&start);
#define STOP stop = time(&stop); delta = stop - start;
#define SUMMARY(s) printf(s " [%ld] seconds\n",delta); #define TESTLOOP for (i=0; i<ITERATIONS; i++)
static void inline shl(void)
{__asm__("shll $3,%edi; addl %edi,%eax");
}
static void inline lea(void)
{__asm__("leal (%eax,%edi,8),%eax");
}
int main(int argc, char *argv[], char *envp[])
{time_t start, stop, delta;
int i;
START;
TESTLOOP
{
#undef T
#define T shl();shl();shl();shl();shl();shl();shl();shl();shl();shl(); #define T100 T T T T T T T T T T T
#define T1000 T100 T100 T100 T100 T100 T100 T100 T100 T100 T100
__asm__ __volatile__("pushl %eax");__asm__ __volatile__("pushl %edi");
T1000 T1000 T1000 T1000 T1000 T1000__asm__ __volatile__("popl %edi");__asm__ __volatile__("popl %eax");
}
STOP;SUMMARY("SHL/ADD");
/*---------------------------------------------------*/
START;
TESTLOOP
{
#undef T
#define T lea();lea();lea();lea();lea();lea();lea();lea();lea();lea(); #define T100 T T T T T T T T T T T
#define T1000 T100 T100 T100 T100 T100 T100 T100 T100 T100 T100
__asm__ __volatile__("pushl %eax");__asm__ __volatile__("pushl %edi");
T1000 T1000 T1000 T1000 T1000 T1000__asm__ __volatile__("popl %edi");__asm__ __volatile__("popl %eax");
}
STOP;SUMMARY("LEA");
return 0;
}
Quoted 9 lines · On Wed, 15 Jun 2005 [email protected] wrote:
In
find_first_bit()there exists this the sequence:shll $3,%%edi
addl %%edi,%%eaxLEA knows how to multiply by small powers of 2 and add all in one shot very efficiently:
leal (%%eax,%%edi,8),%%eax
Be careful about model-specific penalties from using certain address
modes and AGIs when using "lea" for such calculations.
Maciej
The only thing I've seen so far that loses is a straight original 486. Its pipe is somewhat more simple minded and interlock prone than later models. PPro, K6 and 386SX are big winners.
On the 386's, lacking actual pipeline, the compactness of the LEA over the SHL/ADD can be more of an overriding factor.
----- Original Message -----
From: "Maciej W. Rozycki" <[email protected]>
To: <[email protected]>
Cc: <[email protected]>
Sent: Wednesday, June 15, 2005 11:34
Subject: Re: .../asm-i386/bitops.h performance improvements
Quoted 5 lines
Be careful about model-specific penalties from using certain address
modes and AGIs when using "lea" for such calculations.Maciej