Today I discovered that GCC does some amazing magic for optimizing switches in this code:
StairsType GetStairsType(uint8_t tileId, uint8_t dlvl){ if (dlvl == 0) return StairsType::Part; if (tileId == 48) { return dlvl >= 21 ? /* Crypt */ StairsType::Down : /* Caves */ StairsType::Part; } switch (tileId) { case 57: return StairsType::Down; // many more tile IDs go here }}
Have a look at this 🪄:
https://godbolt.org/z/snY3jv8Wz
(gcc is on the left, clang is on the right)
Somehow GCC manages to compile this to 1/4 of the code compared to Clang.
- What does this asm do, conceptually?
- How does GCC do this?