blob: d28a11b8b8f617b93e2a437925777a9cddff0a64 [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 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000029 return i == iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030 }
31
32 bool operator==(const float f) const
33 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000034 return f == fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035 }
36
37 bool operator==(const bool b) const
38 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000039 return b == bConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040 }
41
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000042 bool operator==(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043 {
44 if (constant.type != type)
45 return false;
46
47 switch (type) {
48 case EbtInt:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000049 return constant.iConst == iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 case EbtFloat:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000051 return constant.fConst == fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052 case EbtBool:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000053 return constant.bConst == bConst;
daniel@transgaming.comcd3a1b92011-03-15 18:23:46 +000054 default:
55 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056 }
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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000076 bool operator!=(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077 {
78 return !operator==(constant);
79 }
80
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000081 bool operator>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000082 {
83 assert(type == constant.type);
84 switch (type) {
85 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000086 return iConst > constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000088 return fConst > constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000090 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091 }
92
93 return false;
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:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000101 return iConst < constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000103 return fConst < constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000105 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106 }
107
108 return false;
109 }
110
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000111 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000113 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000124 ConstantUnion operator-(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000126 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000137 ConstantUnion operator*(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000139 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000150 ConstantUnion operator%(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000152 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000162 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000164 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000165 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000174 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000176 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000186 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000188 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000198 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000200 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000210 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000212 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000222 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000224 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225 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
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000234 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000236 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237 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
alokp@chromium.org76b82082010-03-24 17:59:39 +0000246 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247private:
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_