blob: 1fdb61de4bd8d03c26309c4848e59c0912e78518 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000011class ConstantUnion {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012public:
13
14 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
15 void setIConst(int i) {iConst = i; type = EbtInt; }
16 void setFConst(float f) {fConst = f; type = EbtFloat; }
17 void setBConst(bool b) {bConst = b; type = EbtBool; }
18
19 int getIConst() { return iConst; }
20 float getFConst() { return fConst; }
21 bool getBConst() { return bConst; }
22 int getIConst() const { return iConst; }
23 float getFConst() const { return fConst; }
24 bool getBConst() const { return bConst; }
25
26 bool operator==(const int i) const
27 {
28 if (i == iConst)
29 return true;
30
31 return false;
32 }
33
34 bool operator==(const float f) const
35 {
36 if (f == fConst)
37 return true;
38
39 return false;
40 }
41
42 bool operator==(const bool b) const
43 {
44 if (b == bConst)
45 return true;
46
47 return false;
48 }
49
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000050 bool operator==(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051 {
52 if (constant.type != type)
53 return false;
54
55 switch (type) {
56 case EbtInt:
57 if (constant.iConst == iConst)
58 return true;
59
60 break;
61 case EbtFloat:
62 if (constant.fConst == fConst)
63 return true;
64
65 break;
66 case EbtBool:
67 if (constant.bConst == bConst)
68 return true;
69
70 break;
71 }
72
73 return false;
74 }
75
76 bool operator!=(const int i) const
77 {
78 return !operator==(i);
79 }
80
81 bool operator!=(const float f) const
82 {
83 return !operator==(f);
84 }
85
86 bool operator!=(const bool b) const
87 {
88 return !operator==(b);
89 }
90
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000091 bool operator!=(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092 {
93 return !operator==(constant);
94 }
95
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000096 bool operator>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097 {
98 assert(type == constant.type);
99 switch (type) {
100 case EbtInt:
101 if (iConst > constant.iConst)
102 return true;
103
104 return false;
105 case EbtFloat:
106 if (fConst > constant.fConst)
107 return true;
108
109 return false;
110 default:
111 assert(false && "Default missing");
112 return false;
113 }
114
115 return false;
116 }
117
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000118 bool operator<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 {
120 assert(type == constant.type);
121 switch (type) {
122 case EbtInt:
123 if (iConst < constant.iConst)
124 return true;
125
126 return false;
127 case EbtFloat:
128 if (fConst < constant.fConst)
129 return true;
130
131 return false;
132 default:
133 assert(false && "Default missing");
134 return false;
135 }
136
137 return false;
138 }
139
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000140 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000142 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143 assert(type == constant.type);
144 switch (type) {
145 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
146 case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
147 default: assert(false && "Default missing");
148 }
149
150 return returnValue;
151 }
152
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000153 ConstantUnion operator-(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000155 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156 assert(type == constant.type);
157 switch (type) {
158 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
159 case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
160 default: assert(false && "Default missing");
161 }
162
163 return returnValue;
164 }
165
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000166 ConstantUnion operator*(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000168 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 assert(type == constant.type);
170 switch (type) {
171 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
172 case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
173 default: assert(false && "Default missing");
174 }
175
176 return returnValue;
177 }
178
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000179 ConstantUnion operator%(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000181 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182 assert(type == constant.type);
183 switch (type) {
184 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
185 default: assert(false && "Default missing");
186 }
187
188 return returnValue;
189 }
190
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000191 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000193 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194 assert(type == constant.type);
195 switch (type) {
196 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
197 default: assert(false && "Default missing");
198 }
199
200 return returnValue;
201 }
202
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000203 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000205 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206 assert(type == constant.type);
207 switch (type) {
208 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
209 default: assert(false && "Default missing");
210 }
211
212 return returnValue;
213 }
214
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000215 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000217 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218 assert(type == constant.type);
219 switch (type) {
220 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
221 default: assert(false && "Default missing");
222 }
223
224 return returnValue;
225 }
226
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000227 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000229 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230 assert(type == constant.type);
231 switch (type) {
232 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
233 default: assert(false && "Default missing");
234 }
235
236 return returnValue;
237 }
238
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000239 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000241 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242 assert(type == constant.type);
243 switch (type) {
244 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
245 default: assert(false && "Default missing");
246 }
247
248 return returnValue;
249 }
250
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000251 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000253 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254 assert(type == constant.type);
255 switch (type) {
256 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
257 default: assert(false && "Default missing");
258 }
259
260 return returnValue;
261 }
262
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000263 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000265 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266 assert(type == constant.type);
267 switch (type) {
268 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
269 default: assert(false && "Default missing");
270 }
271
272 return returnValue;
273 }
274
alokp@chromium.org76b82082010-03-24 17:59:39 +0000275 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276private:
277
278 union {
279 int iConst; // used for ivec, scalar ints
280 bool bConst; // used for bvec, scalar bools
281 float fConst; // used for vec, mat, scalar floats
282 } ;
283
284 TBasicType type;
285};
286
287#endif // _CONSTANT_UNION_INCLUDED_