File: narrow_predicates.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (57 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download | duplicates (3)
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
#include "Halide.h"

using namespace Halide;

Var x;

template<typename T>
void check(const Expr &e) {
    Func g1, g2;
    g1(x) = e;
    g2(x) = e;

    // Introduce some vector predicates to g1
    g1.vectorize(x, 64, TailStrategy::GuardWithIf);

    Buffer<T> b1(1024), b2(1024);
    g1.realize(b1);
    g2.realize(b2);

    for (int i = 0; i < b1.width(); i++) {
        if (b1(i) != b2(i)) {
            printf("b1(%d) = %d instead of %d\n",
                   i, b1(i), b2(i));
            exit(-1);
        }
    }
}

template<typename T>
void check_all() {
    Func f;
    f(x) = cast<T>(x);
    f.compute_root();

    // This will have a predicated instruction in the loop tail:
    check<T>(f(x));

    // These will also have a comparison mask in the loop body:
    check<T>(select(x < 50, f(x), cast<T>(17)));
    check<T>(select(x > 50, f(x), cast<T>(17)));

    // Also test boundary conditions, which introduce all sorts of coordinate
    // comparisons:
    check<T>(BoundaryConditions::repeat_edge(f, {{10, 100}})(x));
    check<T>(BoundaryConditions::repeat_image(f, {{10, 100}})(x));
    check<T>(BoundaryConditions::constant_exterior(f, cast<T>(17), {{10, 100}})(x));
    check<T>(BoundaryConditions::mirror_image(f, {{10, 100}})(x));
    check<T>(BoundaryConditions::mirror_interior(f, {{10, 100}})(x));
}

int main(int argc, char **argv) {
    check_all<uint8_t>();
    check_all<uint16_t>();

    printf("Success!\n");
    return 0;
}