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
|
#include "Halide.h"
#include <stdio.h>
using namespace Halide;
using namespace Halide::Internal;
int main(int argc, char **argv) {
// ternary select with Expr condition
{
Var x("x"), y("y");
Func f("f");
f(x, y) = select(x + y < 30, Tuple(x, y), Tuple(x - 1, y - 2));
Realization result = f.realize({200, 200});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x + y < 30) ? x : x - 1;
int correct_b = (x + y < 30) ? y : y - 2;
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
// ternary select with Expr condition
{
Var x("x"), y("y");
Func f("f");
f(x, y) = select(Tuple(x < 30, y < 30), Tuple(x, y), Tuple(x - 1, y - 2));
Realization result = f.realize({200, 200});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x < 30) ? x : x - 1;
int correct_b = (y < 30) ? y : y - 2;
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
// multiway select with Expr condition
{
Var x("x"), y("y");
Func f("f");
f(x, y) = select(x + y < 30, Tuple(x, y),
x + y < 100, Tuple(x - 1, y - 2),
Tuple(x - 100, y - 200));
Realization result = f.realize({200, 200});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x + y < 30) ? x : ((x + y < 100) ? x - 1 : x - 100);
int correct_b = (x + y < 30) ? y : ((x + y < 100) ? y - 2 : y - 200);
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
// multiway select with Tuple condition
{
Var x("x"), y("y");
Func f("f");
f(x, y) = select(Tuple(x < 30, y < 30), Tuple(x, y),
Tuple(x < 100, y < 100), Tuple(x - 1, y - 2),
Tuple(x - 100, y - 200));
Realization result = f.realize({200, 200});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x < 30) ? x : ((x < 100) ? x - 1 : x - 100);
int correct_b = (y < 30) ? y : ((y < 100) ? y - 2 : y - 200);
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
printf("Success!\n");
return 0;
}
|