-
Notifications
You must be signed in to change notification settings - Fork 5
/
ConvectionKernels_ParallelMath.h
1816 lines (1502 loc) · 56.3 KB
/
ConvectionKernels_ParallelMath.h
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
/*
Convection Texture Tools
Copyright (c) 2018-2019 Eric Lasota
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifndef __CVTT_PARALLELMATH_H__
#define __CVTT_PARALLELMATH_H__
#include "ConvectionKernels.h"
#include "ConvectionKernels_Config.h"
#ifdef CVTT_USE_SSE2
#include <emmintrin.h>
#endif
#include <float.h>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define UNREFERENCED_PARAMETER(n) ((void)n)
// Parallel math implementation
//
// After preprocessor defs are handled, what this should do is expose the following types:
// SInt16 - Signed 16-bit integer
// UInt16 - Signed 16-bit integer
// UInt15 - Unsigned 15-bit integer
// SInt32 - Signed 32-bit integer
// UInt31 - Unsigned 31-bit integer
// AInt16 - 16-bit integer of unknown signedness (only used for storage)
// Int16CompFlag - Comparison flags from comparing 16-bit integers
// Int32CompFlag - Comparison flags from comparing 32-bit integers
// FloatCompFlag - Comparison flags from comparing 32-bit floats
//
// The reason for these distinctions are that depending on the instruction set, signed or unsigned versions of certain ops
// (particularly max, min, compares, and right shift) may not be available. In cases where ops are not available, it's
// necessary to do high bit manipulations to accomplish the operation with 16-bit numbers. The 15-bit and 31-bit uint types
// can elide the bit flips if unsigned versions are not available.
namespace cvtt
{
#ifdef CVTT_USE_SSE2
// SSE2 version
struct ParallelMath
{
typedef uint16_t ScalarUInt16;
typedef int16_t ScalarSInt16;
template<unsigned int TRoundingMode>
struct RoundForScope
{
unsigned int m_oldCSR;
RoundForScope()
{
m_oldCSR = _mm_getcsr();
_mm_setcsr((m_oldCSR & ~_MM_ROUND_MASK) | (TRoundingMode));
}
~RoundForScope()
{
_mm_setcsr(m_oldCSR);
}
};
struct RoundTowardZeroForScope : RoundForScope<_MM_ROUND_TOWARD_ZERO>
{
};
struct RoundTowardNearestForScope : RoundForScope<_MM_ROUND_NEAREST>
{
};
struct RoundUpForScope : RoundForScope<_MM_ROUND_UP>
{
};
struct RoundDownForScope : RoundForScope<_MM_ROUND_DOWN>
{
};
static const int ParallelSize = 8;
enum Int16Subtype
{
IntSubtype_Signed,
IntSubtype_UnsignedFull,
IntSubtype_UnsignedTruncated,
IntSubtype_Abstract,
};
template<int TSubtype>
struct VInt16
{
__m128i m_value;
inline VInt16 operator+(int16_t other) const
{
VInt16 result;
result.m_value = _mm_add_epi16(m_value, _mm_set1_epi16(static_cast<int16_t>(other)));
return result;
}
inline VInt16 operator+(const VInt16 &other) const
{
VInt16 result;
result.m_value = _mm_add_epi16(m_value, other.m_value);
return result;
}
inline VInt16 operator|(const VInt16 &other) const
{
VInt16 result;
result.m_value = _mm_or_si128(m_value, other.m_value);
return result;
}
inline VInt16 operator&(const VInt16 &other) const
{
VInt16 result;
result.m_value = _mm_and_si128(m_value, other.m_value);
return result;
}
inline VInt16 operator-(const VInt16 &other) const
{
VInt16 result;
result.m_value = _mm_sub_epi16(m_value, other.m_value);
return result;
}
inline VInt16 operator<<(int bits) const
{
VInt16 result;
result.m_value = _mm_slli_epi16(m_value, bits);
return result;
}
inline VInt16 operator^(const VInt16 &other) const
{
VInt16 result;
result.m_value = _mm_xor_si128(m_value, other.m_value);
return result;
}
};
typedef VInt16<IntSubtype_Signed> SInt16;
typedef VInt16<IntSubtype_UnsignedFull> UInt16;
typedef VInt16<IntSubtype_UnsignedTruncated> UInt15;
typedef VInt16<IntSubtype_Abstract> AInt16;
template<int TSubtype>
struct VInt32
{
__m128i m_values[2];
inline VInt32 operator+(const VInt32& other) const
{
VInt32 result;
result.m_values[0] = _mm_add_epi32(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_add_epi32(m_values[1], other.m_values[1]);
return result;
}
inline VInt32 operator-(const VInt32& other) const
{
VInt32 result;
result.m_values[0] = _mm_sub_epi32(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_sub_epi32(m_values[1], other.m_values[1]);
return result;
}
inline VInt32 operator<<(const int other) const
{
VInt32 result;
result.m_values[0] = _mm_slli_epi32(m_values[0], other);
result.m_values[1] = _mm_slli_epi32(m_values[1], other);
return result;
}
inline VInt32 operator|(const VInt32& other) const
{
VInt32 result;
result.m_values[0] = _mm_or_si128(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_or_si128(m_values[1], other.m_values[1]);
return result;
}
};
typedef VInt32<IntSubtype_Signed> SInt32;
typedef VInt32<IntSubtype_UnsignedTruncated> UInt31;
typedef VInt32<IntSubtype_UnsignedFull> UInt32;
typedef VInt32<IntSubtype_Abstract> AInt32;
template<class TTargetType>
struct LosslessCast
{
#ifdef CVTT_PERMIT_ALIASING
template<int TSrcSubtype>
static const TTargetType& Cast(const VInt32<TSrcSubtype> &src)
{
return reinterpret_cast<VInt32<TSubtype>&>(src);
}
template<int TSrcSubtype>
static const TTargetType& Cast(const VInt16<TSrcSubtype> &src)
{
return reinterpret_cast<VInt16<TSubtype>&>(src);
}
#else
template<int TSrcSubtype>
static TTargetType Cast(const VInt32<TSrcSubtype> &src)
{
TTargetType result;
result.m_values[0] = src.m_values[0];
result.m_values[1] = src.m_values[1];
return result;
}
template<int TSrcSubtype>
static TTargetType Cast(const VInt16<TSrcSubtype> &src)
{
TTargetType result;
result.m_value = src.m_value;
return result;
}
#endif
};
struct Int64
{
__m128i m_values[4];
};
struct Float
{
__m128 m_values[2];
inline Float operator+(const Float &other) const
{
Float result;
result.m_values[0] = _mm_add_ps(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_add_ps(m_values[1], other.m_values[1]);
return result;
}
inline Float operator+(float other) const
{
Float result;
result.m_values[0] = _mm_add_ps(m_values[0], _mm_set1_ps(other));
result.m_values[1] = _mm_add_ps(m_values[1], _mm_set1_ps(other));
return result;
}
inline Float operator-(const Float& other) const
{
Float result;
result.m_values[0] = _mm_sub_ps(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_sub_ps(m_values[1], other.m_values[1]);
return result;
}
inline Float operator-() const
{
Float result;
result.m_values[0] = _mm_sub_ps(_mm_setzero_ps(), m_values[0]);
result.m_values[1] = _mm_sub_ps(_mm_setzero_ps(), m_values[1]);
return result;
}
inline Float operator*(const Float& other) const
{
Float result;
result.m_values[0] = _mm_mul_ps(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_mul_ps(m_values[1], other.m_values[1]);
return result;
}
inline Float operator*(float other) const
{
Float result;
result.m_values[0] = _mm_mul_ps(m_values[0], _mm_set1_ps(other));
result.m_values[1] = _mm_mul_ps(m_values[1], _mm_set1_ps(other));
return result;
}
inline Float operator/(const Float &other) const
{
Float result;
result.m_values[0] = _mm_div_ps(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_div_ps(m_values[1], other.m_values[1]);
return result;
}
inline Float operator/(float other) const
{
Float result;
result.m_values[0] = _mm_div_ps(m_values[0], _mm_set1_ps(other));
result.m_values[1] = _mm_div_ps(m_values[1], _mm_set1_ps(other));
return result;
}
};
struct Int16CompFlag
{
__m128i m_value;
inline Int16CompFlag operator&(const Int16CompFlag &other) const
{
Int16CompFlag result;
result.m_value = _mm_and_si128(m_value, other.m_value);
return result;
}
inline Int16CompFlag operator|(const Int16CompFlag &other) const
{
Int16CompFlag result;
result.m_value = _mm_or_si128(m_value, other.m_value);
return result;
}
};
struct Int32CompFlag
{
__m128i m_values[2];
inline Int32CompFlag operator&(const Int32CompFlag &other) const
{
Int32CompFlag result;
result.m_values[0] = _mm_and_si128(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_and_si128(m_values[1], other.m_values[1]);
return result;
}
inline Int32CompFlag operator|(const Int32CompFlag &other) const
{
Int32CompFlag result;
result.m_values[0] = _mm_or_si128(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_or_si128(m_values[1], other.m_values[1]);
return result;
}
};
struct FloatCompFlag
{
__m128 m_values[2];
inline FloatCompFlag operator&(const FloatCompFlag &other) const
{
FloatCompFlag result;
result.m_values[0] = _mm_and_ps(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_and_ps(m_values[1], other.m_values[1]);
return result;
}
inline FloatCompFlag operator|(const FloatCompFlag &other) const
{
FloatCompFlag result;
result.m_values[0] = _mm_or_ps(m_values[0], other.m_values[0]);
result.m_values[1] = _mm_or_ps(m_values[1], other.m_values[1]);
return result;
}
};
template<int TSubtype>
static VInt16<TSubtype> AbstractAdd(const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
{
VInt16<TSubtype> result;
result.m_value = _mm_add_epi16(a.m_value, b.m_value);
return result;
}
template<int TSubtype>
static VInt16<TSubtype> AbstractSubtract(const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
{
VInt16<TSubtype> result;
result.m_value = _mm_sub_epi16(a.m_value, b.m_value);
return result;
}
static Float Select(const FloatCompFlag &flag, const Float &a, const Float &b)
{
Float result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_or_ps(_mm_and_ps(flag.m_values[i], a.m_values[i]), _mm_andnot_ps(flag.m_values[i], b.m_values[i]));
return result;
}
template<int TSubtype>
static VInt16<TSubtype> Select(const Int16CompFlag &flag, const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
{
VInt16<TSubtype> result;
result.m_value = _mm_or_si128(_mm_and_si128(flag.m_value, a.m_value), _mm_andnot_si128(flag.m_value, b.m_value));
return result;
}
template<int TSubtype>
static VInt16<TSubtype> SelectOrZero(const Int16CompFlag &flag, const VInt16<TSubtype> &a)
{
VInt16<TSubtype> result;
result.m_value = _mm_and_si128(flag.m_value, a.m_value);
return result;
}
template<int TSubtype>
static void ConditionalSet(VInt16<TSubtype> &dest, const Int16CompFlag &flag, const VInt16<TSubtype> &src)
{
dest.m_value = _mm_or_si128(_mm_andnot_si128(flag.m_value, dest.m_value), _mm_and_si128(flag.m_value, src.m_value));
}
template<int TSubtype>
static void ConditionalSet(VInt32<TSubtype> &dest, const Int16CompFlag &flag, const VInt32<TSubtype> &src)
{
__m128i lowFlags = _mm_unpacklo_epi16(flag.m_value, flag.m_value);
__m128i highFlags = _mm_unpackhi_epi16(flag.m_value, flag.m_value);
dest.m_values[0] = _mm_or_si128(_mm_andnot_si128(lowFlags, dest.m_values[0]), _mm_and_si128(lowFlags, src.m_values[0]));
dest.m_values[1] = _mm_or_si128(_mm_andnot_si128(highFlags, dest.m_values[1]), _mm_and_si128(highFlags, src.m_values[1]));
}
static void ConditionalSet(ParallelMath::Int16CompFlag &dest, const Int16CompFlag &flag, const ParallelMath::Int16CompFlag &src)
{
dest.m_value = _mm_or_si128(_mm_andnot_si128(flag.m_value, dest.m_value), _mm_and_si128(flag.m_value, src.m_value));
}
static SInt16 ConditionalNegate(const Int16CompFlag &flag, const SInt16 &v)
{
SInt16 result;
result.m_value = _mm_add_epi16(_mm_xor_si128(flag.m_value, v.m_value), _mm_srli_epi16(flag.m_value, 15));
return result;
}
template<int TSubtype>
static void NotConditionalSet(VInt16<TSubtype> &dest, const Int16CompFlag &flag, const VInt16<TSubtype> &src)
{
dest.m_value = _mm_or_si128(_mm_and_si128(flag.m_value, dest.m_value), _mm_andnot_si128(flag.m_value, src.m_value));
}
static void ConditionalSet(Float &dest, const FloatCompFlag &flag, const Float &src)
{
for (int i = 0; i < 2; i++)
dest.m_values[i] = _mm_or_ps(_mm_andnot_ps(flag.m_values[i], dest.m_values[i]), _mm_and_ps(flag.m_values[i], src.m_values[i]));
}
static void NotConditionalSet(Float &dest, const FloatCompFlag &flag, const Float &src)
{
for (int i = 0; i < 2; i++)
dest.m_values[i] = _mm_or_ps(_mm_and_ps(flag.m_values[i], dest.m_values[i]), _mm_andnot_ps(flag.m_values[i], src.m_values[i]));
}
static void MakeSafeDenominator(Float& v)
{
ConditionalSet(v, Equal(v, MakeFloatZero()), MakeFloat(1.0f));
}
static SInt16 TruncateToPrecisionSigned(const SInt16 &v, int precision)
{
int lostBits = 16 - precision;
if (lostBits == 0)
return v;
SInt16 result;
result.m_value = _mm_srai_epi16(_mm_slli_epi16(v.m_value, lostBits), lostBits);
return result;
}
static UInt16 TruncateToPrecisionUnsigned(const UInt16 &v, int precision)
{
int lostBits = 16 - precision;
if (lostBits == 0)
return v;
UInt16 result;
result.m_value = _mm_srli_epi16(_mm_slli_epi16(v.m_value, lostBits), lostBits);
return result;
}
static UInt16 Min(const UInt16 &a, const UInt16 &b)
{
__m128i bitFlip = _mm_set1_epi16(-32768);
UInt16 result;
result.m_value = _mm_xor_si128(_mm_min_epi16(_mm_xor_si128(a.m_value, bitFlip), _mm_xor_si128(b.m_value, bitFlip)), bitFlip);
return result;
}
static SInt16 Min(const SInt16 &a, const SInt16 &b)
{
SInt16 result;
result.m_value = _mm_min_epi16(a.m_value, b.m_value);
return result;
}
static UInt15 Min(const UInt15 &a, const UInt15 &b)
{
UInt15 result;
result.m_value = _mm_min_epi16(a.m_value, b.m_value);
return result;
}
static Float Min(const Float &a, const Float &b)
{
Float result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_min_ps(a.m_values[i], b.m_values[i]);
return result;
}
static UInt16 Max(const UInt16 &a, const UInt16 &b)
{
__m128i bitFlip = _mm_set1_epi16(-32768);
UInt16 result;
result.m_value = _mm_xor_si128(_mm_max_epi16(_mm_xor_si128(a.m_value, bitFlip), _mm_xor_si128(b.m_value, bitFlip)), bitFlip);
return result;
}
static SInt16 Max(const SInt16 &a, const SInt16 &b)
{
SInt16 result;
result.m_value = _mm_max_epi16(a.m_value, b.m_value);
return result;
}
static UInt15 Max(const UInt15 &a, const UInt15 &b)
{
UInt15 result;
result.m_value = _mm_max_epi16(a.m_value, b.m_value);
return result;
}
static Float Max(const Float &a, const Float &b)
{
Float result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_max_ps(a.m_values[i], b.m_values[i]);
return result;
}
static Float Clamp(const Float &v, float min, float max)
{
Float result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_max_ps(_mm_min_ps(v.m_values[i], _mm_set1_ps(max)), _mm_set1_ps(min));
return result;
}
static Float Reciprocal(const Float &v)
{
Float result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_rcp_ps(v.m_values[i]);
return result;
}
static void ConvertLDRInputs(const PixelBlockU8* inputBlocks, int pxOffset, int channel, UInt15 &chOut)
{
int16_t values[8];
for (int i = 0; i < 8; i++)
values[i] = inputBlocks[i].m_pixels[pxOffset][channel];
chOut.m_value = _mm_set_epi16(values[7], values[6], values[5], values[4], values[3], values[2], values[1], values[0]);
}
static void ConvertHDRInputs(const PixelBlockF16* inputBlocks, int pxOffset, int channel, SInt16 &chOut)
{
int16_t values[8];
for (int i = 0; i < 8; i++)
values[i] = inputBlocks[i].m_pixels[pxOffset][channel];
chOut.m_value = _mm_set_epi16(values[7], values[6], values[5], values[4], values[3], values[2], values[1], values[0]);
}
static Float MakeFloat(float v)
{
Float f;
f.m_values[0] = f.m_values[1] = _mm_set1_ps(v);
return f;
}
static Float MakeFloatZero()
{
Float f;
f.m_values[0] = f.m_values[1] = _mm_setzero_ps();
return f;
}
static UInt16 MakeUInt16(uint16_t v)
{
UInt16 result;
result.m_value = _mm_set1_epi16(static_cast<short>(v));
return result;
}
static SInt16 MakeSInt16(int16_t v)
{
SInt16 result;
result.m_value = _mm_set1_epi16(static_cast<short>(v));
return result;
}
static AInt16 MakeAInt16(int16_t v)
{
AInt16 result;
result.m_value = _mm_set1_epi16(static_cast<short>(v));
return result;
}
static UInt15 MakeUInt15(uint16_t v)
{
UInt15 result;
result.m_value = _mm_set1_epi16(static_cast<short>(v));
return result;
}
static SInt32 MakeSInt32(int32_t v)
{
SInt32 result;
result.m_values[0] = _mm_set1_epi32(v);
result.m_values[1] = _mm_set1_epi32(v);
return result;
}
static UInt31 MakeUInt31(uint32_t v)
{
UInt31 result;
result.m_values[0] = _mm_set1_epi32(v);
result.m_values[1] = _mm_set1_epi32(v);
return result;
}
static uint16_t Extract(const UInt16 &v, int offset)
{
return reinterpret_cast<const uint16_t*>(&v.m_value)[offset];
}
static int16_t Extract(const SInt16 &v, int offset)
{
return reinterpret_cast<const int16_t*>(&v.m_value)[offset];
}
static uint16_t Extract(const UInt15 &v, int offset)
{
return reinterpret_cast<const uint16_t*>(&v.m_value)[offset];
}
static int16_t Extract(const AInt16 &v, int offset)
{
return reinterpret_cast<const int16_t*>(&v.m_value)[offset];
}
static int32_t Extract(const SInt32 &v, int offset)
{
return reinterpret_cast<const int32_t*>(&v.m_values[offset >> 2])[offset & 3];
}
static float Extract(const Float &v, int offset)
{
return reinterpret_cast<const float*>(&v.m_values[offset >> 2])[offset & 3];
}
static bool Extract(const ParallelMath::Int16CompFlag &v, int offset)
{
return reinterpret_cast<const int16_t*>(&v.m_value)[offset] != 0;
}
static void PutUInt16(UInt16 &dest, int offset, uint16_t v)
{
reinterpret_cast<uint16_t*>(&dest)[offset] = v;
}
static void PutUInt15(UInt15 &dest, int offset, uint16_t v)
{
reinterpret_cast<uint16_t*>(&dest)[offset] = v;
}
static void PutSInt16(SInt16 &dest, int offset, int16_t v)
{
reinterpret_cast<int16_t*>(&dest)[offset] = v;
}
static float ExtractFloat(const Float& v, int offset)
{
return reinterpret_cast<const float*>(&v)[offset];
}
static void PutFloat(Float &dest, int offset, float v)
{
reinterpret_cast<float*>(&dest)[offset] = v;
}
static void PutBoolInt16(Int16CompFlag &dest, int offset, bool v)
{
reinterpret_cast<int16_t*>(&dest)[offset] = v ? -1 : 0;
}
static Int32CompFlag Less(const UInt31 &a, const UInt31 &b)
{
Int32CompFlag result;
result.m_values[0] = _mm_cmplt_epi32(a.m_values[0], b.m_values[0]);
result.m_values[1] = _mm_cmplt_epi32(a.m_values[1], b.m_values[1]);
return result;
}
static Int16CompFlag Less(const SInt16 &a, const SInt16 &b)
{
Int16CompFlag result;
result.m_value = _mm_cmplt_epi16(a.m_value, b.m_value);
return result;
}
static Int16CompFlag Less(const UInt15 &a, const UInt15 &b)
{
Int16CompFlag result;
result.m_value = _mm_cmplt_epi16(a.m_value, b.m_value);
return result;
}
static Int16CompFlag LessOrEqual(const UInt15 &a, const UInt15 &b)
{
Int16CompFlag result;
result.m_value = _mm_cmplt_epi16(a.m_value, b.m_value);
return result;
}
static FloatCompFlag Less(const Float &a, const Float &b)
{
FloatCompFlag result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_cmplt_ps(a.m_values[i], b.m_values[i]);
return result;
}
static FloatCompFlag LessOrEqual(const Float &a, const Float &b)
{
FloatCompFlag result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_cmple_ps(a.m_values[i], b.m_values[i]);
return result;
}
template<int TSubtype>
static Int16CompFlag Equal(const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
{
Int16CompFlag result;
result.m_value = _mm_cmpeq_epi16(a.m_value, b.m_value);
return result;
}
static FloatCompFlag Equal(const Float &a, const Float &b)
{
FloatCompFlag result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_cmpeq_ps(a.m_values[i], b.m_values[i]);
return result;
}
static Int16CompFlag Equal(const Int16CompFlag &a, const Int16CompFlag &b)
{
Int16CompFlag notResult;
notResult.m_value = _mm_xor_si128(a.m_value, b.m_value);
return Not(notResult);
}
static Float ToFloat(const UInt16 &v)
{
Float result;
result.m_values[0] = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v.m_value, _mm_setzero_si128()));
result.m_values[1] = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v.m_value, _mm_setzero_si128()));
return result;
}
static UInt31 ToUInt31(const UInt16 &v)
{
UInt31 result;
result.m_values[0] = _mm_unpacklo_epi16(v.m_value, _mm_setzero_si128());
result.m_values[1] = _mm_unpackhi_epi16(v.m_value, _mm_setzero_si128());
return result;
}
static SInt32 ToInt32(const UInt16 &v)
{
SInt32 result;
result.m_values[0] = _mm_unpacklo_epi16(v.m_value, _mm_setzero_si128());
result.m_values[1] = _mm_unpackhi_epi16(v.m_value, _mm_setzero_si128());
return result;
}
static SInt32 ToInt32(const UInt15 &v)
{
SInt32 result;
result.m_values[0] = _mm_unpacklo_epi16(v.m_value, _mm_setzero_si128());
result.m_values[1] = _mm_unpackhi_epi16(v.m_value, _mm_setzero_si128());
return result;
}
static SInt32 ToInt32(const SInt16 &v)
{
SInt32 result;
result.m_values[0] = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), v.m_value), 16);
result.m_values[1] = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), v.m_value), 16);
return result;
}
static Float ToFloat(const SInt16 &v)
{
Float result;
result.m_values[0] = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), v.m_value), 16));
result.m_values[1] = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), v.m_value), 16));
return result;
}
static Float ToFloat(const UInt15 &v)
{
Float result;
result.m_values[0] = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v.m_value, _mm_setzero_si128()));
result.m_values[1] = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v.m_value, _mm_setzero_si128()));
return result;
}
static Float ToFloat(const UInt31 &v)
{
Float result;
result.m_values[0] = _mm_cvtepi32_ps(v.m_values[0]);
result.m_values[1] = _mm_cvtepi32_ps(v.m_values[1]);
return result;
}
static Int16CompFlag FloatFlagToInt16(const FloatCompFlag &v)
{
__m128i lo = _mm_castps_si128(v.m_values[0]);
__m128i hi = _mm_castps_si128(v.m_values[1]);
Int16CompFlag result;
result.m_value = _mm_packs_epi32(lo, hi);
return result;
}
static FloatCompFlag Int16FlagToFloat(const Int16CompFlag &v)
{
__m128i lo = _mm_unpacklo_epi16(v.m_value, v.m_value);
__m128i hi = _mm_unpackhi_epi16(v.m_value, v.m_value);
FloatCompFlag result;
result.m_values[0] = _mm_castsi128_ps(lo);
result.m_values[1] = _mm_castsi128_ps(hi);
return result;
}
static Int16CompFlag Int32FlagToInt16(const Int32CompFlag &v)
{
__m128i lo = v.m_values[0];
__m128i hi = v.m_values[1];
Int16CompFlag result;
result.m_value = _mm_packs_epi32(lo, hi);
return result;
}
static Int16CompFlag MakeBoolInt16(bool b)
{
Int16CompFlag result;
if (b)
result.m_value = _mm_set1_epi16(-1);
else
result.m_value = _mm_setzero_si128();
return result;
}
static FloatCompFlag MakeBoolFloat(bool b)
{
FloatCompFlag result;
if (b)
result.m_values[0] = result.m_values[1] = _mm_castsi128_ps(_mm_set1_epi32(-1));
else
result.m_values[0] = result.m_values[1] = _mm_setzero_ps();
return result;
}
static Int16CompFlag AndNot(const Int16CompFlag &a, const Int16CompFlag &b)
{
Int16CompFlag result;
result.m_value = _mm_andnot_si128(b.m_value, a.m_value);
return result;
}
static Int16CompFlag Not(const Int16CompFlag &b)
{
Int16CompFlag result;
result.m_value = _mm_xor_si128(b.m_value, _mm_set1_epi32(-1));
return result;
}
static Int32CompFlag Not(const Int32CompFlag &b)
{
Int32CompFlag result;
result.m_values[0] = _mm_xor_si128(b.m_values[0], _mm_set1_epi32(-1));
result.m_values[1] = _mm_xor_si128(b.m_values[1], _mm_set1_epi32(-1));
return result;
}
static UInt16 RoundAndConvertToU16(const Float &v, const void* /*roundingMode*/)
{
__m128i lo = _mm_cvtps_epi32(_mm_add_ps(v.m_values[0], _mm_set1_ps(-32768)));
__m128i hi = _mm_cvtps_epi32(_mm_add_ps(v.m_values[1], _mm_set1_ps(-32768)));
__m128i packed = _mm_packs_epi32(lo, hi);
UInt16 result;
result.m_value = _mm_xor_si128(packed, _mm_set1_epi16(-32768));
return result;
}
static UInt15 RoundAndConvertToU15(const Float &v, const void* /*roundingMode*/)
{
__m128i lo = _mm_cvtps_epi32(v.m_values[0]);
__m128i hi = _mm_cvtps_epi32(v.m_values[1]);
__m128i packed = _mm_packs_epi32(lo, hi);
UInt15 result;
result.m_value = _mm_packs_epi32(lo, hi);
return result;
}
static SInt16 RoundAndConvertToS16(const Float &v, const void* /*roundingMode*/)
{
__m128i lo = _mm_cvtps_epi32(v.m_values[0]);
__m128i hi = _mm_cvtps_epi32(v.m_values[1]);
__m128i packed = _mm_packs_epi32(lo, hi);
SInt16 result;
result.m_value = _mm_packs_epi32(lo, hi);
return result;
}
static Float Sqrt(const Float &f)
{
Float result;
for (int i = 0; i < 2; i++)
result.m_values[i] = _mm_sqrt_ps(f.m_values[i]);
return result;
}
static UInt16 Abs(const SInt16 &a)
{
__m128i signBitsXor = _mm_srai_epi16(a.m_value, 15);
__m128i signBitsAdd = _mm_srli_epi16(a.m_value, 15);
UInt16 result;
result.m_value = _mm_add_epi16(_mm_xor_si128(a.m_value, signBitsXor), signBitsAdd);
return result;
}
static Float Abs(const Float& a)
{
__m128 invMask = _mm_set1_ps(-0.0f);
Float result;
result.m_values[0] = _mm_andnot_ps(invMask, a.m_values[0]);
result.m_values[1] = _mm_andnot_ps(invMask, a.m_values[1]);
return result;
}
static UInt16 SqDiffUInt8(const UInt15 &a, const UInt15 &b)
{
__m128i diff = _mm_sub_epi16(a.m_value, b.m_value);
UInt16 result;
result.m_value = _mm_mullo_epi16(diff, diff);
return result;
}
static Float SqDiffSInt16(const SInt16 &a, const SInt16 &b)
{
__m128i diffU = _mm_sub_epi16(_mm_max_epi16(a.m_value, b.m_value), _mm_min_epi16(a.m_value, b.m_value));
__m128i mulHi = _mm_mulhi_epu16(diffU, diffU);