Article View: comp.lang.c
Article #220Re: Taming The Switch-Statement
From: bart
Date: Sun, 11 Oct 2026 15:41
Date: Sun, 11 Oct 2026 15:41
50 lines
2997 bytes
2997 bytes
On 11/10/2026 11:58, bart wrote:
> On 10/10/2026 16:50, David Brown wrote:
> This is the core C code for Duff's Device from Wikipedia:
>
> switch (count % 8) {
> case 0: *to = *from++;
> case 7: *to = *from++;
> case 6: *to = *from++;
> case 5: *to = *from++;
> case 4: *to = *from++;
> case 3: *to = *from++;
> case 2: *to = *from++;
> case 1: *to = *from++;
> }
>
> This is the emulation of it in my language ('to' is a reserved word, and
> that constuct is 1-based):
>
> goto (count rem 8| L1, L2, L3, L4, L5, L6, L7 | L0)
>
> L0: d^ := s++^
> L7: d^ := s++^
> L6: d^ := s++^
> L5: d^ := s++^
> L4: d^ := s++^
> L3: d^ := s++^
> L2: d^ := s++^
> L1: d^ := s++^
>
> Those labels need to be unique, but probably you won't need more than
> one Duff's Device in any one function.
Here is it in my language after 10 minutes' work to create a
fall-through version of my switch:
switchf count rem 8
when 0 then d^ := s++^
when 7 then d^ := s++^
when 6 then d^ := s++^
when 5 then d^ := s++^
when 4 then d^ := s++^
when 3 then d^ := s++^
when 2 then d^ := s++^
when 1 then d^ := s++^
end
It remains properly structured, but the block order is now important.
The feature will not be kept.
Technical Details
Message-ID:
<11aganl$2oslb$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>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 16:41:43 +0100 Organization: A noiseless patient Spider Lines: 50 Message-ID: <11aganl$2oslb$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 11 Oct 2026 15:41:41 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2912939"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0KJj+t31GlEioRRtxr1AA"; posting-host="bb4c63ed27c528dcfc498e66a186f254" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:bohvevcAw3lek2CXY+4KU+mTGOg= sha256:17pgMVo8AYdDxT7NtILuw/UXXR1exqXE3bDhVzBGaY0= sha1:5Sb34kj6P920HgWlmHwp4ktFq/w= sha256:/zZhQeGebXes3TvzAngnFLstT2ByHALcoMtVOfDewt4= Content-Language: en-GB In-Reply-To: <11afq4b$2idte$1@dont-email.me>