rc5s.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // gettsc.inl
  3. //
  4. // gives access to the Pentium's (secret) cycle counter
  5. //
  6. // This software was written by Leonard Janke (janke@unixg.ubc.ca)
  7. // in 1996-7 and is entered, by him, into the public domain.
  8. #if defined(__WATCOMC__)
  9. void GetTSC(unsigned long&);
  10. #pragma aux GetTSC = 0x0f 0x31 "mov [edi], eax" parm [edi] modify [edx eax];
  11. #elif defined(__GNUC__)
  12. inline
  13. void GetTSC(unsigned long& tsc)
  14. {
  15. asm volatile(".byte 15, 49\n\t"
  16. : "=eax" (tsc)
  17. :
  18. : "%edx", "%eax");
  19. }
  20. #elif defined(_MSC_VER)
  21. inline
  22. void GetTSC(unsigned long& tsc)
  23. {
  24. unsigned long a;
  25. __asm _emit 0fh
  26. __asm _emit 31h
  27. __asm mov a, eax;
  28. tsc=a;
  29. }
  30. #endif
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <openssl/rc5.h>
  34. void main(int argc,char *argv[])
  35. {
  36. RC5_32_KEY key;
  37. unsigned long s1,s2,e1,e2;
  38. unsigned long data[2];
  39. int i,j;
  40. static unsigned char d[16]={0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
  41. RC5_32_set_key(&key, 16,d,12);
  42. for (j=0; j<6; j++)
  43. {
  44. for (i=0; i<1000; i++) /**/
  45. {
  46. RC5_32_encrypt(&data[0],&key);
  47. GetTSC(s1);
  48. RC5_32_encrypt(&data[0],&key);
  49. RC5_32_encrypt(&data[0],&key);
  50. RC5_32_encrypt(&data[0],&key);
  51. GetTSC(e1);
  52. GetTSC(s2);
  53. RC5_32_encrypt(&data[0],&key);
  54. RC5_32_encrypt(&data[0],&key);
  55. RC5_32_encrypt(&data[0],&key);
  56. RC5_32_encrypt(&data[0],&key);
  57. GetTSC(e2);
  58. RC5_32_encrypt(&data[0],&key);
  59. }
  60. printf("cast %d %d (%d)\n",
  61. e1-s1,e2-s2,((e2-s2)-(e1-s1)));
  62. }
  63. }