raz / commit 090fc959

commit
090fc9596e3c23468c9fece5c74738a3d88eeb04
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ed7c3fe4
Reals on the stack in xmm registers

A real on the value stack was held in an integer register and copied
to xmm0 and xmm1 for each operation, and back. Now the value at depth
d is held in xmm8 + d mod 4 (XRegs, RegIn), loaded and stored with
movsd or movss, a constant read from read-only data (RealConst), and
an operation takes its right operand from memory as it is (RealOp).
Call moves a real argument straight to its xmm register. Op2 leaves a
constant on the left of +, *, &, | or xor on top, as their order
doesn't matter, rather than swapping it under the other and back.

Jacobi 251 -> 129 ms (FPC -O2 80), NBody 375 -> 225 (85); rat 462 ->
448 ms, within noise; the integer kernels unchanged. 9,017 lines of
code.

patch · log

5 files changed, +276 -108

PLAN.md (history) modified +5 -4

@@ -24,10 +24,11 @@
2424 and a constant; and `NBody`, bodies under gravity: real variables,
2525 a square root and a division in the inner loop. raz 3.1 and 4.4
2626 times FPC's time.
27-2. **Reals on the stack in xmm registers.** A real on the value stack
28- is held in xmm8 to xmm11, loaded and stored there with `movsd`, a
29- constant read from read-only data, and an operation takes its right
30- operand from memory as it is.
27+2. Done: **Reals on the stack in xmm registers.** A real on the value
28+ stack is held in xmm8 to xmm11, loaded and stored there with
29+ `movsd`, a constant read from read-only data, and an operation takes
30+ its right operand from memory as it is. Jacobi 251 ms to 129, NBody
31+ 375 to 225; rat within noise.
3132 3. **Real variables in xmm registers**, where that pays: measured, and
3233 kept only if it does.
3334

doc/compiler.md (history) modified +67 -6

@@ -14,10 +14,10 @@
1414 | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions |
1515 | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout |
1616 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
17-| `src/x64.pas` | 1,146 | 951 | The instruction encoder, jumps within a procedure, the assembler |
18-| `src/gen.pas` | 2,010 | 1,462 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19-| `src/item.pas` | 1,574 | 1,299 | Items: types, constants, variables, operators, conversions, calls |
20-| `src/parse.pas` | 3,078 | 2,689 | Declarations, statements, expressions, built-ins, recovery, warnings |
17+| `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler |
18+| `src/gen.pas` | 2,093 | 1,522 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19+| `src/item.pas` | 1,579 | 1,302 | Items: types, constants, variables, operators, conversions, calls |
20+| `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings |
2121 | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places |
2222 | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers |
2323 | `src/raz.pas` | 245 | 188 | The command line |
@@ -45,7 +45,8 @@
4545
4646 On x86-64 the stack lives in registers, as in Wirth's compilers
4747 (Niklaus Wirth's Pascal and Oberon compilers): the value at depth `d`,
48-depth 0 being the bottom, is in register `d mod 4`, of `r8` to `r11`.
48+depth 0 being the bottom, is in register `d mod 4`, of `r8` to `r11`,
49+or of `xmm8` to `xmm11` for a real.
4950 A value that needs a register a lower one holds *spills* it: stores it
5051 in a frame slot, to free the register. A constant or an address in the
5152 frame or of a chunk is only noted, until something needs it in a
@@ -429,7 +430,65 @@
429430 fewer, the search 15%, the sum 41%, each now within 2% of FPC
430431 `-O2`'s; rat 2.5% fewer cycles and 6% fewer instructions on its
431432 workload. Every default program is 20 to 25% smaller.
433+
434+## Better code for reals
435+
436+Each change below was measured as v0.8.0's were, on rat and on the
437+kernels, two of which compute with reals: `Jacobi`, a sweep of a grid
438+of `F64`s, each point the mean of its four neighbours, and `NBody`,
439+bodies under gravity, with real variables, a square root and a
440+division in its inner loop.
432441
442+1. **Reals on the stack in xmm registers.** A real on the value stack
443+ was held in an integer register, as its bits, and copied to `xmm0`
444+ and `xmm1` for each operation and back. The sweep's
445+ `0.25 * (s[k - 1] + s[k + 1] + s[k - N] + s[k + N])` was, for each
446+ addition and the product:
447+
448+ ```
449+ mov r10, [r13 + r10 * 8]
450+ mov r9, [r13 + r9 * 8]
451+ movq xmm0, r9
452+ movq xmm1, r10
453+ addsd xmm0, xmm1
454+ movq r9, xmm0
455+ ...
456+ mov r10, r9
457+ mov r9, 4598175219545276416 ; 0.25, as its bits
458+ movq xmm0, r9
459+ movq xmm1, r10
460+ mulsd xmm0, xmm1
461+ movq r9, xmm0
462+ mov [rdi + r8 * 8], r9
463+ ```
464+
465+ Now a real at depth `d` is held in `xmm8` to `xmm11`, in
466+ `XRegs[d mod 4]` (`RegIn`): it is loaded and stored with `movsd`
467+ (`movss` for an `F32`; `XValueTo`, `PopVar`), a constant is read
468+ from 8 bytes of read-only data (`RealConst`), and an operation takes
469+ its right operand from memory as it is, as `Op` does for an integer
470+ (`RealOp`). A constant on the left of `+` or `*` is left on top by
471+ `Op2`, in `item.pas`, as their order doesn't matter, rather than
472+ swapped under the other operand and back:
473+
474+ ```
475+ movsd xmm9, [r13 + r9 * 8]
476+ addsd xmm9, [r13 + r10 * 8]
477+ ...
478+ addsd xmm9, [r13 + r10 * 8]
479+ mulsd xmm9, [literal498]
480+ movsd [rdi + r8 * 8], xmm9
481+ ```
482+
483+ `Call` moves a real argument straight to its `xmm0` to `xmm7`, and
484+ takes a real result from `xmm0`; a comparison is `ucomisd` of the
485+ two registers; a conversion is done in the value's own registers.
486+ The registers from `xmm8` up carry no arguments, so that moving one
487+ argument to its register never overwrites another still to go; all
488+ xmm registers are the caller's to save, so a call spills the reals
489+ under it as it does the integers. Jacobi 251 ms to 129, NBody 375
490+ to 225; rat within noise.
491+
433492 ## diag.pas: messages
434493
435494 Every message the compiler prints is made here. A position, `TPos`, is
@@ -621,7 +680,9 @@
621680 is their index. The value at depth `d` belongs in
622681 `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division
623682 and shifts need, and `rsi` and `rdi`, which copying needs, aren't among
624-them. `Claim` gives a value its register, and `Spill`s the lower one
683+them. A real belongs in `XRegs[d mod 4]`, `xmm8` to `xmm11`, which
684+carry no arguments; each depth has the two registers, and `owner`
685+doesn't tell them apart (`RegIn`, `RegOf`). `Claim` gives a value its register, and `Spill`s the lower one
625686 that holds it, when an expression is deeper than the registers;
626687 `Loaded` brings a value into its register, from its constant, address
627688 or slot; `Place` uses a noted address as the operand itself, so that

src/gen.pas modified +159 -75

@@ -11,9 +11,10 @@
1111
1212 The stack is kept in registers, as in Wirth's compilers (Niklaus
1313 Wirth's Pascal and Oberon compilers): the value at depth d, depth 0
14- being the bottom, is in r8 to r11, register d mod 4. A value that
15- needs a register a lower one holds spills that one: stores it in a
16- frame slot, to free the register. A constant, or an address in the
14+ being the bottom, is in r8 to r11, register d mod 4, or in xmm8 to
15+ xmm11 if it is a real. A value that needs a register a lower one
16+ holds spills that one: stores it in a frame slot, to free the
17+ register. A constant, or an address in the
1718 frame or of a chunk, is only noted until something needs it in a
1819 register, so that it can be an instruction's operand.
1920
@@ -242,6 +243,7 @@
242243 and rdi, which copying needs, aren't among them. }
243244 NumRegs = 4;
244245 Regs: array[0..NumRegs - 1] of longint = (R8, R9, R10, R11);
246+ XRegs: array[0..NumRegs - 1] of longint = (8, 9, 10, 11); { xmm8 to xmm11 }
245247 IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9);
246248
247249 var
@@ -294,12 +296,53 @@
294296 Registers outside the stack, free within one operation: rax, rcx and
295297 rdx for arithmetic; rsi and rdi for copying; and, where the stack holds
296298 nothing in them, r10 for the prologue's zeroing loop and r11 for a call
297- through a procedure value. Reals are kept as their bits in the integer
298- registers, and moved to xmm0 and xmm1 for each operation (RealOp). }
299+ through a procedure value; xmm0 and xmm1 for reals.
299300
301+ A real, an F32 or an F64, is held in xmm8 to xmm11 (XRegs), where the
302+ machine computes with reals, the value at depth d in XRegs[d mod
303+ NumRegs]: so each depth has two registers, and holds its value in the
304+ one of its kind, which owner doesn't tell apart. The xmm registers
305+ from xmm8 up carry no arguments, so that Call can move a real into
306+ xmm0 to xmm7 without overwriting another still to go. }
307+
308+{ The register of kind k for the value at depth i }
309+function RegIn(k: TKind; i: longint): longint;
310+begin
311+ if k in [kF32, kF64] then RegIn := XRegs[i mod NumRegs] else RegIn := Regs[i mod NumRegs];
312+end;
313+
300314 function RegOf(i: longint): longint;
301315 begin
302- RegOf := Regs[i mod NumRegs];
316+ RegOf := RegIn(stack[i].k, i);
317+end;
318+
319+function IsReal(i: longint): boolean;
320+begin
321+ IsReal := stack[i].k in [kF32, kF64];
322+end;
323+
324+{ A real constant, its bits v, as an operand: 8 bytes in read-only
325+ data, as the machine has no instruction that holds one }
326+function RealConst(v: int64): TMem;
327+var c: longint;
328+begin
329+ c := NewChunk(secRodata, -1, -1);
330+ SetData(c, @v, 8, 8);
331+ RealConst := MemRip(c, 0);
332+end;
333+
334+{ The real i into xmm register x, from wherever it is, as ValueTo does
335+ for an integer register }
336+procedure XValueTo(i, x: longint);
337+var single: boolean;
338+begin
339+ single := stack[i].k = kF32;
340+ case stack[i].loc of
341+ lConst: MovsXM(x, RealConst(stack[i].val), single);
342+ lMem: MovsXM(x, stack[i].m, stack[i].size = 4);
343+ lReg: if RegOf(i) <> x then MovX(x, RegOf(i));
344+ else MovsXM(x, Mem(RBP, stack[i].val), false); { spilled, as 8 bytes }
345+ end;
303346 end;
304347
305348 function Loaded(i: longint): longint; forward;
@@ -311,7 +354,8 @@
311354 begin
312355 if stack[i].loc <> lReg then Loaded(i);
313356 s := AllocTemp(8, 8);
314- Store(Mem(RBP, s), RegOf(i), 8);
357+ if IsReal(i) then MovsMX(Mem(RBP, s), RegOf(i), false)
358+ else Store(Mem(RBP, s), RegOf(i), 8);
315359 stack[i].loc := lFrame;
316360 stack[i].val := s;
317361 owner[i mod NumRegs] := -1;
@@ -336,15 +380,15 @@
336380 if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]);
337381 end;
338382
339-{ The register of value i, for it to hold: a lower value in it is
340- spilled. }
341-function Claim(i: longint): longint;
383+{ The register of kind k of value i, for it to hold: a lower value at
384+ its depth's registers is spilled. }
385+function Claim(i: longint; k: TKind): longint;
342386 var r: longint;
343387 begin
344388 r := i mod NumRegs;
345389 if (owner[r] >= 0) and (owner[r] <> i) then Spill(owner[r]);
346390 owner[r] := i;
347- Claim := Regs[r];
391+ Claim := RegIn(k, i);
348392 end;
349393
350394 { The memory at the noted address i, plus off }
@@ -359,8 +403,11 @@
359403 var r: longint;
360404 begin
361405 if stack[i].loc = lInto then exit(stack[i].val);
362- if stack[i].loc <> lReg then begin
363- r := Claim(i);
406+ if (stack[i].loc <> lReg) and IsReal(i) then begin
407+ XValueTo(i, Claim(i, stack[i].k));
408+ stack[i].loc := lReg;
409+ end else if stack[i].loc <> lReg then begin
410+ r := Claim(i, kWord);
364411 case stack[i].loc of
365412 lConst: MovRI(r, stack[i].val);
366413 lHeld: MovRR(r, stack[i].val);
@@ -406,15 +453,21 @@
406453 stack[depth - 1].m := m;
407454 stack[depth - 1].size := size;
408455 stack[depth - 1].signed := signed;
409- if m.index = RegOf(depth - 1) then owner[(depth - 1) mod NumRegs] := depth - 1;
456+ if m.index = Regs[(depth - 1) mod NumRegs] then owner[(depth - 1) mod NumRegs] := depth - 1;
410457 end;
411458
412459 { The register a new value on top is to be computed in }
413460 function Target: longint;
414461 begin
415- Target := Claim(depth);
462+ Target := Claim(depth, kWord);
416463 end;
417464
465+{ The xmm register a new real on top is to be computed in }
466+function XTarget: longint;
467+begin
468+ XTarget := Claim(depth, kF64);
469+end;
470+
418471 { The value just computed in Target goes on top }
419472 procedure Computed(k: TKind);
420473 begin
@@ -446,7 +499,9 @@
446499 lAdr: Lea(r, AdrMem(i, 0));
447500 lIdx: Lea(r, stack[i].m);
448501 lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed);
449- lReg: if RegOf(i) <> r then MovRR(r, RegOf(i));
502+ lReg:
503+ if IsReal(i) then MovqRX(r, RegOf(i))
504+ else if RegOf(i) <> r then MovRR(r, RegOf(i));
450505 else Load(r, Mem(RBP, stack[i].val), 8, false);
451506 end;
452507 end;
@@ -560,7 +615,8 @@
560615 { in the order PushAdr explains }
561616 if a.base = bStack then begin m := Place(a, -1); r := Target end
562617 else begin r := Target; m := Place(a, r) end;
563- Load(r, m, size, signed);
618+ if k in [kF32, kF64] then MovsXM(XTarget, m, size = 4)
619+ else Load(r, m, size, signed);
564620 Computed(k);
565621 end;
566622
@@ -576,7 +632,7 @@
576632 { Pops a value into the variable at a; an address on the stack is under
577633 the value. }
578634 procedure PopVar(const a: TAdr; size: longint);
579-var r: longint; v: int64;
635+var r: longint; v: int64; real: boolean;
580636 begin
581637 if a.base = bReg then begin
582638 if TopConst(v) then MovRI(RegAt(a), v)
@@ -591,13 +647,21 @@
591647 end;
592648 if TopConst(v) then begin r := RAX; MovRI(RAX, v) end
593649 else r := Peek(depth - 1);
650+ real := (stack[depth - 1].loc <> lConst) and IsReal(depth - 1);
594651 Drop;
595- Store(Place(a, RCX), r, size);
652+ if real then MovsMX(Place(a, RCX), r, size = 4)
653+ else Store(Place(a, RCX), r, size);
654+end;
655+
656+{ Register src to dst, an xmm register's if real }
657+procedure MovReg(real: boolean; dst, src: longint);
658+begin
659+ if real then MovX(dst, src) else MovRR(dst, src);
596660 end;
597661
598-{ The two values on top change places. }
662+{ The two values on top, of one kind, change places. }
599663 procedure Swap;
600-var e: TEntry; x, y: longint; rx, ry: boolean;
664+var e: TEntry; x, y, t: longint; rx, ry, real: boolean;
601665 begin
602666 x := depth - 2; y := depth - 1;
603667 { a value noted at an address that uses its register is worked out,
@@ -606,16 +670,18 @@
606670 if stack[y].loc in [lIdx, lMem] then Loaded(y);
607671 rx := stack[x].loc = lReg;
608672 ry := stack[y].loc = lReg;
673+ real := IsReal(y);
674+ if real then t := 0 else t := RAX; { xmm0, or rax }
609675 if rx and ry then begin
610- MovRR(RAX, RegOf(x));
611- MovRR(RegOf(x), RegOf(y));
612- MovRR(RegOf(y), RAX);
676+ MovReg(real, t, RegOf(x));
677+ MovReg(real, RegOf(x), RegOf(y));
678+ MovReg(real, RegOf(y), t);
613679 end else if rx then begin
614680 owner[x mod NumRegs] := -1;
615- MovRR(Claim(y), RegOf(x));
681+ MovReg(real, Claim(y, stack[y].k), RegOf(x));
616682 end else if ry then begin
617683 owner[y mod NumRegs] := -1;
618- MovRR(Claim(x), RegOf(y));
684+ MovReg(real, Claim(x, stack[x].k), RegOf(y));
619685 end;
620686 e := stack[x]; stack[x] := stack[y]; stack[y] := e;
621687 end;
@@ -765,20 +831,33 @@
765831 Operands := Top;
766832 end;
767833
834+{ The two reals on top, the left op the right, worked out in the left's
835+ xmm register. The right is taken from memory where it is, as a
836+ constant, a variable or a spilled value is: mulsd xmm8, [rbp - 16]. }
768837 procedure RealOp(op: TSym; k: TKind);
769-var f: boolean; r, y: longint;
838+var f: boolean; c, x, y: longint; m: TMem;
770839 begin
771840 f := k = kF32;
772- r := Operands(y);
773- MovqXR(0, r);
774- MovqXR(1, y);
775841 case op of
776- sPlus: Sse($58, 0, 1, f); { addsd, or addss for an F32 }
777- sMinus: Sse($5C, 0, 1, f); { subsd, subss }
778- sTimes: Sse($59, 0, 1, f); { mulsd, mulss }
779- sSlash: Sse($5E, 0, 1, f); { divsd, divss }
842+ sPlus: c := $58; { addsd, or addss for an F32 }
843+ sMinus: c := $5C; { subsd, subss }
844+ sTimes: c := $59; { mulsd, mulss }
845+ else c := $5E; { divsd, divss }
846+ end;
847+ if stack[depth - 1].loc in [lConst, lMem, lFrame] then begin
848+ case stack[depth - 1].loc of
849+ lConst: m := RealConst(stack[depth - 1].val);
850+ lMem: m := stack[depth - 1].m;
851+ else m := Mem(RBP, stack[depth - 1].val);
852+ end;
853+ Drop;
854+ SseM(c, Top, m, f);
855+ end else begin
856+ y := Loaded(depth - 1);
857+ Drop;
858+ x := Top;
859+ Sse(c, x, y, f);
780860 end;
781- MovqRX(r, 0);
782861 end;
783862
784863 { The group 1 operation of +, -, &, | and xor }
@@ -859,13 +938,15 @@
859938 const
860939 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
861940 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
862-var f: boolean; r, y: longint; v: int64; m: TMem;
941+var f: boolean; r, x, y: longint; v: int64; m: TMem;
863942 begin
864943 if k in [kF32, kF64] then begin
865944 f := k = kF32;
866- r := Operands(y);
867- MovqXR(0, r);
868- MovqXR(1, y);
945+ y := Loaded(depth - 1);
946+ Drop;
947+ x := Top;
948+ Drop;
949+ r := Target; { the BOOL, in the left's integer register }
869950 { ucomis sets the flags as an unsigned comparison would, but for an
870951 unordered pair (a NaN) it sets zero, parity and carry all. So only
871952 "above" can be tested straight: it is false for NaN, as every
@@ -873,13 +954,14 @@
873954 no parity, as NaN = NaN sets the zero flag, and <> asks for
874955 parity as well. }
875956 case op of
876- sEql: begin Ucomis(0, 1, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end;
877- sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end;
878- sLss: begin Ucomis(1, 0, f); SetCC(ccA, r) end;
879- sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, r) end;
880- sGtr: begin Ucomis(0, 1, f); SetCC(ccA, r) end;
881- sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, r) end;
957+ sEql: begin Ucomis(x, y, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end;
958+ sNeq: begin Ucomis(x, y, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end;
959+ sLss: begin Ucomis(y, x, f); SetCC(ccA, r) end;
960+ sLeq: begin Ucomis(y, x, f); SetCC(ccAE, r) end;
961+ sGtr: begin Ucomis(x, y, f); SetCC(ccA, r) end;
962+ sGeq: begin Ucomis(x, y, f); SetCC(ccAE, r) end;
882963 end;
964+ Computed(kWord);
883965 end else begin
884966 if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end
885967 else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Top; AluM(aluCmp, r, m) end
@@ -889,13 +971,17 @@
889971 stack[depth - 1].k := kWord;
890972 end;
891973
974+{ A real is negated by flipping its sign bit, in rax }
892975 procedure Neg(k: TKind; size: longint; signed: boolean);
893976 var r: longint;
894977 begin
895978 r := Top;
896979 case k of
897- kF32: Btc(r, 31);
898- kF64: Btc(r, 63);
980+ kF32, kF64: begin
981+ MovqRX(RAX, r);
982+ if k = kF32 then Btc(RAX, 31) else Btc(RAX, 63);
983+ MovqXR(r, RAX);
984+ end;
899985 else
900986 Unary(3, r); { neg }
901987 if size < 8 then Extend(r, size, signed);
@@ -927,48 +1013,45 @@
9271013 { The integer on top, as a real of kind k; big if it may be a U64 above
9281014 high(I64), which the machine would take as negative }
9291015 procedure IntToReal(k: TKind; big: boolean);
930-var L1, L2, r: longint; f: boolean;
1016+var L1, L2, r, x: longint; f: boolean;
9311017 begin
9321018 f := k = kF32;
9331019 r := Top;
1020+ x := RegIn(k, depth - 1); { its xmm register }
9341021 if big then begin
9351022 { above high(I64): halve, keeping the low bit, and double }
9361023 L1 := NoLabel; L2 := NoLabel;
9371024 TestRR(r, r);
9381025 Jcc(ccS, L1);
939- Cvtsi2s(0, r, f);
1026+ Cvtsi2s(x, r, f);
9401027 Jmp(L2);
9411028 FixLink(L1);
9421029 MovRR(RCX, r);
9431030 ShiftI(shShr, RCX, 1);
9441031 AluI(aluAnd, r, 1);
9451032 Alu(aluOr, RCX, r);
946- Cvtsi2s(0, RCX, f);
947- Sse($58, 0, 0, f); { adds it to itself: doubles }
1033+ Cvtsi2s(x, RCX, f);
1034+ Sse($58, x, x, f); { adds it to itself: doubles }
9481035 FixLink(L2);
949- end else Cvtsi2s(0, r, f);
950- MovqRX(r, 0);
1036+ end else Cvtsi2s(x, r, f);
9511037 stack[depth - 1].k := k;
9521038 end;
9531039
9541040 { The real of kind k on top, truncated to an integer }
9551041 procedure RealToInt(k: TKind);
956-var r: longint;
1042+var x: longint;
9571043 begin
958- r := Top;
959- MovqXR(0, r);
960- Cvtts2si(r, 0, k = kF32);
1044+ x := Top;
1045+ Cvtts2si(RegIn(kWord, depth - 1), x, k = kF32);
9611046 stack[depth - 1].k := kWord;
9621047 end;
9631048
9641049 { The real of kind k on top, as the other kind }
9651050 procedure RealToReal(k: TKind);
966-var r: longint;
1051+var x: longint;
9671052 begin
968- r := Top;
969- MovqXR(0, r);
970- Sse($5A, 0, 0, k = kF32); { cvtss2sd, or cvtsd2ss }
971- MovqRX(r, 0);
1053+ x := Top;
1054+ Sse($5A, x, x, k = kF32); { cvtss2sd, or cvtsd2ss }
9721055 if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32;
9731056 end;
9741057
@@ -1339,7 +1422,7 @@
13391422 AluM(aluAdd, r, m);
13401423 end;
13411424 if inRax then MovRR(Target, RAX)
1342- else Claim(depth); { the index's register again, now for the address }
1425+ else Claim(depth, kWord); { the index's register again, now for the address }
13431426 Computed(kWord);
13441427 end;
13451428
@@ -1499,8 +1582,7 @@
14991582 for i := 0 to c.n - 1 do
15001583 if not onStack[i] then
15011584 if c.isF[i] then begin
1502- if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val))
1503- else begin ValueTo(first + i, RAX); MovqXR(nf, RAX) end;
1585+ XValueTo(first + i, nf);
15041586 inc(nf);
15051587 end else begin
15061588 if ni < 4 then ValueTo(first + i, IntRegs[ni]) else late[ni] := first + i;
@@ -1532,14 +1614,14 @@
15321614 end else if InTwoRegs(res) then begin
15331615 MovRR(Target, RAX); Computed(kWord);
15341616 MovRR(Target, RDX); Computed(kWord);
1617+ end else if (c.ret = 0) and (res^.form = fReal) then begin
1618+ MovX(XTarget, 0);
1619+ Computed(KindOf(res));
15351620 end else if c.ret = 0 then begin
15361621 r := Target;
1537- if res^.form = fReal then MovqRX(r, 0)
1538- else begin
1539- MovRR(r, RAX);
1540- if res^.form = fBool then Extend(r, 1, false)
1541- else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed);
1542- end;
1622+ MovRR(r, RAX);
1623+ if res^.form = fBool then Extend(r, 1, false)
1624+ else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed);
15431625 Computed(KindOf(res));
15441626 end;
15451627 end;
@@ -1776,7 +1858,7 @@
17761858 register variable, to its register: a slice's second word, the one
17771859 for the slot 8 bytes into its own, to its length's }
17781860 for i := 0 to numIn - 1 do
1779- if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i])
1861+ if inReg[i] < 0 then MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false)
17801862 else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin
17811863 r := inObj[i]^.reg;
17821864 if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2;
@@ -1801,12 +1883,14 @@
18011883 { Pops one of several results, a value of size bytes, into its place
18021884 off bytes into the caller's, behind the hidden pointer. }
18031885 procedure ResultTo(off: int64; size: longint);
1804-var r: longint;
1886+var r: longint; real: boolean;
18051887 begin
18061888 r := Top;
1889+ real := IsReal(depth - 1);
18071890 Drop;
18081891 Load(RCX, Mem(RBP, retSlot), 8, false);
1809- Store(Mem(RCX, off), r, size);
1892+ if real then MovsMX(Mem(RCX, off), r, size = 4)
1893+ else Store(Mem(RCX, off), r, size);
18101894 end;
18111895
18121896 { Pops an address, and copies size bytes from it to the result, off
@@ -1831,7 +1915,7 @@
18311915 PopTo(RDX);
18321916 PopTo(RAX);
18331917 end else begin
1834- if resType^.form = fReal then begin MovqXR(0, Top); Drop end
1918+ if resType^.form = fReal then begin XValueTo(depth - 1, 0); Drop end
18351919 else PopTo(RAX);
18361920 end;
18371921 Jmp(exitL);

src/item.pas modified +7 -2

@@ -1051,9 +1051,14 @@
10511051 exit;
10521052 end;
10531053
1054- { x and y on the stack, x under y; a constant x goes on after y }
1054+ { x and y on the stack, x under y; a constant x goes on after y, and
1055+ is left on top where the order doesn't matter, as gen takes a
1056+ constant best on the right: 0.25 * e is mulsd xmm8, [0.25] }
10551057 Value(y);
1056- if x.mode = mConst then begin Value(x); Swap end;
1058+ if x.mode = mConst then begin
1059+ Value(x);
1060+ if not (op in [sPlus, sTimes, sAmp, sBar, sXor]) or isPtr then Swap;
1061+ end;
10571062
10581063 if t^.form = fReal then begin
10591064 if op in Rel then Compare(op, KindOf(t), false) else gen.Op(op, KindOf(t), 8, false)

src/x64.pas modified +38 -21

@@ -92,10 +92,12 @@
9292 procedure LockXadd(const m: TMem; r: longint);
9393 procedure MovqXR(x, r: longint);
9494 procedure MovqRX(r, x: longint);
95-procedure MovsdXM(x: longint; const m: TMem);
96-procedure MovsdMX(const m: TMem; x: longint);
95+procedure MovX(x, y: longint);
9796 { The real instructions take single: F32 (ss) rather than F64 (sd) }
97+procedure MovsXM(x: longint; const m: TMem; single: boolean);
98+procedure MovsMX(const m: TMem; x: longint; single: boolean);
9899 procedure Sse(op, x, y: longint; single: boolean); { 58 add, 59 mul, 5C sub, 5E div, 5A convert }
100+procedure SseM(op, x: longint; const m: TMem; single: boolean);
99101 procedure Ucomis(x, y: longint; single: boolean);
100102 procedure Cvtsi2s(x, r: longint; single: boolean);
101103 procedure Cvtts2si(r, x: longint; single: boolean);
@@ -621,36 +623,51 @@
621623 OpR($66, true, $7E0F, x, r);
622624 end;
623625
624-procedure MovsdXM(x: longint; const m: TMem);
626+{ movaps: all of xmm y to xmm x, the shortest move between two }
627+procedure MovX(x, y: longint);
625628 begin
626- if listing then Say('movsd ' + XN(x) + ', ' + MN(m));
627- OpM($F2, false, $100F, x, m);
629+ if listing then Say('movaps ' + XN(x) + ', ' + XN(y));
630+ OpR(0, false, $280F, x, y);
628631 end;
629632
630-procedure MovsdMX(const m: TMem; x: longint);
633+const
634+ RealPfx: array[boolean] of longint = ($F2, $F3);
635+ RealEnd: array[boolean] of string[2] = ('sd', 'ss');
636+
637+procedure MovsXM(x: longint; const m: TMem; single: boolean);
631638 begin
632- if listing then Say('movsd ' + MN(m) + ', ' + XN(x));
633- OpM($F2, false, $110F, x, m);
639+ if listing then Say('movs' + RealEnd[single][2] + ' ' + XN(x) + ', ' + MN(m));
640+ OpM(RealPfx[single], false, $100F, x, m);
634641 end;
635642
636-const
637- RealPfx: array[boolean] of longint = ($F2, $F3);
638- RealEnd: array[boolean] of string[2] = ('sd', 'ss');
643+procedure MovsMX(const m: TMem; x: longint; single: boolean);
644+begin
645+ if listing then Say('movs' + RealEnd[single][2] + ' ' + MN(m) + ', ' + XN(x));
646+ OpM(RealPfx[single], false, $110F, x, m);
647+end;
639648
640-procedure Sse(op, x, y: longint; single: boolean);
641-var s: shortstring;
649+function SseName(op: longint; single: boolean): shortstring;
642650 begin
643- if listing then begin
644651 case op of
645- $58: s := 'add' + RealEnd[single];
646- $59: s := 'mul' + RealEnd[single];
647- $5C: s := 'sub' + RealEnd[single];
648- $5E: s := 'div' + RealEnd[single];
649- else s := 'cvt' + RealEnd[single] + '2' + RealEnd[not single];
652+ $58: SseName := 'add' + RealEnd[single];
653+ $59: SseName := 'mul' + RealEnd[single];
654+ $5C: SseName := 'sub' + RealEnd[single];
655+ $5E: SseName := 'div' + RealEnd[single];
656+ else SseName := 'cvt' + RealEnd[single] + '2' + RealEnd[not single];
650657 end;
651- Say(s + ' ' + XN(x) + ', ' + XN(y));
652- end;
658+end;
659+
660+procedure Sse(op, x, y: longint; single: boolean);
661+begin
662+ if listing then Say(SseName(op, single) + ' ' + XN(x) + ', ' + XN(y));
653663 OpR(RealPfx[single], false, $0F or op shl 8, x, y);
664+end;
665+
666+{ The same, its right operand in memory: addsd x, [m] }
667+procedure SseM(op, x: longint; const m: TMem; single: boolean);
668+begin
669+ if listing then Say(SseName(op, single) + ' ' + XN(x) + ', ' + MN(m));
670+ OpM(RealPfx[single], false, $0F or op shl 8, x, m);
654671 end;
655672
656673 procedure Ucomis(x, y: longint; single: boolean);