Advertisement
Guest User

Untitled

a guest
Jun 15th, 2020
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1.  
  2. #include <stdint.h>
  3. #include <stdio.h>
  4.  
  5. #include <x86intrin.h>
  6.  
  7. #define ISODD(n) (n) & 1
  8.  
  9. unsigned char max2(unsigned char * x, int n) {
  10. unsigned char m = *x++;
  11. for(int i = 0; i < n; i++, x++)
  12. if(m < *x)
  13. m = *x;
  14. return m;
  15. }
  16.  
  17.  
  18.  
  19. unsigned char m1(const unsigned char* u8x, int n)
  20. {
  21. const uint64_t* u64x = (u8x);
  22. int i, j;
  23. unsigned char max = 0;
  24. for(i = 0, j = 0; i < n; i += 8, j++)
  25. {
  26. //"or" and "bitor" are keywords, of course
  27. uint64_t oR = u64x[j];
  28. oR |= oR >> 32;
  29. oR |= oR >> 16;
  30. oR |= oR >> 8;
  31. if(oR > (uint64_t)max)
  32. {
  33. for(int off = 0; off < 8; off++)
  34. max = max > u8x[i + off] ? max : u8x[i + off];
  35. }
  36. }
  37. i -= 8;
  38. for(; i < n; i++)
  39. max = max > u8x[i] ? max : u8x[i];
  40. return max;
  41. }
  42.  
  43.  
  44. int main(void) {
  45. uint64_t a, b;
  46. unsigned char ret, array[4096];
  47.  
  48. srand(time(NULL));
  49.  
  50. for(int i = 0; i < 4096; i++)
  51. array[i] = rand();
  52.  
  53. a = __rdtsc();
  54. ret = m1(array, 4096);
  55. printf("you: %llu ", __rdtsc() - a, ret);
  56.  
  57. a = __rdtsc();
  58. ret = max2(array, 4096);
  59. printf("Hui: %llu", __rdtsc() - a, ret);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement