File: keyset.go

package info (click to toggle)
golang-github-segmentio-asm 1.2.0%2Bgit20231107.1cfacc8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: asm: 6,093; makefile: 32
file content (40 lines) | stat: -rw-r--r-- 969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package keyset

import (
	"bytes"

	"github.com/segmentio/asm/cpu"
	"github.com/segmentio/asm/cpu/arm64"
	"github.com/segmentio/asm/cpu/x86"
)

// New prepares a set of keys for use with Lookup.
//
// An optimized routine is used if the processor supports AVX instructions and
// the maximum length of any of the keys is less than or equal to 16. If New
// returns nil, this indicates that an optimized routine is not available, and
// the caller should use a fallback.
func New(keys [][]byte) []byte {
	maxWidth, hasNullByte := checkKeys(keys)
	if hasNullByte || maxWidth > 16 || !(cpu.X86.Has(x86.AVX) || cpu.ARM64.Has(arm64.ASIMD)) {
		return nil
	}

	set := make([]byte, len(keys)*16)
	for i, k := range keys {
		copy(set[i*16:], k)
	}
	return set
}

func checkKeys(keys [][]byte) (maxWidth int, hasNullByte bool) {
	for _, k := range keys {
		if len(k) > maxWidth {
			maxWidth = len(k)
		}
		if bytes.IndexByte(k, 0) >= 0 {
			hasNullByte = true
		}
	}
	return
}