102 |
struct composite_node *n = &node->composite; |
struct composite_node *n = &node->composite; |
103 |
assert (is_composite (node->type)); |
assert (is_composite (node->type)); |
104 |
|
|
105 |
|
/* If you add to these optimizations, please also add a |
106 |
|
correctness test in tests/expressions/expressions.sh. */ |
107 |
|
|
108 |
/* x+0, x-0, 0+x => x. */ |
/* x+0, x-0, 0+x => x. */ |
109 |
if ((n->type == OP_ADD || n->type == OP_SUB) && eq_double (n->args[1], 0.)) |
if ((n->type == OP_ADD || n->type == OP_SUB) && eq_double (n->args[1], 0.)) |
110 |
return n->args[0]; |
return n->args[0]; |
118 |
else if (n->type == OP_MUL && eq_double (n->args[0], 1.)) |
else if (n->type == OP_MUL && eq_double (n->args[0], 1.)) |
119 |
return n->args[1]; |
return n->args[1]; |
120 |
|
|
121 |
/* 0*x, 0/x, x*0, MOD(0,x) => x. */ |
/* 0*x, 0/x, x*0, MOD(0,x) => 0. */ |
122 |
else if (((n->type == OP_MUL || n->type == OP_DIV || n->type == OP_MOD_nn) |
else if (((n->type == OP_MUL || n->type == OP_DIV || n->type == OP_MOD_nn) |
123 |
&& eq_double (n->args[0], 0.)) |
&& eq_double (n->args[0], 0.)) |
124 |
|| (n->type == OP_MUL && eq_double (n->args[1], 0.))) |
|| (n->type == OP_MUL && eq_double (n->args[1], 0.))) |
129 |
return n->args[0]; |
return n->args[0]; |
130 |
|
|
131 |
/* x**2 => SQUARE(x). */ |
/* x**2 => SQUARE(x). */ |
132 |
else if (n->type == OP_POW && eq_double (n->args[2], 2)) |
else if (n->type == OP_POW && eq_double (n->args[1], 2)) |
133 |
return expr_allocate_unary (e,OP_SQUARE, node); |
return expr_allocate_unary (e, OP_SQUARE, n->args[0]); |
134 |
|
|
135 |
/* Otherwise, nothing to do. */ |
/* Otherwise, nothing to do. */ |
136 |
else |
else |