Home >Backend Development >C++ >Why Does Compiler Optimization Break This 64-bit Swap Function?

Why Does Compiler Optimization Break This 64-bit Swap Function?

DDD
DDDOriginal
2024-11-28 08:34:12529browse

Why Does Compiler Optimization Break This 64-bit Swap Function?

Why Optimization Degrades This Function

In a university lecture on programming specialties, a lecturer presented a function that baffled students:

inline u64 Swap_64(u64 x)
{
    u64 tmp;
    (*(u32*)&tmp)       = Swap_32(*(((u32*)&x)+1));
    (*(((u32*)&tmp)+1)) = Swap_32(*(u32*) &x);

    return tmp;
}

Initially, the function operated smoothly, but upon enabling high optimization levels, it became inert. The reason behind this behavior lies in the concept of strict aliasing rules.

Strict Aliasing Violation

The provided code violates strict aliasing rules, which dictate that objects should only be accessed through compatible pointer types. In this case, the u32 and u64 pointers point to potentially overlapping memory, but the compiler assumes they represent distinct objects. This assumption allows it to optimize out assignments to the temporary variable tmp, rendering the function ineffective.

Why Optimization Occurs

The compiler is allowed to optimize code based on assumptions about pointer behavior. Since u32 and u64 are different types, the compiler assumes they do not point to the same memory and that the changes made through the u32 pointers will not affect tmp's value. This optimization leads to the observed behavior.

Solutions to Preserve Function Behavior

To prevent the code from being optimized out, the pointer types should match the accessed data types. One approach is to use a union to access the bits directly:

typedef union
{
  uint32_t u32;
  uint16_t u16[2];
} U32;

uint32_t swap_words(uint32_t arg)
{
  U32 in;
  uint16_t lo;
  uint16_t hi;

  in.u32    = arg;
  hi        = in.u16[0];
  lo        = in.u16[1];
  in.u16[0] = lo;
  in.u16[1] = hi;

  return (in.u32);
}

By using a union, we ensure that the pointers and data types are compatible, preventing the compiler from optimizing out the intended changes.

The above is the detailed content of Why Does Compiler Optimization Break This 64-bit Swap Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn