From 71b16e4f7269bbb9205ae609e0b71afc4c31d6a9 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:49:55 +0100 Subject: [PATCH] Shifts for *, div and mod by a power of two idiv costs tens of cycles; a constant 2^k on the right now becomes shl, shr, or for a signed div or mod a bias and sar, so div still truncates towards zero. test/run/Pow2 checks it against idiv and imul. --- diff --git a/doc/compiler.md b/doc/compiler.md index fbaeaba..8b9e5b5 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -12,7 +12,7 @@ | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout | | `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder | -| `src/gen.pas` | 1,421 | 1,247 | Items: loading, storing, copying, frames, calls | +| `src/gen.pas` | 1,443 | 1,264 | Items: loading, storing, copying, frames, calls | | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | @@ -247,7 +247,8 @@ operand (or, for `and` and `or`, emits the test that skips the right one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), and otherwise puts the left operand in `rax`, the right in `rcx`, and -emits the operation. Narrow integers are brought back to their width +emits the operation. A power of two on the right of `*`, `div` or +`mod` becomes shifts (`DivPow2`), since `idiv` is slow. Narrow integers are brought back to their width after each operation (`Norm`), which is how `U8` arithmetic wraps. Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a diff --git a/src/gen.pas b/src/gen.pas index a06e20d..830b3a9 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -701,6 +701,24 @@ Log2 := n; end; +{ rax div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards + zero, so a negative dividend first gets 2^k - 1 added (the bias). } +procedure DivPow2(op: TSym; k: longint; unsign: boolean); +begin + if unsign then begin + if op = sDiv then ShiftI(shShr, RAX, k) + else begin ShiftI(shShl, RAX, 64 - k); ShiftI(shShr, RAX, 64 - k) end; + exit; + end; + MovRR(RCX, RAX); + ShiftI(shSar, RCX, 63); + ShiftI(shShr, RCX, 64 - k); + Alu(aluAdd, RCX, RAX); + ShiftI(shSar, RCX, k); + if op = sDiv then MovRR(RAX, RCX) + else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; +end; + function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; var u: boolean; begin @@ -763,7 +781,7 @@ 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 - xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign, f: boolean; a, b: int64; + xt, yt, t: PType; r: TItem; scale, sh, k: longint; isPtr, unsign, f: boolean; a, b: int64; begin xt := x.typ; yt := y.typ; if y.mode in [mType, mBuiltin, mNone, mCase] then MarkAt(y.pos, 'not a value'); @@ -870,10 +888,14 @@ exit; end; + { a power of two on the right: shifts instead of imul and idiv } + k := -1; + if (y.mode = mConst) and (op in [sTimes, sDiv, sMod]) and (t^.form = fInt) then k := Log2(y.val); + { x into rax, y into rcx } if y.mode = mConst then begin if x.mode = mStack then PopR(RAX) else Value(x); - ConstTo(RCX, y); + if k < 1 then ConstTo(RCX, y); end else begin Value(y); MovRR(RCX, RAX); @@ -931,8 +953,8 @@ else begin MovRI(RCX, scale); Cqo; Unary(7, RCX) end; end; end; - sTimes: Imul(RAX, RCX); - sDiv, sMod: begin + sTimes: if k >= 1 then ShiftI(shShl, RAX, k) else Imul(RAX, RCX); + sDiv, sMod: if k >= 1 then DivPow2(op, k, unsign) else begin if unsign then begin MovRI(RDX, 0); Unary(6, RCX) end else begin Cqo; Unary(7, RCX) end; if op = sMod then MovRR(RAX, RDX); diff --git a/test/run/Pow2.expect b/test/run/Pow2.expect new file mode 100644 index 0000000..c675e6e --- /dev/null +++ b/test/run/Pow2.expect @@ -0,0 +1,2 @@ +-7 div 2 = -3, -7 mod 2 = -1 +0 wrong diff --git a/test/run/Pow2.raz b/test/run/Pow2.raz new file mode 100644 index 0000000..ff907b4 --- /dev/null +++ b/test/run/Pow2.raz @@ -0,0 +1,68 @@ +(* div, mod and * by a power of two are shifts; they must agree with + idiv and imul, which a divisor in a variable still gets. *) +module Pow2; +import Out; + +var bad: I64; + +proc check(s: []U8; x, got, want: I64); +begin + if got # want then + Out.str(s); Out.str(" "); Out.int(x); Out.str(": "); + Out.int(got); Out.str(" # "); Out.int(want); Out.ln; + bad := bad + 1; + end; +end; + +proc i64s(x: I64); +var d1, d2, d8, d62: I64; +begin + d1 := 1; d2 := 2; d8 := 8; d62 := 4000000000000000H; + check("div 1", x, x div 1, x div d1); + check("mod 1", x, x mod 1, x mod d1); + check("div 2", x, x div 2, x div d2); + check("mod 2", x, x mod 2, x mod d2); + check("div 8", x, x div 8, x div d8); + check("mod 8", x, x mod 8, x mod d8); + check("div 2^62", x, x div 4000000000000000H, x div d62); + check("mod 2^62", x, x mod 4000000000000000H, x mod d62); + check("* 8", x, x * 8, x * d8); + check("div -8", x, x div (0 - 8), x div (0 - d8)); +end; + +proc u64s(x: U64); +var d2, d8, d62: U64; +begin + d2 := 2; d8 := 8; d62 := 4000000000000000H; + check("u div 2", I64(x), I64(x div 2), I64(x div d2)); + check("u mod 2", I64(x), I64(x mod 2), I64(x mod d2)); + check("u div 8", I64(x), I64(x div 8), I64(x div d8)); + check("u mod 8", I64(x), I64(x mod 8), I64(x mod d8)); + check("u div 2^62", I64(x), I64(x div 4000000000000000H), I64(x div d62)); + check("u mod 2^62", I64(x), I64(x mod 4000000000000000H), I64(x mod d62)); +end; + +proc main*; +var i: I64; b: I8; d: I8; w: U8; dw: U8; +begin + for i := -300 to 300 do i64s(i); u64s(U64(i)); end; + i64s(07FFFFFFFFFFFFFFFH); i64s(-07FFFFFFFFFFFFFFFH - 1); + i64s(4000000000000001H); i64s(-4000000000000001H); + u64s(0FFFFFFFFFFFFFFFFH); u64s(8000000000000000H); + d := 8; dw := 8; + for i := -128 to 127 do + b := I8(i); w := U8(i + 128); + check("i8 div 8", i, I64(b div 8), I64(b div d)); + check("i8 mod 8", i, I64(b mod 8), I64(b mod d)); + check("i8 * 8", i, I64(b * 8), I64(b * d)); + check("u8 div 8", i, I64(w div 8), I64(w div dw)); + check("u8 mod 8", i, I64(w mod 8), I64(w mod dw)); + check("u8 * 8", i, I64(w * 8), I64(w * dw)); + end; + i := -7; + Out.str("-7 div 2 = "); Out.int(i div 2); + Out.str(", -7 mod 2 = "); Out.int(i mod 2); Out.ln; + Out.int(bad); Out.str(" wrong\n"); +end; + +end Pow2.