From: Wolfgang Bumiller Date: Sun, 13 Jan 2013 19:53:07 +0000 (+0100) Subject: constant-fold the 1/N division from a_vector/N X-Git-Tag: before-library~266 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=ab8cc64dfd1726b060f87fc6cf2db2e902c66622;p=xonotic%2Fgmqcc.git constant-fold the 1/N division from a_vector/N --- diff --git a/parser.c b/parser.c index b8e5a48..44ab18f 100644 --- a/parser.c +++ b/parser.c @@ -937,9 +937,13 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) if (CanConstFold(exprs[0], exprs[1])) out = (ast_expression*)parser_const_vector(parser, vec3_mulvf(ConstV(0), 1.0/ConstF(1))); else { - out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, - (ast_expression*)parser_const_float_1(parser), - exprs[1]); + if (CanConstFold1(exprs[1])) { + out = (ast_expression*)parser_const_float(parser, 1.0 / ConstF(1)); + } else { + out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, + (ast_expression*)parser_const_float_1(parser), + exprs[1]); + } if (!out) { compile_error(ctx, "internal error: failed to generate division"); return false; @@ -1288,11 +1292,17 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) exprs[0], exprs[1]); } else { /* there's no DIV_VF */ - out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, - (ast_expression*)parser_const_float_1(parser), - exprs[1]); - if (!out) + if (CanConstFold1(exprs[1])) { + out = (ast_expression*)parser_const_float(parser, 1.0 / ConstF(1)); + } else { + out = (ast_expression*)ast_binary_new(ctx, INSTR_DIV_F, + (ast_expression*)parser_const_float_1(parser), + exprs[1]); + } + if (!out) { + compile_error(ctx, "internal error: failed to generate division"); return false; + } out = (ast_expression*)ast_binstore_new(ctx, assignop, INSTR_MUL_VF, exprs[0], out); }