Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22056

How does GCC optimize this `switch`

$
0
0

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.

  1. What does this asm do, conceptually?
  2. How does GCC do this?

Viewing all articles
Browse latest Browse all 22056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>