1 // 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 10 #include <assert.h> 11 12 class ConstantUnion { 13 public: 14 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)15 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator) 16 void setIConst(int i) {iConst = i; type = EbtInt; } setFConst(float f)17 void setFConst(float f) {fConst = f; type = EbtFloat; } setBConst(bool b)18 void setBConst(bool b) {bConst = b; type = EbtBool; } 19 getIConst()20 int getIConst() { return iConst; } getFConst()21 float getFConst() { return fConst; } getBConst()22 bool getBConst() { return bConst; } getIConst()23 int getIConst() const { return iConst; } getFConst()24 float getFConst() const { return fConst; } getBConst()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 51 bool operator==(const ConstantUnion& constant) const 52 { 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 92 bool operator!=(const ConstantUnion& constant) const 93 { 94 return !operator==(constant); 95 } 96 97 bool operator>(const ConstantUnion& constant) const 98 { 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 119 bool operator<(const ConstantUnion& constant) const 120 { 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 141 ConstantUnion operator+(const ConstantUnion& constant) const 142 { 143 ConstantUnion returnValue; 144 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 154 ConstantUnion operator-(const ConstantUnion& constant) const 155 { 156 ConstantUnion returnValue; 157 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 167 ConstantUnion operator*(const ConstantUnion& constant) const 168 { 169 ConstantUnion returnValue; 170 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 180 ConstantUnion operator%(const ConstantUnion& constant) const 181 { 182 ConstantUnion returnValue; 183 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 192 ConstantUnion operator>>(const ConstantUnion& constant) const 193 { 194 ConstantUnion returnValue; 195 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 204 ConstantUnion operator<<(const ConstantUnion& constant) const 205 { 206 ConstantUnion returnValue; 207 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 216 ConstantUnion operator&(const ConstantUnion& constant) const 217 { 218 ConstantUnion returnValue; 219 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 228 ConstantUnion operator|(const ConstantUnion& constant) const 229 { 230 ConstantUnion returnValue; 231 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 240 ConstantUnion operator^(const ConstantUnion& constant) const 241 { 242 ConstantUnion returnValue; 243 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 252 ConstantUnion operator&&(const ConstantUnion& constant) const 253 { 254 ConstantUnion returnValue; 255 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 264 ConstantUnion operator||(const ConstantUnion& constant) const 265 { 266 ConstantUnion returnValue; 267 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 getType()276 TBasicType getType() const { return type; } 277 private: 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_ 289