Disturbing C: interleaved Switch and While

Feel like posting Off Topic? Do it here.

Moderator: MaxCoderz Staff

Post Reply
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Disturbing C: interleaved Switch and While

Post by King Harold »

Instead of:

Code: Select all

do {
    *to = *from++;
} while (--count > 0);
this: (automatic loop unrolling)

Code: Select all

    register n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *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++;
               } while (--n > 0);
    }
I find it.. disturbing.
So, what's everyone else's opinion?
(btw, this is called "Duff's device")
Spencer
Extreme Poster
Posts: 346
Joined: Mon 17 Jan, 2005 8:56 am
Location: Indiana

Post by Spencer »

I like it. Why stop there? 16 cases = $$$

These days, I think I'll stick with -funroll-loops. There my have been a time when these "manual C optimizations" where beneficial, but now they would just confuse your compiler.
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

I like how that code writes repeatedly to the same destination address.
You know your hexadecimal output routine is broken when it displays the character 'G'.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

Dwedit wrote:I like how that code writes repeatedly to the same destination address.
It’s a memory mapped register.

A much nicer example of abusing switch and C macros is implementing coroutines. I think Contiki uses something similar for its protothreads.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Spencer wrote:I like it. Why stop there? 16 cases = $$$

These days, I think I'll stick with -funroll-loops. There my have been a time when these "manual C optimizations" where beneficial, but now they would just confuse your compiler.
It could end up doing the same
Or it could just use 2 loops with the second one doing the "rest" (but that's boring)

edit: those coroutine macro's scare me a bit..
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

The best method for practical programming is to just call memset or memcpy, those functions are ASM optimized like crazy.
You know your hexadecimal output routine is broken when it displays the character 'G'.
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

They update their 'to' pointer as well though (or even only their to pointer in the case of memset) so they do something different
User avatar
Timendus
Calc King
Posts: 1729
Joined: Sun 23 Jan, 2005 12:37 am
Location: Netherlands
Contact:

Post by Timendus »

CoBB wrote:A much nicer example of abusing switch and C macros is implementing coroutines. I think Contiki uses something similar for its protothreads.
You are correct, it's exactely the same principle... I still think it's creepy though ;)
http://clap.timendus.com/ - The Calculator Link Alternative Protocol
http://api.timendus.com/ - Make your life easier, leave the coding to the API
http://vera.timendus.com/ - The calc lover's OS
Post Reply