-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIMD.h
More file actions
1727 lines (1537 loc) · 71.9 KB
/
SIMD.h
File metadata and controls
1727 lines (1537 loc) · 71.9 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
#ifdef _MSC_VER
#define _SIMD_INL_ __forceinline
#elif defined(__GNUC__) || defined(__clang__)
#define _SIMD_INL_ __attribute__((always_inline)) inline
#endif
#include <immintrin.h>
#include <type_traits>
#include <iostream>
#include <stdint.h>
#include <array>
#include <memory>
#ifdef _WIN32
#include <malloc.h>
#elif defined(__linux__)
#include <stdlib.h>
#endif
#if !defined(BASIC_SIMD_NAMESPACE)
#define BASIC_SIMD_NAMESPACE SIMD
#endif
namespace AlignedMemory
{
template<typename T>
class AlignedDeleter {
public:
explicit AlignedDeleter(size_t alignment = 32) : alignment_(alignment) {}
void operator()(T* ptr) const {
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
private:
size_t alignment_;
};
template<typename T>
class AlignedAllocator {
public:
using pointer = T*;
using size_type = std::size_t;
explicit AlignedAllocator(size_t alignment = 32) : alignment_(alignment) {}
pointer allocate(size_type n) {
#ifdef _WIN32
data = _aligned_malloc(n * sizeof(T), alignment_);
#else
data = aligned_alloc(alignment_, n * sizeof(T));
#endif
if (!data) throw std::bad_alloc();
return static_cast<pointer>(data);
}
void* get() const {
return data;
}
private:
void* data;
size_t alignment_;
};
template<typename T>
using AlignedPtr = std::unique_ptr<T, AlignedDeleter<T>>;
template<typename T>
AlignedPtr<T> make_aligned(size_t size, size_t alignment = 32) {
AlignedAllocator<T> allocator(alignment);
AlignedDeleter<T> deleter(alignment);
return AlignedPtr<T>(allocator.allocate(size), deleter);
}
}
#ifdef _MSC_VER
#include <intrin.h>
#elif defined(__GNUC__) || defined(__clang__)
#include <cpuid.h>
#include <x86intrin.h>
#endif
enum class InstructionSet {
NONE = 0,
SSE = 1,
SSE2 = 2, // Added SSE2
AVX = 3, // Updated ordinal values
AVX2 = 4, // Updated ordinal values
AVX512 = 5 // Updated ordinal values
};
class CPUFeatures {
private:
static bool initialized_;
static bool has_sse_;
static bool has_sse2_;
static bool has_avx_;
static bool has_avx2_;
static bool has_avx512f_;
static void initialize() {
if (initialized_) return;
#if defined(_MSC_VER) || defined(__GNUC__) || defined(__clang__)
std::array<int, 4> cpui{};
// Get vendor string and max CPUID level
#if defined(_MSC_VER)
__cpuid(cpui.data(), 0);
#else
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
__get_cpuid(0, &eax, &ebx, &ecx, &edx);
cpui = {static_cast<int>(eax), static_cast<int>(ebx),
static_cast<int>(ecx), static_cast<int>(edx)};
#endif
int max_std_id = cpui[0];
// Check SSE, SSE2 and AVX
if (max_std_id >= 1) {
#if defined(_MSC_VER)
__cpuid(cpui.data(), 1);
#else
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
cpui = {static_cast<int>(eax), static_cast<int>(ebx),
static_cast<int>(ecx), static_cast<int>(edx)};
#endif
// Raw CPU feature flags
bool cpu_has_sse = (cpui[3] & (1 << 25)) != 0; // EDX bit 25
bool cpu_has_sse2 = (cpui[3] & (1 << 26)) != 0; // EDX bit 26
bool cpu_has_avx = (cpui[2] & (1 << 28)) != 0; // ECX bit 28
bool cpu_uses_xsave = (cpui[2] & (1 << 27)) != 0; // ECX bit 27
// Safely assign SSE/SSE2 flags (these don't need OS support)
has_sse_ = cpu_has_sse;
has_sse2_ = cpu_has_sse2;
// For AVX and beyond, we need to check OS support via XCR0
if (cpu_has_avx && cpu_uses_xsave) {
// Get XCR0 register to check OS support for YMM/ZMM state saving
unsigned long long xcrFeatureMask = 0;
try {
#if defined(_MSC_VER)
xcrFeatureMask = _xgetbv(0);
#elif defined(__GNUC__) || defined(__clang__)
unsigned int eax_xcr = 0, edx_xcr = 0;
__asm__ __volatile__("xgetbv" : "=a"(eax_xcr), "=d"(edx_xcr) : "c"(0));
xcrFeatureMask = ((unsigned long long)edx_xcr << 32) | eax_xcr;
#endif
// Check bits 1 and 2 for SSE and AVX state saving (XMM and YMM registers)
// XCR0[2:1] = '11b' (XMM and YMM are enabled by OS)
bool avxSupportedByOS = (xcrFeatureMask & 0x6) == 0x6;
has_avx_ = cpu_has_avx && avxSupportedByOS;
// Check AVX2 and AVX-512
if (max_std_id >= 7) {
#if defined(_MSC_VER)
__cpuidex(cpui.data(), 7, 0);
#else
__cpuid_count(7, 0, eax, ebx, ecx, edx);
cpui = {static_cast<int>(eax), static_cast<int>(ebx),
static_cast<int>(ecx), static_cast<int>(edx)};
#endif
bool cpu_has_avx2 = (cpui[1] & (1 << 5)) != 0; // EBX bit 5
bool cpu_has_avx512f = (cpui[1] & (1 << 16)) != 0; // EBX bit 16
has_avx2_ = cpu_has_avx2 && avxSupportedByOS;
// For AVX-512, additionally check bits 7:5 of XCR0 for ZMM state
// XCR0[7:5] = '111b' (ZMM 0-15, ZMM 16-31 and mask registers)
bool avx512SupportedByOS = avxSupportedByOS &&
((xcrFeatureMask & 0xE0) == 0xE0);
has_avx512f_ = cpu_has_avx512f && avx512SupportedByOS;
}
} catch (...) {
has_avx_ = false;
has_avx2_ = false;
has_avx512f_ = false;
}
} else {
has_avx_ = false;
has_avx2_ = false;
has_avx512f_ = false;
}
}
#else
has_sse_ = false;
has_sse2_ = false;
has_avx_ = false;
has_avx2_ = false;
has_avx512f_ = false;
#endif
initialized_ = true;
}
public:
static bool hasSSE() {
if (!initialized_) initialize();
return has_sse_;
}
static bool hasSSE2() {
if (!initialized_) initialize();
return has_sse2_;
}
static bool hasAVX() {
if (!initialized_) initialize();
return has_avx_;
}
static bool hasAVX2() {
if (!initialized_) initialize();
return has_avx2_;
}
static bool hasAVX512() {
if (!initialized_) initialize();
return has_avx512f_;
}
template<InstructionSet T>
static typename std::enable_if<T == InstructionSet::NONE, bool>::type
supportsInstructionSet() {
return true; // NONE is always supported
}
template<InstructionSet T>
static typename std::enable_if<T == InstructionSet::SSE, bool>::type
supportsInstructionSet() {
return hasSSE();
}
template<InstructionSet T>
static typename std::enable_if<T == InstructionSet::SSE2, bool>::type
supportsInstructionSet() {
return hasSSE2();
}
template<InstructionSet T>
static typename std::enable_if<T == InstructionSet::AVX, bool>::type
supportsInstructionSet() {
return hasAVX();
}
template<InstructionSet T>
static typename std::enable_if<T == InstructionSet::AVX2, bool>::type
supportsInstructionSet() {
return hasAVX2();
}
template<InstructionSet T>
static typename std::enable_if<T == InstructionSet::AVX512, bool>::type
supportsInstructionSet() {
return hasAVX512();
}
// New function to print all supported instruction sets
static void printSupportedInstructionSets() {
if (!initialized_) initialize();
std::cout << "Supported SIMD Instruction Sets:" << std::endl;
std::cout << "-------------------------------" << std::endl;
std::cout << "SSE: " << (has_sse_ ? "Yes" : "No") << std::endl;
std::cout << "SSE2: " << (has_sse2_ ? "Yes" : "No") << std::endl;
std::cout << "AVX: " << (has_avx_ ? "Yes" : "No") << std::endl;
std::cout << "AVX2: " << (has_avx2_ ? "Yes" : "No") << std::endl;
std::cout << "AVX512: " << (has_avx512f_ ? "Yes" : "No") << std::endl;
}
// New function to print all supported SIMD types
static void printSupportedSIMDTypes() {
if (!initialized_) initialize();
std::cout << "Supported SIMD Types:" << std::endl;
std::cout << "-------------------" << std::endl;
// Integer SIMD types
std::cout << "Integer Types:" << std::endl;
// 128-bit types (SSE/SSE2)
if (has_sse_) {
std::cout << " SIMD::int_128<int8_t>: Yes" << std::endl;
std::cout << " SIMD::int_128<uint8_t>: Yes" << std::endl;
std::cout << " SIMD::int_128<int16_t>: Yes" << std::endl;
std::cout << " SIMD::int_128<uint16_t>: Yes" << std::endl;
std::cout << " SIMD::int_128<int32_t>: Yes" << std::endl;
std::cout << " SIMD::int_128<uint32_t>: Yes" << std::endl;
} else {
std::cout << " SIMD::int_128<*>: No (requires SSE)" << std::endl;
}
if (has_sse2_) {
std::cout << " SIMD::int_128<int64_t>: Yes" << std::endl;
std::cout << " SIMD::int_128<uint64_t>: Yes" << std::endl;
} else {
std::cout << " SIMD::int_128<int64_t>: No (requires SSE2)" << std::endl;
std::cout << " SIMD::int_128<uint64_t>: No (requires SSE2)" << std::endl;
}
// 256-bit types (AVX/AVX2)
if (has_avx2_) {
std::cout << " SIMD::int_256<int8_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<uint8_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<int16_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<uint16_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<int32_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<uint32_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<int64_t>: Yes" << std::endl;
std::cout << " SIMD::int_256<uint64_t>: Yes" << std::endl;
} else {
std::cout << " SIMD::int_256<*>: No (requires AVX2)" << std::endl;
}
// 512-bit types (AVX-512)
if (has_avx512f_) {
std::cout << " SIMD::int_512<int8_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<uint8_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<int16_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<uint16_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<int32_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<uint32_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<int64_t>: Yes" << std::endl;
std::cout << " SIMD::int_512<uint64_t>: Yes" << std::endl;
} else {
std::cout << " SIMD::int_512<*>: No (requires AVX-512)" << std::endl;
}
// Float SIMD types
std::cout << "\nFloat Types:" << std::endl;
if (has_avx_) {
std::cout << " SIMD::float_256: Yes" << std::endl;
} else {
std::cout << " SIMD::float_256: No (requires AVX)" << std::endl;
}
if (has_avx512f_) {
std::cout << " SIMD::float_512: Yes" << std::endl;
} else {
std::cout << " SIMD::float_512: No (requires AVX-512)" << std::endl;
}
// Double SIMD types
std::cout << "\nDouble Types:" << std::endl;
if (has_avx_) {
std::cout << " SIMD::double_256: Yes" << std::endl;
} else {
std::cout << " SIMD::double_256: No (requires AVX)" << std::endl;
}
if (has_avx512f_) {
std::cout << " SIMD::double_512: Yes" << std::endl;
} else {
std::cout << " SIMD::double_512: No (requires AVX-512)" << std::endl;
}
}
};
// Initialize static members
bool CPUFeatures::initialized_ = false;
bool CPUFeatures::has_sse_ = false;
bool CPUFeatures::has_sse2_ = false; // Initialize SSE2 static member
bool CPUFeatures::has_avx_ = false;
bool CPUFeatures::has_avx2_ = false;
bool CPUFeatures::has_avx512f_ = false;
// Primary template - default is NONE
template<typename T, size_t BitWidth>
struct RequiredInstructionSet {
static constexpr InstructionSet value = InstructionSet::NONE;
};
// Macro for specializations
#define DEFINE_REQUIRED_INSTRUCTION_SET(TYPE, BITS, SET) \
template<> \
struct RequiredInstructionSet<TYPE, BITS> { \
static constexpr InstructionSet value = InstructionSet::SET; \
static constexpr const char* name = #SET; \
};
// Integer types
DEFINE_REQUIRED_INSTRUCTION_SET(int8_t, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(uint8_t, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(int16_t, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(uint16_t, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(int32_t, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(uint32_t, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(int64_t, 128, SSE2)
DEFINE_REQUIRED_INSTRUCTION_SET(uint64_t, 128, SSE2)
DEFINE_REQUIRED_INSTRUCTION_SET(int8_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(uint8_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(int16_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(uint16_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(int32_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(uint32_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(int64_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(uint64_t, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(int8_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(uint8_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(int16_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(uint16_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(int32_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(uint32_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(int64_t, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(uint64_t, 512, AVX512)
// Floating-point types
DEFINE_REQUIRED_INSTRUCTION_SET(float, 128, SSE)
DEFINE_REQUIRED_INSTRUCTION_SET(double, 128, SSE2)
DEFINE_REQUIRED_INSTRUCTION_SET(float, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(double, 256, AVX)
DEFINE_REQUIRED_INSTRUCTION_SET(float, 512, AVX512)
DEFINE_REQUIRED_INSTRUCTION_SET(double, 512, AVX512)
#undef DEFINE_REQUIRED_INSTRUCTION_SET
template<typename T>
struct AssertFalse : std::false_type {};
template<typename T>
using IsSIMD_Int = typename std::enable_if<std::is_same<typename T::Type, int>::value, int>::type;
template<typename T>
using IsSIMD_Float = typename std::enable_if<std::is_same<typename T::Type, float>::value, int>::type;
template<typename T>
using IsSIMD_Double = typename std::enable_if<std::is_same<typename T::Type, double>::value, int>::type;
template<typename T>
struct IsElementAnyOfInts : std::integral_constant<
bool,
std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value ||
std::is_same<T, uint16_t>::value || std::is_same<T, int16_t>::value ||
std::is_same<T, uint32_t>::value || std::is_same<T, int32_t>::value ||
std::is_same<T, uint64_t>::value || std::is_same<T, int64_t>::value> {};
template<typename SIMD_Kind, typename ElementType>
struct IsElementValid_Impl : std::false_type {};
template<typename ElementType>
struct IsElementValid_Impl<int, ElementType> : IsElementAnyOfInts<ElementType> {};
template<>
struct IsElementValid_Impl<float, float> : std::true_type {};
template<>
struct IsElementValid_Impl<double, double> : std::true_type {};
template<typename SIMD_Kind, typename ElementType>
using IsElementValid = typename std::enable_if<IsElementValid_Impl<SIMD_Kind, ElementType>::value, int>::type;
template<typename Element, typename Target>
using IsElementAnyOfT = typename std::enable_if<std::is_same<Element, Target>::value, int>::type;
// Base case: If no Args are provided, return true
template<typename T_ElementType, typename... Args>
using IsArrayConstructible = typename std::enable_if<
std::is_constructible<std::array<T_ElementType, sizeof...(Args)>, Args...>::value,
int
>::type;
template<int Size, typename... Args>
using IsSizeValid = typename std::enable_if<(Size >= static_cast<int>(sizeof...(Args))), int>::type;
template<typename TypeRequired, typename FirstElementType, typename... Rest>
struct IsAllElementsCompatible_impl : std::conditional<
std::is_convertible<TypeRequired, FirstElementType>::value,
IsAllElementsCompatible_impl<TypeRequired, Rest...>,
std::false_type>::type {};
template<typename TypeRequired, typename FirstElementType>
struct IsAllElementsCompatible_impl<TypeRequired, FirstElementType> : std::conditional<
std::is_convertible<TypeRequired, FirstElementType>::value,
std::true_type,
std::false_type>::type {};
template<typename T, typename... Elements>
using IsAllElementsCompatible = typename std::enable_if<IsAllElementsCompatible_impl<T, Elements...>::value, int>::type;
template<typename ContainerType, int Bits, typename T_ElementType,
typename = IsElementValid<ContainerType, T_ElementType>,
typename = typename std::enable_if<Bits%8 == 0, int>::type >
struct SIMD_Type_t
{
public:
using Type = ContainerType;
using ElementType = T_ElementType;
static constexpr unsigned int BitWidth = Bits;
static constexpr unsigned int SizeBytes = Bits/8;
static constexpr unsigned int Alignment = Bits/8;
static constexpr unsigned int ElementCount = (Bits/8)/sizeof(T_ElementType);
void* Data;
AlignedMemory::AlignedPtr<T_ElementType> AlignedData;
private:
bool IsImported;
public:
SIMD_Type_t() :Data(nullptr), IsImported(false)
{
if (!CPUFeatures::supportsInstructionSet<RequiredInstructionSet<ContainerType, Bits>::value>())
{
std::string errorMessage = "For SIMD Type " + std::string(typeid(ContainerType).name()) + "_" + std::to_string(Bits) + " the required instruction set " + std::string(RequiredInstructionSet<ContainerType, Bits>::name) + " does not supported in this device.";
throw std::runtime_error(errorMessage);
}
AlignedData = std::move(AlignedMemory::make_aligned<T_ElementType>(SizeBytes, Alignment));
Data = AlignedData.get();
}
template<typename ...Args,
IsElementValid<ContainerType, T_ElementType> = 0,
IsAllElementsCompatible<T_ElementType, Args...> = 0,
IsSizeValid< ElementCount, Args...> = 0>
SIMD_Type_t(Args... args) : Data(nullptr), IsImported(false)
{
if (!CPUFeatures::supportsInstructionSet<RequiredInstructionSet<ContainerType, Bits>::value>())
{
std::string errorMessage = "For SIMD Type " + std::string(typeid(ContainerType).name()) + "_" + std::to_string(Bits) + " the required instruction set " + std::string(RequiredInstructionSet<ContainerType, Bits>::name) + " does not supported in this device.";
throw std::runtime_error(errorMessage);
}
alignas(Alignment) T_ElementType data[SizeBytes / sizeof(T_ElementType)] = { args... };
AlignedData = std::move(AlignedMemory::make_aligned<T_ElementType>(SizeBytes, Alignment));
Data = AlignedData.get();
std::copy(data, data + SizeBytes / sizeof(T_ElementType), reinterpret_cast<T_ElementType*>(Data));
}
SIMD_Type_t(void* data) : Data(nullptr)
{
if(!CPUFeatures::supportsInstructionSet<RequiredInstructionSet<ContainerType, Bits>::value>())
{
std::string errorMessage = "For SIMD Type " + std::string(typeid(ContainerType).name()) + "_" + std::to_string(Bits) + " the required instruction set " + std::string(RequiredInstructionSet<ContainerType, Bits>::name) + " does not supported in this device.";
throw std::runtime_error(errorMessage);
}
/*This will check for memory alignment*/
if((reinterpret_cast<uintptr_t>(data) & (Alignment - 1)) != 0)
{
throw std::runtime_error("Data is not aligned to the required boundary.");
}
IsImported = true;
Data = data;
}
/* Move constructor */
SIMD_Type_t(SIMD_Type_t&& other) noexcept : Data(other.Data), IsImported(other.IsImported) {
other.Data = nullptr;
other.IsImported = false;
}
SIMD_Type_t(const SIMD_Type_t& other) noexcept : Data(nullptr), IsImported(false) {
AlignedData = std::move(AlignedMemory::make_aligned<T_ElementType>(SizeBytes, Alignment));
Data = AlignedData.get();
std::copy(other.Data, other.Data + SizeBytes / sizeof(T_ElementType), reinterpret_cast<T_ElementType*>(Data));
}
~SIMD_Type_t()
{
}
static _SIMD_INL_ SIMD_Type_t Import(void* data)
{
return std::move(SIMD_Type_t(data));
}
const T_ElementType *const Get()
{
return reinterpret_cast<T_ElementType*>(Data);
}
/* Copy assignment operator */
SIMD_Type_t& operator=(const SIMD_Type_t& other) {
Data = other.Data;\
return *this; \
}
explicit operator bool() noexcept {
return Data != nullptr;
}
T_ElementType ElementAt(unsigned int index) const
{
if(index >= ElementCount)
{
return 0;
}
return *(reinterpret_cast<T_ElementType*>(Data) + index);
}
static _SIMD_INL_ SIMD_Type_t Add(const SIMD_Type_t& a, const SIMD_Type_t& b) {
SIMD_Type_t result;
/* Implementation will be specialized */
return result;
}
static _SIMD_INL_ void AddInplace(SIMD_Type_t& to, const SIMD_Type_t& from) {
/* Implementation will be specialized */
}
static _SIMD_INL_ void AddInplaceRaw(T_ElementType* to, const T_ElementType* from) {
/* Implementation will be specialized */
}
static _SIMD_INL_ SIMD_Type_t Subtract(const SIMD_Type_t& a, const SIMD_Type_t& b) {
SIMD_Type_t result;
/* Implementation will be specialized */
return result;
}
static _SIMD_INL_ void SubtractInplace(SIMD_Type_t& to, const SIMD_Type_t& from) {
/* Implementation will be specialized */
}
static _SIMD_INL_ void SubtractInplaceRaw(T_ElementType* to, const T_ElementType* from) {
/* Implementation will be specialized */
}
static _SIMD_INL_ SIMD_Type_t Multiply(const SIMD_Type_t& a, const SIMD_Type_t& b) {
static_assert(AssertFalse<T_ElementType>::value, "Multiplication is not supported for this type.");
}
static _SIMD_INL_ void MultiplyInplace(SIMD_Type_t& to, const SIMD_Type_t& from) {
static_assert(AssertFalse<T_ElementType>::value, "Multiplication is not supported for this type.");
}
static _SIMD_INL_ void MultiplyInplaceRaw(T_ElementType* to, const T_ElementType* from) {
static_assert(AssertFalse<T_ElementType>::value, "Multiplication is not supported for this type.");
}
static _SIMD_INL_ SIMD_Type_t Divide(const SIMD_Type_t& a, const SIMD_Type_t& b) {
static_assert(AssertFalse<T_ElementType>::value, "Division is not supported for this type.");
}
static _SIMD_INL_ void DivideInplace(SIMD_Type_t& to, const SIMD_Type_t& from) {
static_assert(AssertFalse<T_ElementType>::value, "Division is not supported for this type.");
}
static _SIMD_INL_ void DivideInplaceRaw(T_ElementType* to, const T_ElementType* from) {
static_assert(AssertFalse<T_ElementType>::value, "Division is not supported for this type.");
}
static _SIMD_INL_ bool IsEqual(const SIMD_Type_t& a, const SIMD_Type_t& b) {
static_assert(AssertFalse<T_ElementType>::value, "Equality check is not supported for this type.");
}
static _SIMD_INL_ bool IsEqualInplaceRaw(T_ElementType* to, const T_ElementType* from) {
static_assert(AssertFalse<T_ElementType>::value, "Equality check is not supported for this type.");
}
_SIMD_INL_ SIMD_Type_t operator+(const SIMD_Type_t& other) const
{
return Add(*this, other);
}
_SIMD_INL_ SIMD_Type_t operator*(const SIMD_Type_t& other) const
{
return Multiply(*this, other);
}
_SIMD_INL_ SIMD_Type_t operator-(const SIMD_Type_t& other) const
{
return Subtract(*this, other);
}
_SIMD_INL_ SIMD_Type_t operator/(const SIMD_Type_t& other) const
{
return Divide(*this, other);
}
_SIMD_INL_ void operator+=(const SIMD_Type_t& other)
{
AddInplace(*this, other);
}
_SIMD_INL_ void operator*=(const SIMD_Type_t& other)
{
MultiplyInplace(*this, other);
}
_SIMD_INL_ void operator-=(const SIMD_Type_t& other)
{
SubtractInplace(*this, other);
}
_SIMD_INL_ void operator/=(const SIMD_Type_t& other)
{
DivideInplace(*this, other);
}
_SIMD_INL_ bool operator==(const SIMD_Type_t& other) const
{
return IsEqual(*this, other);
}
_SIMD_INL_ T_ElementType& operator[](unsigned int index)
{
return *(reinterpret_cast<T_ElementType*>(Data) + index);
}
};
#define DECLARE_SIMD_USE_TYPE_INT(TYPE, XXX) \
namespace BASIC_SIMD_NAMESPACE \
{\
template<typename ElementType=int32_t>\
using TYPE##_##XXX = SIMD_Type_t<TYPE, XXX, ElementType>; \
}
#define DECLARE_SIMD_USE_TYPE_FLOATING(TYPE, XXX) \
namespace BASIC_SIMD_NAMESPACE \
{\
using TYPE##_##XXX = SIMD_Type_t<TYPE, XXX, TYPE>; \
}
// ██╗███╗ ██╗████████╗ ██╗██████╗ █████╗
// ██║████╗ ██║╚══██╔══╝ ███║╚════██╗██╔══██╗
// ██║██╔██╗ ██║ ██║█████╗╚██║ █████╔╝╚█████╔╝
// ██║██║╚██╗██║ ██║╚════╝ ██║██╔═══╝ ██╔══██╗
// ██║██║ ╚████║ ██║ ██║███████╗╚█████╔╝
// ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝ ╚════╝
#define CREATE_INT128_OPERATOR_PLUS(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, int##XX##_t> SIMD_Type_t<int, 128, int##XX##_t>::Add(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t<int, 128, int##XX##_t> result;\
_mm_store_si128((__m128i*)result.Data, _mm_add_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, uint##XX##_t> SIMD_Type_t<int, 128,uint##XX##_t>::Add(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t<int, 128, uint##XX##_t> result;\
_mm_store_si128((__m128i*)result.Data, _mm_add_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::AddInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_add_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::AddInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_add_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::AddInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_add_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::AddInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_add_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}
#define CREATE_INT128_OPERATOR_MINUS(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, int##XX##_t> SIMD_Type_t<int, 128, int##XX##_t>::Subtract(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm_store_si128((__m128i*)result.Data, _mm_sub_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, uint##XX##_t> SIMD_Type_t<int, 128,uint##XX##_t>::Subtract(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm_store_si128((__m128i*)result.Data, _mm_sub_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::SubtractInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_sub_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::SubtractInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_sub_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::SubtractInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_sub_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::SubtractInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_sub_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}
#define CREATE_INT128_OPERATOR_MULTIPLY(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, int##XX##_t> SIMD_Type_t<int, 128, int##XX##_t>::Multiply(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm_store_si128((__m128i*)result.Data, _mm_mullo_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, uint##XX##_t> SIMD_Type_t<int, 128,uint##XX##_t>::Multiply(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm_store_si128((__m128i*)result.Data, _mm_mullo_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::MultiplyInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_mullo_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::MultiplyInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_mullo_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::MultiplyInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_mullo_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::MultiplyInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_mullo_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}
#define CREATE_INT128_OPERATOR_DIVIDE(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, int##XX##_t> SIMD_Type_t<int, 128, int##XX##_t>::Divide(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm_store_si128((__m128i*)result.Data, _mm_div_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 128, uint##XX##_t> SIMD_Type_t<int, 128,uint##XX##_t>::Divide(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm_store_si128((__m128i*)result.Data, _mm_div_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::DivideInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_div_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::DivideInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm_store_si128((__m128i*)to.Data, _mm_div_epi##XX(_mm_load_si128((__m128i*)to.Data), _mm_load_si128((__m128i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, int##XX##_t>::DivideInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_div_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 128, uint##XX##_t>::DivideInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm_store_si128((__m128i*)to, _mm_div_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from)));\
}
#define CREATE_INT128_OPERATOR_EQUAL(XX) \
template<>\
_SIMD_INL_ bool SIMD_Type_t<int, 128, int##XX##_t>::IsEqual(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
return _mm_movemask_epi8(_mm_cmpeq_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data))) == 0xFFFF;\
}\
template<>\
_SIMD_INL_ bool SIMD_Type_t<int, 128, uint##XX##_t>::IsEqual(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
return _mm_movemask_epi8(_mm_cmpeq_epi##XX(_mm_load_si128((__m128i*)a.Data), _mm_load_si128((__m128i*)b.Data))) == 0xFFFF;\
}\
template<>\
_SIMD_INL_ bool SIMD_Type_t<int, 128, int##XX##_t>::IsEqualInplaceRaw(int##XX##_t* to, const int##XX##_t* from) {\
return _mm_movemask_epi8(_mm_cmpeq_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from))) == 0xFFFF;\
}\
template<>\
_SIMD_INL_ bool SIMD_Type_t<int, 128, uint##XX##_t>::IsEqualInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from) {\
return _mm_movemask_epi8(_mm_cmpeq_epi##XX(_mm_load_si128((__m128i*)to), _mm_load_si128((__m128i*)from))) == 0xFFFF;\
}
// ██╗███╗ ██╗████████╗ ██████╗ ███████╗ ██████╗
// ██║████╗ ██║╚══██╔══╝ ╚════██╗██╔════╝██╔════╝
// ██║██╔██╗ ██║ ██║█████╗ █████╔╝███████╗███████╗
// ██║██║╚██╗██║ ██║╚════╝██╔═══╝ ╚════██║██╔═══██╗
// ██║██║ ╚████║ ██║ ███████╗███████║╚██████╔╝
// ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚══════╝ ╚═════╝
#define CREATE_INT256_OPERATOR_PLUS(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, int##XX##_t> SIMD_Type_t<int, 256, int##XX##_t>::Add(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_add_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, uint##XX##_t> SIMD_Type_t<int, 256, uint##XX##_t>::Add(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_add_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::AddInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_add_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::AddInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_add_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::AddInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm256_store_si256((__m256i*)to, _mm256_add_epi##XX(_mm256_load_si256((__m256i*)to), _mm256_load_si256((__m256i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::AddInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm256_store_si256((__m256i*)to, _mm256_add_epi##XX(_mm256_load_si256((__m256i*)to), _mm256_load_si256((__m256i*)from)));\
}
#define CREATE_INT256_OPERATOR_MINUS(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, int##XX##_t> SIMD_Type_t<int, 256, int##XX##_t>::Subtract(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_sub_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, uint##XX##_t> SIMD_Type_t<int, 256, uint##XX##_t>::Subtract(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_sub_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::SubtractInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_sub_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::SubtractInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_sub_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::SubtractInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm256_store_si256((__m256i*)to, _mm256_sub_epi##XX(_mm256_load_si256((__m256i*)to), _mm256_load_si256((__m256i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::SubtractInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm256_store_si256((__m256i*)to, _mm256_sub_epi##XX(_mm256_load_si256((__m256i*)to), _mm256_load_si256((__m256i*)from)));\
}
#define CREATE_INT256_OPERATOR_MULTIPLY(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, int##XX##_t> SIMD_Type_t<int, 256, int##XX##_t>::Multiply(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_mullo_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, uint##XX##_t> SIMD_Type_t<int, 256,uint##XX##_t>::Multiply(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_mullo_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::MultiplyInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_mullo_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::MultiplyInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_mullo_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::MultiplyInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\
{\
_mm256_store_si256((__m256i*)to, _mm256_mullo_epi##XX(_mm256_load_si256((__m256i*)to), _mm256_load_si256((__m256i*)from)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::MultiplyInplaceRaw(uint##XX##_t* to, const uint##XX##_t* from)\
{\
_mm256_store_si256((__m256i*)to, _mm256_mullo_epi##XX(_mm256_load_si256((__m256i*)to), _mm256_load_si256((__m256i*)from)));\
}
#define CREATE_INT256_OPERATOR_DIVIDE(XX) \
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, int##XX##_t> SIMD_Type_t<int, 256, int##XX##_t>::Divide(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_div_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ SIMD_Type_t<int, 256, uint##XX##_t> SIMD_Type_t<int, 256,uint##XX##_t>::Divide(const SIMD_Type_t& a, const SIMD_Type_t& b) {\
SIMD_Type_t result;\
_mm256_store_si256((__m256i*)result.Data, _mm256_div_epi##XX(_mm256_load_si256((__m256i*)a.Data), _mm256_load_si256((__m256i*)b.Data)));\
return result;\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::DivideInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_div_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, uint##XX##_t>::DivideInplace(SIMD_Type_t& to, const SIMD_Type_t& from)\
{\
_mm256_store_si256((__m256i*)to.Data, _mm256_div_epi##XX(_mm256_load_si256((__m256i*)to.Data), _mm256_load_si256((__m256i*)from.Data)));\
}\
template<>\
_SIMD_INL_ void SIMD_Type_t<int, 256, int##XX##_t>::DivideInplaceRaw(int##XX##_t* to, const int##XX##_t* from)\