bswap breaks when using gcc with optimization

46 Views Asked by At

I have some code that does a bswap_16() in a loop that does not work properly when optimized and I cannot for the life of me figure out why. If I create a function like the following it fixes my problem so my code is specifically tripped up by gcc's optimized bswap.

__attribute__((optimize("O0"))) static uint16_t swap16(uint16_t s) {
    return bswap_16(s);
}

Additionally if I print out the value of sum after the loop finishes then I can use O3 or get rid of the shim function without issue. sum is not an unused variable and all the code is in main(). It also happens when using htons() which I assume gets optimized in the same way.

My loop code is as follows

// uint16_t* arr; data to be swapped
uint32_t sum = 0;
for(uint16_t count = 0; count < (length / 2); ++count) {
    sum += swap16(arr[count]);
}

//printf("%x\n", sum) here allows optimization

// uint16_t* other_data;
for(uint8_t count = 0; count < 8; ++count) {
    sum += other_data[count];
}

EDIT: Decided to ask google bard this question and it suggested adding a __sync_synchronize() after the loop which does indeed fix the issue. I'm not sure if that's the best answer or not but I figured I'd add that information.

0

There are 0 best solutions below