[lore] C hacks

David Basden davidb at ucc.gu.uwa.edu.au
Wed Apr 2 15:58:35 WST 2008


Looks like I was a bit too hasty in posting. Looking at the assembly
of gcc -O3, it can obviously be shortened to the much less evil:

	b -= ((unsigned)(b-a)>>(sizeof(-1)*8-1))*(b-a);

David


On Wed, Apr 02, 2008 at 03:47:40PM +0800, David Basden wrote:
> 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);
> 
> David
> 
> 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
> > http://lists.ucc.gu.uwa.edu.au/mailman/listinfo/lore
> 
> _______________________________________________
> lore mailing list
> lore at ucc.gu.uwa.edu.au
> http://lists.ucc.gu.uwa.edu.au/mailman/listinfo/lore



More information about the lore mailing list