S
Skybuck Flying
Hello,
There is a bit position which needs to be converted to the longword position
in "byte pointer/offset form".
Method 1:
Divide the bit position by 32 (bits) and then to multiply it by 8 (bits).
The division gets rid of the remainder/fraction, the integer part is then
multiplied with 8 (bits) again to get the byte/memory cell offset.
(In other words "bit space is converted to longword space which is then
converted back to byte space")
Method 2:
Zero out bit position value range 0 to 31 by and-ing with the not of bits 0
to 4 set (the first/lower 5 bits).
This gets rid of the remainder as well and then divides it by 8 (bits).
(In other words "bit space is longword-nerved
and converted directly to
byte space")
Bit space has intervals of 1 bits.
Longword space has intervals of 32 bits.
Byte space has intervals of 8 bits.
So it kinda makes sense doesn't it

Anyway that's not that the weird part =D
The weird part is what Delphi does, and that's where you guys come in !

Delphi implement method 1 as follows:
TestProgram.dpr.27: mLongwordPosition1 := (mBitPosition shr 5) shl 2; //
delphi does something special here...
00408F5D 8BC3 mov eax,ebx
00408F5F C1E805 shr eax,$05
00408F62 03C0 add eax,eax
00408F64 03C0 add eax,eax
The shift right 5 is done with instruction shr 5.
The shift left 2 is done with two adds ?!?.
A total of 4 instructions (including the initializer (?))
For a total of 9 instruction bytes.
Delphi implement method 2 as follows:
TestProgram.dpr.30: mLongwordPosition2 := (mBitPosition and not
(1+2+4+8+16)) shr 3;
00408F66 8BD3 mov edx,ebx
00408F68 83E2E0 and edx,-$20
00408F6B C1EA03 shr edx,$03
A total of 3 instructions (including the initializer (?))
For a total of 8 instruction bytes.
Pretty straight forward...
Now some questions for you to answer:
1. Why does Delphi replace the shl 2 with two adds like that ?!?
Is it maybe faster ? If so why ? Maybe pairing ? Maybe shl/shr conflict with
each other ?
2. Which method would be faster and why ?
3. Also if you have any alternative methods of calculating the same results
let me know !

Bye,
Skybuck.
There is a bit position which needs to be converted to the longword position
in "byte pointer/offset form".
Method 1:
Divide the bit position by 32 (bits) and then to multiply it by 8 (bits).
The division gets rid of the remainder/fraction, the integer part is then
multiplied with 8 (bits) again to get the byte/memory cell offset.
(In other words "bit space is converted to longword space which is then
converted back to byte space")
Method 2:
Zero out bit position value range 0 to 31 by and-ing with the not of bits 0
to 4 set (the first/lower 5 bits).
This gets rid of the remainder as well and then divides it by 8 (bits).
(In other words "bit space is longword-nerved
byte space")
Bit space has intervals of 1 bits.
Longword space has intervals of 32 bits.
Byte space has intervals of 8 bits.
So it kinda makes sense doesn't it
Anyway that's not that the weird part =D
The weird part is what Delphi does, and that's where you guys come in !
Delphi implement method 1 as follows:
TestProgram.dpr.27: mLongwordPosition1 := (mBitPosition shr 5) shl 2; //
delphi does something special here...
00408F5D 8BC3 mov eax,ebx
00408F5F C1E805 shr eax,$05
00408F62 03C0 add eax,eax
00408F64 03C0 add eax,eax
The shift right 5 is done with instruction shr 5.
The shift left 2 is done with two adds ?!?.
A total of 4 instructions (including the initializer (?))
For a total of 9 instruction bytes.
Delphi implement method 2 as follows:
TestProgram.dpr.30: mLongwordPosition2 := (mBitPosition and not
(1+2+4+8+16)) shr 3;
00408F66 8BD3 mov edx,ebx
00408F68 83E2E0 and edx,-$20
00408F6B C1EA03 shr edx,$03
A total of 3 instructions (including the initializer (?))
For a total of 8 instruction bytes.
Pretty straight forward...
Now some questions for you to answer:
1. Why does Delphi replace the shl 2 with two adds like that ?!?
Is it maybe faster ? If so why ? Maybe pairing ? Maybe shl/shr conflict with
each other ?
2. Which method would be faster and why ?
3. Also if you have any alternative methods of calculating the same results
let me know !
Bye,
Skybuck.