blob: c2631de61ecaa5e64aab6c09659cbe6370bf699e [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
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000010#include <assert.h>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000012class ConstantUnion {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013public:
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 if (i == iConst)
30 return true;
31
32 return false;
33 }
34
35 bool operator==(const float f) const
36 {
37 if (f == fConst)
38 return true;
39
40 return false;
41 }
42
43 bool operator==(const bool b) const
44 {
45 if (b == bConst)
46 return true;
47
48 return false;
49 }
50
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000051 bool operator==(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052 {
53 if (constant.type != type)
54 return false;
55
56 switch (type) {
57 case EbtInt:
58 if (constant.iConst == iConst)
59 return true;
60
61 break;
62 case EbtFloat:
63 if (constant.fConst == fConst)
64 return true;
65
66 break;
67 case EbtBool:
68 if (constant.bConst == bConst)
69 return true;
70
71 break;
72 }
73
74 return false;
75 }
76
77 bool operator!=(const int i) const
78 {
79 return !operator==(i);
80 }
81
82 bool operator!=(const float f) const
83 {
84 return !operator==(f);
85 }
86
87 bool operator!=(const bool b) const
88 {
89 return !operator==(b);
90 }
91
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000092 bool operator!=(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093 {
94 return !operator==(constant);
95 }
96
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000097 bool operator>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098 {
99 assert(type == constant.type);
100 switch (type) {
101 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000102 return iConst > constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000104 return fConst > constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000106 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107 }
108
109 return false;
110 }
111
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000112 bool operator<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113 {
114 assert(type == constant.type);
115 switch (type) {
116 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000117 return iConst < constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000119 return fConst < constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000121 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122 }
123
124 return false;
125 }
126
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000127 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000129 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 assert(type == constant.type);
131 switch (type) {
132 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
133 case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
134 default: assert(false && "Default missing");
135 }
136
137 return returnValue;
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 default: assert(false && "Default missing");
173 }
174
175 return returnValue;
176 }
177
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000178 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000180 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 assert(type == constant.type);
182 switch (type) {
183 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
184 default: assert(false && "Default missing");
185 }
186
187 return returnValue;
188 }
189
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000190 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000192 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 assert(type == constant.type);
194 switch (type) {
195 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
196 default: assert(false && "Default missing");
197 }
198
199 return returnValue;
200 }
201
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000202 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000204 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205 assert(type == constant.type);
206 switch (type) {
207 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
208 default: assert(false && "Default missing");
209 }
210
211 return returnValue;
212 }
213
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000214 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000216 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217 assert(type == constant.type);
218 switch (type) {
219 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
220 default: assert(false && "Default missing");
221 }
222
223 return returnValue;
224 }
225
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000226 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000228 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229 assert(type == constant.type);
230 switch (type) {
231 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
232 default: assert(false && "Default missing");
233 }
234
235 return returnValue;
236 }
237
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000238 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000240 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241 assert(type == constant.type);
242 switch (type) {
243 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
244 default: assert(false && "Default missing");
245 }
246
247 return returnValue;
248 }
249
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000250 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000252 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253 assert(type == constant.type);
254 switch (type) {
255 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
256 default: assert(false && "Default missing");
257 }
258
259 return returnValue;
260 }
261
alokp@chromium.org76b82082010-03-24 17:59:39 +0000262 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263private:
264
265 union {
266 int iConst; // used for ivec, scalar ints
267 bool bConst; // used for bvec, scalar bools
268 float fConst; // used for vec, mat, scalar floats
269 } ;
270
271 TBasicType type;
272};
273
274#endif // _CONSTANT_UNION_INCLUDED_