From 4af5dad424b8558b36f6aaddc376b2d48d924e88 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 11 Jan 2011 08:49:43 +0900 Subject: [PATCH] More math identities. I forgot about 0 for multiplication and division. Detects division by zero. --- tools/qfcc/source/constfold.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/qfcc/source/constfold.c b/tools/qfcc/source/constfold.c index 9f218f8ec..d110f2519 100644 --- a/tools/qfcc/source/constfold.c +++ b/tools/qfcc/source/constfold.c @@ -220,8 +220,16 @@ do_op_float (int op, expr_t *e, expr_t *e1, expr_t *e2) return dec_users (e2); if (op == '*' && is_constant (e2) && e2->e.float_val == 1) return dec_users (e1); + if (op == '*' && is_constant (e1) && e1->e.float_val == 0) + return dec_users (e1); + if (op == '*' && is_constant (e2) && e2->e.float_val == 0) + return dec_users (e2); if (op == '/' && is_constant (e2) && e2->e.float_val == 1) return dec_users (e1); + if (op == '/' && is_constant (e2) && e2->e.float_val == 0) + return error (e, "division by zero"); + if (op == '/' && is_constant (e1) && e1->e.float_val == 0) + return dec_users (e1); if (op == '+' && is_constant (e1) && e1->e.float_val == 0) return dec_users (e2); if (op == '+' && is_constant (e2) && e2->e.float_val == 0) @@ -347,9 +355,18 @@ do_op_vector (int op, expr_t *e, expr_t *e1, expr_t *e2) if (op == '*' && is_constant (e2) && e2->type == ex_float && e2->e.float_val == 1) return dec_users (e1); + if (op == '*' && is_constant (e1) && e1->type == ex_float + && e1->e.float_val == 0) + return new_vector_expr (vec3_origin); + if (op == '*' && is_constant (e2) && e2->type == ex_float + && e2->e.float_val == 0) + return new_vector_expr (vec3_origin); if (op == '/' && is_constant (e2) && e2->type == ex_float && e2->e.float_val == 1) return dec_users (e1); + if (op == '/' && is_constant (e2) && e2->type == ex_float + && e2->e.float_val == 0) + return error (e, "division by zero"); if (op == '+' && is_constant (e1) && VectorIsZero (e1->e.vector_val)) return dec_users (e2); if (op == '+' && is_constant (e2) && VectorIsZero (e2->e.vector_val)) @@ -524,9 +541,18 @@ do_op_quaternion (int op, expr_t *e, expr_t *e1, expr_t *e2) if (op == '*' && is_constant (e2) && e2->type == ex_float && e2->e.float_val == 1) return dec_users (e1); + if (op == '*' && is_constant (e1) && e1->type == ex_float + && e1->e.float_val == 0) + return new_quaternion_expr (quat_origin); + if (op == '*' && is_constant (e2) && e2->type == ex_float + && e2->e.float_val == 0) + return new_quaternion_expr (quat_origin); if (op == '/' && is_constant (e2) && e2->type == ex_float && e2->e.float_val == 1) return dec_users (e1); + if (op == '/' && is_constant (e2) && e2->type == ex_float + && e2->e.float_val == 0) + return error (e, "division by zero"); if (op == '+' && is_constant (e1) && QuatIsZero (e1->e.quaternion_val)) return dec_users (e2); if (op == '+' && is_constant (e2) && QuatIsZero (e2->e.quaternion_val)) @@ -606,8 +632,16 @@ do_op_integer (int op, expr_t *e, expr_t *e1, expr_t *e2) return dec_users (e2); if (op == '*' && is_constant (e2) && e2->e.integer_val == 1) return dec_users (e1); + if (op == '*' && is_constant (e1) && e1->e.integer_val == 0) + return dec_users (e1); + if (op == '*' && is_constant (e2) && e2->e.integer_val == 0) + return dec_users (e2); if (op == '/' && is_constant (e2) && e2->e.integer_val == 1) return dec_users (e1); + if (op == '/' && is_constant (e2) && e2->e.integer_val == 0) + return error (e, "division by zero"); + if (op == '/' && is_constant (e1) && e1->e.integer_val == 0) + return dec_users (e1); if (op == '+' && is_constant (e1) && e1->e.integer_val == 0) return dec_users (e2); if (op == '+' && is_constant (e2) && e2->e.integer_val == 0)