1 // 2 // Copyright (c) 2002-2013 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 10 #include <assert.h> 11 12 class ConstantUnion { 13 public: 14 POOL_ALLOCATOR_NEW_DELETE(); 15 ConstantUnion() 16 { 17 iConst = 0; 18 type = EbtVoid; 19 } 20 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 { 34 return i == iConst; 35 } 36 37 bool operator==(const float f) const 38 { 39 return f == fConst; 40 } 41 42 bool operator==(const bool b) const 43 { 44 return b == bConst; 45 } 46 47 bool operator==(const ConstantUnion& constant) const 48 { 49 if (constant.type != type) 50 return false; 51 52 switch (type) { 53 case EbtInt: 54 return constant.iConst == iConst; 55 case EbtFloat: 56 return constant.fConst == fConst; 57 case EbtBool: 58 return constant.bConst == bConst; 59 default: 60 return false; 61 } 62 } 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 79 bool operator!=(const ConstantUnion& constant) const 80 { 81 return !operator==(constant); 82 } 83 84 bool operator>(const ConstantUnion& constant) const 85 { 86 assert(type == constant.type); 87 switch (type) { 88 case EbtInt: 89 return iConst > constant.iConst; 90 case EbtFloat: 91 return fConst > constant.fConst; 92 default: 93 return false; // Invalid operation, handled at semantic analysis 94 } 95 } 96 97 bool operator<(const ConstantUnion& constant) const 98 { 99 assert(type == constant.type); 100 switch (type) { 101 case EbtInt: 102 return iConst < constant.iConst; 103 case EbtFloat: 104 return fConst < constant.fConst; 105 default: 106 return false; // Invalid operation, handled at semantic analysis 107 } 108 } 109 110 ConstantUnion operator+(const ConstantUnion& constant) const 111 { 112 ConstantUnion returnValue; 113 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 123 ConstantUnion operator-(const ConstantUnion& constant) const 124 { 125 ConstantUnion returnValue; 126 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 136 ConstantUnion operator*(const ConstantUnion& constant) const 137 { 138 ConstantUnion returnValue; 139 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 149 ConstantUnion operator%(const ConstantUnion& constant) const 150 { 151 ConstantUnion returnValue; 152 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 161 ConstantUnion operator>>(const ConstantUnion& constant) const 162 { 163 ConstantUnion returnValue; 164 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 173 ConstantUnion operator<<(const ConstantUnion& constant) const 174 { 175 ConstantUnion returnValue; 176 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 185 ConstantUnion operator&(const ConstantUnion& constant) const 186 { 187 ConstantUnion returnValue; 188 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 197 ConstantUnion operator|(const ConstantUnion& constant) const 198 { 199 ConstantUnion returnValue; 200 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 209 ConstantUnion operator^(const ConstantUnion& constant) const 210 { 211 ConstantUnion returnValue; 212 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 221 ConstantUnion operator&&(const ConstantUnion& constant) const 222 { 223 ConstantUnion returnValue; 224 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 233 ConstantUnion operator||(const ConstantUnion& constant) const 234 { 235 ConstantUnion returnValue; 236 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 245 TBasicType getType() const { return type; } 246 private: 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_ 258