Why is memcmp(a, b, 4) only sometimes optimized to a uint32 comparison?

后端 未结 4 926
暗喜
暗喜 2021-02-03 16:36

Given this code:

#include 

int equal4(const char* a, const char* b)
{
    return memcmp(a, b, 4) == 0;
}

int less4(const char* a, const char* b         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 17:36

    If you generate code for a little-endian platform, optimizing four-byte memcmp for inequality to a single DWORD comparison is invalid.

    When memcmp compares individual bytes it goes from low-addressed bytes to high-addressed bytes, regardless of the platform.

    In order for memcmp to return zero all four bytes must be identical. Hence, the order of comparison does not matter. Therefore, DWORD optimization is valid, because you ignore the sign of the result.

    However, when memcmp returns a positive number, byte ordering matters. Hence, implementing the same comparison using 32-bit DWORD comparison requires a specific endianness: the platform must be big-endian, otherwise the result of comparison would be incorrect.

提交回复
热议问题