From 3994272cdf248f8c8218d4f129186a5b2ec3ca6e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:51:05 +0100 Subject: [PATCH] COPY goes backwards only when the destination starts inside the source, and then by words, not a byte-at-a-time std rep movsb (10M upward copies of 1,000 U32s 10,900 ms to 410) --- diff --git a/conform/run/CopyOverlap.expect b/conform/run/CopyOverlap.expect new file mode 100644 index 0000000..5d39c89 --- /dev/null +++ b/conform/run/CopyOverlap.expect @@ -0,0 +1 @@ +10625 cases, 0 wrong diff --git a/conform/run/CopyOverlap.raz b/conform/run/CopyOverlap.raz new file mode 100644 index 0000000..d8cd600 --- /dev/null +++ b/conform/run/CopyOverlap.raz @@ -0,0 +1,77 @@ +module CopyOverlap; +(* COPY within one array, every pair of places in its first 24 + elements and every length that fits, for elements of 1, 4 and 3 + bytes: the destination inside the source, the source inside it, + the two the same, touching above and below, and apart. Each is + checked against a copy made through a second array. *) +import Out; +const N = 40; Most = 24; +type Rgb = record r, g, b: U8; end; +var bytes, bytesWant: [N]U8; words, wordsWant: [N]U32; rgbs, rgbsWant: [N]Rgb; + cases, wrong: INT; + +(* Says where a copy came out wrong. *) +proc fail(kind: []U8; d, s, n: INT); +begin + Out.str(kind); Out.str(" wrong: d "); Out.int(I64(d)); Out.str(" s "); Out.int(I64(s)); + Out.str(" n "); Out.int(I64(n)); Out.ln; + INC(wrong); +end; + +(* n bytes from s to d. *) +proc tryBytes(d, s, n: INT); +var i: INT; tmp: [N]U8; +begin + for i := 0 to N - 1 do bytes[i] := U8(i + 1); end; + bytesWant := bytes; + for i := 0 to n - 1 do tmp[i] := bytesWant[s + i]; end; + for i := 0 to n - 1 do bytesWant[d + i] := tmp[i]; end; + COPY(bytes[d..d + n], bytes[s..s + n]); + if bytes # bytesWant then fail("U8", d, s, n); end; +end; + +(* n words from s to d. *) +proc tryWords(d, s, n: INT); +var i: INT; tmp: [N]U32; +begin + for i := 0 to N - 1 do words[i] := U32(i * 65537 + 1); end; + wordsWant := words; + for i := 0 to n - 1 do tmp[i] := wordsWant[s + i]; end; + for i := 0 to n - 1 do wordsWant[d + i] := tmp[i]; end; + COPY(words[d..d + n], words[s..s + n]); + if words # wordsWant then fail("U32", d, s, n); end; +end; + +(* n records of 3 bytes from s to d. *) +proc tryRgbs(d, s, n: INT); +var i: INT; tmp: [N]Rgb; +begin + for i := 0 to N - 1 do rgbs[i] := {r: U8(i), g: U8(i + 100), b: U8(i + 200)}; end; + rgbsWant := rgbs; + for i := 0 to n - 1 do tmp[i] := rgbsWant[s + i]; end; + for i := 0 to n - 1 do rgbsWant[d + i] := tmp[i]; end; + COPY(rgbs[d..d + n], rgbs[s..s + n]); + for i := 0 to N - 1 do + if (rgbs[i].r # rgbsWant[i].r) or (rgbs[i].g # rgbsWant[i].g) or (rgbs[i].b # rgbsWant[i].b) then + fail("Rgb", d, s, n); + return; + end; + end; +end; + +proc main*; +var d, s, n: INT; +begin + for d := 0 to Most do + for s := 0 to Most do + for n := 0 to N - Most do + tryBytes(d, s, n); + tryWords(d, s, n); + tryRgbs(d, s, n); + INC(cases); + end; + end; + end; + Out.int(I64(cases)); Out.str(" cases, "); Out.int(I64(wrong)); Out.str(" wrong"); Out.ln; +end; +end CopyOverlap. diff --git a/doc/compiler.md b/doc/compiler.md index 6013c5c..adda2e7 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -20,8 +20,8 @@ | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | | `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | -| `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,666 | 1,885 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler | +| `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,459 | 3,851 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | @@ -1089,8 +1089,8 @@ `notBool`, `narrow` (to a narrow integer's width), the conversions between integers and reals, `compareSeq` (of two slices), and copying: `copyBlock`, `copyTo`, `zeroVar`, and `copySlice` for `COPY`, a `rep - movsb` that goes down when the destination is above the source; the - small ones by moves (`copyBytes`, `zeroBytes`); + movsb`, or a loop going down (`copyDown`) when the destination starts + inside the source; the small ones by moves (`copyBytes`, `zeroBytes`); - jumps: `jump`, `jumpIf` (on a `BOOL`), `jumpCmp` (on two integers), `jumpOut` (`jumpCmp` from within an expression) and `condValue` for `and` and `or`, `fix` where the label is, and `joinJumps`; @@ -1103,6 +1103,51 @@ (`deferBegin`, `deferEnd`, `runDefer`), the built-ins that are instructions (`atomic`, `sysCall`, `fence`), and the program's entry point (`entryPoint`). + +`COPY` must come out right when its two sides overlap (6.8 of the +report). `copySlice` once went backwards whenever the destination was +above the source, overlapping or not: + +``` +cmp rdi, rsi +jbe forwards +lea rsi, [rsi + rcx - 1] ; the last byte of each +lea rdi, [rdi + rcx - 1] +std ; rep movsb runs down +rep movsb +cld +``` + +But a `rep movsb` going down gets none of the processor's fast copying: +it moves a byte at a time. Now only a destination that starts inside +the source, `rsi < rdi < rsi + rcx`, goes backwards, and by a loop of +words from the ends, then one of bytes for the last few +(`copyDown(8)`, `copyDown(1)`); a word is loaded whole before it is +stored, so the loop is right however near the two are: + +``` +cmp rdi, rsi +jbe forwards +lea rax, [rsi + rcx] +cmp rdi, rax +jae forwards ; above, but past the source's end +add rsi, rcx ; one past the ends +add rdi, rcx +words: cmp rcx, 8 +jb bytes +sub rsi, 8 +sub rdi, 8 +sub rcx, 8 +mov rax, [rsi] +mov [rdi], rax +jmp words +bytes: ... ; the same, by 1 +``` + +Ten million copies between the two halves of a `[4096]U32`, upward: +576 ms to 71 at 10 elements, 10,900 to 410 at 1,000, as fast as +downward; and one a single element up inside itself, 539 to 57 and +10,800 to 1,600. A variable's place is an `Adr`: relative to the frame (`bFrame`), to a chunk (`bGlobal`), to an address on the stack (`bStack`), or to one diff --git a/src/Gen.raz b/src/Gen.raz index 3fd070a..55ffecb 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -1843,10 +1843,32 @@ X.store(X.mem(RBP, tmp), RAX, 8); end; +(* The rcx bytes below rsi to those below rdi, n at a time while rcx + holds n or more, through rax, moving rsi, rdi and rcx down: COPY's + backward copy, as a loop, since rep movsb going down (std) moves a + byte at a time. *) +proc copyDown(n: INT); +var top, out: INT; +begin + top := Chunk.pc; + X.aluI(X.aluCmp, RCX, n); + out := NoLabel; + X.jcc(X.ccB, out); + X.aluI(X.aluSub, RSI, n); + X.aluI(X.aluSub, RDI, n); + X.aluI(X.aluSub, RCX, n); + X.load(RAX, X.mem(RSI, 0), n, false); + X.store(X.mem(RDI, 0), RAX, n); + X.jmpBack(top); + X.fixLink(out); +end; + (* COPY: the elements of the slice in the frame at sOff, of size bytes each, to the start of the one at dOff, which must be at least as long - (the source may be shorter), as one block; backwards when the - destination is above the source, so that an overlap comes out right. *) + (the source may be shorter), as one block. It goes forwards, by rep + movsb, but backwards when the destination starts inside the source, + so that an overlap comes out right: by words from the ends, then the + last few bytes one by one. *) proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos); var ml: X.Mem; L, E: INT; begin @@ -1862,16 +1884,16 @@ mulI(RCX, size); X.load(RSI, X.mem(RBP, sOff), 8, false); X.load(RDI, X.mem(RBP, dOff), 8, false); + L := NoLabel; X.alu(X.aluCmp, RDI, RSI); - L := NoLabel; X.jcc(X.ccBE, L); (* the destination is not above: forwards, at L *) - (* backwards: std makes rep movsb run downward, so it starts at the - last byte of each *) - X.lea(RSI, X.memIdx(RSI, RCX, 1, -1)); - X.lea(RDI, X.memIdx(RDI, RCX, 1, -1)); - X.std(); - X.repMovsb(); - X.cld(); + X.lea(RAX, X.memIdx(RSI, RCX, 1, 0)); + X.alu(X.aluCmp, RDI, RAX); + X.jcc(X.ccAE, L); (* nor does it start inside the source *) + X.alu(X.aluAdd, RSI, RCX); (* backwards, from the ends *) + X.alu(X.aluAdd, RDI, RCX); + copyDown(8); + copyDown(1); E := NoLabel; X.jmp(E); X.fixLink(L); diff --git a/src/X64.raz b/src/X64.raz index 744a1a8..477972d 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -692,17 +692,6 @@ say("rep movsb"); Chunk.put1(0F3H); Chunk.put1(0A4H); end; -(* the direction flag, which has rep movsb go down, and back up *) -proc std*; -begin - say("std"); Chunk.put1(0FDH); -end; - -proc cld*; -begin - say("cld"); Chunk.put1(0FCH); -end; - proc repStosb*; begin say("rep stosb"); Chunk.put1(0F3H); Chunk.put1(0AAH);