-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathLocalMultiscaleFilterInterpolation.cpp
2463 lines (2224 loc) · 102 KB
/
LocalMultiscaleFilterInterpolation.cpp
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "multiscalefilter/MultiScaleFilter.hpp"
using namespace cv;
using namespace std;
namespace cp
{
#pragma region FastLLFReference
float FastLLFReference::getTau(const int k)
{
const float delta = intensityRange / (order - 1);
return float(k * delta + intensityMin);
}
void FastLLFReference::blendLaplacianLinear(const vector<vector<Mat>>& LaplacianPyramid, vector<Mat>& GaussianPyramid, vector<Mat>& destPyramid, const int order)
{
const int level = (int)GaussianPyramid.size();
destPyramid.resize(level);
AutoBuffer<const float*> lptr(order);
for (int l = 0; l < level - 1; l++)
{
destPyramid[l].create(GaussianPyramid[l].size(), CV_32F);
float* g = GaussianPyramid[l].ptr<float>();
float* d = destPyramid[l].ptr<float>();
for (int k = 0; k < order; k++)
{
lptr[k] = LaplacianPyramid[k][l].ptr<float>();
}
for (int i = 0; i < GaussianPyramid[l].size().area(); i++)
{
float alpha;
int high, low;
getLinearIndex(g[i], low, high, alpha, order, intensityMin, intensityMax);
d[i] = alpha * lptr[low][i] + (1.f - alpha) * lptr[high][i];
}
}
}
void FastLLFReference::pyramid(const Mat& src, Mat& dest)
{
pyramidComputeMethod = PyramidComputeMethod::Full;
if (GaussianPyramid.size() != level + 1)GaussianPyramid.resize(level + 1);
if (src.depth() == CV_32F) src.copyTo(GaussianPyramid[0]);
else src.convertTo(GaussianPyramid[0], CV_32F);
//(1) build Gaussian Pyramid
buildGaussianPyramid(GaussianPyramid[0], GaussianPyramid, level, sigma_space);
//(2) build Laplacian Pyramids
LaplacianPyramidOrder.resize(order);
for (int n = 0; n < order; n++)
{
LaplacianPyramidOrder[n].resize(level + 1);
//(2)-1 Remap Input Image
if (adaptiveMethod == AdaptiveMethod::FIX) remap(GaussianPyramid[0], LaplacianPyramidOrder[n][0], getTau(n), sigma_range, boost);
else remapAdaptive(GaussianPyramid[0], LaplacianPyramidOrder[n][0], getTau(n), adaptiveSigmaMap[0], adaptiveBoostMap[0]);
//(2)-2 Build Remapped Laplacian Pyramids
buildLaplacianPyramid(LaplacianPyramidOrder[n][0], LaplacianPyramidOrder[n], level, sigma_space);
}
//(3) interpolate Laplacian Pyramid from Remapped Laplacian Pyramids
blendLaplacianLinear(LaplacianPyramidOrder, GaussianPyramid, LaplacianPyramid, order);
//set last level
LaplacianPyramid[level] = GaussianPyramid[level];
//(4) collapse Laplacian Pyramid
collapseLaplacianPyramid(LaplacianPyramid, dest, src.depth());
}
void FastLLFReference::filter(const Mat& src, Mat& dest, const int order, const float sigma_range, const float sigma_space, const float boost, const int level, const ScaleSpace scaleSpaceMethod, const int interpolationMethod)
{
allocSpaceWeight(sigma_space);
this->sigma_range = sigma_range;
this->sigma_space = sigma_space;
this->level = level;
this->boost = boost;
this->scalespaceMethod = scaleSpaceMethod;
this->order = order;
body(src, dest);
freeSpaceWeight();
}
#pragma endregion
void LocalMultiScaleFilterInterpolation::initRangeTableInteger(const float sigma, const float boost)
{
const int intensityRange2 = get_simd_ceil((int)intensityRange, order - 1);
const int tableSize = intensityRange2 + 1;
integerSampleTable = (float*)_mm_malloc(sizeof(float) * tableSize, AVX_ALIGN);
int rem = intensityRange2 - (int)intensityRange;
intensityRange = float(intensityRange2);
intensityMax += (float)rem;
for (int i = 0; i < tableSize; i++)
{
integerSampleTable[i] = getGaussianRangeWeight(float(i), sigma_range, boost);
}
}
float LocalMultiScaleFilterInterpolation::getTau(const int k)
{
#if 1
const float delta = intensityRange / (order - 1);
return float(k * delta + intensityMin);
#else
const float intensityRange = float(intensityMax - intensityMin);
const float delta = intensityRange / (order - 2);
return float(k * delta + intensityMin - delta);
#endif
}
#pragma region pyramid
template<bool is_use_table, int D>
void LocalMultiScaleFilterInterpolation::remapGaussDownIgnoreBoundary(const Mat& src, Mat& remapIm, Mat& dest, const float g, const float sigma_range, const float boost)
{
CV_Assert(src.depth() == CV_32F);
const Size size = src.size();
dest.create(size / 2, CV_32F);
remapIm.create(size, CV_32F);
//const int D = 2 * radius + 1;
const int rs = radius >> 1;
__m256* W = (__m256*)_mm_malloc(sizeof(__m256) * D, AVX_ALIGN);
for (int k = 0; k < D; k++)
{
W[k] = _mm256_set1_ps(GaussWeight[k]);
}
const int width = src.cols;
const int height = src.rows;
#pragma region remap top
const __m256 mg = _mm256_set1_ps(g);
const float coeff = float(1.0 / (-2.0 * sigma_range * sigma_range));
const __m256 mcoeff = _mm256_set1_ps(coeff);
const __m256 mdetail = _mm256_set1_ps(boost);
//splat
{
const float* sptr = src.ptr<float>();
float* d = remapIm.ptr<float>();
const int size = width * (D - 1);
const int REMAPSIZE32 = get_simd_floor(size, 32);
const int REMAPSIZE8 = get_simd_ceil(size, 8);
if constexpr (is_use_table)
{
//float* rt = &rangeTable[0];
float* rt = integerSampleTable;
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
}
else
{
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
}
}
#pragma endregion
const int linesize = src.cols;
float* linebuff = (float*)_mm_malloc(sizeof(float) * linesize, AVX_ALIGN);
//memset(linebuff, 0, sizeof(float) * linesize);
const float* sptr = remapIm.ptr<float>();
float* dptr = dest.ptr<float>(rs, rs);
const int hend = width - 2 * radius;
const int vend = height - 2 * radius;
const int WIDTH = get_simd_floor(width, 8);
const int HEND32 = get_simd_floor(hend, 32);
const int HEND = get_simd_floor(hend, 8);
const __m128i maskhend = get_storemask1(hend, 8);
for (int j = 0; j < vend; j += 2)
{
//remap line
{
const float* sptr = src.ptr<float>(j + D - 1);
float* d = remapIm.ptr<float>(j + D - 1);
const int size = 2 * width;
const int REMAPSIZE32 = get_simd_floor(size, 32);
const int REMAPSIZE8 = get_simd_ceil(size, 8);
if constexpr (is_use_table)
{
//float* rt = &rangeTable[0];
float* rt = integerSampleTable;
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
}
else
{
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
}
}
//v filter
for (int i = 0; i < WIDTH; i += 8)
{
const float* s = sptr + i;
__m256 sum = _mm256_mul_ps(W[0], _mm256_loadu_ps(s));
s += width;
for (int k = 1; k < D; k++)
{
sum = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(s), sum); s += width;
}
_mm256_storeu_ps(linebuff + i, sum);
}
for (int i = WIDTH; i < width; i++)
{
const float* s = sptr + i;
float sum = GaussWeight[0] * *s;
s += width;
for (int k = 1; k < D; k++)
{
sum += GaussWeight[k] * *s;
s += width;
}
linebuff[i] = sum;
}
sptr += 2 * width;
//h filter
for (int i = 0; i < HEND32; i += 32)
{
float* lb0 = linebuff + i;
float* lb1 = linebuff + i + 8;
float* lb2 = linebuff + i + 16;
float* lb3 = linebuff + i + 24;
__m256 sum0 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb0++));
__m256 sum1 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb1++));
__m256 sum2 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb2++));
__m256 sum3 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb3++));
for (int k = 1; k < D; k++)
{
sum0 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb0++), sum0);
sum1 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb1++), sum1);
sum2 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb2++), sum2);
sum3 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb3++), sum3);
}
sum0 = _mm256_shuffle_ps(sum0, sum0, _MM_SHUFFLE(2, 0, 2, 0));
sum0 = _mm256_permute4x64_ps(sum0, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + (i >> 1), _mm256_castps256_ps128(sum0));
sum1 = _mm256_shuffle_ps(sum1, sum1, _MM_SHUFFLE(2, 0, 2, 0));
sum1 = _mm256_permute4x64_ps(sum1, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + ((i + 8) >> 1), _mm256_castps256_ps128(sum1));
sum2 = _mm256_shuffle_ps(sum2, sum2, _MM_SHUFFLE(2, 0, 2, 0));
sum2 = _mm256_permute4x64_ps(sum2, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + ((i + 16) >> 1), _mm256_castps256_ps128(sum2));
sum3 = _mm256_shuffle_ps(sum3, sum3, _MM_SHUFFLE(2, 0, 2, 0));
sum3 = _mm256_permute4x64_ps(sum3, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + ((i + 24) >> 1), _mm256_castps256_ps128(sum3));
}
for (int i = HEND32; i < HEND; i += 8)
{
float* lb0 = linebuff + i;
__m256 sum0 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb0++));
for (int k = 1; k < D; k++)
{
sum0 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb0++), sum0);
}
sum0 = _mm256_shuffle_ps(sum0, sum0, _MM_SHUFFLE(2, 0, 2, 0));
sum0 = _mm256_permute4x64_ps(sum0, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + (i >> 1), _mm256_castps256_ps128(sum0));
}
#ifdef MASKSTORE
//last
{
float* lb0 = linebuff + HEND;
__m256 sum0 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb0++));
for (int k = 1; k < D; k++)
{
sum0 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb0++), sum0);
}
sum0 = _mm256_shuffle_ps(sum0, sum0, _MM_SHUFFLE(2, 0, 2, 0));
sum0 = _mm256_permute4x64_ps(sum0, _MM_SHUFFLE(2, 0, 2, 0));
_mm_maskstore_ps(dptr + (HEND >> 1), maskhend, _mm256_castps256_ps128(sum0));
}
#else
for (int i = HEND; i < hend; i += 2)
{
float sum = GaussWeight[0] * linebuff[i];
for (int k = 1; k < D; k++)
{
sum += GaussWeight[k] * linebuff[i + k];
}
dptr[i >> 1] = sum;
}
#endif
dptr += dest.cols;
}
_mm_free(linebuff);
_mm_free(W);
}
template<bool is_use_table>
void LocalMultiScaleFilterInterpolation::remapGaussDownIgnoreBoundary(const Mat& src, Mat& remapIm, Mat& dest, const float g, const float sigma_range, const float boost)
{
CV_Assert(src.depth() == CV_32F);
const Size size = src.size();
dest.create(size / 2, CV_32F);
remapIm.create(size, CV_32F);
const int D = 2 * radius + 1;
const int rs = radius >> 1;
__m256* W = (__m256*)_mm_malloc(sizeof(__m256) * D, AVX_ALIGN);
for (int k = 0; k < D; k++)
{
W[k] = _mm256_set1_ps(GaussWeight[k]);
}
const int width = src.cols;
const int height = src.rows;
#pragma region remap top
const __m256 mg = _mm256_set1_ps(g);
const float coeff = float(1.0 / (-2.0 * sigma_range * sigma_range));
const __m256 mcoeff = _mm256_set1_ps(coeff);
const __m256 mdetail = _mm256_set1_ps(boost);
//splat
{
const float* sptr = src.ptr<float>();
float* d = remapIm.ptr<float>();
const int size = width * (D - 1);
const int REMAPSIZE32 = get_simd_floor(size, 32);
const int REMAPSIZE8 = get_simd_ceil(size, 8);
if constexpr (is_use_table)
{
//float* rt = &rangeTable[0];
float* rt = integerSampleTable;
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
}
else
{
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
}
}
#pragma endregion
const int linesize = src.cols;
float* linebuff = (float*)_mm_malloc(sizeof(float) * linesize, AVX_ALIGN);
//memset(linebuff, 0, sizeof(float) * linesize);
const float* sptr = remapIm.ptr<float>();
float* dptr = dest.ptr<float>(rs, rs);
const int hend = width - 2 * radius;
const int vend = height - 2 * radius;
const int WIDTH = get_simd_floor(width, 8);
const int HEND32 = get_simd_floor(hend, 32);
const int HEND = get_simd_floor(hend, 8);
const __m128i maskhend = get_storemask1(hend, 8);
for (int j = 0; j < vend; j += 2)
{
//remap line
{
const float* sptr = src.ptr<float>(j + D - 1);
float* d = remapIm.ptr<float>(j + D - 1);
const int size = 2 * width;
const int REMAPSIZE32 = get_simd_floor(size, 32);
const int REMAPSIZE8 = get_simd_ceil(size, 8);
if constexpr (is_use_table)
{
//float* rt = &rangeTable[0];
float* rt = integerSampleTable;
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_i32gather_ps(rt, _mm256_cvtps_epi32(_mm256_abs_ps(subsg)), sizeof(float)), ms));
}
}
else
{
for (int i = 0; i < REMAPSIZE32; i += 32)
{
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 8);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 8, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 16);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 16, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
ms = _mm256_loadu_ps(sptr + i + 24);
subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i + 24, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
for (int i = REMAPSIZE32; i < REMAPSIZE8; i += 8)
{
const __m256 ms = _mm256_loadu_ps(sptr + i);
const __m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
}
}
//v filter
for (int i = 0; i < WIDTH; i += 8)
{
const float* s = sptr + i;
__m256 sum = _mm256_mul_ps(W[0], _mm256_loadu_ps(s));
s += width;
for (int k = 1; k < D; k++)
{
sum = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(s), sum); s += width;
}
_mm256_storeu_ps(linebuff + i, sum);
}
for (int i = WIDTH; i < width; i++)
{
const float* s = sptr + i;
float sum = GaussWeight[0] * *s;
s += width;
for (int k = 1; k < D; k++)
{
sum += GaussWeight[k] * *s;
s += width;
}
linebuff[i] = sum;
}
sptr += 2 * width;
//h filter
for (int i = 0; i < HEND32; i += 32)
{
float* lb0 = linebuff + i;
float* lb1 = linebuff + i + 8;
float* lb2 = linebuff + i + 16;
float* lb3 = linebuff + i + 24;
__m256 sum0 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb0++));
__m256 sum1 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb1++));
__m256 sum2 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb2++));
__m256 sum3 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb3++));
for (int k = 1; k < D; k++)
{
sum0 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb0++), sum0);
sum1 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb1++), sum1);
sum2 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb2++), sum2);
sum3 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb3++), sum3);
}
sum0 = _mm256_shuffle_ps(sum0, sum0, _MM_SHUFFLE(2, 0, 2, 0));
sum0 = _mm256_permute4x64_ps(sum0, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + (i >> 1), _mm256_castps256_ps128(sum0));
sum1 = _mm256_shuffle_ps(sum1, sum1, _MM_SHUFFLE(2, 0, 2, 0));
sum1 = _mm256_permute4x64_ps(sum1, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + ((i + 8) >> 1), _mm256_castps256_ps128(sum1));
sum2 = _mm256_shuffle_ps(sum2, sum2, _MM_SHUFFLE(2, 0, 2, 0));
sum2 = _mm256_permute4x64_ps(sum2, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + ((i + 16) >> 1), _mm256_castps256_ps128(sum2));
sum3 = _mm256_shuffle_ps(sum3, sum3, _MM_SHUFFLE(2, 0, 2, 0));
sum3 = _mm256_permute4x64_ps(sum3, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + ((i + 24) >> 1), _mm256_castps256_ps128(sum3));
}
for (int i = HEND32; i < HEND; i += 8)
{
float* lb0 = linebuff + i;
__m256 sum0 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb0++));
for (int k = 1; k < D; k++)
{
sum0 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb0++), sum0);
}
sum0 = _mm256_shuffle_ps(sum0, sum0, _MM_SHUFFLE(2, 0, 2, 0));
sum0 = _mm256_permute4x64_ps(sum0, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + (i >> 1), _mm256_castps256_ps128(sum0));
}
#ifdef MASKSTORE
//last
{
float* lb0 = linebuff + HEND;
__m256 sum0 = _mm256_mul_ps(W[0], _mm256_loadu_ps(lb0++));
for (int k = 1; k < D; k++)
{
sum0 = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(lb0++), sum0);
}
sum0 = _mm256_shuffle_ps(sum0, sum0, _MM_SHUFFLE(2, 0, 2, 0));
sum0 = _mm256_permute4x64_ps(sum0, _MM_SHUFFLE(2, 0, 2, 0));
_mm_maskstore_ps(dptr + (HEND >> 1), maskhend, _mm256_castps256_ps128(sum0));
}
#else
for (int i = HEND; i < hend; i += 2)
{
float sum = GaussWeight[0] * linebuff[i];
for (int k = 1; k < D; k++)
{
sum += GaussWeight[k] * linebuff[i + k];
}
dptr[i >> 1] = sum;
}
#endif
dptr += dest.cols;
}
_mm_free(linebuff);
_mm_free(W);
}
void LocalMultiScaleFilterInterpolation::remapAdaptiveGaussDownIgnoreBoundary(const Mat& src, Mat& remapIm, Mat& dest, const float g, const Mat& sigma_range, const Mat& boost)
{
CV_Assert(src.depth() == CV_32F);
const Size size = src.size();
dest.create(size / 2, CV_32F);
remapIm.create(size, CV_32F);
const int D = 2 * radius + 1;
const int rs = radius >> 1;
__m256* W = (__m256*)_mm_malloc(sizeof(__m256) * D, AVX_ALIGN);
for (int k = 0; k < D; k++)
{
W[k] = _mm256_set1_ps(GaussWeight[k]);
}
const int width = src.cols;
const int height = src.rows;
#pragma region remap top
const __m256 mg = _mm256_set1_ps(g);
//splat
{
const float* sptr = src.ptr<float>();
const float* asmap = sigma_range.ptr<float>();
const float* abmap = boost.ptr<float>();
float* d = remapIm.ptr<float>();
const int SIZE = get_simd_ceil(width * (D - 1), 8);
for (int i = 0; i < SIZE; i += 8)
{
const __m256 msgma = _mm256_loadu_ps(asmap + i);
__m256 mcoeff = _mm256_rcpnr_ps(_mm256_mul_ps(_mm256_set1_ps(-2.f), _mm256_mul_ps(msgma, msgma)));
const __m256 mdetail = _mm256_loadu_ps(abmap + i);
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
}
#pragma endregion
const int linesize = src.cols;
float* linebuff = (float*)_mm_malloc(sizeof(float) * linesize, AVX_ALIGN);
memset(linebuff, 0, sizeof(float) * linesize);
const float* sptr = remapIm.ptr<float>();
float* dptr = dest.ptr<float>(rs, rs);
const int hend = width - 2 * radius;
const int vend = height - 2 * radius;
const int WIDTH = get_simd_floor(width, 8);
const int HEND = get_simd_floor(hend, 8);
for (int j = 0; j < vend; j += 2)
{
//remap line
{
const float* sptr = src.ptr<float>(j + D - 1);
const float* asmap = sigma_range.ptr<float>(j + D - 1);
const float* abmap = boost.ptr<float>(j + D - 1);
float* d = remapIm.ptr<float>(j + D - 1);
const int SIZE = get_simd_floor(width * 2, 8);
for (int i = 0; i < SIZE; i += 8)
{
const __m256 msgma = _mm256_loadu_ps(asmap + i);
const __m256 mcoeff = _mm256_rcpnr_ps(_mm256_mul_ps(_mm256_set1_ps(-2.f), _mm256_mul_ps(msgma, msgma)));
const __m256 mdetail = _mm256_loadu_ps(abmap + i);
__m256 ms = _mm256_loadu_ps(sptr + i);
__m256 subsg = _mm256_sub_ps(ms, mg);
_mm256_storeu_ps(d + i, _mm256_fmadd_ps(subsg, _mm256_mul_ps(mdetail, _mm256_exp_ps(_mm256_mul_ps(_mm256_mul_ps(subsg, subsg), mcoeff))), ms));
}
for (int i = SIZE; i < width * 2; i++)
{
const float sigma = asmap[i];
const float coeff = 1.f / (-2.f * sigma * sigma);
const float detail = abmap[i];
float s = sptr[i];
float subsg = s - g;
d[i] = subsg * (detail * exp(subsg * subsg * coeff)) + s;
}
}
//v filter
for (int i = 0; i < WIDTH; i += 8)
{
const float* s = sptr + i;
__m256 sum = _mm256_mul_ps(W[0], _mm256_loadu_ps(s));
s += width;
for (int k = 1; k < D; k++)
{
sum = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(s), sum); s += width;
}
_mm256_storeu_ps(linebuff + i, sum);
}
for (int i = WIDTH; i < width; i++)
{
const float* s = sptr + i;
float sum = GaussWeight[0] * *s;
s += width;
for (int k = 1; k < D; k++)
{
sum += GaussWeight[k] * *s;
s += width;
}
linebuff[i] = sum;
}
sptr += 2 * width;
//h filter
for (int i = 0; i < HEND; i += 8)
{
__m256 sum = _mm256_mul_ps(W[0], _mm256_loadu_ps(linebuff + i));
for (int k = 1; k < D; k++)
{
sum = _mm256_fmadd_ps(W[k], _mm256_loadu_ps(linebuff + i + k), sum);
}
sum = _mm256_shuffle_ps(sum, sum, _MM_SHUFFLE(2, 0, 2, 0));
sum = _mm256_permute4x64_ps(sum, _MM_SHUFFLE(2, 0, 2, 0));
_mm_storeu_ps(dptr + (i >> 1), _mm256_castps256_ps128(sum));
}
for (int i = HEND; i < hend; i += 2)
{
float sum = GaussWeight[0] * linebuff[i];
for (int k = 1; k < D; k++)
{
sum += GaussWeight[k] * linebuff[i + k];
}
dptr[i >> 1] = sum;
}
dptr += dest.cols;
}
_mm_free(linebuff);
_mm_free(W);
}
template<bool isInit, int interpolation, int D2>
void LocalMultiScaleFilterInterpolation::GaussUpSubProductSumIgnoreBoundary(const Mat& src, const cv::Mat& subsrc, const Mat& GaussianPyramid, Mat& dest, const float g)
{
CV_Assert(src.depth() == CV_32F);
dest.create(src.size() * 2, src.type());
__m256* GW = (__m256*)_mm_malloc(sizeof(__m256) * (2 * radius + 1), AVX_ALIGN);
for (int i = 0; i < 2 * radius + 1; i++)
{
GW[i] = _mm256_set1_ps(GaussWeight[i]);
}
const __m256 mevenoddratio = _mm256_setr_ps(evenratio, oddratio, evenratio, oddratio, evenratio, oddratio, evenratio, oddratio);
const __m256 mevenratio = _mm256_set1_ps(evenratio);
const __m256 moddratio = _mm256_set1_ps(oddratio);
const int rs = radius >> 1;
const int D = 2 * rs + 1;
//const int D2 = 2 * D;
const int step = src.cols;
float* linebuff = (float*)_mm_malloc(sizeof(float) * (src.cols * 2 + 8), AVX_ALIGN);
float* linee = linebuff;
float* lineo = linebuff + src.cols;
const int hend = src.cols - 2 * rs;
const int HEND8 = get_simd_floor(hend, 8);
const int WIDTH32 = get_simd_floor(src.cols, 32);
const int WIDTH8 = get_simd_floor(src.cols, 8);
const __m256i maskwidth = get_simd_residualmask_epi32(src.cols);
__m256i maskhendL, maskhendR;
get_storemask2(hend, maskhendL, maskhendR, 8);
const float delta = intensityRange / (order - 1);
const float idelta = 1.f / delta;
const __m256 mg = _mm256_set1_ps(g);
const __m256 mgmax = _mm256_set1_ps(intensityMax - delta);
const __m256 mgmin = _mm256_set1_ps(intensityMin + delta);
const __m256 midelta = _mm256_set1_ps(idelta);
const __m256 mcubicalpha = _mm256_set1_ps(cubicAlpha);
const __m256 mone = _mm256_set1_ps(1.f);
const __m256 mtwo = _mm256_set1_ps(2.f);
const __m256 mtwoalpha = _mm256_set1_ps(2.f + cubicAlpha);
const __m256 mnthreealpha = _mm256_set1_ps(-(3.f + cubicAlpha));
const __m256 mmfouralpha = _mm256_set1_ps(-4.f * cubicAlpha);
const __m256 mmfivealpha = _mm256_set1_ps(-5.f * cubicAlpha);
const __m256 meightalpha = _mm256_set1_ps(8.f * cubicAlpha);
for (int j = radius; j < dest.rows - radius; j += 2)
{
const float* sptr = src.ptr<float>((j - radius) >> 1);
//v filter
for (int i = 0; i < WIDTH32; i += 32)
{
const float* si = sptr + i;
__m256 sume0 = _mm256_mul_ps(GW[0], _mm256_loadu_ps(si));
__m256 sumo0 = _mm256_setzero_ps();
__m256 sume1 = _mm256_mul_ps(GW[0], _mm256_loadu_ps(si + 8));
__m256 sumo1 = _mm256_setzero_ps();
__m256 sume2 = _mm256_mul_ps(GW[0], _mm256_loadu_ps(si + 16));
__m256 sumo2 = _mm256_setzero_ps();
__m256 sume3 = _mm256_mul_ps(GW[0], _mm256_loadu_ps(si + 24));
__m256 sumo3 = _mm256_setzero_ps();
si += step;
for (int k = 2; k < D2; k += 2)
{
__m256 ms = _mm256_loadu_ps(si);
sume0 = _mm256_fmadd_ps(GW[k], ms, sume0);
sumo0 = _mm256_fmadd_ps(GW[k - 1], ms, sumo0);
ms = _mm256_loadu_ps(si + 8);
sume1 = _mm256_fmadd_ps(GW[k], ms, sume1);
sumo1 = _mm256_fmadd_ps(GW[k - 1], ms, sumo1);
ms = _mm256_loadu_ps(si + 16);
sume2 = _mm256_fmadd_ps(GW[k], ms, sume2);
sumo2 = _mm256_fmadd_ps(GW[k - 1], ms, sumo2);
ms = _mm256_loadu_ps(si + 24);
sume3 = _mm256_fmadd_ps(GW[k], ms, sume3);
sumo3 = _mm256_fmadd_ps(GW[k - 1], ms, sumo3);
si += step;
}
_mm256_storeu_ps(linee + i, _mm256_mul_ps(sume0, mevenratio));
_mm256_storeu_ps(linee + i + 8, _mm256_mul_ps(sume1, mevenratio));
_mm256_storeu_ps(linee + i + 16, _mm256_mul_ps(sume2, mevenratio));
_mm256_storeu_ps(linee + i + 24, _mm256_mul_ps(sume3, mevenratio));
_mm256_storeu_ps(lineo + i, _mm256_mul_ps(sumo0, moddratio));
_mm256_storeu_ps(lineo + i + 8, _mm256_mul_ps(sumo1, moddratio));
_mm256_storeu_ps(lineo + i + 16, _mm256_mul_ps(sumo2, moddratio));
_mm256_storeu_ps(lineo + i + 24, _mm256_mul_ps(sumo3, moddratio));
}
for (int i = WIDTH32; i < WIDTH8; i += 8)
{
const float* si = sptr + i;
__m256 sume = _mm256_mul_ps(GW[0], _mm256_loadu_ps(si)); si += step;
__m256 sumo = _mm256_setzero_ps();
for (int k = 2; k < D2; k += 2)
{
const __m256 ms = _mm256_loadu_ps(si); si += step;
sume = _mm256_fmadd_ps(GW[k], ms, sume);
sumo = _mm256_fmadd_ps(GW[k - 1], ms, sumo);
}
_mm256_storeu_ps(linee + i, _mm256_mul_ps(sume, mevenratio));
_mm256_storeu_ps(lineo + i, _mm256_mul_ps(sumo, moddratio));
}
#ifdef MASKSTORE
{
const float* si = sptr + WIDTH8;
__m256 sume = _mm256_mul_ps(GW[0], _mm256_loadu_ps(si)); si += step;
__m256 sumo = _mm256_setzero_ps();
for (int k = 2; k < D2; k += 2)
{
const __m256 ms = _mm256_loadu_ps(si); si += step;
sume = _mm256_fmadd_ps(GW[k], ms, sume);
sumo = _mm256_fmadd_ps(GW[k - 1], ms, sumo);
}
_mm256_maskstore_ps(linee + WIDTH8, maskwidth, _mm256_mul_ps(sume, mevenratio));
_mm256_maskstore_ps(lineo + WIDTH8, maskwidth, _mm256_mul_ps(sumo, moddratio));
}
#else
for (int i = WIDTH8; i < src.cols; i++)
{
const float* si = sptr + i;
float sume = GaussWeight[0] * *si; si += step;
float sumo = 0.f;
for (int k = 1; k < D; k++)
{
const int K = k << 1;
sume += GaussWeight[K] * *si;
sumo += GaussWeight[K - 1] * *si;
si += step;
}
linee[i] = sume * evenratio;
lineo[i] = sumo * oddratio;
}
#endif
// h filter
float* deptr = dest.ptr<float>(j, radius);
float* doptr = dest.ptr<float>(j + 1, radius);
const float* gpye = GaussianPyramid.ptr<float>(j, radius);
const float* gpyo = GaussianPyramid.ptr<float>(j + 1, radius);
const float* daeptr = subsrc.ptr<float>(j, radius);
const float* daoptr = subsrc.ptr<float>(j + 1, radius);
for (int i = 0; i < HEND8; i += 8)
{
float* sie = linee + i;
float* sio = lineo + i;
__m256 sumee = _mm256_mul_ps(GW[0], _mm256_loadu_ps(sie++));
__m256 sumoe = _mm256_setzero_ps();
__m256 sumeo = _mm256_mul_ps(GW[0], _mm256_loadu_ps(sio++));
__m256 sumoo = _mm256_setzero_ps();
for (int k = 2; k < D2; k += 2)
{
const __m256 msie = _mm256_loadu_ps(sie++);
sumee = _mm256_fmadd_ps(GW[k], msie, sumee);
sumoe = _mm256_fmadd_ps(GW[k - 1], msie, sumoe);
const __m256 msio = _mm256_loadu_ps(sio++);
sumeo = _mm256_fmadd_ps(GW[k], msio, sumeo);
sumoo = _mm256_fmadd_ps(GW[k - 1], msio, sumoo);
}
__m256 s1 = _mm256_unpacklo_ps(sumee, sumoe);
__m256 s2 = _mm256_unpackhi_ps(sumee, sumoe);
__m256 w;
if constexpr (interpolation == 0) w = _mm256_andnot_ps(_mm256_cmp_ps(_mm256_mul_ps(midelta, _mm256_abs_ps(_mm256_sub_ps(_mm256_loadu_ps(gpye + 2 * i + 0), mg))), _mm256_set1_ps(0.5f), 14), mone);
if constexpr (interpolation == 1) w = _mm256_max_ps(_mm256_setzero_ps(), _mm256_fnmadd_ps(midelta, _mm256_abs_ps(_mm256_sub_ps(_mm256_loadu_ps(gpye + 2 * i + 0), mg)), mone));
if constexpr (interpolation == 2)
{
const __m256 mgpy = _mm256_loadu_ps(gpye + 2 * i + 0);
const __m256 x = _mm256_mul_ps(midelta, _mm256_abs_ps(_mm256_sub_ps(mgpy, mg)));
const __m256 x2 = _mm256_mul_ps(x, x);
const __m256 x3 = _mm256_mul_ps(x2, x);
const __m256 m1 = _mm256_fmadd_ps(mtwoalpha, x3, _mm256_fmadd_ps(mnthreealpha, x2, mone));
const __m256 m2 = _mm256_fmadd_ps(mcubicalpha, x3, _mm256_fmadd_ps(mmfivealpha, x2, _mm256_fmadd_ps(meightalpha, x, mmfouralpha)));
w = _mm256_andnot_ps(_mm256_cmp_ps(x, mtwo, 14), m2);
w = _mm256_blendv_ps(m1, w, _mm256_cmp_ps(x, mone, 14));
__m256 wl = _mm256_max_ps(_mm256_setzero_ps(), _mm256_sub_ps(mone, x));
w = _mm256_blendv_ps(w, wl, _mm256_cmp_ps(mgpy, mgmin, 1));
w = _mm256_blendv_ps(w, wl, _mm256_cmp_ps(mgpy, mgmax, 14));
}
if constexpr (isInit) _mm256_storeu_ps(deptr + 2 * i + 0, _mm256_mul_ps(w, _mm256_fnmadd_ps(mevenoddratio, _mm256_permute2f128_ps(s1, s2, 0x20), _mm256_loadu_ps(daeptr + 2 * i + 0))));
else _mm256_storeu_ps(deptr + 2 * i + 0, _mm256_fmadd_ps(w, _mm256_fnmadd_ps(mevenoddratio, _mm256_permute2f128_ps(s1, s2, 0x20), _mm256_loadu_ps(daeptr + 2 * i + 0)), _mm256_loadu_ps(deptr + 2 * i + 0)));
if constexpr (interpolation == 0) w = _mm256_andnot_ps(_mm256_cmp_ps(_mm256_mul_ps(midelta, _mm256_abs_ps(_mm256_sub_ps(_mm256_loadu_ps(gpye + 2 * i + 8), mg))), _mm256_set1_ps(0.5f), 14), mone);
if constexpr (interpolation == 1) w = _mm256_max_ps(_mm256_setzero_ps(), _mm256_fnmadd_ps(midelta, _mm256_abs_ps(_mm256_sub_ps(_mm256_loadu_ps(gpye + 2 * i + 8), mg)), mone));
if constexpr (interpolation == 2)
{
const __m256 mgpy = _mm256_loadu_ps(gpye + 2 * i + 8);