forked from joelacarpenter/digHolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigHolo.cpp
More file actions
17943 lines (15514 loc) · 633 KB
/
digHolo.cpp
File metadata and controls
17943 lines (15514 loc) · 633 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
//Defining this will make calls to the BLAS library using the CBLAS interface, rather than the FORTRAN interface
//e.g. cblas_cgesvd rather than cgesvd
#define CBLAS_ENABLE
//Defining this will compile against the mkl_lapack.h and mkl_blas.h or mkl_cblas.h headers
//If not defined, it will compile against the lapack.h and blas.h or cblas.h headers (e.g. openBLAS)
#define MKL_ENABLE
//Defining this line will enable LAPACK/BLAS functions. The program won't work without them, but it can help debugging.
#define LAPACKBLAS_ENABLE
//Defining this line will enable the Intel SVML library, used for sincos and exp. If SVML is not available, a custom fast sincos, exp will be used instead.
//_MSC_VER>=1920 should support SVML, as should Intel Compiler
#if (_MSC_VER>=1920) || defined(__INTEL_COMPILER)
#define SVML_ENABLE
#endif
#ifdef _MSC_VER
//Microsoft compiler complains about functions like fopen
#define _CRT_SECURE_NO_WARNINGS
//Disable Arithmetic overflow errors (e.g. pointless warnings about adding two ints and then multiplying by a size_t)
#pragma warning (disable: 26451)
#endif
#include <cstdint>//required for int64_t
#include <cstring>
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <functional>
#include <condition_variable>
#include <atomic>
#include <vector>
#include <thread>//std::thread
#include <float.h>//FLT_MAX
#include <immintrin.h>//SIMD AVX intrisics
//When linking statically with FFTW3, you'll have to comment out the line #define FFTW_DLL
//If you're dynamically linking, you'll have to make sure #define FFTW_DLL
//Because Intel MKL also includes an FFTW compatible interface. Make sure you include the FFTW library _before_ MKL.
//e.g. libfftw3f-3.lib;mkl_rt.lib NOT mkl_rt.lib;libfftw3f-3.lib;
//Otherwise, when you link, you'll actually link with the MKL FFTs, not the FFTW FFTs. Which would be fine, except for the fact that real-to-real transforms are not implemented in MKL.
//Real-to-real transforms are used during the 'AutoAlign' routine.
#include <fftw3.h>
#ifdef LAPACKBLAS_ENABLE
#ifdef MKL_ENABLE
//Intel MKL library
//Console 'MKL Link link advisor' for assistance selecting the correct .libs
//https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/onemkl/link-line-advisor.html
//e.g. static linked : mkl_intel_lp64.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib
//or if you're having issues with libiomp5md.lib (openMP threads), link with the sequential version
//mkl_intel_lp64.lib;mkl_sequential.lib;mkl_core.lib
//e.g. dynamic linked : mkl_rt.lib
//If using Intel MKL...
//Don't forget C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\redist\intel64_win\compiler\libiomp5md.dll
//C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\redist\intel64_win\compiler\libiomp5md.dll
//Most of your dlls will be in folder C:\Program Files (x86)\Intel\oneAPI\mkl\latest\redist\intel64\..., you'll need to copy those into the same folder as your executable.
#include <mkl_lapack.h>//cgesvd, sgels
#ifdef CBLAS_ENABLE
#include <mkl_cblas.h> //cgemv, cgemm
#else //Fortran interface
#include <mkl_blas.h> //cgemv, cgemm
#endif
//Define the complex type so functions don't complain that complex isn't what they expect, even if it's bitwise compatible with std::complex<float>
#define BLAS_COMPLEXTYPE MKL_Complex8
#else
//openBLAS (https://www.openblas.net/)
//Link against libopenblas.lib (libopenblas.dll)
#include <lapack.h>
#ifdef CBLAS_ENABLE
#include <cblas.h> //cgemv, cgemm
#else //Fortran interface
#include <blas.h> //cgemv, cgemm
#endif
#define BLAS_COMPLEXTYPE _Complex float
#define cgesvd cgesvd_
#define sgels sgels_
#endif
#endif
//Header file containing some routine for allocated memory aligned (for AVX) multi-dimensional arrays.
//#include "memoryAllocation.h"
//Header file for the functions and defines used by this code
#include "digHolo.h"
#define CEILINT(a,b) ((a+b-1)/b)
const float pi = (float)3.14159265358979323846264338327950288;
//File or stdout used for printing to console
FILE* consoleOut = stdout;
//Pixel dimensions must be a multiple of this for simplicity of SIMD processing (16xint16 = AVX2)
#define DIGHOLO_PIXEL_QUANTA 16
//The maximum number of polarisations
#define DIGHOLO_POLCOUNTMAX 2
//The maximum number of axes (x and y)
#define DIGHOLO_AXISCOUNTMAX 2
//The maximum number of planes (Image plane, Fourier plane)
#define DIGHOLO_PLANECOUNTMAX 2
//Extra 2 planes used in WoI variables for the frame calibration.
#define DIGHOLO_PLANECOUNTCAL 2
//A default wavelength to use to initialise variables or when the user doesn't specify a wavelength
#define DIGHOLO_WAVELENGTH_DEFAULT 1565e-9
//[sqrt(2) / (3 + sqrt(2))]. The fraction of the maximum spatial frequency usable as a Fourier window radius without wrapping
#define DIGHOLO_WMAXRATIO 0.32037724101704079249230971981887705624103546142578125
//MEMORY ALLOCATION ROUTINES
//The memory boundary alignment. For AVX, it should be a multiple of 32. I've chosen 64 (x86 cache boundary)
#define ALIGN 64
//There can be some compiler dependent issues using aligned allocation routines.
//_mm_malloc has been around a long time and is well supported. Alternatives are _aligned_malloc, posix_memalign or aligned_alloc (C11 or C++17)
#define alignedAllocate _mm_malloc
#define alignedFree _mm_free
template<typename T>
void free1D(T*& a)
{
if (a)
{
alignedFree(a);
}
a = 0;
}
template<typename T>
void free2D(T**& a)
{
if (a)
{
//Free the data itself
alignedFree(&a[0][0]);
//Free the array of pointers
alignedFree(a);
}
a = 0;
}
template<typename T>
void free3D(T***& arr3D)
{
if (arr3D)
{
size_t i = 0;
//Free the array data itself
alignedFree(&arr3D[0][0][0]);
while (arr3D[i] != 0)
{
//Free the array of pointers to pointers
alignedFree(arr3D[i]);
i++;
}
//Free the array of pointers
alignedFree(arr3D);
}
arr3D = 0;
}
template<typename T>
void free4D(T****& arr4D)
{
if (arr4D)
{
//Free the array data
alignedFree(&arr4D[0][0][0][0]);
size_t i = 0;
while (arr4D[i] != 0)
{
size_t j = 0;
while (arr4D[i][j] != 0)
{
//Free the array of pointers to pointers to pointers
alignedFree(arr4D[i][j]);
j++;
}
//Free the array of pointers to pointers
alignedFree(arr4D[i]);
i++;
}
alignedFree(arr4D);
}
arr4D = 0;
}
template<typename T>
int allocate1D(size_t n, T*& a)
{
if (n > 0)
{
if (a)
{
free1D<T>(a);
}
a = (T*)alignedAllocate(sizeof(T) * n, ALIGN);// _aligned_malloc(sizeof(T) * n, ALIGN);
if (!a)
{
std::cout << "Allocate1D failed " << n << " (" << (n * sizeof(T) / 1073741824.0) << " GB)" << "\n\r";
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
return DIGHOLO_ERROR_SUCCESS;
}
else
{
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
template<typename T>
int allocate2D(size_t nx, size_t ny, T**& a)
{
if (nx > 0 && ny > 0)
{
if (a)
{
free2D<T>(a);
}
a = (T**)alignedAllocate(sizeof(T*) * (nx + 1), ALIGN);
T* a1D = (T*)alignedAllocate(sizeof(T) * nx * ny, ALIGN);
if (a && a1D)
{
/* now allocate the actual rows */
for (size_t i = 0; i < nx; i++)
{
a[i] = &a1D[i * ny];
}
a[nx] = 0;
return DIGHOLO_ERROR_SUCCESS;
}
else
{
std::cout << "Allocate2D failed " << nx << "x" << ny << " (" << (nx * ny * sizeof(T) / 1073741824.0) << " GB)" << "\n\r";
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
else
{
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
template<typename T>
size_t allocate2DSparseBlock(size_t groupCount, T**& a)
{
size_t totalElements = 0;
if (groupCount > 0)
{
if (a)
{
free2D<T>(a);
}
for (size_t i = 1; i <= groupCount; i++)
{
totalElements += (i * i);
}
a = (T**)alignedAllocate(sizeof(T*) * (groupCount + 1), ALIGN);
T* a1D = (T*)alignedAllocate(sizeof(T) * totalElements, ALIGN);
if (a && a1D)
{
size_t idx = 0;
/* now allocate the actual rows */
for (size_t i = 0; i < groupCount; i++)
{
a[i] = &a1D[idx];
idx += ((i + 1) * (i + 1));
}
a[groupCount] = 0;
}
else
{
std::cout << "Allocate2DSparse failed " << groupCount << " (" << (totalElements * sizeof(T) / 1073741824.0) << " GB)" << "\n\r";
totalElements = 0;
}
}
return totalElements;
}
template<typename T>
int allocate3D(size_t nx, size_t ny, size_t nz, T***& a)
{
if (nx > 0 && ny > 0 && nz > 0)
{
if (a)
{
free3D<T>(a);
}
a = (T***)alignedAllocate(sizeof(T**) * (nx + 1), ALIGN); /* one extra for sentinel */
T* a1D = (T*)alignedAllocate(sizeof(T) * (nx * ny * nz), ALIGN);
if (a && a1D)
{
for (size_t i = 0; i < nx; i++)
{
a[i] = (T**)alignedAllocate(sizeof(T*) * (ny + 1), ALIGN);
for (size_t j = 0; j < ny; j++)
{
//a[i][j] = (T*)_aligned_malloc(sizeof(T)*nz, ALIGN);
a[i][j] = &a1D[i * ny * nz + j * nz];
}
a[i][ny] = 0;
}
a[nx] = 0;
return DIGHOLO_ERROR_SUCCESS;
}
else
{
std::cout << "Allocate3D failed " << nx << "x" << ny << "x" << nz << " (" << (nx * ny * nz * sizeof(T) / 1073741824.0) << " GB)" << "\n\r";
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
else
{
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
template<typename T>
int allocate4D(size_t nx, size_t ny, size_t nz, size_t nw, T****& a)
{
if (nx > 0 && ny > 0 && nz > 0 && nw > 0)
{
if (a)
{
free4D<T>(a);
}
a = (T****)alignedAllocate(sizeof(T***) * (nx + 1), ALIGN); // one extra for sentinel
T* a1D = (T*)alignedAllocate(sizeof(T) * (nx * ny * nz * nw), ALIGN);
if (a && a1D)
{
// now allocate the actual rows
for (size_t i = 0; i < nx; i++)
{
a[i] = (T***)alignedAllocate(sizeof(T**) * (ny + 1), ALIGN);
for (size_t j = 0; j < ny; j++)
{
a[i][j] = (T**)alignedAllocate(sizeof(T*) * (nz + 1), ALIGN);
for (size_t k = 0; k < nz; k++)
{
a[i][j][k] = &a1D[i * ny * nz * nw + j * nz * nw + k * nw];
}
a[i][j][nz] = 0;
}
a[i][ny] = 0;
}
a[nx] = 0;
return DIGHOLO_ERROR_SUCCESS;
}
else
{
std::cout << "Allocate4D failed " << nx << "x" << ny << "x" << nz << "x" << nw << " (" << (nx * ny * nz * nw * sizeof(T) / 1073741824.0) << " GB)" << "\n\r";
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
else
{
return DIGHOLO_ERROR_MEMORYALLOCATION;
}
}
//END MEMORY ALLOCATION ROUTINES
/*
struct BmpHeader {
char bitmapSignatureBytes[2] = { 'B', 'M' };
uint32_t sizeOfBitmapFile = 54 + 786432; // total size of bitmap file
uint32_t reservedBytes = 0;
uint32_t pixelDataOffset = 54;
} bmpHeader;
struct BmpInfoHeader {
uint32_t sizeOfThisHeader = 40;
int32_t width = 512; // in pixels
int32_t height = 512; // in pixels
uint16_t numberOfColorPlanes = 1; // must be 1
uint16_t colorDepth = 24;
uint32_t compressionMethod = 0;
uint32_t rawBitmapDataSize = 0; // generally ignored
int32_t horizontalResolution = 3780; // in pixel per meter
int32_t verticalResolution = 3780; // in pixel per meter
uint32_t colorTableEntries = 0;
uint32_t importantColors = 0;
} bmpInfoHeader;
struct Pixel {
uint8_t blue = 255;
uint8_t green = 255;
uint8_t red = 0;
} pixel;
*/
//https://dev.to/muiz6/c-how-to-write-a-bitmap-image-from-scratch-1k6m
int writeToBitmap(unsigned char* pixelArrayRGB, int width, int height, const char *filename)
{
const int widthPad = (int)(4.0 * ceil(width*3.0 / 4.0));
//BmpHeader
char bitmapSignatureBytes[2] = { 'B', 'M' };
uint32_t sizeOfBitmapFile = 54 + 786432; // total size of bitmap file
uint32_t reservedBytes = 0;
uint32_t pixelDataOffset = 54;
//BmpInfoHeader
uint32_t sizeOfThisHeader = 40;
int32_t h = height; // in pixels
int32_t w = width; // in pixels
uint16_t numberOfColorPlanes = 1; // must be 1
uint16_t colorDepth = 24;
uint32_t compressionMethod = 0;
uint32_t rawBitmapDataSize = 0; // generally ignored
int32_t horizontalResolution = 3780; // in pixel per meter
int32_t verticalResolution = 3780; // in pixel per meter
uint32_t colorTableEntries = 0;
uint32_t importantColors = 0;
std::ofstream fout(filename, std::ios::binary);
fout.write((char*)&bitmapSignatureBytes, 2);
fout.write((char*)&sizeOfBitmapFile, sizeof(uint32_t));
fout.write((char*)&reservedBytes, sizeof(uint32_t));
fout.write((char*)&pixelDataOffset, sizeof(uint32_t));
fout.write((char*)&sizeOfThisHeader, sizeof(uint32_t));
fout.write((char*)&w, sizeof(int32_t));
fout.write((char*)&h, sizeof(int32_t));
fout.write((char*)&numberOfColorPlanes, sizeof(uint16_t));
fout.write((char*)&colorDepth, sizeof(uint16_t));
fout.write((char*)&compressionMethod, sizeof(uint32_t));
fout.write((char*)&rawBitmapDataSize, sizeof(uint32_t));
fout.write((char*)&horizontalResolution, sizeof(int32_t));
fout.write((char*)&verticalResolution, sizeof(int32_t));
fout.write((char*)&colorTableEntries, sizeof(uint32_t));
fout.write((char*)&importantColors, sizeof(uint32_t));
//Memory rearrangement (flip RGB to BGR and enforce 4 byte memory alignment on width dimension, 'rows')
const size_t totalSize = ((size_t)widthPad) * ((size_t)height);
unsigned char* tempBuffer = 0;
allocate1D(totalSize, tempBuffer);
for (size_t yIdx = 0; yIdx < height; yIdx++)
{
for (size_t xIdx = 0; xIdx < width; xIdx++)
{
size_t idxIn = (yIdx * width + xIdx)*3;
size_t idxOut = (yIdx * widthPad + xIdx*3);
tempBuffer[idxOut + 2] = pixelArrayRGB[idxIn + 0];
tempBuffer[idxOut + 1] = pixelArrayRGB[idxIn + 1];
tempBuffer[idxOut + 0] = pixelArrayRGB[idxIn + 2];
}
}
fout.write((char*)tempBuffer, sizeof(unsigned char)*totalSize);
fout.close();
return 1;
}
#ifdef _WIN32
#include <intrin.h>
#else
#include <cpuid.h>
#endif
//Struct for reading info about what features the CPU supports
struct cpuINFO
{
char brand[0x40];
char brandHex[0x81];
int avx = 0;
int avx2 = 0;
int fma3 = 0;
int avx512f = 0;
};
//Get info about the CPU such as name, brand and instruction sets supported (e.g. avx, avx2, avx512)
int cpuInfoGet(cpuINFO* c)
{
for (int i = 0; i < 0x40; i++)
{
c[0].brand[i] = 0;
}
for (int i = 0; i < 0x81; i++)
{
c[0].brandHex[i] = 0;
}
int a = 0x80000000;
int fma3 = 0;
int avx = 0;
int avx2 = 0;
int avx512f = 0;
//Unforunately this part is platform dependent. Calls the cpuID instruction to find out what is supported.
//https://en.wikipedia.org/wiki/CPUID
#ifdef _WIN32
int cpuInfo[4];
__cpuid((int*)&cpuInfo[0], a);
if (cpuInfo[0] >= 0x80000004)
{
__cpuid((int*)&c[0].brand[0], 0x80000002);
__cpuid((int*)&c[0].brand[16], 0x80000003);
__cpuid((int*)&c[0].brand[32], 0x80000004);
}
a = 1;
__cpuidex((int*)&cpuInfo[0], a, 0);
fma3 = cpuInfo[2] >> 12 & 1;
avx = cpuInfo[2] >> 28 & 1;
a = 7;
__cpuidex((int*)&cpuInfo[0], a, 0);
avx2 = cpuInfo[1] >> 5 & 1;
avx512f = cpuInfo[1] >> 16 & 1;
#else
unsigned int cpuInfo[4];
__get_cpuid(a, &cpuInfo[0], &cpuInfo[1], &cpuInfo[2], &cpuInfo[3]);
if (cpuInfo[0] >= 0x80000004)
{
//cpuidGet(0x80000002,((cpuidType)&brand[0]));
__get_cpuid(0x80000002, (unsigned int*)&c[0].brand[0], (unsigned int*)&c[0].brand[4], (unsigned int*)&c[0].brand[8], (unsigned int*)&c[0].brand[12]);
__get_cpuid(0x80000003, (unsigned int*)&c[0].brand[16], (unsigned int*)&c[0].brand[20], (unsigned int*)&c[0].brand[24], (unsigned int*)&c[0].brand[28]);
__get_cpuid(0x80000004, (unsigned int*)&c[0].brand[32], (unsigned int*)&c[0].brand[36], (unsigned int*)&c[0].brand[40], (unsigned int*)&c[0].brand[44]);
}
a = 1;
//__get_cpuid(a, &cpuInfo[0], &cpuInfo[1], &cpuInfo[2], &cpuInfo[3]);
__cpuid_count(a, 0, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]);
fma3 = cpuInfo[2] >> 12 & 1;
avx = cpuInfo[2] >> 28 & 1;
a = 7;
//__get_cpuid(a, &cpuInfo[0], &cpuInfo[1], &cpuInfo[2], &cpuInfo[3]);
__cpuid_count(a, 0, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]);
avx2 = cpuInfo[1] >> 5 & 1;
avx512f = cpuInfo[1] >> 16 & 1;
#endif
//The brand of the CPU as hexadecimal (rather than ascii). This hex string version will be used as a 'unique' identifier for things like writing the FFTW wisdom to file.
for (int i = 0; i < 0x40; i++)
{
sprintf(&c[0].brandHex[2 * i], "%02x", c[0].brand[i]);
}
c[0].avx = avx;
c[0].fma3 = fma3;
c[0].avx2 = avx2;
c[0].avx512f = avx512f;
return 1;
}
//If SVML isn't available, implement vectorised functions like exp, sincos approximations.
//Otherwise we'll just use SVML
#ifndef SVML_ENABLE
//https://stackoverflow.com/questions/48863719/fastest-implementation-of-exponential-function-using-avx
__m256 exp_ps(__m256 x)
{
const __m256 exp_hi = _mm256_set1_ps(88.3762626647949f);
const __m256 exp_lo = _mm256_set1_ps(-88.3762626647949f);
const __m256 cephes_LOG2EF = _mm256_set1_ps(1.44269504088896341f);
//const __m256 inv_LOG2EF = _mm256_set1_ps(0.693147180559945f);
const __m256 cephes_exp_C1 = _mm256_set1_ps(0.693359375f);
const __m256 cephes_exp_C2 = _mm256_set1_ps(-2.12194440e-4f);
const __m256 cephes_exp_p0 = _mm256_set1_ps(1.9875691500E-4f);
const __m256 cephes_exp_p1 = _mm256_set1_ps(1.3981999507E-3f);
const __m256 cephes_exp_p2 = _mm256_set1_ps(8.3334519073E-3f);
const __m256 cephes_exp_p3 = _mm256_set1_ps(4.1665795894E-2f);
const __m256 cephes_exp_p4 = _mm256_set1_ps(1.6666665459E-1f);
const __m256 cephes_exp_p5 = _mm256_set1_ps(5.0000001201E-1f);
__m256 tmp = _mm256_setzero_ps(), fx;
__m256i imm0;
__m256 one = _mm256_set1_ps(1.0f);
x = _mm256_min_ps(x, exp_hi);
x = _mm256_max_ps(x, exp_lo);
/* express exp(x) as exp(g + n*log(2)) */
//Relative error 2.53575e-7 @ -0.346707
fx = _mm256_mul_ps(x, cephes_LOG2EF);
fx = _mm256_add_ps(fx, _mm256_set1_ps(0.5f));
tmp = _mm256_floor_ps(fx);
__m256 mask = _mm256_cmp_ps(tmp, fx, _CMP_GT_OS);
mask = _mm256_and_ps(mask, one);
fx = _mm256_sub_ps(tmp, mask);
tmp = _mm256_mul_ps(fx, cephes_exp_C1);
__m256 z = _mm256_mul_ps(fx, cephes_exp_C2);
x = _mm256_sub_ps(x, tmp);
x = _mm256_sub_ps(x, z);
z = _mm256_mul_ps(x, x);
//Relative erro 4.23278e-6 @ 0.346556
/* express exp(x) as exp(g + n*log(2)) */
/* fx = _mm256_mul_ps(x, cephes_LOG2EF);
fx = _mm256_round_ps(fx, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256 z = _mm256_mul_ps(fx, inv_LOG2EF);
x = _mm256_sub_ps(x, z);
z = _mm256_mul_ps(x, x);*/
__m256 y = _mm256_fmadd_ps(cephes_exp_p0, x, cephes_exp_p1);
y = _mm256_fmadd_ps(y, x, cephes_exp_p2);
y = _mm256_fmadd_ps(y, x, cephes_exp_p3);
y = _mm256_fmadd_ps(y, x, cephes_exp_p4);
y = _mm256_fmadd_ps(y, x, cephes_exp_p5);
y = _mm256_fmadd_ps(y, z, x);
y = _mm256_add_ps(y, one);
/* build 2^n */
imm0 = _mm256_cvttps_epi32(fx);
imm0 = _mm256_add_epi32(imm0, _mm256_set1_epi32(0x7f));
imm0 = _mm256_slli_epi32(imm0, 23);
__m256 pow2n = _mm256_castsi256_ps(imm0);
y = _mm256_mul_ps(y, pow2n);
return y;
}
//This version is basically identical to the float32 version exp_ps, not a true float64 implementation
__m256d exp_pd(__m256d x)
{
const __m256d exp_hi = _mm256_set1_pd(88.3762626647949f);
const __m256d exp_lo = _mm256_set1_pd(-88.3762626647949f);
const __m256d cephes_LOG2EF = _mm256_set1_pd(1.442695040888963387004650940070860087871551513671875);
// const __m256d inv_LOG2EF = _mm256_set1_pd(0.693147180559945f);
const __m256d cephes_exp_C1 = _mm256_set1_pd(0.693359375);
const __m256d cephes_exp_C2 = _mm256_set1_pd(-2.121944400546905827679E-4);
const __m256d cephes_exp_p0 = _mm256_set1_pd(1.9875691500E-4);
const __m256d cephes_exp_p1 = _mm256_set1_pd(1.3981999507E-3);
const __m256d cephes_exp_p2 = _mm256_set1_pd(8.3334519073E-3);
const __m256d cephes_exp_p3 = _mm256_set1_pd(4.1665795894E-2);
const __m256d cephes_exp_p4 = _mm256_set1_pd(1.6666665459E-1);
const __m256d cephes_exp_p5 = _mm256_set1_pd(5.0000001201E-1);
__m256d tmp = _mm256_setzero_pd(), fx;
__m128i imm0;
__m256d one = _mm256_set1_pd(1.0f);
x = _mm256_min_pd(x, exp_hi);
x = _mm256_max_pd(x, exp_lo);
/* express exp(x) as exp(g + n*log(2)) */
//Relative error 2.53575e-7 @ -0.346707
fx = _mm256_mul_pd(x, cephes_LOG2EF);
fx = _mm256_add_pd(fx, _mm256_set1_pd(0.5));
tmp = _mm256_floor_pd(fx);
__m256d mask = _mm256_cmp_pd(tmp, fx, _CMP_GT_OS);
mask = _mm256_and_pd(mask, one);
fx = _mm256_sub_pd(tmp, mask);
tmp = _mm256_mul_pd(fx, cephes_exp_C1);
__m256d z = _mm256_mul_pd(fx, cephes_exp_C2);
x = _mm256_sub_pd(x, tmp);
x = _mm256_sub_pd(x, z);
z = _mm256_mul_pd(x, x);
//Relative erro 4.23278e-6 @ 0.346556
/* express exp(x) as exp(g + n*log(2)) */
/*fx = _mm256_mul_pd(x, cephes_LOG2EF);
fx = _mm256_round_pd(fx, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256d z = _mm256_mul_pd(fx, inv_LOG2EF);
x = _mm256_sub_pd(x, z);
z = _mm256_mul_pd(x, x);*/
__m256d y = _mm256_fmadd_pd(cephes_exp_p0, x, cephes_exp_p1);
y = _mm256_fmadd_pd(y, x, cephes_exp_p2);
y = _mm256_fmadd_pd(y, x, cephes_exp_p3);
y = _mm256_fmadd_pd(y, x, cephes_exp_p4);
y = _mm256_fmadd_pd(y, x, cephes_exp_p5);
y = _mm256_fmadd_pd(y, z, x);
y = _mm256_add_pd(y, one);
/* build 2^n */
//This bit is a bit dodgy, converting to float32, but there's not pd_epi64 conversion in AVX2, it's an AVX-512 command
imm0 = _mm256_cvtpd_epi32(fx);// _mm_cvtps_epi32(_mm256_cvtpd_ps(fx));
imm0 = _mm_add_epi32(imm0, _mm_set1_epi32(0x7f));
imm0 = _mm_slli_epi32(imm0, 23);
__m128 pow2n = _mm_castsi128_ps(imm0);
y = _mm256_mul_pd(y, _mm256_cvtps_pd(pow2n));
return y;
}
//This sincos function will fail for large angles, e.g. >~1 million pi
//To improve performance, a better range reduction of x0 onto the period +/- pi/4 would be necessary
//gcc alternative? https://stackoverflow.com/questions/40475140/mathematical-functions-for-simd-registers _ZGVdN8vvv_sincosf
__m256 sincos_ps(__m256* c, __m256 x0)
{
//I don't know where these constants come from. Double-precision uses slightly different values
//Payne Hanek?
//Cephes library code above
//https ://github-wiki-see.page/m/david-c14/SubmarineFree/wiki/Examining-the-Fast-Sine-Approximations
//Dekker arithmetic ?
//Agner Fog's vector library was also an inspiration for this routine, it also uses these same constants.
//https://github.com/vectorclass/version2/blob/master/vectormath_trig.h
const __m256 DP1F = _mm256_set1_ps(-0.78515625f * 2.0f);
const __m256 DP2F = _mm256_set1_ps(-2.4187564849853515625E-4f * 2.0f);
const __m256 DP3F = _mm256_set1_ps(-3.77489497744594108E-8f * 2.0f);
const __m256 twoOnPi = _mm256_set1_ps(0.63661977236758138243288840385503135621547698974609375f);//2/pi
//Few constant values
const __m256 one256 = _mm256_set1_ps(1.0);
const __m256i one = _mm256_set1_epi32(1);
const __m256 negOne = _mm256_set1_ps(-1);
const __m256 zero = _mm256_set1_ps(0);
//Polynomial terms used for Taylor series expansion of sine and cos.
//Adding more terms does nothing for float32.
__m256 Psinf[4];
__m256 Pcosf[4];
Psinf[0] = _mm256_set1_ps(-0.1666666666666666574148081281236954964697360992431640625f);//1/factorial(3)
Psinf[1] = _mm256_set1_ps(8.33333333333333321768510160154619370587170124053955078125e-03f);//1/factorial(5)
Psinf[2] = _mm256_set1_ps(-1.984126984126984125263171154784913596813566982746124267578125e-04f);//1/factorial(7)
Psinf[3] = _mm256_set1_ps(2.755731922398589251095059327045788677423843182623386383056640625e-06f);//1/factorial(9)
Pcosf[0] = _mm256_set1_ps(-0.5f);//1/factorial(2)
Pcosf[1] = _mm256_set1_ps(4.1666666666666664353702032030923874117434024810791015625e-02f);//1\factorial(4)
Pcosf[2] = _mm256_set1_ps(-1.38888888888888894189432843262466121814213693141937255859375e-03f);//1\factorial(6)
Pcosf[3] = _mm256_set1_ps(2.48015873015873015657896394348114199601695872843265533447265625e-05f);//1\factorial(8)
//Anding with this will remove sign bit of a float
const unsigned int signMaskInt = 0x7FFFFFFF;
const float* signMaskF = (float*)&signMaskInt;
const __m256 signMask = _mm256_set1_ps(signMaskF[0]);
//absolute value of x0 (sign bit masked out)
__m256 xa = _mm256_and_ps(x0, signMask);
//Find the quadrant
__m256 y = _mm256_round_ps(_mm256_mul_ps(xa, twoOnPi), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256i q = _mm256_cvtps_epi32(y);
//This could be worth reading to improve accuracy
/// "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit"
// K. C. Ng et al, March 24, 1992
// https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf
//Wraps everything between + / -pi / 4
// Range reduction
// x = ((xa - y * DP1F) - y * DP2F) - y * DP3F;
__m256 x = _mm256_fmadd_ps(y, DP1F, xa);
x = _mm256_fmadd_ps(y, DP2F, x);
x = _mm256_fmadd_ps(y, DP3F, x);
__m256 x2 = _mm256_mul_ps(x, x);
__m256 x3 = _mm256_mul_ps(x2, x);
//Horner method style polynomial evaluation
__m256 sinV = _mm256_mul_ps(Psinf[3], x2);
sinV = _mm256_fmadd_ps(sinV, x2, Psinf[2]);
sinV = _mm256_fmadd_ps(sinV, x2, Psinf[1]);
sinV = _mm256_fmadd_ps(sinV, x2, Psinf[0]);
sinV = _mm256_fmadd_ps(sinV, x3, x);
__m256 cosV = _mm256_mul_ps(Pcosf[3], x2);
cosV = _mm256_fmadd_ps(cosV, x, Pcosf[2]);
cosV = _mm256_fmadd_ps(cosV, x2, Pcosf[1]);
cosV = _mm256_fmadd_ps(cosV, x2, Pcosf[0]);
cosV = _mm256_fmadd_ps(cosV, x2, one256);
//Now a whole bunch of swapping between sine and cos, plus some sign flips will be employed to construct full sine and cos functions
//From the interval +/-pi/4.
//These variables are used as the conditional for 3 different 'if' statements, that will swap sin/cos and sign bits based on which quadrant we're in.
const __m256 qShift2 = _mm256_castsi256_ps(_mm256_cmpeq_epi32(one, _mm256_and_si256(q, one)));
const __m256 qShift3 = _mm256_castsi256_ps(_mm256_cmpeq_epi32(one, _mm256_and_si256(_mm256_srli_epi32(q, 1), one)));
const __m256 qShift4 = _mm256_castsi256_ps(_mm256_cmpeq_epi32(one, _mm256_and_si256(_mm256_srli_epi32(_mm256_add_epi32(q, one), 1), one)));
//Flip sin and cos values if necessary
__m256 sinV_ = _mm256_or_ps(_mm256_and_ps(qShift2, cosV), _mm256_andnot_ps(qShift2, sinV));
cosV = _mm256_or_ps(_mm256_and_ps(qShift2, sinV), _mm256_andnot_ps(qShift2, cosV));
sinV = sinV_;
__m256 negSin = _mm256_mul_ps(sinV, negOne);
__m256 ltZero = _mm256_cmp_ps(x0, zero, _CMP_LT_OS);
//Flip the sign of sine if x<0
sinV = _mm256_or_ps(_mm256_andnot_ps(ltZero, sinV), _mm256_and_ps(ltZero, negSin));
negSin = _mm256_mul_ps(sinV, negOne);
//Select +sine or -sine depending on the quadrant
sinV = _mm256_or_ps(_mm256_andnot_ps(qShift3, sinV), _mm256_and_ps(qShift3, negSin));
//Select +cos or -cos depending on the quadrant
__m256 negCos = _mm256_mul_ps(cosV, negOne);
cosV = _mm256_or_ps(_mm256_andnot_ps(qShift4, cosV), _mm256_and_ps(qShift4, negCos));
c[0] = cosV;
return sinV;
}
//Inverse sinc function, Taylor expansion of x*csc(x)
__m256 xcsc_ps(__m256 x0)
{
const float B[] = { 1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e+00,
1.6666666666666665741480812812369549646973609924316406250000000000000000000000000000000000000000000000e-01,
1.9444444444444444752839729062543483451008796691894531250000000000000000000000000000000000000000000000e-02,
2.0502645502645500640015452376019311486743390560150146484375000000000000000000000000000000000000000000e-03,
2.0998677248677249788412491060540787657373584806919097900390625000000000000000000000000000000000000000e-04,
2.1336045641601196259842104785420247026195283979177474975585937500000000000000000000000000000000000000e-05,
2.1633474427786596446595009102242812559779849834740161895751953125000000000000000000000000000000000000e-06,
2.1923271344567642348143804952032009936147005646489560604095458984375000000000000000000000000000000000e-07,
2.2213930853920414129458539066221123281508198488154448568820953369140625000000000000000000000000000000e-08,
2.2507674795567867192556934851982095024958141493698349222540855407714843750000000000000000000000000000e-09,
2.2805107707218210504466873082498766175940652090048388345167040824890136718750000000000000000000000000e-10,
2.3106421580996967056883462652674466643321071757100071408785879611968994140625000000000000000000000000e-11,
2.3411704028931946895665814109782041111881834005714608792914077639579772949218750000000000000000000000e-12,
2.3721016693292249581388281702987705818223493348106956091214669868350028991699218750000000000000000000e-13,
2.4034415154237361473283816007884783312538489255527629495645669521763920783996582031250000000000000000e-14,
2.4351953983824322819707983818800718319934437306600871764317162160295993089675903320312500000000000000e-15,
2.4673688033682493186494982782386660623426708426574049948243327889940701425075531005859375000000000000e-16,
2.4999672768310467871805584707818666930635055628681304162874710073083406314253807067871093750000000000e-17,
2.5329964356669153147321962640172335832500557852866272801062308417385793291032314300537109375000000000e-18,
2.5664619702639559155055581896210584700410144846440300362561126590321691764984279870986938476562500000e-19,
2.6003696460089974176554143570182781389488296763507902255394693691314955685811582952737808227539062500e-20,
2.6347253044141823749536611738059263017676864156427437061672863771732977511419449001550674438476562500e-21,
2.6695348641570913023487672463169811744713668590495819354387442914888772804715699749067425727844238281e-22,
2.7048043221089548426023479799726085284884239838165518558183776122449959444793421425856649875640869141e-23,
2.7405397543699319392597053166464580615825908026532700879383534406895372992352122309966944158077239990e-24,
2.7767473173164390618600292900242213453938062979192225338592870097784506833171747075539315119385719299e-25,
2.8134332486618780841645484013765617263573358808544755942032976204749067719590449598854320356622338295e-26,
2.8506038685312913829026949868459556162129993133469813901295681151211924495014748970334039768204092979e-27,
2.8882655805501746464131310942060315361662500130438441297155885191305599052780800350959111710835713893e-28,
2.9264248729476754654074784087816524756714457238524401424785766874722768476403157722476322533111670054e-29,
2.9650883196743632236438561588299363780959880024982907537930431943087746421810189413614811115849079215e-30 };
const int orderCount = 12;//12 terms is enough for -pi/2->pi/2. 28 terms for (3pi/4)
__m256 x2 = _mm256_mul_ps(x0, x0);
__m256 y0 = _mm256_set1_ps(B[orderCount]);
for (int k = (orderCount - 1); k >= 0; k--)
{
y0 = _mm256_fmadd_ps(y0, x2, _mm256_set1_ps(B[k]));
}
return y0;
}
#else
__m256 exp_ps(__m256 x) { return _mm256_exp_ps(x); }
__m256d exp_pd(__m256d x) { return _mm256_exp_pd(x); }
__m256 sincos_ps(__m256* c, __m256 x0) { return _mm256_sincos_ps(c, x0); }
//inverse sinc function. Used for calibrating the effect of finite pixel fill factor in the Fourier plane.
__m256 xcsc_ps(__m256 x)
{
const __m256 zero = _mm256_set1_ps(0.0);
const __m256 one = _mm256_set1_ps(1.0);
const __m256 isZero = _mm256_cmp_ps(x, zero, _CMP_EQ_OS);
__m256 v = _mm256_div_ps(x, _mm256_sin_ps(x));
v = _mm256_or_ps(_mm256_and_ps(isZero, one), _mm256_andnot_ps(isZero, v));
return v;
}
#endif
//AVX complex multiply
static inline __m256 cmul(const __m256& ab, const __m256& xy) noexcept
{
const __m256 aa = _mm256_shuffle_ps(ab, ab, 0xA0);//0b10100000
const __m256 bb = _mm256_shuffle_ps(ab, ab, 0xF5);//0b11110101
const __m256 yx = _mm256_shuffle_ps(xy, xy, 0xB1);//0b10110001
//return _mm256_addsub_ps(_mm256_mul_ps(aa, xy), _mm256_mul_ps(bb, yx));
return _mm256_fmaddsub_ps(aa, xy, _mm256_mul_ps(bb, yx));
}
//Sums up the elements of a SIMD block.
static inline float reduce(__m256& a) noexcept
{
//2 hadds, 1 add, 1 cross-lane permute (2x7+1x4+1x3)=21 latency [2x2 1x0.5 1x1] = 5.5 CPI
/*a = _mm256_hadd_ps(a, a);
a = _mm256_hadd_ps(a, a);
a = _mm256_add_ps(a, _mm256_permute2f128_ps(a, a, 1));
*/
//Alternative (3 adds, 2 in-lane permutes, 1 cross-lane permute) (3x4+2x1+1x3)=18 latency [3x0.5+ 2x1+ 1x1] = 4.5 CPI
a = _mm256_add_ps(a, _mm256_permute_ps(a, 0xB1));//0b10110001
a = _mm256_add_ps(a, _mm256_permute_ps(a, 0x4E));//0b01001110
a = _mm256_add_ps(a, _mm256_permute2f128_ps(a, a, 1));
float* aPtr = (float*)&a;
return aPtr[0];
}
static inline double reduce(__m256d& a) noexcept
{
a = _mm256_add_pd(a, _mm256_permute_pd(a, 0x05));//0b0101
a = _mm256_add_pd(a, _mm256_permute2f128_pd(a, a, 1));
double* aPtr = (double*)&a;
return aPtr[0];
}
//Takes in 8 complex numbers at the pointer V, and returns the abs(V)^2
static inline __m256 cabs2(complex64* V) noexcept
{
const __m256i shufflePattern = _mm256_set_epi32(7, 5, 3, 1, 6, 4, 2, 0);
__m256 Vout[2];
Vout[0] = _mm256_loadu_ps(&V[0][0]);
Vout[1] = _mm256_loadu_ps(&V[4][0]);
Vout[0] = _mm256_mul_ps(Vout[0], Vout[0]);
Vout[1] = _mm256_mul_ps(Vout[1], Vout[1]);
Vout[0] = _mm256_add_ps(Vout[0], _mm256_permute_ps(Vout[0], 0xB1));//0b10110001
Vout[1] = _mm256_add_ps(Vout[1], _mm256_permute_ps(Vout[1], 0xB1));
Vout[0] = _mm256_permutevar8x32_ps(Vout[0], shufflePattern);
Vout[1] = _mm256_permutevar8x32_ps(Vout[1], shufflePattern);
Vout[0] = _mm256_blend_ps(Vout[0], Vout[1], 0xF0); //0b11110000
return Vout[0];
}
/*
//Takes two sets of 8 float32s and converts to a set of 16 int16
//Not currently used anywhere
static inline __m256i cvtps_epi16(__m256 A, __m256 B)
{
__m256i x = _mm256_packs_epi32(_mm256_cvtps_epi32(A), _mm256_cvtps_epi32(B));
return _mm256_permute4x64_epi64(x, 0xD8);
}
*/
//Takes two sets of 8 float32s and converts to a set of 16 uint16
static inline __m256i cvtps_epu16(__m256 A, __m256 B)
{
__m256i x = _mm256_packus_epi32(_mm256_cvtps_epi32(A), _mm256_cvtps_epi32(B));
return _mm256_permute4x64_epi64(x, 0xD8);
}
/**
* @brief Converts and input complex number array (fieldf) into a complex colourmap bitmap (RGB) based on a HSV representation of phase and lightness representation of amplitude.
* https://codereview.stackexchange.com/questions/174617/avx-assembly-for-fast-atan2-approximation
* Might no longer be necessary, potentially use _mm256_atan2_ps?
* @param[out] pixelBuffer : The output RGB pixel array into which the complex colourmapped representation of the field will be written.
* @param[in] fieldf : Complex number array to be converted to a complex colourmap bitmap
* @param[in] pixelCount : The number of elements (pixels) in the input complex number array, and the output complex colourmapped bitmap.
* @param[in] maxValue : The maximum magnitude value within the fieldf array. This sets the scaling of the colourmap
* @return errorCode : [DIGHOLO_ERROR_SUCCESS, DIGHOLO_ERROR_INVALIDHANDLE, DIGHOLO_ERROR_INVALIDDIMENSION, DIGHOLO_ERROR_FILENOTFOUND, DIGHOLO_ERROR_MEMORYALLOCATION]
*/
void complexColormapConvert(unsigned char* pixelBuffer, complex64* fieldf, size_t pixelCount, float maxValue)
{
__m128* field = (__m128*)fieldf;
size_t idx = 0;
size_t blockSize = 8;
__m256 norm = _mm256_set1_ps((float)(1.0 / maxValue));
float hsvScale = (float)(1.0 / 360.0);
__m256i zeroInt16 = _mm256_set1_epi16(0);
__m256i zeroInt32 = _mm256_set1_epi32(0);
__m256 hsvSlopeDownStart[3];
__m256 hsvSlopeDownStop[3];
__m256 hsvSlopeUpStart[3];
__m256 hsvSlopeUpStop[3];
__m256 hsvSlopeZeroStart[3];
__m256 hsvSlopeZeroStop[3];
//RED
hsvSlopeDownStart[0] = _mm256_set1_ps(60 * hsvScale);
hsvSlopeDownStop[0] = _mm256_set1_ps(120 * hsvScale);
hsvSlopeUpStart[0] = _mm256_set1_ps(240 * hsvScale);
hsvSlopeUpStop[0] = _mm256_set1_ps(300 * hsvScale);
hsvSlopeZeroStart[0] = _mm256_set1_ps(120 * hsvScale);
hsvSlopeZeroStop[0] = _mm256_set1_ps(240 * hsvScale);