From 090fc9596e3c23468c9fece5c74738a3d88eeb04 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:38:16 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index b9f4720..65162fd 100644 --- a/PLAN.md +++ b/PLAN.md @@ -24,10 +24,11 @@ and a constant; and `NBody`, bodies under gravity: real variables, a square root and a division in the inner loop. raz 3.1 and 4.4 times FPC's time. -2. **Reals on the stack in xmm registers.** A real on the value stack - is held in xmm8 to xmm11, loaded and stored there with `movsd`, a - constant read from read-only data, and an operation takes its right - operand from memory as it is. +2. Done: **Reals on the stack in xmm registers.** A real on the value + stack is held in xmm8 to xmm11, loaded and stored there with + `movsd`, a constant read from read-only data, and an operation takes + its right operand from memory as it is. Jacobi 251 ms to 129, NBody + 375 to 225; rat within noise. 3. **Real variables in xmm registers**, where that pays: measured, and kept only if it does. diff --git a/doc/compiler.md b/doc/compiler.md index b9625b1..708f9fd 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -14,10 +14,10 @@ | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | -| `src/x64.pas` | 1,146 | 951 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 2,010 | 1,462 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,574 | 1,299 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 3,078 | 2,689 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler | +| `src/gen.pas` | 2,093 | 1,522 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/item.pas` | 1,579 | 1,302 | Items: types, constants, variables, operators, conversions, calls | +| `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 245 | 188 | The command line | @@ -45,7 +45,8 @@ On x86-64 the stack lives in registers, as in Wirth's compilers (Niklaus Wirth's Pascal and Oberon compilers): the value at depth `d`, -depth 0 being the bottom, is in register `d mod 4`, of `r8` to `r11`. +depth 0 being the bottom, is in register `d mod 4`, of `r8` to `r11`, +or of `xmm8` to `xmm11` for a real. A value that needs a register a lower one holds *spills* it: stores it in a frame slot, to free the register. A constant or an address in the frame or of a chunk is only noted, until something needs it in a @@ -429,7 +430,65 @@ fewer, the search 15%, the sum 41%, each now within 2% of FPC `-O2`'s; rat 2.5% fewer cycles and 6% fewer instructions on its workload. Every default program is 20 to 25% smaller. + +## Better code for reals + +Each change below was measured as v0.8.0's were, on rat and on the +kernels, two of which compute with reals: `Jacobi`, a sweep of a grid +of `F64`s, each point the mean of its four neighbours, and `NBody`, +bodies under gravity, with real variables, a square root and a +division in its inner loop. +1. **Reals on the stack in xmm registers.** A real on the value stack + was held in an integer register, as its bits, and copied to `xmm0` + and `xmm1` for each operation and back. The sweep's + `0.25 * (s[k - 1] + s[k + 1] + s[k - N] + s[k + N])` was, for each + addition and the product: + + ``` + mov r10, [r13 + r10 * 8] + mov r9, [r13 + r9 * 8] + movq xmm0, r9 + movq xmm1, r10 + addsd xmm0, xmm1 + movq r9, xmm0 + ... + mov r10, r9 + mov r9, 4598175219545276416 ; 0.25, as its bits + movq xmm0, r9 + movq xmm1, r10 + mulsd xmm0, xmm1 + movq r9, xmm0 + mov [rdi + r8 * 8], r9 + ``` + + Now a real at depth `d` is held in `xmm8` to `xmm11`, in + `XRegs[d mod 4]` (`RegIn`): it is loaded and stored with `movsd` + (`movss` for an `F32`; `XValueTo`, `PopVar`), a constant is read + from 8 bytes of read-only data (`RealConst`), and an operation takes + its right operand from memory as it is, as `Op` does for an integer + (`RealOp`). A constant on the left of `+` or `*` is left on top by + `Op2`, in `item.pas`, as their order doesn't matter, rather than + swapped under the other operand and back: + + ``` + movsd xmm9, [r13 + r9 * 8] + addsd xmm9, [r13 + r10 * 8] + ... + addsd xmm9, [r13 + r10 * 8] + mulsd xmm9, [literal498] + movsd [rdi + r8 * 8], xmm9 + ``` + + `Call` moves a real argument straight to its `xmm0` to `xmm7`, and + takes a real result from `xmm0`; a comparison is `ucomisd` of the + two registers; a conversion is done in the value's own registers. + The registers from `xmm8` up carry no arguments, so that moving one + argument to its register never overwrites another still to go; all + xmm registers are the caller's to save, so a call spills the reals + under it as it does the integers. Jacobi 251 ms to 129, NBody 375 + to 225; rat within noise. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is @@ -621,7 +680,9 @@ is their index. The value at depth `d` belongs in `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division and shifts need, and `rsi` and `rdi`, which copying needs, aren't among -them. `Claim` gives a value its register, and `Spill`s the lower one +them. A real belongs in `XRegs[d mod 4]`, `xmm8` to `xmm11`, which +carry no arguments; each depth has the two registers, and `owner` +doesn't tell them apart (`RegIn`, `RegOf`). `Claim` gives a value its register, and `Spill`s the lower one that holds it, when an expression is deeper than the registers; `Loaded` brings a value into its register, from its constant, address or slot; `Place` uses a noted address as the operand itself, so that diff --git a/src/gen.pas b/src/gen.pas index 5f9579a..5e773c6 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -11,9 +11,10 @@ The stack is kept in registers, as in Wirth's compilers (Niklaus Wirth's Pascal and Oberon compilers): the value at depth d, depth 0 - being the bottom, is in r8 to r11, register d mod 4. A value that - needs a register a lower one holds spills that one: stores it in a - frame slot, to free the register. A constant, or an address in the + being the bottom, is in r8 to r11, register d mod 4, or in xmm8 to + xmm11 if it is a real. A value that needs a register a lower one + holds spills that one: stores it in a frame slot, to free the + register. A constant, or an address in the frame or of a chunk, is only noted until something needs it in a register, so that it can be an instruction's operand. @@ -242,6 +243,7 @@ and rdi, which copying needs, aren't among them. } NumRegs = 4; Regs: array[0..NumRegs - 1] of longint = (R8, R9, R10, R11); + XRegs: array[0..NumRegs - 1] of longint = (8, 9, 10, 11); { xmm8 to xmm11 } IntRegs: array[0..5] of longint = (RDI, RSI, RDX, RCX, R8, R9); var @@ -294,12 +296,53 @@ Registers outside the stack, free within one operation: rax, rcx and rdx for arithmetic; rsi and rdi for copying; and, where the stack holds nothing in them, r10 for the prologue's zeroing loop and r11 for a call - through a procedure value. Reals are kept as their bits in the integer - registers, and moved to xmm0 and xmm1 for each operation (RealOp). } + through a procedure value; xmm0 and xmm1 for reals. + A real, an F32 or an F64, is held in xmm8 to xmm11 (XRegs), where the + machine computes with reals, the value at depth d in XRegs[d mod + NumRegs]: so each depth has two registers, and holds its value in the + one of its kind, which owner doesn't tell apart. The xmm registers + from xmm8 up carry no arguments, so that Call can move a real into + xmm0 to xmm7 without overwriting another still to go. } + +{ The register of kind k for the value at depth i } +function RegIn(k: TKind; i: longint): longint; +begin + if k in [kF32, kF64] then RegIn := XRegs[i mod NumRegs] else RegIn := Regs[i mod NumRegs]; +end; + function RegOf(i: longint): longint; begin - RegOf := Regs[i mod NumRegs]; + RegOf := RegIn(stack[i].k, i); +end; + +function IsReal(i: longint): boolean; +begin + IsReal := stack[i].k in [kF32, kF64]; +end; + +{ A real constant, its bits v, as an operand: 8 bytes in read-only + data, as the machine has no instruction that holds one } +function RealConst(v: int64): TMem; +var c: longint; +begin + c := NewChunk(secRodata, -1, -1); + SetData(c, @v, 8, 8); + RealConst := MemRip(c, 0); +end; + +{ The real i into xmm register x, from wherever it is, as ValueTo does + for an integer register } +procedure XValueTo(i, x: longint); +var single: boolean; +begin + single := stack[i].k = kF32; + case stack[i].loc of + lConst: MovsXM(x, RealConst(stack[i].val), single); + lMem: MovsXM(x, stack[i].m, stack[i].size = 4); + lReg: if RegOf(i) <> x then MovX(x, RegOf(i)); + else MovsXM(x, Mem(RBP, stack[i].val), false); { spilled, as 8 bytes } + end; end; function Loaded(i: longint): longint; forward; @@ -311,7 +354,8 @@ begin if stack[i].loc <> lReg then Loaded(i); s := AllocTemp(8, 8); - Store(Mem(RBP, s), RegOf(i), 8); + if IsReal(i) then MovsMX(Mem(RBP, s), RegOf(i), false) + else Store(Mem(RBP, s), RegOf(i), 8); stack[i].loc := lFrame; stack[i].val := s; owner[i mod NumRegs] := -1; @@ -336,15 +380,15 @@ if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]); end; -{ The register of value i, for it to hold: a lower value in it is - spilled. } -function Claim(i: longint): longint; +{ The register of kind k of value i, for it to hold: a lower value at + its depth's registers is spilled. } +function Claim(i: longint; k: TKind): longint; var r: longint; begin r := i mod NumRegs; if (owner[r] >= 0) and (owner[r] <> i) then Spill(owner[r]); owner[r] := i; - Claim := Regs[r]; + Claim := RegIn(k, i); end; { The memory at the noted address i, plus off } @@ -359,8 +403,11 @@ var r: longint; begin if stack[i].loc = lInto then exit(stack[i].val); - if stack[i].loc <> lReg then begin - r := Claim(i); + if (stack[i].loc <> lReg) and IsReal(i) then begin + XValueTo(i, Claim(i, stack[i].k)); + stack[i].loc := lReg; + end else if stack[i].loc <> lReg then begin + r := Claim(i, kWord); case stack[i].loc of lConst: MovRI(r, stack[i].val); lHeld: MovRR(r, stack[i].val); @@ -406,15 +453,21 @@ stack[depth - 1].m := m; stack[depth - 1].size := size; stack[depth - 1].signed := signed; - if m.index = RegOf(depth - 1) then owner[(depth - 1) mod NumRegs] := depth - 1; + if m.index = Regs[(depth - 1) mod NumRegs] then owner[(depth - 1) mod NumRegs] := depth - 1; end; { The register a new value on top is to be computed in } function Target: longint; begin - Target := Claim(depth); + Target := Claim(depth, kWord); end; +{ The xmm register a new real on top is to be computed in } +function XTarget: longint; +begin + XTarget := Claim(depth, kF64); +end; + { The value just computed in Target goes on top } procedure Computed(k: TKind); begin @@ -446,7 +499,9 @@ lAdr: Lea(r, AdrMem(i, 0)); lIdx: Lea(r, stack[i].m); lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); - lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); + lReg: + if IsReal(i) then MovqRX(r, RegOf(i)) + else if RegOf(i) <> r then MovRR(r, RegOf(i)); else Load(r, Mem(RBP, stack[i].val), 8, false); end; end; @@ -560,7 +615,8 @@ { in the order PushAdr explains } if a.base = bStack then begin m := Place(a, -1); r := Target end else begin r := Target; m := Place(a, r) end; - Load(r, m, size, signed); + if k in [kF32, kF64] then MovsXM(XTarget, m, size = 4) + else Load(r, m, size, signed); Computed(k); end; @@ -576,7 +632,7 @@ { Pops a value into the variable at a; an address on the stack is under the value. } procedure PopVar(const a: TAdr; size: longint); -var r: longint; v: int64; +var r: longint; v: int64; real: boolean; begin if a.base = bReg then begin if TopConst(v) then MovRI(RegAt(a), v) @@ -591,13 +647,21 @@ end; if TopConst(v) then begin r := RAX; MovRI(RAX, v) end else r := Peek(depth - 1); + real := (stack[depth - 1].loc <> lConst) and IsReal(depth - 1); Drop; - Store(Place(a, RCX), r, size); + if real then MovsMX(Place(a, RCX), r, size = 4) + else Store(Place(a, RCX), r, size); +end; + +{ Register src to dst, an xmm register's if real } +procedure MovReg(real: boolean; dst, src: longint); +begin + if real then MovX(dst, src) else MovRR(dst, src); end; -{ The two values on top change places. } +{ The two values on top, of one kind, change places. } procedure Swap; -var e: TEntry; x, y: longint; rx, ry: boolean; +var e: TEntry; x, y, t: longint; rx, ry, real: boolean; begin x := depth - 2; y := depth - 1; { a value noted at an address that uses its register is worked out, @@ -606,16 +670,18 @@ if stack[y].loc in [lIdx, lMem] then Loaded(y); rx := stack[x].loc = lReg; ry := stack[y].loc = lReg; + real := IsReal(y); + if real then t := 0 else t := RAX; { xmm0, or rax } if rx and ry then begin - MovRR(RAX, RegOf(x)); - MovRR(RegOf(x), RegOf(y)); - MovRR(RegOf(y), RAX); + MovReg(real, t, RegOf(x)); + MovReg(real, RegOf(x), RegOf(y)); + MovReg(real, RegOf(y), t); end else if rx then begin owner[x mod NumRegs] := -1; - MovRR(Claim(y), RegOf(x)); + MovReg(real, Claim(y, stack[y].k), RegOf(x)); end else if ry then begin owner[y mod NumRegs] := -1; - MovRR(Claim(x), RegOf(y)); + MovReg(real, Claim(x, stack[x].k), RegOf(y)); end; e := stack[x]; stack[x] := stack[y]; stack[y] := e; end; @@ -765,20 +831,33 @@ Operands := Top; end; +{ The two reals on top, the left op the right, worked out in the left's + xmm register. The right is taken from memory where it is, as a + constant, a variable or a spilled value is: mulsd xmm8, [rbp - 16]. } procedure RealOp(op: TSym; k: TKind); -var f: boolean; r, y: longint; +var f: boolean; c, x, y: longint; m: TMem; begin f := k = kF32; - r := Operands(y); - MovqXR(0, r); - MovqXR(1, y); case op of - sPlus: Sse($58, 0, 1, f); { addsd, or addss for an F32 } - sMinus: Sse($5C, 0, 1, f); { subsd, subss } - sTimes: Sse($59, 0, 1, f); { mulsd, mulss } - sSlash: Sse($5E, 0, 1, f); { divsd, divss } + sPlus: c := $58; { addsd, or addss for an F32 } + sMinus: c := $5C; { subsd, subss } + sTimes: c := $59; { mulsd, mulss } + else c := $5E; { divsd, divss } + end; + if stack[depth - 1].loc in [lConst, lMem, lFrame] then begin + case stack[depth - 1].loc of + lConst: m := RealConst(stack[depth - 1].val); + lMem: m := stack[depth - 1].m; + else m := Mem(RBP, stack[depth - 1].val); + end; + Drop; + SseM(c, Top, m, f); + end else begin + y := Loaded(depth - 1); + Drop; + x := Top; + Sse(c, x, y, f); end; - MovqRX(r, 0); end; { The group 1 operation of +, -, &, | and xor } @@ -859,13 +938,15 @@ const CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); -var f: boolean; r, y: longint; v: int64; m: TMem; +var f: boolean; r, x, y: longint; v: int64; m: TMem; begin if k in [kF32, kF64] then begin f := k = kF32; - r := Operands(y); - MovqXR(0, r); - MovqXR(1, y); + y := Loaded(depth - 1); + Drop; + x := Top; + Drop; + r := Target; { the BOOL, in the left's integer register } { ucomis sets the flags as an unsigned comparison would, but for an unordered pair (a NaN) it sets zero, parity and carry all. So only "above" can be tested straight: it is false for NaN, as every @@ -873,13 +954,14 @@ no parity, as NaN = NaN sets the zero flag, and <> asks for parity as well. } case op of - sEql: begin Ucomis(0, 1, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end; - sNeq: begin Ucomis(0, 1, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end; - sLss: begin Ucomis(1, 0, f); SetCC(ccA, r) end; - sLeq: begin Ucomis(1, 0, f); SetCC(ccAE, r) end; - sGtr: begin Ucomis(0, 1, f); SetCC(ccA, r) end; - sGeq: begin Ucomis(0, 1, f); SetCC(ccAE, r) end; + sEql: begin Ucomis(x, y, f); SetCC(ccE, r); SetCC(ccNP, RCX); Alu(aluAnd, r, RCX) end; + sNeq: begin Ucomis(x, y, f); SetCC(ccNE, r); SetCC(ccP, RCX); Alu(aluOr, r, RCX) end; + sLss: begin Ucomis(y, x, f); SetCC(ccA, r) end; + sLeq: begin Ucomis(y, x, f); SetCC(ccAE, r) end; + sGtr: begin Ucomis(x, y, f); SetCC(ccA, r) end; + sGeq: begin Ucomis(x, y, f); SetCC(ccAE, r) end; end; + Computed(kWord); end else begin if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Top; AluM(aluCmp, r, m) end @@ -889,13 +971,17 @@ stack[depth - 1].k := kWord; end; +{ A real is negated by flipping its sign bit, in rax } procedure Neg(k: TKind; size: longint; signed: boolean); var r: longint; begin r := Top; case k of - kF32: Btc(r, 31); - kF64: Btc(r, 63); + kF32, kF64: begin + MovqRX(RAX, r); + if k = kF32 then Btc(RAX, 31) else Btc(RAX, 63); + MovqXR(r, RAX); + end; else Unary(3, r); { neg } if size < 8 then Extend(r, size, signed); @@ -927,48 +1013,45 @@ { The integer on top, as a real of kind k; big if it may be a U64 above high(I64), which the machine would take as negative } procedure IntToReal(k: TKind; big: boolean); -var L1, L2, r: longint; f: boolean; +var L1, L2, r, x: longint; f: boolean; begin f := k = kF32; r := Top; + x := RegIn(k, depth - 1); { its xmm register } if big then begin { above high(I64): halve, keeping the low bit, and double } L1 := NoLabel; L2 := NoLabel; TestRR(r, r); Jcc(ccS, L1); - Cvtsi2s(0, r, f); + Cvtsi2s(x, r, f); Jmp(L2); FixLink(L1); MovRR(RCX, r); ShiftI(shShr, RCX, 1); AluI(aluAnd, r, 1); Alu(aluOr, RCX, r); - Cvtsi2s(0, RCX, f); - Sse($58, 0, 0, f); { adds it to itself: doubles } + Cvtsi2s(x, RCX, f); + Sse($58, x, x, f); { adds it to itself: doubles } FixLink(L2); - end else Cvtsi2s(0, r, f); - MovqRX(r, 0); + end else Cvtsi2s(x, r, f); stack[depth - 1].k := k; end; { The real of kind k on top, truncated to an integer } procedure RealToInt(k: TKind); -var r: longint; +var x: longint; begin - r := Top; - MovqXR(0, r); - Cvtts2si(r, 0, k = kF32); + x := Top; + Cvtts2si(RegIn(kWord, depth - 1), x, k = kF32); stack[depth - 1].k := kWord; end; { The real of kind k on top, as the other kind } procedure RealToReal(k: TKind); -var r: longint; +var x: longint; begin - r := Top; - MovqXR(0, r); - Sse($5A, 0, 0, k = kF32); { cvtss2sd, or cvtsd2ss } - MovqRX(r, 0); + x := Top; + Sse($5A, x, x, k = kF32); { cvtss2sd, or cvtsd2ss } if k = kF32 then stack[depth - 1].k := kF64 else stack[depth - 1].k := kF32; end; @@ -1339,7 +1422,7 @@ AluM(aluAdd, r, m); end; if inRax then MovRR(Target, RAX) - else Claim(depth); { the index's register again, now for the address } + else Claim(depth, kWord); { the index's register again, now for the address } Computed(kWord); end; @@ -1499,8 +1582,7 @@ for i := 0 to c.n - 1 do if not onStack[i] then if c.isF[i] then begin - if stack[first + i].loc = lFrame then MovsdXM(nf, Mem(RBP, stack[first + i].val)) - else begin ValueTo(first + i, RAX); MovqXR(nf, RAX) end; + XValueTo(first + i, nf); inc(nf); end else begin if ni < 4 then ValueTo(first + i, IntRegs[ni]) else late[ni] := first + i; @@ -1532,14 +1614,14 @@ end else if InTwoRegs(res) then begin MovRR(Target, RAX); Computed(kWord); MovRR(Target, RDX); Computed(kWord); + end else if (c.ret = 0) and (res^.form = fReal) then begin + MovX(XTarget, 0); + Computed(KindOf(res)); end else if c.ret = 0 then begin r := Target; - if res^.form = fReal then MovqRX(r, 0) - else begin - MovRR(r, RAX); - if res^.form = fBool then Extend(r, 1, false) - else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed); - end; + MovRR(r, RAX); + if res^.form = fBool then Extend(r, 1, false) + else if (res^.form = fInt) and (res^.size < 8) then Extend(r, res^.size, res^.signed); Computed(KindOf(res)); end; end; @@ -1776,7 +1858,7 @@ register variable, to its register: a slice's second word, the one for the slot 8 bytes into its own, to its length's } for i := 0 to numIn - 1 do - if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i]) + if inReg[i] < 0 then MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false) else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin r := inObj[i]^.reg; if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2; @@ -1801,12 +1883,14 @@ { Pops one of several results, a value of size bytes, into its place off bytes into the caller's, behind the hidden pointer. } procedure ResultTo(off: int64; size: longint); -var r: longint; +var r: longint; real: boolean; begin r := Top; + real := IsReal(depth - 1); Drop; Load(RCX, Mem(RBP, retSlot), 8, false); - Store(Mem(RCX, off), r, size); + if real then MovsMX(Mem(RCX, off), r, size = 4) + else Store(Mem(RCX, off), r, size); end; { Pops an address, and copies size bytes from it to the result, off @@ -1831,7 +1915,7 @@ PopTo(RDX); PopTo(RAX); end else begin - if resType^.form = fReal then begin MovqXR(0, Top); Drop end + if resType^.form = fReal then begin XValueTo(depth - 1, 0); Drop end else PopTo(RAX); end; Jmp(exitL); diff --git a/src/item.pas b/src/item.pas index 360ff33..d69bf11 100644 --- a/src/item.pas +++ b/src/item.pas @@ -1051,9 +1051,14 @@ exit; end; - { x and y on the stack, x under y; a constant x goes on after y } + { x and y on the stack, x under y; a constant x goes on after y, and + is left on top where the order doesn't matter, as gen takes a + constant best on the right: 0.25 * e is mulsd xmm8, [0.25] } Value(y); - if x.mode = mConst then begin Value(x); Swap end; + if x.mode = mConst then begin + Value(x); + if not (op in [sPlus, sTimes, sAmp, sBar, sXor]) or isPtr then Swap; + end; if t^.form = fReal then begin if op in Rel then Compare(op, KindOf(t), false) else gen.Op(op, KindOf(t), 8, false) diff --git a/src/x64.pas b/src/x64.pas index 7d846d7..25e4bfe 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -92,10 +92,12 @@ procedure LockXadd(const m: TMem; r: longint); procedure MovqXR(x, r: longint); procedure MovqRX(r, x: longint); -procedure MovsdXM(x: longint; const m: TMem); -procedure MovsdMX(const m: TMem; x: longint); +procedure MovX(x, y: longint); { The real instructions take single: F32 (ss) rather than F64 (sd) } +procedure MovsXM(x: longint; const m: TMem; single: boolean); +procedure MovsMX(const m: TMem; x: longint; single: boolean); procedure Sse(op, x, y: longint; single: boolean); { 58 add, 59 mul, 5C sub, 5E div, 5A convert } +procedure SseM(op, x: longint; const m: TMem; single: boolean); procedure Ucomis(x, y: longint; single: boolean); procedure Cvtsi2s(x, r: longint; single: boolean); procedure Cvtts2si(r, x: longint; single: boolean); @@ -621,36 +623,51 @@ OpR($66, true, $7E0F, x, r); end; -procedure MovsdXM(x: longint; const m: TMem); +{ movaps: all of xmm y to xmm x, the shortest move between two } +procedure MovX(x, y: longint); begin - if listing then Say('movsd ' + XN(x) + ', ' + MN(m)); - OpM($F2, false, $100F, x, m); + if listing then Say('movaps ' + XN(x) + ', ' + XN(y)); + OpR(0, false, $280F, x, y); end; -procedure MovsdMX(const m: TMem; x: longint); +const + RealPfx: array[boolean] of longint = ($F2, $F3); + RealEnd: array[boolean] of string[2] = ('sd', 'ss'); + +procedure MovsXM(x: longint; const m: TMem; single: boolean); begin - if listing then Say('movsd ' + MN(m) + ', ' + XN(x)); - OpM($F2, false, $110F, x, m); + if listing then Say('movs' + RealEnd[single][2] + ' ' + XN(x) + ', ' + MN(m)); + OpM(RealPfx[single], false, $100F, x, m); end; -const - RealPfx: array[boolean] of longint = ($F2, $F3); - RealEnd: array[boolean] of string[2] = ('sd', 'ss'); +procedure MovsMX(const m: TMem; x: longint; single: boolean); +begin + if listing then Say('movs' + RealEnd[single][2] + ' ' + MN(m) + ', ' + XN(x)); + OpM(RealPfx[single], false, $110F, x, m); +end; -procedure Sse(op, x, y: longint; single: boolean); -var s: shortstring; +function SseName(op: longint; single: boolean): shortstring; begin - if listing then begin case op of - $58: s := 'add' + RealEnd[single]; - $59: s := 'mul' + RealEnd[single]; - $5C: s := 'sub' + RealEnd[single]; - $5E: s := 'div' + RealEnd[single]; - else s := 'cvt' + RealEnd[single] + '2' + RealEnd[not single]; + $58: SseName := 'add' + RealEnd[single]; + $59: SseName := 'mul' + RealEnd[single]; + $5C: SseName := 'sub' + RealEnd[single]; + $5E: SseName := 'div' + RealEnd[single]; + else SseName := 'cvt' + RealEnd[single] + '2' + RealEnd[not single]; end; - Say(s + ' ' + XN(x) + ', ' + XN(y)); - end; +end; + +procedure Sse(op, x, y: longint; single: boolean); +begin + if listing then Say(SseName(op, single) + ' ' + XN(x) + ', ' + XN(y)); OpR(RealPfx[single], false, $0F or op shl 8, x, y); +end; + +{ The same, its right operand in memory: addsd x, [m] } +procedure SseM(op, x: longint; const m: TMem; single: boolean); +begin + if listing then Say(SseName(op, single) + ' ' + XN(x) + ', ' + MN(m)); + OpM(RealPfx[single], false, $0F or op shl 8, x, m); end; procedure Ucomis(x, y: longint; single: boolean);