1 #include "Halide.h"
2 #include <iostream>
3 #include <stdio.h>
4 
5 using namespace Halide;
6 using namespace Halide::ConciseCasts;
7 using namespace Halide::Internal;
8 
main(int arch,char ** argv)9 int main(int arch, char **argv) {
10     const int W = 256, H = 256;
11 
12     Buffer<uint8_t> in(W, H);
13     // Set up the input.
14     for (int y = 0; y < H; y++) {
15         for (int x = 0; x < W; x++) {
16             in(x, y) = rand() & 0xff;
17         }
18     }
19 
20     // Define a convolution kernel, and its sum.
21     Buffer<int8_t> kernel(3, 3);
22     kernel.set_min(-1, -1);
23     for (int y = -1; y <= 1; y++) {
24         for (int x = -1; x <= 1; x++) {
25             kernel(x, y) = rand() % 8 - 4;
26         }
27     }
28 
29     Var x("x"), y("y"), xi("xi"), yi("yi");
30     RDom r(-1, 3, -1, 3);
31 
32     // Boundary condition.
33     Func input = BoundaryConditions::repeat_edge(in);
34     input.compute_root();
35 
36     // Test a widening reduction, followed by a narrowing.
37     {
38         Func f;
39         f(x, y) = u8_sat(sum(i16(input(x + r.x, y + r.y)) * kernel(r.x, r.y)) / 16);
40 
41         // Schedule.
42         Target target = get_jit_target_from_environment();
43         if (target.has_gpu_feature()) {
44             f.gpu_tile(x, y, xi, yi, 16, 16);
45         } else if (target.features_any_of({Target::HVX_64, Target::HVX_128})) {
46             f.hexagon().vectorize(x, 128);
47         } else {
48             f.vectorize(x, target.natural_vector_size<uint8_t>());
49         }
50 
51         // Run the pipeline and verify the results are correct.
52         Buffer<uint8_t> out = f.realize(W, H, target);
53 
54         for (int y = 1; y < H - 1; y++) {
55             for (int x = 1; x < W - 1; x++) {
56                 int16_t correct = 0;
57                 for (int ry = -1; ry <= 1; ry++) {
58                     for (int rx = -1; rx <= 1; rx++) {
59                         correct += static_cast<int16_t>(in(x + rx, y + ry)) * kernel(rx, ry);
60                     }
61                 }
62                 correct = std::min(std::max(correct / 16, 0), 255);
63                 if (correct != out(x, y)) {
64                     std::cout << "out(" << x << ", " << y << ") = " << (int)out(x, y) << " instead of " << correct << "\n";
65                     return -1;
66                 }
67             }
68         }
69     }
70 
71     // Test a tuple reduction with widening, followed by narrowing the result.
72     {
73         Func f;
74         f(x, y) = {i16(0), i8(0)};
75         f(x, y) = {
76             f(x, y)[0] + i16(input(x + r.x, y + r.y)) * kernel(r.x, r.y),
77             f(x, y)[1] + kernel(r.x, r.y),
78         };
79 
80         Func g;
81         g(x, y) = u8_sat((f(x, y)[0] + f(x, y)[1]) / 16);
82 
83         // Schedule.
84         Target target = get_jit_target_from_environment();
85         if (target.has_gpu_feature()) {
86             g.gpu_tile(x, y, xi, yi, 16, 16);
87         } else if (target.features_any_of({Target::HVX_64, Target::HVX_128})) {
88             g.hexagon().vectorize(x, 128);
89         } else {
90             g.vectorize(x, target.natural_vector_size<uint8_t>());
91         }
92 
93         // Run the pipeline and verify the results are correct.
94         Buffer<uint8_t> out = g.realize(W, H, target);
95 
96         for (int y = 1; y < H - 1; y++) {
97             for (int x = 1; x < W - 1; x++) {
98                 int16_t correct = 0;
99                 for (int ry = -1; ry <= 1; ry++) {
100                     for (int rx = -1; rx <= 1; rx++) {
101                         correct += static_cast<int16_t>(in(x + rx, y + ry)) * kernel(rx, ry);
102                         correct += kernel(rx, ry);
103                     }
104                 }
105                 correct = std::min(std::max(correct / 16, 0), 255);
106                 if (correct != out(x, y)) {
107                     std::cout << "out(" << x << ", " << y << ") = " << (int)out(x, y) << " instead of " << correct << "\n";
108                     return -1;
109                 }
110             }
111         }
112     }
113 
114     // Test a widening, followed by a narrowing reduction with an
115     // unaligned output. This triggered a bug in EliminateInterleaves
116     // on Hexagon.
117     {
118         Func f;
119         f(x, y) = i16(input(x, y));
120 
121         Func g;
122         g(x, y) = u8_sat((f(x, y) + f(x + 1, y)) / 2);
123 
124         // Schedule.
125         Target target = get_jit_target_from_environment();
126         if (target.has_gpu_feature()) {
127             g.gpu_tile(x, y, xi, yi, 16, 16);
128         } else if (target.features_any_of({Target::HVX_64, Target::HVX_128})) {
129             g.hexagon().vectorize(x, 128);
130             f.compute_at(g, y).vectorize(x, 128, TailStrategy::RoundUp);
131         } else {
132             g.vectorize(x, target.natural_vector_size<uint8_t>());
133         }
134 
135         g.output_buffer().dim(0).set_min(0).set_extent(W - 2);
136         g.output_buffer().dim(1).set_min(0).set_extent(H);
137 
138         // Run the pipeline and verify the results are correct.
139         Buffer<uint8_t> out = g.realize(W - 2, H, target);
140 
141         for (int y = 1; y < H - 1; y++) {
142             for (int x = 0; x < W - 3; x++) {
143                 uint8_t correct = (static_cast<int16_t>(in(x, y)) + in(x + 1, y)) / 2;
144                 if (correct != out(x, y)) {
145                     std::cout << "out(" << x << ", " << y << ") = " << (int)out(x, y) << " instead of " << (int)correct << "\n";
146                     return -1;
147                 }
148             }
149         }
150     }
151 
152     std::cout << "Success!\n";
153     return 0;
154 }
155