initial import
[vuplus_webkit] / Source / ThirdParty / ANGLE / src / compiler / ConstantUnion.h
1 //
2 // Copyright (c) 2002-2010 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
15     POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)        
16     void setIConst(int i) {iConst = i; type = EbtInt; }
17     void setFConst(float f) {fConst = f; type = EbtFloat; }
18     void setBConst(bool b) {bConst = b; type = EbtBool; }
19
20     int getIConst() { return iConst; }
21     float getFConst() { return fConst; }
22     bool getBConst() { return bConst; }
23     int getIConst() const { return iConst; }
24     float getFConst() const { return fConst; }
25     bool getBConst() const { return bConst; }
26
27     bool operator==(const int i) const
28     {
29         return i == iConst;
30     }
31
32     bool operator==(const float f) const
33     {
34         return f == fConst;
35     }
36
37     bool operator==(const bool b) const
38     {
39         return b == bConst;
40     }
41
42     bool operator==(const ConstantUnion& constant) const
43     {
44         if (constant.type != type)
45             return false;
46
47         switch (type) {
48         case EbtInt:
49             return constant.iConst == iConst;
50         case EbtFloat:
51             return constant.fConst == fConst;
52         case EbtBool:
53             return constant.bConst == bConst;
54         default:
55             return false;
56         }
57
58         return false;
59     }
60
61     bool operator!=(const int i) const
62     {
63         return !operator==(i);
64     }
65
66     bool operator!=(const float f) const
67     {
68         return !operator==(f);
69     }
70
71     bool operator!=(const bool b) const
72     {
73         return !operator==(b);
74     }
75
76     bool operator!=(const ConstantUnion& constant) const
77     {
78         return !operator==(constant);
79     }
80
81     bool operator>(const ConstantUnion& constant) const
82     { 
83         assert(type == constant.type);
84         switch (type) {
85         case EbtInt:
86             return iConst > constant.iConst;
87         case EbtFloat:
88             return fConst > constant.fConst;
89         default:
90             return false;   // Invalid operation, handled at semantic analysis
91         }
92
93         return false;
94     }
95
96     bool operator<(const ConstantUnion& constant) const
97     { 
98         assert(type == constant.type);
99         switch (type) {
100         case EbtInt:
101             return iConst < constant.iConst;
102         case EbtFloat:
103             return fConst < constant.fConst;
104         default:
105             return false;   // Invalid operation, handled at semantic analysis
106         }
107
108         return false;
109     }
110
111     ConstantUnion operator+(const ConstantUnion& constant) const
112     { 
113         ConstantUnion returnValue;
114         assert(type == constant.type);
115         switch (type) {
116         case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
117         case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
118         default: assert(false && "Default missing");
119         }
120
121         return returnValue;
122     }
123
124     ConstantUnion operator-(const ConstantUnion& constant) const
125     { 
126         ConstantUnion returnValue;
127         assert(type == constant.type);
128         switch (type) {
129         case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
130         case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
131         default: assert(false && "Default missing");
132         }
133
134         return returnValue;
135     }
136
137     ConstantUnion operator*(const ConstantUnion& constant) const
138     { 
139         ConstantUnion returnValue;
140         assert(type == constant.type);
141         switch (type) {
142         case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
143         case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break; 
144         default: assert(false && "Default missing");
145         }
146
147         return returnValue;
148     }
149
150     ConstantUnion operator%(const ConstantUnion& constant) const
151     { 
152         ConstantUnion returnValue;
153         assert(type == constant.type);
154         switch (type) {
155         case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
156         default:     assert(false && "Default missing");
157         }
158
159         return returnValue;
160     }
161
162     ConstantUnion operator>>(const ConstantUnion& constant) const
163     { 
164         ConstantUnion returnValue;
165         assert(type == constant.type);
166         switch (type) {
167         case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
168         default:     assert(false && "Default missing");
169         }
170
171         return returnValue;
172     }
173
174     ConstantUnion operator<<(const ConstantUnion& constant) const
175     { 
176         ConstantUnion returnValue;
177         assert(type == constant.type);
178         switch (type) {
179         case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
180         default:     assert(false && "Default missing");
181         }
182
183         return returnValue;
184     }
185
186     ConstantUnion operator&(const ConstantUnion& constant) const
187     { 
188         ConstantUnion returnValue;
189         assert(type == constant.type);
190         switch (type) {
191         case EbtInt:  returnValue.setIConst(iConst & constant.iConst); break;
192         default:     assert(false && "Default missing");
193         }
194
195         return returnValue;
196     }
197
198     ConstantUnion operator|(const ConstantUnion& constant) const
199     { 
200         ConstantUnion returnValue;
201         assert(type == constant.type);
202         switch (type) {
203         case EbtInt:  returnValue.setIConst(iConst | constant.iConst); break;
204         default:     assert(false && "Default missing");
205         }
206
207         return returnValue;
208     }
209
210     ConstantUnion operator^(const ConstantUnion& constant) const
211     { 
212         ConstantUnion returnValue;
213         assert(type == constant.type);
214         switch (type) {
215         case EbtInt:  returnValue.setIConst(iConst ^ constant.iConst); break;
216         default:     assert(false && "Default missing");
217         }
218
219         return returnValue;
220     }
221
222     ConstantUnion operator&&(const ConstantUnion& constant) const
223     { 
224         ConstantUnion returnValue;
225         assert(type == constant.type);
226         switch (type) {
227         case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
228         default:     assert(false && "Default missing");
229         }
230
231         return returnValue;
232     }
233
234     ConstantUnion operator||(const ConstantUnion& constant) const
235     { 
236         ConstantUnion returnValue;
237         assert(type == constant.type);
238         switch (type) {
239         case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
240         default:     assert(false && "Default missing");
241         }
242
243         return returnValue;
244     }
245
246     TBasicType getType() const { return type; }
247 private:
248
249     union  {
250         int iConst;  // used for ivec, scalar ints
251         bool bConst; // used for bvec, scalar bools
252         float fConst;   // used for vec, mat, scalar floats
253     } ;
254
255     TBasicType type;
256 };
257
258 #endif // _CONSTANT_UNION_INCLUDED_