1 /* SPDX-License-Identifier: GPL-2.0 */
3 * NH - ε-almost-universal hash function, x86_64 AVX2 accelerated
5 * Copyright 2018 Google LLC
7 * Author: Eric Biggers <ebiggers@google.com>
10 #include <linux/linkage.h>
12 #define PASS0_SUMS %ymm0
13 #define PASS1_SUMS %ymm1
14 #define PASS2_SUMS %ymm2
15 #define PASS3_SUMS %ymm3
36 #define MESSAGE_LEN %rdx
39 .macro _nh_2xstride k0, k1, k2, k3
41 // Add message words to key words
47 // Multiply 32x32 => 64 and accumulate
60 vpaddq T0, PASS0_SUMS, PASS0_SUMS
61 vpaddq T1, PASS1_SUMS, PASS1_SUMS
62 vpaddq T2, PASS2_SUMS, PASS2_SUMS
63 vpaddq T3, PASS3_SUMS, PASS3_SUMS
67 * void nh_avx2(const u32 *key, const u8 *message, size_t message_len,
68 * u8 hash[NH_HASH_BYTES])
70 * It's guaranteed that message_len % 16 == 0.
72 SYM_FUNC_START(nh_avx2)
77 vpxor PASS0_SUMS, PASS0_SUMS, PASS0_SUMS
78 vpxor PASS1_SUMS, PASS1_SUMS, PASS1_SUMS
79 vpxor PASS2_SUMS, PASS2_SUMS, PASS2_SUMS
80 vpxor PASS3_SUMS, PASS3_SUMS, PASS3_SUMS
82 sub $0x40, MESSAGE_LEN
88 _nh_2xstride K0, K1, K2, K3
90 vmovdqu 0x20(MESSAGE), T3
93 _nh_2xstride K2, K3, K0, K1
97 sub $0x40, MESSAGE_LEN
101 and $0x3f, MESSAGE_LEN
104 cmp $0x20, MESSAGE_LEN
107 // 2 or 3 strides remain; do 2 more.
108 vmovdqu (MESSAGE), T3
109 vmovdqu 0x00(KEY), K2
110 vmovdqu 0x10(KEY), K3
111 _nh_2xstride K0, K1, K2, K3
114 sub $0x20, MESSAGE_LEN
119 // Last stride. Zero the high 128 bits of the message and keys so they
120 // don't affect the result when processing them like 2 strides.
121 vmovdqu (MESSAGE), T3_XMM
122 vmovdqa K0_XMM, K0_XMM
123 vmovdqa K1_XMM, K1_XMM
124 vmovdqu 0x00(KEY), K2_XMM
125 vmovdqu 0x10(KEY), K3_XMM
126 _nh_2xstride K0, K1, K2, K3
129 // Sum the accumulators for each pass, then store the sums to 'hash'
131 // PASS0_SUMS is (0A 0B 0C 0D)
132 // PASS1_SUMS is (1A 1B 1C 1D)
133 // PASS2_SUMS is (2A 2B 2C 2D)
134 // PASS3_SUMS is (3A 3B 3C 3D)
135 // We need the horizontal sums:
136 // (0A + 0B + 0C + 0D,
137 // 1A + 1B + 1C + 1D,
138 // 2A + 2B + 2C + 2D,
139 // 3A + 3B + 3C + 3D)
142 vpunpcklqdq PASS1_SUMS, PASS0_SUMS, T0 // T0 = (0A 1A 0C 1C)
143 vpunpckhqdq PASS1_SUMS, PASS0_SUMS, T1 // T1 = (0B 1B 0D 1D)
144 vpunpcklqdq PASS3_SUMS, PASS2_SUMS, T2 // T2 = (2A 3A 2C 3C)
145 vpunpckhqdq PASS3_SUMS, PASS2_SUMS, T3 // T3 = (2B 3B 2D 3D)
147 vinserti128 $0x1, T2_XMM, T0, T4 // T4 = (0A 1A 2A 3A)
148 vinserti128 $0x1, T3_XMM, T1, T5 // T5 = (0B 1B 2B 3B)
149 vperm2i128 $0x31, T2, T0, T0 // T0 = (0C 1C 2C 3C)
150 vperm2i128 $0x31, T3, T1, T1 // T1 = (0D 1D 2D 3D)
157 SYM_FUNC_END(nh_avx2)