| Switch Lowering in GCC(xoranth.net) | |
| 208 points by signa11 1 day ago | 47 comments | |
tl;dr: GCC lowers switch statements using three main strategies: bitset tests (packing case values into a register and using the `bt` instruction), jump tables, and binary decision trees. The compiler uses dynamic programming to find optimal groupings for bitsets and jump tables, with heuristics based on case density—jump tables require roughly one case per 8 entries when optimizing for speed (one per 2 for size), while bitsets need ≤64-value ranges and ≤3 unique destinations. Anything not profitable for these optimizations falls back to a probability-balanced binary comparison tree. | |
HN Discussion:
| |