Cooler_

Cooler's bitwise otimize PoC

Feb 20th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. /*
  2. by Cooler
  3. to test div with bitwise
  4. gcc -o code code.c -O3
  5. */
  6. #include <stdio.h>
  7. #include <x86intrin.h>
  8. #include <cpuid.h>
  9. #include <inttypes.h>
  10.  
  11. #define LAST "\033[0m"
  12. #define RED "\033[22;31m"
  13.  
  14. void mul()
  15. {
  16.  int x=50,y=0;
  17.  while(x)
  18.  {
  19.   y=x*2;
  20.   x--;
  21.  }
  22. }
  23.  
  24.  
  25. void leftshift()
  26. {
  27.  int x=50,y=0;
  28.  while(x)
  29.  {
  30.   y=x<<1;
  31.   x--;
  32.  }
  33. }
  34.  
  35. unsigned div7(unsigned n)
  36. {
  37.  return n/7;
  38. }
  39.  
  40. unsigned bit_div7(unsigned num)
  41. {  
  42.  unsigned x,y;
  43.  
  44.  x=(num>>1)+(num>>4);
  45.  x+=x>>6;
  46.  x+=(x>>12)+(x>>24);
  47.  x>>=2;
  48.  y=num-((x<<3)-x);
  49.  
  50.  return x+((y+1)>>3);
  51. }  
  52.  
  53.  
  54. unsigned div3(unsigned n)
  55. {
  56.  return n/3;
  57. }
  58.  
  59. //
  60. unsigned bit_div3(unsigned num)
  61. {
  62.  unsigned x,y;
  63.  
  64.  x=(num>>2)+(num>>4);
  65.  x+=x>>4;
  66.  x+=x>>8;
  67.  x+=x>>16;
  68.  y=num-((x<<2)-x);
  69. // ficou ruim , return x+( ((y<<3)+(y<<2)-y)>>5);
  70. // ruim  return x+( (y+5+(y<<2)) >>4);
  71.  return x+( (((y+1)<<2)+(y+1)) >>4);
  72.  
  73. }
  74.  
  75.  
  76.  
  77. int main(void)
  78. {
  79.   int i, x;
  80.   uint32_t a, b, c, d;
  81.   uint64_t y;
  82.  
  83.   x = 0;
  84.   do {
  85.     __cpuid(0, a, b, c, d);
  86.     y = _rdtsc();
  87.     leftshift();
  88.     y = _rdtsc() - y;
  89.   } while (++x < 2);
  90.  
  91.   printf("::: left shift: %s  %lld %s cicles\n",RED, y,LAST);
  92.  
  93.  
  94.   x = 0;
  95.   do {
  96.     __cpuid(0, a, b, c, d);
  97.     y = _rdtsc();
  98.     mul();
  99.     y = _rdtsc() - y;
  100.   } while (++x < 2);
  101.  
  102.   printf("::: mul: %s %lld %s cicles\n", RED,y,LAST);
  103.  
  104.  
  105.   unsigned int z=0;
  106.  
  107.   x = 0;
  108.   do {
  109.     __cpuid(0, a, b, c, d);
  110.     y = _rdtsc();
  111.     z=div7(560000*x);
  112.     printf("result: %d\n",z);
  113.     y = _rdtsc() - y;
  114.   } while (++x < 2);
  115.  
  116.   printf("::: div7: %s %lld %s cicles\n", RED,y,LAST);
  117.  
  118.   z=0;
  119.   x = 0;
  120.  
  121.   do {
  122.     __cpuid(0, a, b, c, d);
  123.     y = _rdtsc();
  124.     z=bit_div7(560000*x);
  125.     printf("result: %d\n",z);
  126.     y = _rdtsc() - y;
  127.   } while (++x < 2);
  128.  
  129.   printf("::: bit_div7:  %s %lld %s cicles\n",RED, y,LAST);
  130.  
  131.  
  132.   x = 0;
  133.   do {
  134.     __cpuid(0, a, b, c, d);
  135.     y = _rdtsc();
  136.     z=div3(560000*x);
  137.     printf("result: %d\n",z);
  138.     y = _rdtsc() - y;
  139.   } while (++x < 2);
  140.  
  141.   printf("::: div3:  %s %lld %s cicles\n",RED, y,LAST);
  142.  
  143.   z=0;
  144.   x = 0;
  145.  
  146.   do {
  147.     __cpuid(0, a, b, c, d);
  148.     y = _rdtsc();
  149.     z=bit_div3(560000*x);
  150.     printf("result: %d\n",z);
  151.     y = _rdtsc() - y;
  152.   } while (++x < 2);
  153.  
  154.   printf("::: bit_div3:  %s %lld %s cicles\n", RED,y,LAST);
  155.  
  156.  
  157.  
  158.   return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment