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.
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | | `src/scan.pas` | 584 | 495 | Symbols, literals, comments, positions | |
| 13 | 13 | | `src/sym.pas` | 510 | 424 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/x64.pas` | 680 | 546 | Code buffer, chunks, fixups, line notes, instruction encoder | |
| 15 | -| `src/gen.pas` | 1,421 | 1,247 | Items: loading, storing, copying, frames, calls | | |
| 15 | +| `src/gen.pas` | 1,443 | 1,264 | Items: loading, storing, copying, frames, calls | | |
| 16 | 16 | | `src/parse.pas` | 1,851 | 1,720 | Declarations, statements, expressions, built-ins | |
| 17 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -247,7 +247,8 @@ | ||
| 247 | 247 | operand (or, for `and` and `or`, emits the test that skips the right |
| 248 | 248 | one). `Op2` checks the types, folds constants (`FoldInt`, `FoldReal`), |
| 249 | 249 | and otherwise puts the left operand in `rax`, the right in `rcx`, and |
| 250 | -emits the operation. Narrow integers are brought back to their width | |
| 250 | +emits the operation. A power of two on the right of `*`, `div` or | |
| 251 | +`mod` becomes shifts (`DivPow2`), since `idiv` is slow. Narrow integers are brought back to their width | |
| 251 | 252 | after each operation (`Norm`), which is how `U8` arithmetic wraps. |
| 252 | 253 | |
| 253 | 254 | Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a |
| @@ -701,6 +701,24 @@ | ||
| 701 | 701 | Log2 := n; |
| 702 | 702 | end; |
| 703 | 703 | |
| 704 | +{ rax div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards | |
| 705 | + zero, so a negative dividend first gets 2^k - 1 added (the bias). } | |
| 706 | +procedure DivPow2(op: TSym; k: longint; unsign: boolean); | |
| 707 | +begin | |
| 708 | + if unsign then begin | |
| 709 | + if op = sDiv then ShiftI(shShr, RAX, k) | |
| 710 | + else begin ShiftI(shShl, RAX, 64 - k); ShiftI(shShr, RAX, 64 - k) end; | |
| 711 | + exit; | |
| 712 | + end; | |
| 713 | + MovRR(RCX, RAX); | |
| 714 | + ShiftI(shSar, RCX, 63); | |
| 715 | + ShiftI(shShr, RCX, 64 - k); | |
| 716 | + Alu(aluAdd, RCX, RAX); | |
| 717 | + ShiftI(shSar, RCX, k); | |
| 718 | + if op = sDiv then MovRR(RAX, RCX) | |
| 719 | + else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; | |
| 720 | +end; | |
| 721 | + | |
| 704 | 722 | function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; |
| 705 | 723 | var u: boolean; |
| 706 | 724 | begin |
| @@ -763,7 +781,7 @@ | ||
| 763 | 781 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 764 | 782 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 765 | 783 | var |
| 766 | - xt, yt, t: PType; r: TItem; scale, sh: longint; isPtr, unsign, f: boolean; a, b: int64; | |
| 784 | + xt, yt, t: PType; r: TItem; scale, sh, k: longint; isPtr, unsign, f: boolean; a, b: int64; | |
| 767 | 785 | begin |
| 768 | 786 | xt := x.typ; yt := y.typ; |
| 769 | 787 | if y.mode in [mType, mBuiltin, mNone, mCase] then MarkAt(y.pos, 'not a value'); |
| @@ -870,10 +888,14 @@ | ||
| 870 | 888 | exit; |
| 871 | 889 | end; |
| 872 | 890 | |
| 891 | + { a power of two on the right: shifts instead of imul and idiv } | |
| 892 | + k := -1; | |
| 893 | + if (y.mode = mConst) and (op in [sTimes, sDiv, sMod]) and (t^.form = fInt) then k := Log2(y.val); | |
| 894 | + | |
| 873 | 895 | { x into rax, y into rcx } |
| 874 | 896 | if y.mode = mConst then begin |
| 875 | 897 | if x.mode = mStack then PopR(RAX) else Value(x); |
| 876 | - ConstTo(RCX, y); | |
| 898 | + if k < 1 then ConstTo(RCX, y); | |
| 877 | 899 | end else begin |
| 878 | 900 | Value(y); |
| 879 | 901 | MovRR(RCX, RAX); |
| @@ -931,8 +953,8 @@ | ||
| 931 | 953 | else begin MovRI(RCX, scale); Cqo; Unary(7, RCX) end; |
| 932 | 954 | end; |
| 933 | 955 | end; |
| 934 | - sTimes: Imul(RAX, RCX); | |
| 935 | - sDiv, sMod: begin | |
| 956 | + sTimes: if k >= 1 then ShiftI(shShl, RAX, k) else Imul(RAX, RCX); | |
| 957 | + sDiv, sMod: if k >= 1 then DivPow2(op, k, unsign) else begin | |
| 936 | 958 | if unsign then begin MovRI(RDX, 0); Unary(6, RCX) end |
| 937 | 959 | else begin Cqo; Unary(7, RCX) end; |
| 938 | 960 | if op = sMod then MovRR(RAX, RDX); |
| @@ -0,0 +1,2 @@ | ||
| 1 | +-7 div 2 = -3, -7 mod 2 = -1 | |
| 2 | +0 wrong |
| @@ -0,0 +1,68 @@ | ||
| 1 | +(* div, mod and * by a power of two are shifts; they must agree with | |
| 2 | + idiv and imul, which a divisor in a variable still gets. *) | |
| 3 | +module Pow2; | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +var bad: I64; | |
| 7 | + | |
| 8 | +proc check(s: []U8; x, got, want: I64); | |
| 9 | +begin | |
| 10 | + if got # want then | |
| 11 | + Out.str(s); Out.str(" "); Out.int(x); Out.str(": "); | |
| 12 | + Out.int(got); Out.str(" # "); Out.int(want); Out.ln; | |
| 13 | + bad := bad + 1; | |
| 14 | + end; | |
| 15 | +end; | |
| 16 | + | |
| 17 | +proc i64s(x: I64); | |
| 18 | +var d1, d2, d8, d62: I64; | |
| 19 | +begin | |
| 20 | + d1 := 1; d2 := 2; d8 := 8; d62 := 4000000000000000H; | |
| 21 | + check("div 1", x, x div 1, x div d1); | |
| 22 | + check("mod 1", x, x mod 1, x mod d1); | |
| 23 | + check("div 2", x, x div 2, x div d2); | |
| 24 | + check("mod 2", x, x mod 2, x mod d2); | |
| 25 | + check("div 8", x, x div 8, x div d8); | |
| 26 | + check("mod 8", x, x mod 8, x mod d8); | |
| 27 | + check("div 2^62", x, x div 4000000000000000H, x div d62); | |
| 28 | + check("mod 2^62", x, x mod 4000000000000000H, x mod d62); | |
| 29 | + check("* 8", x, x * 8, x * d8); | |
| 30 | + check("div -8", x, x div (0 - 8), x div (0 - d8)); | |
| 31 | +end; | |
| 32 | + | |
| 33 | +proc u64s(x: U64); | |
| 34 | +var d2, d8, d62: U64; | |
| 35 | +begin | |
| 36 | + d2 := 2; d8 := 8; d62 := 4000000000000000H; | |
| 37 | + check("u div 2", I64(x), I64(x div 2), I64(x div d2)); | |
| 38 | + check("u mod 2", I64(x), I64(x mod 2), I64(x mod d2)); | |
| 39 | + check("u div 8", I64(x), I64(x div 8), I64(x div d8)); | |
| 40 | + check("u mod 8", I64(x), I64(x mod 8), I64(x mod d8)); | |
| 41 | + check("u div 2^62", I64(x), I64(x div 4000000000000000H), I64(x div d62)); | |
| 42 | + check("u mod 2^62", I64(x), I64(x mod 4000000000000000H), I64(x mod d62)); | |
| 43 | +end; | |
| 44 | + | |
| 45 | +proc main*; | |
| 46 | +var i: I64; b: I8; d: I8; w: U8; dw: U8; | |
| 47 | +begin | |
| 48 | + for i := -300 to 300 do i64s(i); u64s(U64(i)); end; | |
| 49 | + i64s(07FFFFFFFFFFFFFFFH); i64s(-07FFFFFFFFFFFFFFFH - 1); | |
| 50 | + i64s(4000000000000001H); i64s(-4000000000000001H); | |
| 51 | + u64s(0FFFFFFFFFFFFFFFFH); u64s(8000000000000000H); | |
| 52 | + d := 8; dw := 8; | |
| 53 | + for i := -128 to 127 do | |
| 54 | + b := I8(i); w := U8(i + 128); | |
| 55 | + check("i8 div 8", i, I64(b div 8), I64(b div d)); | |
| 56 | + check("i8 mod 8", i, I64(b mod 8), I64(b mod d)); | |
| 57 | + check("i8 * 8", i, I64(b * 8), I64(b * d)); | |
| 58 | + check("u8 div 8", i, I64(w div 8), I64(w div dw)); | |
| 59 | + check("u8 mod 8", i, I64(w mod 8), I64(w mod dw)); | |
| 60 | + check("u8 * 8", i, I64(w * 8), I64(w * dw)); | |
| 61 | + end; | |
| 62 | + i := -7; | |
| 63 | + Out.str("-7 div 2 = "); Out.int(i div 2); | |
| 64 | + Out.str(", -7 mod 2 = "); Out.int(i mod 2); Out.ln; | |
| 65 | + Out.int(bad); Out.str(" wrong\n"); | |
| 66 | +end; | |
| 67 | + | |
| 68 | +end Pow2. |