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