Re: Does Duff Device break C compilers

preston@tera.com (Preston Briggs)
7 May 1996 00:48:07 -0400

          From comp.compilers

Related articles
Does Duff Device break C compilers Lambert.Lum@eng.efi.com (Lambert Lum) (1996-05-06)
Re: Does Duff Device break C compilers preston@tera.com (1996-05-07)
Re: Does Duff Device break C compilers krste@ICSI.Berkeley.EDU (1996-05-08)
Re: Does Duff Device break C compilers jeremy@floyd.sw.oz.au (1996-05-10)
Re: Does Duff Device break C compilers rankin@eql.caltech.edu (1996-05-10)
Re: Does Duff Device break C compilers msb@sq.com (1996-05-10)
Re: Does Duff Device break C compilers preston@tera.com (1996-05-13)
Re: Does Duff Device break C compilers baynes@ukpsshp1.serigate.philips.nl (1996-05-19)
| List of all articles for this month |

From: preston@tera.com (Preston Briggs)
Newsgroups: comp.compilers
Date: 7 May 1996 00:48:07 -0400
Organization: /etc/organization
References: 96-05-050
Keywords: C, optimize

>item called Duff's Device. I showed to a couple of guys here at my
>work place and they never seen it before. Then they declared that such
>an unorthodox technique could potential break some C compilers.


I agree with the moderator. It's not obvious, but it's not that hard
to compile. I'd expect everyone to get it right. On the other hand,
I wouldn't be surprised if the resulting code wasn't that great. Why?
It makes an irreducible loop and lots of optimizers don't do much with
irreducible loops. Also hard to software pipeline, etc.


> n = (count + 7) / 8; /* count > 0 assumed */
> 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);
> }


Better is to rewrite it, like this


/* no assumptions made about count */
for (n = 0; n < count; n++)
to[n] = from[n];


which is correct, obvious, maintainable, parallelizable, vectorizable,
software pipelinable, and in all other ways superior. Well, I guess
it doesn't have a fancy name. You may call it "Preston's Ploy" if
that helps.


Preston Briggs
[Duff's device generated truly wonderful code on the PDP-11 because it
exactly matched its auto-increment addressing modes and loop control
instructions. It's hard to think of a modern machine where it'd be of
any practical use. Great hacks are like that. (See HAKMEM for many
fine PDP-6/10 examples.) -John]






--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.