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 | { |
daniel@transgaming.com | e90a0d5 | 2011-02-18 02:52:06 +0000 | [diff] [blame] | 29 | return i == iConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | bool operator==(const float f) const |
| 33 | { |
daniel@transgaming.com | e90a0d5 | 2011-02-18 02:52:06 +0000 | [diff] [blame] | 34 | return f == fConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool operator==(const bool b) const |
| 38 | { |
daniel@transgaming.com | e90a0d5 | 2011-02-18 02:52:06 +0000 | [diff] [blame] | 39 | return b == bConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 42 | bool operator==(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 43 | { |
| 44 | if (constant.type != type) |
| 45 | return false; |
| 46 | |
| 47 | switch (type) { |
| 48 | case EbtInt: |
daniel@transgaming.com | e90a0d5 | 2011-02-18 02:52:06 +0000 | [diff] [blame] | 49 | return constant.iConst == iConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | case EbtFloat: |
daniel@transgaming.com | e90a0d5 | 2011-02-18 02:52:06 +0000 | [diff] [blame] | 51 | return constant.fConst == fConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 52 | case EbtBool: |
daniel@transgaming.com | e90a0d5 | 2011-02-18 02:52:06 +0000 | [diff] [blame] | 53 | return constant.bConst == bConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | bool operator!=(const int i) const |
| 60 | { |
| 61 | return !operator==(i); |
| 62 | } |
| 63 | |
| 64 | bool operator!=(const float f) const |
| 65 | { |
| 66 | return !operator==(f); |
| 67 | } |
| 68 | |
| 69 | bool operator!=(const bool b) const |
| 70 | { |
| 71 | return !operator==(b); |
| 72 | } |
| 73 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 74 | bool operator!=(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 75 | { |
| 76 | return !operator==(constant); |
| 77 | } |
| 78 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 79 | bool operator>(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 80 | { |
| 81 | assert(type == constant.type); |
| 82 | switch (type) { |
| 83 | case EbtInt: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame] | 84 | return iConst > constant.iConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 85 | case EbtFloat: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame] | 86 | return fConst > constant.fConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 87 | default: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame] | 88 | return false; // Invalid operation, handled at semantic analysis |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 94 | bool operator<(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | { |
| 96 | assert(type == constant.type); |
| 97 | switch (type) { |
| 98 | case EbtInt: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame] | 99 | return iConst < constant.iConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 100 | case EbtFloat: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame] | 101 | return fConst < constant.fConst; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 102 | default: |
daniel@transgaming.com | b31f35a | 2011-02-11 13:19:35 +0000 | [diff] [blame] | 103 | return false; // Invalid operation, handled at semantic analysis |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 109 | ConstantUnion operator+(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 110 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 111 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 112 | assert(type == constant.type); |
| 113 | switch (type) { |
| 114 | case EbtInt: returnValue.setIConst(iConst + constant.iConst); break; |
| 115 | case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break; |
| 116 | default: assert(false && "Default missing"); |
| 117 | } |
| 118 | |
| 119 | return returnValue; |
| 120 | } |
| 121 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 122 | ConstantUnion operator-(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 123 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 124 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 125 | assert(type == constant.type); |
| 126 | switch (type) { |
| 127 | case EbtInt: returnValue.setIConst(iConst - constant.iConst); break; |
| 128 | case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break; |
| 129 | default: assert(false && "Default missing"); |
| 130 | } |
| 131 | |
| 132 | return returnValue; |
| 133 | } |
| 134 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 135 | ConstantUnion operator*(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 137 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 138 | assert(type == constant.type); |
| 139 | switch (type) { |
| 140 | case EbtInt: returnValue.setIConst(iConst * constant.iConst); break; |
| 141 | case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break; |
| 142 | default: assert(false && "Default missing"); |
| 143 | } |
| 144 | |
| 145 | return returnValue; |
| 146 | } |
| 147 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 148 | ConstantUnion operator%(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 149 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 150 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | assert(type == constant.type); |
| 152 | switch (type) { |
| 153 | case EbtInt: returnValue.setIConst(iConst % constant.iConst); break; |
| 154 | default: assert(false && "Default missing"); |
| 155 | } |
| 156 | |
| 157 | return returnValue; |
| 158 | } |
| 159 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 160 | ConstantUnion operator>>(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 161 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 162 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 163 | assert(type == constant.type); |
| 164 | switch (type) { |
| 165 | case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break; |
| 166 | default: assert(false && "Default missing"); |
| 167 | } |
| 168 | |
| 169 | return returnValue; |
| 170 | } |
| 171 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 172 | ConstantUnion operator<<(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 173 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 174 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 175 | assert(type == constant.type); |
| 176 | switch (type) { |
| 177 | case EbtInt: returnValue.setIConst(iConst << constant.iConst); break; |
| 178 | default: assert(false && "Default missing"); |
| 179 | } |
| 180 | |
| 181 | return returnValue; |
| 182 | } |
| 183 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 184 | ConstantUnion operator&(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 185 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 186 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 187 | assert(type == constant.type); |
| 188 | switch (type) { |
| 189 | case EbtInt: returnValue.setIConst(iConst & constant.iConst); break; |
| 190 | default: assert(false && "Default missing"); |
| 191 | } |
| 192 | |
| 193 | return returnValue; |
| 194 | } |
| 195 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 196 | ConstantUnion operator|(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 197 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 198 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 199 | assert(type == constant.type); |
| 200 | switch (type) { |
| 201 | case EbtInt: returnValue.setIConst(iConst | constant.iConst); break; |
| 202 | default: assert(false && "Default missing"); |
| 203 | } |
| 204 | |
| 205 | return returnValue; |
| 206 | } |
| 207 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 208 | ConstantUnion operator^(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 209 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 210 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 211 | assert(type == constant.type); |
| 212 | switch (type) { |
| 213 | case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break; |
| 214 | default: assert(false && "Default missing"); |
| 215 | } |
| 216 | |
| 217 | return returnValue; |
| 218 | } |
| 219 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 220 | ConstantUnion operator&&(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 221 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 222 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 223 | assert(type == constant.type); |
| 224 | switch (type) { |
| 225 | case EbtBool: returnValue.setBConst(bConst && constant.bConst); break; |
| 226 | default: assert(false && "Default missing"); |
| 227 | } |
| 228 | |
| 229 | return returnValue; |
| 230 | } |
| 231 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 232 | ConstantUnion operator||(const ConstantUnion& constant) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 233 | { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 234 | ConstantUnion returnValue; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 235 | assert(type == constant.type); |
| 236 | switch (type) { |
| 237 | case EbtBool: returnValue.setBConst(bConst || constant.bConst); break; |
| 238 | default: assert(false && "Default missing"); |
| 239 | } |
| 240 | |
| 241 | return returnValue; |
| 242 | } |
| 243 | |
alokp@chromium.org | 76b8208 | 2010-03-24 17:59:39 +0000 | [diff] [blame] | 244 | TBasicType getType() const { return type; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 245 | private: |
| 246 | |
| 247 | union { |
| 248 | int iConst; // used for ivec, scalar ints |
| 249 | bool bConst; // used for bvec, scalar bools |
| 250 | float fConst; // used for vec, mat, scalar floats |
| 251 | } ; |
| 252 | |
| 253 | TBasicType type; |
| 254 | }; |
| 255 | |
| 256 | #endif // _CONSTANT_UNION_INCLUDED_ |