fork download
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. #ifdef _DEBUG
  5. const signed num_tests = 10000000;
  6. #else
  7. const signed num_tests = 1000000000;
  8. #endif
  9.  
  10. #ifdef _MSC_VER
  11. #define noinline __declspec(noinline)
  12. #else
  13. #define noinline __attribute__((noinline))
  14. #endif
  15.  
  16. void baseline(signed n, signed& cr0) {
  17. cr0 = 4;
  18. }
  19.  
  20. void AlfaOmega(signed n, signed& cr0) {
  21. if (n < 0)
  22. cr0 = 1;
  23. else if (n > 0)
  24. cr0 = 2;
  25. else
  26. cr0 = 4;
  27. }
  28.  
  29. void jalf(signed n, signed& cr0) {
  30. cr0 = 1 << ((n >= 0) + (n == 0));
  31. }
  32.  
  33. void harold(signed n, signed& cr0) {
  34. int y = ((n >> 31) & 1) | ((-n >> 31) & 2);
  35. y &= 1 | ~(y << 1); // remove the 2 if odd
  36. cr0 = (~(-y >> 31) & 4) | y;
  37. }
  38.  
  39. void MichaelBurr(signed n, signed& cr0) {
  40. cr0 = 4 >> ((2 * (n < 0)) + (n > 0));
  41. }
  42.  
  43. void BenVoigt(signed n, signed& cr0) {
  44. cr0 = (-(n | (n-1)) >> 31) & 6;
  45. cr0 |= (n >> 31) & 5;
  46. cr0 ^= 4;
  47. }
  48.  
  49. void Manish(signed n, signed& cr0) {
  50. cr0 = ((n >> 31 & 1) | (~n >> 30 & 2)) << !n;
  51. }
  52.  
  53. void MooingDuck1(signed n, signed& cr0) {
  54. cr0 = 1 << (!(unsigned(n)>>31)+(n==0));
  55. }
  56.  
  57. void MooingDuck2(signed n, signed& cr0) {
  58. cr0 = 2 << ((n == 0) - (unsigned(n) >> 31));
  59. }
  60.  
  61. typedef void (*funcptr)(signed n, signed& cr0);
  62. static noinline long test(funcptr func, const char* name, long overhead) {
  63. signed nocheat = 0;
  64. clock_t begin = clock();
  65. for(int i=0; i<num_tests; ++i) {
  66. signed t;
  67. func(-i, t);
  68. nocheat += t;
  69. func(0, t);
  70. nocheat += t;
  71. func(i, t);
  72. nocheat += t;
  73. }
  74. clock_t end = clock();
  75. std::cout << name << " found " << nocheat << " in " << (end-begin-overhead) << " ticks.\n";
  76. return end-begin;
  77. }
  78.  
  79. int main() {
  80. std::cout << "CLOCKS_PER_SEC is " << CLOCKS_PER_SEC << ".\n";
  81. long overhead = test(baseline, "baseline", 0); //6359
  82. test(AlfaOmega, "AlfaOmega", overhead); //2500
  83. test(jalf, "jalf", overhead); //3297
  84. test(harold, "harold", overhead); //6562
  85. test(MichaelBurr, "MichaelBurr", overhead); //3281
  86. test(BenVoigt, "BenVoigt", overhead); //0859
  87. test(Manish, "Manish", overhead); //3688
  88. test(MooingDuck1, "MooingDuck1", overhead); //3765
  89. test(MooingDuck2, "MooingDuck2", overhead); //2031
  90. return 0;
  91. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty