Compare commits
3 commits
03064b235c
...
a7dfe216fb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7dfe216fb | ||
|
|
c6e84189e6 | ||
|
|
18491a1336 |
3 changed files with 74 additions and 0 deletions
11
lookup3.c
11
lookup3.c
|
|
@ -1,5 +1,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
|
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
|
||||||
|
|
||||||
|
|
@ -61,3 +63,12 @@ uint32_t lookup3(const void *key, size_t length, uint32_t initval) {
|
||||||
final(a, b, c);
|
final(a, b, c);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
char *a = "Hello!\0";
|
||||||
|
int len = strlen(a);
|
||||||
|
|
||||||
|
uint32_t hash = lookup3(a, len, 0);
|
||||||
|
|
||||||
|
printf("%.8X\n", hash);
|
||||||
|
}
|
||||||
|
|
|
||||||
3
prand.h
3
prand.h
|
|
@ -4,6 +4,9 @@
|
||||||
/*
|
/*
|
||||||
* This is a PRNG for non-cryptographic use. May be used on micros given that
|
* This is a PRNG for non-cryptographic use. May be used on micros given that
|
||||||
* the seed is periodically saved to EEPROM or flash.
|
* the seed is periodically saved to EEPROM or flash.
|
||||||
|
*
|
||||||
|
* See:
|
||||||
|
* https://en.wikipedia.org/wiki/Linear_congruential_generator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
60
xorshift.c
Normal file
60
xorshift.c
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
struct xorshift32_state {
|
||||||
|
uint32_t a;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* The state must be initialized to non-zero */
|
||||||
|
uint32_t xorshift32(struct xorshift32_state *state) {
|
||||||
|
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
|
||||||
|
uint32_t x = state->a;
|
||||||
|
x ^= x << 13;
|
||||||
|
x ^= x >> 17;
|
||||||
|
x ^= x << 5;
|
||||||
|
return state->a = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct xorshift64_state {
|
||||||
|
uint64_t a;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* The state must be initialized to non-zero */
|
||||||
|
uint64_t xorshift64(struct xorshift64_state *state) {
|
||||||
|
uint64_t x = state->a;
|
||||||
|
x ^= x << 13;
|
||||||
|
x ^= x >> 7;
|
||||||
|
x ^= x << 17;
|
||||||
|
return state->a = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* struct xorshift128_state can alternatively be defined as a pair
|
||||||
|
of uint64_t or a uint128_t where supported */
|
||||||
|
struct xorshift128_state {
|
||||||
|
uint32_t x[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
/* The state must be initialized to non-zero */
|
||||||
|
uint32_t xorshift128(struct xorshift128_state *state) {
|
||||||
|
/* Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" */
|
||||||
|
uint32_t t = state->x[3];
|
||||||
|
|
||||||
|
uint32_t s = state->x[0]; /* Perform a contrived 32-bit shift. */
|
||||||
|
state->x[3] = state->x[2];
|
||||||
|
state->x[2] = state->x[1];
|
||||||
|
state->x[1] = s;
|
||||||
|
|
||||||
|
t ^= t << 11;
|
||||||
|
t ^= t >> 8;
|
||||||
|
return state->x[0] = t ^ s ^ (s >> 19);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
struct xorshift32_state s = {};
|
||||||
|
s.a = time(0);
|
||||||
|
|
||||||
|
for (int a = 0; a < 10; a++) {
|
||||||
|
printf("Number %d: %d\n", a, xorshift32(&s));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue