[lore] C hacks
davidb at ucc.gu.uwa.edu.au
Wed Apr 2 15:47:40 WST 2008
The third line doesn't work, but here is a 'better' version.
Note the lack of conditionals, so none of that pesky branch
prediction when calculating b = max(a,b).
This line at least also compiles cleanly with gcc -Wall -ansi -pedantic
on both IA-32 and AMD-64.
b -= (((b-a)&(((unsigned)-1>>1)^-1))>>(sizeof(-1)*8-1))*(b-a);
On Fri, Mar 28, 2008 at 12:50:11AM +0900, David Basden wrote:
> I was thinking that maybe people are just writing C that is either
> too readable, too inefficient or both. Maybe a connect-the-equivilent-
> code puzzle or something?
> Here is just a quick one 'cause I'm beyond tired, but please add to it;
> I don't know if the third works, and might not be a good idea to let out
> in public even if it does...
> while (*a++ == *b++); strcpy(b,a);
> a ^= b ^= a; int tmp = a;
> a = b;
> b = a;
> a >> 1; a = a / 2;
> b = (((a - b) & (1<<(sizeof(int)-1))) && b) || a; if (a > b) b = a;
> a << 3; a = a * 8;
> 1 << a; (int) pow((int) a, 2);
> a & 1; a % 2;
> a & 7; a % 8;
> (a & 15) || ++b; if (a % 16 == 0)
> b = b + 1;
> lore mailing list
> lore at ucc.gu.uwa.edu.au
More information about the lore