-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLeopard2BackendAVX2Xor.cpp
More file actions
356 lines (328 loc) · 12.2 KB
/
Copy pathLeopard2BackendAVX2Xor.cpp
File metadata and controls
356 lines (328 loc) · 12.2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Copyright (c) 2017 Christopher A. Taylor. All rights reserved.
See Leopard2Backend.h for the full BSD license notice.
Exact AVX2 XOR remainder arities live in this separate translation unit so
they cannot change GCC's inlining or top-level ordering for the mature
transform and reduction kernels in Leopard2BackendAVX2.cpp.
*/
#include "Leopard2Backend.h"
#include <cassert>
#include <cstring>
#include <immintrin.h>
namespace leopard { namespace backend {
#if !defined(NO_LEO_HAS_FF8)
#if defined(_MSC_VER)
#define LEO2_AVX2_XOR_FORCE_INLINE inline __forceinline
#elif defined(__GNUC__) || defined(__clang__)
#define LEO2_AVX2_XOR_FORCE_INLINE inline __attribute__((always_inline))
#else
#define LEO2_AVX2_XOR_FORCE_INLINE inline
#endif
#if defined(_MSC_VER)
#define LEO2_AVX2_FIXED_NOINLINE __declspec(noinline)
#elif defined(__GNUC__) && !defined(__clang__) && defined(__ELF__)
#define LEO2_AVX2_FIXED_NOINLINE \
__attribute__((noinline, noclone, section(".text.leo2_avx2_r1_fixed"), \
aligned(64)))
#elif (defined(__GNUC__) || defined(__clang__)) && defined(__ELF__)
#define LEO2_AVX2_FIXED_NOINLINE \
__attribute__((noinline, section(".text.leo2_avx2_r1_fixed"), aligned(64)))
#elif defined(__GNUC__) || defined(__clang__)
#define LEO2_AVX2_FIXED_NOINLINE __attribute__((noinline))
#else
#define LEO2_AVX2_FIXED_NOINLINE
#endif
static LEO2_AVX2_XOR_FORCE_INLINE void AVX2AccumulateFixed64(
const void* source_pointer,
__m256i& accumulator0,
__m256i& accumulator1)
{
const uint8_t* const source =
static_cast<const uint8_t*>(source_pointer);
assert(source != NULL);
accumulator0 = _mm256_xor_si256(accumulator0,
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(source)));
accumulator1 = _mm256_xor_si256(accumulator1,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 32)));
}
static LEO2_AVX2_XOR_FORCE_INLINE void AVX2AccumulateFixed256(
const void* source_pointer,
__m256i& accumulator0,
__m256i& accumulator1,
__m256i& accumulator2,
__m256i& accumulator3,
__m256i& accumulator4,
__m256i& accumulator5,
__m256i& accumulator6,
__m256i& accumulator7)
{
const uint8_t* const source =
static_cast<const uint8_t*>(source_pointer);
assert(source != NULL);
accumulator0 = _mm256_xor_si256(accumulator0,
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(source)));
accumulator1 = _mm256_xor_si256(accumulator1,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 32)));
accumulator2 = _mm256_xor_si256(accumulator2,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 64)));
accumulator3 = _mm256_xor_si256(accumulator3,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 96)));
accumulator4 = _mm256_xor_si256(accumulator4,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 128)));
accumulator5 = _mm256_xor_si256(accumulator5,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 160)));
accumulator6 = _mm256_xor_si256(accumulator6,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 192)));
accumulator7 = _mm256_xor_si256(accumulator7,
_mm256_loadu_si256(
reinterpret_cast<const __m256i*>(source + 224)));
}
LEO2_AVX2_FIXED_NOINLINE void AVX2XorMemorySourcesFixed64(
void* destination,
const void* initial_source,
const void* const* sources,
uint32_t source_count,
uint32_t excluded_source)
{
assert(destination != NULL && initial_source != NULL && sources != NULL);
assert(excluded_source <= source_count);
assert(excluded_source == source_count ||
sources[excluded_source] == NULL);
const uint8_t* const initial =
static_cast<const uint8_t*>(initial_source);
__m256i accumulator0 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial));
__m256i accumulator1 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 32));
uint32_t source_index = 0;
for (; source_index < excluded_source; ++source_index)
AVX2AccumulateFixed64(
sources[source_index], accumulator0, accumulator1);
if (excluded_source < source_count)
++source_index;
for (; source_index < source_count; ++source_index)
AVX2AccumulateFixed64(
sources[source_index], accumulator0, accumulator1);
uint8_t* const output = static_cast<uint8_t*>(destination);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output), accumulator0);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 32), accumulator1);
}
LEO2_AVX2_FIXED_NOINLINE void AVX2XorMemorySourcesFixed256(
void* destination,
const void* initial_source,
const void* const* sources,
uint32_t source_count,
uint32_t excluded_source)
{
assert(destination != NULL && initial_source != NULL && sources != NULL);
assert(excluded_source <= source_count);
assert(excluded_source == source_count ||
sources[excluded_source] == NULL);
const uint8_t* const initial =
static_cast<const uint8_t*>(initial_source);
__m256i accumulator0 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial));
__m256i accumulator1 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 32));
__m256i accumulator2 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 64));
__m256i accumulator3 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 96));
__m256i accumulator4 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 128));
__m256i accumulator5 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 160));
__m256i accumulator6 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 192));
__m256i accumulator7 = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + 224));
uint32_t source_index = 0;
for (; source_index < excluded_source; ++source_index)
AVX2AccumulateFixed256(sources[source_index],
accumulator0, accumulator1, accumulator2, accumulator3,
accumulator4, accumulator5, accumulator6, accumulator7);
if (excluded_source < source_count)
++source_index;
for (; source_index < source_count; ++source_index)
AVX2AccumulateFixed256(sources[source_index],
accumulator0, accumulator1, accumulator2, accumulator3,
accumulator4, accumulator5, accumulator6, accumulator7);
uint8_t* const output = static_cast<uint8_t*>(destination);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output), accumulator0);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 32), accumulator1);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 64), accumulator2);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 96), accumulator3);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 128), accumulator4);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 160), accumulator5);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 192), accumulator6);
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + 224), accumulator7);
}
#undef LEO2_AVX2_FIXED_NOINLINE
template<unsigned SourceCount>
static LEO2_AVX2_XOR_FORCE_INLINE void AVX2XorMemoryFusedFinalGroup(
void* destination,
const void* initial_source,
const void* const* sources,
uint64_t byte_count)
{
uint8_t* output = static_cast<uint8_t*>(destination);
const uint8_t* initial = static_cast<const uint8_t*>(initial_source);
const uint8_t* inputs[SourceCount];
for (unsigned lane = 0; lane < SourceCount; ++lane)
inputs[lane] = static_cast<const uint8_t*>(sources[lane]);
uint64_t offset = 0;
while (byte_count - offset >= 32)
{
__m256i result = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(initial + offset));
for (unsigned lane = 0; lane < SourceCount; ++lane)
result = _mm256_xor_si256(result, _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(inputs[lane] + offset)));
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(output + offset), result);
offset += 32;
}
/* The public API permits arbitrary positive byte lengths. Volatile keeps
GCC and Clang from cloning overlap-checking vector loops for this cold
suffix; the API requires destination/source ranges to be disjoint. */
volatile uint8_t* const tail_output = output;
const volatile uint8_t* const tail_initial = initial;
while (offset < byte_count)
{
uint8_t result = tail_initial[offset];
for (unsigned lane = 0; lane < SourceCount; ++lane)
result ^= static_cast<const volatile uint8_t*>(
inputs[lane])[offset];
tail_output[offset++] = result;
}
}
void AVX2XorMemorySourcesFusedFinal(
void* destination,
const void* initial_source,
const void* const* sources,
uint32_t source_count,
uint64_t byte_count)
{
if (byte_count == 0)
return;
const void* waiting[8];
unsigned waiting_count = 0;
const void* accumulator = initial_source;
bool wrote_destination = false;
for (uint32_t i = 0; i < source_count; ++i)
{
if (!sources[i])
continue;
waiting[waiting_count++] = sources[i];
if (waiting_count == 8)
{
AVX2XorMemoryFusedFinalGroup<8>(
destination, accumulator, waiting, byte_count);
accumulator = destination;
wrote_destination = true;
waiting_count = 0;
}
}
switch (waiting_count)
{
case 7:
AVX2XorMemoryFusedFinalGroup<7>(
destination, accumulator, waiting, byte_count);
return;
case 6:
AVX2XorMemoryFusedFinalGroup<6>(
destination, accumulator, waiting, byte_count);
return;
case 5:
AVX2XorMemoryFusedFinalGroup<5>(
destination, accumulator, waiting, byte_count);
return;
case 4:
AVX2XorMemoryFusedFinalGroup<4>(
destination, accumulator, waiting, byte_count);
return;
case 3:
AVX2XorMemoryFusedFinalGroup<3>(
destination, accumulator, waiting, byte_count);
return;
case 2:
AVX2XorMemoryFusedFinalGroup<2>(
destination, accumulator, waiting, byte_count);
return;
case 1:
AVX2XorMemoryFusedFinalGroup<1>(
destination, accumulator, waiting, byte_count);
return;
default:
break;
}
if (!wrote_destination)
std::memcpy(destination, initial_source, static_cast<size_t>(byte_count));
}
void AVX2XorMemorySourcesGroup4(
void* destination,
const void* initial_source,
const void* const* sources,
uint32_t source_count,
uint64_t byte_count)
{
if (byte_count == 0)
return;
const void* waiting[4];
unsigned waiting_count = 0;
const void* accumulator = initial_source;
bool wrote_destination = false;
for (uint32_t i = 0; i < source_count; ++i)
{
if (!sources[i])
continue;
waiting[waiting_count++] = sources[i];
if (waiting_count == 4)
{
AVX2XorMemoryFusedFinalGroup<4>(
destination, accumulator, waiting, byte_count);
accumulator = destination;
wrote_destination = true;
waiting_count = 0;
}
}
switch (waiting_count)
{
case 3:
AVX2XorMemoryFusedFinalGroup<3>(
destination, accumulator, waiting, byte_count);
return;
case 2:
AVX2XorMemoryFusedFinalGroup<2>(
destination, accumulator, waiting, byte_count);
return;
case 1:
AVX2XorMemoryFusedFinalGroup<1>(
destination, accumulator, waiting, byte_count);
return;
default:
break;
}
if (!wrote_destination)
std::memcpy(destination, initial_source, static_cast<size_t>(byte_count));
}
#undef LEO2_AVX2_XOR_FORCE_INLINE
#endif // !NO_LEO_HAS_FF8
}} // namespace leopard::backend