From d7853b9963781dd0b91acd6173a35d6413deebc1 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 29 Aug 2015 20:51:02 +0200 Subject: [PATCH] Fixed simplification of fma(a, b, c) (#6958) --- lib/tokenize.cpp | 11 ++++------- test/testtokenize.cpp | 12 ++++++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 17bfa6d77..b46e7dd3e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8226,13 +8226,10 @@ void Tokenizer::simplifyMathFunctions() tok->str(strNumber); // insert result into token list simplifcationMade = true; } else if (Token::Match(tok, "fma|fmaf|fmal ( %any% , %any% , %any% )")) { - // Simplify: fma(a,b,c) == > ( a ) * ( b ) + ( c ) - // get parameters - const std::string& a(tok->strAt(2)); - const std::string& b(tok->strAt(4)); - const std::string& c(tok->strAt(6)); - tok->str("( " + a + " ) * ( " + b + " ) + ( " + c + " )"); // insert result into token list - tok->deleteNext(7); // delete fma call + // Simplify: fma(a,b,c) == > ( a * b + c ) + tok->tokAt(3)->str("*"); + tok->tokAt(5)->str("+"); + tok->deleteThis(); // delete fma call simplifcationMade = true; } else if (Token::Match(tok, "sqrt|sqrtf|sqrtl|cbrt|cbrtf|cbrtl ( %num% )")) { // Simplify: sqrt(0) = 0 and cbrt(0) == 0 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 29d04521a..9558306cf 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7231,15 +7231,15 @@ private: void simplifyMathFunctions_fma() { // verify fma(), fmal(), fmaf() - simplifcation const char code_fma[] ="int f(int a, int b, int c) { return fma(a,b,c); }"; - const char expected_fma[] = "int f ( int a , int b , int c ) { return ( a ) * ( b ) + ( c ) ; }"; + const char expected_fma[] = "int f ( int a , int b , int c ) { return ( a * b + c ) ; }"; ASSERT_EQUALS(expected_fma, tokenizeAndStringify(code_fma)); const char code_fmaf[] ="float f ( float a , float b , float c ) { return fmaf(a,b,c); }"; - const char expected_fmaf[] = "float f ( float a , float b , float c ) { return ( a ) * ( b ) + ( c ) ; }"; + const char expected_fmaf[] = "float f ( float a , float b , float c ) { return ( a * b + c ) ; }"; ASSERT_EQUALS(expected_fmaf, tokenizeAndStringify(code_fmaf)); const char code_fmal[] ="long double f ( long double a , long double b , long double c ) { return fmal(a,b,c); }"; - const char expected_fmal[] = "long double f ( long double a , long double b , long double c ) { return ( a ) * ( b ) + ( c ) ; }"; + const char expected_fmal[] = "long double f ( long double a , long double b , long double c ) { return ( a * b + c ) ; }"; ASSERT_EQUALS(expected_fmal, tokenizeAndStringify(code_fmal)); const char code_fma1[] = "void f() {\n" @@ -7255,9 +7255,9 @@ private: "} ;"; const char current_fma1[] = "void f ( ) {\n" - "std :: cout << \"fma(1,2,3): \" << ( 1 ) * ( 2 ) + ( 3 ) << std :: endl ;\n" - "std :: cout << \"fmaf(1,2,3): \" << ( 1 ) * ( 2 ) + ( 3 ) << std :: endl ;\n" - "std :: cout << \"fmal(1,2,3): \" << ( 1 ) * ( 2 ) + ( 3 ) << std :: endl ;\n" + "std :: cout << \"fma(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ;\n" + "std :: cout << \"fmaf(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ;\n" + "std :: cout << \"fmal(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ;\n" "} ;"; TODO_ASSERT_EQUALS(expected_fma1, current_fma1,tokenizeAndStringify(code_fma1)); }