blob: 32af4d38b0b57a4910d322855ec38523df80bcd7 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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:
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000014 ConstantUnion()
15 {
16 iConst = 0;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +000017 type = EbtVoid;
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000018 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019
20 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
21 void setIConst(int i) {iConst = i; type = EbtInt; }
22 void setFConst(float f) {fConst = f; type = EbtFloat; }
23 void setBConst(bool b) {bConst = b; type = EbtBool; }
24
25 int getIConst() { return iConst; }
26 float getFConst() { return fConst; }
27 bool getBConst() { return bConst; }
28 int getIConst() const { return iConst; }
29 float getFConst() const { return fConst; }
30 bool getBConst() const { return bConst; }
31
32 bool operator==(const int i) const
33 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000034 return i == iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035 }
36
37 bool operator==(const float f) const
38 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000039 return f == fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040 }
41
42 bool operator==(const bool b) const
43 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000044 return b == bConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 }
46
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000047 bool operator==(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 {
49 if (constant.type != type)
50 return false;
51
52 switch (type) {
53 case EbtInt:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000054 return constant.iConst == iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055 case EbtFloat:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000056 return constant.fConst == fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057 case EbtBool:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000058 return constant.bConst == bConst;
daniel@transgaming.comcd3a1b92011-03-15 18:23:46 +000059 default:
60 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 }
63
64 bool operator!=(const int i) const
65 {
66 return !operator==(i);
67 }
68
69 bool operator!=(const float f) const
70 {
71 return !operator==(f);
72 }
73
74 bool operator!=(const bool b) const
75 {
76 return !operator==(b);
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 return !operator==(constant);
82 }
83
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000084 bool operator>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085 {
86 assert(type == constant.type);
87 switch (type) {
88 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000089 return iConst > constant.iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000091 return fConst > constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +000093 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 }
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 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108 }
109
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000110 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000112 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113 assert(type == constant.type);
114 switch (type) {
115 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
116 case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
117 default: assert(false && "Default missing");
118 }
119
120 return returnValue;
121 }
122
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000123 ConstantUnion operator-(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000125 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126 assert(type == constant.type);
127 switch (type) {
128 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
129 case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
130 default: assert(false && "Default missing");
131 }
132
133 return returnValue;
134 }
135
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000136 ConstantUnion operator*(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000138 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 assert(type == constant.type);
140 switch (type) {
141 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
142 case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
143 default: assert(false && "Default missing");
144 }
145
146 return returnValue;
147 }
148
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000149 ConstantUnion operator%(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000151 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152 assert(type == constant.type);
153 switch (type) {
154 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
155 default: assert(false && "Default missing");
156 }
157
158 return returnValue;
159 }
160
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000161 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000163 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164 assert(type == constant.type);
165 switch (type) {
166 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
167 default: assert(false && "Default missing");
168 }
169
170 return returnValue;
171 }
172
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000173 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000175 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176 assert(type == constant.type);
177 switch (type) {
178 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
179 default: assert(false && "Default missing");
180 }
181
182 return returnValue;
183 }
184
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000185 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000187 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188 assert(type == constant.type);
189 switch (type) {
190 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
191 default: assert(false && "Default missing");
192 }
193
194 return returnValue;
195 }
196
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000197 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000199 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200 assert(type == constant.type);
201 switch (type) {
202 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
203 default: assert(false && "Default missing");
204 }
205
206 return returnValue;
207 }
208
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000209 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000211 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212 assert(type == constant.type);
213 switch (type) {
214 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
215 default: assert(false && "Default missing");
216 }
217
218 return returnValue;
219 }
220
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000221 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000223 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224 assert(type == constant.type);
225 switch (type) {
226 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
227 default: assert(false && "Default missing");
228 }
229
230 return returnValue;
231 }
232
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000233 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000235 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236 assert(type == constant.type);
237 switch (type) {
238 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
239 default: assert(false && "Default missing");
240 }
241
242 return returnValue;
243 }
244
alokp@chromium.org76b82082010-03-24 17:59:39 +0000245 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246private:
247
248 union {
249 int iConst; // used for ivec, scalar ints
250 bool bConst; // used for bvec, scalar bools
251 float fConst; // used for vec, mat, scalar floats
252 } ;
253
254 TBasicType type;
255};
256
257#endif // _CONSTANT_UNION_INCLUDED_