From b1472f5db8ca2040fa889ca43c8a724ac41f1c24 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:02:28 +0100 Subject: [PATCH] The assembler knows SSE2's integer instructions and the bit scans --- diff --git a/doc/compiler.md b/doc/compiler.md index 4317118..2782e2b 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -554,7 +554,10 @@ `OpM` and `OpR`: `Operand` reads a register, a memory operand or a constant (by the parser's `ConstExpr`, handed in), and a few tables give the operations of a kind their opcodes (`Alus`, `Unaries`, -`Shifts`, `SseOps`, `Plain`). Labels are chains of jumps, as below. +`Shifts`, `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations +on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside +them, and `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, +`lzcnt`, `popcnt`). Labels are chains of jumps, as below. `db` gives bytes it doesn't know. `RegNames` and `CondNames` are the names of the registers and conditions. diff --git a/src/x64.pas b/src/x64.pas index 4502cf3..c6026f4 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -890,6 +890,14 @@ Shifts = ' rol ror rcl rcr shl shr sal sar '; SseOps = ' sqrt add mul sub min div max '; SseCodes: array[0..6] of longint = ($51, $58, $59, $5C, $5D, $5E, $5F); + { SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, + xmm and xmm or memory (pshufd also takes a byte) } + PackedOps = ' punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb '; + PackedCodes: array[0..9] of longint = ($60, $61, $74, $70, $DB, $DF, $EB, $EF, $FC, $F8); + { bit scans and counts: reg, reg or memory, with the prefix and code that tell them } + BitOps = ' bsf bsr tzcnt lzcnt popcnt '; + BitPfx: array[0..4] of longint = (0, 0, $F3, $F3, $F3); + BitCodes: array[0..4] of longint = ($BC0F, $BD0F, $BC0F, $BD0F, $B80F); Plain: array[0..14] of string[7] = ('ret', 'leave', 'syscall', 'cpuid', 'rdtsc', 'pause', 'nop', 'ud2', 'int3', 'hlt', 'cqo', 'cdq', 'mfence', 'lfence', 'sfence'); PlainOp: array[0..14] of longint = ($C3, $C9, $050F, $A20F, $310F, @@ -1061,6 +1069,27 @@ if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; k := $0F or SseCodes[WordIndex(copy(w, 1, length(w) - 2), SseOps)] shl 8; RM(RealPfx[w[length(w)] = 's'], false, k, o[0].r, o[1]); + end else if WordIndex(w, PackedOps) >= 0 then begin + if (n < 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; + k := WordIndex(w, PackedOps); + RM($66, false, $0F or PackedCodes[k] shl 8, o[0].r, o[1]); + if w = 'pshufd' then begin + if (n <> 3) or (o[2].kind <> oImm) then Bad; + Put1(o[2].v); + end else if n <> 2 then Bad; + end else if (w = 'movdqu') or (w = 'movdqa') then begin + { the load is 6F and the store 7F; movdqu is F3, movdqa 66 } + if w = 'movdqu' then k := $F3 else k := $66; + if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oXmm, oMem]) then RM(k, false, $6F0F, o[0].r, o[1]) + else if (n = 2) and (o[0].kind = oMem) and (o[1].kind = oXmm) then RM(k, false, $7F0F, o[1].r, o[0]) + else Bad; + end else if w = 'pmovmskb' then begin + if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind <> oXmm) then Bad; + RM($66, false, $D70F, o[0].r, o[1]); + end else if WordIndex(w, BitOps) >= 0 then begin + if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) or (size < 4) then Bad; + k := WordIndex(w, BitOps); + RM(BitPfx[k], wide, BitCodes[k], o[0].r, o[1]); end else if (w = 'movsd') or (w = 'movss') then begin if w = 'movsd' then k := $F2 else k := $F3; if (n = 2) and (o[0].kind = oXmm) and (o[1].kind in [oXmm, oMem]) then RM(k, false, $100F, o[0].r, o[1]) diff --git a/test/run/Simd.expect b/test/run/Simd.expect new file mode 100644 index 0000000..32acca9 --- /dev/null +++ b/test/run/Simd.expect @@ -0,0 +1,2 @@ +5 20 37 -1 -1 14 +48150505 diff --git a/test/run/Simd.raz b/test/run/Simd.raz new file mode 100644 index 0000000..06e79de --- /dev/null +++ b/test/run/Simd.raz @@ -0,0 +1,75 @@ +module Simd; +(* Code procedures with SSE2 and the bit scans: a search sixteen bytes + at a time, and the counts of bits *) +import Out; + +(* the first place of b in the n bytes at p, or -1: p in rdi, n in rsi, b in dl *) +proc index(p: ^U8; n: INT; b: U8): INT; +code + movd xmm1, edx; + punpcklbw xmm1, xmm1; (* b in the low two bytes... *) + punpcklwd xmm1, xmm1; (* ...in the low four... *) + pshufd xmm1, xmm1, 0; (* ...and in all sixteen *) + xor ecx, ecx; + blocks: + lea rax, [rcx + 16]; + cmp rax, rsi; + ja tail; + movdqu xmm0, [rdi + rcx]; + pcmpeqb xmm0, xmm1; + pmovmskb eax, xmm0; + test eax, eax; + jnz found; + add rcx, 16; + jmp blocks; + found: + bsf eax, eax; + add rax, rcx; + ret; + tail: + cmp rcx, rsi; + jae missing; + cmp [rdi + rcx], dl; + je here; + inc rcx; + jmp tail; + here: + mov rax, rcx; + ret; + missing: + mov rax, -1; + ret; +end index; + +(* the bits set in x, the places of its lowest and highest, and its + leading zeros, packed as count + 100 * (low + 100 * (high + 100 * lead)) *) +proc bits(x: U64): I64; +code + popcnt rax, rdi; + tzcnt rcx, rdi; + imul rcx, rcx, 100; + add rax, rcx; + bsr rcx, rdi; + imul rcx, rcx, 10000; + add rax, rcx; + lzcnt rcx, rdi; + imul rcx, rcx, 1000000; + add rax, rcx; + ret; +end bits; + +proc main*; +var buf: [40]U8; i: INT; +begin + for i := 0 to 39 do buf[i] := 'a'; end; + buf[37] := 'x'; buf[20] := 'y'; buf[5] := 'z'; + Out.int(index(ADR(buf[0]), 40, 'z')); Out.str(" "); + Out.int(index(ADR(buf[0]), 40, 'y')); Out.str(" "); + Out.int(index(ADR(buf[0]), 40, 'x')); Out.str(" "); + Out.int(index(ADR(buf[0]), 40, 'q')); Out.str(" "); + Out.int(index(ADR(buf[0]), 0, 'a')); Out.str(" "); + Out.int(index(ADR(buf[6]), 15, 'y')); Out.ln; + Out.int(bits(0B0A0H)); Out.ln; +end; + +end Simd.