user3161924
user3161924

Reputation: 2309

correct fix for warning: dereferencing type-punned pointer will break strict-aliasing rules?

Given:

inline uint16_t Hash3_x86_16(const void* key, int len, uint32_t seed)
{
  uint32_t hash32;
  Hash3_x86_32(key, len, seed, &hash32);
  return (reinterpret_cast<uint16_t*>(&hash32)[1] | reinterpret_cast<uint16_t*>(&hash32)[0]);
}

What is the correct/best fix to prevent GCC from giving the following warning?

warning: dereferencing type-punned pointer will break strict-aliasing rules

Upvotes: 2

Views: 97

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38463

Bit shifting:

return static_cast<uint16_t>(hash32 | (hash32 >> 16));

Upvotes: 4

Related Questions