/
simdarray.h
2715 lines (2521 loc) · 124 KB
/
simdarray.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
/* This file is part of the Vc library. {{{
Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the names of contributing organizations nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}}}*/
#ifndef VC_COMMON_SIMDARRAY_H_
#define VC_COMMON_SIMDARRAY_H_
//#define Vc_DEBUG_SIMD_CAST 1
//#define Vc_DEBUG_SORTED 1
//#include "../IO"
#include <array>
#include "writemaskedvector.h"
#include "simdarrayhelper.h"
#include "simdmaskarray.h"
#include "utility.h"
#include "interleave.h"
#include "indexsequence.h"
#include "transpose.h"
#include "macros.h"
namespace Vc_VERSIONED_NAMESPACE
{
// select_best_vector_type {{{
namespace Common
{
/// \addtogroup SimdArray
/// @{
/**
* \internal
* Selects the best SIMD type out of a typelist to store N scalar values.
*/
template <std::size_t N, class... Candidates> struct select_best_vector_type_impl;
// last candidate; this one must work; assume it does:
template <std::size_t N, class T> struct select_best_vector_type_impl<N, T> {
using type = T;
};
// check the next candidate; use it if N >= T::size(); recurse otherwise:
template <std::size_t N, class T, class... Candidates>
struct select_best_vector_type_impl<N, T, Candidates...> {
using type = typename std::conditional<
(N < T::Size), typename select_best_vector_type_impl<N, Candidates...>::type,
T>::type;
};
template <class T, std::size_t N>
struct select_best_vector_type : select_best_vector_type_impl<N,
#ifdef Vc_IMPL_AVX2
Vc::AVX2::Vector<T>,
#elif defined Vc_IMPL_AVX
Vc::AVX::Vector<T>,
#endif
#ifdef Vc_IMPL_SSE
Vc::SSE::Vector<T>,
#endif
Vc::Scalar::Vector<T>> {
};
/// @}
} // namespace Common
// }}}
// internal namespace (product & sum helper) {{{1
namespace internal
{
template <typename T> T Vc_INTRINSIC Vc_PURE product_helper_(const T &l, const T &r) { return l * r; }
template <typename T> T Vc_INTRINSIC Vc_PURE sum_helper_(const T &l, const T &r) { return l + r; }
} // namespace internal
// min & max declarations {{{1
template <typename T, std::size_t N, typename V, std::size_t M>
inline SimdArray<T, N, V, M> min(const SimdArray<T, N, V, M> &x,
const SimdArray<T, N, V, M> &y);
template <typename T, std::size_t N, typename V, std::size_t M>
inline SimdArray<T, N, V, M> max(const SimdArray<T, N, V, M> &x,
const SimdArray<T, N, V, M> &y);
// SimdArray class {{{1
/// \addtogroup SimdArray
/// @{
// atomic SimdArray {{{1
#define Vc_CURRENT_CLASS_NAME SimdArray
/**\internal
* Specialization of `SimdArray<T, N, VectorType, VectorSize>` for the case where `N ==
* VectorSize`.
*
* This is specialized for implementation purposes: Since the general implementation uses
* two SimdArray data members it recurses over different SimdArray instantiations. The
* recursion is ended by this specialization, which has a single \p VectorType_ data
* member to which all functions are forwarded more or less directly.
*/
template <typename T, std::size_t N, typename VectorType_>
class SimdArray<T, N, VectorType_, N>
{
static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value ||
std::is_same<T, int32_t>::value ||
std::is_same<T, uint32_t>::value ||
std::is_same<T, int16_t>::value ||
std::is_same<T, uint16_t>::value,
"SimdArray<T, N> may only be used with T = { double, float, int32_t, uint32_t, "
"int16_t, uint16_t }");
public:
using VectorType = VectorType_;
using vector_type = VectorType;
using storage_type = vector_type;
using vectorentry_type = typename vector_type::VectorEntryType;
using value_type = T;
using mask_type = SimdMaskArray<T, N, vector_type>;
using index_type = SimdArray<int, N>;
static constexpr std::size_t size() { return N; }
using Mask = mask_type;
using MaskType = Mask;
using MaskArgument = const MaskType &;
using VectorEntryType = vectorentry_type;
using EntryType = value_type;
using IndexType = index_type;
using AsArg = const SimdArray &;
using reference = Detail::ElementReference<SimdArray>;
static constexpr std::size_t Size = size();
static constexpr std::size_t MemoryAlignment = storage_type::MemoryAlignment;
// zero init
Vc_INTRINSIC SimdArray() = default;
// default copy ctor/operator
Vc_INTRINSIC SimdArray(const SimdArray &) = default;
Vc_INTRINSIC SimdArray(SimdArray &&) = default;
Vc_INTRINSIC SimdArray &operator=(const SimdArray &) = default;
// broadcast
Vc_INTRINSIC SimdArray(const value_type &a) : data(a) {}
Vc_INTRINSIC SimdArray(value_type &a) : data(a) {}
Vc_INTRINSIC SimdArray(value_type &&a) : data(a) {}
template <
typename U,
typename = enable_if<std::is_same<U, int>::value && !std::is_same<int, value_type>::value>>
Vc_INTRINSIC SimdArray(U a)
: SimdArray(static_cast<value_type>(a))
{
}
// implicit casts
template <class U, class V, class = enable_if<N == V::Size>>
Vc_INTRINSIC SimdArray(const SimdArray<U, N, V> &x)
: data(simd_cast<vector_type>(internal_data(x)))
{
}
template <class U, class V, class = enable_if<(N > V::Size && N <= 2 * V::Size)>,
class = U>
Vc_INTRINSIC SimdArray(const SimdArray<U, N, V> &x)
: data(simd_cast<vector_type>(internal_data(internal_data0(x)),
internal_data(internal_data1(x))))
{
}
template <class U, class V, class = enable_if<(N > 2 * V::Size && N <= 4 * V::Size)>,
class = U, class = U>
Vc_INTRINSIC SimdArray(const SimdArray<U, N, V> &x)
: data(simd_cast<vector_type>(internal_data(internal_data0(internal_data0(x))),
internal_data(internal_data1(internal_data0(x))),
internal_data(internal_data0(internal_data1(x))),
internal_data(internal_data1(internal_data1(x)))))
{
}
template <typename V, std::size_t Pieces, std::size_t Index>
Vc_INTRINSIC SimdArray(Common::Segment<V, Pieces, Index> &&x)
: data(simd_cast<vector_type, Index>(x.data))
{
}
Vc_INTRINSIC SimdArray(const std::initializer_list<value_type> &init)
: data(init.begin(), Vc::Unaligned)
{
#if defined Vc_CXX14 && 0 // doesn't compile yet
static_assert(init.size() == size(), "The initializer_list argument to "
"SimdArray<T, N> must contain exactly N "
"values.");
#else
Vc_ASSERT(init.size() == size());
#endif
}
// implicit conversion from underlying vector_type
template <
typename V,
typename = enable_if<Traits::is_simd_vector<V>::value && !Traits::isSimdArray<V>::value>>
Vc_INTRINSIC SimdArray(const V &x)
: data(simd_cast<vector_type>(x))
{
}
// implicit conversion to Vector<U, AnyAbi> for if Vector<U, AnyAbi>::size() == N and
// T implicitly convertible to U
template <typename U, typename A,
typename =
enable_if<std::is_convertible<T, U>::value && Vector<U, A>::Size == N &&
!std::is_same<A, simd_abi::fixed_size<N>>::value>>
Vc_INTRINSIC operator Vector<U, A>() const
{
return simd_cast<Vector<U, A>>(data);
}
operator fixed_size_simd<T, N>() const
{
return static_cast<fixed_size_simd<T, N>>(data);
}
#include "gatherinterface.h"
#include "scatterinterface.h"
// forward all remaining ctors
template <typename... Args,
typename = enable_if<!Traits::is_cast_arguments<Args...>::value &&
!Traits::is_gather_signature<Args...>::value &&
!Traits::is_initializer_list<Args...>::value>>
explicit Vc_INTRINSIC SimdArray(Args &&... args)
: data(std::forward<Args>(args)...)
{
}
template <std::size_t Offset>
explicit Vc_INTRINSIC SimdArray(
Common::AddOffset<VectorSpecialInitializerIndexesFromZero, Offset>)
: data(Vc::IndexesFromZero)
{
data += value_type(Offset);
}
Vc_INTRINSIC void setZero() { data.setZero(); }
Vc_INTRINSIC void setZero(mask_type k) { data.setZero(internal_data(k)); }
Vc_INTRINSIC void setZeroInverted() { data.setZeroInverted(); }
Vc_INTRINSIC void setZeroInverted(mask_type k) { data.setZeroInverted(internal_data(k)); }
Vc_INTRINSIC void setQnan() { data.setQnan(); }
Vc_INTRINSIC void setQnan(mask_type m) { data.setQnan(internal_data(m)); }
// internal: execute specified Operation
template <typename Op, typename... Args>
static Vc_INTRINSIC SimdArray fromOperation(Op op, Args &&... args)
{
SimdArray r;
Common::unpackArgumentsAuto(op, r.data, std::forward<Args>(args)...);
return r;
}
template <typename Op, typename... Args>
static Vc_INTRINSIC void callOperation(Op op, Args &&... args)
{
Common::unpackArgumentsAuto(op, nullptr, std::forward<Args>(args)...);
}
static Vc_INTRINSIC SimdArray Zero()
{
return SimdArray(Vc::Zero);
}
static Vc_INTRINSIC SimdArray One()
{
return SimdArray(Vc::One);
}
static Vc_INTRINSIC SimdArray IndexesFromZero()
{
return SimdArray(Vc::IndexesFromZero);
}
static Vc_INTRINSIC SimdArray Random()
{
return fromOperation(Common::Operations::random());
}
template <typename... Args> Vc_INTRINSIC void load(Args &&... args)
{
data.load(std::forward<Args>(args)...);
}
template <typename... Args> Vc_INTRINSIC void store(Args &&... args) const
{
data.store(std::forward<Args>(args)...);
}
Vc_INTRINSIC mask_type operator!() const
{
return {private_init, !data};
}
Vc_INTRINSIC SimdArray operator-() const
{
return {private_init, -data};
}
/// Returns a copy of itself
Vc_INTRINSIC SimdArray operator+() const { return *this; }
Vc_INTRINSIC SimdArray operator~() const
{
return {private_init, ~data};
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC Vc_CONST SimdArray operator<<(U x) const
{
return {private_init, data << x};
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC SimdArray &operator<<=(U x)
{
data <<= x;
return *this;
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC Vc_CONST SimdArray operator>>(U x) const
{
return {private_init, data >> x};
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC SimdArray &operator>>=(U x)
{
data >>= x;
return *this;
}
#define Vc_BINARY_OPERATOR_(op) \
Vc_INTRINSIC Vc_CONST SimdArray operator op(const SimdArray &rhs) const \
{ \
return {private_init, data op rhs.data}; \
} \
Vc_INTRINSIC SimdArray &operator op##=(const SimdArray &rhs) \
{ \
data op## = rhs.data; \
return *this; \
}
Vc_ALL_ARITHMETICS(Vc_BINARY_OPERATOR_);
Vc_ALL_BINARY(Vc_BINARY_OPERATOR_);
Vc_ALL_SHIFTS(Vc_BINARY_OPERATOR_);
#undef Vc_BINARY_OPERATOR_
#define Vc_COMPARES(op) \
Vc_INTRINSIC mask_type operator op(const SimdArray &rhs) const \
{ \
return {private_init, data op rhs.data}; \
}
Vc_ALL_COMPARES(Vc_COMPARES);
#undef Vc_COMPARES
/// \copydoc Vector::isNegative
Vc_DEPRECATED("use isnegative(x) instead") Vc_INTRINSIC MaskType isNegative() const
{
return {private_init, isnegative(data)};
}
private:
friend reference;
Vc_INTRINSIC static value_type get(const SimdArray &o, int i) noexcept
{
return o.data[i];
}
template <typename U>
Vc_INTRINSIC static void set(SimdArray &o, int i, U &&v) noexcept(
noexcept(std::declval<value_type &>() = v))
{
o.data[i] = v;
}
public:
/**
* \note the returned object models the concept of a reference and
* as such it can exist longer than the data it is referencing.
* \note to avoid lifetime issues, we strongly advice not to store
* any reference objects.
*/
Vc_INTRINSIC reference operator[](size_t i) noexcept
{
static_assert(noexcept(reference{std::declval<SimdArray &>(), int()}), "");
return {*this, int(i)};
}
Vc_INTRINSIC value_type operator[](size_t i) const noexcept
{
return get(*this, int(i));
}
Vc_INTRINSIC Common::WriteMaskedVector<SimdArray, mask_type> operator()(const mask_type &k)
{
return {*this, k};
}
Vc_INTRINSIC void assign(const SimdArray &v, const mask_type &k)
{
data.assign(v.data, internal_data(k));
}
// reductions ////////////////////////////////////////////////////////
#define Vc_REDUCTION_FUNCTION_(name_) \
Vc_INTRINSIC Vc_PURE value_type name_() const { return data.name_(); } \
Vc_INTRINSIC Vc_PURE value_type name_(mask_type mask) const \
{ \
return data.name_(internal_data(mask)); \
} \
Vc_NOTHING_EXPECTING_SEMICOLON
Vc_REDUCTION_FUNCTION_(min);
Vc_REDUCTION_FUNCTION_(max);
Vc_REDUCTION_FUNCTION_(product);
Vc_REDUCTION_FUNCTION_(sum);
#undef Vc_REDUCTION_FUNCTION_
Vc_INTRINSIC Vc_PURE SimdArray partialSum() const
{
return {private_init, data.partialSum()};
}
template <typename F> Vc_INTRINSIC SimdArray apply(F &&f) const
{
return {private_init, data.apply(std::forward<F>(f))};
}
template <typename F> Vc_INTRINSIC SimdArray apply(F &&f, const mask_type &k) const
{
return {private_init, data.apply(std::forward<F>(f), k)};
}
Vc_INTRINSIC SimdArray shifted(int amount) const
{
return {private_init, data.shifted(amount)};
}
template <std::size_t NN>
Vc_INTRINSIC SimdArray shifted(int amount, const SimdArray<value_type, NN> &shiftIn)
const
{
return {private_init, data.shifted(amount, simd_cast<VectorType>(shiftIn))};
}
Vc_INTRINSIC SimdArray rotated(int amount) const
{
return {private_init, data.rotated(amount)};
}
/// \copydoc Vector::exponent
Vc_DEPRECATED("use exponent(x) instead") Vc_INTRINSIC SimdArray exponent() const
{
return {private_init, exponent(data)};
}
Vc_INTRINSIC SimdArray interleaveLow(SimdArray x) const
{
return {private_init, data.interleaveLow(x.data)};
}
Vc_INTRINSIC SimdArray interleaveHigh(SimdArray x) const
{
return {private_init, data.interleaveHigh(x.data)};
}
Vc_INTRINSIC SimdArray reversed() const
{
return {private_init, data.reversed()};
}
Vc_INTRINSIC SimdArray sorted() const
{
return {private_init, data.sorted()};
}
template <typename G> static Vc_INTRINSIC SimdArray generate(const G &gen)
{
return {private_init, VectorType::generate(gen)};
}
Vc_DEPRECATED("use copysign(x, y) instead") Vc_INTRINSIC SimdArray
copySign(const SimdArray &x) const
{
return {private_init, Vc::copysign(data, x.data)};
}
friend VectorType &internal_data<>(SimdArray &x);
friend const VectorType &internal_data<>(const SimdArray &x);
/// \internal
Vc_INTRINSIC SimdArray(private_init_t, VectorType &&x) : data(std::move(x)) {}
Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(storage_type));
private:
// The alignas attribute attached to the class declaration above is ignored by ICC
// 17.0.0 (at least). So just move the alignas attribute down here where it works for
// all compilers.
alignas(static_cast<std::size_t>(
Common::BoundedAlignment<Common::NextPowerOfTwo<N>::value * sizeof(VectorType_) /
VectorType_::size()>::value)) storage_type data;
};
template <typename T, std::size_t N, typename VectorType> constexpr std::size_t SimdArray<T, N, VectorType, N>::Size;
template <typename T, std::size_t N, typename VectorType>
constexpr std::size_t SimdArray<T, N, VectorType, N>::MemoryAlignment;
template <typename T, std::size_t N, typename VectorType>
#ifndef Vc_MSVC
Vc_INTRINSIC
#endif
VectorType &internal_data(SimdArray<T, N, VectorType, N> &x)
{
return x.data;
}
template <typename T, std::size_t N, typename VectorType>
#ifndef Vc_MSVC
Vc_INTRINSIC
#endif
const VectorType &internal_data(const SimdArray<T, N, VectorType, N> &x)
{
return x.data;
}
// unpackIfSegment {{{2
template <typename T> T unpackIfSegment(T &&x) { return std::forward<T>(x); }
template <typename T, size_t Pieces, size_t Index>
auto unpackIfSegment(Common::Segment<T, Pieces, Index> &&x) -> decltype(x.asSimdArray())
{
return x.asSimdArray();
}
// gatherImplementation {{{2
template <typename T, std::size_t N, typename VectorType>
template <typename MT, typename IT>
inline void SimdArray<T, N, VectorType, N>::gatherImplementation(const MT *mem,
const IT &indexes)
{
data.gather(mem, unpackIfSegment(indexes));
}
template <typename T, std::size_t N, typename VectorType>
template <typename MT, typename IT>
inline void SimdArray<T, N, VectorType, N>::gatherImplementation(const MT *mem,
const IT &indexes,
MaskArgument mask)
{
data.gather(mem, unpackIfSegment(indexes), mask);
}
// scatterImplementation {{{2
template <typename T, std::size_t N, typename VectorType>
template <typename MT, typename IT>
inline void SimdArray<T, N, VectorType, N>::scatterImplementation(MT *mem,
IT &&indexes) const
{
data.scatter(mem, unpackIfSegment(std::forward<IT>(indexes)));
}
template <typename T, std::size_t N, typename VectorType>
template <typename MT, typename IT>
inline void SimdArray<T, N, VectorType, N>::scatterImplementation(MT *mem,
IT &&indexes,
MaskArgument mask) const
{
data.scatter(mem, unpackIfSegment(std::forward<IT>(indexes)), mask);
}
// generic SimdArray {{{1
/**
* Data-parallel arithmetic type with user-defined number of elements.
*
* \tparam T The type of the vector's elements. The supported types currently are limited
* to the types supported by Vc::Vector<T>.
*
* \tparam N The number of elements to store and process concurrently. You can choose an
* arbitrary number, though not every number is a good idea.
* Generally, a power of two value or the sum of two power of two values might
* work efficiently, though this depends a lot on the target system.
*
* \tparam V Don't change the default value unless you really know what you are doing.
* This type is set to the underlying native Vc::Vector type used in the
* implementation of the type.
* Having it as part of the type name guards against some cases of ODR
* violations (i.e. linking incompatible translation units / libraries).
*
* \tparam Wt Don't ever change the default value.
* This parameter is an unfortunate implementation detail shining through.
*
* \warning Choosing \p N too large (what “too large” means depends on the target) will
* result in excessive compilation times and high (or too high) register
* pressure, thus potentially negating the improvement from concurrent execution.
* As a rule of thumb, keep \p N less or equal to `2 * float_v::size()`.
*
* \warning A special portability concern arises from a current limitation in the MIC
* implementation (Intel Knights Corner), where SimdArray types with \p T = \p
* (u)short require an \p N either less than short_v::size() or a multiple of
* short_v::size().
*
* \headerfile simdarray.h <Vc/SimdArray>
*/
template <typename T, size_t N, typename V, size_t Wt> class SimdArray
{
static_assert(std::is_same<T, double>::value ||
std::is_same<T, float>::value ||
std::is_same<T, int32_t>::value ||
std::is_same<T, uint32_t>::value ||
std::is_same<T, int16_t>::value ||
std::is_same<T, uint16_t>::value, "SimdArray<T, N> may only be used with T = { double, float, int32_t, uint32_t, int16_t, uint16_t }");
static_assert(
// either the EntryType and VectorEntryType of the main V are equal
std::is_same<typename V::EntryType, typename V::VectorEntryType>::value ||
// or N is a multiple of V::size()
(N % V::size() == 0),
"SimdArray<(un)signed short, N> on MIC only works correctly for N = k * "
"MIC::(u)short_v::size(), i.e. k * 16.");
using my_traits = SimdArrayTraits<T, N>;
static constexpr std::size_t N0 = my_traits::N0;
static constexpr std::size_t N1 = my_traits::N1;
using Split = Common::Split<N0>;
template <typename U, std::size_t K> using CArray = U[K];
public:
using storage_type0 = typename my_traits::storage_type0;
using storage_type1 = typename my_traits::storage_type1;
static_assert(storage_type0::size() == N0, "");
/**\internal
* This type reveals the implementation-specific type used for the data member.
*/
using vector_type = V;
using vectorentry_type = typename storage_type0::vectorentry_type;
typedef vectorentry_type alias_type Vc_MAY_ALIAS;
/// The type of the elements (i.e.\ \p T)
using value_type = T;
/// The type of the mask used for masked operations and returned from comparisons.
using mask_type = SimdMaskArray<T, N, vector_type>;
/// The type of the vector used for indexes in gather and scatter operations.
using index_type = SimdArray<int, N>;
/**
* Returns \p N, the number of scalar components in an object of this type.
*
* The size of the SimdArray, i.e. the number of scalar elements in the vector. In
* contrast to Vector::size() you have control over this value via the \p N template
* parameter of the SimdArray class template.
*
* \returns The number of scalar values stored and manipulated concurrently by objects
* of this type.
*/
static constexpr std::size_t size() { return N; }
/// \copydoc mask_type
using Mask = mask_type;
/// \copydoc mask_type
using MaskType = Mask;
using MaskArgument = const MaskType &;
using VectorEntryType = vectorentry_type;
/// \copydoc value_type
using EntryType = value_type;
/// \copydoc index_type
using IndexType = index_type;
using AsArg = const SimdArray &;
using reference = Detail::ElementReference<SimdArray>;
///\copydoc Vector::MemoryAlignment
static constexpr std::size_t MemoryAlignment =
storage_type0::MemoryAlignment > storage_type1::MemoryAlignment
? storage_type0::MemoryAlignment
: storage_type1::MemoryAlignment;
/// \name Generators
///@{
///\copybrief Vector::Zero
static Vc_INTRINSIC SimdArray Zero()
{
return SimdArray(Vc::Zero);
}
///\copybrief Vector::One
static Vc_INTRINSIC SimdArray One()
{
return SimdArray(Vc::One);
}
///\copybrief Vector::IndexesFromZero
static Vc_INTRINSIC SimdArray IndexesFromZero()
{
return SimdArray(Vc::IndexesFromZero);
}
///\copydoc Vector::Random
static Vc_INTRINSIC SimdArray Random()
{
return fromOperation(Common::Operations::random());
}
///\copybrief Vector::generate
template <typename G> static Vc_INTRINSIC SimdArray generate(const G &gen) // {{{2
{
auto tmp = storage_type0::generate(gen); // GCC bug: the order of evaluation in
// an initializer list is well-defined
// (front to back), but GCC 4.8 doesn't
// implement this correctly. Therefore
// we enforce correct order.
return {std::move(tmp),
storage_type1::generate([&](std::size_t i) { return gen(i + N0); })};
}
///@}
/// \name Compile-Time Constant Initialization
///@{
///\copydoc Vector::Vector()
SimdArray() = default;
///@}
/// \name Conversion/Broadcast Constructors
///@{
///\copydoc Vector::Vector(EntryType)
Vc_INTRINSIC SimdArray(value_type a) : data0(a), data1(a) {}
template <
typename U,
typename = enable_if<std::is_same<U, int>::value && !std::is_same<int, value_type>::value>>
SimdArray(U a)
: SimdArray(static_cast<value_type>(a))
{
}
///@}
// default copy ctor/operator
SimdArray(const SimdArray &) = default;
SimdArray(SimdArray &&) = default;
SimdArray &operator=(const SimdArray &) = default;
// load ctor
template <typename U, typename Flags = DefaultLoadTag,
typename = enable_if<std::is_arithmetic<U>::value &&
Traits::is_load_store_flag<Flags>::value>>
explicit Vc_INTRINSIC SimdArray(const U *mem, Flags f = Flags())
: data0(mem, f), data1(mem + storage_type0::size(), f)
{
}
// MSVC does overload resolution differently and takes the const U *mem overload (I hope)
#ifndef Vc_MSVC
/**\internal
* Load from a C-array. This is basically the same function as the load constructor
* above, except that the forwarding reference overload would steal the deal and the
* constructor above doesn't get called. This overload is required to enable loads
* from C-arrays.
*/
template <typename U, std::size_t Extent, typename Flags = DefaultLoadTag,
typename = enable_if<std::is_arithmetic<U>::value &&
Traits::is_load_store_flag<Flags>::value>>
explicit Vc_INTRINSIC SimdArray(CArray<U, Extent> &mem, Flags f = Flags())
: data0(&mem[0], f), data1(&mem[storage_type0::size()], f)
{
}
/**\internal
* Const overload of the above.
*/
template <typename U, std::size_t Extent, typename Flags = DefaultLoadTag,
typename = enable_if<std::is_arithmetic<U>::value &&
Traits::is_load_store_flag<Flags>::value>>
explicit Vc_INTRINSIC SimdArray(const CArray<U, Extent> &mem, Flags f = Flags())
: data0(&mem[0], f), data1(&mem[storage_type0::size()], f)
{
}
#endif
// initializer list
Vc_INTRINSIC SimdArray(const std::initializer_list<value_type> &init)
: data0(init.begin(), Vc::Unaligned)
, data1(init.begin() + storage_type0::size(), Vc::Unaligned)
{
#if defined Vc_CXX14 && 0 // doesn't compile yet
static_assert(init.size() == size(), "The initializer_list argument to "
"SimdArray<T, N> must contain exactly N "
"values.");
#else
Vc_ASSERT(init.size() == size());
#endif
}
#include "gatherinterface.h"
#include "scatterinterface.h"
// forward all remaining ctors
template <typename... Args,
typename = enable_if<!Traits::is_cast_arguments<Args...>::value &&
!Traits::is_initializer_list<Args...>::value &&
!Traits::is_gather_signature<Args...>::value &&
!Traits::is_load_arguments<Args...>::value>>
explicit Vc_INTRINSIC SimdArray(Args &&... args)
: data0(Split::lo(args)...) // no forward here - it could move and thus
// break the next line
, data1(Split::hi(std::forward<Args>(args))...)
{
}
// explicit casts
template <class W, class = enable_if<
(Traits::is_simd_vector<W>::value &&
Traits::simd_vector_size<W>::value == N &&
!(std::is_convertible<Traits::entry_type_of<W>, T>::value &&
Traits::isSimdArray<W>::value))>>
Vc_INTRINSIC explicit SimdArray(W &&x) : data0(Split::lo(x)), data1(Split::hi(x))
{
}
// implicit casts
template <class W, class = enable_if<
(Traits::isSimdArray<W>::value &&
Traits::simd_vector_size<W>::value == N &&
std::is_convertible<Traits::entry_type_of<W>, T>::value)>,
class = W>
Vc_INTRINSIC SimdArray(W &&x) : data0(Split::lo(x)), data1(Split::hi(x))
{
}
// implicit conversion to Vector<U, AnyAbi> for if Vector<U, AnyAbi>::size() == N and
// T implicitly convertible to U
template <typename U, typename A,
typename =
enable_if<std::is_convertible<T, U>::value && Vector<U, A>::Size == N &&
!std::is_same<A, simd_abi::fixed_size<N>>::value>>
operator Vector<U, A>() const
{
auto r = simd_cast<Vector<U, A>>(data0, data1);
return r;
}
Vc_INTRINSIC operator const fixed_size_simd<T, N> &() const
{
return static_cast<const fixed_size_simd<T, N> &>(*this);
}
//////////////////// other functions ///////////////
Vc_INTRINSIC void setZero()
{
data0.setZero();
data1.setZero();
}
Vc_INTRINSIC void setZero(const mask_type &k)
{
data0.setZero(Split::lo(k));
data1.setZero(Split::hi(k));
}
Vc_INTRINSIC void setZeroInverted()
{
data0.setZeroInverted();
data1.setZeroInverted();
}
Vc_INTRINSIC void setZeroInverted(const mask_type &k)
{
data0.setZeroInverted(Split::lo(k));
data1.setZeroInverted(Split::hi(k));
}
Vc_INTRINSIC void setQnan() {
data0.setQnan();
data1.setQnan();
}
Vc_INTRINSIC void setQnan(const mask_type &m) {
data0.setQnan(Split::lo(m));
data1.setQnan(Split::hi(m));
}
///\internal execute specified Operation
template <typename Op, typename... Args>
static Vc_INTRINSIC SimdArray fromOperation(Op op, Args &&... args)
{
SimdArray r = {
storage_type0::fromOperation(op, Split::lo(args)...), // no forward here - it
// could move and thus
// break the next line
storage_type1::fromOperation(op, Split::hi(std::forward<Args>(args))...)};
return r;
}
///\internal
template <typename Op, typename... Args>
static Vc_INTRINSIC void callOperation(Op op, Args &&... args)
{
storage_type0::callOperation(op, Split::lo(args)...);
storage_type1::callOperation(op, Split::hi(std::forward<Args>(args))...);
}
template <typename U, typename... Args> Vc_INTRINSIC void load(const U *mem, Args &&... args)
{
data0.load(mem, Split::lo(args)...); // no forward here - it could move and thus
// break the next line
data1.load(mem + storage_type0::size(), Split::hi(std::forward<Args>(args))...);
}
template <typename U, typename... Args> Vc_INTRINSIC void store(U *mem, Args &&... args) const
{
data0.store(mem, Split::lo(args)...); // no forward here - it could move and thus
// break the next line
data1.store(mem + storage_type0::size(), Split::hi(std::forward<Args>(args))...);
}
Vc_INTRINSIC mask_type operator!() const
{
return {!data0, !data1};
}
Vc_INTRINSIC SimdArray operator-() const
{
return {-data0, -data1};
}
/// Returns a copy of itself
Vc_INTRINSIC SimdArray operator+() const { return *this; }
Vc_INTRINSIC SimdArray operator~() const
{
return {~data0, ~data1};
}
// left/right shift operators {{{2
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC Vc_CONST SimdArray operator<<(U x) const
{
return {data0 << x, data1 << x};
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC SimdArray &operator<<=(U x)
{
data0 <<= x;
data1 <<= x;
return *this;
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC Vc_CONST SimdArray operator>>(U x) const
{
return {data0 >> x, data1 >> x};
}
template <typename U,
typename = enable_if<std::is_integral<T>::value && std::is_integral<U>::value>>
Vc_INTRINSIC SimdArray &operator>>=(U x)
{
data0 >>= x;
data1 >>= x;
return *this;
}
// binary operators {{{2
#define Vc_BINARY_OPERATOR_(op) \
Vc_INTRINSIC Vc_CONST SimdArray operator op(const SimdArray &rhs) const \
{ \
return {data0 op rhs.data0, data1 op rhs.data1}; \
} \
Vc_INTRINSIC SimdArray &operator op##=(const SimdArray &rhs) \
{ \
data0 op## = rhs.data0; \
data1 op## = rhs.data1; \
return *this; \
}
Vc_ALL_ARITHMETICS(Vc_BINARY_OPERATOR_);
Vc_ALL_BINARY(Vc_BINARY_OPERATOR_);
Vc_ALL_SHIFTS(Vc_BINARY_OPERATOR_);
#undef Vc_BINARY_OPERATOR_
#define Vc_COMPARES(op) \
Vc_INTRINSIC mask_type operator op(const SimdArray &rhs) const \
{ \
return {data0 op rhs.data0, data1 op rhs.data1}; \
}
Vc_ALL_COMPARES(Vc_COMPARES);
#undef Vc_COMPARES
// operator[] {{{2
/// \name Scalar Subscript Operators
///@{
private:
friend reference;
Vc_INTRINSIC static value_type get(const SimdArray &o, int i) noexcept
{
return reinterpret_cast<const alias_type *>(&o)[i];
}
template <typename U>