Friday, May 18, 2007

Livin on the edge

I just wrote my master's thesis in two weeks. If it gets accepted I'm off to LA for the most awesome job ever and the beach within walking distance. If it gets rejected I'm stranded in Sweden with a mediocre salary and 1.5 months of summer a year. Ahh, life on the edge, who can resist its tainted charm!

Sunday, May 13, 2007

Sthlm turf war @ the Sartorialist

Turns out the Sartorialist really digs Stockholm. Quite typically, its inhabitants got so enthused by all the attention that a regular word war broke out between the natural enemies Östermalm and Södermalm. Hilarious.

Tuesday, May 01, 2007

Duff's Device

This is a neat little trick for copying data containers that I don't see mentioned nearly as often in modern-day forums as in old Usenet posts. Whether this is because most STL implementations now include some similar contraption or because it's been superseded by fancier things is unclear, but I thought I'd publish it here in any case.

Quoting Wikipedia, "Duff's Device is an optimized implementation of a serial copy that uses a technique widely applied in assembly language for loop unwinding". Basically, a standard for-loop copy, e.g.
do
*to++ = *from++;
while(--count>0);
can be unrolled to minimize the amount of branches by using a while loop nested into a switch-statement:
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);
}
Many sources, including Wikipedia, claim that Duff's Device is a waste of optimization time and that sticking to library routines is always better. Others claim it has its uses though, albeit not as a general replacement to memcpy or std::copy. To quote Tom Duff himself, "If your code is too slow, you must make it faster. If no better algorithm is available, you must trim cycles."