🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #259

Re: Taming The Switch-Statement

#259
From: Waldek Hebisch
Date: Mon, 12 Oct 2026 07:01
79 lines
4070 bytes
bart <bc@freeuk.com> wrote:
> On 11/10/2026 18:48, Waldek Hebisch wrote:
>> bart <bc@freeuk.com> wrote:
>>> On 11/10/2026 15:42, Waldek Hebisch wrote:
>>>> bart <bc@freeuk.com> wrote:
> 
>>>>> 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.
>> 
>> Of course I removed 'static'.  A single 'static' function which is
>> never called should be removed.
> 
> If I do this:
> 
>   static void nsieve_sw(int m) { ... }
> 
>   static void* abc = nsieve_sw;
> 
>   int main(void) {
>       abc = nsieve_sw;
>   }
> 
> then nsieve_sw is never called, but it is not removed. It /is/ removed 
> if 'abc' is moved into main().

Another missing optimization.
 
> Apart from that anomaly, only the potential to call even a static 
> function needs to exist, say by taking its address. The function might 
> never be called though.

Sure, if compiler can not decide (say due to taken address) if a
function is called, then it should keep it.  But the case above is
an easy one.  And in other contexts gcc actually can do transfomations
that require rather complex reasoning to justify.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11ai0jt$206ko$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
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> <11agd65$2pscf$1@dont-email.me> <11agi5a$1oe5j$1@paganini.bofh.team> <11agjff$2sf17$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Mon, 12 Oct 2026 07:01:19 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11ai0jt$206ko$1@paganini.bofh.team>
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> <11agd65$2pscf$1@dont-email.me> <11agi5a$1oe5j$1@paganini.bofh.team> <11agjff$2sf17$1@dont-email.me>
Injection-Date: Mon, 12 Oct 2026 07:01:19 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="2103960"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2