From ea49ede5d221267cfd494fbfa13f41b87d81c43e Mon Sep 17 00:00:00 2001 From: thorstenhater <24411438+thorstenhater@users.noreply.github.com> Date: Wed, 20 May 2020 14:31:05 +0200 Subject: [PATCH] Modcc/optimise divide by const (#1043) Add a simple re-write to eliminate division by a known constant. --- modcc/symdiff.cpp | 3 +++ test/unit-modcc/test_printers.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/modcc/symdiff.cpp b/modcc/symdiff.cpp index 27c2ff2b..03b3d4f2 100644 --- a/modcc/symdiff.cpp +++ b/modcc/symdiff.cpp @@ -430,6 +430,9 @@ public: else if (expr_value(rhs)==1) { result_ = e->lhs()->clone(); } + else if (is_number(rhs)) { + result_ = make_expression<MulBinaryExpression>(loc, std::move(lhs), make_expression<NumberExpression>(loc, 1.0/expr_value(rhs))); + } else { result_ = make_expression<DivBinaryExpression>(loc, std::move(lhs), std::move(rhs)); } diff --git a/test/unit-modcc/test_printers.cpp b/test/unit-modcc/test_printers.cpp index d99d6c8b..fa8bd3ab 100644 --- a/test/unit-modcc/test_printers.cpp +++ b/test/unit-modcc/test_printers.cpp @@ -200,7 +200,7 @@ TEST(CPrinter, proc_body_const) { TEST(CPrinter, proc_body_inlined) { const char* expected = - "r_9_=s2[i_]/3;\n" + "r_9_=s2[i_]*0.33333333333333331;\n" "r_8_=s1[i_]+2;\n" "if(s1[i_]==3){\n" " r_7_=2*r_8_;\n" -- GitLab