The assembler knows SSE2's integer instructions and the bit scans
| @@ -554,7 +554,10 @@ | ||
| 554 | 554 | `OpM` and `OpR`: `Operand` reads a register, a memory operand or a |
| 555 | 555 | constant (by the parser's `ConstExpr`, handed in), and a few tables |
| 556 | 556 | give the operations of a kind their opcodes (`Alus`, `Unaries`, |
| 557 | -`Shifts`, `SseOps`, `Plain`). Labels are chains of jumps, as below. | |
| 557 | +`Shifts`, `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations | |
| 558 | +on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside | |
| 559 | +them, and `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, | |
| 560 | +`lzcnt`, `popcnt`). Labels are chains of jumps, as below. | |
| 558 | 561 | `db` gives bytes it doesn't know. `RegNames` and `CondNames` are the |
| 559 | 562 | names of the registers and conditions. |
| 560 | 563 |
| @@ -890,6 +890,14 @@ | ||
| 890 | 890 | Shifts = ' rol ror rcl rcr shl shr sal sar '; |
| 891 | 891 | SseOps = ' sqrt add mul sub min div max '; |
| 892 | 892 | SseCodes: array[0..6] of longint = ($51, $58, $59, $5C, $5D, $5E, $5F); |
| 893 | + { SSE2 integer instructions that work on 16 bytes at a time: 66 0F code, | |
| 894 | + xmm and xmm or memory (pshufd also takes a byte) } | |
| 895 | + PackedOps = ' punpcklbw punpcklwd pcmpeqb pshufd pand pandn por pxor paddb psubb '; | |
| 896 | + PackedCodes: array[0..9] of longint = ($60, $61, $74, $70, $DB, $DF, $EB, $EF, $FC, $F8); | |
| 897 | + { bit scans and counts: reg, reg or memory, with the prefix and code that tell them } | |
| 898 | + BitOps = ' bsf bsr tzcnt lzcnt popcnt '; | |
| 899 | + BitPfx: array[0..4] of longint = (0, 0, $F3, $F3, $F3); | |
| 900 | + BitCodes: array[0..4] of longint = ($BC0F, $BD0F, $BC0F, $BD0F, $B80F); | |
| 893 | 901 | Plain: array[0..14] of string[7] = ('ret', 'leave', 'syscall', 'cpuid', 'rdtsc', |
| 894 | 902 | 'pause', 'nop', 'ud2', 'int3', 'hlt', 'cqo', 'cdq', 'mfence', 'lfence', 'sfence'); |
| 895 | 903 | PlainOp: array[0..14] of longint = ($C3, $C9, $050F, $A20F, $310F, |
| @@ -1061,6 +1069,27 @@ | ||
| 1061 | 1069 | if (n <> 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; |
| 1062 | 1070 | k := $0F or SseCodes[WordIndex(copy(w, 1, length(w) - 2), SseOps)] shl 8; |
| 1063 | 1071 | RM(RealPfx[w[length(w)] = 's'], false, k, o[0].r, o[1]); |
| 1072 | + end else if WordIndex(w, PackedOps) >= 0 then begin | |
| 1073 | + if (n < 2) or (o[0].kind <> oXmm) or not (o[1].kind in [oXmm, oMem]) then Bad; | |
| 1074 | + k := WordIndex(w, PackedOps); | |
| 1075 | + RM($66, false, $0F or PackedCodes[k] shl 8, o[0].r, o[1]); | |
| 1076 | + if w = 'pshufd' then begin | |
| 1077 | + if (n <> 3) or (o[2].kind <> oImm) then Bad; | |
| 1078 | + Put1(o[2].v); | |
| 1079 | + end else if n <> 2 then Bad; | |
| 1080 | + end else if (w = 'movdqu') or (w = 'movdqa') then begin | |
| 1081 | + { the load is 6F and the store 7F; movdqu is F3, movdqa 66 } | |
| 1082 | + if w = 'movdqu' then k := $F3 else k := $66; | |
| 1083 | + 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]) | |
| 1084 | + else if (n = 2) and (o[0].kind = oMem) and (o[1].kind = oXmm) then RM(k, false, $7F0F, o[1].r, o[0]) | |
| 1085 | + else Bad; | |
| 1086 | + end else if w = 'pmovmskb' then begin | |
| 1087 | + if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind <> oXmm) then Bad; | |
| 1088 | + RM($66, false, $D70F, o[0].r, o[1]); | |
| 1089 | + end else if WordIndex(w, BitOps) >= 0 then begin | |
| 1090 | + if (n <> 2) or (o[0].kind <> oReg) or (o[1].kind = oImm) or (size < 4) then Bad; | |
| 1091 | + k := WordIndex(w, BitOps); | |
| 1092 | + RM(BitPfx[k], wide, BitCodes[k], o[0].r, o[1]); | |
| 1064 | 1093 | end else if (w = 'movsd') or (w = 'movss') then begin |
| 1065 | 1094 | if w = 'movsd' then k := $F2 else k := $F3; |
| 1066 | 1095 | 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]) |
| @@ -0,0 +1,2 @@ | ||
| 1 | +5 20 37 -1 -1 14 | |
| 2 | +48150505 |
| @@ -0,0 +1,75 @@ | ||
| 1 | +module Simd; | |
| 2 | +(* Code procedures with SSE2 and the bit scans: a search sixteen bytes | |
| 3 | + at a time, and the counts of bits *) | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +(* the first place of b in the n bytes at p, or -1: p in rdi, n in rsi, b in dl *) | |
| 7 | +proc index(p: ^U8; n: INT; b: U8): INT; | |
| 8 | +code | |
| 9 | + movd xmm1, edx; | |
| 10 | + punpcklbw xmm1, xmm1; (* b in the low two bytes... *) | |
| 11 | + punpcklwd xmm1, xmm1; (* ...in the low four... *) | |
| 12 | + pshufd xmm1, xmm1, 0; (* ...and in all sixteen *) | |
| 13 | + xor ecx, ecx; | |
| 14 | + blocks: | |
| 15 | + lea rax, [rcx + 16]; | |
| 16 | + cmp rax, rsi; | |
| 17 | + ja tail; | |
| 18 | + movdqu xmm0, [rdi + rcx]; | |
| 19 | + pcmpeqb xmm0, xmm1; | |
| 20 | + pmovmskb eax, xmm0; | |
| 21 | + test eax, eax; | |
| 22 | + jnz found; | |
| 23 | + add rcx, 16; | |
| 24 | + jmp blocks; | |
| 25 | + found: | |
| 26 | + bsf eax, eax; | |
| 27 | + add rax, rcx; | |
| 28 | + ret; | |
| 29 | + tail: | |
| 30 | + cmp rcx, rsi; | |
| 31 | + jae missing; | |
| 32 | + cmp [rdi + rcx], dl; | |
| 33 | + je here; | |
| 34 | + inc rcx; | |
| 35 | + jmp tail; | |
| 36 | + here: | |
| 37 | + mov rax, rcx; | |
| 38 | + ret; | |
| 39 | + missing: | |
| 40 | + mov rax, -1; | |
| 41 | + ret; | |
| 42 | +end index; | |
| 43 | + | |
| 44 | +(* the bits set in x, the places of its lowest and highest, and its | |
| 45 | + leading zeros, packed as count + 100 * (low + 100 * (high + 100 * lead)) *) | |
| 46 | +proc bits(x: U64): I64; | |
| 47 | +code | |
| 48 | + popcnt rax, rdi; | |
| 49 | + tzcnt rcx, rdi; | |
| 50 | + imul rcx, rcx, 100; | |
| 51 | + add rax, rcx; | |
| 52 | + bsr rcx, rdi; | |
| 53 | + imul rcx, rcx, 10000; | |
| 54 | + add rax, rcx; | |
| 55 | + lzcnt rcx, rdi; | |
| 56 | + imul rcx, rcx, 1000000; | |
| 57 | + add rax, rcx; | |
| 58 | + ret; | |
| 59 | +end bits; | |
| 60 | + | |
| 61 | +proc main*; | |
| 62 | +var buf: [40]U8; i: INT; | |
| 63 | +begin | |
| 64 | + for i := 0 to 39 do buf[i] := 'a'; end; | |
| 65 | + buf[37] := 'x'; buf[20] := 'y'; buf[5] := 'z'; | |
| 66 | + Out.int(index(ADR(buf[0]), 40, 'z')); Out.str(" "); | |
| 67 | + Out.int(index(ADR(buf[0]), 40, 'y')); Out.str(" "); | |
| 68 | + Out.int(index(ADR(buf[0]), 40, 'x')); Out.str(" "); | |
| 69 | + Out.int(index(ADR(buf[0]), 40, 'q')); Out.str(" "); | |
| 70 | + Out.int(index(ADR(buf[0]), 0, 'a')); Out.str(" "); | |
| 71 | + Out.int(index(ADR(buf[6]), 15, 'y')); Out.ln; | |
| 72 | + Out.int(bits(0B0A0H)); Out.ln; | |
| 73 | +end; | |
| 74 | + | |
| 75 | +end Simd. |