From a674ed1aa9c3425e6990d6c77275dfb96d0fd188 Mon Sep 17 00:00:00 2001 From: Martin Ettl Date: Sun, 13 Oct 2013 16:28:02 +0200 Subject: [PATCH] Tokenizer:simplifyMathFunction: added simplifcation for fma[f|l]() functions. --- lib/tokenize.cpp | 13 ++++++++++++- test/testtokenize.cpp | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fbef04b33..4730622de 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8349,7 +8349,7 @@ bool Tokenizer::isTwoNumber(const std::string &s) // erfl(), erff(), asin(), asinf(), asinf(), asinh(), // asinhf(), asinhl(), tan(), tanf(), tanl(), tanh(), // tanhf(), tanhl(), atan(), atanf(), atanl(), atanh(), -// atanhf(), atanhl(), expm1(), expm1l(), expm1f() +// atanhf(), atanhl(), expm1(), expm1l(), expm1f(), fma() // in the tokenlist. // // Reference: @@ -8391,6 +8391,17 @@ bool Tokenizer::simplifyMathFunctions() tok->deleteNext(3); // delete e.g. abs ( 1 ) 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->tokAt(2)->str()); + const std::string b(tok->tokAt(4)->str()); + const std::string c(tok->tokAt(6)->str()); + if (!a.empty() && !b.empty() && !c.empty()) { + tok->deleteNext(7); // delete fma call + tok->str("( " + a + " ) * ( " + b + " ) + ( " + c + " )"); // insert result into token list + simplifcationMade = true; + } } else if (Token::Match(tok, "sqrt|sqrtf|sqrtl|cbrt|cbrtf|cbrtl ( %num% )")) { // Simplify: sqrt(0) = 0 and cbrt(0) == 0 // sqrt(1) = 1 and cbrt(1) == 1 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 376b5e8c1..6e1ee284c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -535,6 +535,7 @@ private: TEST_CASE(simplifyMathFunctions_atan); TEST_CASE(simplifyMathFunctions_atanh); TEST_CASE(simplifyMathFunctions_expm1); + TEST_CASE(simplifyMathFunctions_fma); TEST_CASE(simplifyMathExpressions); //ticket #1620 @@ -8826,6 +8827,21 @@ private: ASSERT_EQUALS(expected_atanl, tokenizeAndStringify(code_atanl)); } + 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 ) ; }"; + 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 ) ; }"; + 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 ) ; }"; + ASSERT_EQUALS(expected_fmal, tokenizeAndStringify(code_fmal)); + } + void simplifyMathFunctions_tanh() { // verify tanh(), tanhf(), tanhl() - simplifcation const char code_tanh[] ="void f(int x) {\n"