| 1 | # xorsimd |
| 2 | |
| 3 | [![GoDoc][1]][2] [![MIT licensed][3]][4] [![Build Status][5]][6] [![Go Report Card][7]][8] [![Sourcegraph][9]][10] |
| 4 | |
| 5 | [1]: https://godoc.org/github.com/templexxx/xorsimd?status.svg |
| 6 | [2]: https://godoc.org/github.com/templexxx/xorsimd |
| 7 | [3]: https://img.shields.io/badge/license-MIT-blue.svg |
| 8 | [4]: LICENSE |
| 9 | [5]: https://github.com/templexxx/xorsimd/workflows/unit-test/badge.svg |
| 10 | [6]: https://github.com/templexxx/xorsimd |
| 11 | [7]: https://goreportcard.com/badge/github.com/templexxx/xorsimd |
| 12 | [8]: https://goreportcard.com/report/github.com/templexxx/xorsimd |
| 13 | [9]: https://sourcegraph.com/github.com/templexxx/xorsimd/-/badge.svg |
| 14 | [10]: https://sourcegraph.com/github.com/templexxx/xorsimd?badge |
| 15 | |
| 16 | `xorsimd` provides high-throughput XOR operations for byte slices. |
| 17 | On amd64 it selects AVX-512/AVX2/SSE2 implementations, and falls back to |
| 18 | portable Go code on other architectures. |
| 19 | |
| 20 | ## Install |
| 21 | |
| 22 | ```bash |
| 23 | go get github.com/templexxx/xorsimd |
| 24 | ``` |
| 25 | |
| 26 | ## Quick Start |
| 27 | |
| 28 | ```go |
| 29 | package main |
| 30 | |
| 31 | import "github.com/templexxx/xorsimd" |
| 32 | |
| 33 | func main() { |
| 34 | a := []byte{1, 2, 3, 4} |
| 35 | b := []byte{9, 8, 7, 6} |
| 36 | dst := make([]byte, 4) |
| 37 | |
| 38 | // dst = a ^ b |
| 39 | n := xorsimd.Bytes(dst, a, b) |
| 40 | _ = n |
| 41 | |
| 42 | // dst = src[0] ^ src[1] ^ src[2] ... |
| 43 | src := [][]byte{a, b, []byte{1, 1, 1, 1}} |
| 44 | n = xorsimd.Encode(dst, src) |
| 45 | _ = n |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | ## API Notes |
| 50 | |
| 51 | - `Encode(dst, src)` XORs all rows in `src` into `dst` and returns the number |
| 52 | of bytes processed. |
| 53 | - `Encode` requires `len(src) >= 1`. |
| 54 | - `Bytes(dst, a, b)` is a convenience wrapper around `Encode`. |
| 55 | - `Bytes8`, `Bytes16`, `Bytes8Align`, and `Bytes16Align` are fixed-size helpers. |
| 56 | - `BytesA` processes `len(a)` bytes; `BytesB` processes `len(b)` bytes. |
| 57 | |
| 58 | All APIs may reuse overlapping slices. For correctness, callers must ensure the |
| 59 | input slices are long enough for the selected function variant. |
| 60 | |
| 61 | ## Performance |
| 62 | |
| 63 | Performance mainly depends on: |
| 64 | |
| 65 | - CPU SIMD instruction support. |
| 66 | - Number of input source rows. |
| 67 | - Slice size. |
| 68 | |
| 69 | Benchmark formula: |
| 70 | |
| 71 | `I/O = (src_num + 1) * vector_size / cost` |
| 72 | |
| 73 | Test environment: |
| 74 | |
| 75 | - AWS c5d.xlarge |
| 76 | - Intel Xeon Platinum 8124M @ 3.00GHz |
| 77 | - Single physical core |
| 78 | |
| 79 | | Src Num | Vector Size | AVX512 I/O (MB/s) | AVX2 I/O (MB/s) | SSE2 I/O (MB/s) | |
| 80 | | --- | --- | --- | --- | --- | |
| 81 | | 5 | 4KB | 270403.73 | 142825.25 | 74443.91 | |
| 82 | | 5 | 1MB | 26948.34 | 26887.37 | 26950.65 | |
| 83 | | 5 | 8MB | 17881.32 | 17212.56 | 16402.97 | |
| 84 | | 10 | 4KB | 190445.30 | 102953.59 | 53244.04 | |
| 85 | | 10 | 1MB | 26424.44 | 26618.65 | 26094.39 | |
| 86 | | 10 | 8MB | 15471.31 | 14866.72 | 13565.80 | |
| 87 | |
| 88 | ## Run Tests and Benchmarks |
| 89 | |
| 90 | ```bash |
| 91 | go test ./... |
| 92 | go test -bench=. -benchmem |
| 93 | ``` |