| 1 |
//go:build ignore |
| 2 |
// +build ignore |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"math" |
| 9 |
|
| 10 |
. "github.com/mmcloughlin/avo/build" |
| 11 |
. "github.com/mmcloughlin/avo/operand" |
| 12 |
. "github.com/mmcloughlin/avo/reg" |
| 13 |
. "github.com/segmentio/asm/build/internal/asm" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
unroll = 4 |
| 18 |
pageSize = 4096 |
| 19 |
maxLength = 16 |
| 20 |
) |
| 21 |
|
| 22 |
func init() { |
| 23 |
ConstraintExpr("!purego") |
| 24 |
} |
| 25 |
|
| 26 |
func main() { |
| 27 |
Lookup() |
| 28 |
|
| 29 |
Generate() |
| 30 |
} |
| 31 |
|
| 32 |
// Lookup searches for a key in a set of keys. |
| 33 |
// |
| 34 |
// Each key in the set of keys should be padded to 16 bytes and concatenated |
| 35 |
// into a single buffer. The routine searches for the input key in the set of |
| 36 |
// keys and returns its index if found. If not found, the routine returns the |
| 37 |
// number of keys (len(keyset)/16). |
| 38 |
func Lookup() { |
| 39 |
TEXT("Lookup", NOSPLIT, "func(keyset []byte, key []byte) int") |
| 40 |
Doc("Lookup searches for a key in a set of keys, returning its index if ", |
| 41 |
"found. If the key cannot be found, the number of keys is returned.") |
| 42 |
|
| 43 |
// Load inputs. |
| 44 |
keyset := Load(Param("keyset").Base(), GP64()) |
| 45 |
count := Load(Param("keyset").Len(), GP64()) |
| 46 |
SHRQ(Imm(4), count) |
| 47 |
keyPtr := Load(Param("key").Base(), GP64()) |
| 48 |
keyLen := Load(Param("key").Len(), GP64()) |
| 49 |
keyCap := Load(Param("key").Cap(), GP64()) |
| 50 |
|
| 51 |
// None of the keys are larger than maxLength. |
| 52 |
CMPQ(keyLen, Imm(maxLength)) |
| 53 |
JA(LabelRef("not_found")) |
| 54 |
|
| 55 |
// We're going to be unconditionally loading 16 bytes from the input key |
| 56 |
// so first check if it's safe to do so (cap >= 16). If not, defer to |
| 57 |
// safe_load for additional checks. |
| 58 |
CMPQ(keyCap, Imm(maxLength)) |
| 59 |
JB(LabelRef("safe_load")) |
| 60 |
|
| 61 |
// Load the input key and pad with zeroes to 16 bytes. |
| 62 |
Label("load") |
| 63 |
key := XMM() |
| 64 |
VMOVUPS(Mem{Base: keyPtr}, key) |
| 65 |
Label("prepare") |
| 66 |
zeroes := XMM() |
| 67 |
VPXOR(zeroes, zeroes, zeroes) |
| 68 |
ones := XMM() |
| 69 |
VPCMPEQB(ones, ones, ones) |
| 70 |
var blendBytes [maxLength * 2]byte |
| 71 |
for j := 0; j < maxLength; j++ { |
| 72 |
blendBytes[j] = 0xFF |
| 73 |
} |
| 74 |
blendMasks := ConstBytes("blend_masks", blendBytes[:]) |
| 75 |
blendMasksPtr := GP64() |
| 76 |
LEAQ(blendMasks.Offset(maxLength), blendMasksPtr) |
| 77 |
SUBQ(keyLen, blendMasksPtr) |
| 78 |
blend := XMM() |
| 79 |
VMOVUPS(Mem{Base: blendMasksPtr}, blend) |
| 80 |
VPBLENDVB(blend, key, zeroes, key) |
| 81 |
|
| 82 |
// Zero out i so we can use it as the loop increment. |
| 83 |
i := GP64() |
| 84 |
XORQ(i, i) |
| 85 |
|
| 86 |
// Round the key count down to the nearest multiple of unroll to determine |
| 87 |
// how many iterations of the big loop we'll need. |
| 88 |
truncatedCount := GP64() |
| 89 |
MOVQ(count, truncatedCount) |
| 90 |
shift := uint64(math.Log2(float64(unroll))) |
| 91 |
SHRQ(Imm(shift), truncatedCount) |
| 92 |
SHLQ(Imm(shift), truncatedCount) |
| 93 |
|
| 94 |
// Loop over multiple keys in the big loop. |
| 95 |
Label("bigloop") |
| 96 |
CMPQ(i, truncatedCount) |
| 97 |
JE(LabelRef("loop")) |
| 98 |
|
| 99 |
x := []VecPhysical{X8, X9, X10, X11, X12, X13, X14, X15} |
| 100 |
for n := 0; n < unroll; n++ { |
| 101 |
VPCMPEQB(Mem{Base: keyset, Disp: maxLength * n}, key, x[n]) |
| 102 |
VPTEST(ones, x[n]) |
| 103 |
var target string |
| 104 |
if n == 0 { |
| 105 |
target = "done" |
| 106 |
} else { |
| 107 |
target = fmt.Sprintf("found%d", n) |
| 108 |
} |
| 109 |
JCS(LabelRef(target)) |
| 110 |
} |
| 111 |
|
| 112 |
// Advance and loop again. |
| 113 |
ADDQ(Imm(unroll), i) |
| 114 |
ADDQ(Imm(unroll*maxLength), keyset) |
| 115 |
JMP(LabelRef("bigloop")) |
| 116 |
|
| 117 |
// Loop over the remaining keys. |
| 118 |
Label("loop") |
| 119 |
CMPQ(i, count) |
| 120 |
JE(LabelRef("done")) |
| 121 |
|
| 122 |
// Try to match against the input key. |
| 123 |
match := XMM() |
| 124 |
VPCMPEQB(Mem{Base: keyset}, key, match) |
| 125 |
VPTEST(ones, match) |
| 126 |
JCS(LabelRef("done")) |
| 127 |
|
| 128 |
// Advance and loop again. |
| 129 |
Label("next") |
| 130 |
INCQ(i) |
| 131 |
ADDQ(Imm(maxLength), keyset) |
| 132 |
JMP(LabelRef("loop")) |
| 133 |
JMP(LabelRef("done")) |
| 134 |
|
| 135 |
// Return the loop increment, or the count if the key wasn't found. If we're |
| 136 |
// here from a jump within the big loop, the loop increment needs |
| 137 |
// correcting first. |
| 138 |
for j := unroll - 1; j > 0; j-- { |
| 139 |
Label(fmt.Sprintf("found%d", j)) |
| 140 |
INCQ(i) |
| 141 |
} |
| 142 |
Label("done") |
| 143 |
Store(i, ReturnIndex(0)) |
| 144 |
RET() |
| 145 |
Label("not_found") |
| 146 |
Store(count, ReturnIndex(0)) |
| 147 |
RET() |
| 148 |
|
| 149 |
// If the input key is near a page boundary, we must change the way we load |
| 150 |
// it to avoid a fault. We instead want to load the 16 bytes up to and |
| 151 |
// including the key, then shuffle the key forward in the register. E.g. for |
| 152 |
// key "foo" we would load the 13 bytes prior to the key along with "foo" |
| 153 |
// and then move the last 3 bytes forward so the first 3 bytes are equal |
| 154 |
// to "foo". |
| 155 |
Label("safe_load") |
| 156 |
pageOffset := GP64() |
| 157 |
MOVQ(keyPtr, pageOffset) |
| 158 |
ANDQ(U32(pageSize-1), pageOffset) |
| 159 |
CMPQ(pageOffset, U32(pageSize-maxLength)) |
| 160 |
JBE(LabelRef("load")) // Not near a page boundary. |
| 161 |
offset := GP64() |
| 162 |
MOVQ(^U64(0)-maxLength+1, offset) |
| 163 |
ADDQ(keyLen, offset) |
| 164 |
VMOVUPS(Mem{Base: keyPtr, Index: offset, Scale: 1}, key) |
| 165 |
var shuffleBytes [maxLength * 2]byte |
| 166 |
for j := 0; j < maxLength; j++ { |
| 167 |
shuffleBytes[j] = byte(j) |
| 168 |
shuffleBytes[j+maxLength] = byte(j) |
| 169 |
} |
| 170 |
shuffleMasks := ConstBytes("shuffle_masks", shuffleBytes[:]) |
| 171 |
shuffleMasksPtr := GP64() |
| 172 |
LEAQ(shuffleMasks.Offset(maxLength), shuffleMasksPtr) |
| 173 |
SUBQ(keyLen, shuffleMasksPtr) |
| 174 |
shuffle := XMM() |
| 175 |
VMOVUPS(Mem{Base: shuffleMasksPtr}, shuffle) |
| 176 |
VPSHUFB(shuffle, key, key) |
| 177 |
JMP(LabelRef("prepare")) |
| 178 |
} |