Feel like posting Off Topic? Do it here.
Moderator: MaxCoderz Staff
King Harold
Calc King
Posts: 1513 Joined: Sat 05 Aug, 2006 7:22 am
Post
by King Harold » Fri 22 Feb, 2008 7:25 pm
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 » Fri 22 Feb, 2008 8:08 pm
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.
Dwedit
Maxcoderz Staff
Posts: 579 Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:
Post
by Dwedit » Fri 22 Feb, 2008 8:36 pm
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 » Fri 22 Feb, 2008 8:36 pm
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 » Fri 22 Feb, 2008 8:46 pm
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..
Dwedit
Maxcoderz Staff
Posts: 579 Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:
Post
by Dwedit » Fri 22 Feb, 2008 9:06 pm
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 » Sat 23 Feb, 2008 10:26 am
They update their 'to' pointer as well though (or even only their to pointer in the case of memset) so they do something different
Timendus
Calc King
Posts: 1729 Joined: Sun 23 Jan, 2005 12:37 am
Location: Netherlands
Contact:
Post
by Timendus » Thu 27 Mar, 2008 5:26 pm
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