Cooler_

test bench

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