🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #227

Re: Taming The Switch-Statement

#227
From: bart
Date: Sun, 11 Oct 2026 18:10
68 lines
3923 bytes
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().

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.
Technical Details
Message-ID: <11agjff$2sf17$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-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>

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 19:10:53 +0100
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <11agjff$2sf17$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>
 <11agd65$2pscf$1@dont-email.me> <11agi5a$1oe5j$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 18:10:56 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3030055"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX191IUqX18AVk3MH2u5bd51J"; posting-host="bb4c63ed27c528dcfc498e66a186f254"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:eq+HL2x7227CaWlfDmsDNgFekyA= sha256:zz7aaTu/auiLOMIb+sJvxzDQBocbSreb58Atle9mJjA=
	sha1:u0rZ5tiBENfKKRn/Sdkyp4dMAjE= sha256:Aq7Wp8ENHsyXEsDlbjpwpCDG6iOxSyRy0yGFsFO6zc8=
In-Reply-To: <11agi5a$1oe5j$1@paganini.bofh.team>
Content-Language: en-GB