raz / commit 3994272c

commit
3994272cdf248f8c8218d4f129186a5b2ec3ca6e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4b40e972
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)

patch · log

5 files changed, +159 -25

conform/run/CopyOverlap.expect (history) added +1 -0

@@ -0,0 +1 @@
1+10625 cases, 0 wrong

conform/run/CopyOverlap.raz (history) added +77 -0

@@ -0,0 +1,77 @@
1+module CopyOverlap;
2+(* COPY within one array, every pair of places in its first 24
3+ elements and every length that fits, for elements of 1, 4 and 3
4+ bytes: the destination inside the source, the source inside it,
5+ the two the same, touching above and below, and apart. Each is
6+ checked against a copy made through a second array. *)
7+import Out;
8+const N = 40; Most = 24;
9+type Rgb = record r, g, b: U8; end;
10+var bytes, bytesWant: [N]U8; words, wordsWant: [N]U32; rgbs, rgbsWant: [N]Rgb;
11+ cases, wrong: INT;
12+
13+(* Says where a copy came out wrong. *)
14+proc fail(kind: []U8; d, s, n: INT);
15+begin
16+ Out.str(kind); Out.str(" wrong: d "); Out.int(I64(d)); Out.str(" s "); Out.int(I64(s));
17+ Out.str(" n "); Out.int(I64(n)); Out.ln;
18+ INC(wrong);
19+end;
20+
21+(* n bytes from s to d. *)
22+proc tryBytes(d, s, n: INT);
23+var i: INT; tmp: [N]U8;
24+begin
25+ for i := 0 to N - 1 do bytes[i] := U8(i + 1); end;
26+ bytesWant := bytes;
27+ for i := 0 to n - 1 do tmp[i] := bytesWant[s + i]; end;
28+ for i := 0 to n - 1 do bytesWant[d + i] := tmp[i]; end;
29+ COPY(bytes[d..d + n], bytes[s..s + n]);
30+ if bytes # bytesWant then fail("U8", d, s, n); end;
31+end;
32+
33+(* n words from s to d. *)
34+proc tryWords(d, s, n: INT);
35+var i: INT; tmp: [N]U32;
36+begin
37+ for i := 0 to N - 1 do words[i] := U32(i * 65537 + 1); end;
38+ wordsWant := words;
39+ for i := 0 to n - 1 do tmp[i] := wordsWant[s + i]; end;
40+ for i := 0 to n - 1 do wordsWant[d + i] := tmp[i]; end;
41+ COPY(words[d..d + n], words[s..s + n]);
42+ if words # wordsWant then fail("U32", d, s, n); end;
43+end;
44+
45+(* n records of 3 bytes from s to d. *)
46+proc tryRgbs(d, s, n: INT);
47+var i: INT; tmp: [N]Rgb;
48+begin
49+ for i := 0 to N - 1 do rgbs[i] := {r: U8(i), g: U8(i + 100), b: U8(i + 200)}; end;
50+ rgbsWant := rgbs;
51+ for i := 0 to n - 1 do tmp[i] := rgbsWant[s + i]; end;
52+ for i := 0 to n - 1 do rgbsWant[d + i] := tmp[i]; end;
53+ COPY(rgbs[d..d + n], rgbs[s..s + n]);
54+ for i := 0 to N - 1 do
55+ if (rgbs[i].r # rgbsWant[i].r) or (rgbs[i].g # rgbsWant[i].g) or (rgbs[i].b # rgbsWant[i].b) then
56+ fail("Rgb", d, s, n);
57+ return;
58+ end;
59+ end;
60+end;
61+
62+proc main*;
63+var d, s, n: INT;
64+begin
65+ for d := 0 to Most do
66+ for s := 0 to Most do
67+ for n := 0 to N - Most do
68+ tryBytes(d, s, n);
69+ tryWords(d, s, n);
70+ tryRgbs(d, s, n);
71+ INC(cases);
72+ end;
73+ end;
74+ end;
75+ Out.int(I64(cases)); Out.str(" cases, "); Out.int(I64(wrong)); Out.str(" wrong"); Out.ln;
76+end;
77+end CopyOverlap.

doc/compiler.md (history) modified +49 -4

@@ -20,8 +20,8 @@
2020 | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions |
2121 | `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
23-| `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,666 | 1,885 | The stack machine, for x86-64: values, operations, checks, calls, frames |
23+| `src/X64.raz` | 1,594 | 1,310 | The instruction encoder, jumps within a procedure, the assembler |
24+| `src/Gen.raz` | 2,688 | 1,902 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,378 | 1,909 | Items: types, constants, variables, operators, conversions, calls |
2626 | `src/Parse.raz` | 4,459 | 3,851 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
@@ -1089,8 +1089,8 @@
10891089 `notBool`, `narrow` (to a narrow integer's width), the conversions
10901090 between integers and reals, `compareSeq` (of two slices), and copying:
10911091 `copyBlock`, `copyTo`, `zeroVar`, and `copySlice` for `COPY`, a `rep
1092- movsb` that goes down when the destination is above the source; the
1093- small ones by moves (`copyBytes`, `zeroBytes`);
1092+ movsb`, or a loop going down (`copyDown`) when the destination starts
1093+ inside the source; the small ones by moves (`copyBytes`, `zeroBytes`);
10941094 - jumps: `jump`, `jumpIf` (on a `BOOL`), `jumpCmp` (on two integers),
10951095 `jumpOut` (`jumpCmp` from within an expression) and `condValue` for
10961096 `and` and `or`, `fix` where the label is, and `joinJumps`;
@@ -1103,6 +1103,51 @@
11031103 (`deferBegin`, `deferEnd`, `runDefer`), the built-ins that are
11041104 instructions (`atomic`, `sysCall`, `fence`), and the program's entry
11051105 point (`entryPoint`).
1106+
1107+`COPY` must come out right when its two sides overlap (6.8 of the
1108+report). `copySlice` once went backwards whenever the destination was
1109+above the source, overlapping or not:
1110+
1111+```
1112+cmp rdi, rsi
1113+jbe forwards
1114+lea rsi, [rsi + rcx - 1] ; the last byte of each
1115+lea rdi, [rdi + rcx - 1]
1116+std ; rep movsb runs down
1117+rep movsb
1118+cld
1119+```
1120+
1121+But a `rep movsb` going down gets none of the processor's fast copying:
1122+it moves a byte at a time. Now only a destination that starts inside
1123+the source, `rsi < rdi < rsi + rcx`, goes backwards, and by a loop of
1124+words from the ends, then one of bytes for the last few
1125+(`copyDown(8)`, `copyDown(1)`); a word is loaded whole before it is
1126+stored, so the loop is right however near the two are:
1127+
1128+```
1129+cmp rdi, rsi
1130+jbe forwards
1131+lea rax, [rsi + rcx]
1132+cmp rdi, rax
1133+jae forwards ; above, but past the source's end
1134+add rsi, rcx ; one past the ends
1135+add rdi, rcx
1136+words: cmp rcx, 8
1137+jb bytes
1138+sub rsi, 8
1139+sub rdi, 8
1140+sub rcx, 8
1141+mov rax, [rsi]
1142+mov [rdi], rax
1143+jmp words
1144+bytes: ... ; the same, by 1
1145+```
1146+
1147+Ten million copies between the two halves of a `[4096]U32`, upward:
1148+576 ms to 71 at 10 elements, 10,900 to 410 at 1,000, as fast as
1149+downward; and one a single element up inside itself, 539 to 57 and
1150+10,800 to 1,600.
11061151
11071152 A variable's place is an `Adr`: relative to the frame (`bFrame`), to a
11081153 chunk (`bGlobal`), to an address on the stack (`bStack`), or to one

src/Gen.raz modified +32 -10

@@ -1843,10 +1843,32 @@
18431843 X.store(X.mem(RBP, tmp), RAX, 8);
18441844 end;
18451845
1846+(* The rcx bytes below rsi to those below rdi, n at a time while rcx
1847+ holds n or more, through rax, moving rsi, rdi and rcx down: COPY's
1848+ backward copy, as a loop, since rep movsb going down (std) moves a
1849+ byte at a time. *)
1850+proc copyDown(n: INT);
1851+var top, out: INT;
1852+begin
1853+ top := Chunk.pc;
1854+ X.aluI(X.aluCmp, RCX, n);
1855+ out := NoLabel;
1856+ X.jcc(X.ccB, out);
1857+ X.aluI(X.aluSub, RSI, n);
1858+ X.aluI(X.aluSub, RDI, n);
1859+ X.aluI(X.aluSub, RCX, n);
1860+ X.load(RAX, X.mem(RSI, 0), n, false);
1861+ X.store(X.mem(RDI, 0), RAX, n);
1862+ X.jmpBack(top);
1863+ X.fixLink(out);
1864+end;
1865+
18461866 (* COPY: the elements of the slice in the frame at sOff, of size bytes
18471867 each, to the start of the one at dOff, which must be at least as long
1848- (the source may be shorter), as one block; backwards when the
1849- destination is above the source, so that an overlap comes out right. *)
1868+ (the source may be shorter), as one block. It goes forwards, by rep
1869+ movsb, but backwards when the destination starts inside the source,
1870+ so that an overlap comes out right: by words from the ends, then the
1871+ last few bytes one by one. *)
18501872 proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos);
18511873 var ml: X.Mem; L, E: INT;
18521874 begin
@@ -1862,16 +1884,16 @@
18621884 mulI(RCX, size);
18631885 X.load(RSI, X.mem(RBP, sOff), 8, false);
18641886 X.load(RDI, X.mem(RBP, dOff), 8, false);
1887+ L := NoLabel;
18651888 X.alu(X.aluCmp, RDI, RSI);
1866- L := NoLabel;
18671889 X.jcc(X.ccBE, L); (* the destination is not above: forwards, at L *)
1868- (* backwards: std makes rep movsb run downward, so it starts at the
1869- last byte of each *)
1870- X.lea(RSI, X.memIdx(RSI, RCX, 1, -1));
1871- X.lea(RDI, X.memIdx(RDI, RCX, 1, -1));
1872- X.std();
1873- X.repMovsb();
1874- X.cld();
1890+ X.lea(RAX, X.memIdx(RSI, RCX, 1, 0));
1891+ X.alu(X.aluCmp, RDI, RAX);
1892+ X.jcc(X.ccAE, L); (* nor does it start inside the source *)
1893+ X.alu(X.aluAdd, RSI, RCX); (* backwards, from the ends *)
1894+ X.alu(X.aluAdd, RDI, RCX);
1895+ copyDown(8);
1896+ copyDown(1);
18751897 E := NoLabel;
18761898 X.jmp(E);
18771899 X.fixLink(L);

src/X64.raz (history) modified +0 -11

@@ -692,17 +692,6 @@
692692 say("rep movsb"); Chunk.put1(0F3H); Chunk.put1(0A4H);
693693 end;
694694
695-(* the direction flag, which has rep movsb go down, and back up *)
696-proc std*;
697-begin
698- say("std"); Chunk.put1(0FDH);
699-end;
700-
701-proc cld*;
702-begin
703- say("cld"); Chunk.put1(0FCH);
704-end;
705-
706695 proc repStosb*;
707696 begin
708697 say("rep stosb"); Chunk.put1(0F3H); Chunk.put1(0AAH);