Skip to content

Instantly share code, notes, and snippets.

@menangen
Last active March 31, 2018 19:14
Show Gist options
  • Save menangen/fa2a0aaa34dfd3e433e742dc403de7b9 to your computer and use it in GitHub Desktop.
Save menangen/fa2a0aaa34dfd3e433e742dc403de7b9 to your computer and use it in GitHub Desktop.
RANDOM xorshift 0xF
uint8_t hash_coord( uint32_t x, uint32_t y, uint32_t seed) {
uint32_t m = ~x >> 19;
m |= 899809343;
uint32_t n = ~y >> 27;
//n += 0x1b873593;
n |= 0x1b873593;
seed ^= (m ^ 0x27d4eb2d);
seed ^= (n ^ 0xe6546b64);
seed ^= (m ^ 0x27d4eb2d);
seed ^= (n ^ 0xe6546b64);
seed = (x+0x7ed55d16) + (seed<<12);
seed = (seed^0xc761c23c) ^ (seed>>19);
seed = (seed+0x165667b1) + (seed<<5);
seed = (seed+0xd3a2646c) ^ (seed<<9);
seed = (seed+0xfd7046c5) + (seed<<3);
seed = (seed^0xb55a4f09) ^ (seed>>16);
x = x-y; x = x-seed; x = x^(seed >> 13);
y = y-seed; y=y-x; y=y^(x << 8);
seed = seed-x; seed=seed-y; seed=seed^(y >> 13);
x = x-y; x=x-seed; x=x^(seed >> 12);
y = y-seed; y=y-x; y=y^(x << 16);
seed = seed-x; seed=seed-y; seed=seed^(y >> 5);
x = x-y; x=x-seed; x=x^(seed >> 3);
y = y-seed; y=y-x; y=y^(x << 10);
seed = seed-x; seed=seed-y; seed=seed^(y >> 15);
return seed;
}
uint32_t integer_hash( uint32_t coordinate_int)
{
coordinate_int -= (coordinate_int << 6);
coordinate_int ^= (coordinate_int >> 17);
coordinate_int -= (coordinate_int << 9);
coordinate_int ^= (coordinate_int << 4);
coordinate_int -= (coordinate_int << 3);
coordinate_int ^= (coordinate_int << 10);
coordinate_int ^= (coordinate_int >> 15);
return coordinate_int;
}
void hash_coord_test(const uint8_t max_seed, const uint16_t max) {
const uint32_t size = max * max * max_seed;
uint32_t accum_array[size];
memset(accum_array, 0, size*sizeof(uint32_t));
static uint32_t index_linear = 0;
double cpu0 = get_cpu_time();
static uint8_t flag = 0;
for (uint8_t seed = 0; seed < max_seed; seed++) {
uint8_t not_seed = (seed << 5) + (seed >> 3) + 1;
printf("Processing seed:%u [~%u] \n", seed, not_seed);
if (not_seed == 0) { printf(">> 0 ! seed:%u\n", seed); }
for (uint16_t counter_y = 0; counter_y < max; counter_y++) {
for (uint16_t counter_x = 0; counter_x < max; counter_x++, index_linear++) {
/*
uint32_t hash_x = _mm_crc32_u32(not_seed, counter_x);
uint32_t hash = _mm_crc32_u32(hash_x, counter_y);
*/
uint32_t hash = integer_hash( (not_seed << 24) + (counter_x << 12) + counter_y);
if (index_linear == 0) {
accum_array[index_linear] = hash;
}
else {
for ( uint32_t index = 0; index < index_linear; ++index )
{
uint32_t hash2 = accum_array[index];
if (hash == hash2) {
printf("\n\t!!! seed:%u (x=%u : y=%u) HASH[%u]\n", seed, counter_y, counter_x, hash);
flag++;
breakLoop
}
else {
accum_array[index_linear] = hash;
}
}
}
//printf("index linear: %u", index_linear);
//printf("\n\nseed: %u|x = %u; y = %u | %u\n", seed, counter_x, counter_y, hash);
breakLoop
}
}
breakLoop
//printf("=====\n");
}
double cpu1 = get_cpu_time();
printf("\n\n\t Time: %f \n\n", cpu1 - cpu0);
}
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n\n\n");
hash_coord_test(255, 64);
/*
uint32_t x, y;
uint8_t seed;
x = 0;
y = 0;
seed = 0;
uint8_t not_seed = ((seed ^ 61) << 4) ^ ((seed ^ 199) << 1);
printf("\n\tseed:%u ~seed[%u]\n", seed, not_seed);
uint32_t hash = small_hash((y << 12) + x + (not_seed << 24));
printf("\n\tseed:%u (x=%u : y=%u) HASH[%u]\n", seed, y, x, hash);
*/
return 0;
}
uint16_t table_xor[15], table_random[15];
uint8 xorshift8menangen(uint64_t s[]) {
uint64_t x = s[0];
uint64_t const y = s[1];
s[0] = y;
x ^= x << 23; // a
// Default: s[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
s[1] = x ^ y ^ (x >> 17) ^ (y >> 26) ^ (((x ^ 255) << 7) ^ ((y ^ 255) << 2 ) ^ 255); // b, c
uint16_t xor_result = (s[1] + y) % 0xF;
uint16_t rnd_result = rand() % 0xF;
table_xor[xor_result] += 1;
table_random[rnd_result] += 1;
return xor_result;
}
int main(int argc, const char * argv[]) {
printf("Hello, random!\n");
uint64_t s[2];
s[0] = 64; // = 0
s[1] = 64; // = 0
for (int i = 0; i < 14700; i++) {
uint8 result = xorshift8menangen(s);
//printf("Random: %u \n", result);
}
for (int i = 0; i < 15; i++) {
printf("Distribution [ %i ] XOR: %u \t RND: %u \n", i, table_xor[i], table_random[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment