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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* embed_kernels_avx.c - x86 SIMD hot kernels (AVX2+FMA, with AVX-512 when available)
*
* bf16 to f32 conversion: load 16 uint16, zero-extend to 32-bit, shift left 16.
* Uses AVX-512F+BW for 16-wide bf16 matvec (dominant for seq_len==1), and
* AVX2+FMA / AVX-512F for f32 attention helpers (which operate on cache-resident data).
*
* The bf16 matvec processes 4 output rows simultaneously to reduce instruction
* overhead and improve out-of-order execution on memory-bound workloads.
*/
#include "kernels_impl.h"
#if defined(__AVX2__) && defined(__FMA__)
#include <immintrin.h>
#include <string.h>
/* =====================================================================
* BF16 matvec - uses AVX-512F+BW when available, AVX2 fallback otherwise
* ===================================================================== */
#if defined(__AVX512F__) && defined(__AVX512BW__)
void embed_bf16_matvec_fused_avx(
float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) {
int o = 0;
/* Process 4 output rows at a time */
for (; o + 3 < out_dim; o += 4) {
const uint16_t *w0 = W_bf16 + (size_t)o * in_dim;
const uint16_t *w1 = w0 + in_dim;
const uint16_t *w2 = w1 + in_dim;
const uint16_t *w3 = w2 + in_dim;
__m512 a0 = _mm512_setzero_ps(), a1 = _mm512_setzero_ps();
__m512 a2 = _mm512_setzero_ps(), a3 = _mm512_setzero_ps();
__m512 a4 = _mm512_setzero_ps(), a5 = _mm512_setzero_ps();
__m512 a6 = _mm512_setzero_ps(), a7 = _mm512_setzero_ps();
int k = 0;
for (; k + 32 <= in_dim; k += 32) {
__m512 xv0 = _mm512_loadu_ps(x + k);
__m512 xv1 = _mm512_loadu_ps(x + k + 16);
/* Row 0 */
__m256i r0a = _mm256_loadu_si256((const __m256i *)(w0 + k));
__m256i r0b = _mm256_loadu_si256((const __m256i *)(w0 + k + 16));
a0 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0a), 16)), xv0, a0);
a1 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0b), 16)), xv1, a1);
/* Row 1 */
__m256i r1a = _mm256_loadu_si256((const __m256i *)(w1 + k));
__m256i r1b = _mm256_loadu_si256((const __m256i *)(w1 + k + 16));
a2 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1a), 16)), xv0, a2);
a3 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1b), 16)), xv1, a3);
/* Row 2 */
__m256i r2a = _mm256_loadu_si256((const __m256i *)(w2 + k));
__m256i r2b = _mm256_loadu_si256((const __m256i *)(w2 + k + 16));
a4 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r2a), 16)), xv0, a4);
a5 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r2b), 16)), xv1, a5);
/* Row 3 */
__m256i r3a = _mm256_loadu_si256((const __m256i *)(w3 + k));
__m256i r3b = _mm256_loadu_si256((const __m256i *)(w3 + k + 16));
a6 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r3a), 16)), xv0, a6);
a7 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r3b), 16)), xv1, a7);
}
for (; k + 16 <= in_dim; k += 16) {
__m512 xv = _mm512_loadu_ps(x + k);
__m256i r0 = _mm256_loadu_si256((const __m256i *)(w0 + k));
__m256i r1 = _mm256_loadu_si256((const __m256i *)(w1 + k));
__m256i r2 = _mm256_loadu_si256((const __m256i *)(w2 + k));
__m256i r3 = _mm256_loadu_si256((const __m256i *)(w3 + k));
a0 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0), 16)), xv, a0);
a2 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1), 16)), xv, a2);
a4 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r2), 16)), xv, a4);
a6 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r3), 16)), xv, a6);
}
float s0 = _mm512_reduce_add_ps(_mm512_add_ps(a0, a1)) + (bias ? bias[o] : 0.0f);
float s1 = _mm512_reduce_add_ps(_mm512_add_ps(a2, a3)) + (bias ? bias[o + 1] : 0.0f);
float s2 = _mm512_reduce_add_ps(_mm512_add_ps(a4, a5)) + (bias ? bias[o + 2] : 0.0f);
float s3 = _mm512_reduce_add_ps(_mm512_add_ps(a6, a7)) + (bias ? bias[o + 3] : 0.0f);
for (; k < in_dim; k++) {
float xk = x[k];
uint32_t b0 = ((uint32_t)w0[k]) << 16, b1 = ((uint32_t)w1[k]) << 16;
uint32_t b2 = ((uint32_t)w2[k]) << 16, b3 = ((uint32_t)w3[k]) << 16;
float v0, v1, v2, v3;
memcpy(&v0, &b0, 4);
memcpy(&v1, &b1, 4);
memcpy(&v2, &b2, 4);
memcpy(&v3, &b3, 4);
s0 += v0 * xk;
s1 += v1 * xk;
s2 += v2 * xk;
s3 += v3 * xk;
}
y[o] = s0;
y[o + 1] = s1;
y[o + 2] = s2;
y[o + 3] = s3;
}
/* Handle 2-row remainder */
for (; o + 1 < out_dim; o += 2) {
const uint16_t *w0 = W_bf16 + (size_t)o * in_dim;
const uint16_t *w1 = w0 + in_dim;
__m512 a0 = _mm512_setzero_ps(), a1 = _mm512_setzero_ps();
__m512 a2 = _mm512_setzero_ps(), a3 = _mm512_setzero_ps();
int k = 0;
for (; k + 32 <= in_dim; k += 32) {
__m512 xv0 = _mm512_loadu_ps(x + k);
__m512 xv1 = _mm512_loadu_ps(x + k + 16);
__m256i r0a = _mm256_loadu_si256((const __m256i *)(w0 + k));
__m256i r0b = _mm256_loadu_si256((const __m256i *)(w0 + k + 16));
__m256i r1a = _mm256_loadu_si256((const __m256i *)(w1 + k));
__m256i r1b = _mm256_loadu_si256((const __m256i *)(w1 + k + 16));
a0 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0a), 16)), xv0, a0);
a1 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0b), 16)), xv1, a1);
a2 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1a), 16)), xv0, a2);
a3 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1b), 16)), xv1, a3);
}
for (; k + 16 <= in_dim; k += 16) {
__m512 xv = _mm512_loadu_ps(x + k);
__m256i r0 = _mm256_loadu_si256((const __m256i *)(w0 + k));
__m256i r1 = _mm256_loadu_si256((const __m256i *)(w1 + k));
a0 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0), 16)), xv, a0);
a2 = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1), 16)), xv, a2);
}
float s0 = _mm512_reduce_add_ps(_mm512_add_ps(a0, a1)) + (bias ? bias[o] : 0.0f);
float s1 = _mm512_reduce_add_ps(_mm512_add_ps(a2, a3)) + (bias ? bias[o + 1] : 0.0f);
for (; k < in_dim; k++) {
uint32_t b0 = ((uint32_t)w0[k]) << 16, b1 = ((uint32_t)w1[k]) << 16;
float v0, v1;
memcpy(&v0, &b0, 4);
memcpy(&v1, &b1, 4);
s0 += v0 * x[k];
s1 += v1 * x[k];
}
y[o] = s0;
y[o + 1] = s1;
}
/* Single remaining row */
for (; o < out_dim; o++) {
const uint16_t *w = W_bf16 + (size_t)o * in_dim;
__m512 acc = _mm512_setzero_ps();
int k = 0;
for (; k + 16 <= in_dim; k += 16) {
__m256i raw = _mm256_loadu_si256((const __m256i *)(w + k));
acc = _mm512_fmadd_ps(
_mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(raw), 16)),
_mm512_loadu_ps(x + k), acc);
}
float sum = _mm512_reduce_add_ps(acc) + (bias ? bias[o] : 0.0f);
for (; k < in_dim; k++) {
uint32_t bits = ((uint32_t)w[k]) << 16;
float wv;
memcpy(&wv, &bits, 4);
sum += wv * x[k];
}
y[o] = sum;
}
}
#else /* AVX2 only (or AVX-512F without BW) */
/* Helper: bf16 to f32 for 8 elements */
static inline __m256 bf16x8_to_f32(__m128i raw) {
return _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(raw), 16));
}
static inline void bf16x16_to_f32(const uint16_t *src, __m256 *lo, __m256 *hi) {
__m256i raw = _mm256_loadu_si256((const __m256i *)src);
*lo = bf16x8_to_f32(_mm256_castsi256_si128(raw));
*hi = bf16x8_to_f32(_mm256_extracti128_si256(raw, 1));
}
void embed_bf16_matvec_fused_avx(
float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) {
int o = 0;
for (; o + 1 < out_dim; o += 2) {
const uint16_t *w0 = W_bf16 + (size_t)o * in_dim;
const uint16_t *w1 = w0 + in_dim;
__m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps();
__m256 a2 = _mm256_setzero_ps(), a3 = _mm256_setzero_ps();
int k = 0;
for (; k + 16 <= in_dim; k += 16) {
__m256 xlo = _mm256_loadu_ps(x + k);
__m256 xhi = _mm256_loadu_ps(x + k + 8);
__m256 wlo, whi;
bf16x16_to_f32(w0 + k, &wlo, &whi);
a0 = _mm256_fmadd_ps(wlo, xlo, a0);
a1 = _mm256_fmadd_ps(whi, xhi, a1);
bf16x16_to_f32(w1 + k, &wlo, &whi);
a2 = _mm256_fmadd_ps(wlo, xlo, a2);
a3 = _mm256_fmadd_ps(whi, xhi, a3);
}
a0 = _mm256_add_ps(a0, a1);
a2 = _mm256_add_ps(a2, a3);
__m128 r0 = _mm_add_ps(_mm256_castps256_ps128(a0), _mm256_extractf128_ps(a0, 1));
__m128 r1 = _mm_add_ps(_mm256_castps256_ps128(a2), _mm256_extractf128_ps(a2, 1));
r0 = _mm_hadd_ps(r0, r1);
r0 = _mm_hadd_ps(r0, r0);
float s0 = _mm_cvtss_f32(r0) + (bias ? bias[o] : 0.0f);
float s1 = _mm_cvtss_f32(_mm_shuffle_ps(r0, r0, 1)) + (bias ? bias[o + 1] : 0.0f);
for (; k < in_dim; k++) {
uint32_t b0 = ((uint32_t)w0[k]) << 16, b1 = ((uint32_t)w1[k]) << 16;
float v0, v1;
memcpy(&v0, &b0, 4);
memcpy(&v1, &b1, 4);
s0 += v0 * x[k];
s1 += v1 * x[k];
}
y[o] = s0;
y[o + 1] = s1;
}
for (; o < out_dim; o++) {
const uint16_t *w = W_bf16 + (size_t)o * in_dim;
__m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps();
int k = 0;
for (; k + 16 <= in_dim; k += 16) {
__m256 wlo, whi;
bf16x16_to_f32(w + k, &wlo, &whi);
a0 = _mm256_fmadd_ps(wlo, _mm256_loadu_ps(x + k), a0);
a1 = _mm256_fmadd_ps(whi, _mm256_loadu_ps(x + k + 8), a1);
}
a0 = _mm256_add_ps(a0, a1);
__m128 r = _mm_add_ps(_mm256_castps256_ps128(a0), _mm256_extractf128_ps(a0, 1));
r = _mm_hadd_ps(r, r);
r = _mm_hadd_ps(r, r);
float sum = _mm_cvtss_f32(r) + (bias ? bias[o] : 0.0f);
for (; k < in_dim; k++) {
uint32_t bits = ((uint32_t)w[k]) << 16;
float wv;
memcpy(&wv, &bits, 4);
sum += wv * x[k];
}
y[o] = sum;
}
}
#endif /* AVX-512F+BW vs AVX2 for bf16 */
/* =====================================================================
* f32 attention helpers - AVX2+FMA, with AVX-512F when available
* (operates on L1-resident head vectors)
* ===================================================================== */
float embed_dot_f32_avx(const float *a, const float *b, int n) {
#if defined(__AVX512F__)
int i = 0;
__m512 acc0 = _mm512_setzero_ps();
__m512 acc1 = _mm512_setzero_ps();
__m512 acc2 = _mm512_setzero_ps();
__m512 acc3 = _mm512_setzero_ps();
for (; i + 64 <= n; i += 64) {
acc0 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i), _mm512_loadu_ps(b + i), acc0);
acc1 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 16), _mm512_loadu_ps(b + i + 16), acc1);
acc2 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 32), _mm512_loadu_ps(b + i + 32), acc2);
acc3 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 48), _mm512_loadu_ps(b + i + 48), acc3);
}
__m512 acc = _mm512_add_ps(_mm512_add_ps(acc0, acc1), _mm512_add_ps(acc2, acc3));
for (; i + 16 <= n; i += 16) {
acc = _mm512_fmadd_ps(_mm512_loadu_ps(a + i), _mm512_loadu_ps(b + i), acc);
}
float sum = _mm512_reduce_add_ps(acc);
for (; i < n; i++)
sum += a[i] * b[i];
return sum;
#else
int i = 0;
__m256 acc0 = _mm256_setzero_ps();
__m256 acc1 = _mm256_setzero_ps();
__m256 acc2 = _mm256_setzero_ps();
__m256 acc3 = _mm256_setzero_ps();
for (; i + 32 <= n; i += 32) {
acc0 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i), acc0);
acc1 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 8), _mm256_loadu_ps(b + i + 8), acc1);
acc2 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 16), _mm256_loadu_ps(b + i + 16), acc2);
acc3 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 24), _mm256_loadu_ps(b + i + 24), acc3);
}
acc0 = _mm256_add_ps(_mm256_add_ps(acc0, acc1), _mm256_add_ps(acc2, acc3));
for (; i + 8 <= n; i += 8) {
acc0 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i), acc0);
}
__m128 r = _mm_add_ps(_mm256_castps256_ps128(acc0), _mm256_extractf128_ps(acc0, 1));
r = _mm_hadd_ps(r, r);
r = _mm_hadd_ps(r, r);
float sum = _mm_cvtss_f32(r);
for (; i < n; i++)
sum += a[i] * b[i];
return sum;
#endif
}
void embed_vec_scale_inplace_avx(float *dst, float scale, int n) {
#if defined(__AVX512F__)
int i = 0;
__m512 s = _mm512_set1_ps(scale);
for (; i + 64 <= n; i += 64) {
_mm512_storeu_ps(dst + i, _mm512_mul_ps(_mm512_loadu_ps(dst + i), s));
_mm512_storeu_ps(dst + i + 16, _mm512_mul_ps(_mm512_loadu_ps(dst + i + 16), s));
_mm512_storeu_ps(dst + i + 32, _mm512_mul_ps(_mm512_loadu_ps(dst + i + 32), s));
_mm512_storeu_ps(dst + i + 48, _mm512_mul_ps(_mm512_loadu_ps(dst + i + 48), s));
}
for (; i + 16 <= n; i += 16) {
_mm512_storeu_ps(dst + i, _mm512_mul_ps(_mm512_loadu_ps(dst + i), s));
}
for (; i < n; i++)
dst[i] *= scale;
#else
int i = 0;
__m256 s = _mm256_set1_ps(scale);
for (; i + 32 <= n; i += 32) {
_mm256_storeu_ps(dst + i, _mm256_mul_ps(_mm256_loadu_ps(dst + i), s));
_mm256_storeu_ps(dst + i + 8, _mm256_mul_ps(_mm256_loadu_ps(dst + i + 8), s));
_mm256_storeu_ps(dst + i + 16, _mm256_mul_ps(_mm256_loadu_ps(dst + i + 16), s));
_mm256_storeu_ps(dst + i + 24, _mm256_mul_ps(_mm256_loadu_ps(dst + i + 24), s));
}
for (; i + 8 <= n; i += 8) {
_mm256_storeu_ps(dst + i, _mm256_mul_ps(_mm256_loadu_ps(dst + i), s));
}
for (; i < n; i++)
dst[i] *= scale;
#endif
}
void embed_vec_axpy_inplace_avx(float *dst, const float *src, float alpha, int n) {
#if defined(__AVX512F__)
int i = 0;
__m512 a = _mm512_set1_ps(alpha);
for (; i + 64 <= n; i += 64) {
_mm512_storeu_ps(dst + i,
_mm512_fmadd_ps(_mm512_loadu_ps(src + i), a, _mm512_loadu_ps(dst + i)));
_mm512_storeu_ps(dst + i + 16, _mm512_fmadd_ps(_mm512_loadu_ps(src + i + 16), a,
_mm512_loadu_ps(dst + i + 16)));
_mm512_storeu_ps(dst + i + 32, _mm512_fmadd_ps(_mm512_loadu_ps(src + i + 32), a,
_mm512_loadu_ps(dst + i + 32)));
_mm512_storeu_ps(dst + i + 48, _mm512_fmadd_ps(_mm512_loadu_ps(src + i + 48), a,
_mm512_loadu_ps(dst + i + 48)));
}
for (; i + 16 <= n; i += 16) {
_mm512_storeu_ps(dst + i,
_mm512_fmadd_ps(_mm512_loadu_ps(src + i), a, _mm512_loadu_ps(dst + i)));
}
for (; i < n; i++)
dst[i] += alpha * src[i];
#else
int i = 0;
__m256 a = _mm256_set1_ps(alpha);
for (; i + 32 <= n; i += 32) {
_mm256_storeu_ps(dst + i,
_mm256_fmadd_ps(_mm256_loadu_ps(src + i), a, _mm256_loadu_ps(dst + i)));
_mm256_storeu_ps(dst + i + 8, _mm256_fmadd_ps(_mm256_loadu_ps(src + i + 8), a,
_mm256_loadu_ps(dst + i + 8)));
_mm256_storeu_ps(dst + i + 16, _mm256_fmadd_ps(_mm256_loadu_ps(src + i + 16), a,
_mm256_loadu_ps(dst + i + 16)));
_mm256_storeu_ps(dst + i + 24, _mm256_fmadd_ps(_mm256_loadu_ps(src + i + 24), a,
_mm256_loadu_ps(dst + i + 24)));
}
for (; i + 8 <= n; i += 8) {
_mm256_storeu_ps(dst + i,
_mm256_fmadd_ps(_mm256_loadu_ps(src + i), a, _mm256_loadu_ps(dst + i)));
}
for (; i < n; i++)
dst[i] += alpha * src[i];
#endif
}
void embed_vec_scale_add_avx(float *dst, const float *src, float correction, int n) {
#if defined(__AVX512F__)
int i = 0;
__m512 c = _mm512_set1_ps(correction);
for (; i + 64 <= n; i += 64) {
_mm512_storeu_ps(dst + i,
_mm512_fmadd_ps(_mm512_loadu_ps(dst + i), c, _mm512_loadu_ps(src + i)));
_mm512_storeu_ps(dst + i + 16, _mm512_fmadd_ps(_mm512_loadu_ps(dst + i + 16), c,
_mm512_loadu_ps(src + i + 16)));
_mm512_storeu_ps(dst + i + 32, _mm512_fmadd_ps(_mm512_loadu_ps(dst + i + 32), c,
_mm512_loadu_ps(src + i + 32)));
_mm512_storeu_ps(dst + i + 48, _mm512_fmadd_ps(_mm512_loadu_ps(dst + i + 48), c,
_mm512_loadu_ps(src + i + 48)));
}
for (; i + 16 <= n; i += 16) {
_mm512_storeu_ps(dst + i,
_mm512_fmadd_ps(_mm512_loadu_ps(dst + i), c, _mm512_loadu_ps(src + i)));
}
for (; i < n; i++)
dst[i] = dst[i] * correction + src[i];
#else
int i = 0;
__m256 c = _mm256_set1_ps(correction);
for (; i + 32 <= n; i += 32) {
_mm256_storeu_ps(dst + i,
_mm256_fmadd_ps(_mm256_loadu_ps(dst + i), c, _mm256_loadu_ps(src + i)));
_mm256_storeu_ps(dst + i + 8, _mm256_fmadd_ps(_mm256_loadu_ps(dst + i + 8), c,
_mm256_loadu_ps(src + i + 8)));
_mm256_storeu_ps(dst + i + 16, _mm256_fmadd_ps(_mm256_loadu_ps(dst + i + 16), c,
_mm256_loadu_ps(src + i + 16)));
_mm256_storeu_ps(dst + i + 24, _mm256_fmadd_ps(_mm256_loadu_ps(dst + i + 24), c,
_mm256_loadu_ps(src + i + 24)));
}
for (; i + 8 <= n; i += 8) {
_mm256_storeu_ps(dst + i,
_mm256_fmadd_ps(_mm256_loadu_ps(dst + i), c, _mm256_loadu_ps(src + i)));
}
for (; i < n; i++)
dst[i] = dst[i] * correction + src[i];
#endif
}
#endif /* __AVX2__ && __FMA__ */
|