|
| 1 | +#include "Halide.h" |
| 2 | +#include <cstdio> |
| 3 | + |
| 4 | +using namespace Halide; |
| 5 | + |
| 6 | +template<typename T> |
| 7 | +T tolerance() { |
| 8 | + return 0; |
| 9 | +} |
| 10 | + |
| 11 | +template<> |
| 12 | +float tolerance<float>() { |
| 13 | + return 1e-7f; |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +template<typename T> |
| 18 | +bool equals(T a, T b, T epsilon = tolerance<T>()) { |
| 19 | + T error = std::abs(a - b); |
| 20 | + return error <= epsilon; |
| 21 | +} |
| 22 | + |
| 23 | +template<typename A> |
| 24 | +bool test(int vec_width) { |
| 25 | + |
| 26 | + int W = vec_width * 4; |
| 27 | + int H = 1000; |
| 28 | + |
| 29 | + Buffer<A> input(W, H + 20); |
| 30 | + for (int y = 0; y < H + 20; y++) { |
| 31 | + for (int x = 0; x < W; x++) { |
| 32 | + input(x, y) = (A)((rand() & 0xffff) * 0.125 + 1.0); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + Var x("x"), y("y"); |
| 37 | + Func f("f"), g("g"); |
| 38 | + |
| 39 | + RDom r(0, W, 0, H); |
| 40 | + r.where((r.x * r.y) % 8 < 7); |
| 41 | + |
| 42 | + Expr e = input(r.x, r.y); |
| 43 | + for (int i = 1; i < 5; i++) { |
| 44 | + e = e + input(r.x, r.y + i); |
| 45 | + } |
| 46 | + |
| 47 | + for (int i = 5; i >= 0; i--) { |
| 48 | + e = e + input(r.x, r.y + i); |
| 49 | + } |
| 50 | + |
| 51 | + f(x, y) = cast<A>(0); |
| 52 | + f(r.x, r.y) = e; |
| 53 | + g(x, y) = cast<A>(0); |
| 54 | + g(r.x, r.y) = e; |
| 55 | + f.update(0).vectorize(r.x); |
| 56 | + |
| 57 | + Buffer<A> outputg = g.realize({W, H}); |
| 58 | + Buffer<A> outputf = f.realize({W, H}); |
| 59 | + |
| 60 | + for (int j = 0; j < H; j++) { |
| 61 | + for (int i = 0; i < W; i++) { |
| 62 | + if (!equals(outputf(i, j), outputg(i, j))) { |
| 63 | + std::cout << type_of<A>() << " x " << vec_width << " failed at " |
| 64 | + << i << " " << j << ": " |
| 65 | + << outputf(i, j) << " vs " << outputg(i, j) << "\n" |
| 66 | + << "Failure!\n"; |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +int main(int argc, char **argv) { |
| 76 | + if (!test<float>(4)) return 1; |
| 77 | + if (!test<float>(8)) return 1; |
| 78 | + |
| 79 | + printf("Success!\n"); |
| 80 | + return 0; |
| 81 | +} |
0 commit comments