Home > Net >  How does GCC optimize this `switch`
How does GCC optimize this `switch`

Time:11-29

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

  • Related