Tokenizer:simplifyMathFunction: added simplifcation for fma[f|l]() functions.

This commit is contained in:
Martin Ettl 2013-10-13 16:28:02 +02:00
parent e18ebf313f
commit a674ed1aa9
2 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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"