From 9e44c888d23a81deea7c95c432e0752e7a6ece2e Mon Sep 17 00:00:00 2001 From: healytpk Date: Wed, 17 Aug 2022 00:30:22 +0100 Subject: [PATCH 1/6] healytpk - First Commit Ater Fork - Can predict MT19937 but still not perfect --- include/Makefile.am | 1 + include/dieharder/diehard_predict_mersenne.h | 25 +++ include/dieharder/tests.h | 1 + include/dieharder/verbose.h | 1 + libdieharder/Makefile.am | 1 + libdieharder/diehard_predict_mersenne.c | 188 +++++++++++++++++++ libdieharder/dieharder_test_types.c | 3 + 7 files changed, 220 insertions(+) create mode 100644 include/dieharder/diehard_predict_mersenne.h create mode 100644 libdieharder/diehard_predict_mersenne.c diff --git a/include/Makefile.am b/include/Makefile.am index f80b4ff..3135821 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -18,6 +18,7 @@ nobase_include_HEADERS = dieharder/copyright.h \ dieharder/diehard_opso.h \ dieharder/diehard_oqso.h \ dieharder/diehard_parking_lot.h \ + dieharder/diehard_predict_mersenne.h \ dieharder/diehard_rank_32x32.h \ dieharder/diehard_rank_6x8.h \ dieharder/diehard_runs.h \ diff --git a/include/dieharder/diehard_predict_mersenne.h b/include/dieharder/diehard_predict_mersenne.h new file mode 100644 index 0000000..c22263d --- /dev/null +++ b/include/dieharder/diehard_predict_mersenne.h @@ -0,0 +1,25 @@ +/* + * diehard_predict_mersenne test header. + */ + +/* + * function prototype + */ +int diehard_predict_mersenne(Test **test, int irun); + +static Dtest diehard_predict_mersenne_dtest __attribute__((unused)) = { + "Diehard Predict Mersenne Test", + "diehard_predict_mersenne", + "Predict future output from the Mersenne Twister RNG", + 1, + 100, + 1, + diehard_predict_mersenne, + 0 +}; + +/* + * Global variables +uint diehard_predict_mersenne_nms,diehard_predict_mersenne_nbits; +uint *diehard_predict_mersenne_rand_uint; + */ diff --git a/include/dieharder/tests.h b/include/dieharder/tests.h index 1674aed..d8d29b8 100644 --- a/include/dieharder/tests.h +++ b/include/dieharder/tests.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include diff --git a/include/dieharder/verbose.h b/include/dieharder/verbose.h index c9ab5c0..21e6827 100644 --- a/include/dieharder/verbose.h +++ b/include/dieharder/verbose.h @@ -23,6 +23,7 @@ D_DIEHARD_COUNT_1S_STREAM, D_DIEHARD_COUNT_1S_BYTE, D_DIEHARD_PARKING_LOT, + D_DIEHARD_PREDICT_MERSENNE, D_DIEHARD_2DSPHERE, D_DIEHARD_3DSPHERE, D_DIEHARD_SQUEEZE, diff --git a/libdieharder/Makefile.am b/libdieharder/Makefile.am index 0873f9c..7817128 100644 --- a/libdieharder/Makefile.am +++ b/libdieharder/Makefile.am @@ -49,6 +49,7 @@ libdieharder_la_SOURCES = \ diehard_opso.c \ diehard_oqso.c \ diehard_parking_lot.c \ + diehard_predict_mersenne.c \ diehard_rank_32x32.c \ diehard_rank_6x8.c \ diehard_runs.c \ diff --git a/libdieharder/diehard_predict_mersenne.c b/libdieharder/diehard_predict_mersenne.c new file mode 100644 index 0000000..8b2ee44 --- /dev/null +++ b/libdieharder/diehard_predict_mersenne.c @@ -0,0 +1,188 @@ +#include + +#include /* uint32_t, uint_fast64_t, uintmax_t */ +#include /* SCNuMAX */ +#include /* printf */ + +#define SAMPLES_NEEDED ((uint32_t)624u) + +static uint32_t const + param_w = 32, /* word size */ + param_m = 397, /* middle term */ + param_r = 31, /* separation point of one word */ + param_a = 0x9908b0df, /* bottom row of matrix A */ + param_u = 11, /* tempering shift */ + param_s = 7, /* tempering shift */ + param_t = 15, /* tempering shift */ + param_l = 18, /* tempering shift */ + param_b = 0x9d2c5680, /* tempering mask */ + param_c = 0xefc60000; /* tempering mask */ + +static uint32_t Undo_Xor_Rshift(uint32_t const x, unsigned const shift) +{ + unsigned shift_amount; + + uint32_t result = x; + + for (shift_amount = shift; shift_amount < param_w; shift_amount += shift) + { + result ^= (x >> shift_amount); + } + + return result; +} + +static uint32_t Undo_Xor_Lshiftmask(uint32_t x, unsigned const shift, uint32_t const mask) +{ + unsigned i; + + uint32_t window = ((uint32_t)1u << shift) - 1u; + + for (i = 0; i < param_w / shift; ++i) + { + x ^= ((window & x) << shift) & mask; + window <<= shift; + } + + return x; +} + +static uint32_t temper(uint32_t x) +{ + x ^= (x >> param_u); + x ^= ((x << param_s) & param_b); + x ^= ((x << param_t) & param_c); + x ^= (x >> param_l); + + return x; +} + +static uint32_t untemper(uint32_t x) +{ + x = Undo_Xor_Rshift(x, param_l); + x = Undo_Xor_Lshiftmask(x, param_t, param_c); + x = Undo_Xor_Lshiftmask(x, param_s, param_b); + x = Undo_Xor_Rshift(x, param_u); + + return x; +} + +static uint32_t upper(uint32_t const x) +{ + return x & 2147483648u; +} + +static uint32_t lower(uint32_t const x) +{ + return x & 2147483647u; +} + +static uint32_t timesA(uint32_t x) +{ + int const is_odd = x & 1u; + + x >>= 1u; + + if ( is_odd ) + { + x ^= param_a; + } + + return x; +} + +static unsigned CircularAdvance(unsigned const offset, unsigned const advance) +{ + return (offset + advance) % SAMPLES_NEEDED; +} + +#define printf(...) /* nothing */ + +int diehard_predict_mersenne(Test **test, int irun) +{ + /* + * for display only. 0 means "ignored". + */ + test[0]->ntuple = 0; + + uint32_t *const samples_seen_so_far = malloc(SAMPLES_NEEDED * sizeof(*samples_seen_so_far)); + + unsigned num_correct = 0, num_incorrect = 0; + + uint_fast64_t count_samples; + + unsigned i; + + printf("Waiting for %u previous inputs\n", (unsigned)SAMPLES_NEEDED); + + //fread(samples_seen_so_far, sizeof *samples_seen_so_far, sizeof(samples_seen_so_far)/sizeof(*samples_seen_so_far), stdin); + + //memset(samples_seen_so_far,0,sizeof samples_seen_so_far); + + for( unsigned i = 0; i != SAMPLES_NEEDED; ++i ) + { + uint32_t tmp; + get_rand_bits(&tmp,sizeof(uint32_t),32u,rng); + samples_seen_so_far[i] = untemper(tmp); + } + + puts("Ready to predict"); + + printf("Third-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 3u]); + printf("Second-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 2u]); + printf("Last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 1u]); + + for ( count_samples = 0; count_samples < (1000u - SAMPLES_NEEDED); ++count_samples ) + { + unsigned const offset = count_samples % SAMPLES_NEEDED; + + char const *status = 0; + + uint32_t predicted, actual; + + uint32_t const alpha = samples_seen_so_far[ CircularAdvance(offset,param_m) ]; + + printf("Alpha == %" SCNuMAX "\n", (uintmax_t)alpha); + printf("Seen[0] == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[0]); + printf("Seen[1] == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[1]); + printf("upper(Seen[0]) == %" SCNuMAX "\n", (uintmax_t)upper(samples_seen_so_far[0])); + printf("lower(Seen[1]) == %" SCNuMAX "\n", (uintmax_t)lower(samples_seen_so_far[1])); + + uint32_t const prebeta = upper(samples_seen_so_far[offset]) + | lower(samples_seen_so_far[CircularAdvance(offset,1u)]); + + printf("Prebeta == %" SCNuMAX "\n", (uintmax_t)prebeta); + + uint32_t const beta = timesA(prebeta); + + printf("Beta == %" SCNuMAX "\n", (uintmax_t)beta); + + uint32_t const next_val = alpha ^ beta; + + samples_seen_so_far[offset] = next_val; + + printf("Next_val == %" SCNuMAX "\n", (uintmax_t)next_val); + + predicted = temper(next_val); + + get_rand_bits(&actual,sizeof(uint32_t),32u,rng); + + if ( predicted == actual ) + { + ++num_correct; + } + else + { + ++num_incorrect; + } + + printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", (uintmax_t)count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); + } + + printf("Correct = %d, Incorrect = %d\n", num_correct, num_incorrect); + test[0]->pvalues[irun] = (0 != num_correct) ? 0.0 : 0.2; + + free(samples_seen_so_far); + + return 0; +} diff --git a/libdieharder/dieharder_test_types.c b/libdieharder/dieharder_test_types.c index 40d731e..eeb36d6 100644 --- a/libdieharder/dieharder_test_types.c +++ b/libdieharder/dieharder_test_types.c @@ -97,6 +97,9 @@ void dieharder_test_types() ADD_TEST(&marsaglia_tsang_gcd_dtest); dh_num_diehard_tests++; + ADD_TEST(&diehard_predict_mersenne_dtest); + dh_num_diehard_tests++; + MYDEBUG(D_TYPES){ printf("# dieharder_test_types(): Found %u diehard tests.\n",dh_num_diehard_tests); } From f9c8d8e268b697a8d92976992e7ae731b610564e Mon Sep 17 00:00:00 2001 From: healytpk Date: Wed, 17 Aug 2022 15:19:11 +0100 Subject: [PATCH 2/6] healytpk - RNG deemed to fail if at least 3 predictions are correct --- include/dieharder/diehard_predict_mersenne.h | 13 ++------ libdieharder/diehard_predict_mersenne.c | 33 +++++++++++--------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/include/dieharder/diehard_predict_mersenne.h b/include/dieharder/diehard_predict_mersenne.h index c22263d..c560a79 100644 --- a/include/dieharder/diehard_predict_mersenne.h +++ b/include/dieharder/diehard_predict_mersenne.h @@ -2,24 +2,15 @@ * diehard_predict_mersenne test header. */ -/* - * function prototype - */ -int diehard_predict_mersenne(Test **test, int irun); +extern int diehard_predict_mersenne(Test **test, int irun); // defined in diehard_predict_mersenne.c static Dtest diehard_predict_mersenne_dtest __attribute__((unused)) = { "Diehard Predict Mersenne Test", "diehard_predict_mersenne", - "Predict future output from the Mersenne Twister RNG", + "Predict future output from the 32-Bit MT19937 Mersenne Twister RNG", 1, 100, 1, diehard_predict_mersenne, 0 }; - -/* - * Global variables -uint diehard_predict_mersenne_nms,diehard_predict_mersenne_nbits; -uint *diehard_predict_mersenne_rand_uint; - */ diff --git a/libdieharder/diehard_predict_mersenne.c b/libdieharder/diehard_predict_mersenne.c index 8b2ee44..068123f 100644 --- a/libdieharder/diehard_predict_mersenne.c +++ b/libdieharder/diehard_predict_mersenne.c @@ -97,6 +97,7 @@ static unsigned CircularAdvance(unsigned const offset, unsigned const advance) } #define printf(...) /* nothing */ +#define always_printf(...) printf(__VA_ARGS__) int diehard_predict_mersenne(Test **test, int irun) { @@ -106,18 +107,10 @@ int diehard_predict_mersenne(Test **test, int irun) test[0]->ntuple = 0; uint32_t *const samples_seen_so_far = malloc(SAMPLES_NEEDED * sizeof(*samples_seen_so_far)); - + unsigned num_correct = 0, num_incorrect = 0; - - uint_fast64_t count_samples; - - unsigned i; - - printf("Waiting for %u previous inputs\n", (unsigned)SAMPLES_NEEDED); - - //fread(samples_seen_so_far, sizeof *samples_seen_so_far, sizeof(samples_seen_so_far)/sizeof(*samples_seen_so_far), stdin); - //memset(samples_seen_so_far,0,sizeof samples_seen_so_far); + printf("Waiting for %u previous inputs\n", (unsigned)SAMPLES_NEEDED); for( unsigned i = 0; i != SAMPLES_NEEDED; ++i ) { @@ -126,18 +119,18 @@ int diehard_predict_mersenne(Test **test, int irun) samples_seen_so_far[i] = untemper(tmp); } - puts("Ready to predict"); + printf("Ready to predict\n"); printf("Third-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 3u]); printf("Second-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 2u]); printf("Last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 1u]); - for ( count_samples = 0; count_samples < (1000u - SAMPLES_NEEDED); ++count_samples ) + for ( unsigned count_samples = 0; count_samples < (1000 - SAMPLES_NEEDED); ++count_samples ) { unsigned const offset = count_samples % SAMPLES_NEEDED; char const *status = 0; - + uint32_t predicted, actual; uint32_t const alpha = samples_seen_so_far[ CircularAdvance(offset,param_m) ]; @@ -169,18 +162,28 @@ int diehard_predict_mersenne(Test **test, int irun) if ( predicted == actual ) { + status = "CORRECT"; ++num_correct; } else { + status = "INACCURATE"; ++num_incorrect; } printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", (uintmax_t)count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); } - printf("Correct = %d, Incorrect = %d\n", num_correct, num_incorrect); - test[0]->pvalues[irun] = (0 != num_correct) ? 0.0 : 0.2; + always_printf("Correct = %d, Incorrect = %d\n", num_correct, num_incorrect); + + if ( !(num_correct > 3u) ) + { + test[0]->pvalues[irun] = 0.2; + } + else + { + test[0]->pvalues[irun] = 0.0; + } free(samples_seen_so_far); From 771bbebd27860397221acba08982c4f5a37cd61f Mon Sep 17 00:00:00 2001 From: healytpk Date: Wed, 17 Aug 2022 15:20:23 +0100 Subject: [PATCH 3/6] New files .gitignore and restore.sh to ignore certain files --- .gitignore | 18 ++++++++++++++++++ restore.sh | 2 ++ 2 files changed, 20 insertions(+) create mode 100644 .gitignore create mode 100755 restore.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7463a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +*.o +*.lo +/config.* +/autom4te.cache/ +/dieharder.html +/dieharder.spec +/dieharder-config +/dieharder/.*/ +/libdieharder/.*/ +/dieharder/Makefile +/dieharder/dieharder +/dieharder_version.h +/Makefile +/include/Makefile +/libdieharder/Makefile +/libdieharder/libdieharder.la +/libtool +/stamp-h1 diff --git a/restore.sh b/restore.sh new file mode 100755 index 0000000..b52c3f4 --- /dev/null +++ b/restore.sh @@ -0,0 +1,2 @@ +find -name Makefile\.in | xargs -i -r -n1 git restore "{}" +git restore ./manual/Makefile ./aclocal.m4 ./configure From efe56d830f26e428d223da6feeb6d7b622c46a52 Mon Sep 17 00:00:00 2001 From: healytpk Date: Wed, 17 Aug 2022 15:31:12 +0100 Subject: [PATCH 4/6] P value is returned as 0.0 if more than 3 predicitons are correct --- libdieharder/diehard_predict_mersenne.c | 40 ++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/libdieharder/diehard_predict_mersenne.c b/libdieharder/diehard_predict_mersenne.c index 068123f..9eccb58 100644 --- a/libdieharder/diehard_predict_mersenne.c +++ b/libdieharder/diehard_predict_mersenne.c @@ -96,8 +96,8 @@ static unsigned CircularAdvance(unsigned const offset, unsigned const advance) return (offset + advance) % SAMPLES_NEEDED; } -#define printf(...) /* nothing */ -#define always_printf(...) printf(__VA_ARGS__) +//#define verbose_printf(...) printf(__VA_ARGS__) +#define verbose_printf(...) /* nothing */ int diehard_predict_mersenne(Test **test, int irun) { @@ -110,7 +110,7 @@ int diehard_predict_mersenne(Test **test, int irun) unsigned num_correct = 0, num_incorrect = 0; - printf("Waiting for %u previous inputs\n", (unsigned)SAMPLES_NEEDED); + verbose_printf("Waiting for %u previous inputs\n", (unsigned)SAMPLES_NEEDED); for( unsigned i = 0; i != SAMPLES_NEEDED; ++i ) { @@ -119,11 +119,11 @@ int diehard_predict_mersenne(Test **test, int irun) samples_seen_so_far[i] = untemper(tmp); } - printf("Ready to predict\n"); + verbose_printf("Ready to predict\n"); - printf("Third-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 3u]); - printf("Second-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 2u]); - printf("Last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 1u]); + verbose_printf("Third-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 3u]); + verbose_printf("Second-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 2u]); + verbose_printf("Last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 1u]); for ( unsigned count_samples = 0; count_samples < (1000 - SAMPLES_NEEDED); ++count_samples ) { @@ -135,26 +135,26 @@ int diehard_predict_mersenne(Test **test, int irun) uint32_t const alpha = samples_seen_so_far[ CircularAdvance(offset,param_m) ]; - printf("Alpha == %" SCNuMAX "\n", (uintmax_t)alpha); - printf("Seen[0] == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[0]); - printf("Seen[1] == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[1]); - printf("upper(Seen[0]) == %" SCNuMAX "\n", (uintmax_t)upper(samples_seen_so_far[0])); - printf("lower(Seen[1]) == %" SCNuMAX "\n", (uintmax_t)lower(samples_seen_so_far[1])); + verbose_printf("Alpha == %" SCNuMAX "\n", (uintmax_t)alpha); + verbose_printf("Seen[0] == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[0]); + verbose_printf("Seen[1] == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[1]); + verbose_printf("upper(Seen[0]) == %" SCNuMAX "\n", (uintmax_t)upper(samples_seen_so_far[0])); + verbose_printf("lower(Seen[1]) == %" SCNuMAX "\n", (uintmax_t)lower(samples_seen_so_far[1])); uint32_t const prebeta = upper(samples_seen_so_far[offset]) | lower(samples_seen_so_far[CircularAdvance(offset,1u)]); - printf("Prebeta == %" SCNuMAX "\n", (uintmax_t)prebeta); + verbose_printf("Prebeta == %" SCNuMAX "\n", (uintmax_t)prebeta); uint32_t const beta = timesA(prebeta); - printf("Beta == %" SCNuMAX "\n", (uintmax_t)beta); + verbose_printf("Beta == %" SCNuMAX "\n", (uintmax_t)beta); uint32_t const next_val = alpha ^ beta; samples_seen_so_far[offset] = next_val; - printf("Next_val == %" SCNuMAX "\n", (uintmax_t)next_val); + verbose_printf("Next_val == %" SCNuMAX "\n", (uintmax_t)next_val); predicted = temper(next_val); @@ -171,18 +171,18 @@ int diehard_predict_mersenne(Test **test, int irun) ++num_incorrect; } - printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", (uintmax_t)count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); + verbose_printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", (uintmax_t)count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); } - always_printf("Correct = %d, Incorrect = %d\n", num_correct, num_incorrect); + printf("Correct = %" SCNuMAX ", Incorrect = %" SCNuMAX "\n", (uintmax_t)num_correct, (uintmax_t)num_incorrect); - if ( !(num_correct > 3u) ) + if ( num_correct > 3u ) { - test[0]->pvalues[irun] = 0.2; + test[0]->pvalues[irun] = 0.0; } else { - test[0]->pvalues[irun] = 0.0; + test[0]->pvalues[irun] = 0.2; } free(samples_seen_so_far); From 21adae10bebb0fdc41862c5cd84e00b2f4164217 Mon Sep 17 00:00:00 2001 From: healytpk Date: Wed, 17 Aug 2022 16:26:43 +0100 Subject: [PATCH 5/6] HT: Default to 1248 samples, PASS if 0.07 of them are predicted correctly --- include/dieharder/diehard_predict_mersenne.h | 16 +++--- libdieharder/diehard_predict_mersenne.c | 56 +++++++++++--------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/include/dieharder/diehard_predict_mersenne.h b/include/dieharder/diehard_predict_mersenne.h index c560a79..7da3113 100644 --- a/include/dieharder/diehard_predict_mersenne.h +++ b/include/dieharder/diehard_predict_mersenne.h @@ -5,12 +5,12 @@ extern int diehard_predict_mersenne(Test **test, int irun); // defined in diehard_predict_mersenne.c static Dtest diehard_predict_mersenne_dtest __attribute__((unused)) = { - "Diehard Predict Mersenne Test", - "diehard_predict_mersenne", - "Predict future output from the 32-Bit MT19937 Mersenne Twister RNG", - 1, - 100, - 1, - diehard_predict_mersenne, - 0 + /* The name of the test */ "Diehard Predict Mersenne Test", + /* The call name */ "diehard_predict_mersenne", + /* Text description */ "Predict future output from the 32-Bit MT19937 Mersenne Twister RNG", + /* default psamples */ 1u, + /* default tsamples */ 1248u, // This is 2 * 624 + /* Independent statistics generated per run */ 1u, + /* pointer to function */ diehard_predict_mersenne, + /* pointer to a vector of additional test arguments */ 0 }; diff --git a/libdieharder/diehard_predict_mersenne.c b/libdieharder/diehard_predict_mersenne.c index 9eccb58..bc04b5e 100644 --- a/libdieharder/diehard_predict_mersenne.c +++ b/libdieharder/diehard_predict_mersenne.c @@ -1,10 +1,10 @@ #include -#include /* uint32_t, uint_fast64_t, uintmax_t */ +#include /* uint32_t, uintmax_t */ #include /* SCNuMAX */ #include /* printf */ -#define SAMPLES_NEEDED ((uint32_t)624u) +#define PREDICT_MERSENNE_SAMPLES_NEEDED ((uint32_t)624u) static uint32_t const param_w = 32, /* word size */ @@ -21,14 +21,14 @@ static uint32_t const static uint32_t Undo_Xor_Rshift(uint32_t const x, unsigned const shift) { unsigned shift_amount; - + uint32_t result = x; for (shift_amount = shift; shift_amount < param_w; shift_amount += shift) { result ^= (x >> shift_amount); } - + return result; } @@ -37,13 +37,13 @@ static uint32_t Undo_Xor_Lshiftmask(uint32_t x, unsigned const shift, uint32_t c unsigned i; uint32_t window = ((uint32_t)1u << shift) - 1u; - + for (i = 0; i < param_w / shift; ++i) { x ^= ((window & x) << shift) & mask; window <<= shift; } - + return x; } @@ -80,9 +80,9 @@ static uint32_t lower(uint32_t const x) static uint32_t timesA(uint32_t x) { int const is_odd = x & 1u; - + x >>= 1u; - + if ( is_odd ) { x ^= param_a; @@ -93,26 +93,30 @@ static uint32_t timesA(uint32_t x) static unsigned CircularAdvance(unsigned const offset, unsigned const advance) { - return (offset + advance) % SAMPLES_NEEDED; + return (offset + advance) % PREDICT_MERSENNE_SAMPLES_NEEDED; } //#define verbose_printf(...) printf(__VA_ARGS__) #define verbose_printf(...) /* nothing */ +//define printf(...) /* nothing */ int diehard_predict_mersenne(Test **test, int irun) { - /* - * for display only. 0 means "ignored". - */ - test[0]->ntuple = 0; + test[0]->ntuple = 0; // 0 means "ignored" + test[0]->pvalues[irun] = 0.0; // 0.0 is a P-value for failure + + // Don't bother running the test if we don't have at least 8 test numbers + if ( test[0]->tsamples < (2*PREDICT_MERSENNE_SAMPLES_NEEDED) ) return 0; - uint32_t *const samples_seen_so_far = malloc(SAMPLES_NEEDED * sizeof(*samples_seen_so_far)); + uint32_t *const samples_seen_so_far = malloc(PREDICT_MERSENNE_SAMPLES_NEEDED * sizeof(*samples_seen_so_far)); - unsigned num_correct = 0, num_incorrect = 0; + if ( !samples_seen_so_far ) return 0; // if dynamic memory allocation error - verbose_printf("Waiting for %u previous inputs\n", (unsigned)SAMPLES_NEEDED); + uintmax_t num_correct = 0, num_incorrect = 0; - for( unsigned i = 0; i != SAMPLES_NEEDED; ++i ) + verbose_printf("Waiting for %" SCNuMAX " previous inputs\n", (uintmax_t)PREDICT_MERSENNE_SAMPLES_NEEDED); + + for( uintmax_t i = 0; i < PREDICT_MERSENNE_SAMPLES_NEEDED; ++i ) { uint32_t tmp; get_rand_bits(&tmp,sizeof(uint32_t),32u,rng); @@ -120,14 +124,14 @@ int diehard_predict_mersenne(Test **test, int irun) } verbose_printf("Ready to predict\n"); - - verbose_printf("Third-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 3u]); - verbose_printf("Second-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 2u]); - verbose_printf("Last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[SAMPLES_NEEDED - 1u]); - for ( unsigned count_samples = 0; count_samples < (1000 - SAMPLES_NEEDED); ++count_samples ) + verbose_printf("Third-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[PREDICT_MERSENNE_SAMPLES_NEEDED - 3u]); + verbose_printf("Second-last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[PREDICT_MERSENNE_SAMPLES_NEEDED - 2u]); + verbose_printf("Last element == %" SCNuMAX "\n", (uintmax_t)samples_seen_so_far[PREDICT_MERSENNE_SAMPLES_NEEDED - 1u]); + + for ( uintmax_t count_samples = 0; count_samples < (test[0]->tsamples - PREDICT_MERSENNE_SAMPLES_NEEDED); ++count_samples ) { - unsigned const offset = count_samples % SAMPLES_NEEDED; + uintmax_t const offset = count_samples % PREDICT_MERSENNE_SAMPLES_NEEDED; char const *status = 0; @@ -171,12 +175,12 @@ int diehard_predict_mersenne(Test **test, int irun) ++num_incorrect; } - verbose_printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", (uintmax_t)count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); + verbose_printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); } - printf("Correct = %" SCNuMAX ", Incorrect = %" SCNuMAX "\n", (uintmax_t)num_correct, (uintmax_t)num_incorrect); + printf("Correct = %" SCNuMAX ", Incorrect = %" SCNuMAX "\n", num_correct, num_incorrect); - if ( num_correct > 3u ) + if ( ((long double)num_correct / num_incorrect) > 0.007L ) { test[0]->pvalues[irun] = 0.0; } From f472d0308035ee5ef7e986e4bb89bf4d8c09494f Mon Sep 17 00:00:00 2001 From: "T. P. K. Healy" <57729245+healytpk@users.noreply.github.com> Date: Sun, 11 Sep 2022 22:37:06 +0100 Subject: [PATCH 6/6] Change printf to verbose_printf before submitting pull request --- libdieharder/diehard_predict_mersenne.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libdieharder/diehard_predict_mersenne.c b/libdieharder/diehard_predict_mersenne.c index bc04b5e..38f99d1 100644 --- a/libdieharder/diehard_predict_mersenne.c +++ b/libdieharder/diehard_predict_mersenne.c @@ -178,7 +178,7 @@ int diehard_predict_mersenne(Test **test, int irun) verbose_printf("Sample Number: %" SCNuMAX " - Predicted %" SCNuMAX " got %" SCNuMAX " -- %s\n", count_samples, (uintmax_t)predicted, (uintmax_t)actual, status); } - printf("Correct = %" SCNuMAX ", Incorrect = %" SCNuMAX "\n", num_correct, num_incorrect); + verbose_printf("Correct = %" SCNuMAX ", Incorrect = %" SCNuMAX "\n", num_correct, num_incorrect); if ( ((long double)num_correct / num_incorrect) > 0.007L ) {