1 #ifndef HALIDE_INTERVAL_H
2 #define HALIDE_INTERVAL_H
3 
4 /** \file
5  * Defines the Interval class
6  */
7 
8 #include "Expr.h"
9 
10 namespace Halide {
11 namespace Internal {
12 
13 /** A class to represent ranges of Exprs. Can be unbounded above or below. */
14 struct Interval {
15 
16     /** Exprs to represent positive and negative infinity */
17 #ifdef COMPILING_HALIDE
pos_infInterval18     static HALIDE_ALWAYS_INLINE Expr pos_inf() {
19         return pos_inf_expr;
20     }
neg_infInterval21     static HALIDE_ALWAYS_INLINE Expr neg_inf() {
22         return neg_inf_expr;
23     }
24 #else
25     static Expr pos_inf() {
26         return pos_inf_noinline();
27     }
28     static Expr neg_inf() {
29         return neg_inf_noinline();
30     }
31 #endif
32 
33     /** The lower and upper bound of the interval. They are included
34      * in the interval. */
35     Expr min, max;
36 
37     /** A default-constructed Interval is everything */
IntervalInterval38     Interval()
39         : min(neg_inf()), max(pos_inf()) {
40     }
41 
42     /** Construct an interval from a lower and upper bound. */
IntervalInterval43     Interval(const Expr &min, const Expr &max)
44         : min(min), max(max) {
45         internal_assert(min.defined() && max.defined());
46     }
47 
48     /** The interval representing everything. */
49     static Interval everything();
50 
51     /** The interval representing nothing. */
52     static Interval nothing();
53 
54     /** Construct an interval representing a single point */
55     static Interval single_point(const Expr &e);
56 
57     /** Is the interval the empty set */
58     bool is_empty() const;
59 
60     /** Is the interval the entire range */
61     bool is_everything() const;
62 
63     /** Is the interval just a single value (min == max) */
64     bool is_single_point() const;
65 
66     /** Is the interval a particular single value */
67     bool is_single_point(const Expr &e) const;
68 
69     /** Does the interval have a finite least upper bound */
70     bool has_upper_bound() const;
71 
72     /** Does the interval have a finite greatest lower bound */
73     bool has_lower_bound() const;
74 
75     /** Does the interval have a finite upper and lower bound */
76     bool is_bounded() const;
77 
78     /** Is the interval the same as another interval */
79     bool same_as(const Interval &other) const;
80 
81     /** Expand the interval to include another Interval */
82     void include(const Interval &i);
83 
84     /** Expand the interval to include an Expr */
85     void include(const Expr &e);
86 
87     /** Construct the smallest interval containing two intervals. */
88     static Interval make_union(const Interval &a, const Interval &b);
89 
90     /** Construct the largest interval contained within two intervals. */
91     static Interval make_intersection(const Interval &a, const Interval &b);
92 
93     /** An eagerly-simplifying max of two Exprs that respects infinities. */
94     static Expr make_max(const Expr &a, const Expr &b);
95 
96     /** An eagerly-simplifying min of two Exprs that respects infinities. */
97     static Expr make_min(const Expr &a, const Expr &b);
98 
99     /** Equivalent to same_as. Exists so that the autoscheduler can
100      * compare two map<string, Interval> for equality in order to
101      * cache computations. */
102     bool operator==(const Interval &other) const;
103 
104 private:
105     static Expr neg_inf_expr, pos_inf_expr;
106 
107     // Never used inside libHalide; provided for Halide tests, to avoid needing to export
108     // data fields in some build environments.
109     static Expr pos_inf_noinline();
110     static Expr neg_inf_noinline();
111 };
112 
113 }  // namespace Internal
114 }  // namespace Halide
115 
116 #endif
117