C++Guns – RoboBlog

10.05.2018

C++ Guns: schlecht generiertes Assember von Pascal Code

Filed under: Allgemein — Tags: — Thomas @ 17:05

Das selbe Beispiel nochmal mit Pascal Code. Für ein halbwegs vernünftiges Ergebnis musste ich Optimierung O2 auswählen und von Hand inline einschalten. Dead Code elimination funktioniert nicht. Die wesentliche Subroutine hat fünf Subtraktionen, zwei Multiplikationen, elf MOVs, ein LEA und der Stack wird genutzt. Auch zum übergeben der Funktionsparameter. Zur Erinnerung: Wir brauchen fünf Subtraktionen, zwei Multiplikationen und vier explizite Kopier-Befehle (MOV) in C++.
Besser wird es nicht.

unit output;
interface
implementation

type
  Point2D = record
    x: double;
    y: double;
  end;

type 
  Line2D = record
    pt1: Point2D;
    pt2: Point2D;
  end;

Operator -(p1: Point2D; p2: Point2D) res: Point2D inline ;  
begin  
  res.x := p1.x-p2.x;  
  res.y := p1.y-p2.y;  
end; 

  function func(line1: Line2D; line2: Line2D) : double;
var   
    denominator: double; 
    a: Point2D;
    b: Point2D;
begin
    a := line1.pt2 - line1.pt1;
    b := line2.pt1 - line2.pt2;
    denominator := a.y * b.x - a.x * b.y;
    exit(denominator)
end;
  
end.

func(line2d,line2d):
  pushq %rbp
  movq %rsp,%rbp
  leaq -48(%rsp),%rsp
  movsd 32(%rbp),%xmm0
  subsd 16(%rbp),%xmm0
  movsd %xmm0,-16(%rbp)
  movsd 40(%rbp),%xmm0
  subsd 24(%rbp),%xmm0
  movsd %xmm0,-8(%rbp)
  movsd 48(%rbp),%xmm0
  subsd 64(%rbp),%xmm0
  movsd %xmm0,-32(%rbp)
  movsd 56(%rbp),%xmm0
  subsd 72(%rbp),%xmm0
  movsd %xmm0,-24(%rbp)
  movsd -8(%rbp),%xmm0
  mulsd -32(%rbp),%xmm0
  movsd -16(%rbp),%xmm1
  mulsd -24(%rbp),%xmm1
  subsd %xmm1,%xmm0
  leave
  ret

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress