Types.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "Types.h"
  2. #include "Random.h"
  3. #include <stdio.h>
  4. uint32_t MurmurOAAT ( const void * blob, int len, uint32_t seed );
  5. //-----------------------------------------------------------------------------
  6. #if defined(_MSC_VER)
  7. #pragma optimize( "", off )
  8. #endif
  9. void blackhole ( uint32_t )
  10. {
  11. }
  12. uint32_t whitehole ( void )
  13. {
  14. return 0;
  15. }
  16. #if defined(_MSC_VER)
  17. #pragma optimize( "", on )
  18. #endif
  19. uint32_t g_verify = 1;
  20. void MixVCode ( const void * blob, int len )
  21. {
  22. g_verify = MurmurOAAT(blob,len,g_verify);
  23. }
  24. //-----------------------------------------------------------------------------
  25. bool isprime ( uint32_t x )
  26. {
  27. uint32_t p[] =
  28. {
  29. 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,
  30. 103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,
  31. 199,211,223,227,229,233,239,241,251
  32. };
  33. for(size_t i=0; i < sizeof(p)/sizeof(uint32_t); i++)
  34. {
  35. if((x % p[i]) == 0)
  36. {
  37. return false;
  38. }
  39. }
  40. for(int i = 257; i < 65536; i += 2)
  41. {
  42. if((x % i) == 0)
  43. {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. void GenerateMixingConstants ( void )
  50. {
  51. Rand r(8350147);
  52. int count = 0;
  53. int trials = 0;
  54. int bitfail = 0;
  55. int popfail = 0;
  56. int matchfail = 0;
  57. int primefail = 0;
  58. //for(uint32_t x = 1; x; x++)
  59. while(count < 100)
  60. {
  61. //if(x % 100000000 == 0) printf(".");
  62. trials++;
  63. uint32_t b = r.rand_u32();
  64. //uint32_t b = x;
  65. //----------
  66. // must have between 14 and 18 set bits
  67. if(popcount(b) < 16) { b = 0; popfail++; }
  68. if(popcount(b) > 16) { b = 0; popfail++; }
  69. if(b == 0) continue;
  70. //----------
  71. // must have 3-5 bits set per 8-bit window
  72. for(int i = 0; i < 32; i++)
  73. {
  74. uint32_t c = ROTL32(b,i) & 0xFF;
  75. if(popcount(c) < 3) { b = 0; bitfail++; break; }
  76. if(popcount(c) > 5) { b = 0; bitfail++; break; }
  77. }
  78. if(b == 0) continue;
  79. //----------
  80. // all 8-bit windows must be different
  81. uint8_t match[256];
  82. memset(match,0,256);
  83. for(int i = 0; i < 32; i++)
  84. {
  85. uint32_t c = ROTL32(b,i) & 0xFF;
  86. if(match[c]) { b = 0; matchfail++; break; }
  87. match[c] = 1;
  88. }
  89. if(b == 0) continue;
  90. //----------
  91. // must be prime
  92. if(!isprime(b))
  93. {
  94. b = 0;
  95. primefail++;
  96. }
  97. if(b == 0) continue;
  98. //----------
  99. if(b)
  100. {
  101. printf("0x%08x : 0x%08x\n",b,~b);
  102. count++;
  103. }
  104. }
  105. printf("%d %d %d %d %d %d\n",trials,popfail,bitfail,matchfail,primefail,count);
  106. }
  107. //-----------------------------------------------------------------------------