| 1 |
//go:build !purego |
| 2 |
// +build !purego |
| 3 |
|
| 4 |
#include "textflag.h" |
| 5 |
|
| 6 |
// func Lookup(keyset []byte, key []byte) int |
| 7 |
TEXT ·Lookup(SB), NOSPLIT, $0-56 |
| 8 |
MOVD keyset+0(FP), R0 |
| 9 |
MOVD keyset_len+8(FP), R1 |
| 10 |
MOVD key+24(FP), R2 |
| 11 |
MOVD key_len+32(FP), R3 |
| 12 |
MOVD key_cap+40(FP), R4 |
| 13 |
|
| 14 |
// None of the keys in the set are greater than 16 bytes, so if the input |
| 15 |
// key is we can jump straight to not found. |
| 16 |
CMP $16, R3 |
| 17 |
BHI notfound |
| 18 |
|
| 19 |
// We'll be moving the keyset pointer (R0) forward as we compare keys, so |
| 20 |
// make a copy of the starting point (R6). Also add the byte length (R1) to |
| 21 |
// obtain a pointer to the end of the keyset (R5). |
| 22 |
MOVD R0, R6 |
| 23 |
ADD R0, R1, R5 |
| 24 |
|
| 25 |
// Prepare a 64-bit mask of all ones. |
| 26 |
MOVD $-1, R7 |
| 27 |
|
| 28 |
// Prepare a vector of all zeroes. |
| 29 |
VMOV ZR, V1.B16 |
| 30 |
|
| 31 |
// Check that it's safe to load 16 bytes of input. If cap(input)<16, jump |
| 32 |
// to a check that determines whether a tail load is necessary (to avoid a |
| 33 |
// page fault). |
| 34 |
CMP $16, R4 |
| 35 |
BLO safeload |
| 36 |
|
| 37 |
load: |
| 38 |
// Load the input key (V0) and pad with zero bytes (V1). To blend the two |
| 39 |
// vectors, we load a mask for the particular key length and then use TBL |
| 40 |
// to select bytes from either V0 or V1. |
| 41 |
VLD1 (R2), [V0.B16] |
| 42 |
MOVD $blend_masks<>(SB), R10 |
| 43 |
ADD R3<<4, R10, R10 |
| 44 |
VLD1 (R10), [V2.B16] |
| 45 |
VTBL V2.B16, [V0.B16, V1.B16], V3.B16 |
| 46 |
|
| 47 |
loop: |
| 48 |
// Loop through each 16 byte key in the keyset. |
| 49 |
CMP R0, R5 |
| 50 |
BEQ notfound |
| 51 |
|
| 52 |
// Load and compare the next key. |
| 53 |
VLD1.P 16(R0), [V4.B16] |
| 54 |
VCMEQ V3.B16, V4.B16, V5.B16 |
| 55 |
VMOV V5.D[0], R8 |
| 56 |
VMOV V5.D[1], R9 |
| 57 |
AND R8, R9, R9 |
| 58 |
|
| 59 |
// If the masks match, we found the key. |
| 60 |
CMP R9, R7 |
| 61 |
BEQ found |
| 62 |
JMP loop |
| 63 |
|
| 64 |
found: |
| 65 |
// If the key was found, take the position in the keyset and convert it |
| 66 |
// to an index. The keyset pointer (R0) will be 1 key past the match, so |
| 67 |
// subtract the starting pointer (R6), divide by 16 to convert from byte |
| 68 |
// length to an index, and then subtract one. |
| 69 |
SUB R6, R0, R0 |
| 70 |
ADD R0>>4, ZR, R0 |
| 71 |
SUB $1, R0, R0 |
| 72 |
MOVD R0, ret+48(FP) |
| 73 |
RET |
| 74 |
|
| 75 |
notfound: |
| 76 |
// Return the number of keys in the keyset, which is the byte length (R1) |
| 77 |
// divided by 16. |
| 78 |
ADD R1>>4, ZR, R1 |
| 79 |
MOVD R1, ret+48(FP) |
| 80 |
RET |
| 81 |
|
| 82 |
safeload: |
| 83 |
// Check if the input crosses a page boundary. If not, jump back. |
| 84 |
AND $4095, R2, R12 |
| 85 |
CMP $4080, R12 |
| 86 |
BLS load |
| 87 |
|
| 88 |
// If it does cross a page boundary, we must assume that loading 16 bytes |
| 89 |
// will cause a fault. Instead, we load the 16 bytes up to and including the |
| 90 |
// key and then shuffle the key forward in the register. We can shuffle and |
| 91 |
// pad with zeroes at the same time to avoid having to also blend (as load |
| 92 |
// does). |
| 93 |
MOVD $16, R12 |
| 94 |
SUB R3, R12, R12 |
| 95 |
SUB R12, R2, R2 |
| 96 |
VLD1 (R2), [V0.B16] |
| 97 |
MOVD $shuffle_masks<>(SB), R10 |
| 98 |
ADD R12, R10, R10 |
| 99 |
VLD1 (R10), [V2.B16] |
| 100 |
VTBL V2.B16, [V0.B16, V1.B16], V3.B16 |
| 101 |
JMP loop |
| 102 |
|
| 103 |
DATA blend_masks<>+0(SB)/8, $0x1010101010101010 |
| 104 |
DATA blend_masks<>+8(SB)/8, $0x1010101010101010 |
| 105 |
DATA blend_masks<>+16(SB)/8, $0x1010101010101000 |
| 106 |
DATA blend_masks<>+24(SB)/8, $0x1010101010101010 |
| 107 |
DATA blend_masks<>+32(SB)/8, $0x1010101010100100 |
| 108 |
DATA blend_masks<>+40(SB)/8, $0x1010101010101010 |
| 109 |
DATA blend_masks<>+48(SB)/8, $0x1010101010020100 |
| 110 |
DATA blend_masks<>+56(SB)/8, $0x1010101010101010 |
| 111 |
DATA blend_masks<>+64(SB)/8, $0x1010101003020100 |
| 112 |
DATA blend_masks<>+72(SB)/8, $0x1010101010101010 |
| 113 |
DATA blend_masks<>+80(SB)/8, $0x1010100403020100 |
| 114 |
DATA blend_masks<>+88(SB)/8, $0x1010101010101010 |
| 115 |
DATA blend_masks<>+96(SB)/8, $0x1010050403020100 |
| 116 |
DATA blend_masks<>+104(SB)/8, $0x1010101010101010 |
| 117 |
DATA blend_masks<>+112(SB)/8, $0x1006050403020100 |
| 118 |
DATA blend_masks<>+120(SB)/8, $0x1010101010101010 |
| 119 |
DATA blend_masks<>+128(SB)/8, $0x0706050403020100 |
| 120 |
DATA blend_masks<>+136(SB)/8, $0x1010101010101010 |
| 121 |
DATA blend_masks<>+144(SB)/8, $0x0706050403020100 |
| 122 |
DATA blend_masks<>+152(SB)/8, $0x1010101010101008 |
| 123 |
DATA blend_masks<>+160(SB)/8, $0x0706050403020100 |
| 124 |
DATA blend_masks<>+168(SB)/8, $0x1010101010100908 |
| 125 |
DATA blend_masks<>+176(SB)/8, $0x0706050403020100 |
| 126 |
DATA blend_masks<>+184(SB)/8, $0x10101010100A0908 |
| 127 |
DATA blend_masks<>+192(SB)/8, $0x0706050403020100 |
| 128 |
DATA blend_masks<>+200(SB)/8, $0x101010100B0A0908 |
| 129 |
DATA blend_masks<>+208(SB)/8, $0x0706050403020100 |
| 130 |
DATA blend_masks<>+216(SB)/8, $0x1010100C0B0A0908 |
| 131 |
DATA blend_masks<>+224(SB)/8, $0x0706050403020100 |
| 132 |
DATA blend_masks<>+232(SB)/8, $0x10100D0C0B0A0908 |
| 133 |
DATA blend_masks<>+240(SB)/8, $0x0706050403020100 |
| 134 |
DATA blend_masks<>+248(SB)/8, $0x100E0D0C0B0A0908 |
| 135 |
DATA blend_masks<>+256(SB)/8, $0x0706050403020100 |
| 136 |
DATA blend_masks<>+264(SB)/8, $0x0F0E0D0C0B0A0908 |
| 137 |
GLOBL blend_masks<>(SB), RODATA|NOPTR, $272 |
| 138 |
|
| 139 |
DATA shuffle_masks<>+0(SB)/8, $0x0706050403020100 |
| 140 |
DATA shuffle_masks<>+8(SB)/8, $0x0F0E0D0C0B0A0908 |
| 141 |
DATA shuffle_masks<>+16(SB)/8, $0x1010101010101010 |
| 142 |
DATA shuffle_masks<>+24(SB)/8, $0x1010101010101010 |
| 143 |
GLOBL shuffle_masks<>(SB), RODATA|NOPTR, $32 |