fixed #4726: simplify bitwise Not (~ %num%)

This commit is contained in:
Ettl Martin 2013-04-11 09:16:40 +02:00
parent a49fc57e00
commit 33cfc1a52e
4 changed files with 18 additions and 0 deletions

View File

@ -339,6 +339,11 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco
}
}
std::string MathLib::bitwiseNot(const std::string &number)
{
return MathLib::longToString(~MathLib::toLongNumber(number));
}
std::string MathLib::sin(const std::string &tok)
{
return doubleToString(std::sin(toDoubleNumber(tok)));

View File

@ -50,6 +50,7 @@ public:
static std::string multiply(const std::string & first, const std::string & second);
static std::string divide(const std::string & first, const std::string & second);
static std::string mod(const std::string & first, const std::string & second);
static std::string bitwiseNot(const std::string &number);
static std::string calculate(const std::string & first, const std::string & second, char action);
static std::string sin(const std::string & tok);

View File

@ -992,6 +992,12 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
tok->str() == MathLib::multiply(tok->strAt(2), MathLib::divide(tok->str(), tok->strAt(2)))) {
tok->deleteNext(2);
}
// simplify bitwise not operations, e.g: ~0 --> -1
else if (Token::Match(tok, "~ %num%")) {
const std::string result = MathLib::bitwiseNot(tok->strAt(1));
tok->str(result);
tok->deleteNext();
}
else {
ret |= simplifyNumericCalculations(tok);

View File

@ -41,6 +41,7 @@ private:
TEST_CASE(isNotEqual)
TEST_CASE(isLess)
TEST_CASE(isLessEqual)
TEST_CASE(bitwiseNot)
}
void isGreater() const {
@ -343,6 +344,11 @@ private:
ASSERT_EQUALS(true , MathLib::isFloat("1.0E-1"));
ASSERT_EQUALS(true , MathLib::isFloat("-1.0E+1"));
}
void bitwiseNot() const {
ASSERT_EQUALS("-1" , MathLib::bitwiseNot("0"));
ASSERT_EQUALS("-8" , MathLib::bitwiseNot("7"));
}
};
REGISTER_TEST(TestMathLib)