From b8d1e31395e8f532269bfda7c16fbdefc902f65a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:10:44 +0100 Subject: [PATCH] Shifts for sizes, constants to the right v0.7.0 step 5. An index into a slice, and a slice's bounds, are multiplied by the element's size with a shift where it is a power of two (MulI), one cycle where imul takes three, on the chain of every load through an index. A constant on the left of + * & | xor goes right, where it is an immediate or a shift: 2 * root + 1 is shl, add. compiler.md's worked example is what the compiler makes now. rat: within noise (504 -> 506 ms), text -0.2%; a heap sort and a walk down a list of records 11% faster (116 -> 103 ms). --- diff --git a/doc/compiler.md b/doc/compiler.md index 005bbbe..693223e 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -13,7 +13,7 @@ | `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,653 | 1,284 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/gen.pas` | 1,665 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,493 | 1,247 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 2,957 | 2,609 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | @@ -109,11 +109,8 @@ mov r9,QWORD PTR [rbp-0x10] ; b, at depth 1; the 2 is only noted shl r9,0x1 ; times 2 is a shift add r8,r9 -mov rax,r8 ; the result -jmp ; return -xor eax,eax ; running off the end returns zero -movq xmm0,rax -leave +mov rax,r8 ; return: the result +leave ; no jump to here, as it is next ret ``` @@ -317,8 +314,11 @@ (`JumpOut`), which both ways to its label must find in the same place. `pushed` counts what is on the machine's stack, which a call must align: the return address, in a deferred statement. `Op` takes a -constant on the right as it is, and a power of two on the right of -`*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow. +constant on the right as it is, after moving one on the left of `+`, +`*`, `&`, `|` or `xor` there, and a power of two on the right of `*`, +`div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow; an +element's size multiplies an index by a shift too (`MulI`), as a shift +takes one cycle and `imul` three. Narrow integers are brought back to their width after each operation, which is how `U8` arithmetic wraps; since they are computed in 64 bits, a shift's count is cut to their width first, and `DivTrap` makes the diff --git a/src/gen.pas b/src/gen.pas index 7b8b10f..2fea262 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -548,6 +548,14 @@ Log2 := n; end; +{ r times v, a size: by a shift where v is a power of two, as it + mostly is, since a shift takes one cycle and imul three } +procedure MulI(r: longint; v: int64); +begin + if Log2(v) > 0 then ShiftI(shShl, r, Log2(v)) + else if v <> 1 then ImulI(r, v); +end; + { r compared with v, through rdx if v needs more than 32 bits; with zero by test, which sets the flags as cmp would and is shorter } procedure CmpI(r: longint; v: int64); @@ -637,6 +645,10 @@ begin if k in [kF32, kF64] then begin RealOp(op, k); exit end; bits := size * 8; + { a constant on the left of an operation whose order doesn't matter + goes right, where it can be an immediate or a shift } + if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst) + and (stack[depth - 1].loc <> lConst) then Swap; c := TopConst(v); sh := -1; if c and (op in [sTimes, sDiv, sMod]) then sh := Log2(v); @@ -1096,7 +1108,7 @@ AluM(aluCmp, r, ml); Check(ccB, 0, p, r, ml, 0, -1); end; - if size <> 1 then ImulI(r, size); + MulI(r, size); AluM(aluAdd, r, m); if inRax then MovRR(Target, RAX) else Claim(depth); { the index's register again, now for the address } @@ -1123,7 +1135,7 @@ MovRR(RAX, RDX); Alu(aluSub, RAX, RCX); Store(Mem(RBP, tmp + 8), RAX, 8); - if size <> 1 then ImulI(RCX, size); + MulI(RCX, size); if isArray then Lea(RAX, m) else Load(RAX, m, 8, false); Alu(aluAdd, RAX, RCX); Store(Mem(RBP, tmp), RAX, 8); @@ -1144,7 +1156,7 @@ Check(ccBE, 1, p, RAX, ml, 0, -1); end; MovRR(RCX, RDX); - if size <> 1 then ImulI(RCX, size); + MulI(RCX, size); Load(RSI, Mem(RBP, sOff), 8, false); Load(RDI, Mem(RBP, dOff), 8, false); Alu(aluCmp, RDI, RSI); @@ -1176,7 +1188,7 @@ MovRR(RAX, RDX); Alu(aluSub, RAX, RCX); Store(Mem(RBP, tmp + 8), RAX, 8); - if size <> 1 then ImulI(RCX, size); + MulI(RCX, size); Alu(aluAdd, RCX, RDI); Store(Mem(RBP, tmp), RCX, 8); end;