blob: 5b986b877ae8a13e4dd50ff0430c514aaa225055 [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.com4f39fd92010-03-08 20:26:45 +000054 }
55
56 return false;
57 }
58
59 bool operator!=(const int i) const
60 {
61 return !operator==(i);
62 }
63
64 bool operator!=(const float f) const
65 {
66 return !operator==(f);
67 }
68
69 bool operator!=(const bool b) const
70 {
71 return !operator==(b);
72 }
73
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000074 bool operator!=(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075 {
76 return !operator==(constant);
77 }
78
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000079 bool operator>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080 {
81 assert(type == constant.type);
82 switch (type) {
83 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000084 return iConst > constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000086 return fConst > constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000088 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089 }
90
91 return false;
92 }
93
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000094 bool operator<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 {
96 assert(type == constant.type);
97 switch (type) {
98 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000099 return iConst < constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000101 return fConst < constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000103 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104 }
105
106 return false;
107 }
108
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000109 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000111 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112 assert(type == constant.type);
113 switch (type) {
114 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
115 case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
116 default: assert(false && "Default missing");
117 }
118
119 return returnValue;
120 }
121
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000122 ConstantUnion operator-(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000124 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125 assert(type == constant.type);
126 switch (type) {
127 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
128 case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
129 default: assert(false && "Default missing");
130 }
131
132 return returnValue;
133 }
134
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000135 ConstantUnion operator*(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000137 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 assert(type == constant.type);
139 switch (type) {
140 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
141 case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
142 default: assert(false && "Default missing");
143 }
144
145 return returnValue;
146 }
147
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000148 ConstantUnion operator%(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000150 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 assert(type == constant.type);
152 switch (type) {
153 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
154 default: assert(false && "Default missing");
155 }
156
157 return returnValue;
158 }
159
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000160 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000162 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163 assert(type == constant.type);
164 switch (type) {
165 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
166 default: assert(false && "Default missing");
167 }
168
169 return returnValue;
170 }
171
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000172 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000174 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175 assert(type == constant.type);
176 switch (type) {
177 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
178 default: assert(false && "Default missing");
179 }
180
181 return returnValue;
182 }
183
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000184 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000186 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187 assert(type == constant.type);
188 switch (type) {
189 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
190 default: assert(false && "Default missing");
191 }
192
193 return returnValue;
194 }
195
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000196 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000197 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000198 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 assert(type == constant.type);
200 switch (type) {
201 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
202 default: assert(false && "Default missing");
203 }
204
205 return returnValue;
206 }
207
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000208 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000210 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211 assert(type == constant.type);
212 switch (type) {
213 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
214 default: assert(false && "Default missing");
215 }
216
217 return returnValue;
218 }
219
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000220 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000222 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223 assert(type == constant.type);
224 switch (type) {
225 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
226 default: assert(false && "Default missing");
227 }
228
229 return returnValue;
230 }
231
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000232 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000234 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235 assert(type == constant.type);
236 switch (type) {
237 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
238 default: assert(false && "Default missing");
239 }
240
241 return returnValue;
242 }
243
alokp@chromium.org76b82082010-03-24 17:59:39 +0000244 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245private:
246
247 union {
248 int iConst; // used for ivec, scalar ints
249 bool bConst; // used for bvec, scalar bools
250 float fConst; // used for vec, mat, scalar floats
251 } ;
252
253 TBasicType type;
254};
255
256#endif // _CONSTANT_UNION_INCLUDED_