]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - fold.c
fixed vector ops constant folding.
[xonotic/gmqcc.git] / fold.c
diff --git a/fold.c b/fold.c
index 5f5c12774c9a30bc68262a23dda1315e75f54f72..5352b78f531b801fd5c9f97b6853388d6067a07a 100644 (file)
--- a/fold.c
+++ b/fold.c
@@ -75,17 +75,17 @@ static GMQCC_INLINE vec3_t vec3_neg(vec3_t a) {
 
 static GMQCC_INLINE vec3_t vec3_xor(vec3_t a, vec3_t b) {
     vec3_t out;
-    out.x = (qcfloat_t)((qcint_t)a.x ^ (qcint_t)b.x);
-    out.y = (qcfloat_t)((qcint_t)a.y ^ (qcint_t)b.y);
-    out.z = (qcfloat_t)((qcint_t)a.z ^ (qcint_t)b.z);
+    out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b.x));
+    out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b.y));
+    out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b.z));
     return out;
 }
 
 static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
     vec3_t out;
-    out.x = (qcfloat_t)((qcint_t)a.x ^ (qcint_t)b);
-    out.y = (qcfloat_t)((qcint_t)a.y ^ (qcint_t)b);
-    out.z = (qcfloat_t)((qcint_t)a.z ^ (qcint_t)b);
+    out.x = (qcfloat_t)(((qcint_t)a.x) ^ ((qcint_t)b));
+    out.y = (qcfloat_t)(((qcint_t)a.y) ^ ((qcint_t)b));
+    out.z = (qcfloat_t)(((qcint_t)a.z) ^ ((qcint_t)b));
     return out;
 }
 
@@ -124,7 +124,8 @@ static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
 }
 
 static GMQCC_INLINE bool fold_can_1(const ast_value *val) {
-    return  (ast_istype((ast_expression*)val, ast_value) && val->hasvalue && val->cvq == CV_CONST && ((ast_expression*)val)->vtype != TYPE_FUNCTION);
+    return (ast_istype(((ast_expression*)(val)), ast_value) && val->hasvalue && (val->cvq == CV_CONST) &&
+              ((ast_expression*)(val))->vtype != TYPE_FUNCTION);
 }
 
 static GMQCC_INLINE bool fold_can_2(const ast_value *v1, const ast_value *v2) {
@@ -149,7 +150,7 @@ static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
         case TYPE_VECTOR: 
             if (OPTS_FLAG(CORRECT_LOGIC))
                 return vec3_pbool(v->constval.vvec);
-            return !!v->constval.vvec.x;
+            return !!(v->constval.vvec.x);
         case TYPE_STRING:
             if (!v->constval.vstring)
                 return false;
@@ -322,7 +323,7 @@ static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, as
         out                        = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
         out->node.keep             = false;
         ((ast_member*)out)->rvalue = true;
-        if (!x != -1)
+        if (x != -1)
             return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x), out);
     }
     return NULL;
@@ -415,8 +416,21 @@ static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_
     } else if (isvector(a)) {
         if (fold_can_2(a, b))
             return fold_constgen_vector(fold, vec3_mulvf(fold_immvalue_vector(a), 1.0f / fold_immvalue_float(b)));
-        else if (fold_can_1(b))
-            return fold_constgen_float (fold, 1.0f / fold_immvalue_float(b));
+        else {
+            return (ast_expression*)ast_binary_new(
+                fold_ctx(fold),
+                INSTR_MUL_VF,
+                (ast_expression*)a,
+                (fold_can_1(b))
+                    ? (ast_expression*)fold_constgen_float(fold, 1.0f / fold_immvalue_float(b))
+                    : (ast_expression*)ast_binary_new(
+                                            fold_ctx(fold),
+                                            INSTR_DIV_F,
+                                            (ast_expression*)fold->imm_float[1],
+                                            (ast_expression*)b
+                    )
+            );
+        }
     }
     return NULL;
 }
@@ -477,8 +491,8 @@ static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, as
                 fold, 
                 ((or) ? (fold_immediate_true(fold, a) || fold_immediate_true(fold, b))
                       : (fold_immediate_true(fold, a) && fold_immediate_true(fold, b)))
-                            ? 1.0f
-                            : 0.0f
+                            ? 1
+                            : 0
             );
         }
     }