blob: cf531ea40582363a5300ddcceee3ee701dd93d23 [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:
102 if (iConst > constant.iConst)
103 return true;
104
105 return false;
106 case EbtFloat:
107 if (fConst > constant.fConst)
108 return true;
109
110 return false;
111 default:
112 assert(false && "Default missing");
113 return false;
114 }
115
116 return false;
117 }
118
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000119 bool operator<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120 {
121 assert(type == constant.type);
122 switch (type) {
123 case EbtInt:
124 if (iConst < constant.iConst)
125 return true;
126
127 return false;
128 case EbtFloat:
129 if (fConst < constant.fConst)
130 return true;
131
132 return false;
133 default:
134 assert(false && "Default missing");
135 return false;
136 }
137
138 return false;
139 }
140
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000141 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000143 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144 assert(type == constant.type);
145 switch (type) {
146 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
147 case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
148 default: assert(false && "Default missing");
149 }
150
151 return returnValue;
152 }
153
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000154 ConstantUnion operator-(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000156 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157 assert(type == constant.type);
158 switch (type) {
159 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
160 case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
161 default: assert(false && "Default missing");
162 }
163
164 return returnValue;
165 }
166
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000167 ConstantUnion operator*(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000169 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170 assert(type == constant.type);
171 switch (type) {
172 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
173 case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
174 default: assert(false && "Default missing");
175 }
176
177 return returnValue;
178 }
179
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000180 ConstantUnion operator%(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000182 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183 assert(type == constant.type);
184 switch (type) {
185 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
186 default: assert(false && "Default missing");
187 }
188
189 return returnValue;
190 }
191
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000192 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000194 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000195 assert(type == constant.type);
196 switch (type) {
197 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
198 default: assert(false && "Default missing");
199 }
200
201 return returnValue;
202 }
203
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000204 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000206 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207 assert(type == constant.type);
208 switch (type) {
209 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
210 default: assert(false && "Default missing");
211 }
212
213 return returnValue;
214 }
215
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000216 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000218 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219 assert(type == constant.type);
220 switch (type) {
221 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
222 default: assert(false && "Default missing");
223 }
224
225 return returnValue;
226 }
227
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000228 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000230 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231 assert(type == constant.type);
232 switch (type) {
233 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
234 default: assert(false && "Default missing");
235 }
236
237 return returnValue;
238 }
239
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000240 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000242 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243 assert(type == constant.type);
244 switch (type) {
245 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
246 default: assert(false && "Default missing");
247 }
248
249 return returnValue;
250 }
251
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000252 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000254 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255 assert(type == constant.type);
256 switch (type) {
257 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
258 default: assert(false && "Default missing");
259 }
260
261 return returnValue;
262 }
263
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000264 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000266 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267 assert(type == constant.type);
268 switch (type) {
269 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
270 default: assert(false && "Default missing");
271 }
272
273 return returnValue;
274 }
275
alokp@chromium.org76b82082010-03-24 17:59:39 +0000276 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277private:
278
279 union {
280 int iConst; // used for ivec, scalar ints
281 bool bConst; // used for bvec, scalar bools
282 float fConst; // used for vec, mat, scalar floats
283 } ;
284
285 TBasicType type;
286};
287
288#endif // _CONSTANT_UNION_INCLUDED_