TL;DR
Get the latest gadgets delivered free with Prime
- Fast, free delivery on millions of items
- Prime Video, Amazon Music and more included
- Member-only deals all year
A January 2024 technical report from Xoranth examines how GCC at -O3 compiles C switch statements into jump tables, comparison trees and mixed strategies. Its examples show that case-value density and outliers affect the generated code, while some patterns the author tested were not simplified as expected.
Xoranth published a technical report on January 14, 2024, showing how GCC turns C switch statements into different forms of machine code depending on their case values. The examples show GCC using jump tables for dense groups, comparison trees for sparser cases and separate handling for distant outliers; the findings describe tested examples, not a universal rule for every program or GCC version.
In a compact example with cases from 0 through 4, each returning a different function call, GCC at -O3 generated a bounds check followed by an indexed jump through a table of target addresses. When the same five cases ran from 97 through 101, the compiler first adjusted the input by subtracting 97, then used a similar table. The report says this shows GCC can account for a constant offset in a dense range.
The report then compares five cases spread across a larger interval. Cases at 0, 7, 14, 21 and 34 produced a jump table in the tested example, with entries for values that had no matching case leading to the default result. Increasing the gaps to 0, 8, 16, 24 and 40 instead produced a tree of comparisons and branches. From experiments, the author estimates a threshold of around eight jump-table entries per case label. That estimate should be read as an observation from the tests, not a guaranteed GCC setting or fixed compiler rule.
GCC also handled an outlier separately in a sample with cases 1 through 5 plus case 100. The generated code used a table for the dense range and a distinct comparison for 100. The author says the examined optimizations, apart from bitset lowering, also translate to Clang, but the article’s focus is GCC. The source excerpt does not provide a full account of compiler versions or testing conditions beyond the stated optimization flag and examples.
Why Case Spacing Changes Code
The examples matter because the same source-language construct can lead to different machine-code structures. A jump table can dispatch among many nearby values using an indexed lookup, while a comparison tree checks values through branches. A mixed approach can reserve a table for a dense cluster and handle distant cases separately. The report illustrates how case distribution, not just the number of labels, shapes GCC’s generated code.
That distinction is useful to developers reading compiler output or investigating performance, but the examples do not establish which form will be faster in a particular application. Actual performance can depend on the target processor, surrounding code and input patterns; the report presents assembly observations rather than benchmark results. It also cautions against assuming the compiler will recognize every mathematically possible simplification: in the tested evenly spaced cases, GCC did not replace the switch with a division-based mapping.
Products Worth Considering

Bose QuietComfort Ultra Wireless Noise-Cancelling Headphones with Spatial Audio, Black

DJI Osmo Mobile 8 Standard Combo Gimbal Stabilizer for Phone

Apple AirPods Pro 3 Wireless Earbuds with Active Noise Cancellation
As an affiliate, we earn on qualifying purchases.
The Report’s GCC Test Cases
The report begins with a simple switch whose five consecutive values each call a different function. It uses that case to introduce a jump table and then shifts the range to show that an offset does not prevent table generation. The next examples increase the spacing between labels to explore where GCC instead builds conditional branches.
The author says the examples were compiled with -O3, except where noted, and that some GCC code and assertions were edited or removed to make the explanation shorter. The article also tests cases with repeated outcomes and outliers. The supplied report excerpt includes the start of a repeated-outcome example, but not its complete assembly or conclusions, so no broader result about that case should be inferred from the excerpt alone.
““Through experimentation, we find that gcc considers the threshold to be around eight jump table entries per case label.””
— Xoranth report, published January 14, 2024
Limits of the Reported Results
The report’s threshold is based on particular experiments and should not be treated as a stable rule across all GCC releases, target architectures or program shapes. The supplied material does not identify the exact GCC version or target used for the assembly examples, and it provides no performance benchmarks comparing the generated forms.
The source excerpt ends during its discussion of multiple labels sharing a result. It therefore does not establish the full outcome of that example or the report’s findings for every lowering strategy it introduces. The report also says bitset lowering is an exception to its statement that the described optimizations translate to Clang; it does not provide enough detail here to assess that exception.
Further Cases and Compiler Testing
The report proceeds from dense and sparse cases to outliers and groups of labels that share an outcome. Those cases can help explain how GCC chooses among code-generation strategies, but readers seeking to apply the observations to a project would need to inspect output from the specific compiler version and target they use. Comparing assembly and measuring real workloads would be needed to determine whether a particular lowering choice affects performance.
No follow-up release, benchmark study or compiler change is announced in the supplied material. The next concrete step for readers is to treat the reported threshold as a prompt for testing, rather than as a setting or promise, and to consult the full report for examples beyond the excerpt provided.
Key Questions
What does switch lowering mean?
Switch lowering is the process of turning a source-code switch statement into lower-level instructions, such as a jump table or a sequence of comparisons and branches.
When did the Xoranth report appear?
The report, titled “Switch Lowering in GCC,” is dated January 14, 2024.
Does GCC always use a jump table for consecutive cases?
No universal rule is established by the report. Its examples show a jump table for several consecutive cases, but generated code can depend on the compiler, target and surrounding program.
Is the reported threshold an official GCC rule?
No. The author estimates a threshold of about eight jump-table entries per case label from experiments. The report does not present it as an official, fixed rule.
Does the report show that jump tables are faster?
No. It examines generated assembly and lowering choices, but the supplied material does not include benchmarks showing that one form performs better in a given application.
Source: xoranth.net
