Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-09

generated assembler

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vincent Snijders

Hi,

I am looking at some shootout benchmarks and I have the following question. Look at the following program:

program loop;
{$mode objfpc}
var
   d: array[0..$FFF] of double;
   di: PDouble;
   i: integer;
   c: double;
begin
   c := 125;
   i := low(d);
   repeat
     d[i] := c*c;
     inc(i);
   until i>high(d);
   di := @d[low(d)];
   repeat
     di^ := c*c;
     inc(di);
   until  di>@d[high(d)]
end.

I am interested at the way the until clause is generated.

The compiler creates this assembler file:
# [15] d[i] := c*c;
fldl U_P$LOOP_C
fmul %st,%st
fstpl U_P$LOOP_D(,%edx,8)
fwait
# [16] inc(i);
incl %edx
# [17] until i>high(d);
cmpl $4095,%edx
jng .L9
# [18] di := @d[low(d)];
movl $U_P$LOOP_D,%ecx
.balign 4
.L16:
# [20] di^ := c*c;
fldl U_P$LOOP_C
fmul %st,%st
fstpl (%ecx)
fwait
# [21] inc(di);
addl $8,%ecx
# [22] until di>@d[high(d)]
movl $U_P$LOOP_D+32760,%eax
cmpl %ecx,%eax
jnb .L16

Line 17 generates two assembler instructions. Line 22 generates three assembler instructions. As far as I can see both high(d) and @d[high(d)] are constants, because d is a global variable.

Is it possible that line 22 will generate 2 assembler instructions in the future? Or is this a limitation of the i386 assembler? Or do I make a mistake in my reasoning?

Vincent

Florian Klaempfl
to Vincent Snijders
Vincent Snijders wrote 66 lines
Hi, I am looking at some shootout benchmarks and I have the following question. Look at the following program: program loop; {$mode objfpc} var d: array[0..$FFF] of double; di: PDouble; i: integer; c: double; begin c := 125; i := low(d); repeat d[i] := c*c; inc(i); until i>high(d); di := @d[low(d)]; repeat di^ := c*c; inc(di); until di>@d[high(d)] end. I am interested at the way the until clause is generated. The compiler creates this assembler file: # [15] d[i] := c*c; fldl U_P$LOOP_C fmul %st,%st fstpl U_P$LOOP_D(,%edx,8) fwait # [16] inc(i); incl %edx # [17] until i>high(d); cmpl $4095,%edx jng .L9 # [18] di := @d[low(d)]; movl $U_P$LOOP_D,%ecx .balign 4 .L16: # [20] di^ := c*c; fldl U_P$LOOP_C fmul %st,%st fstpl (%ecx) fwait # [21] inc(di); addl $8,%ecx # [22] until di>@d[high(d)] movl $U_P$LOOP_D+32760,%eax cmpl %ecx,%eax jnb .L16 Line 17 generates two assembler instructions. Line 22 generates three assembler instructions. As far as I can see both high(d) and @d[high(d)] are constants, because d is a global variable. Is it possible that line 22 will generate 2 assembler instructions in the future? Or is this a limitation of the i386 assembler? Or do I make a mistake in my reasoning?

No, you aren't, however, the problem of this benchmark are the inserted fwaits which are necessary to get fpu exceptions at correct lines. The best would be to introduce a switch which prevents the fwaits.

Show quoted text 5 lines
Vincent _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Vincent Snijders
to Florian Klaempfl
Florian Klaempfl wrote 13 lines
> > > Line 17 generates two assembler instructions. Line 22 generates three > assembler instructions. As far as I can see both high(d) and @d[high(d)] > are constants, because d is a global variable. > > Is it possible that line 22 will generate 2 assembler instructions in > the future? Or is this a limitation of the i386 assembler? Or do I make > a mistake in my reasoning? No, you aren't, however, the problem of this benchmark are the inserted fwaits which are necessary to get fpu exceptions at correct lines. The best would be to introduce a switch which prevents the fwaits.

Aha, that explains why explicit subexpression elimination fails too.

using comon expression, less calculations:
	distance:=sqrt(sqr(dx)+sqr(dy)+sqr(dz));
       	mag := dt / (distance*distance*distance);
but this is faster:
	distance:=sqrt(sqr(dx)+sqr(dy)+sqr(dz));
       	mag := dt / (sqrt(sqr(dx)+sqr(dy)+sqr(dz))*(sqr(dx)+sqr(dy)+sqr(dz)));
using comon expression, less calculations:
       b1mag := b1^.mass * mag;
       b2^.vx := b2^.vx + dx * b1mag;
       b2^.vy := b2^.vy + dy * b1mag;
       b2^.vz := b2^.vz + dz * b1mag;
but this is faster:
       b2^.vx := b2^.vx + dx * b1^.mass * mag;
       b2^.vy := b2^.vy + dy * b1^.mass * mag;
       b2^.vz := b2^.vz + dz * b1^.mass * mag;

Vincent

Florian Klaempfl
to Vincent Snijders
Vincent Snijders wrote 18 lines
Florian Klaempfl schreef: >> >> >> Line 17 generates two assembler instructions. Line 22 generates three >> assembler instructions. As far as I can see both high(d) and @d[high(d)] >> are constants, because d is a global variable. >> >> Is it possible that line 22 will generate 2 assembler instructions in >> the future? Or is this a limitation of the i386 assembler? Or do I make >> a mistake in my reasoning? > > No, you aren't, however, the problem of this benchmark are the > inserted fwaits > which are necessary to get fpu exceptions at correct lines. The best > would be to > introduce a switch which prevents the fwaits. Aha, that explains why explicit subexpression elimination fails too.

Yes, an fwait patch would help a lot here :) Even more, because C doesn't insert these fwaits neither.

Show quoted text 23 lines
using comon expression, less calculations: distance:=sqrt(sqr(dx)+sqr(dy)+sqr(dz)); mag := dt / (distance*distance*distance); but this is faster: distance:=sqrt(sqr(dx)+sqr(dy)+sqr(dz)); mag := dt / (sqrt(sqr(dx)+sqr(dy)+sqr(dz))*(sqr(dx)+sqr(dy)+sqr(dz))); using comon expression, less calculations: b1mag := b1^.mass * mag; b2^.vx := b2^.vx + dx * b1mag; b2^.vy := b2^.vy + dy * b1mag; b2^.vz := b2^.vz + dz * b1mag; but this is faster: b2^.vx := b2^.vx + dx * b1^.mass * mag; b2^.vy := b2^.vy + dy * b1^.mass * mag; b2^.vz := b2^.vz + dz * b1^.mass * mag; Vincent _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal