Re: Does Duff Device break C compilers

krste@ICSI.Berkeley.EDU (Krste Asanovic)
8 May 1996 00:18:33 -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: krste@ICSI.Berkeley.EDU (Krste Asanovic)
Newsgroups: comp.compilers
Date: 8 May 1996 00:18:33 -0400
Organization: International Computer Science Institute, Berkeley, CA, U.S.A.
References: 96-05-050 96-05-054
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.


preston@tera.com (Preston Briggs) writes:
> 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. ...
> 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.


Better yet, "Krste's Kopy" :-)


                memcpy(from, to, count*(sizeof *from));


This is even shorter and more maintainable code, and will let you use
the best hand-tweaked hacks on any platform, without relying on
compiler heroics. (Even if you have to write your own memcpy because
the supplied libraries are too slow....)


Can also be wrapped up into a macro for even shorter, more maintable
code:


#define veccpy(count, from, to) memcpy((from), (to), count*(sizeof *(from)))


--
Krste Asanovic phone: +1 (510) 642-4274 x143
International Computer Science Institute fax: +1 (510) 643-7684
1947 Center Street, Suite 600 email: krste@icsi.berkeley.edu
Berkeley, CA 94704-1198, USA http://www.icsi.berkeley.edu/~krste
[Oh, right. Duh. It was still a great hack on the PDP-11. -John]


--


Post a followup to this message

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