Usually I would let the compiler do it’s magic of optimizing complicated logical expressions, however, in this case the compiler I have to use is not very good at this (basically all it can do is to replaced things like /64 with bit-shifts and %512 with bitwise-and).
Is there any tool available that can analyze and provide optimized versions of expressions, (i.e. the same way good optimizing compilers do)?
e.g. I would like to optimize the following:
int w = 2 - z/2;
int y0 = y + (((v % 512) / 64) / 4) * 8 + ((v / 512) / mb)*16;
int x0 = x + (((v % 512) / 64) % 4) * 8 * (w - 1) + ((v / 512) % mb)*8 * w;
int i = x0 * (w ^ 3) * 2 + y0 * mb * 16 * 2 + (2*z - 3) * (z/2);
Here’s a test:
I compiled with GCC 4.7.0 with
-O3.With
int:With
unsigned int:“Optimizing” further by folding constants manually has (predictably) no further effect.