• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #ifndef _CONSTANT_UNION_INCLUDED_
8 #define _CONSTANT_UNION_INCLUDED_
9 
10 #include <assert.h>
11 
12 class ConstantUnion {
13 public:
14     POOL_ALLOCATOR_NEW_DELETE();
ConstantUnion()15     ConstantUnion()
16     {
17         iConst = 0;
18         type = EbtVoid;
19     }
20 
setIConst(int i)21     void setIConst(int i) {iConst = i; type = EbtInt; }
setUConst(unsigned int u)22     void setUConst(unsigned int u) { uConst = u; type = EbtUInt; }
setFConst(float f)23     void setFConst(float f) {fConst = f; type = EbtFloat; }
setBConst(bool b)24     void setBConst(bool b) {bConst = b; type = EbtBool; }
25 
getIConst()26     int getIConst() const { return iConst; }
getUConst()27     unsigned int getUConst() const { return uConst; }
getFConst()28     float getFConst() const { return fConst; }
getBConst()29     bool getBConst() const { return bConst; }
30 
31     bool operator==(const int i) const
32     {
33         return i == iConst;
34     }
35 
36     bool operator==(const unsigned int u) const
37     {
38         return u == uConst;
39     }
40 
41     bool operator==(const float f) const
42     {
43         return f == fConst;
44     }
45 
46     bool operator==(const bool b) const
47     {
48         return b == bConst;
49     }
50 
51     bool operator==(const ConstantUnion& constant) const
52     {
53         if (constant.type != type)
54             return false;
55 
56         switch (type) {
57         case EbtInt:
58             return constant.iConst == iConst;
59         case EbtUInt:
60             return constant.uConst == uConst;
61         case EbtFloat:
62             return constant.fConst == fConst;
63         case EbtBool:
64             return constant.bConst == bConst;
65         default:
66             return false;
67         }
68     }
69 
70     bool operator!=(const int i) const
71     {
72         return !operator==(i);
73     }
74 
75     bool operator!=(const unsigned int u) const
76     {
77         return !operator==(u);
78     }
79 
80     bool operator!=(const float f) const
81     {
82         return !operator==(f);
83     }
84 
85     bool operator!=(const bool b) const
86     {
87         return !operator==(b);
88     }
89 
90     bool operator!=(const ConstantUnion& constant) const
91     {
92         return !operator==(constant);
93     }
94 
95     bool operator>(const ConstantUnion& constant) const
96     {
97         assert(type == constant.type);
98         switch (type) {
99         case EbtInt:
100             return iConst > constant.iConst;
101         case EbtUInt:
102             return uConst > constant.uConst;
103         case EbtFloat:
104             return fConst > constant.fConst;
105         default:
106             return false;   // Invalid operation, handled at semantic analysis
107         }
108     }
109 
110     bool operator<(const ConstantUnion& constant) const
111     {
112         assert(type == constant.type);
113         switch (type) {
114         case EbtInt:
115             return iConst < constant.iConst;
116         case EbtUInt:
117             return uConst < constant.uConst;
118         case EbtFloat:
119             return fConst < constant.fConst;
120         default:
121             return false;   // Invalid operation, handled at semantic analysis
122         }
123     }
124 
125     ConstantUnion operator+(const ConstantUnion& constant) const
126     {
127         ConstantUnion returnValue;
128         assert(type == constant.type);
129         switch (type) {
130         case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
131         case EbtUInt: returnValue.setUConst(uConst + constant.uConst); break;
132         case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
133         default: assert(false && "Default missing");
134         }
135 
136         return returnValue;
137     }
138 
139     ConstantUnion operator-(const ConstantUnion& constant) const
140     {
141         ConstantUnion returnValue;
142         assert(type == constant.type);
143         switch (type) {
144         case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
145         case EbtUInt: returnValue.setUConst(uConst - constant.uConst); break;
146         case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
147         default: assert(false && "Default missing");
148         }
149 
150         return returnValue;
151     }
152 
153     ConstantUnion operator*(const ConstantUnion& constant) const
154     {
155         ConstantUnion returnValue;
156         assert(type == constant.type);
157         switch (type) {
158         case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
159         case EbtUInt: returnValue.setUConst(uConst * constant.uConst); break;
160         case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
161         default: assert(false && "Default missing");
162         }
163 
164         return returnValue;
165     }
166 
167     ConstantUnion operator%(const ConstantUnion& constant) const
168     {
169         ConstantUnion returnValue;
170         assert(type == constant.type);
171         switch (type) {
172         case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
173         case EbtUInt: returnValue.setUConst(uConst % constant.uConst); break;
174         default:     assert(false && "Default missing");
175         }
176 
177         return returnValue;
178     }
179 
180     ConstantUnion operator>>(const ConstantUnion& constant) const
181     {
182         ConstantUnion returnValue;
183         assert(type == constant.type);
184         switch (type) {
185         case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
186         case EbtUInt: returnValue.setUConst(uConst >> constant.uConst); break;
187         default:     assert(false && "Default missing");
188         }
189 
190         return returnValue;
191     }
192 
193     ConstantUnion operator<<(const ConstantUnion& constant) const
194     {
195         ConstantUnion returnValue;
196         assert(type == constant.type);
197         switch (type) {
198         case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
199         case EbtUInt: returnValue.setUConst(uConst << constant.uConst); break;
200         default:     assert(false && "Default missing");
201         }
202 
203         return returnValue;
204     }
205 
206     ConstantUnion operator&(const ConstantUnion& constant) const
207     {
208         ConstantUnion returnValue;
209         assert(type == constant.type);
210         switch (type) {
211         case EbtInt:  returnValue.setIConst(iConst & constant.iConst); break;
212         case EbtUInt:  returnValue.setUConst(uConst & constant.uConst); break;
213         default:     assert(false && "Default missing");
214         }
215 
216         return returnValue;
217     }
218 
219     ConstantUnion operator|(const ConstantUnion& constant) const
220     {
221         ConstantUnion returnValue;
222         assert(type == constant.type);
223         switch (type) {
224         case EbtInt:  returnValue.setIConst(iConst | constant.iConst); break;
225         case EbtUInt:  returnValue.setUConst(uConst | constant.uConst); break;
226         default:     assert(false && "Default missing");
227         }
228 
229         return returnValue;
230     }
231 
232     ConstantUnion operator^(const ConstantUnion& constant) const
233     {
234         ConstantUnion returnValue;
235         assert(type == constant.type);
236         switch (type) {
237         case EbtInt:  returnValue.setIConst(iConst ^ constant.iConst); break;
238         case EbtUInt:  returnValue.setUConst(uConst ^ constant.uConst); break;
239         default:     assert(false && "Default missing");
240         }
241 
242         return returnValue;
243     }
244 
245     ConstantUnion operator&&(const ConstantUnion& constant) const
246     {
247         ConstantUnion returnValue;
248         assert(type == constant.type);
249         switch (type) {
250         case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
251         default:     assert(false && "Default missing");
252         }
253 
254         return returnValue;
255     }
256 
257     ConstantUnion operator||(const ConstantUnion& constant) const
258     {
259         ConstantUnion returnValue;
260         assert(type == constant.type);
261         switch (type) {
262         case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
263         default:     assert(false && "Default missing");
264         }
265 
266         return returnValue;
267     }
268 
getType()269     TBasicType getType() const { return type; }
270 private:
271 
272     union  {
273         int iConst;  // used for ivec, scalar ints
274         unsigned int uConst; // used for uvec, scalar uints
275         bool bConst; // used for bvec, scalar bools
276         float fConst;   // used for vec, mat, scalar floats
277     } ;
278 
279     TBasicType type;
280 };
281 
282 #endif // _CONSTANT_UNION_INCLUDED_
283