Predator  [unstable] git snapshot
intrange.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Kamil Dudka <kdudka@redhat.com>
3  *
4  * This file is part of predator.
5  *
6  * predator is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * predator is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with predator. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef H_GUARD_INTRANGE_H
21 #define H_GUARD_INTRANGE_H
22 
23 #include "config.h"
24 
25 namespace IR {
26 
27 #ifdef USE_LONG_LONG
28 typedef signed long long TInt;
29 typedef unsigned long long TUInt;
30 #else
31 typedef signed long TInt;
32 typedef unsigned long TUInt;
33 #endif
34 
35 extern const TInt Int0;
36 extern const TInt Int1;
37 extern const TInt IntMin;
38 extern const TInt IntMax;
39 
40 /// a closed interval over integral domain
41 struct Range {
42  TInt lo; ///< lower bound of the interval (included)
43  TInt hi; ///< upper bound of the interval (included)
44  TInt alignment; ///< target alignment, values below Int1 are invalid
45 
46  // NOTE: there is no constructor because we put Range to unions
47 };
48 
49 inline Range rngFromNum(TInt num)
50 {
51  Range rng;
52 
53  rng.lo = num;
54  rng.hi = num;
55  rng.alignment = Int1;
56 
57  return rng;
58 }
59 
60 extern const Range FullRange;
61 
62 /// insert ones as padding from left
64 
65 /// this does nothing unless running a debug build
66 void chkRange(const Range &rng);
67 
68 inline bool operator==(const Range &a, const Range &b)
69 {
70  return (a.lo == b.lo)
71  && (a.hi == b.hi)
72  && (a.alignment == b.alignment);
73 }
74 
75 inline bool operator!=(const Range &a, const Range &b)
76 {
77  return !operator==(a, b);
78 }
79 
80 /// invert polarity of the number
82 
83 /// invert polarity of the range
84 inline Range operator-(Range rng)
85 {
86  const TInt hi = invertInt(rng.lo);
87  rng.lo = invertInt(rng.hi);
88  rng.hi = hi;
89 
90  return rng;
91 }
92 
93 /// add another range, but preserve boundary values if already reached
94 Range& operator+=(Range &rng, const Range &other);
95 
96 /// multiply by another range, but preserve boundary values if already reached
97 Range& operator*=(Range &rng, const Range &other);
98 
99 /// bitwise AND on range where the bitmask is a single number
100 Range& operator&=(Range &rng, TInt mask);
101 
102 Range& operator<<=(Range &rng, TUInt);
103 Range& operator>>=(Range &rng, TUInt);
104 
105 /// subtract another range, but preserve boundary values if already reached
106 inline Range& operator-=(Range &rng, const Range &other)
107 {
108  rng += (-other);
109  return rng;
110 }
111 
112 inline Range operator+(Range rng, const Range &other)
113 {
114  rng += other;
115  return rng;
116 }
117 
118 inline Range operator*(Range rng, const Range &other)
119 {
120  rng *= other;
121  return rng;
122 }
123 
124 inline Range operator-(Range rng, const Range &other)
125 {
126  rng -= other;
127  return rng;
128 }
129 
130 inline Range operator&(Range rng, TInt mask)
131 {
132  rng &= mask;
133  return rng;
134 }
135 
136 inline Range operator<<(Range rng, TUInt n)
137 {
138  rng <<= n;
139  return rng;
140 }
141 
142 inline Range operator>>(Range rng, TUInt n)
143 {
144  rng >>= n;
145  return rng;
146 }
147 
148 /// return a range that covers both given ranges, preserve alignment if possible
149 Range join(const Range &rng1, const Range &rng2);
150 
151 /// return true if exactly one of the given ranges represents a single number
152 bool isRangeByNum(bool *pIsRange1, const Range &rng1, const Range &rng2);
153 
154 /// true if the small range is inside the big one (sharing endpoints is fine)
155 bool isCovered(const Range &small, const Range &big);
156 
157 /// return true if the range contain exactly one number; break if no one at all
158 bool isSingular(const Range &);
159 
160 /// return true if the range is non-trivially aligned
161 bool isAligned(const Range &);
162 
163 /// adjust the alignment so that the bounds are aligned
164 void adjustAlignment(Range *);
165 
166 /// return the count of integral numbers that the given range represents
167 TUInt widthOf(const Range &);
168 
169 } // namespace IR
170 
171 #endif /* H_GUARD_INTRANGE_H */