gfx/angle/src/compiler/ConstantUnion.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/angle/src/compiler/ConstantUnion.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,257 @@
     1.4 +//
     1.5 +// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
     1.6 +// Use of this source code is governed by a BSD-style license that can be
     1.7 +// found in the LICENSE file.
     1.8 +//
     1.9 +
    1.10 +#ifndef _CONSTANT_UNION_INCLUDED_
    1.11 +#define _CONSTANT_UNION_INCLUDED_
    1.12 +
    1.13 +#include <assert.h>
    1.14 +
    1.15 +class ConstantUnion {
    1.16 +public:
    1.17 +    POOL_ALLOCATOR_NEW_DELETE();
    1.18 +    ConstantUnion()
    1.19 +    {
    1.20 +        iConst = 0;
    1.21 +        type = EbtVoid;
    1.22 +    }
    1.23 +
    1.24 +    void setIConst(int i) {iConst = i; type = EbtInt; }
    1.25 +    void setFConst(float f) {fConst = f; type = EbtFloat; }
    1.26 +    void setBConst(bool b) {bConst = b; type = EbtBool; }
    1.27 +
    1.28 +    int getIConst() { return iConst; }
    1.29 +    float getFConst() { return fConst; }
    1.30 +    bool getBConst() { return bConst; }
    1.31 +    int getIConst() const { return iConst; }
    1.32 +    float getFConst() const { return fConst; }
    1.33 +    bool getBConst() const { return bConst; }
    1.34 +
    1.35 +    bool operator==(const int i) const
    1.36 +    {
    1.37 +        return i == iConst;
    1.38 +    }
    1.39 +
    1.40 +    bool operator==(const float f) const
    1.41 +    {
    1.42 +        return f == fConst;
    1.43 +    }
    1.44 +
    1.45 +    bool operator==(const bool b) const
    1.46 +    {
    1.47 +        return b == bConst;
    1.48 +    }
    1.49 +
    1.50 +    bool operator==(const ConstantUnion& constant) const
    1.51 +    {
    1.52 +        if (constant.type != type)
    1.53 +            return false;
    1.54 +
    1.55 +        switch (type) {
    1.56 +        case EbtInt:
    1.57 +            return constant.iConst == iConst;
    1.58 +        case EbtFloat:
    1.59 +            return constant.fConst == fConst;
    1.60 +        case EbtBool:
    1.61 +            return constant.bConst == bConst;
    1.62 +        default:
    1.63 +            return false;
    1.64 +        }
    1.65 +    }
    1.66 +
    1.67 +    bool operator!=(const int i) const
    1.68 +    {
    1.69 +        return !operator==(i);
    1.70 +    }
    1.71 +
    1.72 +    bool operator!=(const float f) const
    1.73 +    {
    1.74 +        return !operator==(f);
    1.75 +    }
    1.76 +
    1.77 +    bool operator!=(const bool b) const
    1.78 +    {
    1.79 +        return !operator==(b);
    1.80 +    }
    1.81 +
    1.82 +    bool operator!=(const ConstantUnion& constant) const
    1.83 +    {
    1.84 +        return !operator==(constant);
    1.85 +    }
    1.86 +
    1.87 +    bool operator>(const ConstantUnion& constant) const
    1.88 +    { 
    1.89 +        assert(type == constant.type);
    1.90 +        switch (type) {
    1.91 +        case EbtInt:
    1.92 +            return iConst > constant.iConst;
    1.93 +        case EbtFloat:
    1.94 +            return fConst > constant.fConst;
    1.95 +        default:
    1.96 +            return false;   // Invalid operation, handled at semantic analysis
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    bool operator<(const ConstantUnion& constant) const
   1.101 +    { 
   1.102 +        assert(type == constant.type);
   1.103 +        switch (type) {
   1.104 +        case EbtInt:
   1.105 +            return iConst < constant.iConst;
   1.106 +        case EbtFloat:
   1.107 +            return fConst < constant.fConst;
   1.108 +        default:
   1.109 +            return false;   // Invalid operation, handled at semantic analysis
   1.110 +        }
   1.111 +    }
   1.112 +
   1.113 +    ConstantUnion operator+(const ConstantUnion& constant) const
   1.114 +    { 
   1.115 +        ConstantUnion returnValue;
   1.116 +        assert(type == constant.type);
   1.117 +        switch (type) {
   1.118 +        case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
   1.119 +        case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
   1.120 +        default: assert(false && "Default missing");
   1.121 +        }
   1.122 +
   1.123 +        return returnValue;
   1.124 +    }
   1.125 +
   1.126 +    ConstantUnion operator-(const ConstantUnion& constant) const
   1.127 +    { 
   1.128 +        ConstantUnion returnValue;
   1.129 +        assert(type == constant.type);
   1.130 +        switch (type) {
   1.131 +        case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
   1.132 +        case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
   1.133 +        default: assert(false && "Default missing");
   1.134 +        }
   1.135 +
   1.136 +        return returnValue;
   1.137 +    }
   1.138 +
   1.139 +    ConstantUnion operator*(const ConstantUnion& constant) const
   1.140 +    { 
   1.141 +        ConstantUnion returnValue;
   1.142 +        assert(type == constant.type);
   1.143 +        switch (type) {
   1.144 +        case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
   1.145 +        case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break; 
   1.146 +        default: assert(false && "Default missing");
   1.147 +        }
   1.148 +
   1.149 +        return returnValue;
   1.150 +    }
   1.151 +
   1.152 +    ConstantUnion operator%(const ConstantUnion& constant) const
   1.153 +    { 
   1.154 +        ConstantUnion returnValue;
   1.155 +        assert(type == constant.type);
   1.156 +        switch (type) {
   1.157 +        case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
   1.158 +        default:     assert(false && "Default missing");
   1.159 +        }
   1.160 +
   1.161 +        return returnValue;
   1.162 +    }
   1.163 +
   1.164 +    ConstantUnion operator>>(const ConstantUnion& constant) const
   1.165 +    { 
   1.166 +        ConstantUnion returnValue;
   1.167 +        assert(type == constant.type);
   1.168 +        switch (type) {
   1.169 +        case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
   1.170 +        default:     assert(false && "Default missing");
   1.171 +        }
   1.172 +
   1.173 +        return returnValue;
   1.174 +    }
   1.175 +
   1.176 +    ConstantUnion operator<<(const ConstantUnion& constant) const
   1.177 +    { 
   1.178 +        ConstantUnion returnValue;
   1.179 +        assert(type == constant.type);
   1.180 +        switch (type) {
   1.181 +        case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
   1.182 +        default:     assert(false && "Default missing");
   1.183 +        }
   1.184 +
   1.185 +        return returnValue;
   1.186 +    }
   1.187 +
   1.188 +    ConstantUnion operator&(const ConstantUnion& constant) const
   1.189 +    { 
   1.190 +        ConstantUnion returnValue;
   1.191 +        assert(type == constant.type);
   1.192 +        switch (type) {
   1.193 +        case EbtInt:  returnValue.setIConst(iConst & constant.iConst); break;
   1.194 +        default:     assert(false && "Default missing");
   1.195 +        }
   1.196 +
   1.197 +        return returnValue;
   1.198 +    }
   1.199 +
   1.200 +    ConstantUnion operator|(const ConstantUnion& constant) const
   1.201 +    { 
   1.202 +        ConstantUnion returnValue;
   1.203 +        assert(type == constant.type);
   1.204 +        switch (type) {
   1.205 +        case EbtInt:  returnValue.setIConst(iConst | constant.iConst); break;
   1.206 +        default:     assert(false && "Default missing");
   1.207 +        }
   1.208 +
   1.209 +        return returnValue;
   1.210 +    }
   1.211 +
   1.212 +    ConstantUnion operator^(const ConstantUnion& constant) const
   1.213 +    { 
   1.214 +        ConstantUnion returnValue;
   1.215 +        assert(type == constant.type);
   1.216 +        switch (type) {
   1.217 +        case EbtInt:  returnValue.setIConst(iConst ^ constant.iConst); break;
   1.218 +        default:     assert(false && "Default missing");
   1.219 +        }
   1.220 +
   1.221 +        return returnValue;
   1.222 +    }
   1.223 +
   1.224 +    ConstantUnion operator&&(const ConstantUnion& constant) const
   1.225 +    { 
   1.226 +        ConstantUnion returnValue;
   1.227 +        assert(type == constant.type);
   1.228 +        switch (type) {
   1.229 +        case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
   1.230 +        default:     assert(false && "Default missing");
   1.231 +        }
   1.232 +
   1.233 +        return returnValue;
   1.234 +    }
   1.235 +
   1.236 +    ConstantUnion operator||(const ConstantUnion& constant) const
   1.237 +    { 
   1.238 +        ConstantUnion returnValue;
   1.239 +        assert(type == constant.type);
   1.240 +        switch (type) {
   1.241 +        case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
   1.242 +        default:     assert(false && "Default missing");
   1.243 +        }
   1.244 +
   1.245 +        return returnValue;
   1.246 +    }
   1.247 +
   1.248 +    TBasicType getType() const { return type; }
   1.249 +private:
   1.250 +
   1.251 +    union  {
   1.252 +        int iConst;  // used for ivec, scalar ints
   1.253 +        bool bConst; // used for bvec, scalar bools
   1.254 +        float fConst;   // used for vec, mat, scalar floats
   1.255 +    } ;
   1.256 +
   1.257 +    TBasicType type;
   1.258 +};
   1.259 +
   1.260 +#endif // _CONSTANT_UNION_INCLUDED_

mercurial