Re: Duff's Device
- From: Laurent Deniau <laurent.deniau@xxxxxxx>
- Date: Thu, 28 Sep 2006 18:28:45 +0200
Hallvard B Furuseth wrote:
I've been wondering sometimes:
Anyone know why Duff's device is usually written like this:
void duff(const char *str, int len) {
int n = (len + 7) / 8;
switch (len % 8) {
case 0: do{ foo(*str++);
case 7: foo(*str++);
case 6: foo(*str++);
...
case 1: foo(*str++);
} while (--n > 0);
}
}
instead of this?
void duff2(const char *str, int len) {
switch (len % 8) {
case 0: while ((len -= 8) >= 0) {
foo(*str++);
case 7: foo(*str++);
case 6: foo(*str++);
...
case 1: foo(*str++);
}
}
}
The original has an extra '+' and doesn't handle len=0.
Nor does it need the divide by 8, though I realize n-=1
may be cheaper than n-=8 on some architectures.
People have had 18 years to notice now:-)
I did some speed test some time ago with gcc on x86 and the fastest version I was able to find was:
static inline void
ooc_memchr_copy( char *restrict dst,
const char *restrict src, size_t cnt)
{
size_t rem = cnt % 8;
cnt = (cnt / 8) + 1;
switch (rem)
do { *dst++ = *src++;
case 7: *dst++ = *src++;
case 6: *dst++ = *src++;
case 5: *dst++ = *src++;
case 4: *dst++ = *src++;
case 3: *dst++ = *src++;
case 2: *dst++ = *src++;
case 1: *dst++ = *src++;
case 0: ;
} while(--cnt);
}
which is not the one published and works for cnt==0 as well as for cnt==(size_t)-1. Why do you think that the orignal version is always the one used?
a+, ld.
.
- References:
- Duff's Device
- From: Hallvard B Furuseth
- Duff's Device
- Prev by Date: Re: How to let the process exit if the signal its waiting for doesn't arrive
- Next by Date: Re: Length of Variable Names Affect Compiled Executable?
- Previous by thread: Re: Duff's Device
- Next by thread: Re: Duff's Device
- Index(es):
Relevant Pages
|