X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/88c05279e142ff97db82c30f4f3936b1ea5eb22a..881854b714cb3987911800c88345ea61db40fb03:/test/rng.c?ds=sidebyside diff --git a/test/rng.c b/test/rng.c index 884d981..8ae2f13 100644 --- a/test/rng.c +++ b/test/rng.c @@ -1,29 +1,30 @@ /* - * Simple random number generator for testing. - * Copyright © 2022 Nick Bowler + * Simple random number generator for testing. + * Copyright © 2022 Nick Bowler * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . * - * This implementation is adapted from xoshiro256+ and splitmix64 - * by David Blackman and Sebastiano Vigna, originally distributed - * under the Creative Commons Zero public domain dedication. + * The RNG implementation is adapted from xoshiro256+ and splitmix64 + * by David Blackman and Sebastiano Vigna, originally distributed under + * the Creative Commons Zero public domain dedication. */ #include #include #include #include +#include #include #include #include @@ -45,16 +46,16 @@ static unsigned long long rot_left64(unsigned long long val, int n) static unsigned long long xoshiro256p(unsigned long long *s) { - unsigned long long ret = s[0]; - unsigned long long t; + unsigned long long tmp, ret; - t = B64(s[1] << 17); + ret = B64(s[0] + s[3]); + tmp = B64(s[1] << 17); s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; - s[2] ^= t; + s[2] ^= tmp; s[3] = rot_left64(s[3], 45); return ret; @@ -71,6 +72,19 @@ static unsigned long long splitmix64(unsigned long long *state) return z ^ (z >> 31); } +#if HAVE_STRTOULL +# define STRTOULL strtoull +#elif HAVE___STRTOULL +/* HP-UX 11 has __strtoull in */ +# define STRTOULL __strtoull +#else +/* + * Just fall back to strtoul -- in the worst case we just lose the ability + * to set all 64 bits of the seed. + */ +# define STRTOULL strtoul +#endif + struct test_rng *test_rng_alloc(const char *seed_str) { unsigned long long seed; @@ -78,7 +92,7 @@ struct test_rng *test_rng_alloc(const char *seed_str) char *end; errno = 0; - seed = strtoull(seed_str, &end, 0); + seed = STRTOULL(seed_str, &end, 0); if (*end != 0) { fprintf(stderr, "%s: invalid seed\n", seed_str); return NULL;