Duff

Skip this unless you’re a C programmer with a strong stomach…
I’m sure that the seasoned hackers and computer scientists amongst you will probably already have encountered Duff’s Device, but it was only after rambling along some overgrown link-paths from a slashdot article that I discovered it. In a nutshell, this is it:

send(to, from, count)
	register short *to, *from;
	register count;
	{
		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);
		}
	}

As the man himself said, “Disgusting, no ?”. It’s just so, so, sick and weird, and yet so beautiful. It’s like one of those sculptures or pictures that your brain just can’t cope with; one that forces you to either run screaming or just stand there tilting your head and squinting.
It wasn’t until I’d compiled it and filled it with printf’s that it started to make sense but I’m still feeling slightly dirty…

Share

Leave a Reply