<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
  <title>xoranth.net</title>
  <link>https://aggr.aymericbeaumet.com/sources/xoranth.net/</link>
  <description>Browse aymeric&#39;s, an independent, searchable archive of readable snapshots from followed feeds, preserved in Git with aggr.</description>
  <language>en</language>
  <atom:link href="https://aggr.aymericbeaumet.com/sources/xoranth.net/rss.xml" rel="self" type="application/rss+xml"/>
  <lastBuildDate>Sat, 31 Oct 2026 08:02:26 +0000</lastBuildDate>
  <generator>aggr</generator>
  <item>
    <title>Switch Lowering in GCC</title>
    <link>https://aggr.aymericbeaumet.com/items/hnrss-org/2026-10-31-switch-lowering-in-gcc/</link>
    <guid isPermaLink="false">urn:aggr:item:06002da099109f158de3095beaa7212306c1877d</guid>
    <atom:link href="https://xoranth.net/gcc-switch/" rel="via" type="text/html"/>
    <pubDate>Sat, 31 Oct 2026 08:02:26 +0000</pubDate>
    <dc:creator>signa11</dc:creator>
    <description>2024-01-14 Introduction We have seen in a previous post that switch statements are more complex than they seem. The compiler is able to implement them as a decision tree (even though maybe it shouldn&#39;t). Is that everything it can do? In…</description>
    <content:encoded>&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;2024-01-14&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;We have seen in a &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://xoranth.net/verb-parse/&quot;&gt;previous post&lt;/a&gt; that switch statements are more complex than they seem. The compiler is able to implement them as a decision tree (even though maybe it shouldn&#39;t). Is that everything it can do?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;In this post, I&#39;ll focus on &lt;code&gt;gcc&lt;/code&gt;. All optimizations, except the bitset lowering, translate to clang. I&#39;ll be using the &lt;code&gt;-O3&lt;/code&gt; optimization flag unless indicated otherwise.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I&#39;ve also slightly edited &lt;code&gt;gcc&lt;/code&gt; code and removed certain assertions for brevity.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Examples&lt;/h3&gt;
&lt;p&gt;Let us start with a simple example. A function that takes two integers &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;val&lt;/code&gt;. Depending on the value of &lt;code&gt;x&lt;/code&gt;, it calls a corresponding function (&lt;code&gt;f0&lt;/code&gt;, &lt;code&gt;f1&lt;/code&gt;, and so forth) with &lt;code&gt;val&lt;/code&gt; as an argument and returns the result. Were we to implement this manually, we&#39;d would use an array of function pointers, where the input serves as the index into the array.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;compact&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;compact:
	mov	eax, edi
	mov	edi, esi
	cmp	eax, 4
	ja	.L2
	jmp	[QWORD PTR .L4[0+rax*8]]
.L4:
	.quad	.L8
	.quad	.L7
	.quad	.L6
	.quad	.L5
	.quad	.L3
.L5:
	jmp	f3
.L3:
	jmp	f4
.L8:
	jmp	f0
.L7:
	jmp	f1
.L6:
	jmp	f2
.L2:
	xor	eax, eax
	ret
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compiler agrees with this approach, and transforms the switch statement into a jump table.&lt;/p&gt;
&lt;p&gt;Moreover, it is able to identify when the values are offset by a constant:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;compact&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;97&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;98&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;101&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;compact:
	lea	eax, [rdi-97]
	cmp	eax, 4
	ja	.L2
	mov	edi, esi
	jmp	[QWORD PTR .L4[0+rax*8]]
.L4:
	.quad	.L8
	.quad	.L7
	.quad	.L6
	.quad	.L5
	.quad	.L3
.L5:
	jmp	f3
.L3:
	jmp	f4
.L8:
	jmp	f0
.L7:
	jmp	f1
.L6:
	jmp	f2
.L2:
	xor	eax, eax
	ret
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our intuition suggests that the more sparse the values, the less advantageous the jump table conversion is.&lt;/p&gt;
&lt;p&gt;Through experimentation, we find that &lt;code&gt;gcc&lt;/code&gt; considers the threshold to be around eight jump table entries per &lt;code&gt;case&lt;/code&gt; label.&lt;/p&gt;
&lt;p&gt;In other words, the following function compiles to a jump table:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;compact_7&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;compact_7:
	mov	eax, edi
	mov	edi, esi
	cmp	eax, 34
	ja	.L2
	jmp	[QWORD PTR .L4[0+rax*8]]
.L4:
	.quad	.L8
	.quad	.L2
	; ...
	.quad	.L2
	.quad	.L7
	.quad	.L2
	; ...
	.quad	.L2
	.quad	.L6
	.quad	.L2
	; ...
	.quad	.L2
	.quad	.L5
	.quad	.L2
	; ...
	.quad	.L2
	.quad	.L3
.L2:
	xor	eax, eax
	ret
.L8:
	jmp	f0
.L7:
	jmp	f1
.L6:
	jmp	f2
.L5:
	jmp	f3
.L3:
	jmp	f4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But this other function is expanded as a tree of comparisons:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;sparse_8&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;sparse_8:
	mov	eax, edi
	mov	edi, esi
	cmp	eax, 16
	je	.L2
	jle	.L10
	cmp	eax, 24
	je	.L7
	cmp	eax, 40
	jne	.L1
	jmp	f4
.L10:
	test	eax, eax
	je	.L4
	cmp	eax, 8
	jne	.L1
	jmp	f1
.L1:
	xor	eax, eax
	ret
.L4:
	jmp	f0
.L7:
	jmp	f3
.L2:
	jmp	f2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s the comparison tree visualized for clarity:&lt;/p&gt;
&lt;p&gt;A couple of observations:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We&#39;ve only tried equally spaced values so far. Can the compiler identify transformations that are profitable on a subset of all cases?&lt;/li&gt;
&lt;li&gt;So far each &lt;code&gt;case&lt;/code&gt; led to a different result. What if we had different cases with the same result?&lt;/li&gt;
&lt;li&gt;The switch in &lt;code&gt;sparse_8&lt;/code&gt; could be simplified by dividing &lt;code&gt;x&lt;/code&gt; by eight before the jump. Interestingly, &lt;code&gt;gcc&lt;/code&gt; &lt;em&gt;doesn&#39;t&lt;/em&gt; find this simplification.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&#39;s dig into 1. and 2.&lt;/p&gt;
&lt;h4&gt;Outliers&lt;/h4&gt;
&lt;p&gt;Altering our first example by including an outlier value shows that the compiler is able to recognize dense subsets of cases.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f5&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;outlier&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f0&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f5&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;outlier:
	mov	eax, edi
	mov	edi, esi
	cmp	eax, 5
	jg	.L2
	test	eax, eax
	jle	.L1
	cmp	eax, 5
	ja	.L4
	jmp	[QWORD PTR .L6[0+rax*8]]
.L6:
	.quad	.L4
	.quad	.L4
	.quad	.L9
	.quad	.L8
	.quad	.L7
	.quad	.L5
.L2:
	cmp	eax, 100
	jne	.L1
	jmp	f5
.L7:
	jmp	f3
.L5:
	jmp	f4
.L9:
	jmp	f1
.L8:
	jmp	f2
.L4:
	jmp	f0
.L1:
	xor	eax, eax
	ret
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compiler emits a branch for case 100, but emits a jump table for case 1 through 5.&lt;/p&gt;
&lt;h4&gt;Contiguous&lt;/h4&gt;
&lt;p&gt;In our examples so far, each case statement lead to a different code block. Let&#39;s test what happens when multiple &lt;code&gt;case&lt;/code&gt; labels invoke the same function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f5&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;blocks_2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;63&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f4&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;84&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-operator syntax-variadic syntax-c&quot;&gt;...&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;87&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f5&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;blocks_2:
	mov	eax, edi
	mov	edi, esi
	cmp	eax, 45
	jg	.L2
	cmp	eax, 41
	jg	.L3
	cmp	eax, 3
	jg	.L4
	test	eax, eax
	js	.L1
	jmp	f1
.L2:
	cmp	eax, 66
	jg	.L8
	cmp	eax, 62
	jle	.L1
	jmp	f4
.L4:
	sub	eax, 21
	cmp	eax, 3
	ja	.L1
	jmp	f2
.L8:
	sub	eax, 84
	cmp	eax, 3
	ja	.L1
	jmp	f5
.L3:
	jmp	f3
.L1:
	xor	eax, eax
	ret
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have 20 case labels, with values between 0 and 87. The compiler could lower this &lt;code&gt;switch&lt;/code&gt; to a jump table with 88 entries. Our hypothesis for the heuristic is that a jump table conversion is profitable when its size is smaller than &lt;code&gt;8 * num_case_labels&lt;/code&gt;. Since &lt;code&gt;88 &amp;lt; 20 * 8&lt;/code&gt;, the conversion should be profitable in this case, however &lt;code&gt;gcc&lt;/code&gt; opted to lower the switch to a sequence of comparisons.&lt;/p&gt;
&lt;p&gt;To better understand why, let&#39;s examine a specific block of &lt;code&gt;case&lt;/code&gt; labels.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;blocks_2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;43&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;44&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;val&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;blocks_2:
	mov	eax, edi ; Move `val` to $eax
	mov	edi, esi
	cmp	eax, 45
	; If val &amp;lt;= 45, do _not_ jump
	jg	.L2
	cmp	eax, 41
	; If val &amp;gt;= 42, jump to L3
	jg	.L3
	; ...
.L2:
; ...
.L3:
        ; We reach L3 only if 42 &amp;lt;= val &amp;lt;= 45
        jmp	f3        ; Call f3(val)
; ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compiler realizes that the cases are contiguous and can be implemented with two comparisons instead of four. If the input of the jump table heuristic is the number of &lt;em&gt;comparisons&lt;/em&gt; instead of &lt;em&gt;case labels&lt;/em&gt;, things work out:&lt;/p&gt;
&lt;p&gt;We have a total of 10 comparisons. Since &lt;code&gt;10 * 8 &amp;lt; 88&lt;/code&gt;, our revised heuristic suggests that a jump table transformation is unprofitable.&lt;/p&gt;
&lt;h4&gt;Non-contiguous&lt;/h4&gt;
&lt;p&gt;Let us try a non-contiguous case.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C&quot;&gt;&lt;span class=&quot;syntax-source syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c&quot;&gt;bittest&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;0&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;2&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;4&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;5&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;7&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;9&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;A&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;C&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;syntax-string syntax-quoted syntax-single syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-begin syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;E&lt;span class=&quot;syntax-punctuation syntax-definition syntax-string syntax-end syntax-c&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;x&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Assembly&quot;&gt;bittest:
	lea	eax, [rdi-48]
	cmp	eax, 21
	ja	.L1
	mov	edx, 2753205  ; ???
	bt	rdx, rax
	jc	.L7
.L1:
	xor	eax, eax
	ret
.L7:
	jmp	f1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compiler emits non-intuitive machine code. Why is there no &lt;code&gt;cmp&lt;/code&gt; nor jump tables? Where does the constant &lt;code&gt;2753205&lt;/code&gt; come from?&lt;/p&gt;
&lt;p&gt;An hint comes from the instruction &lt;code&gt;bt&lt;/code&gt;. According to the &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://www.felixcloutier.com/x86/bt&quot;&gt;Intel docs&lt;/a&gt;, &lt;code&gt;bt&lt;/code&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Selects the bit in a bit string (specified with the first operand, called the bit base) at the bit-position designated by the bit offset (specified by the second operand) and stores the value of the bit in the CF flag.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If we examine the binary representation of &lt;code&gt;2753205&lt;/code&gt;, we find that it is in fact a bitset. The bits that are set to one correspond to the ASCII values of the case labels, each value being reduced by 48.&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;We&#39;ve encountered several distinct lowering strategies employed by gcc. For a more in-depth understanding, we can delve into the compiler&#39;s source code.&lt;/p&gt;
&lt;p&gt;The compiler pass that responsible for lowering &lt;code&gt;switch&lt;/code&gt; statements is the (appropriately named) &lt;code&gt;pass_lower_switch&lt;/code&gt;, which in turn calls &lt;code&gt;analyze_switch_statement&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since &lt;code&gt;gcc&lt;/code&gt; source can be daunting to follow, I&#39;ll provide a step-by-step commented version below. Following this, I will break down each section to explain its functionality.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;switch_decision_tree&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;analyze_switch_statement &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; l &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;gimple_switch_num_labels&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_switch&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    auto_vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; clusters&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;compute_cases_per_edge&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Some preamble code. &lt;code&gt;m_switch&lt;/code&gt; points to a gimple object. Gimple is the internal representation used by &lt;code&gt;gcc&lt;/code&gt;. Through this, we extract the overall count of cases and the individual counts of cases for each outgoing edge.&lt;/p&gt;
&lt;p&gt;We also allocate a vector that will record the lowering strategy chosen for each case label.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; l&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        tree elt &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;gimple_switch_label&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_switch&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; i&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;        tree low &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;CASE_LOW&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;elt&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        tree high &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;CASE_HIGH&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;elt&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        profile_probability p
            &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; case_edge&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;probability&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;apply_scale&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-support syntax-type syntax-stdint syntax-c&quot;&gt;intptr_t&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;case_edge&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;aux&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        clusters&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;quick_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;simple_cluster&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;low&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; high&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; elt&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; case_edge&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; p&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More setup code. We notice that gimple has a concept of high and low value for each label. That is, contiguous &lt;code&gt;case&lt;/code&gt; labels have already joined together at this stage.&lt;/p&gt;
&lt;p&gt;For each case label, we allocate a &lt;code&gt;simple_cluster&lt;/code&gt;. This cluster serves as a record, capturing the range of values associated with the label, the relevant output edge, and the likelihood of this cluster being chosen. This probability comes from profile-guided optimization. By default it is uniform among case labels.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;    vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; output &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;bit_test_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;find_bit_tests&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We transform the vector of clusters we have created. The name &lt;code&gt;bit_test_cluster&lt;/code&gt; hints to the bitset lowering strategy we saw before.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Find jump table clusters.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
    vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; output2&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    auto_vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; tmp&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;    
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;c &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; output&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;get_type&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;!=&lt;/span&gt; SIMPLE_CASE&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;tmp&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;is_empty&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ... See below for what&amp;#39;s here
&lt;/span&gt;                tmp&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;truncate&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
            output2&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;c&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; 
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt;
            tmp&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;c&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This section allocates a vector for the transformed cases and a scratch vector. Any case that wasn&#39;t altered during the bitset transformation process is gathered in this scratch vector. These cases are then processed and cleared whenever we come across an optimized cluster.&lt;/p&gt;
&lt;p&gt;Essentially, in this section we are looking for sequences of case labels that couldn&#39;t be converted into bitset clusters.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;                vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; n &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;jump_table_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;find_jump_tables&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;tmp&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
                output2&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_splice&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
                n&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;release&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We attempt to convert these consecutive labels into a jump table and add the outcome to the output.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; We still can have a temporary vector to test.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;tmp&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;is_empty&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; n &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;jump_table_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;find_jump_tables&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;tmp&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        output2&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_splice&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;n&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        n&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;release&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a coda for the loop above.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; expanded &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;try_switch_expansion&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;output2&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;release_clusters&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;output2&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; expanded&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-c++&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Whatever is left goes through another pass, which turns the switch statement into a decision tree. The bitsets and jump table are leaves of the decision tree.&lt;/p&gt;
&lt;h4&gt;Bitsets&lt;/h4&gt;
&lt;p&gt;Let&#39;s dig further into &lt;code&gt;bit_test_cluster::find_bit_tests&lt;/code&gt;. The high level structure of the code is as follow.&lt;/p&gt;
&lt;p&gt;Initially, we aim to find the optimal method to bundle all clusters into bitsets. Optimality, in this context, refers to minimizing the number of bitsets used. We do so using dynamic programming.&lt;/p&gt;
&lt;p&gt;Subsequently, we iterate over the bitsets and apply a profitability heuristic. If the bitsets meet this heuristic, we emit a &lt;code&gt;bit_test_cluster&lt;/code&gt;; otherwise, we keep the original &lt;code&gt;simple_cluster&lt;/code&gt;s.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;bit_test_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;find_bit_tests &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;clusters&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;
    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; l &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; clusters&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    auto_vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;min_cluster_item&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; min&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;
    min&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;quick_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; l&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Set minimal # of clusters with i-th item to infinite.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
        min&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;quick_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;INT_MAX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; i&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; j&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt;
                    &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;can_be_handled&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;gcc_checking_assert&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;!=&lt;/span&gt; INT_MAX&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the dynamic programming, we&#39;ll use a vector of &lt;code&gt;min_cluster_item&lt;/code&gt; structures. Each &lt;code&gt;min_cluster_item&lt;/code&gt; has three components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The first records how many bitsets do we need to represent the first &lt;em&gt;i&lt;/em&gt; case labels, where &lt;code&gt;i&lt;/code&gt; is the index in the vector.&lt;/li&gt;
&lt;li&gt;The second records the &lt;strong&gt;start&lt;/strong&gt; of the last bitset.&lt;/li&gt;
&lt;li&gt;The third is unused.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The dynamic programming is as follows. We want to find the minimum number of bitsets to represent the first &lt;em&gt;i&lt;/em&gt; case labels.&lt;/p&gt;
&lt;p&gt;The base case is straightforward, a single case label can be represented by a single bitset.&lt;/p&gt;
&lt;p&gt;Suppose we have already have found the optimal number of bitsets for the first &lt;em&gt;j&lt;/em&gt; case label, for all . We define to be the optimal number of splits for the first &lt;code&gt;i&lt;/code&gt; elements. This means we already computed , and are now seeking .&lt;/p&gt;
&lt;p&gt;If the range of case labels can be represented as a bitset cluster, it must be true that&lt;/p&gt;
&lt;p&gt;Conversely, assume we have the optimal assignment for the first &lt;em&gt;i&lt;/em&gt; labels. If the last bitset starts at label , this assignment is also optimal when restricted to . Were this not the case, we could improve the assignment on as well.&lt;/p&gt;
&lt;p&gt;Therefore, it must be that:&lt;/p&gt;
&lt;p&gt;These conditions are exactly what the &lt;code&gt;if&lt;/code&gt; statement in the code above checks for.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;can_be_handled&lt;/code&gt; function controls whether a range of &lt;code&gt;case&lt;/code&gt; labels can be turned into a bitset test. It tests two criteria:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;combined&lt;/em&gt; range of all the case labels must be smaller than the width of a register. On contemporary systems, the combined range should be less than 64.&lt;/li&gt;
&lt;li&gt;The case labels must have at most 3 &lt;em&gt;unique&lt;/em&gt; outgoing edges.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If there is more than one edge, we emit a cascade of bit tests.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;    
    &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Find and build the clusters.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; l&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; start &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;end&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_start&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;is_beneficial&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; entire &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; start &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; clusters&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;bit_test_cluster&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; entire&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        end &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;start &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;reverse&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-c++&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we convert only bitsets that satisfy the &lt;code&gt;is_beneficial&lt;/code&gt; test. In the code &lt;code&gt;count&lt;/code&gt; represents the number of case labels handled by the cluster, while &lt;code&gt;uniq&lt;/code&gt; denotes the number of distinct outgoing edges.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;bit_test_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;is_beneficial &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;uniq&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;uniq &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; count &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
              &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;uniq &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; count &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
              &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;uniq &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; count &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Jump tables&lt;/h4&gt;
&lt;p&gt;The logic for jump table conversion mirrors the one used for bit test conversion. It leverages dynamic programming to identify &amp;quot;clusters&amp;quot; that can be profitably converted to a jump table.&lt;/p&gt;
&lt;p&gt;Let&#39;s delve into the code.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;jump_table_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;find_jump_tables &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;clusters&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;is_enabled&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; clusters&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;copy&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; l &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; clusters&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    auto_vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;min_cluster_item&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; min&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    min&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;reserve&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;l &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We start by preallocating a vector of &lt;code&gt;min_cluster_item&lt;/code&gt;. We&#39;ll use this vector to keep track of the best jump table assignments for consecutive &lt;code&gt;case&lt;/code&gt; clusters.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    min&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;quick_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We also push the base case for dynamic programming. This corresponds to using a single jump table for the whole &lt;code&gt;switch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s focus now on the main loop.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; HOST_WIDE_INT max_ratio &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Used in the main heuristic. See next snippet. &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
                
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; l&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): Base case
&lt;/span&gt;        min&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;quick_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;INT_MAX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        
        &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Pre-calculate number of comparisons for the clusters.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
        HOST_WIDE_INT comparison_count &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; k &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; k &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; k&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            simple_cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;sc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-cast syntax-c++&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;simple_cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;clusters&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;k&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): get_comparison_count is 2 for contiguous case 
&lt;/span&gt;            &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; labels, and 1 otherwise
&lt;/span&gt;            comparison_count &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; sc&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;get_comparison_count&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; i&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; j&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; HOST_WIDE_INT s &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_non_jt_cases&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;case_values_threshold&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                s &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; j&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            
            &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Prefer clusters with smaller number of numbers covered.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Check if new split is better. See next snippet &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; 
            &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): Update best score.
&lt;/span&gt;                min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
					   s &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; `s` is the number of unhandled cases &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

            simple_cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;sc &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-word syntax-cast syntax-c++&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;simple_cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;clusters&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            comparison_count &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;-=&lt;/span&gt; sc&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;get_comparison_count&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The dynamic programming loop mirrors the one we used for bitsets. We want to find the minimum number of jump tables to represent the first i case labels.&lt;/p&gt;
&lt;p&gt;We iterate over splits at the &lt;code&gt;j&lt;/code&gt;-th case label, for &lt;code&gt;j &amp;lt; i&lt;/code&gt;. During each iteration we track the best scoring split. The special case &lt;code&gt;j == 0&lt;/code&gt; represents a single jump table for the entire &lt;code&gt;switch&lt;/code&gt; statement.&lt;/p&gt;
&lt;p&gt;Inside this inner loop we also keep track of:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How many distinct jump tables we would need to lower the switch.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;start&lt;/strong&gt; of the &lt;em&gt;last&lt;/em&gt; jump table of the decomposition.&lt;/li&gt;
&lt;li&gt;How many cases &lt;em&gt;cannot&lt;/em&gt; be handled by a jump table. In the code, this is the accumulator &lt;code&gt;s&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Point 1. and 2. are analogous to the bitset lowering pass, while 3. is new and specific to jump tables.&lt;/p&gt;
&lt;p&gt;Namely, there is some overhead to dispatching to a jump table, due to the indirect jump and the need to check bounds. Therefore &lt;code&gt;gcc&lt;/code&gt; requires a minimum amount of &lt;code&gt;case&lt;/code&gt; labels to generate a jump table.&lt;/p&gt;
&lt;p&gt;If a split would generate a jump table that is too small, the size of the jump table is added to an accumulator &lt;code&gt;s&lt;/code&gt;, which is used as a tie-breaker between different split candidates.&lt;/p&gt;
&lt;p&gt;To compute the scoring, we need the comparison count between &lt;code&gt;j&lt;/code&gt;-th and the &lt;code&gt;i&lt;/code&gt;-th case label. We compute it summing up the number of comparisons for all labels, and decreasing the count as we progress in the inner loop.&lt;/p&gt;
&lt;p&gt;We also need the number of unhandled &lt;code&gt;case&lt;/code&gt; labels. We determine it comparing the size of the jump table to the result of the &lt;code&gt;case_values_threshold&lt;/code&gt; function. This function is architecture dependent and takes into account the presence of switch-case specific instructions, but on modern x64 and Arm64 machines, it defaults to 5.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;default_case_values_threshold &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c++&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;targetm&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;have_casesi&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s now focus on the main heuristic, starting with how &lt;code&gt;max_ratio&lt;/code&gt; is computed. I&#39;ve reproduced below the main loop, omitting lines unrelated to the main heuristic.&lt;/p&gt;
&lt;p&gt;We&#39;ve seen in the examples section that gcc uses the ratio between jump table entries and comparisons as a metric for the lowering the heuristic. The code below shows that distinct ratios are used when optimizing for speed (flags &lt;code&gt;-O[123|fast]&lt;/code&gt;) or size (flag &lt;code&gt;-Os&lt;/code&gt;). Specifically, a ratio of 8 is used when optimizing for speed, while a ratio of 2 is used when optimizing for size.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; HOST_WIDE_INT &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;max_ratio
        &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;optimize_insn_for_size_p&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
                &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; param_jump_table_max_growth_ratio_for_size    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This is `2`
&lt;/span&gt;                &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; param_jump_table_max_growth_ratio_for_speed&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; This is `8`
&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; l&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Set minimal # of clusters with i-th item to infinite.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
        min&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;quick_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;INT_MAX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; INT_MAX&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): Compute comparison count, see previous snippet
&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; j &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; i&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; j&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): Compute unhandled cases, see previous snippet
&lt;/span&gt;            
            &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Prefer clusters with smaller number of numbers covered.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
               &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; s &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_non_jt_cases&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
               &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;can_be_handled&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; max_ratio&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; comparison_count&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; 
            &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
                min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;min_cluster_item&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_count&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
					   s &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; `s` is the number of unhandled cases &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

            &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): Update comparison count, see previous snippet
&lt;/span&gt;        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now onto the main loop. Given two splits, we prefer the one that results in fewer jump tables being emitted. Or, if they would result in the same number of jump tables, we prefer the one that handles the most &lt;code&gt;case&lt;/code&gt; labels. We also ignore splits that would result in unprofitable jump tables, where profitability is checked by the &lt;code&gt;can_be_handled&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;can_be_handled&lt;/code&gt; condition, checks for overflow and compares the ratio of case labels to jump table entries:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;jump_table_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;can_be_handled &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-c++&quot;&gt;const&lt;/span&gt; vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;clusters&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; HOST_WIDE_INT &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;max_ratio&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt;
                                         &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; HOST_WIDE_INT &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;comparison_count&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; If the switch is relatively small such that the cost of one
       indirect jump on the target are higher than the cost of a
       decision tree, go with the decision tree.

       [...]

       For algorithm correctness, jump table for a single case must return
       true.  We bail out in is_beneficial if it&amp;#39;s called just for
       a single case.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;start &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; end&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; HOST_WIDE_INT range &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;get_range&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;start&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;get_low&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; clusters&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;end&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;get_high&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Check for overflow.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; ...
&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; lhs &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; max_ratio &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt; comparison_count&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The coda of the function also mirrors the one for bitset clusters. It iterates over all jump tables in reverse order, and prunes the ones that fail the &lt;code&gt;is_beneficial&lt;/code&gt; check.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;    vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;create&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Find and build the clusters.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; l&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; start &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; min&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;end&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_start&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Do not allow clusters with small number of cases.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;is_beneficial&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
            output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;jump_table_cluster&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;safe_push&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;clusters&lt;span class=&quot;syntax-meta syntax-brackets syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-begin syntax-c++&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;syntax-punctuation syntax-section syntax-brackets syntax-end syntax-c++&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        end &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; start&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;start &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

    output&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;reverse&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; output&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-c++&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;is_beneficial&lt;/code&gt; check for jump tables aims to prune small jump tables. It compares the number of case labels to &lt;code&gt;case_values_threshold&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;jump_table_cluster&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;is_beneficial &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-modifier syntax-c++&quot;&gt;const&lt;/span&gt; vec&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-begin syntax-c++&quot;&gt;&amp;lt;&lt;/span&gt;cluster &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-generic syntax-end syntax-c++&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; &lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;start &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; end&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;return&lt;/span&gt; end &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;-&lt;/span&gt; start &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;case_values_threshold&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Binary tree&lt;/h4&gt;
&lt;p&gt;If &lt;code&gt;gcc&lt;/code&gt; cannot emit a bitset cluster or a jump table, it falls back to a binary tree of comparison.&lt;/p&gt;
&lt;p&gt;In emitting the tree, it takes into account the probability of each case label cluster. The probability can be obtained via profile-guided optimization, or with the &lt;code&gt;__builtin_expect_with_probability&lt;/code&gt; builtin.&lt;/p&gt;
&lt;p&gt;The code is pretty straightforward.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;&lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-entity syntax-name syntax-function syntax-c++&quot;&gt;switch_decision_tree&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;balance_case_nodes &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-parameters syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;case_tree_node &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; case_tree_node &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-parameter syntax-c++&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;
    case_tree_node &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;np &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;head&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-storage syntax-type syntax-c&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        case_tree_node &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;left&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        profile_probability prob &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;profile_probability&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-c++&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sum probability of all case labels.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;        &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Count the number of entries on branch.  Also count the ranges.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            i&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            prob &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_prob&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            np &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_right&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Split the case label list such that both halves have equal probability.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;        &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
            npp &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; head&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            left &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
	    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; NOTE(xoranth): this divides prob by 2.
&lt;/span&gt;            profile_probability pivot_prob &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; prob&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;apply_scale&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;syntax-comment syntax-block syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;/*&lt;/span&gt; Skip nodes while their probability does not reach that amount.  &lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
                prob &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_prob&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;prob&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-dot syntax-c++&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-member syntax-c++&quot;&gt;initialized_p&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-method-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; prob &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;&amp;lt;&lt;/span&gt; pivot_prob&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-arithmetic syntax-c&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_right&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                    &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
                npp &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_right&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;

            np &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;npp &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-numeric syntax-c++&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;*&lt;/span&gt;head &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_parent&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; parent&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_left&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; left &lt;span class=&quot;syntax-keyword syntax-operator syntax-comparison syntax-c&quot;&gt;==&lt;/span&gt; np &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;syntax-constant syntax-language syntax-c&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-ternary syntax-c&quot;&gt;:&lt;/span&gt; left&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Recurse in both halves to build the binary tree.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;C++&quot;&gt;&lt;span class=&quot;syntax-source syntax-c++&quot;&gt;            &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;balance_case_nodes&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_left&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; np&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-c++&quot;&gt;balance_case_nodes&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-c++&quot;&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-keyword syntax-operator syntax-c&quot;&gt;&amp;amp;&lt;/span&gt;np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_right&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-separator syntax-c++&quot;&gt;,&lt;/span&gt; np&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_subtree_prob&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-c&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_prob&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_left&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_subtree_prob&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_left&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_subtree_prob&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-group syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-begin syntax-c++&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_right&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-group syntax-end syntax-c++&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
                np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_subtree_prob&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-operator syntax-assignment syntax-augmented syntax-c&quot;&gt;+=&lt;/span&gt; np&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_right&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_c&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-accessor syntax-arrow syntax-c++&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-variable syntax-other syntax-readwrite syntax-member syntax-c++&quot;&gt;m_subtree_prob&lt;/span&gt;&lt;span class=&quot;syntax-punctuation syntax-terminator syntax-c++&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-c++&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;syntax-keyword syntax-control syntax-c++&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;syntax-meta syntax-block syntax-c++&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-begin syntax-c++&quot;&gt;{&lt;/span&gt;
	    &lt;span class=&quot;syntax-comment syntax-line syntax-double-slash syntax-c&quot;&gt;&lt;span class=&quot;syntax-punctuation syntax-definition syntax-comment syntax-c&quot;&gt;//&lt;/span&gt; [...] Base case
&lt;/span&gt;        &lt;span class=&quot;syntax-punctuation syntax-section syntax-block syntax-end syntax-c++&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-c++&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;syntax-invalid syntax-illegal syntax-stray-bracket-end syntax-c++&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;We&#39;ve seen the heuristics and lowering strategies &lt;code&gt;gcc&lt;/code&gt; employs when compiling switch statements, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Compressing multiple &lt;code&gt;case&lt;/code&gt; labels into a bitset.&lt;/li&gt;
&lt;li&gt;Transforming the &lt;code&gt;switch&lt;/code&gt; statement into a jump table.&lt;/li&gt;
&lt;li&gt;Transforming the &lt;code&gt;switch&lt;/code&gt; statement into a binary decision tree.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For anyone interested in delving deeper, I&#39;d suggest examining the &lt;code&gt;gcc&lt;/code&gt; &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://github.com/gcc-mirror/gcc/blob/master/gcc/tree-switch-conversion.cc&quot;&gt;source code&lt;/a&gt;, and reading &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://www.researchgate.net/publication/245584786_A_Superoptimizer_Analysis_of_Multiway_Branch_Code_Generation&quot;&gt;this paper&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Finally, I want to thank B.X.V., C.P., and V.P. for their feedback when writing this post&lt;/em&gt;&lt;/p&gt;
</content:encoded>
    <category>news</category>
  </item>
</channel>
</rss>
