daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 10 | #include <assert.h> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 11 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 12 | class ConstantUnion { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 13 | public: |
| 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 | { |
| 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 51 | bool operator==(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 92 | bool operator!=(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | { |
| 94 | return !operator==(constant); |
| 95 | } |
| 96 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 97 | bool operator>(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 119 | bool operator<(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 141 | ConstantUnion operator+(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 143 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 154 | ConstantUnion operator-(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 155 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 156 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 167 | ConstantUnion operator*(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 168 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 169 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 180 | ConstantUnion operator%(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 181 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 182 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 192 | ConstantUnion operator>>(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 193 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 194 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 204 | ConstantUnion operator<<(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 205 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 206 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 216 | ConstantUnion operator&(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 217 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 218 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 228 | ConstantUnion operator|(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 229 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 230 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 240 | ConstantUnion operator^(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 241 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 242 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 252 | ConstantUnion operator&&(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 253 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 254 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 264 | ConstantUnion operator||(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 265 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 266 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 76b8208 | 2010-03-24 17:59:39 +0000 | [diff] [blame] | 276 | TBasicType getType() const { return type; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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_ |