okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-2a-oracle 07df43732 (quant_parity golden reference leg, quant_golden_regen) into f-own-int67148809b

main
146.2 KiB3076 linesraw
1// x86_dots.inc - ggml's x86 dot products (llama.cpp ggml/src/ggml-cpu/arch/x86/quants.c at 3cf03257f219afbe7334045ff7c6a06ac68c627d,
2// Copyright (c) 2023-2026 The ggml authors, MIT) in the layer's signature.  Included by dot_avx.cpp and dot_avx2.cpp inside their tier namespace: the
3// #if __AVX2__ / __AVX__ / generic branches are ggml's own, so each tier gives the bits of ggml-cpu built for that flavour.
4// Needs before it: <immintrin.h>, internal.hpp, h2f / f2h (fp16 conversions) and the strata::quant::generic dot declarations.
5
6void dot_q4_0_q8_0(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
7    const int qk = QK8_0;
8    const int nb = n / qk;
9
10    assert(n % qk == 0);
11
12    const block_q4_0* STRATA_RESTRICT x = (const block_q4_0*) vx;
13    const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
14
15    int ib = 0;
16    float sumf = 0;
17
18#if defined(__AVX2__)
19    // Initialize accumulator with zeros
20    __m256 acc = _mm256_setzero_ps();
21
22    // Main loop
23    for (; ib < nb; ++ib) {
24        /* Compute combined scale for the block */
25        const __m256 d = _mm256_set1_ps( h2f(x[ib].d) * h2f(y[ib].d) );
26
27        __m256i qx = bytes_from_nibbles_32(x[ib].qs);
28
29        // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
30        const __m256i off = _mm256_set1_epi8( 8 );
31        qx = _mm256_sub_epi8( qx, off );
32
33        __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs);
34
35        const __m256 q = mul_sum_i8_pairs_float(qx, qy);
36
37        /* Multiply q with scale and accumulate */
38        acc = _mm256_fmadd_ps( d, q, acc );
39    }
40
41    sumf = hsum_float_8(acc);
42#elif defined(__AVX__)
43    __m256 accum = _mm256_setzero_ps();
44    for (; ib + 1 < nb; ib += 2) {
45        const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)x[ib + 0].qs);
46        const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs);
47        const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs);
48        const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs + 1);
49        const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs);
50        const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1);
51
52        const __m128i q4b_1_0 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), q4bits_1), _mm_set1_epi8(8));
53        const __m128i q4b_1_1 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), _mm_srli_epi16(q4bits_1, 4)), _mm_set1_epi8(8));
54        const __m128i q4b_2_0 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), q4bits_2), _mm_set1_epi8(8));
55        const __m128i q4b_2_1 = _mm_sub_epi8(_mm_and_si128(_mm_set1_epi8(15), _mm_srli_epi16(q4bits_2, 4)), _mm_set1_epi8(8));
56
57        const __m128i p16_1_0 = mul_add_epi8_sse(q4b_1_0, q8b_1_0);
58        const __m128i p16_1_1 = mul_add_epi8_sse(q4b_1_1, q8b_1_1);
59        const __m128i p16_2_0 = mul_add_epi8_sse(q4b_2_0, q8b_2_0);
60        const __m128i p16_2_1 = mul_add_epi8_sse(q4b_2_1, q8b_2_1);
61        const __m128i p_1 = _mm_add_epi16(p16_1_0, p16_1_1);
62        const __m128i p_2 = _mm_add_epi16(p16_2_0, p16_2_1);
63        const __m256 p =  sum_i16_pairs_float(p_2, p_1);
64
65        const __m256 deltas = quad_fp16_delta_float(x[ib].d, y[ib].d, x[ib + 1].d, y[ib + 1].d);
66        accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum);
67    }
68
69    sumf = hsum_float_8(accum);
70#elif defined(__SSSE3__)
71    // set constants
72    const __m128i lowMask = _mm_set1_epi8(0xF);
73    const __m128i off = _mm_set1_epi8(8);
74
75    // Initialize accumulator with zeros
76    __m128 acc_0 = _mm_setzero_ps();
77    __m128 acc_1 = _mm_setzero_ps();
78    __m128 acc_2 = _mm_setzero_ps();
79    __m128 acc_3 = _mm_setzero_ps();
80
81    for (; ib + 1 < nb; ib += 2) {
82        _mm_prefetch(&x[ib] + sizeof(block_q4_0), _MM_HINT_T0);
83        _mm_prefetch(&y[ib] + sizeof(block_q8_0), _MM_HINT_T0);
84
85        // Compute combined scale for the block 0 and 1
86        const __m128 d_0_1 = _mm_set1_ps( h2f(x[ib].d) * h2f(y[ib].d) );
87
88        const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[ib].qs);
89
90        __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
91        __m128i by_0 = _mm_loadu_si128((const __m128i *)y[ib].qs);
92        bx_0 = _mm_sub_epi8(bx_0, off);
93        const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
94
95        __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
96        __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[ib].qs + 16));
97        bx_1 = _mm_sub_epi8(bx_1, off);
98        const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
99
100        _mm_prefetch(&x[ib] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
101        _mm_prefetch(&y[ib] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
102
103        // Compute combined scale for the block 2 and 3
104        const __m128 d_2_3 = _mm_set1_ps( h2f(x[ib + 1].d) * h2f(y[ib + 1].d) );
105
106        const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs);
107
108        __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
109        __m128i by_2 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs);
110        bx_2 = _mm_sub_epi8(bx_2, off);
111        const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
112
113        __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
114        __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[ib + 1].qs + 16));
115        bx_3 = _mm_sub_epi8(bx_3, off);
116        const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
117
118        // Convert int32_t to float
119        __m128 p0 = _mm_cvtepi32_ps(i32_0);
120        __m128 p1 = _mm_cvtepi32_ps(i32_1);
121        __m128 p2 = _mm_cvtepi32_ps(i32_2);
122        __m128 p3 = _mm_cvtepi32_ps(i32_3);
123
124        // Apply the scale
125        __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
126        __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
127        __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
128        __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
129
130        // Acummulate
131        acc_0 = _mm_add_ps(p0_d, acc_0);
132        acc_1 = _mm_add_ps(p1_d, acc_1);
133        acc_2 = _mm_add_ps(p2_d, acc_2);
134        acc_3 = _mm_add_ps(p3_d, acc_3);
135    }
136
137    sumf = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
138
139#endif
140    for (; ib < nb; ++ib) {
141        int sumi0 = 0;
142        int sumi1 = 0;
143
144        for (int j = 0; j < qk/2; ++j) {
145            const int v0 = (x[ib].qs[j] & 0x0F) - 8;
146            const int v1 = (x[ib].qs[j] >>   4) - 8;
147
148            sumi0 += (v0 * y[ib].qs[j]);
149            sumi1 += (v1 * y[ib].qs[j + qk/2]);
150        }
151
152        int sumi = sumi0 + sumi1;
153        sumf += sumi*h2f(x[ib].d)*h2f(y[ib].d);
154    }
155
156    *s = sumf;
157}
158
159void dot_q4_1_q8_1(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
160    const int qk = QK8_1;
161    const int nb = n / qk;
162
163    assert(n % qk == 0);
164
165    const block_q4_1* STRATA_RESTRICT x = (const block_q4_1*) vx;
166    const block_q8_1* STRATA_RESTRICT y = (const block_q8_1*) vy;
167
168    int ib = 0;
169
170#if defined(__AVX2__) || defined(__AVX__)
171    // Initialize accumulator with zeros
172    __m256 acc = _mm256_setzero_ps();
173
174    float summs = 0;
175
176    // Main loop
177    for (; ib < nb; ++ib) {
178        const float d0 = h2f(x[ib].d);
179        const float d1 = h2f(y[ib].d);
180
181        summs += h2f(x[ib].m) * h2f(y[ib].s);
182
183        const __m256 d0v = _mm256_set1_ps( d0 );
184        const __m256 d1v = _mm256_set1_ps( d1 );
185
186        // Compute combined scales
187        const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
188
189        // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
190        const __m256i qx = bytes_from_nibbles_32(x[ib].qs);
191        const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[ib].qs );
192
193        const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
194
195        // Accumulate d0*d1*x*y
196#if defined(__AVX2__)
197        acc = _mm256_fmadd_ps( d0d1, xy, acc );
198#else
199        acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
200#endif
201    }
202
203    *s = hsum_float_8(acc) + summs;
204#else
205    UNUSED(nb);
206    UNUSED(x);
207    UNUSED(y);
208    UNUSED(ib);
209    generic::dot_q4_1_q8_1_generic(n, s, vx, vy);
210#endif
211}
212
213void dot_q5_0_q8_0(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
214    const int qk = QK8_0;
215    const int nb = n / qk;
216
217    int ib = 0;
218
219    assert(n % qk == 0);
220    assert(qk == QK5_0);
221
222    const block_q5_0* STRATA_RESTRICT x = (const block_q5_0*) vx;
223    const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
224
225#if defined(__AVX2__)
226    // Initialize accumulator with zeros
227    __m256 acc = _mm256_setzero_ps();
228
229    // Main loop
230    for (; ib < nb; ++ib) {
231        /* Compute combined scale for the block */
232        const __m256 d = _mm256_set1_ps(h2f(x[ib].d) * h2f(y[ib].d));
233
234        __m256i qx = bytes_from_nibbles_32(x[ib].qs);
235        __m256i bxhi = bytes_from_bits_32(x[ib].qh);
236        bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
237        qx = _mm256_or_si256(qx, bxhi);
238
239        __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs);
240
241        const __m256 q = mul_sum_i8_pairs_float(qx, qy);
242
243        /* Multiply q with scale and accumulate */
244        acc = _mm256_fmadd_ps(d, q, acc);
245    }
246
247    *s = hsum_float_8(acc);
248#elif defined(__AVX__)
249    // Initialize accumulator with zeros
250    __m256 acc = _mm256_setzero_ps();
251    __m128i mask = _mm_set1_epi8((char)0xF0);
252
253    // Main loop
254    for (; ib < nb; ++ib) {
255        /* Compute combined scale for the block */
256        const __m256 d = _mm256_set1_ps(h2f(x[ib].d) * h2f(y[ib].d));
257
258        __m256i bx_0 = bytes_from_nibbles_32(x[ib].qs);
259        const __m256i bxhi = bytes_from_bits_32(x[ib].qh);
260        __m128i bxhil = _mm256_castsi256_si128(bxhi);
261        __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
262        bxhil = _mm_andnot_si128(bxhil, mask);
263        bxhih = _mm_andnot_si128(bxhih, mask);
264        __m128i bxl = _mm256_castsi256_si128(bx_0);
265        __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
266        bxl = _mm_or_si128(bxl, bxhil);
267        bxh = _mm_or_si128(bxh, bxhih);
268        bx_0 = MM256_SET_M128I(bxh, bxl);
269
270        const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[ib].qs);
271
272        const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
273
274        /* Multiply q with scale and accumulate */
275        acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
276    }
277
278    *s = hsum_float_8(acc);
279#else
280    UNUSED(nb);
281    UNUSED(ib);
282    UNUSED(x);
283    UNUSED(y);
284    generic::dot_q5_0_q8_0_generic(n, s, vx, vy);
285#endif
286}
287
288void dot_q5_1_q8_1(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
289    const int qk = QK8_1;
290    const int nb = n / qk;
291
292    int ib = 0;
293
294    assert(n % qk == 0);
295    assert(qk == QK5_1);
296
297    const block_q5_1* STRATA_RESTRICT x = (const block_q5_1*) vx;
298    const block_q8_1* STRATA_RESTRICT y = (const block_q8_1*) vy;
299
300#if defined(__AVX2__)
301    // Initialize accumulator with zeros
302    __m256 acc = _mm256_setzero_ps();
303
304    float summs = 0.0f;
305
306    // Main loop
307    for (; ib < nb; ++ib) {
308        const __m256 dx = _mm256_set1_ps(h2f(x[ib].d));
309
310        summs += h2f(x[ib].m) * h2f(y[ib].s);
311
312        __m256i qx = bytes_from_nibbles_32(x[ib].qs);
313        __m256i bxhi = bytes_from_bits_32(x[ib].qh);
314        bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
315        qx = _mm256_or_si256(qx, bxhi);
316
317        const __m256 dy = _mm256_set1_ps(h2f(y[ib].d));
318        const __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs);
319
320        const __m256 q = mul_sum_us8_pairs_float(qx, qy);
321
322        acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
323    }
324
325    *s = hsum_float_8(acc) + summs;
326#elif defined(__AVX__)
327    // Initialize accumulator with zeros
328    __m256 acc = _mm256_setzero_ps();
329    __m128i mask = _mm_set1_epi8(0x10);
330
331    float summs = 0.0f;
332
333    // Main loop
334    for (; ib < nb; ++ib) {
335        const __m256 dx = _mm256_set1_ps(h2f(x[ib].d));
336
337        summs += h2f(x[ib].m) * h2f(y[ib].s);
338
339        __m256i bx_0 = bytes_from_nibbles_32(x[ib].qs);
340        const __m256i bxhi = bytes_from_bits_32(x[ib].qh);
341        __m128i bxhil = _mm256_castsi256_si128(bxhi);
342        __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
343        bxhil = _mm_and_si128(bxhil, mask);
344        bxhih = _mm_and_si128(bxhih, mask);
345        __m128i bxl = _mm256_castsi256_si128(bx_0);
346        __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
347        bxl = _mm_or_si128(bxl, bxhil);
348        bxh = _mm_or_si128(bxh, bxhih);
349        bx_0 = MM256_SET_M128I(bxh, bxl);
350
351        const __m256 dy = _mm256_set1_ps(h2f(y[ib].d));
352        const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[ib].qs);
353
354        const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
355
356        acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
357    }
358
359    *s = hsum_float_8(acc) + summs;
360#else
361    UNUSED(nb);
362    UNUSED(ib);
363    UNUSED(x);
364    UNUSED(y);
365    generic::dot_q5_1_q8_1_generic(n, s, vx, vy);
366#endif
367}
368
369void dot_q8_0_q8_0(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
370    const int qk = QK8_0;
371    const int nb = n / qk;
372
373    assert(n % qk == 0);
374
375    const block_q8_0* STRATA_RESTRICT x = (const block_q8_0*) vx;
376    const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
377
378    int ib = 0;
379    float sumf = 0;
380
381#if defined(__AVX2__)
382    // Initialize accumulator with zeros
383    __m256 acc = _mm256_setzero_ps();
384
385    // Main loop
386    for (; ib < nb; ++ib) {
387        // Compute combined scale for the block
388        const __m256 d = _mm256_set1_ps(h2f(x[ib].d) * h2f(y[ib].d));
389        __m256i qx = _mm256_loadu_si256((const __m256i *)x[ib].qs);
390        __m256i qy = _mm256_loadu_si256((const __m256i *)y[ib].qs);
391
392        const __m256 q = mul_sum_i8_pairs_float(qx, qy);
393
394        // Multiply q with scale and accumulate
395        acc = _mm256_fmadd_ps( d, q, acc );
396    }
397
398    sumf = hsum_float_8(acc);
399#elif defined(__AVX__)
400    __m256 accum = _mm256_setzero_ps();
401
402    for (; ib + 1 < nb; ib += 2) {
403        const __m128i qx_1_0 = _mm_loadu_si128((const __m128i *)x[ib].qs);
404        const __m128i qx_1_1 = _mm_loadu_si128((const __m128i *)x[ib].qs + 1);
405        const __m128i qx_2_0 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs);
406        const __m128i qx_2_1 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs + 1);
407        const __m128i qy_1_0 = _mm_loadu_si128((const __m128i *)y[ib].qs);
408        const __m128i qy_1_1 = _mm_loadu_si128((const __m128i *)y[ib].qs + 1);
409        const __m128i qy_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs);
410        const __m128i qy_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1);
411
412        const __m256 p = mul_sum_i8_quad_float(qx_1_0, qx_1_1, qx_2_0, qx_2_1, qy_1_0, qy_1_1, qy_2_0, qy_2_1);
413        const __m256 deltas = quad_fp16_delta_float(x[ib].d, y[ib].d, x[ib + 1].d, y[ib + 1].d);
414        accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum);
415    }
416
417    sumf = hsum_float_8(accum);
418#endif
419    for (; ib < nb; ++ib) {
420        int sumi = 0;
421
422        for (int j = 0; j < qk; j++) {
423            sumi += x[ib].qs[j]*y[ib].qs[j];
424        }
425
426        sumf += sumi*(h2f(x[ib].d)*h2f(y[ib].d));
427    }
428
429    *s = sumf;
430}
431
432void dot_iq4_nl_q8_0(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
433    assert(n % QK4_NL == 0);
434    static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
435
436    const block_iq4_nl* STRATA_RESTRICT x = (const block_iq4_nl*) vx;
437    const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
438
439    const int nb = n / QK4_NL;
440
441    int ib = 0;
442    float sumf = 0;
443
444#if defined __AVX2__
445
446    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
447    const __m128i m4b  = _mm_set1_epi8(0x0f);
448    const __m256i mone = _mm256_set1_epi16(1);
449
450    __m256 accum1 = _mm256_setzero_ps();
451    __m256 accum2 = _mm256_setzero_ps();
452    for (; ib + 1 < nb; ib += 2) {
453        const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[ib + 0].qs);
454        const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[ib + 1].qs);
455        const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[ib + 0].qs);
456        const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[ib + 1].qs);
457        const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
458                                              _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
459        const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
460                                              _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
461        const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
462        const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
463        const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
464        const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
465        accum1 = _mm256_fmadd_ps(_mm256_set1_ps(h2f(y[ib + 0].d)*h2f(x[ib + 0].d)),
466                _mm256_cvtepi32_ps(p_1), accum1);
467        accum2 = _mm256_fmadd_ps(_mm256_set1_ps(h2f(y[ib + 1].d)*h2f(x[ib + 1].d)),
468                _mm256_cvtepi32_ps(p_2), accum2);
469    }
470
471    sumf = hsum_float_8(_mm256_add_ps(accum1, accum2));
472
473#elif defined __AVX__
474    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
475    const __m128i m4b  = _mm_set1_epi8(0x0f);
476
477    __m256 accum = _mm256_setzero_ps();
478    for (; ib + 1 < nb; ib += 2) {
479        const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)x[ib + 0].qs);
480        const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs);
481        const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs);
482        const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs + 1);
483        const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs);
484        const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1);
485
486        const __m128i q4b_1_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b));
487        const __m128i q4b_1_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b));
488        const __m128i q4b_2_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b));
489        const __m128i q4b_2_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b));
490
491        const __m256 p = mul_sum_i8_quad_float(q4b_1_0, q4b_1_1, q4b_2_0, q4b_2_1, q8b_1_0, q8b_1_1, q8b_2_0, q8b_2_1);
492        const __m256 deltas = quad_fp16_delta_float(x[ib].d, y[ib].d, x[ib + 1].d, y[ib + 1].d);
493        accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum);
494    }
495
496    sumf = hsum_float_8(accum);
497
498#endif
499    for (; ib < nb; ++ib) {
500        const float d = h2f(y[ib].d)*h2f(x[ib].d);
501        int sumi1 = 0, sumi2 = 0;
502        for (int j = 0; j < QK4_NL/2; ++j) {
503            sumi1 += y[ib].qs[j+       0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
504            sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >>  4];
505        }
506        // Pinned unfused (a multiply, then an add).  The contract is ggml-cpu as GCC 13.3 builds it (gnu11, -O3 -mfma), which does not fuse this tail; our C++
507        // file (-ffp-contract=fast) would.  GCC 14.3 and 15.2 DO fuse it inside ggml-cpu (55 rows of the oracle, 1-2 ulp, one Broadwell box, 11-10), MSVC 19.44
508        // does not; our result is the same bytes under all four.  So the oracle's reference is tests/quant/golden_x86.txt, not the ggml a newer GCC builds.
509        { float t = d * (float) (sumi1 + sumi2); STRATA_NOFUSE(t); sumf += t; }
510    }
511    *s = sumf;
512}
513
514void dot_mxfp4_q8_0(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
515    assert(n % QK_MXFP4 == 0);
516    static_assert(QK_MXFP4 == QK8_0, "QK_MXFP4 and QK8_0 must be the same");
517
518    const block_mxfp4* STRATA_RESTRICT x = (const block_mxfp4*) vx;
519    const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
520
521    const int nb = n / QK_MXFP4;
522
523    int ib = 0;
524    float sumf = 0;
525
526#if defined __AVX2__
527
528    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4);
529    const __m128i m4b  = _mm_set1_epi8(0x0f);
530    const __m256i mone = _mm256_set1_epi16(1);
531
532    __m256 accum1 = _mm256_setzero_ps();
533    __m256 accum2 = _mm256_setzero_ps();
534
535    for (; ib + 1 < nb; ib += 2) {
536        const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[ib + 0].qs);
537        const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[ib + 1].qs);
538        const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[ib + 0].qs);
539        const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[ib + 1].qs);
540        const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
541                                              _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
542        const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
543                                              _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
544        const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
545        const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
546        const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
547        const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
548        const __m256 scale0 = _mm256_set1_ps(h2f(y[ib + 0].d)*e8m0_to_fp32_half(x[ib + 0].e));
549        const __m256 scale1 = _mm256_set1_ps(h2f(y[ib + 1].d)*e8m0_to_fp32_half(x[ib + 1].e));
550        accum1 = _mm256_fmadd_ps(scale0, _mm256_cvtepi32_ps(p_1), accum1);
551        accum2 = _mm256_fmadd_ps(scale1, _mm256_cvtepi32_ps(p_2), accum2);
552    }
553
554    sumf = hsum_float_8(_mm256_add_ps(accum1, accum2));
555
556#elif defined __AVX__
557    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4);
558    const __m128i m4b  = _mm_set1_epi8(0x0f);
559
560    __m256 accum = _mm256_setzero_ps();
561    for (; ib + 1 < nb; ib += 2) {
562        const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)x[ib + 0].qs);
563        const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)x[ib + 1].qs);
564        const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs);
565        const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)y[ib + 0].qs + 1);
566        const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs);
567        const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)y[ib + 1].qs + 1);
568
569        const __m128i q4b_1_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b));
570        const __m128i q4b_1_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b));
571        const __m128i q4b_2_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b));
572        const __m128i q4b_2_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b));
573
574        const __m256 p = mul_sum_i8_quad_float(q4b_1_0, q4b_1_1, q4b_2_0, q4b_2_1, q8b_1_0, q8b_1_1, q8b_2_0, q8b_2_1);
575        const __m256 deltas = quad_mx_delta_float(x[ib].e, y[ib].d, x[ib + 1].e, y[ib + 1].d);
576        accum = _mm256_add_ps(_mm256_mul_ps(deltas, p), accum);
577    }
578
579    sumf = hsum_float_8(accum);
580
581#endif
582    for (; ib < nb; ++ib) {
583        const float d = h2f(y[ib].d)*e8m0_to_fp32_half(x[ib].e);
584        int sumi1 = 0;
585        int sumi2 = 0;
586        for (int j = 0; j < QK_MXFP4/2; ++j) {
587            sumi1 += y[ib].qs[j +          0] * kvalues_fp4[x[ib].qs[j] & 0xf];
588            sumi2 += y[ib].qs[j + QK_MXFP4/2] * kvalues_fp4[x[ib].qs[j] >>  4];
589        }
590        sumf += d * (sumi1 + sumi2);
591    }
592    *s = sumf;
593}
594
595void dot_nvfp4_q8_0(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
596    assert(n % QK_NVFP4 == 0);
597
598    const block_nvfp4* STRATA_RESTRICT x = (const block_nvfp4*) vx;
599    const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
600
601    const int nb = n / QK_NVFP4;
602    int ib = 0;
603    float sumf = 0;
604
605#if defined(__AVX2__)
606
607    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4);
608    const __m128i m4b  = _mm_set1_epi8(0x0f);
609    const __m256i mone = _mm256_set1_epi16(1);
610
611    __m256 accum = _mm256_setzero_ps();
612    for(; ib < nb; ib++){
613
614        const __m128i q4bits_01 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 0));
615        const __m128i q4bits_23 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 16));
616
617        const __m256i q8_01 = _mm256_loadu_si256((const __m256i *)y[2*ib + 0].qs);
618        const __m256i q8_23 = _mm256_loadu_si256((const __m256i *)y[2*ib + 1].qs);
619
620        const __m128i q4_01_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_01, m4b));
621        const __m128i q4_01_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_01, 4), m4b));
622        const __m128i q4_23_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_23, m4b));
623        const __m128i q4_23_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_23, 4), m4b));
624
625        //reordering
626        const __m256i q4_01 = MM256_SET_M128I(_mm_unpackhi_epi64(q4_01_lo,q4_01_hi), _mm_unpacklo_epi64(q4_01_lo,q4_01_hi));
627        const __m256i q4_23 = MM256_SET_M128I(_mm_unpackhi_epi64(q4_23_lo,q4_23_hi),_mm_unpacklo_epi64(q4_23_lo,q4_23_hi));
628
629        const __m256i p01 = mul_add_epi8(q4_01,q8_01);
630        const __m256i p_1 = _mm256_madd_epi16(p01, mone);
631
632        const __m256i p23 = mul_add_epi8(q4_23,q8_23);
633        const __m256i p_2 = _mm256_madd_epi16(p23, mone);
634
635        const float dy0 = h2f(y[2*ib].d);
636        const float dy1 = h2f(y[2*ib+1].d);
637
638        const float s0 = ue4m3_to_fp32(x[ib].d[0]) * dy0;
639        const float s1 = ue4m3_to_fp32(x[ib].d[1]) * dy0;
640        const float s2 = ue4m3_to_fp32(x[ib].d[2]) * dy1;
641        const float s3 = ue4m3_to_fp32(x[ib].d[3]) * dy1;
642
643        const __m256 scales01 = _mm256_set_m128(_mm_set1_ps(s1), _mm_set1_ps(s0));
644        const __m256 scales23 = _mm256_set_m128(_mm_set1_ps(s3), _mm_set1_ps(s2));
645
646        accum = _mm256_fmadd_ps(scales01, _mm256_cvtepi32_ps(p_1), accum);
647        accum = _mm256_fmadd_ps(scales23, _mm256_cvtepi32_ps(p_2), accum);
648    }
649    sumf = hsum_float_8(accum);
650
651#elif defined(__AVX__)
652
653    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_fp4);
654    const __m128i m4b  = _mm_set1_epi8(0x0f);
655
656    __m256 accum = _mm256_setzero_ps();
657    for(; ib < nb; ib++){
658
659        const __m128i q4bits_01 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 0));
660        const __m128i q4bits_23 = _mm_loadu_si128((const __m128i *)(x[ib].qs + 16));
661
662        const __m128i q8_0 = _mm_loadu_si128((const __m128i *)(y[2*ib + 0].qs + 0));
663        const __m128i q8_1 = _mm_loadu_si128((const __m128i *)(y[2*ib + 0].qs + 16));
664        const __m128i q8_2 = _mm_loadu_si128((const __m128i *)(y[2*ib + 1].qs + 0));
665        const __m128i q8_3 = _mm_loadu_si128((const __m128i *)(y[2*ib + 1].qs + 16));
666
667        const __m128i q4_01_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_01, m4b));
668        const __m128i q4_01_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_01, 4), m4b));
669        const __m128i q4_23_lo = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_23, m4b));
670        const __m128i q4_23_hi = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_23, 4), m4b));
671
672        const __m128i q4_0 = _mm_unpacklo_epi64(q4_01_lo, q4_01_hi);
673        const __m128i q4_1 = _mm_unpackhi_epi64(q4_01_lo, q4_01_hi);
674        const __m128i q4_2 = _mm_unpacklo_epi64(q4_23_lo, q4_23_hi);
675        const __m128i q4_3 = _mm_unpackhi_epi64(q4_23_lo, q4_23_hi);
676
677        const __m128i p0_i32 = mul_sum_i8_pairs(q4_0, q8_0);
678        const __m128i p1_i32 = mul_sum_i8_pairs(q4_1, q8_1);
679        const __m128i p2_i32 = mul_sum_i8_pairs(q4_2, q8_2);
680        const __m128i p3_i32 = mul_sum_i8_pairs(q4_3, q8_3);
681
682        const __m128 p0 = _mm_cvtepi32_ps(p0_i32);
683        const __m128 p1 = _mm_cvtepi32_ps(p1_i32);
684        const __m128 p2 = _mm_cvtepi32_ps(p2_i32);
685        const __m128 p3 = _mm_cvtepi32_ps(p3_i32);
686
687        const __m256 p01 = _mm256_set_m128(p1, p0);
688        const __m256 p23 = _mm256_set_m128(p3, p2);
689
690        const float dy0 = h2f(y[2*ib].d);
691        const float dy1 = h2f(y[2*ib+1].d);
692
693        const float s0 = ue4m3_to_fp32(x[ib].d[0]) * dy0;
694        const float s1 = ue4m3_to_fp32(x[ib].d[1]) * dy0;
695        const float s2 = ue4m3_to_fp32(x[ib].d[2]) * dy1;
696        const float s3 = ue4m3_to_fp32(x[ib].d[3]) * dy1;
697
698        const __m256 scales01 = _mm256_set_m128(_mm_set1_ps(s1), _mm_set1_ps(s0));
699        const __m256 scales23 = _mm256_set_m128(_mm_set1_ps(s3), _mm_set1_ps(s2));
700
701        accum = _mm256_add_ps(accum, _mm256_mul_ps(p01, scales01));
702        accum = _mm256_add_ps(accum, _mm256_mul_ps(p23, scales23));
703    }
704    sumf = hsum_float_8(accum);
705
706#endif
707
708    for (;ib < nb; ++ib) {
709        for (int s_idx = 0; s_idx < 4; ++s_idx) {
710            const float d = ue4m3_to_fp32(x[ib].d[s_idx]);
711            const int q8_block = s_idx / 2;
712            const int q8_off   = (s_idx % 2) * QK_NVFP4_SUB;
713            const float dy = h2f(y[2*ib + q8_block].d);
714
715            int sumi_lo = 0, sumi_hi = 0;
716            for (int j = 0; j < QK_NVFP4_SUB/2; ++j) {
717                const uint8_t qv = x[ib].qs[s_idx*(QK_NVFP4_SUB/2) + j];
718                sumi_lo += y[2*ib + q8_block].qs[q8_off + j +               0] * kvalues_fp4[qv & 0xf];
719                sumi_hi += y[2*ib + q8_block].qs[q8_off + j + QK_NVFP4_SUB/2] * kvalues_fp4[qv >>  4];
720            }
721
722            sumf += dy * d * (sumi_lo + sumi_hi);
723        }
724    }
725    *s = sumf;
726}
727
728void dot_q2_K_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
729
730    const block_q2_K* STRATA_RESTRICT x = (const block_q2_K*) vx;
731    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
732
733    const int nb = n / QK_K;
734
735#if defined __AVX2__
736
737    const __m256i m3 = _mm256_set1_epi8(3);
738    const __m128i m4 = _mm_set1_epi8(0xF);
739
740    __m256 acc = _mm256_setzero_ps();
741
742    for (int i = 0; i < nb; ++i) {
743
744        const float d = y[i].d * h2f(x[i].d);
745        const float dmin = -y[i].d * h2f(x[i].dmin);
746
747        const uint8_t * STRATA_RESTRICT q2 = x[i].qs;
748        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
749
750        const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
751        const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
752        const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
753        const __m256i mins = _mm256_cvtepi8_epi16(mins8);
754        const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
755
756        acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
757
758        const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
759        const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
760        const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
761        const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
762
763        __m256i sumi = _mm256_setzero_si256();
764
765        for (int j = 0; j < QK_K/128; ++j) {
766
767            const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
768
769            const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
770            const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
771            const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
772            const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
773
774            const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
775            const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
776            const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
777            const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
778
779            __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
780            __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
781            __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
782            __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
783
784            p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
785            p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
786            p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
787            p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
788
789            p0 = _mm256_add_epi32(p0, p1);
790            p2 = _mm256_add_epi32(p2, p3);
791
792            sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
793        }
794
795        acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
796
797    }
798
799    *s = hsum_float_8(acc);
800
801#elif defined __AVX__
802
803    const __m128i m3 = _mm_set1_epi8(0x3);
804    const __m128i m4 = _mm_set1_epi8(0xF);
805    const __m128i m2 = _mm_set1_epi8(0x2);
806
807    __m256 acc = _mm256_setzero_ps();
808
809    for (int i = 0; i < nb; ++i) {
810
811        const float dall = y[i].d * h2f(x[i].d);
812        const float dmin = -y[i].d * h2f(x[i].dmin);
813
814        const uint8_t * STRATA_RESTRICT q2 = x[i].qs;
815        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
816
817        // load mins and scales from block_q2_K.scales[QK_K/16]
818        const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
819        const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
820        const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
821        const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
822        const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
823
824        // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
825        const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
826        const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
827
828        // sumf += -dmin * summs in 32bits*8
829        acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
830
831        const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
832        const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
833        const __m128i scales[2] = { scales_0, scales_1 };
834
835        __m128i sumi_0 = _mm_setzero_si128();
836        __m128i sumi_1 = _mm_setzero_si128();
837
838        for (int j = 0; j < QK_K/128; ++j) {
839
840            // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
841            const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
842            const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
843            const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
844            const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
845            const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
846            const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
847            const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
848            const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
849
850            // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
851            __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
852            const __m128i q2_0 = _mm_and_si128(q2bits, m3);
853            const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
854            const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
855            const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
856            q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
857            const __m128i q2_1 = _mm_and_si128(q2bits, m3);
858            const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
859            const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
860            const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
861
862            // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
863            __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
864            __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
865            __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
866            __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
867            __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
868            __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
869            __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
870            __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
871
872            // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
873            __m128i shuffle = _mm_set1_epi16(0x0100);
874            p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
875            shuffle = _mm_add_epi16(shuffle, m2);
876            p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
877            shuffle = _mm_add_epi16(shuffle, m2);
878            p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
879            shuffle = _mm_add_epi16(shuffle, m2);
880            p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
881            shuffle = _mm_add_epi16(shuffle, m2);
882            p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
883            shuffle = _mm_add_epi16(shuffle, m2);
884            p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
885            shuffle = _mm_add_epi16(shuffle, m2);
886            p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
887            shuffle = _mm_add_epi16(shuffle, m2);
888            p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
889
890            p0 = _mm_add_epi32(p0, p1);
891            p2 = _mm_add_epi32(p2, p3);
892            p4 = _mm_add_epi32(p4, p5);
893            p6 = _mm_add_epi32(p6, p7);
894
895            // isum in 32bits*4*2
896            sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
897            sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
898        }
899
900        // sumf += dall * isum - dmin * summs in 32bits
901        __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
902        acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
903    }
904
905    *s = hsum_float_8(acc);
906
907#else
908    UNUSED(x);
909    UNUSED(y);
910    UNUSED(nb);
911    generic::dot_q2_K_q8_K_generic(n, s, vx, vy);
912#endif
913}
914
915void dot_q3_K_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
916    assert(n % QK_K == 0);
917
918    const uint32_t kmask1 = 0x03030303;
919    const uint32_t kmask2 = 0x0f0f0f0f;
920
921    const block_q3_K* STRATA_RESTRICT x = (const block_q3_K*) vx;
922    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
923
924    const int nb = n / QK_K;
925
926#if defined __AVX2__
927
928    const __m256i m3 = _mm256_set1_epi8(3);
929    const __m256i mone = _mm256_set1_epi8(1);
930    const __m128i m32 = _mm_set1_epi8(32);
931
932    __m256 acc = _mm256_setzero_ps();
933
934    uint32_t aux[3];
935
936    for (int i = 0; i < nb; ++i) {
937
938        const float d = y[i].d * h2f(x[i].d);
939
940        const uint8_t * STRATA_RESTRICT q3 = x[i].qs;
941        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
942
943        // Set up scales
944        memcpy(aux, x[i].scales, 12);
945        __m128i scales128 = _mm_set_epi32(
946                ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
947                ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
948                (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
949                (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
950        scales128 = _mm_sub_epi8(scales128, m32);
951        const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
952        const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
953        const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
954        const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
955
956        // high bit
957        const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
958
959        // integer accumulator
960        __m256i sumi = _mm256_setzero_si256();
961
962        int bit = 0;
963        int is  = 0;
964
965        for (int j = 0; j < QK_K/128; ++j) {
966            // load low 2 bits
967            const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
968
969            // prepare low and high bits
970            const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
971            const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
972            ++bit;
973
974            const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
975            const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
976            ++bit;
977
978            const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
979            const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
980            ++bit;
981
982            const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
983            const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
984            ++bit;
985
986            // load Q8 quants
987            const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
988            const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
989            const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
990            const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
991
992            // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
993            // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
994            // and 2 if the high bit was set)
995            __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
996            __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
997            __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
998            __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
999
1000            __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
1001            __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
1002            __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
1003            __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
1004
1005            p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
1006            p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
1007            p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
1008            p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
1009
1010            // multiply with scales
1011            p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
1012            p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
1013            p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
1014            p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
1015
1016            // accumulate
1017            p16_0 = _mm256_add_epi32(p16_0, p16_1);
1018            p16_2 = _mm256_add_epi32(p16_2, p16_3);
1019            sumi  = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
1020
1021        }
1022
1023        // multiply with block scale and accumulate
1024        acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
1025
1026    }
1027
1028    *s = hsum_float_8(acc);
1029
1030#elif defined __AVX__
1031
1032    const __m128i m3 = _mm_set1_epi8(3);
1033    const __m128i mone = _mm_set1_epi8(1);
1034    const __m128i m32 = _mm_set1_epi8(32);
1035    const __m128i m2 = _mm_set1_epi8(2);
1036
1037    __m256 acc = _mm256_setzero_ps();
1038
1039    const uint32_t *aux;
1040
1041    for (int i = 0; i < nb; ++i) {
1042
1043        const float d = y[i].d * h2f(x[i].d);
1044
1045        const uint8_t * STRATA_RESTRICT q3 = x[i].qs;
1046        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1047
1048        // Set up scales
1049        aux = (const uint32_t *)x[i].scales;
1050        __m128i scales128 = _mm_set_epi32(
1051                ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
1052                ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
1053                (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
1054                (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
1055        scales128 = _mm_sub_epi8(scales128, m32);
1056        const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
1057        const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
1058        const __m128i scales[2] = { scales_0, scales_1 };
1059
1060        // high bit *128*2 from block_q3_K.hmask[QK_K/8]
1061        const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
1062        const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
1063
1064        // integer accumulator
1065        __m128i sumi_0 = _mm_setzero_si128();
1066        __m128i sumi_1 = _mm_setzero_si128();
1067
1068        for (int j = 0; j < QK_K/128; ++j) {
1069            // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
1070            const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
1071            const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
1072
1073            // prepare low and high bits
1074            const int bit = j << 2;
1075
1076            const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
1077            const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
1078            const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
1079            const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
1080
1081            const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
1082            const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
1083            const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
1084            const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
1085
1086            const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
1087            const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
1088            const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
1089            const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
1090
1091            const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
1092            const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
1093            const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
1094            const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
1095
1096            // load Q8 quants from block_q8_K.qs[QK_K]
1097            const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1098            const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1099            const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1100            const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1101            const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1102            const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1103            const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1104            const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1105
1106            // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
1107            // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
1108            // and 2 if the high bit was set)
1109            __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
1110            __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
1111            __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
1112            __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
1113            __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
1114            __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
1115            __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
1116            __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
1117
1118            __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
1119            __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
1120            __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
1121            __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
1122            __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
1123            __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
1124            __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
1125            __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
1126
1127            p16_0 = _mm_sub_epi16(p16_0, q8s_0);
1128            p16_1 = _mm_sub_epi16(p16_1, q8s_1);
1129            p16_2 = _mm_sub_epi16(p16_2, q8s_2);
1130            p16_3 = _mm_sub_epi16(p16_3, q8s_3);
1131            p16_4 = _mm_sub_epi16(p16_4, q8s_4);
1132            p16_5 = _mm_sub_epi16(p16_5, q8s_5);
1133            p16_6 = _mm_sub_epi16(p16_6, q8s_6);
1134            p16_7 = _mm_sub_epi16(p16_7, q8s_7);
1135
1136            // multiply with scales
1137            __m128i shuffle = _mm_set1_epi16(0x0100);
1138            p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
1139            shuffle = _mm_add_epi16(shuffle, m2);
1140            p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
1141            shuffle = _mm_add_epi16(shuffle, m2);
1142            p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
1143            shuffle = _mm_add_epi16(shuffle, m2);
1144            p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
1145            shuffle = _mm_add_epi16(shuffle, m2);
1146            p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
1147            shuffle = _mm_add_epi16(shuffle, m2);
1148            p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
1149            shuffle = _mm_add_epi16(shuffle, m2);
1150            p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
1151            shuffle = _mm_add_epi16(shuffle, m2);
1152            p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
1153
1154            // accumulate
1155            p16_0 = _mm_add_epi32(p16_0, p16_1);
1156            p16_2 = _mm_add_epi32(p16_2, p16_3);
1157            p16_4 = _mm_add_epi32(p16_4, p16_5);
1158            p16_6 = _mm_add_epi32(p16_6, p16_7);
1159            sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
1160            sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
1161
1162        }
1163
1164        // multiply with block scale and accumulate
1165        __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
1166        acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
1167
1168    }
1169
1170    *s = hsum_float_8(acc);
1171
1172#else
1173    UNUSED(kmask1);
1174    UNUSED(kmask2);
1175    UNUSED(x);
1176    UNUSED(y);
1177    UNUSED(nb);
1178    generic::dot_q3_K_q8_K_generic(n, s, vx, vy);
1179#endif
1180}
1181
1182void dot_q4_K_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
1183    assert(n % QK_K == 0);
1184
1185    const block_q4_K* STRATA_RESTRICT x = (const block_q4_K*) vx;
1186    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
1187
1188    const int nb = n / QK_K;
1189
1190    static const uint32_t kmask1 = 0x3f3f3f3f;
1191    static const uint32_t kmask2 = 0x0f0f0f0f;
1192    static const uint32_t kmask3 = 0x03030303;
1193
1194    uint32_t utmp[4];
1195
1196#if defined __AVX2__
1197
1198    const __m256i m4 = _mm256_set1_epi8(0xF);
1199
1200    __m256 acc = _mm256_setzero_ps();
1201    __m128 acc_m = _mm_setzero_ps();
1202
1203   for (int i = 0; i < nb; ++i) {
1204
1205        const float d = y[i].d * h2f(x[i].d);
1206        const float dmin = -y[i].d * h2f(x[i].dmin);
1207
1208        memcpy(utmp, x[i].scales, 12);
1209        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
1210        const uint32_t uaux = utmp[1] & kmask1;
1211        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
1212        utmp[2] = uaux;
1213        utmp[0] &= kmask1;
1214
1215        const uint8_t * STRATA_RESTRICT q4 = x[i].qs;
1216        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1217
1218        const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
1219
1220        const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
1221        const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
1222        const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
1223        acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
1224
1225        const __m128i sc128  = _mm256_extracti128_si256(mins_and_scales, 0);
1226        const __m256i scales = MM256_SET_M128I(sc128, sc128);
1227
1228        __m256i sumi = _mm256_setzero_si256();
1229
1230        for (int j = 0; j < QK_K/64; ++j) {
1231
1232            const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
1233            const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
1234
1235            const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
1236            const __m256i q4l = _mm256_and_si256(q4bits, m4);
1237            const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
1238
1239            const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1240            __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
1241            p16l = _mm256_madd_epi16(scale_l, p16l);
1242
1243            const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1244            __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
1245            p16h = _mm256_madd_epi16(scale_h, p16h);
1246            const __m256i sumj = _mm256_add_epi32(p16l, p16h);
1247
1248            sumi = _mm256_add_epi32(sumi, sumj);
1249        }
1250
1251        __m256 vd = _mm256_set1_ps(d);
1252        acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
1253
1254    }
1255
1256    acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
1257    acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
1258
1259    *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
1260
1261#elif defined __AVX__
1262
1263    const __m128i m4 = _mm_set1_epi8(0xF);
1264    const __m128i m2 = _mm_set1_epi8(0x2);
1265
1266    __m256 acc = _mm256_setzero_ps();
1267    __m128 acc_m = _mm_setzero_ps();
1268
1269   for (int i = 0; i < nb; ++i) {
1270
1271        const float d = y[i].d * h2f(x[i].d);
1272        const float dmin = -y[i].d * h2f(x[i].dmin);
1273
1274        const uint8_t * STRATA_RESTRICT q4 = x[i].qs;
1275        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1276
1277        memcpy(utmp, x[i].scales, 12);
1278        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
1279        const uint32_t uaux = utmp[1] & kmask1;
1280        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
1281        utmp[2] = uaux;
1282        utmp[0] &= kmask1;
1283
1284        const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
1285        const __m128i scales = _mm_cvtepu8_epi16(utmps);
1286        const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
1287
1288        const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
1289        const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
1290        const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
1291        const __m128i prod = _mm_madd_epi16(mins, q8s);
1292        acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
1293
1294        __m128i sumi_0 = _mm_setzero_si128();
1295        __m128i sumi_1 = _mm_setzero_si128();
1296
1297        __m128i shuffle = _mm_set1_epi16(0x0100);
1298        for (int j = 0; j < QK_K/64; ++j) {
1299
1300            const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
1301            shuffle = _mm_add_epi16(shuffle, m2);
1302            const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
1303            shuffle = _mm_add_epi16(shuffle, m2);
1304
1305            __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
1306            const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
1307            const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
1308            q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
1309            const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
1310            const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
1311
1312            const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1313            __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
1314            p16l = _mm_madd_epi16(scale_l, p16l);
1315            sumi_0 = _mm_add_epi32(sumi_0, p16l);
1316            const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1317            p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
1318            p16l = _mm_madd_epi16(scale_l, p16l);
1319            sumi_1 = _mm_add_epi32(sumi_1, p16l);
1320
1321            const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1322            __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
1323            p16h = _mm_madd_epi16(scale_h, p16h);
1324            sumi_0 = _mm_add_epi32(sumi_0, p16h);
1325            const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1326            p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
1327            p16h = _mm_madd_epi16(scale_h, p16h);
1328            sumi_1 = _mm_add_epi32(sumi_1, p16h);
1329
1330        }
1331
1332        __m256 vd = _mm256_set1_ps(d);
1333        __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
1334        acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
1335
1336    }
1337
1338    acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
1339    acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
1340
1341    *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
1342
1343#else
1344    UNUSED(x);
1345    UNUSED(y);
1346    UNUSED(nb);
1347    UNUSED(kmask1);
1348    UNUSED(kmask2);
1349    UNUSED(kmask3);
1350    UNUSED(utmp);
1351    generic::dot_q4_K_q8_K_generic(n, s, vx, vy);
1352#endif
1353}
1354
1355void dot_q5_K_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
1356    assert(n % QK_K == 0);
1357
1358    const block_q5_K* STRATA_RESTRICT x = (const block_q5_K*) vx;
1359    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
1360
1361    const int nb = n / QK_K;
1362
1363    static const uint32_t kmask1 = 0x3f3f3f3f;
1364    static const uint32_t kmask2 = 0x0f0f0f0f;
1365    static const uint32_t kmask3 = 0x03030303;
1366
1367    uint32_t utmp[4];
1368
1369#if defined __AVX2__
1370
1371    const __m256i m4 = _mm256_set1_epi8(0xF);
1372    const __m128i mzero = _mm_setzero_si128();
1373    const __m256i mone  = _mm256_set1_epi8(1);
1374
1375    __m256 acc = _mm256_setzero_ps();
1376
1377    float summs = 0.f;
1378
1379    for (int i = 0; i < nb; ++i) {
1380        const uint8_t * STRATA_RESTRICT q5 = x[i].qs;
1381        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1382
1383        const float d = y[i].d * h2f(x[i].d);
1384        const float dmin = -y[i].d * h2f(x[i].dmin);
1385
1386        memcpy(utmp, x[i].scales, 12);
1387        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
1388        const uint32_t uaux = utmp[1] & kmask1;
1389        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
1390        utmp[2] = uaux;
1391        utmp[0] &= kmask1;
1392
1393        const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
1394
1395        const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
1396        const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
1397        const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
1398        const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
1399        summs += dmin * _mm_extract_epi32(hsum, 0);
1400
1401        const __m128i sc128  = _mm256_extracti128_si256(mins_and_scales, 0);
1402        const __m256i scales = MM256_SET_M128I(sc128, sc128);
1403
1404        const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
1405        __m256i hmask = mone;
1406
1407        __m256i sumi = _mm256_setzero_si256();
1408
1409        int bit = 0;
1410
1411        for (int j = 0; j < QK_K/64; ++j) {
1412
1413            const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
1414            const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
1415
1416            const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
1417
1418            const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
1419            const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
1420            const __m256i q5_0  = _mm256_add_epi8(q5l_0, q5h_0);
1421            hmask = _mm256_slli_epi16(hmask, 1);
1422
1423            const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
1424            const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
1425            const __m256i q5_1  = _mm256_add_epi8(q5l_1, q5h_1);
1426            hmask = _mm256_slli_epi16(hmask, 1);
1427
1428            const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1429            const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1430
1431            __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
1432            __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
1433
1434            p16_0 = _mm256_madd_epi16(scale_0, p16_0);
1435            p16_1 = _mm256_madd_epi16(scale_1, p16_1);
1436
1437            sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
1438
1439        }
1440
1441        __m256 vd = _mm256_set1_ps(d);
1442        acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
1443
1444    }
1445
1446    *s = hsum_float_8(acc) + summs;
1447
1448#elif defined __AVX__
1449
1450    const __m128i m4 = _mm_set1_epi8(0xF);
1451    const __m128i mzero = _mm_setzero_si128();
1452    const __m128i mone  = _mm_set1_epi8(1);
1453    const __m128i m2 = _mm_set1_epi8(2);
1454
1455    __m256 acc = _mm256_setzero_ps();
1456
1457    float summs = 0.f;
1458
1459    for (int i = 0; i < nb; ++i) {
1460
1461        const float d = y[i].d * h2f(x[i].d);
1462        const float dmin = -y[i].d * h2f(x[i].dmin);
1463
1464        const uint8_t * STRATA_RESTRICT q5 = x[i].qs;
1465        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1466
1467        memcpy(utmp, x[i].scales, 12);
1468        utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
1469        const uint32_t uaux = utmp[1] & kmask1;
1470        utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
1471        utmp[2] = uaux;
1472        utmp[0] &= kmask1;
1473
1474        const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
1475        const __m128i scales = _mm_cvtepu8_epi16(utmps);
1476        const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
1477
1478        const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
1479        const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
1480        const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
1481        const __m128i prod = _mm_madd_epi16(mins, q8s);
1482        const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
1483        summs += dmin * _mm_extract_epi32(hsum, 0);
1484
1485        const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
1486        const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
1487        __m128i hmask = mone;
1488
1489        __m128i sumi_0 = _mm_setzero_si128();
1490        __m128i sumi_1 = _mm_setzero_si128();
1491
1492        int bit = 0;
1493
1494        __m128i shuffle = _mm_set1_epi16(0x0100);
1495        for (int j = 0; j < QK_K/64; ++j) {
1496
1497            const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
1498            shuffle = _mm_add_epi16(shuffle, m2);
1499            const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
1500            shuffle = _mm_add_epi16(shuffle, m2);
1501
1502            const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
1503            const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
1504
1505            __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
1506            __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
1507            __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
1508            __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
1509            __m128i q5_0  = _mm_add_epi8(q5l_0, q5h_0);
1510            __m128i q5_1  = _mm_add_epi8(q5l_1, q5h_1);
1511            hmask = _mm_slli_epi16(hmask, 1);
1512
1513            __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1514            __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1515            __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
1516            __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
1517            p16_0 = _mm_madd_epi16(scale_0, p16_0);
1518            p16_1 = _mm_madd_epi16(scale_0, p16_1);
1519
1520            q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
1521            q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
1522            q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
1523            q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
1524            q5_0  = _mm_add_epi8(q5l_0, q5h_0);
1525            q5_1  = _mm_add_epi8(q5l_1, q5h_1);
1526            hmask = _mm_slli_epi16(hmask, 1);
1527
1528            q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1529            q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1530            __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
1531            __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
1532            p16_2 = _mm_madd_epi16(scale_1, p16_2);
1533            p16_3 = _mm_madd_epi16(scale_1, p16_3);
1534
1535            sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
1536            sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
1537
1538        }
1539
1540        __m256 vd = _mm256_set1_ps(d);
1541        __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
1542        acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
1543
1544    }
1545
1546    *s = hsum_float_8(acc) + summs;
1547
1548#else
1549    UNUSED(x);
1550    UNUSED(y);
1551    UNUSED(nb);
1552    UNUSED(kmask1);
1553    UNUSED(kmask2);
1554    UNUSED(kmask3);
1555    UNUSED(utmp);
1556    generic::dot_q5_K_q8_K_generic(n, s, vx, vy);
1557#endif
1558}
1559
1560void dot_q6_K_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
1561    assert(n % QK_K == 0);
1562
1563    const block_q6_K* STRATA_RESTRICT x = (const block_q6_K*) vx;
1564    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
1565
1566    const int nb = n / QK_K;
1567
1568#if defined __AVX2__
1569
1570    const __m256i m3 = _mm256_set1_epi8(3);
1571    const __m256i m15 = _mm256_set1_epi8(15);
1572
1573    __m256 acc = _mm256_setzero_ps();
1574
1575    for (int i = 0; i < nb; ++i) {
1576
1577        const float d = y[i].d * h2f(x[i].d);
1578
1579        const uint8_t * STRATA_RESTRICT q4 = x[i].ql;
1580        const uint8_t * STRATA_RESTRICT qh = x[i].qh;
1581        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1582
1583        const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
1584        const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
1585        const __m256i scales_16 = _mm256_cvtepi8_epi16(scales);
1586        const __m256i q8sclsub = _mm256_slli_epi32(_mm256_madd_epi16(q8sums, scales_16), 5);
1587
1588        __m256i sumi = _mm256_setzero_si256();
1589
1590        int is = 0;
1591
1592        for (int j = 0; j < QK_K/128; ++j) {
1593            const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
1594            const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
1595            const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
1596
1597            const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m3), 4);
1598            const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, _mm256_set1_epi8(12)), 2);
1599            const __m256i q4h_2 = _mm256_and_si256(q4bitsH, _mm256_set1_epi8(48));
1600            const __m256i q4h_3 = _mm256_srli_epi16(_mm256_and_si256(q4bitsH, _mm256_set1_epi8(-64)), 2);
1601
1602            const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m15), q4h_0);
1603            const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m15), q4h_1);
1604            const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m15), q4h_2);
1605            const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m15), q4h_3);
1606
1607            const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1608            const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1609            const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1610            const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1611
1612            __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
1613            __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
1614            __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
1615            __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
1616
1617            const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
1618            const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
1619            const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
1620            const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
1621            is += 4;
1622
1623            p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
1624            p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
1625            p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
1626            p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
1627
1628            sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
1629            sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
1630
1631        }
1632
1633        sumi = _mm256_sub_epi32(sumi, q8sclsub);
1634        acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
1635    }
1636
1637    *s = hsum_float_8(acc);
1638
1639#elif defined __AVX__
1640
1641    const __m128i m3 = _mm_set1_epi8(3);
1642    const __m128i m15 = _mm_set1_epi8(15);
1643
1644    __m256 acc = _mm256_setzero_ps();
1645
1646    for (int i = 0; i < nb; ++i) {
1647
1648        const float d = y[i].d * h2f(x[i].d);
1649
1650        const uint8_t * STRATA_RESTRICT q4 = x[i].ql;
1651        const uint8_t * STRATA_RESTRICT qh = x[i].qh;
1652        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
1653
1654        // handle the q6_k -32 offset separately using bsums
1655        const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)y[i].bsums);
1656        const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)y[i].bsums + 1);
1657        const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
1658        const __m128i scales_16_0 = _mm_cvtepi8_epi16(scales);
1659        const __m128i scales_16_1 = _mm_cvtepi8_epi16(_mm_bsrli_si128(scales, 8));
1660        const __m128i q8sclsub_0 = _mm_slli_epi32(_mm_madd_epi16(q8sums_0, scales_16_0), 5);
1661        const __m128i q8sclsub_1 = _mm_slli_epi32(_mm_madd_epi16(q8sums_1, scales_16_1), 5);
1662
1663        __m128i sumi_0 = _mm_setzero_si128();
1664        __m128i sumi_1 = _mm_setzero_si128();
1665
1666        int is = 0;
1667
1668        for (int j = 0; j < QK_K/128; ++j) {
1669
1670            const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
1671            const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
1672
1673            const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
1674            const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
1675            const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, _mm_set1_epi8(12)), 2);
1676            const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, _mm_set1_epi8(12)), 2);
1677            const __m128i q4h_4 = _mm_and_si128(q4bitsH_0, _mm_set1_epi8(48));
1678            const __m128i q4h_5 = _mm_and_si128(q4bitsH_1, _mm_set1_epi8(48));
1679            const __m128i q4h_6 = _mm_srli_epi16(_mm_and_si128(q4bitsH_0, _mm_set1_epi8(-64)), 2);
1680            const __m128i q4h_7 = _mm_srli_epi16(_mm_and_si128(q4bitsH_1, _mm_set1_epi8(-64)), 2);
1681
1682            const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
1683            const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
1684            const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
1685            const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
1686
1687            const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m15), q4h_0);
1688            const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m15), q4h_1);
1689            const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m15), q4h_2);
1690            const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m15), q4h_3);
1691            const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m15), q4h_4);
1692            const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m15), q4h_5);
1693            const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m15), q4h_6);
1694            const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m15), q4h_7);
1695
1696            const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1697            const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1698            const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1699            const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1700            const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1701            const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1702            const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1703            const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
1704
1705            __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
1706            __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
1707            __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
1708            __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
1709            __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
1710            __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
1711            __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
1712            __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
1713
1714            const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
1715            const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
1716            const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
1717            const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
1718            is += 4;
1719
1720            p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
1721            p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_0, 8)), p16_1);
1722            p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
1723            p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_1, 8)), p16_3);
1724            p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
1725            p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_2, 8)), p16_5);
1726            p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
1727            p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_bsrli_si128(scale_3, 8)), p16_7);
1728
1729            sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
1730            sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
1731            sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
1732            sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
1733
1734        }
1735
1736        sumi_0 = _mm_sub_epi32(sumi_0, q8sclsub_0);
1737        sumi_1 = _mm_sub_epi32(sumi_1, q8sclsub_1);
1738        const __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
1739        acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi)), acc);
1740    }
1741
1742    *s = hsum_float_8(acc);
1743
1744#else
1745    UNUSED(x);
1746    UNUSED(y);
1747    UNUSED(nb);
1748    generic::dot_q6_K_q8_K_generic(n, s, vx, vy);
1749#endif
1750}
1751
1752void dot_iq1_s_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
1753    assert(n % QK_K == 0);
1754
1755    const block_iq1_s* STRATA_RESTRICT x = (const block_iq1_s*) vx;
1756    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
1757
1758    const int nb = n / QK_K;
1759
1760#if defined __AVX2__
1761
1762    __m256 accum = _mm256_setzero_ps();
1763    float accum1 = 0;
1764    for (int i = 0; i < nb; ++i) {
1765
1766        const int8_t   * q8 = y[i].qs;
1767        const uint8_t  * qs = x[i].qs;
1768        const uint16_t * qh = x[i].qh;
1769
1770        __m256i sumi = _mm256_setzero_si256();
1771        int sumi1 = 0;
1772        for (int ib = 0; ib < QK_K/32; ib += 2) {
1773#ifdef __BMI2__
1774            const uint64_t packed_idx1 = _pdep_u64(*(const uint32_t *)qs, 0x00ff00ff00ff00ffULL) | _pdep_u64(qh[ib], 0x700070007000700ULL);
1775            const uint64_t packed_idx2 = _pdep_u64(*(const uint32_t *)(qs + 4), 0x00ff00ff00ff00ffULL) | _pdep_u64(qh[ib + 1], 0x700070007000700ULL);
1776            const uint16_t *idx1 = (const uint16_t *)(&packed_idx1);
1777            const uint16_t *idx2 = (const uint16_t *)(&packed_idx2);
1778            const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[idx1[3]], iq1s_grid[idx1[2]], iq1s_grid[idx1[1]], iq1s_grid[idx1[0]]);
1779            const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[idx2[3]], iq1s_grid[idx2[2]], iq1s_grid[idx2[1]], iq1s_grid[idx2[0]]);
1780#else
1781            const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)],
1782                                                    iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
1783            const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)],
1784                                                    iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
1785#endif
1786            qs += 8;
1787            const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1788            const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1789
1790            const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
1791            const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
1792            const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
1793            const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
1794            const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
1795            const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
1796
1797            sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
1798            sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
1799                   + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
1800        }
1801
1802        const float d = y[i].d * h2f(x[i].d);
1803        accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
1804        accum1 += d * sumi1;
1805
1806    }
1807
1808    *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
1809
1810#elif defined __AVX__
1811    __m256 accum = _mm256_setzero_ps();
1812    float accum1 = 0;
1813    for (int i = 0; i < nb; ++i) {
1814
1815        const int8_t   * q8 = y[i].qs;
1816        const uint8_t  * qs = x[i].qs;
1817        const uint16_t * qh = x[i].qh;
1818
1819        __m128i sumi1_0 = _mm_setzero_si128();
1820        __m128i sumi1_1 = _mm_setzero_si128();
1821        int sumi1 = 0;
1822        for (int ib = 0; ib < QK_K/32; ib += 2) {
1823            const __m128i q1b_1_0 = _mm_set_epi64x(iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
1824            const __m128i q1b_1_1 = _mm_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)]);
1825            const __m128i q1b_2_0 = _mm_set_epi64x(iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
1826            const __m128i q1b_2_1 = _mm_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)]);
1827            qs += 8;
1828            const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
1829            const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
1830            const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
1831            const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
1832
1833            const __m128i dot1_0 = mul_add_epi8_sse(q1b_1_0, q8b_1_0);
1834            const __m128i dot1_1 = mul_add_epi8_sse(q1b_1_1, q8b_1_1);
1835            const __m128i dot2_0 = mul_add_epi8_sse(q1b_2_0, q8b_2_0);
1836            const __m128i dot2_1 = mul_add_epi8_sse(q1b_2_1, q8b_2_1);
1837            const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
1838            const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
1839            const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(ls1));
1840            const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(ls1));
1841            const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(ls2));
1842            const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(ls2));
1843
1844            sumi1_0 = _mm_add_epi32(sumi1_0, _mm_add_epi32(p1_0, p2_0));
1845            sumi1_1 = _mm_add_epi32(sumi1_1, _mm_add_epi32(p1_1, p2_1));
1846            sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
1847                   + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
1848        }
1849
1850        const float d = y[i].d * h2f(x[i].d);
1851        accum = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi1_1, sumi1_0))), accum);
1852        accum1 += d * sumi1;
1853
1854    }
1855
1856    *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
1857
1858#else
1859    UNUSED(x);
1860    UNUSED(y);
1861    UNUSED(nb);
1862    generic::dot_iq1_s_q8_K_generic(n, s, vx, vy);
1863#endif
1864}
1865
1866void dot_iq1_m_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
1867    assert(n % QK_K == 0);
1868
1869    const block_iq1_m* STRATA_RESTRICT x = (const block_iq1_m*) vx;
1870    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
1871
1872    const int nb = n / QK_K;
1873
1874    iq1m_scale_t scale;
1875
1876#if defined __AVX2__
1877
1878    const __m256i mask = _mm256_set1_epi16(0x7);
1879    const __m256i mone = _mm256_set1_epi16(1);
1880    const __m256i mone8 = _mm256_set1_epi8(1);
1881    const __m256i mtwo8 = _mm256_set1_epi8(2);
1882    // VPSHUFB cannot cross 128-bit lanes so odd shifts go to upper half.
1883    const __m256i scales_shift = _mm256_set_epi64x(9, 3, 6, 0);
1884
1885    __m256 accum1 = _mm256_setzero_ps();
1886    __m256 accum2 = _mm256_setzero_ps();
1887    for (int i = 0; i < nb; ++i) {
1888
1889        const int8_t   * q8 = y[i].qs;
1890        const uint8_t  * qs = x[i].qs;
1891        const uint8_t  * qh = x[i].qh;
1892        const uint16_t * sc = (const uint16_t *)x[i].scales;
1893
1894        scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
1895        // Extract 3-bit scales (16 values)
1896        __m256i scales = _mm256_set1_epi64x(*(const uint64_t*)sc);
1897        scales = _mm256_srlv_epi64(scales, scales_shift);
1898        scales = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scales, mask), 1), mone);
1899
1900        // Indices to repeat each scale 8 times.
1901        __m256i scales_idx1 = _mm256_set1_epi16(0x0100);
1902        __m256i scales_idx2 = _mm256_add_epi8(scales_idx1, _mm256_set1_epi8(8));
1903
1904        __m256i sumi1 = _mm256_setzero_si256();
1905        __m256i sumi2 = _mm256_setzero_si256();
1906        for (int ib = 0; ib < QK_K/32; ib += 2) {
1907#ifdef __BMI2__
1908            const uint64_t packed_idx1 = _pdep_u64(*(const uint32_t *)qs, 0x00ff00ff00ff00ffULL)
1909                                       | _pdep_u64(*(const uint16_t*)(qh) & 0x7777, 0xf000f000f000f00ULL);
1910            const uint64_t packed_idx2 = _pdep_u64(*(const uint32_t *)(qs + 4), 0x00ff00ff00ff00ffULL)
1911                                       | _pdep_u64(*(const uint16_t*)(qh + 2) & 0x7777, 0xf000f000f000f00ULL);
1912            const uint16_t *idx1 = (const uint16_t *)(&packed_idx1);
1913            const uint16_t *idx2 = (const uint16_t *)(&packed_idx2);
1914            const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[idx1[3]], iq1s_grid[idx1[2]], iq1s_grid[idx1[1]], iq1s_grid[idx1[0]]);
1915            const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[idx2[3]], iq1s_grid[idx2[2]], iq1s_grid[idx2[1]], iq1s_grid[idx2[0]]);
1916
1917            // Convert signs to bytes 0x81 (negative) or 0x01 (positive)
1918            const uint64_t delta_sign = _pdep_u64(*(const uint32_t*)(qh) & 0x88888888, 0xf0f0f0f0f0f0f0f0ULL);
1919            const __m256i delta1 = _mm256_or_si256(mone8, _mm256_cvtepi8_epi64(_mm_set1_epi32(delta_sign)));
1920            const __m256i delta2 = _mm256_or_si256(mone8, _mm256_cvtepi8_epi64(_mm_set1_epi32(delta_sign >> 32)));
1921#else
1922            const __m256i q1b_1 = _mm256_set_epi64x(
1923                    iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
1924                    iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
1925            );
1926            const __m256i q1b_2 = _mm256_set_epi64x(
1927                    iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
1928                    iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
1929            );
1930
1931            const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
1932                                                     qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
1933                                                     qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
1934                                                     qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
1935            const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
1936                                                     qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
1937                                                     qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
1938                                                     qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
1939#endif
1940            const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1941            const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
1942
1943            const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
1944            const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
1945            const __m256i dot3 = _mm256_maddubs_epi16(mone8, _mm256_sign_epi8(q8b_1, delta1));
1946            const __m256i dot4 = _mm256_maddubs_epi16(mone8, _mm256_sign_epi8(q8b_2, delta2));
1947
1948            __m256i scale1 = _mm256_shuffle_epi8(scales, scales_idx1);
1949            __m256i scale2 = _mm256_shuffle_epi8(scales, scales_idx2);
1950
1951            scales_idx1 = _mm256_add_epi8(scales_idx1, mtwo8);
1952            scales_idx2 = _mm256_add_epi8(scales_idx2, mtwo8);
1953
1954            const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
1955            const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
1956            const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
1957            const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
1958
1959            sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
1960            sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
1961
1962            qs += 8; qh += 4;
1963        }
1964
1965        const __m256 d = _mm256_set1_ps(y[i].d * h2f(scale.f16));
1966
1967        accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
1968        accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
1969    }
1970
1971    *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
1972
1973#elif defined __AVX__
1974    const __m128i mask = _mm_set1_epi16(0x7);
1975    const __m128i mone = _mm_set1_epi16(1);
1976
1977    __m256 accum1 = _mm256_setzero_ps();
1978    __m256 accum2 = _mm256_setzero_ps();
1979    for (int i = 0; i < nb; ++i) {
1980
1981        const int8_t   * q8 = y[i].qs;
1982        const uint8_t  * qs = x[i].qs;
1983        const uint8_t  * qh = x[i].qh;
1984        const uint16_t * sc = (const uint16_t *)x[i].scales;
1985
1986        scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
1987
1988        __m128i sumi1_0 = _mm_setzero_si128();
1989        __m128i sumi1_1 = _mm_setzero_si128();
1990        __m128i sumi2_0 = _mm_setzero_si128();
1991        __m128i sumi2_1 = _mm_setzero_si128();
1992        for (int ib = 0; ib < QK_K/32; ib += 2) {
1993            const __m128i q1b_1_0 = _mm_set_epi64x(
1994                    iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]);
1995            const __m128i q1b_1_1 = _mm_set_epi64x(
1996                    iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)]);
1997            const __m128i q1b_2_0 = _mm_set_epi64x(
1998                    iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]);
1999            const __m128i q1b_2_1 = _mm_set_epi64x(
2000                    iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)]);
2001            const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2002            const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2003            const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2004            const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2005
2006            const __m128i dot1_0 = mul_add_epi8_sse(q1b_1_0, q8b_1_0);
2007            const __m128i dot1_1 = mul_add_epi8_sse(q1b_1_1, q8b_1_1);
2008            const __m128i dot2_0 = mul_add_epi8_sse(q1b_2_0, q8b_2_0);
2009            const __m128i dot2_1 = mul_add_epi8_sse(q1b_2_1, q8b_2_1);
2010
2011            const __m128i delta1_0 = _mm_set_epi64x(qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
2012                                                     qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
2013            const __m128i delta1_1 = _mm_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
2014                                                     qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
2015            const __m128i delta2_0 = _mm_set_epi64x(qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
2016                                                     qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
2017            const __m128i delta2_1 = _mm_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
2018                                                     qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
2019
2020            const __m128i dot3_0 = mul_add_epi8_sse(delta1_0, q8b_1_0);
2021            const __m128i dot3_1 = mul_add_epi8_sse(delta1_1, q8b_1_1);
2022            const __m128i dot4_0 = mul_add_epi8_sse(delta2_0, q8b_2_0);
2023            const __m128i dot4_1 = mul_add_epi8_sse(delta2_1, q8b_2_1);
2024
2025            __m128i scale1_0 = _mm_set1_epi16(sc[ib/2] >> 0);
2026            __m128i scale1_1 = _mm_set1_epi16(sc[ib/2] >> 3);
2027            __m128i scale2_0 = _mm_set1_epi16(sc[ib/2] >> 6);
2028            __m128i scale2_1 = _mm_set1_epi16(sc[ib/2] >> 9);
2029
2030            scale1_0 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale1_0, mask), 1), mone);
2031            scale1_1 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale1_1, mask), 1), mone);
2032            scale2_0 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale2_0, mask), 1), mone);
2033            scale2_1 = _mm_add_epi16(_mm_slli_epi16(_mm_and_si128(scale2_1, mask), 1), mone);
2034            const __m128i p1_0 = _mm_madd_epi16(dot1_0, scale1_0);
2035            const __m128i p1_1 = _mm_madd_epi16(dot1_1, scale1_1);
2036            const __m128i p2_0 = _mm_madd_epi16(dot2_0, scale2_0);
2037            const __m128i p2_1 = _mm_madd_epi16(dot2_1, scale2_1);
2038            const __m128i p3_0 = _mm_madd_epi16(dot3_0, scale1_0);
2039            const __m128i p3_1 = _mm_madd_epi16(dot3_1, scale1_1);
2040            const __m128i p4_0 = _mm_madd_epi16(dot4_0, scale2_0);
2041            const __m128i p4_1 = _mm_madd_epi16(dot4_1, scale2_1);
2042
2043            sumi1_0 = _mm_add_epi32(sumi1_0, _mm_add_epi32(p1_0, p2_0));
2044            sumi1_1 = _mm_add_epi32(sumi1_1, _mm_add_epi32(p1_1, p2_1));
2045            sumi2_0 = _mm_add_epi32(sumi2_0, _mm_add_epi32(p3_0, p4_0));
2046            sumi2_1 = _mm_add_epi32(sumi2_1, _mm_add_epi32(p3_1, p4_1));
2047
2048            qs += 8; qh += 4;
2049        }
2050
2051        const __m256 d = _mm256_set1_ps(y[i].d * h2f(scale.f16));
2052
2053        accum1 = _mm256_add_ps(_mm256_mul_ps(d, _mm256_cvtepi32_ps(MM256_SET_M128I(sumi1_1, sumi1_0))), accum1);
2054        accum2 = _mm256_add_ps(_mm256_mul_ps(d, _mm256_cvtepi32_ps(MM256_SET_M128I(sumi2_1, sumi2_0))), accum2);
2055    }
2056
2057    *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
2058
2059#else
2060    UNUSED(x);
2061    UNUSED(y);
2062    UNUSED(nb);
2063    UNUSED(scale);
2064    generic::dot_iq1_m_q8_K_generic(n, s, vx, vy);
2065#endif
2066}
2067
2068void dot_iq2_xxs_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
2069    assert(n % QK_K == 0);
2070
2071    const block_iq2_xxs* STRATA_RESTRICT x = (const block_iq2_xxs*) vx;
2072    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
2073
2074    const int nb = n / QK_K;
2075
2076#if defined(__AVX2__)
2077
2078    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
2079
2080    uint32_t aux32[4];
2081    const uint8_t * aux8 = (const uint8_t *)aux32;
2082
2083    __m256 accumf = _mm256_setzero_ps();
2084    for (int i = 0; i < nb; ++i) {
2085        const float d = h2f(x[i].d) * y[i].d;
2086        const uint16_t * STRATA_RESTRICT q2 = x[i].qs;
2087        const int8_t   * STRATA_RESTRICT q8 = y[i].qs;
2088        __m256i sumi1 = _mm256_setzero_si256();
2089        __m256i sumi2 = _mm256_setzero_si256();
2090        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2091            const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2092            const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2093            memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
2094            const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
2095            const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
2096            const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
2097                                                   signs64[(aux32[1] >>  7) & 127], signs64[(aux32[1] >>  0) & 127]);
2098            const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
2099                                                   signs64[(aux32[3] >>  7) & 127], signs64[(aux32[3] >>  0) & 127]);
2100            const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
2101            const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
2102            const __m256i dot1  = _mm256_maddubs_epi16(q2_1, q8s_1);
2103            const __m256i dot2  = _mm256_maddubs_epi16(q2_2, q8s_2);
2104            const uint16_t ls1 = aux32[1] >> 28;
2105            const uint16_t ls2 = aux32[3] >> 28;
2106            const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
2107            const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
2108            sumi1 = _mm256_add_epi32(sumi1, p1);
2109            sumi2 = _mm256_add_epi32(sumi2, p2);
2110        }
2111
2112        accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
2113
2114    }
2115
2116    *s = 0.125f * hsum_float_8(accumf);
2117
2118#elif defined(__AVX__)
2119    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
2120
2121    uint32_t aux32[4];
2122    const uint8_t * aux8 = (const uint8_t *)aux32;
2123
2124    __m256 accumf = _mm256_setzero_ps();
2125    for (int i = 0; i < nb; ++i) {
2126        const float d = h2f(x[i].d) * y[i].d;
2127        const uint16_t * STRATA_RESTRICT q2 = x[i].qs;
2128        const int8_t   * STRATA_RESTRICT q8 = y[i].qs;
2129        __m128i sumi1_0 = _mm_setzero_si128();
2130        __m128i sumi1_1 = _mm_setzero_si128();
2131        __m128i sumi2_0 = _mm_setzero_si128();
2132        __m128i sumi2_1 = _mm_setzero_si128();
2133        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2134            const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2135            const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2136            const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2137            const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2138            memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
2139            const __m128i q2_1_0 = _mm_set_epi64x(iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
2140            const __m128i q2_1_1 = _mm_set_epi64x(iq2xxs_grid[aux8[3]], iq2xxs_grid[aux8[2]]);
2141            const __m128i q2_2_0 = _mm_set_epi64x(iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
2142            const __m128i q2_2_1 = _mm_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]]);
2143            const __m128i s2_1_0 = _mm_set_epi64x(signs64[(aux32[1] >>  7) & 127], signs64[(aux32[1] >>  0) & 127]);
2144            const __m128i s2_1_1 = _mm_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127]);
2145            const __m128i s2_2_0 = _mm_set_epi64x(signs64[(aux32[3] >>  7) & 127], signs64[(aux32[3] >>  0) & 127]);
2146            const __m128i s2_2_1 = _mm_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127]);
2147            const __m128i q8s_1_0 = _mm_sign_epi8(q8_1_0, s2_1_0);
2148            const __m128i q8s_1_1 = _mm_sign_epi8(q8_1_1, s2_1_1);
2149            const __m128i q8s_2_0 = _mm_sign_epi8(q8_2_0, s2_2_0);
2150            const __m128i q8s_2_1 = _mm_sign_epi8(q8_2_1, s2_2_1);
2151            const __m128i dot1_0  = _mm_maddubs_epi16(q2_1_0, q8s_1_0);
2152            const __m128i dot1_1  = _mm_maddubs_epi16(q2_1_1, q8s_1_1);
2153            const __m128i dot2_0  = _mm_maddubs_epi16(q2_2_0, q8s_2_0);
2154            const __m128i dot2_1  = _mm_maddubs_epi16(q2_2_1, q8s_2_1);
2155            const uint16_t ls1 = aux32[1] >> 28;
2156            const uint16_t ls2 = aux32[3] >> 28;
2157            const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(2*ls1+1));
2158            const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(2*ls1+1));
2159            const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(2*ls2+1));
2160            const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(2*ls2+1));
2161            sumi1_0 = _mm_add_epi32(sumi1_0, p1_0);
2162            sumi1_1 = _mm_add_epi32(sumi1_1, p1_1);
2163            sumi2_0 = _mm_add_epi32(sumi2_0, p2_0);
2164            sumi2_1 = _mm_add_epi32(sumi2_1, p2_1);
2165        }
2166
2167        accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf);
2168
2169    }
2170
2171    *s = 0.125f * hsum_float_8(accumf);
2172
2173#else
2174    UNUSED(x);
2175    UNUSED(y);
2176    UNUSED(nb);
2177    generic::dot_iq2_xxs_q8_K_generic(n, s, vx, vy);
2178#endif
2179}
2180
2181void dot_iq2_xs_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
2182    assert(n % QK_K == 0);
2183
2184    const block_iq2_xs* STRATA_RESTRICT x = (const block_iq2_xs*) vx;
2185    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
2186
2187    const int nb = n / QK_K;
2188
2189#if defined(__AVX2__)
2190
2191    const __m256i mone = _mm256_set1_epi8(1);
2192    static const char block_sign_shuffle_mask_1[32] = {
2193        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
2194        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
2195    };
2196    static const char block_sign_shuffle_mask_2[32] = {
2197        0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
2198        0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
2199    };
2200    static const uint8_t bit_selector_mask_bytes[32] = {
2201        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2202        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2203    };
2204
2205    const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
2206    const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
2207    const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
2208
2209    static const uint8_t k_bit_helper[32] = {
2210        0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
2211        0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
2212    };
2213    const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
2214    const __m256i m511 = _mm256_set1_epi16(511);
2215    const __m128i m4 = _mm_set1_epi8(0xf);
2216    const __m128i m1 = _mm_set1_epi8(1);
2217
2218    uint64_t aux64;
2219
2220    // somewhat hacky, but gives a significant boost in performance
2221    __m256i aux_gindex;
2222    const uint16_t * gindex = (const uint16_t *)&aux_gindex;
2223
2224    __m256 accumf = _mm256_setzero_ps();
2225    for (int i = 0; i < nb; ++i) {
2226        const float d = h2f(x[i].d) * y[i].d;
2227        const uint16_t * STRATA_RESTRICT q2 = x[i].qs;
2228        const int8_t   * STRATA_RESTRICT q8 = y[i].qs;
2229
2230        memcpy(&aux64, x[i].scales, 8);
2231        __m128i stmp = _mm_set1_epi64x(aux64);
2232        stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
2233        const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
2234
2235        __m256i sumi1 = _mm256_setzero_si256();
2236        __m256i sumi2 = _mm256_setzero_si256();
2237        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
2238
2239            const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2);  q2 += 16;
2240            aux_gindex = _mm256_and_si256(q2_data, m511);
2241
2242            const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
2243            const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
2244            const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
2245
2246            const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
2247            const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
2248
2249            const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2250            const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2251            const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2252            const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2253
2254            const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
2255                                                   iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
2256            const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
2257                                                   iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
2258            const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
2259                                                   iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
2260            const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
2261                                                   iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
2262
2263            const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
2264            const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
2265            const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
2266            const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
2267
2268            __m256i signs;
2269            signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
2270            signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
2271            const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
2272
2273            signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
2274            signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
2275            const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
2276
2277            signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
2278            signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
2279            const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
2280
2281            signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
2282            signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
2283            const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
2284
2285            const __m256i dot1  = _mm256_maddubs_epi16(q2_1, q8s_1);
2286            const __m256i dot2  = _mm256_maddubs_epi16(q2_2, q8s_2);
2287            const __m256i dot3  = _mm256_maddubs_epi16(q2_3, q8s_3);
2288            const __m256i dot4  = _mm256_maddubs_epi16(q2_4, q8s_4);
2289
2290            const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
2291            const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
2292            const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
2293            const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
2294
2295            sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
2296            sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
2297            sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
2298            sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
2299        }
2300
2301        accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
2302
2303    }
2304
2305    *s = 0.125f * hsum_float_8(accumf);
2306
2307#elif defined(__AVX__)
2308    const __m128i mone = _mm_set1_epi8(1);
2309    static const char block_sign_shuffle_mask_1[32] = {
2310        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
2311        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
2312    };
2313    static const char block_sign_shuffle_mask_2[32] = {
2314        0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
2315        0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
2316    };
2317    static const uint8_t bit_selector_mask_bytes[32] = {
2318        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2319        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2320    };
2321
2322    const __m128i bit_selector_mask_0 = _mm_loadu_si128((const __m128i*)bit_selector_mask_bytes);
2323    const __m128i bit_selector_mask_1 = _mm_loadu_si128((const __m128i*)bit_selector_mask_bytes + 1);
2324    const __m128i block_sign_shuffle_1_0 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_1);
2325    const __m128i block_sign_shuffle_1_1 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_1 + 1);
2326    const __m128i block_sign_shuffle_2_0 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_2);
2327    const __m128i block_sign_shuffle_2_1 = _mm_loadu_si128((const __m128i*)block_sign_shuffle_mask_2 + 1);
2328
2329    static const uint8_t k_bit_helper[32] = {
2330        0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
2331        0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
2332    };
2333    const __m128i bit_helper_0 = _mm_loadu_si128((const __m128i*)k_bit_helper);
2334    const __m128i bit_helper_1 = _mm_loadu_si128((const __m128i*)k_bit_helper + 1);
2335    const __m128i m511 = _mm_set1_epi16(511);
2336    const __m128i m4 = _mm_set1_epi8(0xf);
2337    const __m128i m1 = _mm_set1_epi8(1);
2338
2339    uint64_t aux64;
2340
2341    // somewhat hacky, but gives a significant boost in performance
2342    __m256i aux_gindex;
2343    const uint16_t * gindex = (const uint16_t *)&aux_gindex;
2344
2345    __m256 accumf = _mm256_setzero_ps();
2346    for (int i = 0; i < nb; ++i) {
2347        const float d = h2f(x[i].d) * y[i].d;
2348        const uint16_t * STRATA_RESTRICT q2 = x[i].qs;
2349        const int8_t   * STRATA_RESTRICT q8 = y[i].qs;
2350
2351        memcpy(&aux64, x[i].scales, 8);
2352        __m128i stmp = _mm_set1_epi64x(aux64);
2353        stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
2354        const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
2355
2356        __m128i sumi1_0 = _mm_setzero_si128();
2357        __m128i sumi1_1 = _mm_setzero_si128();
2358        __m128i sumi2_0 = _mm_setzero_si128();
2359        __m128i sumi2_1 = _mm_setzero_si128();
2360        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
2361
2362            const __m128i q2_data_0 = _mm_loadu_si128((const __m128i*)q2);
2363            const __m128i q2_data_1 = _mm_loadu_si128((const __m128i*)q2 + 1);  q2 += 16;
2364            aux_gindex = MM256_SET_M128I(_mm_and_si128(q2_data_1, m511), _mm_and_si128(q2_data_0, m511));
2365
2366            const __m128i partial_sign_bits_0 = _mm_srli_epi16(q2_data_0, 9);
2367            const __m128i partial_sign_bits_1 = _mm_srli_epi16(q2_data_1, 9);
2368            const __m128i partial_sign_bits_upper_0 = _mm_srli_epi16(q2_data_0, 13);
2369            const __m128i partial_sign_bits_upper_1 = _mm_srli_epi16(q2_data_1, 13);
2370            const __m128i partial_sign_bits_for_counting_0 = _mm_xor_si128(partial_sign_bits_0, partial_sign_bits_upper_0);
2371            const __m128i partial_sign_bits_for_counting_1 = _mm_xor_si128(partial_sign_bits_1, partial_sign_bits_upper_1);
2372
2373            const __m128i odd_bits_0 = _mm_shuffle_epi8(bit_helper_0, partial_sign_bits_for_counting_0);
2374            const __m128i odd_bits_1 = _mm_shuffle_epi8(bit_helper_1, partial_sign_bits_for_counting_1);
2375            const __m128i full_sign_bits_0 = _mm_or_si128(partial_sign_bits_0, odd_bits_0);
2376            const __m128i full_sign_bits_1 = _mm_or_si128(partial_sign_bits_1, odd_bits_1);
2377
2378            const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2379            const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2380            const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2381            const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2382            const __m128i q8_3_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2383            const __m128i q8_3_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2384            const __m128i q8_4_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2385            const __m128i q8_4_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2386
2387            const __m128i q2_1_0 = _mm_set_epi64x(iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]);
2388            const __m128i q2_1_1 = _mm_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]]);
2389            const __m128i q2_2_0 = _mm_set_epi64x(iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]);
2390            const __m128i q2_2_1 = _mm_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]]);
2391            const __m128i q2_3_0 = _mm_set_epi64x(iq2xs_grid[gindex[9]], iq2xs_grid[gindex[8]]);
2392            const __m128i q2_3_1 = _mm_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]]);
2393            const __m128i q2_4_0 = _mm_set_epi64x(iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
2394            const __m128i q2_4_1 = _mm_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]]);
2395
2396            // AVX2 full_signs_1 is full_sign_bits_0 here
2397            // AVX2 full_signs_2 is full_sign_bits_1 here
2398            __m128i signs_0, signs_1;
2399            signs_0 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_1_0);
2400            signs_1 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_1_1);
2401            signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0);
2402            signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1);
2403            const __m128i q8s_1_0 = _mm_sign_epi8(q8_1_0, _mm_or_si128(signs_0, mone));
2404            const __m128i q8s_1_1 = _mm_sign_epi8(q8_1_1, _mm_or_si128(signs_1, mone));
2405
2406            signs_0 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_2_0);
2407            signs_1 = _mm_shuffle_epi8(full_sign_bits_0, block_sign_shuffle_2_1);
2408            signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0);
2409            signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1);
2410            const __m128i q8s_2_0 = _mm_sign_epi8(q8_2_0, _mm_or_si128(signs_0, mone));
2411            const __m128i q8s_2_1 = _mm_sign_epi8(q8_2_1, _mm_or_si128(signs_1, mone));
2412
2413            signs_0 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_1_0);
2414            signs_1 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_1_1);
2415            signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0);
2416            signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1);
2417            const __m128i q8s_3_0 = _mm_sign_epi8(q8_3_0, _mm_or_si128(signs_0, mone));
2418            const __m128i q8s_3_1 = _mm_sign_epi8(q8_3_1, _mm_or_si128(signs_1, mone));
2419
2420            signs_0 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_2_0);
2421            signs_1 = _mm_shuffle_epi8(full_sign_bits_1, block_sign_shuffle_2_1);
2422            signs_0 = _mm_cmpeq_epi8(_mm_and_si128(signs_0, bit_selector_mask_0), bit_selector_mask_0);
2423            signs_1 = _mm_cmpeq_epi8(_mm_and_si128(signs_1, bit_selector_mask_1), bit_selector_mask_1);
2424            const __m128i q8s_4_0 = _mm_sign_epi8(q8_4_0, _mm_or_si128(signs_0, mone));
2425            const __m128i q8s_4_1 = _mm_sign_epi8(q8_4_1, _mm_or_si128(signs_1, mone));
2426
2427            const __m128i dot1_0  = _mm_maddubs_epi16(q2_1_0, q8s_1_0);
2428            const __m128i dot1_1  = _mm_maddubs_epi16(q2_1_1, q8s_1_1);
2429            const __m128i dot2_0  = _mm_maddubs_epi16(q2_2_0, q8s_2_0);
2430            const __m128i dot2_1  = _mm_maddubs_epi16(q2_2_1, q8s_2_1);
2431            const __m128i dot3_0  = _mm_maddubs_epi16(q2_3_0, q8s_3_0);
2432            const __m128i dot3_1  = _mm_maddubs_epi16(q2_3_1, q8s_3_1);
2433            const __m128i dot4_0  = _mm_maddubs_epi16(q2_4_0, q8s_4_0);
2434            const __m128i dot4_1  = _mm_maddubs_epi16(q2_4_1, q8s_4_1);
2435
2436            __m128i sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0));
2437            const __m128i sc1_0 = _mm_cvtepi8_epi16(sc_tmp);
2438            const __m128i sc1_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8));
2439            sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1));
2440            const __m128i sc2_0 = _mm_cvtepi8_epi16(sc_tmp);
2441            const __m128i sc2_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8));
2442            sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2));
2443            const __m128i sc3_0 = _mm_cvtepi8_epi16(sc_tmp);
2444            const __m128i sc3_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8));
2445            sc_tmp = _mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3));
2446            const __m128i sc4_0 = _mm_cvtepi8_epi16(sc_tmp);
2447            const __m128i sc4_1 = _mm_cvtepi8_epi16(_mm_srli_si128(sc_tmp, 8));
2448
2449            sumi1_0 = _mm_add_epi32(sumi1_0, _mm_madd_epi16(dot1_0, sc1_0));
2450            sumi1_1 = _mm_add_epi32(sumi1_1, _mm_madd_epi16(dot1_1, sc1_1));
2451            sumi2_0 = _mm_add_epi32(sumi2_0, _mm_madd_epi16(dot2_0, sc2_0));
2452            sumi2_1 = _mm_add_epi32(sumi2_1, _mm_madd_epi16(dot2_1, sc2_1));
2453            sumi1_0 = _mm_add_epi32(sumi1_0, _mm_madd_epi16(dot3_0, sc3_0));
2454            sumi1_1 = _mm_add_epi32(sumi1_1, _mm_madd_epi16(dot3_1, sc3_1));
2455            sumi2_0 = _mm_add_epi32(sumi2_0, _mm_madd_epi16(dot4_0, sc4_0));
2456            sumi2_1 = _mm_add_epi32(sumi2_1, _mm_madd_epi16(dot4_1, sc4_1));
2457        }
2458
2459        accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf);
2460
2461    }
2462
2463    *s = 0.125f * hsum_float_8(accumf);
2464
2465#else
2466    UNUSED(x);
2467    UNUSED(y);
2468    UNUSED(nb);
2469    generic::dot_iq2_xs_q8_K_generic(n, s, vx, vy);
2470#endif
2471}
2472
2473void dot_iq2_s_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
2474    assert(n % QK_K == 0);
2475
2476    const block_iq2_s* STRATA_RESTRICT x = (const block_iq2_s*) vx;
2477    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
2478
2479    const int nb = n / QK_K;
2480
2481#if defined(__AVX2__)
2482
2483   static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2484                                       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
2485   };
2486
2487    static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2488                                        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2489    };
2490
2491    const __m128i m4 = _mm_set1_epi8(0xf);
2492    const __m128i m1 = _mm_set1_epi8(1);
2493
2494    const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
2495    const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
2496
2497    uint64_t aux64;
2498
2499    __m256 accumf = _mm256_setzero_ps();
2500    for (int i = 0; i < nb; ++i) {
2501        const float d = h2f(x[i].d) * y[i].d;
2502        const uint8_t * STRATA_RESTRICT qs = x[i].qs;
2503        const uint8_t * STRATA_RESTRICT qh = x[i].qh;
2504        const uint16_t * STRATA_RESTRICT signs = (const uint16_t *)(x[i].qs + QK_K/8);
2505        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
2506
2507        memcpy(&aux64, x[i].scales, 8);
2508        const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
2509        const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
2510
2511        __m256i sumi1 = _mm256_setzero_si256();
2512        __m256i sumi2 = _mm256_setzero_si256();
2513        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2514            const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2515            const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2516            const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
2517                                                   iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
2518                                                   iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
2519                                                   iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
2520            const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
2521                                                   iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
2522                                                   iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
2523                                                   iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
2524            qs += 8;
2525
2526            __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
2527            aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
2528            const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
2529            const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
2530
2531            aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
2532            aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
2533            const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
2534            const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
2535
2536            signs += 4;
2537
2538            const __m256i dot1  = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
2539            const __m256i dot2  = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
2540
2541            const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
2542            const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
2543            sumi1 = _mm256_add_epi32(sumi1, p1);
2544            sumi2 = _mm256_add_epi32(sumi2, p2);
2545        }
2546
2547        accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
2548
2549    }
2550
2551    *s = 0.125f * hsum_float_8(accumf);
2552
2553#elif defined(__AVX__)
2554   static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2555                                       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
2556   };
2557
2558    static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2559                                        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2560    };
2561
2562    const __m128i m4 = _mm_set1_epi8(0xf);
2563    const __m128i m1 = _mm_set1_epi8(1);
2564
2565    const __m128i mask1_0 = _mm_loadu_si128((const __m128i*)k_mask1);
2566    const __m128i mask1_1 = _mm_loadu_si128((const __m128i*)k_mask1 + 1);
2567    const __m128i mask2_0 = _mm_loadu_si128((const __m128i*)k_mask2);
2568    const __m128i mask2_1 = _mm_loadu_si128((const __m128i*)k_mask2 + 1);
2569
2570    uint64_t aux64;
2571
2572    __m256 accumf = _mm256_setzero_ps();
2573    for (int i = 0; i < nb; ++i) {
2574        const float d = h2f(x[i].d) * y[i].d;
2575        const uint8_t * STRATA_RESTRICT qs = x[i].qs;
2576        const uint8_t * STRATA_RESTRICT qh = x[i].qh;
2577        const uint16_t * STRATA_RESTRICT signs = (const uint16_t *)(x[i].qs + QK_K/8);
2578        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
2579
2580        memcpy(&aux64, x[i].scales, 8);
2581        const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
2582        const __m128i scales16_0 = _mm_cvtepi8_epi16(scales8);
2583        const __m128i scales16_1 = _mm_cvtepi8_epi16(_mm_srli_si128(scales8, 8));
2584
2585        __m128i sumi1_0 = _mm_setzero_si128();
2586        __m128i sumi1_1 = _mm_setzero_si128();
2587        __m128i sumi2_0 = _mm_setzero_si128();
2588        __m128i sumi2_1 = _mm_setzero_si128();
2589        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2590            const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2591            const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2592            const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2593            const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2594            const __m128i q2_1_0 = _mm_set_epi64x(iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
2595                                                  iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
2596            const __m128i q2_1_1 = _mm_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
2597                                                  iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)]);
2598            const __m128i q2_2_0 = _mm_set_epi64x(iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
2599                                                  iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
2600            const __m128i q2_2_1 = _mm_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
2601                                                  iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)]);
2602            qs += 8;
2603
2604            __m128i aux128_0 = _mm_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
2605            __m128i aux128_1 = aux128_0;
2606            aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0);
2607            aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1);
2608            const __m128i s2_1_0 = _mm_cmpeq_epi8(aux128_0, mask2_0);
2609            const __m128i s2_1_1 = _mm_cmpeq_epi8(aux128_1, mask2_1);
2610            const __m128i q8s_1_0 = _mm_sub_epi8(_mm_xor_si128(s2_1_0, q8_1_0), s2_1_0);
2611            const __m128i q8s_1_1 = _mm_sub_epi8(_mm_xor_si128(s2_1_1, q8_1_1), s2_1_1);
2612
2613            aux128_0 = _mm_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
2614            aux128_1 = aux128_0;
2615            aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0);
2616            aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1);
2617            const __m128i s2_2_0 = _mm_cmpeq_epi8(aux128_0, mask2_0);
2618            const __m128i s2_2_1 = _mm_cmpeq_epi8(aux128_1, mask2_1);
2619            const __m128i q8s_2_0 = _mm_sub_epi8(_mm_xor_si128(s2_2_0, q8_2_0), s2_2_0);
2620            const __m128i q8s_2_1 = _mm_sub_epi8(_mm_xor_si128(s2_2_1, q8_2_1), s2_2_1);
2621
2622            signs += 4;
2623
2624            const __m128i dot1_0  = _mm_maddubs_epi16(q2_1_0, q8s_1_0);
2625            const __m128i dot1_1  = _mm_maddubs_epi16(q2_1_1, q8s_1_1);
2626            const __m128i dot2_0  = _mm_maddubs_epi16(q2_2_0, q8s_2_0);
2627            const __m128i dot2_1  = _mm_maddubs_epi16(q2_2_1, q8s_2_1);
2628
2629            const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_shuffle_epi8(scales16_0, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+0), 0)));
2630            const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_shuffle_epi8(scales16_1, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+0), 1)));
2631            const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_shuffle_epi8(scales16_0, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+1), 0)));
2632            const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_shuffle_epi8(scales16_1, _mm256_extractf128_si256(get_scale_shuffle_k4(ib32+1), 1)));
2633            sumi1_0 = _mm_add_epi32(sumi1_0, p1_0);
2634            sumi1_1 = _mm_add_epi32(sumi1_1, p1_1);
2635            sumi2_0 = _mm_add_epi32(sumi2_0, p2_0);
2636            sumi2_1 = _mm_add_epi32(sumi2_1, p2_1);
2637        }
2638
2639        accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf);
2640
2641    }
2642
2643    *s = 0.125f * hsum_float_8(accumf);
2644
2645#else
2646    UNUSED(x);
2647    UNUSED(y);
2648    UNUSED(nb);
2649    generic::dot_iq2_s_q8_K_generic(n, s, vx, vy);
2650#endif
2651}
2652
2653void dot_iq3_xxs_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
2654    assert(n % QK_K == 0);
2655
2656    const block_iq3_xxs* STRATA_RESTRICT x = (const block_iq3_xxs*) vx;
2657    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
2658
2659    const int nb = n / QK_K;
2660
2661#if defined(__AVX2__)
2662
2663    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
2664
2665    uint32_t aux32[2];
2666
2667    __m256 accumf = _mm256_setzero_ps();
2668    for (int i = 0; i < nb; ++i) {
2669        const float d = h2f(x[i].d) * y[i].d;
2670        const uint8_t * STRATA_RESTRICT q3 = x[i].qs;
2671        const uint8_t * STRATA_RESTRICT gas = x[i].qs + QK_K/4;
2672        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
2673        __m256i sumi1 = _mm256_setzero_si256();
2674        __m256i sumi2 = _mm256_setzero_si256();
2675        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2676            const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2677            const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2678            const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
2679                                                  iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
2680            q3 += 8;
2681            const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
2682                                                  iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
2683            q3 += 8;
2684            memcpy(aux32, gas, 8); gas += 8;
2685            const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
2686                                                   signs64[(aux32[0] >>  7) & 127], signs64[(aux32[0] >>  0) & 127]);
2687            const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
2688                                                   signs64[(aux32[1] >>  7) & 127], signs64[(aux32[1] >>  0) & 127]);
2689            const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
2690            const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
2691            const __m256i dot1  = _mm256_maddubs_epi16(q2_1, q8s_1);
2692            const __m256i dot2  = _mm256_maddubs_epi16(q2_2, q8s_2);
2693            const uint16_t ls1 = aux32[0] >> 28;
2694            const uint16_t ls2 = aux32[1] >> 28;
2695            const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
2696            const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
2697            sumi1 = _mm256_add_epi32(sumi1, p1);
2698            sumi2 = _mm256_add_epi32(sumi2, p2);
2699        }
2700
2701        accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
2702
2703    }
2704
2705    *s = 0.25f * hsum_float_8(accumf);
2706
2707#elif defined(__AVX__)
2708    const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
2709
2710    uint32_t aux32[2];
2711
2712    __m256 accumf = _mm256_setzero_ps();
2713    for (int i = 0; i < nb; ++i) {
2714        const float d = h2f(x[i].d) * y[i].d;
2715        const uint8_t * STRATA_RESTRICT q3 = x[i].qs;
2716        const uint8_t * STRATA_RESTRICT gas = x[i].qs + QK_K/4;
2717        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
2718        __m128i sumi1_0 = _mm_setzero_si128();
2719        __m128i sumi1_1 = _mm_setzero_si128();
2720        __m128i sumi2_0 = _mm_setzero_si128();
2721        __m128i sumi2_1 = _mm_setzero_si128();
2722        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2723            const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2724            const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2725            const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2726            const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2727            const __m128i q2_1_0 = _mm_set_epi32(iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
2728            const __m128i q2_1_1 = _mm_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]]);
2729            q3 += 8;
2730            const __m128i q2_2_0 = _mm_set_epi32(iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
2731            const __m128i q2_2_1 = _mm_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]]);
2732            q3 += 8;
2733            memcpy(aux32, gas, 8); gas += 8;
2734            const __m128i s2_1_0 = _mm_set_epi64x(signs64[(aux32[0] >>  7) & 127], signs64[(aux32[0] >>  0) & 127]);
2735            const __m128i s2_1_1 = _mm_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127]);
2736            const __m128i s2_2_0 = _mm_set_epi64x(signs64[(aux32[1] >>  7) & 127], signs64[(aux32[1] >>  0) & 127]);
2737            const __m128i s2_2_1 = _mm_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127]);
2738            const __m128i q8s_1_0 = _mm_sign_epi8(q8_1_0, s2_1_0);
2739            const __m128i q8s_1_1 = _mm_sign_epi8(q8_1_1, s2_1_1);
2740            const __m128i q8s_2_0 = _mm_sign_epi8(q8_2_0, s2_2_0);
2741            const __m128i q8s_2_1 = _mm_sign_epi8(q8_2_1, s2_2_1);
2742            const __m128i dot1_0  = _mm_maddubs_epi16(q2_1_0, q8s_1_0);
2743            const __m128i dot1_1  = _mm_maddubs_epi16(q2_1_1, q8s_1_1);
2744            const __m128i dot2_0  = _mm_maddubs_epi16(q2_2_0, q8s_2_0);
2745            const __m128i dot2_1  = _mm_maddubs_epi16(q2_2_1, q8s_2_1);
2746            const uint16_t ls1 = aux32[0] >> 28;
2747            const uint16_t ls2 = aux32[1] >> 28;
2748            const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(2*ls1+1));
2749            const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(2*ls1+1));
2750            const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(2*ls2+1));
2751            const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(2*ls2+1));
2752            sumi1_0 = _mm_add_epi32(sumi1_0, p1_0);
2753            sumi1_1 = _mm_add_epi32(sumi1_1, p1_1);
2754            sumi2_0 = _mm_add_epi32(sumi2_0, p2_0);
2755            sumi2_1 = _mm_add_epi32(sumi2_1, p2_1);
2756        }
2757
2758        accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf);
2759
2760    }
2761
2762    *s = 0.25f * hsum_float_8(accumf);
2763
2764#else
2765    UNUSED(x);
2766    UNUSED(y);
2767    UNUSED(nb);
2768    generic::dot_iq3_xxs_q8_K_generic(n, s, vx, vy);
2769#endif
2770}
2771
2772void dot_iq3_s_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
2773    assert(n % QK_K == 0);
2774
2775    const block_iq3_s* STRATA_RESTRICT x = (const block_iq3_s*) vx;
2776    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
2777
2778    const int nb = n / QK_K;
2779
2780#if defined(__AVX2__)
2781
2782   static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2783                                       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
2784   };
2785
2786    static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2787                                        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2788    };
2789
2790    const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
2791    const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
2792
2793    const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
2794    const __m256i idx_mask  = _mm256_set1_epi32(256);
2795
2796    typedef union {
2797        __m256i  vec[2];
2798        uint32_t index[16];
2799    } index_t;
2800
2801    index_t idx;
2802
2803    __m256 accumf = _mm256_setzero_ps();
2804    for (int i = 0; i < nb; ++i) {
2805        const float d = h2f(x[i].d) * y[i].d;
2806        const uint8_t * STRATA_RESTRICT qs = x[i].qs;
2807        const uint8_t * STRATA_RESTRICT qh = x[i].qh;
2808        const uint16_t * STRATA_RESTRICT signs = (const uint16_t *)x[i].signs;
2809        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
2810        __m256i sumi1 = _mm256_setzero_si256();
2811        __m256i sumi2 = _mm256_setzero_si256();
2812        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2813            const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2814            const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
2815            const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
2816            idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
2817            idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
2818            idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
2819            idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
2820            idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
2821            idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
2822
2823            // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
2824            //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
2825            //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
2826            const __m256i q2_1 = _mm256_set_epi32(
2827                    iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
2828                    iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
2829            );
2830            const __m256i q2_2 = _mm256_set_epi32(
2831                    iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
2832                    iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
2833            );
2834
2835            __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
2836            aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
2837            const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
2838            const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
2839
2840            aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
2841            aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
2842            const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
2843            const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
2844
2845            signs += 4;
2846
2847            const __m256i dot1  = _mm256_maddubs_epi16(q2_1, q8s_1);
2848            const __m256i dot2  = _mm256_maddubs_epi16(q2_2, q8s_2);
2849            const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
2850            const uint16_t ls2 = x[i].scales[ib32/2] >>  4;
2851            const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
2852            const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
2853            sumi1 = _mm256_add_epi32(sumi1, p1);
2854            sumi2 = _mm256_add_epi32(sumi2, p2);
2855        }
2856
2857        accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
2858
2859    }
2860
2861    *s = hsum_float_8(accumf);
2862
2863#elif defined(__AVX__)
2864   static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2865                                       0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
2866   };
2867
2868    static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2869                                        0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
2870    };
2871
2872    const __m128i mask1_0 = _mm_loadu_si128((const __m128i*)k_mask1);
2873    const __m128i mask1_1 = _mm_loadu_si128((const __m128i*)k_mask1 + 1);
2874    const __m128i mask2_0 = _mm_loadu_si128((const __m128i*)k_mask2);
2875    const __m128i mask2_1 = _mm_loadu_si128((const __m128i*)k_mask2 + 1);
2876
2877    const __m128i idx_mul_0 = _mm_set_epi32(32, 64, 128, 256);
2878    const __m128i idx_mul_1 = _mm_set_epi32(2, 4, 8, 16);
2879    const __m128i idx_mask  = _mm_set1_epi32(256);
2880
2881    typedef union {
2882        __m128i  vec[4];
2883        uint32_t index[16];
2884    } index_t;
2885
2886    index_t idx;
2887
2888    __m256 accumf = _mm256_setzero_ps();
2889    for (int i = 0; i < nb; ++i) {
2890        const float d = h2f(x[i].d) * y[i].d;
2891        const uint8_t * STRATA_RESTRICT qs = x[i].qs;
2892        const uint8_t * STRATA_RESTRICT qh = x[i].qh;
2893        const uint16_t * STRATA_RESTRICT signs = (const uint16_t *)x[i].signs;
2894        const int8_t  * STRATA_RESTRICT q8 = y[i].qs;
2895        __m128i sumi1_0 = _mm_setzero_si128();
2896        __m128i sumi1_1 = _mm_setzero_si128();
2897        __m128i sumi2_0 = _mm_setzero_si128();
2898        __m128i sumi2_1 = _mm_setzero_si128();
2899        for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
2900            const __m128i q8_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2901            const __m128i q8_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2902            const __m128i q8_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2903            const __m128i q8_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
2904            const __m128i qs_tmp = _mm_loadu_si128((const __m128i *)qs);
2905            const __m128i idx_l_0 = _mm_cvtepu8_epi16(qs_tmp);
2906            const __m128i idx_l_1 = _mm_cvtepu8_epi16(_mm_srli_si128(qs_tmp, 8)); qs += 16;
2907            idx.vec[0] = _mm_set1_epi32(qh[ib32+0]);
2908            idx.vec[1] = idx.vec[0];
2909            idx.vec[2] = _mm_set1_epi32(qh[ib32+1]);
2910            idx.vec[3] = idx.vec[2];
2911
2912            idx.vec[0] = _mm_and_si128(_mm_mullo_epi32(idx.vec[0], idx_mul_0), idx_mask);
2913            idx.vec[1] = _mm_and_si128(_mm_mullo_epi32(idx.vec[1], idx_mul_1), idx_mask);
2914            idx.vec[2] = _mm_and_si128(_mm_mullo_epi32(idx.vec[2], idx_mul_0), idx_mask);
2915            idx.vec[3] = _mm_and_si128(_mm_mullo_epi32(idx.vec[3], idx_mul_1), idx_mask);
2916
2917            idx.vec[0] = _mm_or_si128(idx.vec[0], _mm_cvtepi16_epi32(idx_l_0));
2918            idx.vec[1] = _mm_or_si128(idx.vec[1], _mm_cvtepi16_epi32(_mm_srli_si128(idx_l_0, 8)));
2919            idx.vec[2] = _mm_or_si128(idx.vec[2], _mm_cvtepi16_epi32(idx_l_1));
2920            idx.vec[3] = _mm_or_si128(idx.vec[3], _mm_cvtepi16_epi32(_mm_srli_si128(idx_l_1, 8)));
2921
2922            const __m128i q2_1_0 = _mm_set_epi32(iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]);
2923            const __m128i q2_1_1 = _mm_set_epi32(iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]]);
2924            const __m128i q2_2_0 = _mm_set_epi32(iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[9]], iq3s_grid[idx.index[8]]);
2925            const __m128i q2_2_1 = _mm_set_epi32(iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]]);
2926
2927            __m128i aux128_0 = _mm_set1_epi32(signs[0] | (signs[1] << 16));
2928            __m128i aux128_1 = aux128_0;
2929            aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0);
2930            aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1);
2931            const __m128i s2_1_0 = _mm_cmpeq_epi8(aux128_0, mask2_0);
2932            const __m128i s2_1_1 = _mm_cmpeq_epi8(aux128_1, mask2_1);
2933            const __m128i q8s_1_0 = _mm_sub_epi8(_mm_xor_si128(s2_1_0, q8_1_0), s2_1_0);
2934            const __m128i q8s_1_1 = _mm_sub_epi8(_mm_xor_si128(s2_1_1, q8_1_1), s2_1_1);
2935
2936            aux128_0 = _mm_set1_epi32(signs[2] | (signs[3] << 16));
2937            aux128_1 = aux128_0;
2938            aux128_0 = _mm_and_si128(_mm_shuffle_epi8(aux128_0,mask1_0), mask2_0);
2939            aux128_1 = _mm_and_si128(_mm_shuffle_epi8(aux128_1,mask1_1), mask2_1);
2940            const __m128i s2_2_0 = _mm_cmpeq_epi8(aux128_0, mask2_0);
2941            const __m128i s2_2_1 = _mm_cmpeq_epi8(aux128_1, mask2_1);
2942            const __m128i q8s_2_0 = _mm_sub_epi8(_mm_xor_si128(s2_2_0, q8_2_0), s2_2_0);
2943            const __m128i q8s_2_1 = _mm_sub_epi8(_mm_xor_si128(s2_2_1, q8_2_1), s2_2_1);
2944
2945            signs += 4;
2946
2947            const __m128i dot1_0  = _mm_maddubs_epi16(q2_1_0, q8s_1_0);
2948            const __m128i dot1_1  = _mm_maddubs_epi16(q2_1_1, q8s_1_1);
2949            const __m128i dot2_0  = _mm_maddubs_epi16(q2_2_0, q8s_2_0);
2950            const __m128i dot2_1  = _mm_maddubs_epi16(q2_2_1, q8s_2_1);
2951            const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
2952            const uint16_t ls2 = x[i].scales[ib32/2] >>  4;
2953            const __m128i p1_0 = _mm_madd_epi16(dot1_0, _mm_set1_epi16(2*ls1+1));
2954            const __m128i p1_1 = _mm_madd_epi16(dot1_1, _mm_set1_epi16(2*ls1+1));
2955            const __m128i p2_0 = _mm_madd_epi16(dot2_0, _mm_set1_epi16(2*ls2+1));
2956            const __m128i p2_1 = _mm_madd_epi16(dot2_1, _mm_set1_epi16(2*ls2+1));
2957            sumi1_0 = _mm_add_epi32(sumi1_0, p1_0);
2958            sumi1_1 = _mm_add_epi32(sumi1_1, p1_1);
2959            sumi2_0 = _mm_add_epi32(sumi2_0, p2_0);
2960            sumi2_1 = _mm_add_epi32(sumi2_1, p2_1);
2961        }
2962
2963        accumf = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(_mm_add_epi32(sumi1_1, sumi2_1), _mm_add_epi32(sumi1_0, sumi2_0)))), accumf);
2964
2965    }
2966
2967    *s = hsum_float_8(accumf);
2968
2969#else
2970    UNUSED(x);
2971    UNUSED(y);
2972    UNUSED(nb);
2973    generic::dot_iq3_s_q8_K_generic(n, s, vx, vy);
2974#endif
2975}
2976
2977void dot_iq4_xs_q8_K(int n, float* STRATA_RESTRICT s, const void* STRATA_RESTRICT vx, const void* STRATA_RESTRICT vy) {
2978    assert(n % QK_K == 0);
2979
2980    const block_iq4_xs* STRATA_RESTRICT x = (const block_iq4_xs*) vx;
2981    const block_q8_K* STRATA_RESTRICT y = (const block_q8_K*) vy;
2982
2983    const int nb = n / QK_K;
2984
2985#if defined __AVX2__
2986
2987    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
2988    const __m128i m4b  = _mm_set1_epi8(0x0f);
2989
2990    __m256 accum = _mm256_setzero_ps();
2991    for (int ibl = 0; ibl < nb; ++ibl) {
2992        const uint8_t * qs = x[ibl].qs;
2993        const int8_t  * q8 = y[ibl].qs;
2994        uint16_t sh = x[ibl].scales_h;
2995        __m256i sumi1 = _mm256_setzero_si256();
2996        __m256i sumi2 = _mm256_setzero_si256();
2997        for (int ib = 0; ib < QK_K/32; ib += 2) {
2998            const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs);  qs += 16;
2999            const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs);  qs += 16;
3000            const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
3001            const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
3002            const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
3003                                                  _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
3004            const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
3005                                                  _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
3006            const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
3007            const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
3008            const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
3009            const int16_t ls2 = ((x[ibl].scales_l[ib/2] >>  4) | ((sh << 2) & 0x30)) - 32;
3010            sh >>= 4;
3011            const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
3012            const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
3013            sumi1 = _mm256_add_epi32(p_1, sumi1);
3014            sumi2 = _mm256_add_epi32(p_2, sumi2);
3015        }
3016        accum = _mm256_fmadd_ps(_mm256_set1_ps(h2f(x[ibl].d)*y[ibl].d),
3017                _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
3018    }
3019
3020    *s = hsum_float_8(accum);
3021
3022#elif defined __AVX__
3023    const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
3024    const __m128i m4b  = _mm_set1_epi8(0x0f);
3025
3026    __m256 accum = _mm256_setzero_ps();
3027    for (int ibl = 0; ibl < nb; ++ibl) {
3028        const uint8_t * qs = x[ibl].qs;
3029        const int8_t  * q8 = y[ibl].qs;
3030        uint16_t sh = x[ibl].scales_h;
3031        __m128i sumi1_0 = _mm_setzero_si128();
3032        __m128i sumi1_1 = _mm_setzero_si128();
3033        __m128i sumi2_0 = _mm_setzero_si128();
3034        __m128i sumi2_1 = _mm_setzero_si128();
3035        for (int ib = 0; ib < QK_K/32; ib += 2) {
3036            const __m128i q4bits_1 = _mm_loadu_si128((const __m128i *)qs); qs += 16;
3037            const __m128i q4bits_2 = _mm_loadu_si128((const __m128i *)qs); qs += 16;
3038            const __m128i q8b_1_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
3039            const __m128i q8b_1_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
3040            const __m128i q8b_2_0 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
3041            const __m128i q8b_2_1 = _mm_loadu_si128((const __m128i *)q8); q8 += 16;
3042            const __m128i q4b_1_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b));
3043            const __m128i q4b_1_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b));
3044            const __m128i q4b_2_0 = _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b));
3045            const __m128i q4b_2_1 = _mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b));
3046            const __m128i p16_1_0 = mul_add_epi8_sse(q4b_1_0, q8b_1_0);
3047            const __m128i p16_1_1 = mul_add_epi8_sse(q4b_1_1, q8b_1_1);
3048            const __m128i p16_2_0 = mul_add_epi8_sse(q4b_2_0, q8b_2_0);
3049            const __m128i p16_2_1 = mul_add_epi8_sse(q4b_2_1, q8b_2_1);
3050            const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
3051            const int16_t ls2 = ((x[ibl].scales_l[ib/2] >>  4) | ((sh << 2) & 0x30)) - 32;
3052            sh >>= 4;
3053            const __m128i p_1_0 = _mm_madd_epi16(p16_1_0, _mm_set1_epi16(ls1));
3054            const __m128i p_1_1 = _mm_madd_epi16(p16_1_1, _mm_set1_epi16(ls1));
3055            const __m128i p_2_0 = _mm_madd_epi16(p16_2_0, _mm_set1_epi16(ls2));
3056            const __m128i p_2_1 = _mm_madd_epi16(p16_2_1, _mm_set1_epi16(ls2));
3057            sumi1_0 = _mm_add_epi32(p_1_0, sumi1_0);
3058            sumi1_1 = _mm_add_epi32(p_1_1, sumi1_1);
3059            sumi2_0 = _mm_add_epi32(p_2_0, sumi2_0);
3060            sumi2_1 = _mm_add_epi32(p_2_1, sumi2_1);
3061        }
3062        __m128i sumi12_0 = _mm_add_epi32(sumi1_0, sumi2_0);
3063        __m128i sumi12_1 = _mm_add_epi32(sumi1_1, sumi2_1);
3064        accum = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(h2f(x[ibl].d)*y[ibl].d),
3065                _mm256_cvtepi32_ps(MM256_SET_M128I(sumi12_1, sumi12_0))), accum);
3066    }
3067
3068    *s = hsum_float_8(accum);
3069
3070#else
3071    UNUSED(x);
3072    UNUSED(y);
3073    UNUSED(nb);
3074    generic::dot_iq4_xs_q8_K_generic(n, s, vx, vy);
3075#endif
3076}