rustsynth-sys 0.5.6

Low level bindings to VapourSynth
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <immintrin.h>
#include <VSHelper4.h>
#include "../generic.h"

#ifdef _MSC_VER
#define FORCE_INLINE inline __forceinline
#else
#define FORCE_INLINE inline __attribute__((always_inline))
#endif

namespace {

template <class T>
T *line_ptr(T *ptr, unsigned i, ptrdiff_t stride)
{
    return (T *)(((unsigned char *)ptr) + static_cast<ptrdiff_t>(i) * stride);
}

template <class T>
void flip_left(T *ptr, unsigned n)
{
    for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(n); ++i) {
        ptr[-1 - i] = ptr[i];
    }
}

template <class T>
void flip_right(T *ptr, unsigned n)
{
    for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(n); ++i) {
        ptr[i] = ptr[-1 - i];
    }
}

FORCE_INLINE __m256 scale_bias_saturate(__m256 x, const __m256 &scale, const __m256 &bias, const __m256 &saturatemask)
{
    return _mm256_and_ps(_mm256_fmadd_ps(x, scale, bias), saturatemask);
}

FORCE_INLINE __m128i export_u8(__m256i i32_lo, __m256i i32_hi, const __m256 &scale, const __m256 &bias, const __m256 &saturatemask)
{
    __m256 tmp;

    tmp = scale_bias_saturate(_mm256_cvtepi32_ps(i32_lo), scale, bias, saturatemask);
    i32_lo = _mm256_cvtps_epi32(tmp);

    tmp = scale_bias_saturate(_mm256_cvtepi32_ps(i32_hi), scale, bias, saturatemask);
    i32_hi = _mm256_cvtps_epi32(tmp);

    i32_lo = _mm256_packs_epi32(i32_lo, i32_hi);
    i32_lo = _mm256_packus_epi16(i32_lo, i32_lo);
    i32_lo = _mm256_permute4x64_epi64(i32_lo, _MM_SHUFFLE(3, 1, 2, 0));
    return _mm256_castsi256_si128(i32_lo);
}

FORCE_INLINE __m256i export_u16(__m256i i32_lo, __m256i i32_hi, const __m256 &scale, const __m256 &bias, const __m256 &saturatemask, const __m256i &maxval)
{
    __m256 tmp;

    tmp = scale_bias_saturate(_mm256_cvtepi32_ps(i32_lo), scale, bias, saturatemask);
    i32_lo = _mm256_cvtps_epi32(tmp);

    tmp = scale_bias_saturate(_mm256_cvtepi32_ps(i32_hi), scale, bias, saturatemask);
    i32_hi = _mm256_cvtps_epi32(tmp);

    i32_lo = _mm256_packus_epi32(i32_lo, i32_hi);
    i32_lo = _mm256_min_epu16(i32_lo, maxval);
    return i32_lo;
}


template <unsigned N, unsigned K, bool First, bool Last>
void conv_scanline_h_byte_pass(const uint8_t *src, uint8_t *dst, int32_t *tmp, const vs_generic_params &params, unsigned n)
{
    auto weight = [=](unsigned k) -> uint32_t { return k < N ? static_cast<uint16_t>(params.matrix[K + k]) : 0; };

    __m256i w0w1 = _mm256_set1_epi32((weight(1) << 16) | weight(0));
    __m256i w2w3 = _mm256_set1_epi32((weight(3) << 16) | weight(2));
    __m256i w4w5 = _mm256_set1_epi32((weight(5) << 16) | weight(4));
    __m256i w6w7 = _mm256_set1_epi32((weight(7) << 16) | weight(6));
    __m256i w8w9 = _mm256_set1_epi32((weight(9) << 16) | weight(8));
    __m256i w10w11 = _mm256_set1_epi32((weight(11) << 16) | weight(10));
    __m256i w12 = _mm256_set1_epi32(weight(12));

    __m256 scale = _mm256_broadcast_ss(&params.div);
    __m256 bias = _mm256_broadcast_ss(&params.bias);
    __m256 saturatemask = _mm256_castsi256_ps(_mm256_set1_epi32(params.saturate ? 0xFFFFFFFF : 0x7FFFFFFF));

    src = src - static_cast<ptrdiff_t>(params.matrixsize / 2) + K;

    for (ptrdiff_t j = 0; j < static_cast<ptrdiff_t>(n); j += 16) {
        __m256i accum_lo = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 0));
        __m256i accum_hi = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 8));
        __m256i x0, x1;

        if (N >= 1) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 0)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 1)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w0w1));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w0w1));
        }

        if (N >= 3) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 2)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 3)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w2w3));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w2w3));
        }

        if (N >= 5) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 4)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 5)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w4w5));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w4w5));
        }

        if (N >= 7) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 6)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 7)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w6w7));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w6w7));
        }

        if (N >= 9) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 8)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 9)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w8w9));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w8w9));
        }

        if (N >= 11) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 10)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 11)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w10w11));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w10w11));
        }

        if (N >= 13) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(src + j + 12)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x0), w12));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x0), w12));
        }

        if (Last) {
            __m128i result = export_u8(accum_lo, accum_hi, scale, bias, saturatemask);
            _mm_store_si128((__m128i *)(dst + j), result);
        } else {
            _mm256_store_si256((__m256i *)(tmp + j + 0), accum_lo);
            _mm256_store_si256((__m256i *)(tmp + j + 8), accum_hi);
        }
    }
}

template <unsigned N, unsigned K, bool First, bool Last>
void conv_scanline_h_word_pass(const uint16_t *src, uint16_t *dst, int32_t *tmp, const vs_generic_params &params, unsigned n, int32_t i32bias)
{
    auto weight = [=](unsigned k) -> uint32_t { return k < N ? static_cast<uint16_t>(params.matrix[K + k]) : 0; };

    __m256i w0w1 = _mm256_set1_epi32((weight(1) << 16) | weight(0));
    __m256i w2w3 = _mm256_set1_epi32((weight(3) << 16) | weight(2));
    __m256i w4w5 = _mm256_set1_epi32((weight(5) << 16) | weight(4));
    __m256i w6w7 = _mm256_set1_epi32((weight(7) << 16) | weight(6));
    __m256i w8w9 = _mm256_set1_epi32((weight(9) << 16) | weight(8));
    __m256i w10w11 = _mm256_set1_epi32((weight(11) << 16) | weight(10));
    __m256i w12 = _mm256_set1_epi32(weight(12));
    __m256i w_bias = _mm256_set1_epi32(i32bias);

    __m256 scale = _mm256_broadcast_ss(&params.div);
    __m256 bias = _mm256_broadcast_ss(&params.bias);
    __m256 saturatemask = _mm256_castsi256_ps(_mm256_set1_epi32(params.saturate ? 0xFFFFFFFF : 0x7FFFFFFF));
    __m256i maxval = _mm256_set1_epi16(params.maxval);

    src = src - static_cast<ptrdiff_t>(params.matrixsize / 2) + K;

    for (ptrdiff_t j = 0; j < static_cast<ptrdiff_t>(n); j += 16) {
        __m256i accum_lo = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 0));
        __m256i accum_hi = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 8));
        __m256i x0, x1;

        if (N >= 1) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 0)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 1)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w0w1));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w0w1));
        }

        if (N >= 3) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 2)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 3)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w2w3));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w2w3));
        }

        if (N >= 5) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 4)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 5)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w4w5));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w4w5));
        }

        if (N >= 7) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 6)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 7)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w6w7));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w6w7));
        }

        if (N >= 9) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 8)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 9)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w8w9));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w8w9));
        }

        if (N >= 11) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 10)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 11)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w10w11));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w10w11));
        }

        if (N >= 13) {
            x0 = _mm256_add_epi16(_mm256_loadu_si256((const __m256i *)(src + j + 12)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x0), w12));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x0), w12));
        }

        if (Last) {
            accum_lo = _mm256_sub_epi32(accum_lo, w_bias);
            accum_hi = _mm256_sub_epi32(accum_hi, w_bias);
            accum_lo = export_u16(accum_lo, accum_hi, scale, bias, saturatemask, maxval);
            _mm256_store_si256((__m256i *)(dst + j), accum_lo);
        } else {
            _mm256_store_si256((__m256i *)(tmp + j + 0), accum_lo);
            _mm256_store_si256((__m256i *)(tmp + j + 8), accum_hi);
        }
    }
}

template <unsigned N, unsigned K, bool First, bool Last>
void conv_scanline_h_float_pass(const float *src, float *dst, const vs_generic_params &params, unsigned n)
{
    auto weight = [=](unsigned k) -> float { return k < N ? params.matrixf[K + k] : 0; };

    __m256 w0 = _mm256_set1_ps(weight(0));
    __m256 w1 = _mm256_set1_ps(weight(1));
    __m256 w2 = _mm256_set1_ps(weight(2));
    __m256 w3 = _mm256_set1_ps(weight(3));
    __m256 w4 = _mm256_set1_ps(weight(4));
    __m256 w5 = _mm256_set1_ps(weight(5));
    __m256 w6 = _mm256_set1_ps(weight(6));
    __m256 w7 = _mm256_set1_ps(weight(7));
    __m256 w8 = _mm256_set1_ps(weight(8));
    __m256 w9 = _mm256_set1_ps(weight(9));

    __m256 scale = _mm256_broadcast_ss(&params.div);
    __m256 bias = _mm256_broadcast_ss(&params.bias);
    __m256 saturatemask = _mm256_castsi256_ps(_mm256_set1_epi32(params.saturate ? 0xFFFFFFFF : 0x7FFFFFFF));

    src = src - static_cast<ptrdiff_t>(params.matrixsize / 2) + K;

    for (ptrdiff_t j = 0; j < static_cast<ptrdiff_t>(n); j += 8) {
        __m256 accum0 = First ? _mm256_setzero_ps() : _mm256_load_ps(dst + j);
        __m256 accum1 = _mm256_setzero_ps();

        if (N >= 1) accum0 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 0), w0, accum0);
        if (N >= 2) accum1 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 1), w1, accum1);
        if (N >= 3) accum0 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 2), w2, accum0);
        if (N >= 4) accum1 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 3), w3, accum1);
        if (N >= 5) accum0 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 4), w4, accum0);
        if (N >= 6) accum1 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 5), w5, accum1);
        if (N >= 7) accum0 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 6), w6, accum0);
        if (N >= 8) accum1 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 7), w7, accum1);
        if (N >= 9) accum0 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 8), w8, accum0);
        if (N >= 10) accum1 = _mm256_fmadd_ps(_mm256_loadu_ps(src + j + 9), w9, accum1);

        accum0 = _mm256_add_ps(accum0, accum1);
        if (Last) accum0 = scale_bias_saturate(accum0, scale, bias, saturatemask);
        _mm256_store_ps(dst + j, accum0);
    }
}

template <unsigned N, unsigned K, bool First, bool Last>
void conv_scanline_v_byte_pass(const void * const src[], uint8_t *dst, int32_t *tmp, const vs_generic_params &params, unsigned n)
{
    auto weight = [=](unsigned k) -> uint32_t { return k < N ? static_cast<uint16_t>(params.matrix[K + k]) : 0; };

    const uint8_t *srcp0 = static_cast<const uint8_t *>(src[K + 0]);
    const uint8_t *srcp1 = N >= 2 ? static_cast<const uint8_t *>(src[K + 1]) : srcp0;
    const uint8_t *srcp2 = N >= 3 ? static_cast<const uint8_t *>(src[K + 2]) : srcp1;
    const uint8_t *srcp3 = N >= 4 ? static_cast<const uint8_t *>(src[K + 3]) : srcp2;
    const uint8_t *srcp4 = N >= 5 ? static_cast<const uint8_t *>(src[K + 4]) : srcp3;
    const uint8_t *srcp5 = N >= 6 ? static_cast<const uint8_t *>(src[K + 5]) : srcp4;
    const uint8_t *srcp6 = N >= 7 ? static_cast<const uint8_t *>(src[K + 6]) : srcp5;
    const uint8_t *srcp7 = N >= 8 ? static_cast<const uint8_t *>(src[K + 7]) : srcp6;
    const uint8_t *srcp8 = N >= 9 ? static_cast<const uint8_t *>(src[K + 8]) : srcp7;
    const uint8_t *srcp9 = N >= 10 ? static_cast<const uint8_t *>(src[K + 9]) : srcp8;

    __m256i w0w1 = _mm256_set1_epi32((weight(1) << 16) | weight(0));
    __m256i w2w3 = _mm256_set1_epi32((weight(3) << 16) | weight(2));
    __m256i w4w5 = _mm256_set1_epi32((weight(5) << 16) | weight(4));
    __m256i w6w7 = _mm256_set1_epi32((weight(7) << 16) | weight(6));
    __m256i w8w9 = _mm256_set1_epi32((weight(9) << 16) | weight(8));

    __m256 scale = _mm256_broadcast_ss(&params.div);
    __m256 bias = _mm256_broadcast_ss(&params.bias);
    __m256 saturatemask = _mm256_castsi256_ps(_mm256_set1_epi32(params.saturate ? 0xFFFFFFFF : 0x7FFFFFFF));

    src = src - static_cast<ptrdiff_t>(params.matrixsize / 2) + K;

    for (ptrdiff_t j = 0; j < static_cast<ptrdiff_t>(n); j += 16) {
        __m256i accum_lo = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 0));
        __m256i accum_hi = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 8));
        __m256i x0, x1;

        if (N >= 1) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp0 + j)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp1 + j)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w0w1));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w0w1));
        }

        if (N >= 3) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp2 + j)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp3 + j)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w2w3));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w2w3));
        }

        if (N >= 5) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp4 + j)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp5 + j)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w4w5));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w4w5));
        }

        if (N >= 7) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp6 + j)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp7 + j)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w6w7));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w6w7));
        }

        if (N >= 9) {
            x0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp8 + j)));
            x1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)(srcp9 + j)));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w8w9));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w8w9));
        }

        if (Last) {
            __m128i result = export_u8(accum_lo, accum_hi, scale, bias, saturatemask);
            _mm_store_si128((__m128i *)(dst + j), result);
        } else {
            _mm256_store_si256((__m256i *)(tmp + j + 0), accum_lo);
            _mm256_store_si256((__m256i *)(tmp + j + 8), accum_hi);
        }
    }
}

template <unsigned N, unsigned K, bool First, bool Last>
void conv_scanline_v_word_pass(const void * const src[], uint16_t *dst, int32_t *tmp, const vs_generic_params &params, unsigned n, int32_t i32bias)
{
    auto weight = [=](unsigned k) -> uint32_t { return k < N ? static_cast<uint16_t>(params.matrix[K + k]) : 0; };

    const uint16_t *srcp0 = static_cast<const uint16_t *>(src[K + 0]);
    const uint16_t *srcp1 = N >= 2 ? static_cast<const uint16_t *>(src[K + 1]) : srcp0;
    const uint16_t *srcp2 = N >= 3 ? static_cast<const uint16_t *>(src[K + 2]) : srcp1;
    const uint16_t *srcp3 = N >= 4 ? static_cast<const uint16_t *>(src[K + 3]) : srcp2;
    const uint16_t *srcp4 = N >= 5 ? static_cast<const uint16_t *>(src[K + 4]) : srcp3;
    const uint16_t *srcp5 = N >= 6 ? static_cast<const uint16_t *>(src[K + 5]) : srcp4;
    const uint16_t *srcp6 = N >= 7 ? static_cast<const uint16_t *>(src[K + 6]) : srcp5;
    const uint16_t *srcp7 = N >= 8 ? static_cast<const uint16_t *>(src[K + 7]) : srcp6;
    const uint16_t *srcp8 = N >= 9 ? static_cast<const uint16_t *>(src[K + 8]) : srcp7;
    const uint16_t *srcp9 = N >= 10 ? static_cast<const uint16_t *>(src[K + 9]) : srcp8;

    __m256i w0w1 = _mm256_set1_epi32((weight(1) << 16) | weight(0));
    __m256i w2w3 = _mm256_set1_epi32((weight(3) << 16) | weight(2));
    __m256i w4w5 = _mm256_set1_epi32((weight(5) << 16) | weight(4));
    __m256i w6w7 = _mm256_set1_epi32((weight(7) << 16) | weight(6));
    __m256i w8w9 = _mm256_set1_epi32((weight(9) << 16) | weight(8));
    __m256i w_bias = _mm256_set1_epi32(i32bias);

    __m256 scale = _mm256_broadcast_ss(&params.div);
    __m256 bias = _mm256_broadcast_ss(&params.bias);
    __m256 saturatemask = _mm256_castsi256_ps(_mm256_set1_epi32(params.saturate ? 0xFFFFFFFF : 0x7FFFFFFF));
    __m256i maxval = _mm256_set1_epi16(params.maxval);

    src = src - static_cast<ptrdiff_t>(params.matrixsize / 2) + K;

    for (ptrdiff_t j = 0; j < static_cast<ptrdiff_t>(n); j += 16) {
        __m256i accum_lo = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 0));
        __m256i accum_hi = First ? _mm256_setzero_si256() : _mm256_load_si256((const __m256i *)(tmp + j + 8));
        __m256i x0, x1;

        if (N >= 1) {
            x0 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp0 + j)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp1 + j)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w0w1));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w0w1));
        }

        if (N >= 3) {
            x0 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp2 + j)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp3 + j)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w2w3));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w2w3));
        }

        if (N >= 5) {
            x0 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp4 + j)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp5 + j)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w4w5));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w4w5));
        }

        if (N >= 7) {
            x0 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp6 + j)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp7 + j)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w6w7));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w6w7));
        }

        if (N >= 9) {
            x0 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp8 + j)), _mm256_set1_epi16(INT16_MIN));
            x1 = _mm256_add_epi16(_mm256_load_si256((const __m256i *)(srcp9 + j)), _mm256_set1_epi16(INT16_MIN));
            accum_lo = _mm256_add_epi32(accum_lo, _mm256_madd_epi16(_mm256_unpacklo_epi16(x0, x1), w8w9));
            accum_hi = _mm256_add_epi32(accum_hi, _mm256_madd_epi16(_mm256_unpackhi_epi16(x0, x1), w8w9));
        }

        if (Last) {
            accum_lo = _mm256_sub_epi32(accum_lo, w_bias);
            accum_hi = _mm256_sub_epi32(accum_hi, w_bias);
            accum_lo = export_u16(accum_lo, accum_hi, scale, bias, saturatemask, maxval);
            _mm256_store_si256((__m256i *)(dst + j), accum_lo);
        } else {
            _mm256_store_si256((__m256i *)(tmp + j + 0), accum_lo);
            _mm256_store_si256((__m256i *)(tmp + j + 8), accum_hi);
        }
    }
}

template <unsigned N, unsigned K, bool First, bool Last>
void conv_scanline_v_float_pass(const void * const src[], float *dst, const vs_generic_params &params, unsigned n)
{
    auto weight = [=](unsigned k) -> float { return k < N ? params.matrixf[K + k] : 0; };

    const float *srcp0 = static_cast<const float *>(src[K + 0]);
    const float *srcp1 = N >= 2 ? static_cast<const float *>(src[K + 1]) : srcp0;
    const float *srcp2 = N >= 3 ? static_cast<const float *>(src[K + 2]) : srcp1;
    const float *srcp3 = N >= 4 ? static_cast<const float *>(src[K + 3]) : srcp2;
    const float *srcp4 = N >= 5 ? static_cast<const float *>(src[K + 4]) : srcp3;
    const float *srcp5 = N >= 6 ? static_cast<const float *>(src[K + 5]) : srcp4;
    const float *srcp6 = N >= 7 ? static_cast<const float *>(src[K + 6]) : srcp5;
    const float *srcp7 = N >= 8 ? static_cast<const float *>(src[K + 7]) : srcp6;
    const float *srcp8 = N >= 9 ? static_cast<const float *>(src[K + 8]) : srcp7;
    const float *srcp9 = N >= 10 ? static_cast<const float *>(src[K + 9]) : srcp8;

    __m256 w0 = _mm256_set1_ps(weight(0));
    __m256 w1 = _mm256_set1_ps(weight(1));
    __m256 w2 = _mm256_set1_ps(weight(2));
    __m256 w3 = _mm256_set1_ps(weight(3));
    __m256 w4 = _mm256_set1_ps(weight(4));
    __m256 w5 = _mm256_set1_ps(weight(5));
    __m256 w6 = _mm256_set1_ps(weight(6));
    __m256 w7 = _mm256_set1_ps(weight(7));
    __m256 w8 = _mm256_set1_ps(weight(8));
    __m256 w9 = _mm256_set1_ps(weight(9));

    __m256 scale = _mm256_broadcast_ss(&params.div);
    __m256 bias = _mm256_broadcast_ss(&params.bias);
    __m256 saturatemask = _mm256_castsi256_ps(_mm256_set1_epi32(params.saturate ? 0xFFFFFFFF : 0x7FFFFFFF));

    src = src - static_cast<ptrdiff_t>(params.matrixsize / 2) + K;

    for (ptrdiff_t j = 0; j < static_cast<ptrdiff_t>(n); j += 8) {
        __m256 accum0 = First ? _mm256_setzero_ps() : _mm256_load_ps(dst + j);
        __m256 accum1 = _mm256_setzero_ps();

        if (N >= 1) accum0 = _mm256_fmadd_ps(_mm256_load_ps(srcp0 + j), w0, accum0);
        if (N >= 2) accum1 = _mm256_fmadd_ps(_mm256_load_ps(srcp1 + j), w1, accum1);
        if (N >= 3) accum0 = _mm256_fmadd_ps(_mm256_load_ps(srcp2 + j), w2, accum0);
        if (N >= 4) accum1 = _mm256_fmadd_ps(_mm256_load_ps(srcp3 + j), w3, accum1);
        if (N >= 5) accum0 = _mm256_fmadd_ps(_mm256_load_ps(srcp4 + j), w4, accum0);
        if (N >= 6) accum1 = _mm256_fmadd_ps(_mm256_load_ps(srcp5 + j), w5, accum1);
        if (N >= 7) accum0 = _mm256_fmadd_ps(_mm256_load_ps(srcp6 + j), w6, accum0);
        if (N >= 8) accum1 = _mm256_fmadd_ps(_mm256_load_ps(srcp7 + j), w7, accum1);
        if (N >= 9) accum0 = _mm256_fmadd_ps(_mm256_load_ps(srcp8 + j), w8, accum0);
        if (N >= 10) accum1 = _mm256_fmadd_ps(_mm256_load_ps(srcp9 + j), w9, accum1);

        accum0 = _mm256_add_ps(accum0, accum1);
        if (Last) accum0 = scale_bias_saturate(accum0, scale, bias, saturatemask);
        _mm256_store_ps(dst + j, accum0);
    }
}
template <unsigned N>
void conv_scanline_h_byte(const void *src, void *dst, void *tmp, const vs_generic_params &params, unsigned n)
{
    const uint8_t *srcp = static_cast<const uint8_t *>(src);
    uint8_t *dstp = static_cast<uint8_t *>(dst);
    int32_t *tmpp = static_cast<int32_t *>(tmp);

    if (N > 13) {
        conv_scanline_h_byte_pass<12, 0, true, false>(srcp, dstp, tmpp, params, n);
        conv_scanline_h_byte_pass<N - 12, 12, false, true>(srcp, dstp, tmpp, params, n);
    } else {
        conv_scanline_h_byte_pass<N, 0, true, true>(srcp, dstp, nullptr, params, n);
    }
}

template <unsigned N>
void conv_scanline_h_word(const void *src, void *dst, void *tmp, const vs_generic_params &params, unsigned n)
{
    const uint16_t *srcp = static_cast<const uint16_t *>(src);
    uint16_t *dstp = static_cast<uint16_t *>(dst);
    int32_t *tmpp = static_cast<int32_t *>(tmp);
    int32_t bias = 0;

    for (unsigned k = 0; k < N; ++k) {
        bias += static_cast<int32_t>(INT16_MIN) * params.matrix[k];
    }

    if (N > 13) {
        conv_scanline_h_word_pass<12, 0, true, false>(srcp, dstp, tmpp, params, n, bias);
        conv_scanline_h_word_pass<N - 12, 12, false, true>(srcp, dstp, tmpp, params, n, bias);
    } else {
        conv_scanline_h_word_pass<N, 0, true, true>(srcp, dstp, nullptr, params, n, bias);
    }
}

template <unsigned N>
void conv_scanline_h_float(const void *src, void *dst, void *, const vs_generic_params &params, unsigned n)
{
    const float *srcp = static_cast<const float *>(src);
    float *dstp = static_cast<float *>(dst);

    if (N > 19) {
        conv_scanline_h_float_pass<10, 0, true, false>(srcp, dstp, params, n);
        conv_scanline_h_float_pass<10, 10, false, false>(srcp, dstp, params, n);
        conv_scanline_h_float_pass<N - 20, 20, false, true>(srcp, dstp, params, n);
    } else if (N > 9) {
        conv_scanline_h_float_pass<10, 0, true, false>(srcp, dstp, params, n);
        conv_scanline_h_float_pass<N - 10, 10, false, true>(srcp, dstp, params, n);
    } else {
        conv_scanline_h_float_pass<N, 0, true, true>(srcp, dstp, params, n);
    }
}

template <unsigned N>
void conv_scanline_v_byte(const void * const src[], void *dst, void *tmp, const vs_generic_params &params, unsigned n)
{
    uint8_t *dstp = static_cast<uint8_t *>(dst);
    int32_t *tmpp = static_cast<int32_t *>(tmp);

    if (N > 19) {
        conv_scanline_v_byte_pass<10, 0, true, false>(src, dstp, tmpp, params, n);
        conv_scanline_v_byte_pass<10, 10, false, false>(src, dstp, tmpp, params, n);
        conv_scanline_v_byte_pass<N - 20, 20, false, true>(src, dstp, tmpp, params, n);
    } else if (N > 9) {
        conv_scanline_v_byte_pass<10, 0, true, false>(src, dstp, tmpp, params, n);
        conv_scanline_v_byte_pass<N - 10, 10, false, true>(src, dstp, tmpp, params, n);
    } else {
        conv_scanline_v_byte_pass<N, 0, true, true>(src, dstp, tmpp, params, n);
    }
}

template <unsigned N>
void conv_scanline_v_word(const void * const src[], void *dst, void *tmp, const vs_generic_params &params, unsigned n)
{
    uint16_t *dstp = static_cast<uint16_t *>(dst);
    int32_t *tmpp = static_cast<int32_t *>(tmp);
    int32_t bias = 0;

    for (unsigned k = 0; k < N; ++k) {
        bias += static_cast<int32_t>(INT16_MIN) * params.matrix[k];
    }

    if (N > 19) {
        conv_scanline_v_word_pass<10, 0, true, false>(src, dstp, tmpp, params, n, bias);
        conv_scanline_v_word_pass<10, 10, false, false>(src, dstp, tmpp, params, n, bias);
        conv_scanline_v_word_pass<N - 20, 20, false, true>(src, dstp, tmpp, params, n, bias);
    } else if (N > 9) {
        conv_scanline_v_word_pass<10, 0, true, false>(src, dstp, tmpp, params, n, bias);
        conv_scanline_v_word_pass<N - 10, 10, false, true>(src, dstp, tmpp, params, n, bias);
    } else {
        conv_scanline_v_word_pass<N, 0, true, true>(src, dstp, tmpp, params, n, bias);
    }
}

template <unsigned N>
void conv_scanline_v_float(const void * const src[], void *dst, void *, const vs_generic_params &params, unsigned n)
{
    float *dstp = static_cast<float *>(dst);

    if (N > 19) {
        conv_scanline_v_float_pass<10, 0, true, false>(src, dstp, params, n);
        conv_scanline_v_float_pass<10, 10, false, false>(src, dstp, params, n);
        conv_scanline_v_float_pass<N - 20, 20, false, true>(src, dstp, params, n);
    } else if (N > 9) {
        conv_scanline_v_float_pass<10, 0, true, false>(src, dstp, params, n);
        conv_scanline_v_float_pass<N - 10, 10, false, true>(src, dstp, params, n);
    } else {
        conv_scanline_v_float_pass<N, 0, true, true>(src, dstp, params, n);
    }
}

template <class T>
decltype(&conv_scanline_h_byte<0>) select_conv_scanline_h(unsigned fwidth);

template <class T>
decltype(&conv_scanline_v_byte<0>) select_conv_scanline_v(unsigned fwidth);

#define SELECT(dir, type, T) \
template <> \
decltype(&conv_scanline_##dir##_##type<0>) select_conv_scanline_##dir<T>(unsigned fwidth) \
{ \
  switch (fwidth) { \
  case 3: return conv_scanline_##dir##_##type<3>; \
  case 5: return conv_scanline_##dir##_##type<5>; \
  case 7: return conv_scanline_##dir##_##type<7>; \
  case 9: return conv_scanline_##dir##_##type<9>; \
  case 11: return conv_scanline_##dir##_##type<11>; \
  case 13: return conv_scanline_##dir##_##type<13>; \
  case 15: return conv_scanline_##dir##_##type<15>; \
  case 17: return conv_scanline_##dir##_##type<17>; \
  case 19: return conv_scanline_##dir##_##type<19>; \
  case 21: return conv_scanline_##dir##_##type<21>; \
  case 23: return conv_scanline_##dir##_##type<23>; \
  case 25: return conv_scanline_##dir##_##type<25>; \
  default: return nullptr; \
  } \
}

SELECT(h, byte, uint8_t)
SELECT(h, word, uint16_t)
SELECT(h, float, float)
SELECT(v, byte, uint8_t)
SELECT(v, word, uint16_t)
SELECT(v, float, float)

#undef SELECT

template <class T>
void conv_plane_h(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const vs_generic_params &params, unsigned width, unsigned height)
{
    unsigned vec_end = std::max((std::max(width, 13U) - 13) & ~15U, 16U);

    // Max support = 12. Max buffering = 12 before, 32 window, 12 after.
    alignas(32) T padded[64];

    // Multi-pass threshold = 13.
    auto kernel = select_conv_scanline_h<T>(params.matrixsize);
    void *tmp = (params.matrixsize > 13 && std::is_integral<T>::value) ? vsh::vsh_aligned_malloc((width + 16) * sizeof(int32_t), 32) : nullptr;

    for (unsigned i = 0; i < height; ++i) {
        const T *srcp = static_cast<const T *>(line_ptr(src, i, src_stride));
        T *dstp = static_cast<T *>(line_ptr(dst, i, dst_stride));

        // Pixels 0...15.
        {
            // Center.
            std::copy_n(srcp, std::min(width, 28U), padded + 16);
            flip_left(padded + 16, 12);
            if (width < 28U)
                flip_right(padded + 16 + width, std::min(28U - width, 12U));
            kernel(padded + 16, dstp, tmp, params, 16U);
        }

        // Pixels 16...vec_end-1.
        if (vec_end > 16U) {
            kernel(srcp + 16, dstp + 16, tmp, params, vec_end - 16U);
        }

        // Pixels vec_end...N-1
        if (width > 16U) {
            std::copy_n(srcp + vec_end - 12U, width - vec_end + 12U, padded + 4U);
            flip_right(padded + 16U + (width - vec_end), 12U);
            kernel(padded + 16, dstp + vec_end, tmp, params, width - vec_end);
        }
    }

    vsh::vsh_aligned_free(tmp);
}

template <class T>
void conv_plane_v(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const vs_generic_params &params, unsigned width, unsigned height)
{
    unsigned fwidth = params.matrixsize;
    unsigned support = fwidth / 2;

    // Multi-pass threshold = 9.
    auto kernel = select_conv_scanline_v<T>(params.matrixsize);
    void *tmp = (params.matrixsize > 9 && std::is_integral<T>::value) ? vsh::vsh_aligned_malloc((width + 16) * sizeof(int32_t), 32) : nullptr;

    for (unsigned i = 0; i < height; ++i) {
        const void *srcp[25];
        void *dstp = line_ptr(dst, i, dst_stride);

        unsigned dist_from_bottom = height - 1 - i;

        for (unsigned k = 0; k < support; ++k) {
            unsigned row = i < support - k ? std::min(support - k - i - 1, height - 1) : i - support + k;
            srcp[k] = line_ptr(src, row, src_stride);
        }
        for (unsigned k = support; k < fwidth; ++k) {
            unsigned row = dist_from_bottom < k - support ? height - std::min(k - support - dist_from_bottom, i) : i - support + k;
            srcp[k] = line_ptr(src, row, src_stride);
        }

        kernel(srcp, dstp, tmp, params, width);
    }

    vsh::vsh_aligned_free(tmp);
}

template <class T>
void conv_plane_x(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const vs_generic_params &params, unsigned width, unsigned height)
{
    unsigned fwidth = params.matrixsize;
    unsigned support = fwidth / 2;

    // Multi-pass threshold = 9 (v), 13 (h).
    auto kernel_v = select_conv_scanline_v<T>(params.matrixsize);
    auto kernel_h = select_conv_scanline_h<T>(params.matrixsize);
    T *tmp1 = vsh::vsh_aligned_malloc<T>((width + 64) * sizeof(T), 32);
    void *tmp2 = (params.matrixsize > 9 && std::is_integral<T>::value) ? vsh::vsh_aligned_malloc((width + 16) * sizeof(int32_t), 32) : nullptr;

    for (unsigned i = 0; i < height; ++i) {
        const void *srcp[25];
        void *dstp = line_ptr(dst, i, dst_stride);

        unsigned dist_from_bottom = height - 1 - i;

        for (unsigned k = 0; k < support; ++k) {
            unsigned row = i < support - k ? std::min(support - k - i - 1, height - 1) : i - support + k;
            srcp[k] = line_ptr(src, row, src_stride);
        }
        for (unsigned k = support; k < fwidth; ++k) {
            unsigned row = dist_from_bottom < k - support ? height - std::min(k - support - dist_from_bottom, i) : i - support + k;
            srcp[k] = line_ptr(src, row, src_stride);
        }

        kernel_v(srcp, tmp1 + 32, tmp2, params, width);
        flip_left(tmp1 + 32, 12);
        flip_right(tmp1 + 32 + width, 12);
        kernel_h(tmp1 + 32, dstp, tmp2, params, width);
    }

    vsh::vsh_aligned_free(tmp2);
    vsh::vsh_aligned_free(tmp1);
}

} // namespace


void vs_generic_1d_conv_h_byte_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_h<uint8_t>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_1d_conv_h_word_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_h<uint16_t>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_1d_conv_h_float_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_h<float>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_1d_conv_v_byte_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_v<uint8_t>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_1d_conv_v_word_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_v<uint16_t>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_1d_conv_v_float_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_v<float>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_2d_conv_sep_byte_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_x<uint8_t>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_2d_conv_sep_word_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_x<uint16_t>(src, src_stride, dst, dst_stride, *params, width, height);
}

void vs_generic_2d_conv_sep_float_avx2(const void *src, ptrdiff_t src_stride, void *dst, ptrdiff_t dst_stride, const struct vs_generic_params *params, unsigned width, unsigned height)
{
    conv_plane_x<float>(src, src_stride, dst, dst_stride, *params, width, height);
}