user2426303
user2426303

Reputation: 1

Comparison operation being completely ignored

I am trying to write a test program for a library I am making. This test program is supposed to test every possible value that goes into some functions. Here is the code:

void test_int8_smr()
{
    for (int i = INT8_MIN; i <= INT8_MAX; i++)
    {
        int8_t testval = i;
        int8_t result = from_int8_smr(to_int8_smr(testval));

        if (testval != result)
        {
            if (testval == 0x80) // This if statement gets ignored.
            {
                continue;
            }
            printf("test_int8_smr()     failed: testval = 0x%02hhX, result = 0x%02hhX\n", testval, result);
            return;
        }
    }

    printf("test_int8_smr()     succeeded for all possible values. \n");
}

Here is the output:

test_int8_smr() failed: testval = 0x80, result = 0x00

This if statement appears to be ignored:

            if (testval == 0x80)
            {
                continue;
            }

This is very baffling. Any idea why this is happening and how to fix this?

Upvotes: 0

Views: 48

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215193

testval has type int8_t so its range of possible values is -0x80 to 0x7f. It can never be equal to 0x80 and thus the equality relation is always false and subject to constant folding and dead code removal.

Upvotes: 4

Related Questions