| 1 |
//go:build ignore |
| 2 |
// +build ignore |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
. "github.com/mmcloughlin/avo/build" |
| 8 |
. "github.com/mmcloughlin/avo/operand" |
| 9 |
. "github.com/mmcloughlin/avo/reg" |
| 10 |
. "github.com/segmentio/asm/build/internal/x86" |
| 11 |
|
| 12 |
"github.com/segmentio/asm/cpu" |
| 13 |
) |
| 14 |
|
| 15 |
func init() { |
| 16 |
ConstraintExpr("!purego") |
| 17 |
} |
| 18 |
|
| 19 |
func main() { |
| 20 |
TEXT("ValidString", NOSPLIT, "func(s string) bool") |
| 21 |
Doc("ValidString returns true if s contains only ASCII characters.") |
| 22 |
|
| 23 |
p := Mem{Base: Load(Param("s").Base(), GP64())} |
| 24 |
n := Load(Param("s").Len(), GP64()) |
| 25 |
ret, _ := ReturnIndex(0).Resolve() |
| 26 |
|
| 27 |
v := GP32() |
| 28 |
vl := GP32() |
| 29 |
maskG := GP64() |
| 30 |
|
| 31 |
MOVQ(U64(0x8080808080808080), maskG) // maskG = 0x8080808080808080 |
| 32 |
|
| 33 |
CMPQ(n, U8(16)) // if n < 16: |
| 34 |
JB(LabelRef("cmp8")) // goto cmp8 |
| 35 |
JumpIfFeature("init_avx", cpu.AVX2) |
| 36 |
|
| 37 |
Label("cmp8") |
| 38 |
CMPQ(n, U8(8)) // if n < 8: |
| 39 |
JB(LabelRef("cmp4")) // goto cmp4 |
| 40 |
TESTQ(maskG, p) // if (p[0:8] & 0x8080808080808080) != 0: |
| 41 |
JNZ(LabelRef("invalid")) // return false |
| 42 |
ADDQ(U8(8), p.Base) // p += 8 |
| 43 |
SUBQ(U8(8), n) // n -= 8 |
| 44 |
JMP(LabelRef("cmp8")) // loop cmp8 |
| 45 |
|
| 46 |
Label("cmp4") |
| 47 |
CMPQ(n, U8(4)) // if n < 4: |
| 48 |
JB(LabelRef("cmp3")) // goto cmp3 |
| 49 |
TESTL(U32(0x80808080), p) // if (p[0:4] & 0x80808080) != 0: |
| 50 |
JNZ(LabelRef("invalid")) // return false |
| 51 |
ADDQ(U8(4), p.Base) // p += 4 |
| 52 |
SUBQ(U8(4), n) // n -= 4 |
| 53 |
|
| 54 |
Label("cmp3") |
| 55 |
CMPQ(n, U8(3)) // if n < 3: |
| 56 |
JB(LabelRef("cmp2")) // goto cmp2 |
| 57 |
MOVWLZX(p, vl) // vl = p[i:i+2] |
| 58 |
MOVBLZX(p.Offset(2), v) // v = p[i+2:i+3] |
| 59 |
SHLL(U8(16), v) // v <<= 16 |
| 60 |
ORL(vl, v) // v = vl | v |
| 61 |
TESTL(U32(0x80808080), v) // ZF = (v & 0x80808080) == 0 |
| 62 |
JMP(LabelRef("done")) // return ZF |
| 63 |
|
| 64 |
Label("cmp2") |
| 65 |
CMPQ(n, U8(2)) // if n < 2: |
| 66 |
JB(LabelRef("cmp1")) // goto cmp1 |
| 67 |
TESTW(U16(0x8080), p) // ZF = (p[0:2] & 0x8080) == 0 |
| 68 |
JMP(LabelRef("done")) // return ZF |
| 69 |
|
| 70 |
Label("cmp1") |
| 71 |
CMPQ(n, U8(0)) // if n == 0: |
| 72 |
JE(LabelRef("done")) // return true |
| 73 |
TESTB(U8(0x80), p) // ZF = (p[0:1] & 0x80) == 0 |
| 74 |
|
| 75 |
Label("done") |
| 76 |
SETEQ(ret.Addr) // return ZF |
| 77 |
RET() // ... |
| 78 |
|
| 79 |
Label("invalid") |
| 80 |
MOVB(U8(0), ret.Addr) |
| 81 |
RET() |
| 82 |
|
| 83 |
Label("init_avx") |
| 84 |
|
| 85 |
maskY := VecBroadcast(maskG, YMM()) |
| 86 |
maskX := maskY.(Vec).AsX() |
| 87 |
|
| 88 |
vec := NewVectorizer(15, func(l VectorLane) Register { |
| 89 |
r := l.Alloc() |
| 90 |
VMOVDQU(l.Offset(p), r) |
| 91 |
VPOR(l.Offset(p), r, r) |
| 92 |
return r |
| 93 |
}).Reduce(ReduceOr) |
| 94 |
|
| 95 |
Label("cmp256") |
| 96 |
CMPQ(n, U32(256)) // if n < 256: |
| 97 |
JB(LabelRef("cmp128")) // goto cmp128 |
| 98 |
VPTEST(vec.Compile(S256, 4)[0], maskY) // if (OR & maskY) != 0: |
| 99 |
JNZ(LabelRef("invalid")) // return false |
| 100 |
ADDQ(U32(256), p.Base) // p += 256 |
| 101 |
SUBQ(U32(256), n) // n -= 256 |
| 102 |
JMP(LabelRef("cmp256")) // loop cmp256 |
| 103 |
|
| 104 |
Label("cmp128") |
| 105 |
CMPQ(n, U8(128)) // if n < 128: |
| 106 |
JB(LabelRef("cmp64")) // goto cmp64 |
| 107 |
VPTEST(vec.Compile(S256, 2)[0], maskY) // if (OR & maskY) != 0: |
| 108 |
JNZ(LabelRef("invalid")) // return false |
| 109 |
ADDQ(U8(128), p.Base) // p += 128 |
| 110 |
SUBQ(U8(128), n) // n -= 128 |
| 111 |
JMP(LabelRef("cmp64")) // goto cmp64 |
| 112 |
|
| 113 |
Label("cmp64") |
| 114 |
CMPQ(n, U8(64)) // if n < 64: |
| 115 |
JB(LabelRef("cmp32")) // goto cmp32 |
| 116 |
VPTEST(vec.Compile(S256, 1)[0], maskY) // if (OR & maskY) != 0: |
| 117 |
JNZ(LabelRef("invalid")) // return false |
| 118 |
ADDQ(U8(64), p.Base) // p += 64 |
| 119 |
SUBQ(U8(64), n) // n -= 64 |
| 120 |
|
| 121 |
Label("cmp32") |
| 122 |
CMPQ(n, U8(32)) // if n < 32: |
| 123 |
JB(LabelRef("cmp16")) // goto cmp16 |
| 124 |
VPTEST(p, maskY) // if (p[0:32] & maskY) != 0: |
| 125 |
JNZ(LabelRef("invalid")) // return false |
| 126 |
ADDQ(U8(32), p.Base) // p += 32 |
| 127 |
SUBQ(U8(32), n) // n -= 32 |
| 128 |
|
| 129 |
Label("cmp16") |
| 130 |
CMPQ(n, U8(16)) // if n <= 16: |
| 131 |
JLE(LabelRef("cmp_tail")) // goto cmp_tail |
| 132 |
VPTEST(p, maskX) // if (p[0:16] & maskX) != 0: |
| 133 |
JNZ(LabelRef("invalid")) // return false |
| 134 |
ADDQ(U8(16), p.Base) // p += 16 |
| 135 |
SUBQ(U8(16), n) // n -= 16 |
| 136 |
|
| 137 |
Label("cmp_tail") |
| 138 |
// At this point, we have <= 16 bytes to compare, but we know the total input |
| 139 |
// is >= 16 bytes. Move the pointer to the *last* 16 bytes of the input so we |
| 140 |
// can skip the fallback. |
| 141 |
SUBQ(Imm(16), n) // n -= 16 |
| 142 |
ADDQ(n, p.Base) // p += n |
| 143 |
VPTEST(p, maskX) // ZF = (p[0:16] & maskX) == 0 |
| 144 |
JMP(LabelRef("done")) // return ZF |
| 145 |
|
| 146 |
Generate() |
| 147 |
} |