[qfcc] Optimize negating anti-commutative operations

Both for geometric algebra and in general (since it's just in
unary_expr). However, it doesn't quite work for float stuff (eg,
`vector × vector`, but that's only because the anti-commutative flag
isn't set.
This commit is contained in:
Bill Currie 2024-09-30 19:15:44 +09:00
parent 9797dcb5de
commit 275201afdf
2 changed files with 12 additions and 0 deletions

View file

@ -121,6 +121,11 @@ neg_expr (const expr_t *e)
return e->expr.e1;
}
auto type = get_type (e);
if (e->type == ex_alias && !e->alias.offset && anti_com (e->alias.expr)) {
auto n = neg_expr (e->alias.expr);
n = algebra_cast_expr (type, n);
return n;
}
expr_t *neg;
if (anti_com (e)) {
neg = new_binary_expr (e->expr.op, e->expr.e2, e->expr.e1);

View file

@ -469,6 +469,13 @@ unary_expr (int op, const expr_t *e)
return error (e, "invalid type for unary %s", get_op_string (op));
}
if (op == '-' && e->type == ex_expr && e->expr.anticommute) {
auto neg = new_expr ();
*neg = *e;
neg->expr.e1 = e->expr.e2;
neg->expr.e2 = e->expr.e1;
return neg;
}
if (unary_type->process) {
return unary_type->process (e);
}