3
uint32_t Seed() {
    uint64_t seed = GetSomeReasonable64BitIntegerSeed();
    return *(uint32_t*)&seed ^ *((uint32_t*)&seed + 1);
}

Above is not real code, but that's basically what the real code does. I got a warning from g++ that it violates strict aliasing, googled it, and okay I want to fix it. I found this question, but it doesn't give a clear solution other than using memcpy, or relying on undefined but practically-no-problem behaviour, that is to access an unset member of a union.

The current options I can think of are,

  1. Use memcpy.
  2. Use a union and compile this part as C, where the language standard allows type punning via unions.
Community
  • 1
  • 1
  • safe type punning can be done with [`std::bit_cast`](https://stackoverflow.com/a/64832038/995714). But this is actually an XY problem that doesn't need type punning at all – phuclv Nov 16 '20 at 16:26

1 Answers1

8

Bit-wise arithmetic is well defined and perhaps more efficient. For this example:

return seed ^ (seed >> 32);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644