Recent

Author Topic: Slow if...then...else x86_64  (Read 2437 times)

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Slow if...then...else x86_64
« on: September 11, 2014, 12:29:05 pm »
Hi! I have a couple of questions regarding the following functions:

Code: [Select]
function q_const(const x,c:INTEGER):INTEGER;
begin
if c=x then  Result:=1 else  result:=0;
end;

function q_var(var x,c:INTEGER):INTEGER;
begin
if c=x then  Result:=1 else  result:=0;
end;


The first thing I'd like to know is:
Both "var" and "const" imply by reference. Should the produced assembly code differ between the two functions?

The second thing I'd like to know is:
Code: [Select]

function q_var_jne(var x,c:integer):integer;assembler;register;
asm
movl (%rsi),%eax
cmpl (%rdi),%eax
jne .Lj6
.Ll2:
movl $1,%eax //RUNS SLOWER
jmp .Lj9
.Lj6:
movl $0,%eax
.Lj9:
end;



function q_var_je(var x,c:integer):integer;assembler;register;
asm
movl (%rsi),%eax
cmpl (%rdi),%eax
je .Lj6
.Ll2:
movl $0,%eax //RUNS SLOWER
jmp .Lj9
.Lj6:
movl $1,%eax
.Lj9:
end;

function q_const_jne(const x,c:integer):integer;assembler;register;
asm
cmpl %edi,%esi
jne .Lj6
.Ll2:
movl $1,%eax //RUNS SLOWER
jmp .Lj9
.Lj6:
movl $0,%eax
.Lj9:
end;

function q_const_je(const x,c:integer):integer;assembler;register;
asm
 cmpl %edi,%esi
 je .Lj6
.Ll2:
 movl $0,%eax //RUNS SLOWER
 jmp .Lj9
.Lj6:
 movl $1,%eax
.Lj9:
end;


Is this the correct behavior when using je/jne? It appears like the code starting the label je/jne points to starts to be executed faster.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7356
Re: Slow if...then...else x86_64
« Reply #1 on: September 11, 2014, 12:48:44 pm »
Afaik what CONST does is architecture dependent. On x86_64 it is not by REF, afaik on x86 it is.

Guaranteed by ref is CONSTREF

See also http://bugs.freepascal.org/view.php?id=26089

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Re: Slow if...then...else x86_64
« Reply #2 on: September 11, 2014, 01:07:56 pm »
I am the reporter of that. I've opened this discussion here because that report will become harder to be read if I add too much useless informations.
Thank you for the "CONSTREF" tip.