diff options
author | Cyril SIX <cyril.six@kalray.eu> | 2018-04-16 17:31:27 +0200 |
---|---|---|
committer | Cyril SIX <cyril.six@kalray.eu> | 2018-04-16 17:31:27 +0200 |
commit | b26424165aa5ae75099792232eca9ea08e09d5a1 (patch) | |
tree | 5412932a7c9dc26831dd8707d54c96d8a7cc2996 /test/mppa/lib/prng.c | |
parent | 0ef341c554aff8d70f879411bb0918fb8349f2e4 (diff) | |
download | compcert-kvx-b26424165aa5ae75099792232eca9ea08e09d5a1.tar.gz compcert-kvx-b26424165aa5ae75099792232eca9ea08e09d5a1.zip |
MPPA - added PRNG generator in the tests
Diffstat (limited to 'test/mppa/lib/prng.c')
-rw-r--r-- | test/mppa/lib/prng.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/test/mppa/lib/prng.c b/test/mppa/lib/prng.c new file mode 100644 index 00000000..5846038c --- /dev/null +++ b/test/mppa/lib/prng.c @@ -0,0 +1,40 @@ +// https://en.wikipedia.org/wiki/Linear_congruential_generator -> MMIX Donald Knuth +// modulo 2^64 = no need to do it explicitly + +#define MULTIPLIER 6364136223846793005LL +#define INCREMENT 1442695040888963407LL + +static unsigned long long current; + +void srand(long long seed){ + seed = current; +} + +unsigned long long randlong(void){ + return (current = MULTIPLIER * current + INCREMENT); +} + +#ifdef __UNIT_TEST__ +char bytewise_sum(unsigned long long to_check){ + char sum = 0; + + for (int i = 0 ; i < 8 ; i++) + sum += (to_check & (unsigned long long)(0xFFULL << i*8)) >> i*8; + + return sum; +} + +int main(void){ + srand(42); + + if (bytewise_sum(0xdeadbeefb00b1355ULL) != 91) + return 1; + + for (int i = 0 ; i < 1000 ; i++) + randlong(); + + unsigned long long last = randlong(); + + return !((unsigned char)bytewise_sum(last) == 251); +} +#endif // __UNIT_TEST__ |