[qfcc] Update vector constant folding checks

Internally, * is not really a valid operator for vectors since it can
have many meanings. This didn't cause trouble until trying to build
everything in game-source (since there's still a lot of legacy code in
there).
This commit is contained in:
Bill Currie 2023-02-11 20:13:44 +09:00
parent b7ad02c71d
commit fa97074aff

View file

@ -394,13 +394,15 @@ do_op_vector (int op, expr_t *e, expr_t *e1, expr_t *e2)
{ {
const float *v1, *v2; const float *v1, *v2;
vec3_t v, float_vec; vec3_t v, float_vec;
static int valid[] = {'+', '-', '*', HADAMARD, SCALE, EQ, NE, 0}; static int vv_valid[] = {'+', '-', DOT, HADAMARD, EQ, NE, 0};
static int vs_valid[] = {SCALE, 0};
static int sv_valid[] = {SCALE, '/', 0};
expr_t *t; expr_t *t;
if (!is_vector(get_type (e1))) { if (!is_vector(get_type (e1))) {
if (op != '*') if (!valid_op (op, sv_valid))
return error (e1, "invalid operator for vector"); return error (e1, "invalid operator for scalar-vector");
t = e1; t = e1;
e->e.expr.e1 = e1 = e2; e->e.expr.e1 = e1 = e2;
@ -408,10 +410,10 @@ do_op_vector (int op, expr_t *e, expr_t *e1, expr_t *e2)
} }
if (!is_vector(get_type (e2))) { if (!is_vector(get_type (e2))) {
e->e.expr.e2 = e2 = convert_to_float (e2); e->e.expr.e2 = e2 = convert_to_float (e2);
if (op != SCALE && op != '/') if (!valid_op (op, vs_valid))
return error (e1, "invalid operator for vector"); return error (e1, "invalid operator for vector");
} else { } else {
if (!valid_op (op, valid)) if (!valid_op (op, vv_valid))
return error (e1, "invalid operator for vector"); return error (e1, "invalid operator for vector");
} }
if (is_compare (op) || is_logic (op)) { if (is_compare (op) || is_logic (op)) {
@ -419,7 +421,7 @@ do_op_vector (int op, expr_t *e, expr_t *e1, expr_t *e2)
e->e.expr.type = &type_int; e->e.expr.type = &type_int;
else else
e->e.expr.type = &type_float; e->e.expr.type = &type_float;
} else if (op == '*' && is_vector(get_type (e2))) { } else if (op == DOT && is_vector(get_type (e2))) {
e->e.expr.type = &type_float; e->e.expr.type = &type_float;
} else if (op == '/' && !is_constant (e1)) { } else if (op == '/' && !is_constant (e1)) {
e2 = fold_constants (binary_expr ('/', new_float_expr (1), e2)); e2 = fold_constants (binary_expr ('/', new_float_expr (1), e2));
@ -428,9 +430,9 @@ do_op_vector (int op, expr_t *e, expr_t *e1, expr_t *e2)
e->e.expr.type = &type_vector; e->e.expr.type = &type_vector;
} }
if (op == '*' && is_float_val (e2) && expr_float (e2) == 1) if (op == SCALE && is_float_val (e2) && expr_float (e2) == 1)
return e1; return e1;
if (op == '*' && is_float_val (e2) && expr_float (e2) == 0) if (op == SCALE && is_float_val (e2) && expr_float (e2) == 0)
return new_vector_expr (vec3_origin); return new_vector_expr (vec3_origin);
if (op == '/' && is_float_val (e2) && expr_float (e2) == 1) if (op == '/' && is_float_val (e2) && expr_float (e2) == 1)
return e1; return e1;
@ -483,13 +485,12 @@ do_op_vector (int op, expr_t *e, expr_t *e1, expr_t *e2)
VectorScale (v1, 1 / v2[0], v); VectorScale (v1, 1 / v2[0], v);
e = new_vector_expr (v); e = new_vector_expr (v);
break; break;
case '*': case DOT:
if (is_vector(get_type (e2))) { e = new_float_expr (DotProduct (v1, v2));
e = new_float_expr (DotProduct (v1, v2)); break;
} else { case SCALE:
VectorScale (v1, v2[0], v); VectorScale (v1, v2[0], v);
e = new_vector_expr (v); e = new_vector_expr (v);
}
break; break;
case EQ: case EQ:
e = cmp_result_expr (VectorCompare (v1, v2)); e = cmp_result_expr (VectorCompare (v1, v2));