Calculating longword pointer, which method is faster ?

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.
 
S

Skybuck Flying

I won't be needing this method so often anymore, because of a different
implementation, so no pressure, no hurry at answering the questions...

If no answer at all that just fine too ;)

Bye,
Skybuck :)
 
N

Nick Keighley

The sibject is important so it should be included in the post body
Subject: Calculating longword pointer, which method is faster ?

There is a bit position which needs to be converted to the longword position
in "byte pointer/offset form".

longword is 32 bits, byte is 8 bits?

Isn't this just quotient and remainder? You could do something fancy
with shifts and masks. Is ldiv from stdlib.h any use to you?

Can't you just time the various techniques? The answer is likely
platform dependent.

<snip>
 
N

nedbrek

Hello all,

Skybuck Flying said:
1. Why does Delphi replace the shl 2 with two adds like that ?!?

On the first P4, add was 1/2 cycle (2 dependent adds in 1 clock). On other
machines, adders are often more available than shifters (being cheaper).
Sometimes, shifts cost more than one cycle. Probably, the compiler has a
bias towards P4.

Ned
 
P

Phil Carmody

nedbrek said:
Hello all,



On the first P4, add was 1/2 cycle (2 dependent adds in 1 clock). On other
machines, adders are often more available than shifters (being cheaper).
Sometimes, shifts cost more than one cycle. Probably, the compiler has a
bias towards P4.

There are few things cheaper than a shifter. A barrel shifter's
more expensive, but for stuff like LEA logic, you don't need a
barrel shifter. LEA was the usual replacement for things like
shl 2, I don't know what Delphi was thinking. Of course, on the
First P4s, the LEA logic used the shifter in the ALU, it magically
became less useful again, but that's intel for you.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top