]> git.draconx.ca Git - cdecl99.git/blobdiff - test/rng.c
Fix configuration on Solaris 8.
[cdecl99.git] / test / rng.c
index 884d981767f9a09b3becef08f3506540adc516ec..8ae2f1369cb812dff820e368ceab54d0e133ef17 100644 (file)
@@ -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 <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  *
- *  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 <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <inttypes.h>
 #include <errno.h>
 #include <limits.h>
 #include <float.h>
@@ -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 <inttypes.h> */
+#  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;