| 1 | // Copyright (c) 2019. Temple3x ([email protected]) |
| 2 | // |
| 3 | // Use of this source code is governed by the MIT License |
| 4 | // that can be found in the LICENSE file. |
| 5 | |
| 6 | package xorsimd |
| 7 | |
| 8 | import ( |
| 9 | "unsafe" |
| 10 | |
| 11 | "github.com/templexxx/cpu" |
| 12 | ) |
| 13 | |
| 14 | const wordSize = int(unsafe.Sizeof(uintptr(0))) |
| 15 | |
| 16 | // EnableAVX512 controls whether AVX-512 is considered during feature detection. |
| 17 | // Note: instruction-path selection is decided at package initialization, so |
| 18 | // changing this value afterwards does not refresh the selected backend. |
| 19 | var EnableAVX512 = true |
| 20 | |
| 21 | // cpuFeature indicates which instruction set will be used. |
| 22 | var cpuFeature = getCPUFeature() |
| 23 | |
| 24 | const ( |
| 25 | avx512 = iota |
| 26 | avx2 |
| 27 | sse2 |
| 28 | generic |
| 29 | ) |
| 30 | |
| 31 | // TODO: Add ARM feature... |
| 32 | |
| 33 | func hasAVX512() (ok bool) { |
| 34 | |
| 35 | return cpu.X86.HasAVX512VL && |
| 36 | cpu.X86.HasAVX512BW && |
| 37 | cpu.X86.HasAVX512F && |
| 38 | cpu.X86.HasAVX512DQ |
| 39 | } |
| 40 | |
| 41 | // Encode XORs all source rows into dst. |
| 42 | // The source and destination may overlap. |
| 43 | // |
| 44 | // Encode returns the number of bytes processed, which is the minimum length of |
| 45 | // dst and every source row. |
| 46 | // |
| 47 | // Callers must provide at least one source row: len(src) >= 1. |
| 48 | func Encode(dst []byte, src [][]byte) (n int) { |
| 49 | n = checkLen(dst, src) |
| 50 | if n == 0 { |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | dst = dst[:n] |
| 55 | for i := range src { |
| 56 | src[i] = src[i][:n] |
| 57 | } |
| 58 | |
| 59 | if len(src) == 1 { |
| 60 | copy(dst, src[0]) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | encode(dst, src) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | func checkLen(dst []byte, src [][]byte) int { |
| 69 | n := len(dst) |
| 70 | for i := range src { |
| 71 | if len(src[i]) < n { |
| 72 | n = len(src[i]) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if n <= 0 { |
| 77 | return 0 |
| 78 | } |
| 79 | return n |
| 80 | } |
| 81 | |
| 82 | // Bytes XORs a and b into dst. |
| 83 | // The source and destination may overlap. |
| 84 | // |
| 85 | // Bytes returns the number of bytes processed, which is the minimum of |
| 86 | // len(dst), len(a), and len(b). |
| 87 | func Bytes(dst, a, b []byte) int { |
| 88 | return Encode(dst, [][]byte{a, b}) |
| 89 | } |