From b26424165aa5ae75099792232eca9ea08e09d5a1 Mon Sep 17 00:00:00 2001 From: Cyril SIX Date: Mon, 16 Apr 2018 17:31:27 +0200 Subject: MPPA - added PRNG generator in the tests --- test/mppa/lib/Makefile | 18 ++++++++++++++++++ test/mppa/lib/prng.c | 40 ++++++++++++++++++++++++++++++++++++++++ test/mppa/lib/prng.h | 8 ++++++++ 3 files changed, 66 insertions(+) create mode 100644 test/mppa/lib/Makefile create mode 100644 test/mppa/lib/prng.c create mode 100644 test/mppa/lib/prng.h (limited to 'test/mppa') diff --git a/test/mppa/lib/Makefile b/test/mppa/lib/Makefile new file mode 100644 index 00000000..10f40f23 --- /dev/null +++ b/test/mppa/lib/Makefile @@ -0,0 +1,18 @@ +prng-test-x86: prng.c + gcc -D__UNIT_TEST__ -O2 -std=c99 $< -o $@ + +.PHONY: +test: test-x86 + +.PHONY: +test-x86: prng-test-x86 + @if ! ./$<; then\ + >&2 echo "ERROR: $< failed";\ + exit;\ + else\ + echo "Test Succeeded";\ + fi + +.PHONY: +clean: + rm -f prng-test-x86 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__ diff --git a/test/mppa/lib/prng.h b/test/mppa/lib/prng.h new file mode 100644 index 00000000..fee8c865 --- /dev/null +++ b/test/mppa/lib/prng.h @@ -0,0 +1,8 @@ +#ifndef __PRNG_H__ +#define __PRNG_H__ + +void srand(long long seed); + +long long randlong(void); + +#endif // __PRNG_H__ -- cgit