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: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame^] | 102 | return iConst > constant.iConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | case EbtFloat: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame^] | 104 | return fConst > constant.fConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 105 | default: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame^] | 106 | return false; // Invalid operation, handled at semantic analysis |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 112 | bool operator<(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | { |
| 114 | assert(type == constant.type); |
| 115 | switch (type) { |
| 116 | case EbtInt: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame^] | 117 | return iConst < constant.iConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 118 | case EbtFloat: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame^] | 119 | return fConst < constant.fConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 120 | default: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame^] | 121 | return false; // Invalid operation, handled at semantic analysis |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 127 | ConstantUnion operator+(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 128 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 129 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | assert(type == constant.type); |
| 131 | switch (type) { |
| 132 | case EbtInt: returnValue.setIConst(iConst + constant.iConst); break; |
| 133 | case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break; |
| 134 | default: assert(false && "Default missing"); |
| 135 | } |
| 136 | |
| 137 | return returnValue; |
| 138 | } |
| 139 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 140 | ConstantUnion operator-(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 142 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 143 | assert(type == constant.type); |
| 144 | switch (type) { |
| 145 | case EbtInt: returnValue.setIConst(iConst - constant.iConst); break; |
| 146 | case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break; |
| 147 | default: assert(false && "Default missing"); |
| 148 | } |
| 149 | |
| 150 | return returnValue; |
| 151 | } |
| 152 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 153 | ConstantUnion operator*(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 154 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 155 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 156 | assert(type == constant.type); |
| 157 | switch (type) { |
| 158 | case EbtInt: returnValue.setIConst(iConst * constant.iConst); break; |
| 159 | case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break; |
| 160 | default: assert(false && "Default missing"); |
| 161 | } |
| 162 | |
| 163 | return returnValue; |
| 164 | } |
| 165 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 166 | ConstantUnion operator%(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 167 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 168 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 169 | assert(type == constant.type); |
| 170 | switch (type) { |
| 171 | case EbtInt: returnValue.setIConst(iConst % constant.iConst); break; |
| 172 | default: assert(false && "Default missing"); |
| 173 | } |
| 174 | |
| 175 | return returnValue; |
| 176 | } |
| 177 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 178 | ConstantUnion operator>>(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 180 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 181 | assert(type == constant.type); |
| 182 | switch (type) { |
| 183 | case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break; |
| 184 | default: assert(false && "Default missing"); |
| 185 | } |
| 186 | |
| 187 | return returnValue; |
| 188 | } |
| 189 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 190 | ConstantUnion operator<<(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 191 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 192 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 193 | assert(type == constant.type); |
| 194 | switch (type) { |
| 195 | case EbtInt: returnValue.setIConst(iConst << constant.iConst); break; |
| 196 | default: assert(false && "Default missing"); |
| 197 | } |
| 198 | |
| 199 | return returnValue; |
| 200 | } |
| 201 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 202 | ConstantUnion operator&(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 203 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 204 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 205 | assert(type == constant.type); |
| 206 | switch (type) { |
| 207 | case EbtInt: returnValue.setIConst(iConst & constant.iConst); break; |
| 208 | default: assert(false && "Default missing"); |
| 209 | } |
| 210 | |
| 211 | return returnValue; |
| 212 | } |
| 213 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 214 | ConstantUnion operator|(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 215 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 216 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 217 | assert(type == constant.type); |
| 218 | switch (type) { |
| 219 | case EbtInt: returnValue.setIConst(iConst | constant.iConst); break; |
| 220 | default: assert(false && "Default missing"); |
| 221 | } |
| 222 | |
| 223 | return returnValue; |
| 224 | } |
| 225 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 226 | ConstantUnion operator^(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 227 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 228 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 229 | assert(type == constant.type); |
| 230 | switch (type) { |
| 231 | case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break; |
| 232 | default: assert(false && "Default missing"); |
| 233 | } |
| 234 | |
| 235 | return returnValue; |
| 236 | } |
| 237 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 238 | ConstantUnion operator&&(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 239 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 240 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 241 | assert(type == constant.type); |
| 242 | switch (type) { |
| 243 | case EbtBool: returnValue.setBConst(bConst && constant.bConst); break; |
| 244 | default: assert(false && "Default missing"); |
| 245 | } |
| 246 | |
| 247 | return returnValue; |
| 248 | } |
| 249 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 250 | ConstantUnion operator||(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 251 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 252 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 253 | assert(type == constant.type); |
| 254 | switch (type) { |
| 255 | case EbtBool: returnValue.setBConst(bConst || constant.bConst); break; |
| 256 | default: assert(false && "Default missing"); |
| 257 | } |
| 258 | |
| 259 | return returnValue; |
| 260 | } |
| 261 | |
alokp@chromium.org | 76b8208 | 2010-03-24 17:59:39 +0000 | [diff] [blame] | 262 | TBasicType getType() const { return type; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 263 | private: |
| 264 | |
| 265 | union { |
| 266 | int iConst; // used for ivec, scalar ints |
| 267 | bool bConst; // used for bvec, scalar bools |
| 268 | float fConst; // used for vec, mat, scalar floats |
| 269 | } ; |
| 270 | |
| 271 | TBasicType type; |
| 272 | }; |
| 273 | |
| 274 | #endif // _CONSTANT_UNION_INCLUDED_ |