#include #include #include /* * Timings - gcc for amd64: * 0: 184 ms * 1: 183 ms * 2: 183 ms * 3: 183 ms * 4: 183 ms 5: 413 ms 6: 1967 ms 7: 2012 ms - gcc for the PS3 (PowerPC): 0: 684.449585 ms 1: 681.490845 ms 2: 680.872742 ms * 3: 634.242981 ms * 4: 632.235352 ms 5: 1004.905457 ms 6: 1333.606567 ms 7: 1333.346313 ms - gcc for i386: * 0: 173 ms * 1: 172 ms * 2: 173 ms 3: 220 ms 4: 215 ms 5: 344 ms 6: 659 ms 7: 750 ms * */ uint32_t getfoo(uint32_t x, uint32_t y) { return (x & y) & 1; } template void fun(uint32_t max) { struct timeval tv0, tv; gettimeofday(&tv0, NULL); uint32_t total = 0; for (uint32_t x = 0; x < max; x++) { uint32_t bar = x; for (uint32_t i = 0; i < 100000; i++) { uint32_t foo = getfoo(bar, i); bar += i; switch (VARIANT) { case 0: bar >>= foo; break; case 1: bar >>= foo ? 1 : 0; break; case 2: bar >>= !!foo; break; case 3: if (foo) bar >>= 1; break; case 4: bar = foo ? bar >> 1 : bar; break; case 5: bar = foo * (bar >> 1) | (1 - foo) * bar; break; case 6: bar /= (1 + foo); break; case 7: bar /= (foo ? 2 : 1); break; } } total += bar; } gettimeofday(&tv, NULL); printf("%i: %i (%i ms)\n", VARIANT, total, (tv.tv_sec - tv0.tv_sec) * 1000ULL + (tv.tv_usec - tv0.tv_usec + 500) / 1000); } int main(void) { int max = 1000; fun<0>(max); fun<1>(max); fun<2>(max); fun<3>(max); fun<4>(max); fun<5>(max); fun<6>(max); fun<7>(max); return 0; }