Article View: comp.lang.c
Article #222Re: Taming The Switch-Statement
From: bart
Date: Sun, 11 Oct 2026 16:23
Date: Sun, 11 Oct 2026 16:23
96 lines
4901 bytes
4901 bytes
On 11/10/2026 15:42, Waldek Hebisch wrote:
> bart <bc@freeuk.com> wrote:
>> On 10/10/2026 16:50, David Brown wrote:
>>> On 10/10/2026 15:11, bart wrote:
>>
>> Because unfortunately C's flexibility makes weird stuff possible.
>>
>> C's 'switch' is truly bizarre. Below is an experiment where I took an
>> ordinary function body, and wrapped 'switch' around it with randomly
>> placed case labels.
>>
>> It still worked (as I placed 'default:' near the start, and providing
>> 'm' is greater than 7), but What The Hell?
>>
>> gcc with warnings says nothing useful about it.
>>
> <snip>
>> The fall-through feature is a minor detail.
>>
>> -----------------------
>>
>> static void nsieve(int m) {
>> unsigned int count = 0, i, j;
>> boolean * flags = (boolean *) malloc(m * sizeof(boolean));
>> for (i = 0; i < m; ++i) flags[i]=1;
>>
>> for (i = 2; i < m; ++i)
>> if (flags[i]) {
>> ++count;
>> for (j = i << 1; j < m; j += i)
>> flags[j] = 0;
>> }
>>
>> free(flags);
>> printf("Primes up to %8u %8u\n", m, count);
>> }
>>
>> static void nsieve_sw(int m) {
>> switch (m) {
>> default:
>> case 1:
>> unsigned int count = 0, i, j;
>> case 2:
>> boolean * flags = (boolean *) malloc(m * sizeof(boolean));
>> for (i = 0; i < m; ++i)
>> case 3:
>> flags[i]=1;
>>
>> for (i = 2; i < m; ++i)
>> if (flags[i]) {
>> case 4:
>> ++count;
>> case 5:
>> for (j = i << 1; j < m; j += i)
>> case 6:
>> flags[j] = 0;
>> }
>>
>> free(flags);
>> case 7:
>> printf("Primes up to %8u %8u\n", m, count);
>> }
>> }
>>
>
> I am disappointed by gcc optimizations (at least by gcc-12). gcc
> should notice that m equal to 3 immediately leads to use of
> uninitialized variable, so is undefined behaviour. Similarly
> m equal to 6. When m equals 4 or m equals 5 then test for
> containing loop will lead to use of uninitialized variable.
> AFAICS m equal 1 and m equal 2 are useless. gcc should assume
> that there is no undefined behaviour, so the only remaining
> case is m = 7. So gcc could simplify this function quite a lot.
> But looking at assembly shows that gcc generated actual switch
> with 5 cases (corresponding to m being 3, 4, 5, 6, 7).
>
My gcc 14 eliminates the entire function with -O3!
If I remove the 'static', the jumptable stays in, but it is only 5 elements.
If 'static' is left in but it is called as nsieve_sw(10000000) from
elsewhere, then it is inlined and the switch disappears.
BTW, nsieve_sw(10000000) takes gcc -O1/2/3 0.15 seconds to run.
With BCC (needs some labels to end with ";") the full jumptable stays
in, it is not inlined, but it also takes 0.15 seconds (when 'boolean' is
unsigned char).
The switch and call is a one-time cost, and the benchmark is memory
intensive, so it is kind to poor optimisers.
(But TCC takes 0.3s because locals reside in memory. Still, it is only
2:1 slower than optimised C. My scripting language takes 0.9s when using
a bit-array. Lua/CPython take 2.7/2.5s using default numeric type.)
Technical Details
Message-ID:
<11agd65$2pscf$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$1@dont-email.me> <11ag78a$1nqv3$1@paganini.bofh.team>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: bart <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Taming The Switch-Statement Date: Sun, 11 Oct 2026 17:23:35 +0100 Organization: A noiseless patient Spider Lines: 96 Message-ID: <11agd65$2pscf$1@dont-email.me> References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$1@dont-email.me> <11ag78a$1nqv3$1@paganini.bofh.team> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 11 Oct 2026 16:23:34 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2945423"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ufQSPUJhT9rUBAiFrb3E5"; posting-host="bb4c63ed27c528dcfc498e66a186f254" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:I2tc3pRQStCc8P4Bxk+r/m5rV0Y= sha256:GX67374fERR0a44rw84pzr3JNFb5Dal51oBNaSEQZvk= sha1:MWV92u/aVhbL45QXrkdg7kec6ZA= sha256:BBLyU1Eh1QA3hWBX4UZksKMnXrBB3oc+/Ro1rhSHCzc= Content-Language: en-GB In-Reply-To: <11ag78a$1nqv3$1@paganini.bofh.team>