File: constant_expr.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 (128 lines) | stat: -rw-r--r-- 3,983 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
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
#include "Halide.h"
#include <limits>
#include <stdio.h>

#ifdef _MSC_VER
#pragma warning(disable : 4800)  // forcing value to bool 'true' or 'false'
#endif

using namespace Halide;
using namespace Halide::Internal;

template<typename T>
bool bit_flip(T a) {
    return ~a;
}

template<typename T>
bool scalar_from_constant_expr(Expr e, T *val) {
    if (type_of<T>().is_int()) {
        auto i = as_const_int(e);
        if (!i) return false;
        *val = (T)(*i);
        return true;
    } else if (type_of<T>().is_uint()) {
        auto u = as_const_uint(e);
        if (!u) return false;
        *val = (T)(*u);
        return true;
    } else if (type_of<T>().is_float()) {
        auto f = as_const_float(e);
        if (!f) return false;
        *val = (T)(*f);
        return true;
    } else {
        return false;
    }
}

template<typename T>
void test_expr(T value) {
    Type t = type_of<T>();

    // std::cout << "Test " << t << " = " << value << "\n";

    Expr e = make_const(t, value);
    if (e.type() != t) {
        std::cerr << "constant of type " << t << " returned expr of type " << e.type() << "\n";
        exit(1);
    }
    T nvalue = T(0);
    if (!scalar_from_constant_expr<T>(e, &nvalue)) {
        std::cerr << "constant of type " << t << " failed scalar_from_constant_expr with value " << value << "\n";
        exit(1);
    }
    if (nvalue != value) {
        std::cerr << "Roundtrip failed for type " << t << ": input " << value << " output " << nvalue << "\n";
        exit(1);
    }
}

template<typename T>
void test_expr_range() {
    const T low = std::numeric_limits<T>::lowest();
    const T min = std::numeric_limits<T>::min();
    const T max = std::numeric_limits<T>::max();

    test_expr<T>(0);
    test_expr<T>(1);

    test_expr<T>(low);
    test_expr<T>(min);
    test_expr<T>(max);
}

int main(int argc, char **argv) {
    test_expr_range<bool>();
    test_expr_range<uint8_t>();
    test_expr_range<uint16_t>();
    test_expr_range<uint32_t>();
    test_expr_range<int8_t>();
    test_expr_range<int16_t>();
    test_expr_range<int32_t>();
    test_expr_range<int64_t>();
    test_expr_range<uint64_t>();
    test_expr_range<float>();
    test_expr_range<double>();

    // Test various edge cases for int64 and double, since we do extra voodoo to
    // disassemble and reassemble them.
    test_expr<int64_t>(-64);
    test_expr<int64_t>((int64_t)0x000000007fffffff);
    test_expr<int64_t>((int64_t)0x0000000080000000);
    test_expr<int64_t>((int64_t)0x0000000080000001);
    test_expr<int64_t>((int64_t)0x00000000ffffffff);
    test_expr<int64_t>((int64_t)0x00000001ffffffff);
    test_expr<int64_t>((int64_t)0x7fffffff00000000);
    test_expr<int64_t>((int64_t)0x7fffffff80000000);
    test_expr<int64_t>((int64_t)0xffffffff80000000);
    test_expr<int64_t>((int64_t)0xffffffff00000001);
    test_expr<int64_t>((int64_t)0x7FFFFFFFFFFFFFFF);
    test_expr<int64_t>((int64_t)0x8000000000000000);
    test_expr<int64_t>((int64_t)0x8000000000000001);

    test_expr<uint64_t>(-64);
    test_expr<uint64_t>((uint64_t)0x000000007fffffff);
    test_expr<uint64_t>((uint64_t)0x0000000080000000);
    test_expr<uint64_t>((uint64_t)0x0000000080000001);
    test_expr<uint64_t>((uint64_t)0x00000000ffffffff);
    test_expr<uint64_t>((uint64_t)0x00000001ffffffff);
    test_expr<uint64_t>((uint64_t)0x7fffffff00000000);
    test_expr<uint64_t>((uint64_t)0x7fffffff80000000);
    test_expr<uint64_t>((uint64_t)0xffffffff80000000);
    test_expr<uint64_t>((uint64_t)0xffffffff00000001);
    test_expr<uint64_t>((uint64_t)0x7FFFFFFFFFFFFFFF);
    test_expr<uint64_t>((uint64_t)0x8000000000000000);
    test_expr<uint64_t>((uint64_t)0x8000000000000001);

    test_expr<float>(3.141592f);
    test_expr<float>(3.40282e+38f);
    test_expr<float>(3.40282e+38f);

    test_expr<double>(3.1415926535897932384626433832795);
    test_expr<double>(1.79769e+308);
    test_expr<double>(-1.79769e+308);

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