-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CodeGen_C.cpp
2946 lines (2654 loc) · 102 KB
/
CodeGen_C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <limits>
#include "CodeGen_C.h"
#include "CodeGen_Internal.h"
#include "Deinterleave.h"
#include "IROperator.h"
#include "Lerp.h"
#include "Param.h"
#include "Simplify.h"
#include "Substitute.h"
#include "Type.h"
#include "Util.h"
#include "Var.h"
namespace Halide {
namespace Internal {
using std::map;
using std::ostream;
using std::ostringstream;
using std::string;
using std::vector;
extern "C" unsigned char halide_internal_initmod_inlined_c[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntime_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeCuda_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeHexagonHost_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeMetal_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeOpenCL_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeOpenGLCompute_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeOpenGL_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeQurt_h[];
extern "C" unsigned char halide_internal_runtime_header_HalideRuntimeD3D12Compute_h[];
namespace {
const string headers =
"#include <iostream>\n"
"#include <math.h>\n"
"#include <float.h>\n"
"#include <assert.h>\n"
"#include <limits.h>\n"
"#include <string.h>\n"
"#include <stdio.h>\n"
"#include <stdint.h>\n";
// We now add definitions of things in the runtime which are
// intended to be inlined into every module but are only expressed
// in .ll. The redundancy is regrettable (FIXME).
const string globals = R"INLINE_CODE(
extern "C" {
int64_t halide_current_time_ns(void *ctx);
void halide_profiler_pipeline_end(void *, void *);
}
#ifdef _WIN32
__declspec(dllimport) float __cdecl roundf(float);
__declspec(dllimport) double __cdecl round(double);
#else
inline float asinh_f32(float x) {return asinhf(x);}
inline float acosh_f32(float x) {return acoshf(x);}
inline float atanh_f32(float x) {return atanhf(x);}
inline double asinh_f64(double x) {return asinh(x);}
inline double acosh_f64(double x) {return acosh(x);}
inline double atanh_f64(double x) {return atanh(x);}
#endif
inline float sqrt_f32(float x) {return sqrtf(x);}
inline float sin_f32(float x) {return sinf(x);}
inline float asin_f32(float x) {return asinf(x);}
inline float cos_f32(float x) {return cosf(x);}
inline float acos_f32(float x) {return acosf(x);}
inline float tan_f32(float x) {return tanf(x);}
inline float atan_f32(float x) {return atanf(x);}
inline float atan2_f32(float x, float y) {return atan2f(x, y);}
inline float sinh_f32(float x) {return sinhf(x);}
inline float cosh_f32(float x) {return coshf(x);}
inline float tanh_f32(float x) {return tanhf(x);}
inline float hypot_f32(float x, float y) {return hypotf(x, y);}
inline float exp_f32(float x) {return expf(x);}
inline float log_f32(float x) {return logf(x);}
inline float pow_f32(float x, float y) {return powf(x, y);}
inline float floor_f32(float x) {return floorf(x);}
inline float ceil_f32(float x) {return ceilf(x);}
inline float round_f32(float x) {return roundf(x);}
inline double sqrt_f64(double x) {return sqrt(x);}
inline double sin_f64(double x) {return sin(x);}
inline double asin_f64(double x) {return asin(x);}
inline double cos_f64(double x) {return cos(x);}
inline double acos_f64(double x) {return acos(x);}
inline double tan_f64(double x) {return tan(x);}
inline double atan_f64(double x) {return atan(x);}
inline double atan2_f64(double x, double y) {return atan2(x, y);}
inline double sinh_f64(double x) {return sinh(x);}
inline double cosh_f64(double x) {return cosh(x);}
inline double tanh_f64(double x) {return tanh(x);}
inline double hypot_f64(double x, double y) {return hypot(x, y);}
inline double exp_f64(double x) {return exp(x);}
inline double log_f64(double x) {return log(x);}
inline double pow_f64(double x, double y) {return pow(x, y);}
inline double floor_f64(double x) {return floor(x);}
inline double ceil_f64(double x) {return ceil(x);}
inline double round_f64(double x) {return round(x);}
inline float nan_f32() {return NAN;}
inline float neg_inf_f32() {return -INFINITY;}
inline float inf_f32() {return INFINITY;}
inline bool is_nan_f32(float x) {return x != x;}
inline bool is_nan_f64(double x) {return x != x;}
template<typename A, typename B>
inline A reinterpret(const B &b) {
#if __cplusplus >= 201103L
static_assert(sizeof(A) == sizeof(B), "type size mismatch");
#endif
A a;
memcpy(&a, &b, sizeof(a));
return a;
}
inline float float_from_bits(uint32_t bits) {
return reinterpret<float, uint32_t>(bits);
}
template<typename T>
inline int halide_popcount(T a) {
int bits_set = 0;
while (a != 0) {
bits_set += a & 1;
a >>= 1;
}
return bits_set;
}
template<typename T>
inline int halide_count_leading_zeros(T a) {
int leading_zeros = 0;
int bit = sizeof(a) * 8 - 1;
while (bit >= 0 && (a & (((T)1) << bit)) == 0) {
leading_zeros++;
bit--;
}
return leading_zeros;
}
template<typename T>
inline int halide_count_trailing_zeros(T a) {
int trailing_zeros = 0;
constexpr int bits = sizeof(a) * 8;
int bit = 0;
while (bit < bits && (a & (((T)1) << bit)) == 0) {
trailing_zeros++;
bit++;
}
return trailing_zeros;
}
template<typename T>
inline T halide_cpp_max(const T &a, const T &b) {return (a > b) ? a : b;}
template<typename T>
inline T halide_cpp_min(const T &a, const T &b) {return (a < b) ? a : b;}
template<typename A, typename B>
const B &return_second(const A &a, const B &b) {
(void) a;
return b;
}
template<typename A, typename B>
inline auto quiet_div(const A &a, const B &b) -> decltype(a / b) {
return b == 0 ? static_cast<decltype(a / b)>(0) : (a / b);
}
template<typename A, typename B>
inline auto quiet_mod(const A &a, const B &b) -> decltype(a % b) {
return b == 0 ? static_cast<decltype(a % b)>(0) : (a % b);
}
namespace {
class HalideFreeHelper {
typedef void (*FreeFunction)(void *user_context, void *p);
void * user_context;
void *p;
FreeFunction free_function;
public:
HalideFreeHelper(void *user_context, void *p, FreeFunction free_function)
: user_context(user_context), p(p), free_function(free_function) {}
~HalideFreeHelper() { free(); }
void free() {
if (p) {
// TODO: do all free_functions guarantee to ignore a nullptr?
free_function(user_context, p);
p = nullptr;
}
}
};
} // namespace
)INLINE_CODE";
} // namespace
class TypeInfoGatherer : public IRGraphVisitor {
private:
using IRGraphVisitor::include;
using IRGraphVisitor::visit;
void include_type(const Type &t) {
if (t.is_vector()) {
if (t.is_bool()) {
// bool vectors are always emitted as uint8 in the C++ backend
// TODO: on some architectures, we could do better by choosing
// a bitwidth that matches the other vectors in use; EliminateBoolVectors
// could be used for this with a bit of work.
vector_types_used.insert(UInt(8).with_lanes(t.lanes()));
} else if (!t.is_handle()) {
// Vector-handle types can be seen when processing (e.g.)
// require() statements that are vectorized, but they
// will all be scalarized away prior to use, so don't emit
// them.
vector_types_used.insert(t);
if (t.is_int()) {
// If we are including an int-vector type, also include
// the same-width uint-vector type; there are various operations
// that can use uint vectors for intermediate results (e.g. lerp(),
// but also Mod, which can generate a call to abs() for int types,
// which always produces uint results for int inputs in Halide);
// it's easier to just err on the side of extra vectors we don't
// use since they are just type declarations.
vector_types_used.insert(t.with_code(halide_type_uint));
}
}
}
}
void include_lerp_types(const Type &t) {
if (t.is_vector() && t.is_int_or_uint() && (t.bits() >= 8 && t.bits() <= 32)) {
Type doubled = t.with_bits(t.bits() * 2);
include_type(doubled);
}
}
protected:
void include(const Expr &e) override {
include_type(e.type());
IRGraphVisitor::include(e);
}
// GCC's __builtin_shuffle takes an integer vector of
// the size of its input vector. Make sure this type exists.
void visit(const Shuffle *op) override {
vector_types_used.insert(Int(32, op->vectors[0].type().lanes()));
IRGraphVisitor::visit(op);
}
void visit(const For *op) override {
for_types_used.insert(op->for_type);
IRGraphVisitor::visit(op);
}
void visit(const Ramp *op) override {
include_type(op->type.with_lanes(op->lanes));
IRGraphVisitor::visit(op);
}
void visit(const Broadcast *op) override {
include_type(op->type.with_lanes(op->lanes));
IRGraphVisitor::visit(op);
}
void visit(const Cast *op) override {
include_type(op->type);
IRGraphVisitor::visit(op);
}
void visit(const Call *op) override {
include_type(op->type);
if (op->is_intrinsic(Call::lerp)) {
// lower_lerp() can synthesize wider vector types.
for (auto &a : op->args) {
include_lerp_types(a.type());
}
} else if (op->is_intrinsic(Call::absd)) {
// absd() can synthesize a new type
include_type(op->type.with_code(op->type.is_int() ? Type::UInt : op->type.code()));
}
IRGraphVisitor::visit(op);
}
public:
std::set<ForType> for_types_used;
std::set<Type> vector_types_used;
};
CodeGen_C::CodeGen_C(ostream &s, Target t, OutputKind output_kind, const std::string &guard) :
IRPrinter(s), id("$$ BAD ID $$"), target(t), output_kind(output_kind), extern_c_open(false) {
if (is_header()) {
// If it's a header, emit an include guard.
stream << "#ifndef HALIDE_" << print_name(guard) << '\n'
<< "#define HALIDE_" << print_name(guard) << '\n'
<< "#include <stdint.h>\n"
<< "\n"
<< "// Forward declarations of the types used in the interface\n"
<< "// to the Halide pipeline.\n"
<< "//\n";
if (target.has_feature(Target::NoRuntime)) {
stream << "// For the definitions of these structs, include HalideRuntime.h\n";
} else {
stream << "// Definitions for these structs are below.\n";
}
stream << "\n"
<< "// Halide's representation of a multi-dimensional array.\n"
<< "// Halide::Runtime::Buffer is a more user-friendly wrapper\n"
<< "// around this. Its declaration is in HalideBuffer.h\n"
<< "struct halide_buffer_t;\n"
<< "\n"
<< "// Metadata describing the arguments to the generated function.\n"
<< "// Used to construct calls to the _argv version of the function.\n"
<< "struct halide_filter_metadata_t;\n"
<< "\n";
// We just forward declared the following types:
forward_declared.insert(type_of<halide_buffer_t *>().handle_type);
forward_declared.insert(type_of<halide_filter_metadata_t *>().handle_type);
if (t.has_feature(Target::LegacyBufferWrappers)) {
stream << "// The legacy buffer type. Do not use in new code.\n"
<< "struct buffer_t;\n"
<< "\n";
forward_declared.insert(type_of<buffer_t *>().handle_type);
}
} else {
// Include declarations of everything generated C source might want
stream
<< headers
<< globals
<< halide_internal_runtime_header_HalideRuntime_h << '\n'
<< halide_internal_initmod_inlined_c << '\n';
add_common_macros(stream);
stream << '\n';
}
// Throw in a default (empty) definition of HALIDE_FUNCTION_ATTRS
// (some hosts may define this to e.g. __attribute__((warn_unused_result)))
stream << "#ifndef HALIDE_FUNCTION_ATTRS\n";
stream << "#define HALIDE_FUNCTION_ATTRS\n";
stream << "#endif\n";
}
CodeGen_C::~CodeGen_C() {
set_name_mangling_mode(NameMangling::Default);
if (is_header()) {
if (!target.has_feature(Target::NoRuntime)) {
stream << "\n"
<< "// The generated object file that goes with this header\n"
<< "// includes a full copy of the Halide runtime so that it\n"
<< "// can be used standalone. Declarations for the functions\n"
<< "// in the Halide runtime are below.\n";
if (target.os == Target::Windows) {
stream
<< "//\n"
<< "// The inclusion of this runtime means that it is not legal\n"
<< "// to link multiple Halide-generated object files together.\n"
<< "// This problem is Windows-specific. On other platforms, we\n"
<< "// use weak linkage.\n";
} else {
stream
<< "//\n"
<< "// The runtime is defined using weak linkage, so it is legal\n"
<< "// to link multiple Halide-generated object files together,\n"
<< "// or to clobber any of these functions with your own\n"
<< "// definition.\n";
}
stream << "//\n"
<< "// To generate an object file without a full copy of the\n"
<< "// runtime, use the -no_runtime target flag. To generate a\n"
<< "// standalone Halide runtime to use with such object files\n"
<< "// use the -r flag with any Halide generator binary, e.g.:\n"
<< "// $ ./my_generator -r halide_runtime -o . target=host\n"
<< "\n"
<< halide_internal_runtime_header_HalideRuntime_h << '\n';
if (target.has_feature(Target::CUDA)) {
stream << halide_internal_runtime_header_HalideRuntimeCuda_h << '\n';
}
if (target.has_feature(Target::HVX_128) ||
target.has_feature(Target::HVX_64)) {
stream << halide_internal_runtime_header_HalideRuntimeHexagonHost_h << '\n';
}
if (target.has_feature(Target::Metal)) {
stream << halide_internal_runtime_header_HalideRuntimeMetal_h << '\n';
}
if (target.has_feature(Target::OpenCL)) {
stream << halide_internal_runtime_header_HalideRuntimeOpenCL_h << '\n';
}
if (target.has_feature(Target::OpenGLCompute)) {
stream << halide_internal_runtime_header_HalideRuntimeOpenGLCompute_h << '\n';
}
if (target.has_feature(Target::OpenGL)) {
stream << halide_internal_runtime_header_HalideRuntimeOpenGL_h << '\n';
}
if (target.has_feature(Target::D3D12Compute)) {
stream << halide_internal_runtime_header_HalideRuntimeD3D12Compute_h << '\n';
}
}
stream << "#endif\n";
}
}
void CodeGen_C::add_common_macros(std::ostream &dest) {
const char *macros = R"INLINE_CODE(
// ll suffix in OpenCL is reserver for 128-bit integers.
#if defined __OPENCL_VERSION__
#define ADD_INT64_T_SUFFIX(x) x##l
#define ADD_UINT64_T_SUFFIX(x) x##ul
// HLSL doesn't have any suffixes.
#elif defined HLSL_VERSION
#define ADD_INT64_T_SUFFIX(x) x
#define ADD_UINT64_T_SUFFIX(x) x
#else
#define ADD_INT64_T_SUFFIX(x) x##ll
#define ADD_UINT64_T_SUFFIX(x) x##ull
#endif
)INLINE_CODE";
dest << macros;
}
void CodeGen_C::add_vector_typedefs(const std::set<Type> &vector_types) {
if (!vector_types.empty()) {
// MSVC has a limit of ~16k for string literals, so split
// up these declarations accordingly
const char *cpp_vector_decl = R"INLINE_CODE(
#if !defined(__has_attribute)
#define __has_attribute(x) 0
#endif
#if !defined(__has_builtin)
#define __has_builtin(x) 0
#endif
template <typename ElementType_, size_t Lanes_>
class CppVector {
public:
typedef ElementType_ ElementType;
static const size_t Lanes = Lanes_;
typedef CppVector<ElementType, Lanes> Vec;
typedef CppVector<uint8_t, Lanes> Mask;
CppVector &operator=(const Vec &src) {
if (this != &src) {
for (size_t i = 0; i < Lanes; i++) {
elements[i] = src[i];
}
}
return *this;
}
/* not-explicit */ CppVector(const Vec &src) {
for (size_t i = 0; i < Lanes; i++) {
elements[i] = src[i];
}
}
CppVector() {
for (size_t i = 0; i < Lanes; i++) {
elements[i] = 0;
}
}
static Vec broadcast(const ElementType &v) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = v;
}
return r;
}
static Vec ramp(const ElementType &base, const ElementType &stride) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = base + stride * i;
}
return r;
}
static Vec load(const void *base, int32_t offset) {
Vec r(empty);
memcpy(&r.elements[0], ((const ElementType*)base + offset), sizeof(r.elements));
return r;
}
// gather
static Vec load(const void *base, const CppVector<int32_t, Lanes> &offset) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = ((const ElementType*)base)[offset[i]];
}
return r;
}
void store(void *base, int32_t offset) const {
memcpy(((ElementType*)base + offset), &this->elements[0], sizeof(this->elements));
}
// scatter
void store(void *base, const CppVector<int32_t, Lanes> &offset) const {
for (size_t i = 0; i < Lanes; i++) {
((ElementType*)base)[offset[i]] = elements[i];
}
}
static Vec shuffle(const Vec &a, const int32_t indices[Lanes]) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
if (indices[i] < 0) {
continue;
}
r.elements[i] = a[indices[i]];
}
return r;
}
template<size_t InputLanes>
static Vec concat(size_t count, const CppVector<ElementType, InputLanes> vecs[]) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = vecs[i / InputLanes][i % InputLanes];
}
return r;
}
Vec replace(size_t i, const ElementType &b) const {
Vec r = *this;
r.elements[i] = b;
return r;
}
ElementType operator[](size_t i) const {
return elements[i];
}
Vec operator~() const {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = ~elements[i];
}
return r;
}
Vec operator!() const {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = !r.elements[i];
}
return r;
}
friend Vec operator+(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] + b[i];
}
return r;
}
friend Vec operator-(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] - b[i];
}
return r;
}
friend Vec operator*(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] * b[i];
}
return r;
}
friend Vec operator/(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] / b[i];
}
return r;
}
friend Vec operator%(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] % b[i];
}
return r;
}
template <typename OtherElementType>
friend Vec operator<<(const Vec &a, const CppVector<OtherElementType, Lanes> &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] << b[i];
}
return r;
}
template <typename OtherElementType>
friend Vec operator>>(const Vec &a, const CppVector<OtherElementType, Lanes> &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] >> b[i];
}
return r;
}
friend Vec operator&(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] & b[i];
}
return r;
}
friend Vec operator|(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] | b[i];
}
return r;
}
friend Vec operator&&(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] && b[i];
}
return r;
}
friend Vec operator||(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] || b[i];
}
return r;
}
friend Vec operator+(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] + b;
}
return r;
}
friend Vec operator-(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] - b;
}
return r;
}
friend Vec operator*(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] * b;
}
return r;
}
friend Vec operator/(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] / b;
}
return r;
}
friend Vec operator%(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] % b;
}
return r;
}
friend Vec operator>>(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] >> b;
}
return r;
}
friend Vec operator<<(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] << b;
}
return r;
}
friend Vec operator&(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] & b;
}
return r;
}
friend Vec operator|(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] | b;
}
return r;
}
friend Vec operator&&(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] && b;
}
return r;
}
friend Vec operator||(const Vec &a, const ElementType &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] || b;
}
return r;
}
friend Vec operator+(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a + b[i];
}
return r;
}
friend Vec operator-(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a - b[i];
}
return r;
}
friend Vec operator*(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a * b[i];
}
return r;
}
friend Vec operator/(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a / b[i];
}
return r;
}
friend Vec operator%(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a % b[i];
}
return r;
}
friend Vec operator>>(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a >> b[i];
}
return r;
}
friend Vec operator<<(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a << b[i];
}
return r;
}
friend Vec operator&(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a & b[i];
}
return r;
}
friend Vec operator|(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a | b[i];
}
return r;
}
friend Vec operator&&(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a && b[i];
}
return r;
}
friend Vec operator||(const ElementType &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a || b[i];
}
return r;
}
friend Mask operator<(const Vec &a, const Vec &b) {
Mask r;
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] < b[i] ? 0xff : 0x00;
}
return r;
}
friend Mask operator<=(const Vec &a, const Vec &b) {
Mask r;
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] <= b[i] ? 0xff : 0x00;
}
return r;
}
friend Mask operator>(const Vec &a, const Vec &b) {
Mask r;
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] > b[i] ? 0xff : 0x00;
}
return r;
}
friend Mask operator>=(const Vec &a, const Vec &b) {
Mask r;
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] >= b[i] ? 0xff : 0x00;
}
return r;
}
friend Mask operator==(const Vec &a, const Vec &b) {
Mask r;
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] == b[i] ? 0xff : 0x00;
}
return r;
}
friend Mask operator!=(const Vec &a, const Vec &b) {
Mask r;
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = a[i] != b[i] ? 0xff : 0x00;
}
return r;
}
static Vec select(const Mask &cond, const Vec &true_value, const Vec &false_value) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = cond[i] ? true_value[i] : false_value[i];
}
return r;
}
template <typename OtherVec>
static Vec convert_from(const OtherVec &src) {
#if __cplusplus >= 201103L
static_assert(Vec::Lanes == OtherVec::Lanes, "Lanes mismatch");
#endif
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = static_cast<typename Vec::ElementType>(src[i]);
}
return r;
}
static Vec max(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = ::halide_cpp_max(a[i], b[i]);
}
return r;
}
static Vec min(const Vec &a, const Vec &b) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.elements[i] = ::halide_cpp_min(a[i], b[i]);
}
return r;
}
private:
template <typename, size_t> friend class CppVector;
ElementType elements[Lanes];
// Leave vector uninitialized for cases where we overwrite every entry
enum Empty { empty };
CppVector(Empty) {}
};
)INLINE_CODE";
const char *native_vector_decl = R"INLINE_CODE(
#if __has_attribute(ext_vector_type) || __has_attribute(vector_size)
template <typename ElementType_, size_t Lanes_>
class NativeVector {
public:
typedef ElementType_ ElementType;
static const size_t Lanes = Lanes_;
typedef NativeVector<ElementType, Lanes> Vec;
typedef NativeVector<uint8_t, Lanes> Mask;
#if __has_attribute(ext_vector_type)
typedef ElementType_ NativeVectorType __attribute__((ext_vector_type(Lanes), aligned(sizeof(ElementType))));
#elif __has_attribute(vector_size) || __GNUC__
typedef ElementType_ NativeVectorType __attribute__((vector_size(Lanes * sizeof(ElementType)), aligned(sizeof(ElementType))));
#endif
NativeVector &operator=(const Vec &src) {
if (this != &src) {
native_vector = src.native_vector;
}
return *this;
}
/* not-explicit */ NativeVector(const Vec &src) {
native_vector = src.native_vector;
}
NativeVector() {
native_vector = (NativeVectorType){};
}
static Vec broadcast(const ElementType &v) {
Vec zero; // Zero-initialized native vector.
return zero + v;
}
// TODO: this should be improved by taking advantage of native operator support.
static Vec ramp(const ElementType &base, const ElementType &stride) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.native_vector[i] = base + stride * i;
}
return r;
}
// TODO: could this be improved by taking advantage of native operator support?
static Vec load(const void *base, int32_t offset) {
Vec r(empty);
// Note: do not use sizeof(NativeVectorType) here; if it's an unusual type
// (e.g. uint8x48, which could be produced by concat()), the actual implementation
// might be larger (e.g. it might really be a uint8x64). Only copy the amount
// that is in the logical type, to avoid possible overreads.
memcpy(&r.native_vector, ((const ElementType*)base + offset), sizeof(ElementType) * Lanes);
return r;
}
// gather
// TODO: could this be improved by taking advantage of native operator support?
static Vec load(const void *base, const NativeVector<int32_t, Lanes> &offset) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.native_vector[i] = ((const ElementType*)base)[offset[i]];
}
return r;
}
// TODO: could this be improved by taking advantage of native operator support?
void store(void *base, int32_t offset) const {
// Note: do not use sizeof(NativeVectorType) here; if it's an unusual type
// (e.g. uint8x48, which could be produced by concat()), the actual implementation
// might be larger (e.g. it might really be a uint8x64). Only copy the amount
// that is in the logical type, to avoid possible overwrites.
memcpy(((ElementType*)base + offset), &native_vector, sizeof(ElementType) * Lanes);
}
// scatter
// TODO: could this be improved by taking advantage of native operator support?
void store(void *base, const NativeVector<int32_t, Lanes> &offset) const {
for (size_t i = 0; i < Lanes; i++) {
((ElementType*)base)[offset[i]] = native_vector[i];
}
}
// TODO: this should be improved by taking advantage of native operator support.
static Vec shuffle(const Vec &a, const int32_t indices[Lanes]) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
if (indices[i] < 0) {
continue;
}
r.native_vector[i] = a[indices[i]];
}
return r;
}
// TODO: this should be improved by taking advantage of native operator support.
template<size_t InputLanes>
static Vec concat(size_t count, const NativeVector<ElementType, InputLanes> vecs[]) {
Vec r(empty);
for (size_t i = 0; i < Lanes; i++) {
r.native_vector[i] = vecs[i / InputLanes][i % InputLanes];
}
return r;
}
// TODO: this should be improved by taking advantage of native operator support.
Vec replace(size_t i, const ElementType &b) const {
Vec r = *this;
r.native_vector[i] = b;
return r;
}
ElementType operator[](size_t i) const {