]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - fold.c
folder: change ~ for floats too
[xonotic/gmqcc.git] / fold.c
diff --git a/fold.c b/fold.c
index c9f1a0695c07f8fe1b9c35b62f7bbfab4eaaa360..c75106ad74c472b8265e4f532a9e48257566e28b 100644 (file)
--- a/fold.c
+++ b/fold.c
@@ -59,9 +59,9 @@ static GMQCC_INLINE vec3_t vec3_add(vec3_t a, vec3_t b) {
 
 static GMQCC_INLINE vec3_t vec3_sub(vec3_t a, vec3_t b) {
     vec3_t out;
-    out.x = a.x + b.x;
-    out.y = a.y + b.y;
-    out.z = a.z + b.z;
+    out.x = a.x - b.x;
+    out.y = a.y - b.y;
+    out.z = a.z - b.z;
     return out;
 }
 
@@ -123,9 +123,9 @@ static GMQCC_INLINE vec3_t vec3_xorvf(vec3_t a, qcfloat_t b) {
 
 static GMQCC_INLINE vec3_t vec3_not(vec3_t a) {
     vec3_t out;
-    out.x = (qcfloat_t)(~((qcint_t)a.x));
-    out.y = (qcfloat_t)(~((qcint_t)a.y));
-    out.z = (qcfloat_t)(~((qcint_t)a.z));
+    out.x = -1-a.x;
+    out.y = -1-a.y;
+    out.z = -1-a.z;
     return out;
 }
 
@@ -160,7 +160,7 @@ static GMQCC_INLINE qcfloat_t vec3_notf(vec3_t a) {
 }
 
 static GMQCC_INLINE bool vec3_pbool(vec3_t a) {
-    return (a.x && a.y && a.z);
+    return (a.x || a.y || a.z);
 }
 
 static GMQCC_INLINE vec3_t vec3_cross(vec3_t a, vec3_t b) {
@@ -537,11 +537,10 @@ static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_
         if (fold_can_2(a, b))
             return fold_constgen_float(fold, (qcfloat_t)(((qcint_t)fold_immvalue_float(a)) ^ ((qcint_t)fold_immvalue_float(b))));
     } else {
-        if (isvector(b)) {
-            if (fold_can_2(a, b))
+        if (fold_can_2(a, b)) {
+            if (isvector(b))
                 return fold_constgen_vector(fold, vec3_xor(fold_immvalue_vector(a), fold_immvalue_vector(b)));
-        } else {
-            if (fold_can_2(a, b))
+            else
                 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
         }
     }
@@ -563,8 +562,10 @@ static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, a
 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
     if (fold_can_2(a, b)) {
         if (OPTS_FLAG(PERL_LOGIC)) {
-            if (fold_immediate_true(fold, a))
-                return (ast_expression*)b;
+            if (expr)
+                return (fold_immediate_true(fold, a)) ? (ast_expression*)a : (ast_expression*)b;
+            else
+                return (fold_immediate_true(fold, a)) ? (ast_expression*)b : (ast_expression*)a;
         } else {
             return fold_constgen_float (
                 fold,
@@ -604,11 +605,10 @@ static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, a
 
 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
     if (fold_can_2(a, b)) {
-        return fold_constgen_float(
-                    fold,
-                    (ne) ? (fold_immvalue_float(a) != fold_immvalue_float(b))
-                         : (fold_immvalue_float(a) == fold_immvalue_float(b))
-                );
+        if (isfloat(a) && isfloat(b))
+            return fold_constgen_float(fold, ne != (fold_immvalue_float(a) == fold_immvalue_float(b)));
+        if (isvector(a) && isvector(b))
+            return fold_constgen_float(fold, ne != vec3_cmp(fold_immvalue_vector(a), fold_immvalue_vector(b)));
     }
     return NULL;
 }
@@ -616,7 +616,7 @@ static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_
 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
     if (isfloat(a)) {
         if (fold_can_1(a))
-            return fold_constgen_float(fold, ~((qcint_t)fold_immvalue_float(a)));
+            return fold_constgen_float(fold, -1-fold_immvalue_float(a));
     } else {
         if (isvector(a)) {
             if (fold_can_1(a))
@@ -799,8 +799,10 @@ static ast_expression *fold_superfluous(ast_expression *left, ast_expression *ri
             break;
 
 
-        case INSTR_ADD_F:
         case INSTR_SUB_F:
+            if (swapped)
+                return NULL;
+        case INSTR_ADD_F:
             if (fold_immvalue_float(load) == 0.0f) {
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
                 ast_unref(right);
@@ -816,8 +818,10 @@ static ast_expression *fold_superfluous(ast_expression *left, ast_expression *ri
             }
             break;
 
-        case INSTR_ADD_V:
         case INSTR_SUB_V:
+            if (swapped)
+                return NULL;
+        case INSTR_ADD_V:
             if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
                 ast_unref(right);