Discussion:
rep ret (x86-64) ?!
(too old to reply)
Claudio Daffra
2006-03-02 00:25:01 UTC
Permalink
hi all

in this example, guide-line of amd64 suggest to put a "rep ret"
instruction
at the end of the loop.
... They recommend this particular combination to avoid making the ret
istruction
be target of a conditional jump instruction. ...
in this case "ret" is preceded by a "jg" instruction and
in the event the jump condition does not hold (?!) the program "falls
through" (!?) to the return.
According with to AMD ... bla bla bla ...

my question is :

what real happens ?
i have no mean can u explain me ?

regards

claudio



int fact_dw( int x ) #1 fact_dw:
[
int result = 1 ; #2 movl $1,%eax
.l2
do [
result *= x ; #3 imul %edi,%eax
x-- ; #4 decl %edi
] while (x>0) ; #5 testl %edi,%edi
#6 jg .l2

return result ;
Tim Roberts
2006-03-03 06:24:58 UTC
Permalink
Post by Claudio Daffra
in this example, guide-line of amd64 suggest to put a "rep ret"
instruction at the end of the loop.
... They recommend this particular combination to avoid making the ret
istruction be target of a conditional jump instruction. ...
in this case "ret" is preceded by a "jg" instruction and
in the event the jump condition does not hold (?!) the program "falls
through" (!?) to the return.
According with to AMD ... bla bla bla ...
what real happens ?
i have no mean can u explain me ?
You are talking about ultra-micro-optimization here.

Here's the principle. The processor tries to fetch the next few
instructions to be executed, so that it can start the process of decoding
and executing them. It even does this with jump and return instructions,
guessing where the program will head next.

What AMD says here is that, if a ret instruction immediately follows a
conditional jump instruction, their predictor cannot figure out where the
ret instruction is going. The pre-fetching has to stop until the ret
actually executes, and only then will it be able to start looking ahead
again.

The "rep ret" trick apparently works around the problem, and lets the
predictor do its job. The "rep" has no effect on the instruction.
--
- Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Claudio Daffra
2006-03-03 20:53:36 UTC
Permalink
now ok, thx tim.

claudio

Continue reading on narkive:
Loading...