Pigeon's Device Many may have heard tell of Duff's device, a rather neat loop optimisation technique in C. This page provides details of Pigeon's device - a related but independently-originated technique. The original instantiation of Pigeon's device was in a piece of C code written for MS-DOS before the days of the internet, so I had not heard of Duff's device. When I did, the similarity between the techniques was immediately apparent. The original Pigeon's device was in a function for comparing two date/time records in various different manners according to the desired sort order - FORWARD, forward chronological order; REVERSE, reverse chrononological order; and REVDFWDT, dates in reverse but times within each day sorted forwards. It's kind of a mess to read in its original form because of the various typedefs which are unique to the original program and other such cack, but the basic structure is quite simple. Here it is expressed as a function: int pigeons_device(int a, int b, int mode) { int result; /* Isn't C a wonderful language? */ switch (mode) { case 0: if (gloop(a, b)) { case 1: result = arfle(a, b); break; } else { case 2: result = barfle(a, b); break; } } return (result); } Or in other words... If mode == 1, return some function of a and b. If mode == 2, return a different function of a and b. But if mode == 0, decide which function of a and b to return based on some other relation between a and b. Here it is with the possible flows of control marked out with pretty coloured arrows: Why might one want to do that? The simple answer is "because one is a pigeon". The more complicated answer is... well, here is the original implementation, from which you may work it out for yourself. int lfdcmp(const void *a, const void *b) { static int mode; struct date d1, d2; struct time t; if (b==NULL) return(mode); if (a==NULL) { mode=*(int *)b; return(0); } /* Isn't C a wonderful language? */ switch (mode) { case REVDFWDT: unixtodos(((lfdy *)a)->stamplog, &d1, &t); un...
First seen: 2026-01-11 19:58
Last seen: 2026-01-11 20:58