]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor handling of %, << and >> in ConstantFolder
authorMikko Rasa <tdb@tdb.fi>
Tue, 20 Apr 2021 15:18:25 +0000 (18:18 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 20 Apr 2021 15:18:25 +0000 (18:18 +0300)
This is in line with the other operators and makes adding unsigned
types easier.

source/glsl/optimize.cpp
source/glsl/optimize.h

index 044dd4e5a29ef1eefde6985b5c4798b93cf239fd..06ea0f49c98a03b4c8186449eaac823d922a4719 100644 (file)
@@ -592,6 +592,18 @@ T ConstantFolder::evaluate_arithmetic(char oper, T left, T right)
        }
 }
 
+template<typename T>
+T ConstantFolder::evaluate_int_special_op(char oper, T left, T right)
+{
+       switch(oper)
+       {
+       case '%': return left%right;
+       case '<': return left<<right;
+       case '>': return left>>right;
+       default: return T();
+       }
+}
+
 void ConstantFolder::set_result(const Variant &value, bool literal)
 {
        r_constant_value = value;
@@ -745,15 +757,8 @@ void ConstantFolder::visit(BinaryExpression &binary)
        }
        else if(oper=='%' || ((oper=='<' || oper=='>') && oper2==oper))
        {
-               if(!left_value.check_type<int>())
-                       return;
-
-               if(oper=='%')
-                       set_result(left_value.value<int>()%r_constant_value.value<int>());
-               else if(oper=='<')
-                       set_result(left_value.value<int>()<<r_constant_value.value<int>());
-               else if(oper=='>')
-                       set_result(left_value.value<int>()>>r_constant_value.value<int>());
+               if(left_value.check_type<int>())
+                       set_result(evaluate_int_special_op(oper, left_value.value<int>(), r_constant_value.value<int>()));
        }
 }
 
index a714a97a1b45ce49cf78a5c0fd4c69d2a4b6a661..ed9f91345f71b405209e3b6a32f7adec0d5ebeb3 100644 (file)
@@ -181,6 +181,8 @@ private:
        static bool evaluate_relation(const char *, T, T);
        template<typename T>
        static T evaluate_arithmetic(char, T, T);
+       template<typename T>
+       static T evaluate_int_special_op(char, T, T);
        void set_result(const Variant &, bool = false);
 
        virtual void visit(RefPtr<Expression> &);