From 96b1761ed163309e5757e5d0e84cdb658cc8aae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 26 Apr 2020 18:30:41 +0200 Subject: [PATCH 01/26] mv ocaml src --- {ocaml => src}/.gitignore | 0 {ocaml => src}/Makefile | 0 {ocaml => src}/bench.ml | 0 {ocaml => src}/dune | 0 {ocaml => src}/dune-project | 0 {ocaml => src}/make-random/bench.ml | 0 {ocaml => src}/make-random/dune | 0 {ocaml => src}/make-random/makeRandom.ml | 0 {ocaml => src}/make-random/test.ml | 0 {ocaml => src}/splitmix/dune | 0 {ocaml => src}/splitmix/sm64.ml | 0 {ocaml => src}/tests/dune | 0 {ocaml => src}/tests/ocamlrandom.ml | 0 {ocaml => src}/tests/splitmix.ml | 0 {ocaml => src}/tests/xoshiro.ml | 0 {ocaml => src}/xoshiro/dune | 0 {ocaml => src}/xoshiro/x256pp.ml | 0 {ocaml => src}/xoshiro/x256pp_c/x256pp.c | 0 {ocaml => src}/xoshiro/x256pp_c/x256pp.h | 0 19 files changed, 0 insertions(+), 0 deletions(-) rename {ocaml => src}/.gitignore (100%) rename {ocaml => src}/Makefile (100%) rename {ocaml => src}/bench.ml (100%) rename {ocaml => src}/dune (100%) rename {ocaml => src}/dune-project (100%) rename {ocaml => src}/make-random/bench.ml (100%) rename {ocaml => src}/make-random/dune (100%) rename {ocaml => src}/make-random/makeRandom.ml (100%) rename {ocaml => src}/make-random/test.ml (100%) rename {ocaml => src}/splitmix/dune (100%) rename {ocaml => src}/splitmix/sm64.ml (100%) rename {ocaml => src}/tests/dune (100%) rename {ocaml => src}/tests/ocamlrandom.ml (100%) rename {ocaml => src}/tests/splitmix.ml (100%) rename {ocaml => src}/tests/xoshiro.ml (100%) rename {ocaml => src}/xoshiro/dune (100%) rename {ocaml => src}/xoshiro/x256pp.ml (100%) rename {ocaml => src}/xoshiro/x256pp_c/x256pp.c (100%) rename {ocaml => src}/xoshiro/x256pp_c/x256pp.h (100%) diff --git a/ocaml/.gitignore b/src/.gitignore similarity index 100% rename from ocaml/.gitignore rename to src/.gitignore diff --git a/ocaml/Makefile b/src/Makefile similarity index 100% rename from ocaml/Makefile rename to src/Makefile diff --git a/ocaml/bench.ml b/src/bench.ml similarity index 100% rename from ocaml/bench.ml rename to src/bench.ml diff --git a/ocaml/dune b/src/dune similarity index 100% rename from ocaml/dune rename to src/dune diff --git a/ocaml/dune-project b/src/dune-project similarity index 100% rename from ocaml/dune-project rename to src/dune-project diff --git a/ocaml/make-random/bench.ml b/src/make-random/bench.ml similarity index 100% rename from ocaml/make-random/bench.ml rename to src/make-random/bench.ml diff --git a/ocaml/make-random/dune b/src/make-random/dune similarity index 100% rename from ocaml/make-random/dune rename to src/make-random/dune diff --git a/ocaml/make-random/makeRandom.ml b/src/make-random/makeRandom.ml similarity index 100% rename from ocaml/make-random/makeRandom.ml rename to src/make-random/makeRandom.ml diff --git a/ocaml/make-random/test.ml b/src/make-random/test.ml similarity index 100% rename from ocaml/make-random/test.ml rename to src/make-random/test.ml diff --git a/ocaml/splitmix/dune b/src/splitmix/dune similarity index 100% rename from ocaml/splitmix/dune rename to src/splitmix/dune diff --git a/ocaml/splitmix/sm64.ml b/src/splitmix/sm64.ml similarity index 100% rename from ocaml/splitmix/sm64.ml rename to src/splitmix/sm64.ml diff --git a/ocaml/tests/dune b/src/tests/dune similarity index 100% rename from ocaml/tests/dune rename to src/tests/dune diff --git a/ocaml/tests/ocamlrandom.ml b/src/tests/ocamlrandom.ml similarity index 100% rename from ocaml/tests/ocamlrandom.ml rename to src/tests/ocamlrandom.ml diff --git a/ocaml/tests/splitmix.ml b/src/tests/splitmix.ml similarity index 100% rename from ocaml/tests/splitmix.ml rename to src/tests/splitmix.ml diff --git a/ocaml/tests/xoshiro.ml b/src/tests/xoshiro.ml similarity index 100% rename from ocaml/tests/xoshiro.ml rename to src/tests/xoshiro.ml diff --git a/ocaml/xoshiro/dune b/src/xoshiro/dune similarity index 100% rename from ocaml/xoshiro/dune rename to src/xoshiro/dune diff --git a/ocaml/xoshiro/x256pp.ml b/src/xoshiro/x256pp.ml similarity index 100% rename from ocaml/xoshiro/x256pp.ml rename to src/xoshiro/x256pp.ml diff --git a/ocaml/xoshiro/x256pp_c/x256pp.c b/src/xoshiro/x256pp_c/x256pp.c similarity index 100% rename from ocaml/xoshiro/x256pp_c/x256pp.c rename to src/xoshiro/x256pp_c/x256pp.c diff --git a/ocaml/xoshiro/x256pp_c/x256pp.h b/src/xoshiro/x256pp_c/x256pp.h similarity index 100% rename from ocaml/xoshiro/x256pp_c/x256pp.h rename to src/xoshiro/x256pp_c/x256pp.h From 4aa7f55647ef12e37bb24476b1f3641a3310cae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 26 Apr 2020 19:05:30 +0200 Subject: [PATCH 02/26] Sm64 C reference implementation as an ocaml module --- c/Makefile | 3 --- c/splitmix64.c | 26 -------------------------- c/splitmix64.h | 3 --- c/testsplitmix.c | 16 ---------------- compare.sh | 17 ----------------- src/splitmix/dune | 1 + src/splitmix/reference.c | 13 +++++++++++++ src/splitmix/reference.h | 10 ++++++++++ src/splitmix/reference.ml | 2 ++ src/splitmix/stubs.c | 23 +++++++++++++++++++++++ 10 files changed, 49 insertions(+), 65 deletions(-) delete mode 100644 c/splitmix64.c delete mode 100644 c/splitmix64.h delete mode 100644 c/testsplitmix.c delete mode 100755 compare.sh create mode 100644 src/splitmix/reference.c create mode 100644 src/splitmix/reference.h create mode 100644 src/splitmix/reference.ml create mode 100644 src/splitmix/stubs.c diff --git a/c/Makefile b/c/Makefile index 691c63f..ef136ec 100644 --- a/c/Makefile +++ b/c/Makefile @@ -1,8 +1,5 @@ all: testxoshiro testocamlrandom testsplitmix -testsplitmix: testsplitmix.c splitmix64.c splitmix64.h - cc -o $@ $< splitmix64.c - testxoshiro: testxoshiro.c xoshiro256pp.c xoshiro256pp.h cc -o $@ $< xoshiro256pp.c diff --git a/c/splitmix64.c b/c/splitmix64.c deleted file mode 100644 index 6dcbad8..0000000 --- a/c/splitmix64.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Written in 2015 by Sebastiano Vigna (vigna@acm.org) - -To the extent possible under law, the author has dedicated all copyright -and related and neighboring rights to this software to the public domain -worldwide. This software is distributed without any warranty. - -See . */ - -#include - -/* This is a fixed-increment version of Java 8's SplittableRandom generator - See http://dx.doi.org/10.1145/2714064.2660195 and - http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html - - It is a very fast generator passing BigCrush, and it can be useful if - for some reason you absolutely want 64 bits of state. */ - -static uint64_t x = 0xdead42beef37ca7a; /* The state can be seeded with any value. */ - -uint64_t next() { - uint64_t z = (x += 0x9e3779b97f4a7c15); - z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; - z = (z ^ (z >> 27)) * 0x94d049bb133111eb; - return z ^ (z >> 31); -} - diff --git a/c/splitmix64.h b/c/splitmix64.h deleted file mode 100644 index 9b48294..0000000 --- a/c/splitmix64.h +++ /dev/null @@ -1,3 +0,0 @@ -#include - -uint64_t next(); diff --git a/c/testsplitmix.c b/c/testsplitmix.c deleted file mode 100644 index 2aa6e34..0000000 --- a/c/testsplitmix.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include -#include "splitmix64.h" - - -int main(int argc, char* argv[]) { - int n = 10; - if (argc == 2) n = atoi(argv[1]); - - for (int i = 0; i < n; i++) - printf("%016lx\n", next()); - - return 0; -} - diff --git a/compare.sh b/compare.sh deleted file mode 100755 index 4904341..0000000 --- a/compare.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh - -set -e - -compare () { - echo "Comparing '$1' and '$2'..." - tmp="$(mktemp)" - "$1" 1000 > "$tmp" - test -z "$("$2" 1000 | diff "$tmp" -)" - rm -f "$tmp" -} - -make -C ocaml tests -make -C c all - -compare ocaml/_build/default/tests/splitmix.exe c/testsplitmix -echo OK! diff --git a/src/splitmix/dune b/src/splitmix/dune index 72ad3ae..1852a11 100644 --- a/src/splitmix/dune +++ b/src/splitmix/dune @@ -1,3 +1,4 @@ (library (name splitmix) + (foreign_stubs (language c) (names reference stubs)) (libraries makeRandom)) diff --git a/src/splitmix/reference.c b/src/splitmix/reference.c new file mode 100644 index 0000000..d653006 --- /dev/null +++ b/src/splitmix/reference.c @@ -0,0 +1,13 @@ +// Reference: http://prng.di.unimi.it/splitmix64.c + +#include + +uint64_t sm64_state; /* The state can be seeded with any value. */ + +uint64_t sm64_next() { + uint64_t z = (sm64_state += 0x9e3779b97f4a7c15); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); +} + diff --git a/src/splitmix/reference.h b/src/splitmix/reference.h new file mode 100644 index 0000000..aef2394 --- /dev/null +++ b/src/splitmix/reference.h @@ -0,0 +1,10 @@ +#ifndef SPLITMIX64_H +#define SPLITMIX64_H + +#include + +uint64_t sm64_state; +uint64_t sm64_next(); + +#endif + diff --git a/src/splitmix/reference.ml b/src/splitmix/reference.ml new file mode 100644 index 0000000..79543d1 --- /dev/null +++ b/src/splitmix/reference.ml @@ -0,0 +1,2 @@ +external seed: int64 -> unit = "caml_sm64_seed" +external next: unit -> int64 = "caml_sm64_next" diff --git a/src/splitmix/stubs.c b/src/splitmix/stubs.c new file mode 100644 index 0000000..daf31ed --- /dev/null +++ b/src/splitmix/stubs.c @@ -0,0 +1,23 @@ +#define CAML_NAME_SPACE +#include +#include +#include +#include +#include "reference.h" + +CAMLprim value caml_sm64_seed(value seed) { + CAMLparam1(seed); + int64_t x = Int64_val(seed); + // reinterpret x as an unsigned integer + sm64_state = *(uint64_t*)(&x); + CAMLreturn(Val_unit); +} + +CAMLprim value caml_sm64_next(value unit) { + CAMLparam1(unit); + uint64_t x = sm64_next(); + // reinterpret x as a signed integer + value res = caml_copy_int64(*(int64_t*)(&x)); + CAMLreturn(res); +} + From 485e691de246dabe67e2058264ac44cb876254e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 27 Apr 2020 00:39:19 +0200 Subject: [PATCH 03/26] Test Sm64 against reference implementation --- src/splitmix/dune | 8 +++++++- src/splitmix/tests.ml | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/splitmix/tests.ml diff --git a/src/splitmix/dune b/src/splitmix/dune index 1852a11..62a2772 100644 --- a/src/splitmix/dune +++ b/src/splitmix/dune @@ -1,4 +1,10 @@ (library (name splitmix) (foreign_stubs (language c) (names reference stubs)) - (libraries makeRandom)) + (libraries makeRandom) + (modules :standard \ Tests)) + +(test + (name tests) + (modules Tests) + (libraries splitmix)) diff --git a/src/splitmix/tests.ml b/src/splitmix/tests.ml new file mode 100644 index 0000000..197f7b0 --- /dev/null +++ b/src/splitmix/tests.ml @@ -0,0 +1,18 @@ +open Splitmix + +let test_compare nb f g = + for _ = 1 to nb do + assert (f () = g ()) + done + +let () = + (* Chosen by fair dice rool. Guaranted to be random. *) + (* https://xkcd.com/221/ *) + let seed = 0xdd6d0e2e849e51adL in + let ocaml_state = ref seed in + + Reference.seed seed; + test_compare + 1_000_000 + Reference.next + (fun () -> Sm64.next ocaml_state) From 8f44bdde72efa149ba24dc919f8fcf1351ba8bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 27 Apr 2020 23:48:09 +0200 Subject: [PATCH 04/26] xoshiro256++ ref implementation as an OCaml module --- c/xoshiro256pp.c | 50 --------------------- src/xoshiro/dune | 2 +- src/xoshiro/reference.c | 45 +++++++++++++++++++ c/xoshiro256pp.h => src/xoshiro/reference.h | 4 +- src/xoshiro/reference.ml | 2 + src/xoshiro/stubs.c | 23 ++++++++++ 6 files changed, 74 insertions(+), 52 deletions(-) delete mode 100644 c/xoshiro256pp.c create mode 100644 src/xoshiro/reference.c rename c/xoshiro256pp.h => src/xoshiro/reference.h (50%) create mode 100644 src/xoshiro/reference.ml create mode 100644 src/xoshiro/stubs.c diff --git a/c/xoshiro256pp.c b/c/xoshiro256pp.c deleted file mode 100644 index 30685eb..0000000 --- a/c/xoshiro256pp.c +++ /dev/null @@ -1,50 +0,0 @@ -/* Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org) - -To the extent possible under law, the author has dedicated all copyright -and related and neighboring rights to this software to the public domain -worldwide. This software is distributed without any warranty. - -See . */ - -#include -#include - -/* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. - It has excellent (sub-ns) speed, a state (256 bits) that is large - enough for any parallel application, and it passes all tests we are - aware of. - - For generating just floating-point numbers, xoshiro256+ is even faster. - - The state must be seeded so that it is not everywhere zero. If you have - a 64-bit seed, we suggest to seed a splitmix64 generator and use its - output to fill s. */ - -static inline uint64_t rotl(const uint64_t x, int k) { - return (x << k) | (x >> (64 - k)); -} - - -static uint64_t s[4] = { - 0xdeadbeefdeadbeef, - 0x4242424242424242, - 0x3737373737373737, - 0xca7aca7aca7aca7a -}; - -uint64_t next(void) { - const uint64_t result = rotl(s[0] + s[3], 23) + s[0]; - - const uint64_t t = s[1] << 17; - - s[2] ^= s[0]; - s[3] ^= s[1]; - s[1] ^= s[2]; - s[0] ^= s[3]; - - s[2] ^= t; - - s[3] = rotl(s[3], 45); - - return result; -} diff --git a/src/xoshiro/dune b/src/xoshiro/dune index baefbf5..2fbfc89 100644 --- a/src/xoshiro/dune +++ b/src/xoshiro/dune @@ -2,4 +2,4 @@ (library (name xoshiro) - (foreign_stubs (language c) (names x256pp))) + (foreign_stubs (language c) (names x256pp stubs reference))) diff --git a/src/xoshiro/reference.c b/src/xoshiro/reference.c new file mode 100644 index 0000000..dc03d63 --- /dev/null +++ b/src/xoshiro/reference.c @@ -0,0 +1,45 @@ +// Reference: http://prng.di.unimi.it/xoshiro256plusplus.c + +#include + +static inline uint64_t rotl(const uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +uint64_t x256pp_state[4] = { + 0xd430ebc2f4d9e7b8, + 0x44815975ddd19d52, + 0xbb18c2b0b18d0bfe, + 0x6c0681407295c644 +}; + +uint64_t x256pp_next(void) { + const uint64_t result = rotl(x256pp_state[0] + x256pp_state[3], 23) + x256pp_state[0]; + + const uint64_t t = x256pp_state[1] << 17; + + x256pp_state[2] ^= x256pp_state[0]; + x256pp_state[3] ^= x256pp_state[1]; + x256pp_state[1] ^= x256pp_state[2]; + x256pp_state[0] ^= x256pp_state[3]; + + x256pp_state[2] ^= t; + + x256pp_state[3] = rotl(x256pp_state[3], 45); + + return result; +} + +static uint64_t splitmix64(uint64_t* s) { + uint64_t z = (*s += 0x9e3779b97f4a7c15); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); +} + +void x256pp_seed(uint64_t seed) { + uint64_t s = seed; + for (int i = 0; i < 4; i++) + x256pp_state[i] = splitmix64(&s); +} + diff --git a/c/xoshiro256pp.h b/src/xoshiro/reference.h similarity index 50% rename from c/xoshiro256pp.h rename to src/xoshiro/reference.h index 4f24453..ec5d146 100644 --- a/c/xoshiro256pp.h +++ b/src/xoshiro/reference.h @@ -3,6 +3,8 @@ #include -uint64_t next(); +uint64_t x256pp_state[4]; +uint64_t x256pp_next(); +void x256pp_seed(uint64_t); #endif diff --git a/src/xoshiro/reference.ml b/src/xoshiro/reference.ml new file mode 100644 index 0000000..ee7ef1e --- /dev/null +++ b/src/xoshiro/reference.ml @@ -0,0 +1,2 @@ +external seed: int64 -> unit = "caml_x256pp_seed" +external next: unit -> int64 = "caml_x256pp_next" diff --git a/src/xoshiro/stubs.c b/src/xoshiro/stubs.c new file mode 100644 index 0000000..518cac6 --- /dev/null +++ b/src/xoshiro/stubs.c @@ -0,0 +1,23 @@ +#define CAML_NAME_SPACE +#include +#include +#include +#include +#include "reference.h" + +CAMLprim value caml_x256pp_seed(value seed) { + CAMLparam1(seed); + int64_t x = Int64_val(seed); + // reinterpret x as an unsigned integer + x256pp_seed(*(uint64_t*)(&x)); + CAMLreturn(Val_unit); +} + +CAMLprim value caml_x256pp_next(value unit) { + CAMLparam1(unit); + uint64_t x = x256pp_next(); + // reinterpret x as a signed integer + value res = caml_copy_int64(*(int64_t*)(&x)); + CAMLreturn(res); +} + From 95401e28666ea1fcfe6151571cbc9ad574a85a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 28 Apr 2020 00:58:31 +0200 Subject: [PATCH 05/26] Merge x256pp_c/* and stubs.c --- src/xoshiro/dune | 4 +-- src/xoshiro/stubs.c | 19 ++++++++++++ src/xoshiro/x256pp.ml | 6 ++-- src/xoshiro/x256pp_c/x256pp.c | 55 ----------------------------------- src/xoshiro/x256pp_c/x256pp.h | 12 -------- 5 files changed, 23 insertions(+), 73 deletions(-) delete mode 100644 src/xoshiro/x256pp_c/x256pp.c delete mode 100644 src/xoshiro/x256pp_c/x256pp.h diff --git a/src/xoshiro/dune b/src/xoshiro/dune index 2fbfc89..ceca707 100644 --- a/src/xoshiro/dune +++ b/src/xoshiro/dune @@ -1,5 +1,3 @@ -(include_subdirs unqualified) - (library (name xoshiro) - (foreign_stubs (language c) (names x256pp stubs reference))) + (foreign_stubs (language c) (names stubs reference))) diff --git a/src/xoshiro/stubs.c b/src/xoshiro/stubs.c index 518cac6..2c872d3 100644 --- a/src/xoshiro/stubs.c +++ b/src/xoshiro/stubs.c @@ -21,3 +21,22 @@ CAMLprim value caml_x256pp_next(value unit) { CAMLreturn(res); } +CAMLprim value caml_x256pp_bits(value unit) { + CAMLparam1(unit); + intnat res = x256pp_next() >> 34; + CAMLreturn(Val_long(res)); +} + +CAMLprim value caml_x256pp_bits62(value unit) { + CAMLparam1(unit); + long res = x256pp_next() >> 2; + CAMLreturn(Val_long(res)); +} + +CAMLprim value caml_x256pp_double(value f) { + CAMLparam1(f); + double res = (x256pp_next() >> 11) * 0x1.0p-53; + res *= Double_val(f); + CAMLreturn(caml_copy_double(res)); +} + diff --git a/src/xoshiro/x256pp.ml b/src/xoshiro/x256pp.ml index 44a8ed5..cd69cf7 100644 --- a/src/xoshiro/x256pp.ml +++ b/src/xoshiro/x256pp.ml @@ -164,7 +164,7 @@ module Int64 = struct end module C = struct - external bits: unit -> int = "x256pp_bits" - external bits62: unit -> int = "x256pp_bits62" - external float: float -> float = "x256pp_double" + external bits: unit -> int = "caml_x256pp_bits" + external bits62: unit -> int = "caml_x256pp_bits62" + external float: float -> float = "caml_x256pp_double" end diff --git a/src/xoshiro/x256pp_c/x256pp.c b/src/xoshiro/x256pp_c/x256pp.c deleted file mode 100644 index c768a4d..0000000 --- a/src/xoshiro/x256pp_c/x256pp.c +++ /dev/null @@ -1,55 +0,0 @@ -#include "x256pp.h" -#include -#include - -static inline uint64_t rotl(const uint64_t x, int k) { - return (x << k) | (x >> (64 - k)); -} - -static uint64_t s[4] = { - 0xdeadbeefdeadbeef, - 0x4242424242424242, - 0x3737373737373737, - 0xca7aca7aca7aca7a -}; - -static uint64_t next(void) { - const uint64_t result = rotl(s[0] + s[3], 23) + s[0]; - - const uint64_t t = s[1] << 17; - - s[2] ^= s[0]; - s[3] ^= s[1]; - s[1] ^= s[2]; - s[0] ^= s[3]; - - s[2] ^= t; - - s[3] = rotl(s[3], 45); - - return result; -} - -static long int buf = 0; -static int buf_len = 0; -static const uint64_t mask30 = (1 << 30) - 1; - -CAMLprim value x256pp_bits() { - if (buf_len == 0) { - buf = next(); - buf_len = 2; - } - long int res = buf & mask30; - buf_len--; - buf = buf >> 32; - return Val_long(res); -} - -CAMLprim value x256pp_bits62() { - return Val_long(next() >> 2); -} - -CAMLprim value x256pp_double(value f) { - double d = (next() >> 11) * 0x1.0p-53 * Double_val(f); - return caml_copy_double(d); -} diff --git a/src/xoshiro/x256pp_c/x256pp.h b/src/xoshiro/x256pp_c/x256pp.h deleted file mode 100644 index 77d8b8a..0000000 --- a/src/xoshiro/x256pp_c/x256pp.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef X256PP_H -#define X256PP_H 1 - -#define CAML_NAME_SPACE -#include -#include - -CAMLprim value x256pp_bits(); -CAMLprim value x256pp_bits62(); -CAMLprim value x256pp_double(value); - -#endif From 430acce2d480afd5bddb629a3ddde63fb4926445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 28 Apr 2020 00:30:02 +0200 Subject: [PATCH 06/26] Test X256pp.Int64 against reference implementation --- src/xoshiro/tests/dune | 3 +++ src/xoshiro/tests/testInt64.ml | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/xoshiro/tests/dune create mode 100644 src/xoshiro/tests/testInt64.ml diff --git a/src/xoshiro/tests/dune b/src/xoshiro/tests/dune new file mode 100644 index 0000000..76caeda --- /dev/null +++ b/src/xoshiro/tests/dune @@ -0,0 +1,3 @@ +(tests + (names testInt64) + (libraries splitmix xoshiro)) diff --git a/src/xoshiro/tests/testInt64.ml b/src/xoshiro/tests/testInt64.ml new file mode 100644 index 0000000..4a9a9de --- /dev/null +++ b/src/xoshiro/tests/testInt64.ml @@ -0,0 +1,20 @@ +open Xoshiro + +let test_compare nb f g = + for _ = 1 to nb do + assert (f () = g ()) + done + +let ocaml_seed seed = + let s = ref seed in + for i = 0 to 3 do + X256pp.Int64.s.(i) <- Splitmix.Sm64.next s + done + +let () = + (* Chosen by fair dice rool. Guaranted to be random. *) + (* https://xkcd.com/221/ *) + let seed = 0xc6bb1e08d1a33f2fL in + Reference.seed seed; + ocaml_seed seed; + test_compare 1_000_000 Reference.next X256pp.Int64.next From e7697eb71ec224e17d0a81993bc1b3296ca68b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 28 Apr 2020 01:17:32 +0200 Subject: [PATCH 07/26] Test X256pp.Int against reference implementation --- src/xoshiro/tests/dune | 2 +- src/xoshiro/tests/testInt.ml | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/xoshiro/tests/testInt.ml diff --git a/src/xoshiro/tests/dune b/src/xoshiro/tests/dune index 76caeda..03ddcf3 100644 --- a/src/xoshiro/tests/dune +++ b/src/xoshiro/tests/dune @@ -1,3 +1,3 @@ (tests - (names testInt64) + (names testInt64 testInt) (libraries splitmix xoshiro)) diff --git a/src/xoshiro/tests/testInt.ml b/src/xoshiro/tests/testInt.ml new file mode 100644 index 0000000..a195023 --- /dev/null +++ b/src/xoshiro/tests/testInt.ml @@ -0,0 +1,36 @@ +open Xoshiro + +let int_int_to_int64 (hi, lo) = + let open Int64 in + logxor (shift_left (of_int hi) 32) (of_int lo) + +let int64_to_int_int n = + let open Int64 in + let hi = to_int (shift_right_logical n 32) in + let lo = to_int (logand n 0xFFFFFFFFL) in + (hi, lo) + +let test_compare nb f g = + for _ = 1 to nb do + assert (f () = g ()) + done + +let ocaml_seed seed = + let s = ref seed in + for i = 0 to 3 do + let hi, lo = Splitmix.Sm64.next s |> int64_to_int_int in + X256pp.Int.state.(2 * i) <- hi; + X256pp.Int.state.(2 * i + 1) <- lo + done + +let () = + (* Chosen by fair dice rool. Guaranted to be random. *) + (* https://xkcd.com/221/ *) + let seed = 0xc6bb1e08d1a33f2fL in + Reference.seed seed; + ocaml_seed seed; + test_compare + 1_000_000 + Reference.next + (fun () -> + X256pp.Int.next () |> int_int_to_int64) From e1d51d18aa0c2eb6568a54849359b5a4dfaa4e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 28 Apr 2020 01:19:36 +0200 Subject: [PATCH 08/26] The c/ folder is obsolete --- c/.gitignore | 3 --- c/Makefile | 12 ------------ c/ocamlrandom.c | 24 ------------------------ c/ocamlrandom.h | 1 - c/testocamlrandom.c | 14 -------------- c/testxoshiro.c | 15 --------------- 6 files changed, 69 deletions(-) delete mode 100644 c/.gitignore delete mode 100644 c/Makefile delete mode 100644 c/ocamlrandom.c delete mode 100644 c/ocamlrandom.h delete mode 100644 c/testocamlrandom.c delete mode 100644 c/testxoshiro.c diff --git a/c/.gitignore b/c/.gitignore deleted file mode 100644 index dba0160..0000000 --- a/c/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/testocamlrandom -/testxoshiro -/testsplitmix diff --git a/c/Makefile b/c/Makefile deleted file mode 100644 index ef136ec..0000000 --- a/c/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -all: testxoshiro testocamlrandom testsplitmix - -testxoshiro: testxoshiro.c xoshiro256pp.c xoshiro256pp.h - cc -o $@ $< xoshiro256pp.c - -testocamlrandom: testocamlrandom.c ocamlrandom.c ocamlrandom.h - cc -o $@ $< ocamlrandom.c - -clean: - rm -f testxoshiro testocamlrandom testsplitmix - -.PHONY: all clean diff --git a/c/ocamlrandom.c b/c/ocamlrandom.c deleted file mode 100644 index 6c90d48..0000000 --- a/c/ocamlrandom.c +++ /dev/null @@ -1,24 +0,0 @@ -static int s[55] = { - 0x3ae2522b, 0x1d8d4634, 0x15b4fad0, 0x18b14ace, 0x12f8a3c4, 0x3b086c47, - 0x16d467d6, 0x101d91c7, 0x321df177, 0x0176c193, 0x1ff72bf1, 0x1e889109, - 0x0b464b18, 0x2b86b97c, 0x0891da48, 0x03137463, 0x085ac5a1, 0x15d61f2f, - 0x3bced359, 0x29c1c132, 0x3a86766e, 0x366d8c86, 0x1f5b6222, 0x3ce1b59f, - 0x2ebf78e1, 0x27cd1b86, 0x258f3dc3, 0x389a8194, 0x02e4c44c, 0x18c43f7d, - 0x0f6e534f, 0x1e7df359, 0x055d0b7e, 0x10e84e7e, 0x126198e4, 0x0e7722cb, - 0x1cbede28, 0x3391b964, 0x3d40e92a, 0x0c59933d, 0x0b8cd0b7, 0x24efff1c, - 0x2803fdaa, 0x08ebc72e, 0x0f522e32, 0x05398edc, 0x2144a04c, 0x0aef3cbd, - 0x01ad4719, 0x35b93cd6, 0x2a559d4f, 0x1e6fd768, 0x26e27f36, 0x186f18c3, - 0x2fbf967a, -}; - -static int idx = 0; - -int bits() { - idx = (idx + 1) % 55; - int curval = s[idx]; - int newval = s[(idx + 24) % 55] + (curval ^ ((curval >> 25) & 0x1F)); - int newval30 = newval & 0x3FFFFFFF; - s[idx] = newval30; - return newval30; -} - diff --git a/c/ocamlrandom.h b/c/ocamlrandom.h deleted file mode 100644 index 6ca5c74..0000000 --- a/c/ocamlrandom.h +++ /dev/null @@ -1 +0,0 @@ -int bits(); diff --git a/c/testocamlrandom.c b/c/testocamlrandom.c deleted file mode 100644 index 77bce0c..0000000 --- a/c/testocamlrandom.c +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include "ocamlrandom.h" - -int main(int argc, char* argv[]) { - int n = 10; - if (argc == 2) n = atoi(argv[1]); - - for (int i = 0; i < n; i++) - printf("%08x\n", bits()); - - return 0; -} - diff --git a/c/testxoshiro.c b/c/testxoshiro.c deleted file mode 100644 index 39bdb2d..0000000 --- a/c/testxoshiro.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include -#include -#include "xoshiro256pp.h" - -int main(int argc, const char* argv[]) { - int n = 10; - if (argc == 2) n = atoi(argv[1]); - - for (int i = 0; i < n; i++) - printf("%016lx\n", next()); - - return 0; -} From 640b0a2de48bc3f02e8f26955e2fcd7b11f3fde6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Tue, 28 Apr 2020 23:29:28 +0200 Subject: [PATCH 09/26] Remove obsolete tests --- Makefile | 12 ++++++++++++ src/dune-project => dune-project | 0 src/Makefile | 14 -------------- src/tests/dune | 13 ------------- src/tests/ocamlrandom.ml | 10 ---------- src/tests/splitmix.ml | 13 ------------- src/tests/xoshiro.ml | 15 --------------- 7 files changed, 12 insertions(+), 65 deletions(-) create mode 100644 Makefile rename src/dune-project => dune-project (100%) delete mode 100644 src/Makefile delete mode 100644 src/tests/dune delete mode 100644 src/tests/ocamlrandom.ml delete mode 100644 src/tests/splitmix.ml delete mode 100644 src/tests/xoshiro.ml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8d40edc --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +all: tests bench + +test: + dune test + +bench: + dune build ./bench.exe + +clean: + dune clean + +.PHONY: all clean bench test diff --git a/src/dune-project b/dune-project similarity index 100% rename from src/dune-project rename to dune-project diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index 1c48a5c..0000000 --- a/src/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -all: tests bench - -tests: - dune build ./tests/ocamlrandom.exe - dune build ./tests/xoshiro.exe - dune build ./tests/splitmix.exe - -bench: - dune build ./bench.exe - -clean: - dune clean - -.PHONY: all clean bench tests diff --git a/src/tests/dune b/src/tests/dune deleted file mode 100644 index 5189e02..0000000 --- a/src/tests/dune +++ /dev/null @@ -1,13 +0,0 @@ -(executable - (name xoshiro) - (modules xoshiro) - (libraries xoshiro)) - -(executable - (name ocamlrandom) - (modules ocamlrandom)) - -(executable - (name splitmix) - (modules splitmix) - (libraries splitmix)) diff --git a/src/tests/ocamlrandom.ml b/src/tests/ocamlrandom.ml deleted file mode 100644 index 00344a3..0000000 --- a/src/tests/ocamlrandom.ml +++ /dev/null @@ -1,10 +0,0 @@ -let () = - let n = - if Array.length Sys.argv = 2 - then int_of_string Sys.argv.(1) - else 10 - in - - for _ = 1 to n do - Format.printf "%08x\n" (Random.bits ()) - done diff --git a/src/tests/splitmix.ml b/src/tests/splitmix.ml deleted file mode 100644 index eb3e35a..0000000 --- a/src/tests/splitmix.ml +++ /dev/null @@ -1,13 +0,0 @@ -open Splitmix - -let () = - let n = - if Array.length Sys.argv = 2 - then int_of_string Sys.argv.(1) - else 10 - in - - let state = ref 0xdead42beef37ca7aL in - for _ = 1 to n do - Format.printf "%016Lx@." (Sm64.next state) - done diff --git a/src/tests/xoshiro.ml b/src/tests/xoshiro.ml deleted file mode 100644 index e615baa..0000000 --- a/src/tests/xoshiro.ml +++ /dev/null @@ -1,15 +0,0 @@ -open Xoshiro - -let () = - let n = - if Array.length Sys.argv = 2 - then int_of_string Sys.argv.(1) - else 10 - in - - for _ = 1 to n do - let hi, lo = X256pp.Int.next () in - Format.printf "%08x%08x " hi lo; - Format.printf "%016Lx " (X256pp.Int64.next ()); - Format.printf "%08x@." (X256pp.C.bits ()) - done From adc429d4edced32592a1ddfcc98d7bbdee0764c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 6 May 2020 15:11:45 +0200 Subject: [PATCH 10/26] Remove duplicate code --- src/splitmix/dune | 8 +++++++- src/splitmix/reference.c | 16 ++++++++++++---- src/splitmix/reference.h | 4 +++- src/splitmix/stubs.c | 7 ++++--- src/xoshiro/dune | 9 ++++++++- src/xoshiro/reference.c | 10 ++-------- 6 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/splitmix/dune b/src/splitmix/dune index 62a2772..fbd8277 100644 --- a/src/splitmix/dune +++ b/src/splitmix/dune @@ -1,9 +1,15 @@ (library (name splitmix) - (foreign_stubs (language c) (names reference stubs)) + (foreign_archives splitmix) + (foreign_stubs (language c) (names stubs)) (libraries makeRandom) (modules :standard \ Tests)) +(foreign_library + (archive_name splitmix) + (language c) + (names reference)) + (test (name tests) (modules Tests) diff --git a/src/splitmix/reference.c b/src/splitmix/reference.c index d653006..f9897ff 100644 --- a/src/splitmix/reference.c +++ b/src/splitmix/reference.c @@ -2,12 +2,20 @@ #include -uint64_t sm64_state; /* The state can be seeded with any value. */ - -uint64_t sm64_next() { - uint64_t z = (sm64_state += 0x9e3779b97f4a7c15); +uint64_t sm64_next_stateless(uint64_t* s) { + uint64_t z = (*s += 0x9e3779b97f4a7c15); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } +static uint64_t state; /* The state can be seeded with any value. */ + +void sm64_seed(uint64_t seed) { + state = seed; +} + +uint64_t sm64_next() { + return sm64_next_stateless(&state); +} + diff --git a/src/splitmix/reference.h b/src/splitmix/reference.h index aef2394..0c8e72f 100644 --- a/src/splitmix/reference.h +++ b/src/splitmix/reference.h @@ -3,7 +3,9 @@ #include -uint64_t sm64_state; +uint64_t sm64_next_stateless(uint64_t*); + +void sm64_seed(uint64_t); uint64_t sm64_next(); #endif diff --git a/src/splitmix/stubs.c b/src/splitmix/stubs.c index daf31ed..b5e5595 100644 --- a/src/splitmix/stubs.c +++ b/src/splitmix/stubs.c @@ -1,15 +1,16 @@ +#include +#include "reference.h" + #define CAML_NAME_SPACE #include #include #include -#include -#include "reference.h" CAMLprim value caml_sm64_seed(value seed) { CAMLparam1(seed); int64_t x = Int64_val(seed); // reinterpret x as an unsigned integer - sm64_state = *(uint64_t*)(&x); + sm64_seed(*(uint64_t*)(&x)); CAMLreturn(Val_unit); } diff --git a/src/xoshiro/dune b/src/xoshiro/dune index ceca707..ef6506c 100644 --- a/src/xoshiro/dune +++ b/src/xoshiro/dune @@ -1,3 +1,10 @@ (library (name xoshiro) - (foreign_stubs (language c) (names stubs reference))) + (foreign_archives xoshiro) + (foreign_stubs (language c) (names stubs))) + +(foreign_library + (archive_name xoshiro) + (language c) + (flags -lsplitmix) + (names reference)) diff --git a/src/xoshiro/reference.c b/src/xoshiro/reference.c index dc03d63..0d0984f 100644 --- a/src/xoshiro/reference.c +++ b/src/xoshiro/reference.c @@ -1,6 +1,7 @@ // Reference: http://prng.di.unimi.it/xoshiro256plusplus.c #include +#include "../splitmix/reference.h" static inline uint64_t rotl(const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); @@ -30,16 +31,9 @@ uint64_t x256pp_next(void) { return result; } -static uint64_t splitmix64(uint64_t* s) { - uint64_t z = (*s += 0x9e3779b97f4a7c15); - z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; - z = (z ^ (z >> 27)) * 0x94d049bb133111eb; - return z ^ (z >> 31); -} - void x256pp_seed(uint64_t seed) { uint64_t s = seed; for (int i = 0; i < 4; i++) - x256pp_state[i] = splitmix64(&s); + x256pp_state[i] = sm64_next_stateless(&s); } From e29fb078602201437b55291fbd7c45e48a284652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 12:20:25 +0200 Subject: [PATCH 11/26] git mv * prng/ --- Makefile | 12 ------------ dune-project => prng/dune-project | 0 {src => prng/src}/.gitignore | 0 {src => prng/src}/bench.ml | 0 {src => prng/src}/dune | 0 {src => prng/src}/make-random/bench.ml | 0 {src => prng/src}/make-random/dune | 0 {src => prng/src}/make-random/makeRandom.ml | 0 {src => prng/src}/make-random/test.ml | 0 {src => prng/src}/splitmix/dune | 0 {src => prng/src}/splitmix/reference.c | 0 {src => prng/src}/splitmix/reference.h | 0 {src => prng/src}/splitmix/reference.ml | 0 {src => prng/src}/splitmix/sm64.ml | 0 {src => prng/src}/splitmix/stubs.c | 0 {src => prng/src}/splitmix/tests.ml | 0 {src => prng/src}/xoshiro/dune | 0 {src => prng/src}/xoshiro/reference.c | 0 {src => prng/src}/xoshiro/reference.h | 0 {src => prng/src}/xoshiro/reference.ml | 0 {src => prng/src}/xoshiro/stubs.c | 0 {src => prng/src}/xoshiro/tests/dune | 0 {src => prng/src}/xoshiro/tests/testInt.ml | 0 {src => prng/src}/xoshiro/tests/testInt64.ml | 0 {src => prng/src}/xoshiro/x256pp.ml | 0 25 files changed, 12 deletions(-) delete mode 100644 Makefile rename dune-project => prng/dune-project (100%) rename {src => prng/src}/.gitignore (100%) rename {src => prng/src}/bench.ml (100%) rename {src => prng/src}/dune (100%) rename {src => prng/src}/make-random/bench.ml (100%) rename {src => prng/src}/make-random/dune (100%) rename {src => prng/src}/make-random/makeRandom.ml (100%) rename {src => prng/src}/make-random/test.ml (100%) rename {src => prng/src}/splitmix/dune (100%) rename {src => prng/src}/splitmix/reference.c (100%) rename {src => prng/src}/splitmix/reference.h (100%) rename {src => prng/src}/splitmix/reference.ml (100%) rename {src => prng/src}/splitmix/sm64.ml (100%) rename {src => prng/src}/splitmix/stubs.c (100%) rename {src => prng/src}/splitmix/tests.ml (100%) rename {src => prng/src}/xoshiro/dune (100%) rename {src => prng/src}/xoshiro/reference.c (100%) rename {src => prng/src}/xoshiro/reference.h (100%) rename {src => prng/src}/xoshiro/reference.ml (100%) rename {src => prng/src}/xoshiro/stubs.c (100%) rename {src => prng/src}/xoshiro/tests/dune (100%) rename {src => prng/src}/xoshiro/tests/testInt.ml (100%) rename {src => prng/src}/xoshiro/tests/testInt64.ml (100%) rename {src => prng/src}/xoshiro/x256pp.ml (100%) diff --git a/Makefile b/Makefile deleted file mode 100644 index 8d40edc..0000000 --- a/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -all: tests bench - -test: - dune test - -bench: - dune build ./bench.exe - -clean: - dune clean - -.PHONY: all clean bench test diff --git a/dune-project b/prng/dune-project similarity index 100% rename from dune-project rename to prng/dune-project diff --git a/src/.gitignore b/prng/src/.gitignore similarity index 100% rename from src/.gitignore rename to prng/src/.gitignore diff --git a/src/bench.ml b/prng/src/bench.ml similarity index 100% rename from src/bench.ml rename to prng/src/bench.ml diff --git a/src/dune b/prng/src/dune similarity index 100% rename from src/dune rename to prng/src/dune diff --git a/src/make-random/bench.ml b/prng/src/make-random/bench.ml similarity index 100% rename from src/make-random/bench.ml rename to prng/src/make-random/bench.ml diff --git a/src/make-random/dune b/prng/src/make-random/dune similarity index 100% rename from src/make-random/dune rename to prng/src/make-random/dune diff --git a/src/make-random/makeRandom.ml b/prng/src/make-random/makeRandom.ml similarity index 100% rename from src/make-random/makeRandom.ml rename to prng/src/make-random/makeRandom.ml diff --git a/src/make-random/test.ml b/prng/src/make-random/test.ml similarity index 100% rename from src/make-random/test.ml rename to prng/src/make-random/test.ml diff --git a/src/splitmix/dune b/prng/src/splitmix/dune similarity index 100% rename from src/splitmix/dune rename to prng/src/splitmix/dune diff --git a/src/splitmix/reference.c b/prng/src/splitmix/reference.c similarity index 100% rename from src/splitmix/reference.c rename to prng/src/splitmix/reference.c diff --git a/src/splitmix/reference.h b/prng/src/splitmix/reference.h similarity index 100% rename from src/splitmix/reference.h rename to prng/src/splitmix/reference.h diff --git a/src/splitmix/reference.ml b/prng/src/splitmix/reference.ml similarity index 100% rename from src/splitmix/reference.ml rename to prng/src/splitmix/reference.ml diff --git a/src/splitmix/sm64.ml b/prng/src/splitmix/sm64.ml similarity index 100% rename from src/splitmix/sm64.ml rename to prng/src/splitmix/sm64.ml diff --git a/src/splitmix/stubs.c b/prng/src/splitmix/stubs.c similarity index 100% rename from src/splitmix/stubs.c rename to prng/src/splitmix/stubs.c diff --git a/src/splitmix/tests.ml b/prng/src/splitmix/tests.ml similarity index 100% rename from src/splitmix/tests.ml rename to prng/src/splitmix/tests.ml diff --git a/src/xoshiro/dune b/prng/src/xoshiro/dune similarity index 100% rename from src/xoshiro/dune rename to prng/src/xoshiro/dune diff --git a/src/xoshiro/reference.c b/prng/src/xoshiro/reference.c similarity index 100% rename from src/xoshiro/reference.c rename to prng/src/xoshiro/reference.c diff --git a/src/xoshiro/reference.h b/prng/src/xoshiro/reference.h similarity index 100% rename from src/xoshiro/reference.h rename to prng/src/xoshiro/reference.h diff --git a/src/xoshiro/reference.ml b/prng/src/xoshiro/reference.ml similarity index 100% rename from src/xoshiro/reference.ml rename to prng/src/xoshiro/reference.ml diff --git a/src/xoshiro/stubs.c b/prng/src/xoshiro/stubs.c similarity index 100% rename from src/xoshiro/stubs.c rename to prng/src/xoshiro/stubs.c diff --git a/src/xoshiro/tests/dune b/prng/src/xoshiro/tests/dune similarity index 100% rename from src/xoshiro/tests/dune rename to prng/src/xoshiro/tests/dune diff --git a/src/xoshiro/tests/testInt.ml b/prng/src/xoshiro/tests/testInt.ml similarity index 100% rename from src/xoshiro/tests/testInt.ml rename to prng/src/xoshiro/tests/testInt.ml diff --git a/src/xoshiro/tests/testInt64.ml b/prng/src/xoshiro/tests/testInt64.ml similarity index 100% rename from src/xoshiro/tests/testInt64.ml rename to prng/src/xoshiro/tests/testInt64.ml diff --git a/src/xoshiro/x256pp.ml b/prng/src/xoshiro/x256pp.ml similarity index 100% rename from src/xoshiro/x256pp.ml rename to prng/src/xoshiro/x256pp.ml From b2565441743715f22a06e0a28bd1c3e3bc0f8572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 16:05:11 +0200 Subject: [PATCH 12/26] Remove CAMLprim everywhere --- prng/src/splitmix/stubs.c | 4 ++-- prng/src/xoshiro/stubs.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/prng/src/splitmix/stubs.c b/prng/src/splitmix/stubs.c index b5e5595..d03b481 100644 --- a/prng/src/splitmix/stubs.c +++ b/prng/src/splitmix/stubs.c @@ -6,7 +6,7 @@ #include #include -CAMLprim value caml_sm64_seed(value seed) { +value caml_sm64_seed(value seed) { CAMLparam1(seed); int64_t x = Int64_val(seed); // reinterpret x as an unsigned integer @@ -14,7 +14,7 @@ CAMLprim value caml_sm64_seed(value seed) { CAMLreturn(Val_unit); } -CAMLprim value caml_sm64_next(value unit) { +value caml_sm64_next(value unit) { CAMLparam1(unit); uint64_t x = sm64_next(); // reinterpret x as a signed integer diff --git a/prng/src/xoshiro/stubs.c b/prng/src/xoshiro/stubs.c index 2c872d3..ab46b1a 100644 --- a/prng/src/xoshiro/stubs.c +++ b/prng/src/xoshiro/stubs.c @@ -5,7 +5,7 @@ #include #include "reference.h" -CAMLprim value caml_x256pp_seed(value seed) { +value caml_x256pp_seed(value seed) { CAMLparam1(seed); int64_t x = Int64_val(seed); // reinterpret x as an unsigned integer @@ -13,7 +13,7 @@ CAMLprim value caml_x256pp_seed(value seed) { CAMLreturn(Val_unit); } -CAMLprim value caml_x256pp_next(value unit) { +value caml_x256pp_next(value unit) { CAMLparam1(unit); uint64_t x = x256pp_next(); // reinterpret x as a signed integer @@ -21,19 +21,19 @@ CAMLprim value caml_x256pp_next(value unit) { CAMLreturn(res); } -CAMLprim value caml_x256pp_bits(value unit) { +value caml_x256pp_bits(value unit) { CAMLparam1(unit); intnat res = x256pp_next() >> 34; CAMLreturn(Val_long(res)); } -CAMLprim value caml_x256pp_bits62(value unit) { +value caml_x256pp_bits62(value unit) { CAMLparam1(unit); long res = x256pp_next() >> 2; CAMLreturn(Val_long(res)); } -CAMLprim value caml_x256pp_double(value f) { +value caml_x256pp_double(value f) { CAMLparam1(f); double res = (x256pp_next() >> 11) * 0x1.0p-53; res *= Double_val(f); From 76a40dd506bad9a7ed070273ec24e0f9a9e0465a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 16:18:48 +0200 Subject: [PATCH 13/26] consistency: #define WHATEVER_H 1 --- prng/src/splitmix/reference.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prng/src/splitmix/reference.h b/prng/src/splitmix/reference.h index 0c8e72f..311677c 100644 --- a/prng/src/splitmix/reference.h +++ b/prng/src/splitmix/reference.h @@ -1,5 +1,5 @@ #ifndef SPLITMIX64_H -#define SPLITMIX64_H +#define SPLITMIX64_H 1 #include From 050a066e084a708809bb88322fe429fe4f4bb071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 16:48:47 +0200 Subject: [PATCH 14/26] mv some stuff WIP --- prng/src/xoshiro/tests/dune | 2 +- prng/src/xoshiro/tests/{testInt.ml => int.ml} | 4 ++-- prng/src/xoshiro/tests/{testInt64.ml => int64.ml} | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename prng/src/xoshiro/tests/{testInt.ml => int.ml} (93%) rename prng/src/xoshiro/tests/{testInt64.ml => int64.ml} (100%) diff --git a/prng/src/xoshiro/tests/dune b/prng/src/xoshiro/tests/dune index 03ddcf3..b7aba12 100644 --- a/prng/src/xoshiro/tests/dune +++ b/prng/src/xoshiro/tests/dune @@ -1,3 +1,3 @@ (tests - (names testInt64 testInt) + (names int64 int) (libraries splitmix xoshiro)) diff --git a/prng/src/xoshiro/tests/testInt.ml b/prng/src/xoshiro/tests/int.ml similarity index 93% rename from prng/src/xoshiro/tests/testInt.ml rename to prng/src/xoshiro/tests/int.ml index a195023..52460b0 100644 --- a/prng/src/xoshiro/tests/testInt.ml +++ b/prng/src/xoshiro/tests/int.ml @@ -1,11 +1,11 @@ open Xoshiro let int_int_to_int64 (hi, lo) = - let open Int64 in + let open Stdlib.Int64 in logxor (shift_left (of_int hi) 32) (of_int lo) let int64_to_int_int n = - let open Int64 in + let open Stdlib.Int64 in let hi = to_int (shift_right_logical n 32) in let lo = to_int (logand n 0xFFFFFFFFL) in (hi, lo) diff --git a/prng/src/xoshiro/tests/testInt64.ml b/prng/src/xoshiro/tests/int64.ml similarity index 100% rename from prng/src/xoshiro/tests/testInt64.ml rename to prng/src/xoshiro/tests/int64.ml From 0012f456206fd768072fb6b77fb72b4126855e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 17:20:45 +0200 Subject: [PATCH 15/26] Hide x256pp_state (and fix a linker issue) --- prng/src/xoshiro/reference.c | 2 +- prng/src/xoshiro/reference.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/prng/src/xoshiro/reference.c b/prng/src/xoshiro/reference.c index 0d0984f..6811f84 100644 --- a/prng/src/xoshiro/reference.c +++ b/prng/src/xoshiro/reference.c @@ -7,7 +7,7 @@ static inline uint64_t rotl(const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); } -uint64_t x256pp_state[4] = { +static uint64_t x256pp_state[4] = { 0xd430ebc2f4d9e7b8, 0x44815975ddd19d52, 0xbb18c2b0b18d0bfe, diff --git a/prng/src/xoshiro/reference.h b/prng/src/xoshiro/reference.h index ec5d146..fe5ad8f 100644 --- a/prng/src/xoshiro/reference.h +++ b/prng/src/xoshiro/reference.h @@ -3,8 +3,8 @@ #include -uint64_t x256pp_state[4]; uint64_t x256pp_next(); void x256pp_seed(uint64_t); #endif + From 2add6b4a2751dafa13727b26fd519441265160d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 17:23:21 +0200 Subject: [PATCH 16/26] More consistent naming of tests --- prng/src/splitmix/dune | 8 +------- prng/src/splitmix/tests/dune | 3 +++ prng/src/splitmix/{tests.ml => tests/sm64.ml} | 0 3 files changed, 4 insertions(+), 7 deletions(-) create mode 100644 prng/src/splitmix/tests/dune rename prng/src/splitmix/{tests.ml => tests/sm64.ml} (100%) diff --git a/prng/src/splitmix/dune b/prng/src/splitmix/dune index fbd8277..a314c11 100644 --- a/prng/src/splitmix/dune +++ b/prng/src/splitmix/dune @@ -2,15 +2,9 @@ (name splitmix) (foreign_archives splitmix) (foreign_stubs (language c) (names stubs)) - (libraries makeRandom) - (modules :standard \ Tests)) + (libraries makeRandom)) (foreign_library (archive_name splitmix) (language c) (names reference)) - -(test - (name tests) - (modules Tests) - (libraries splitmix)) diff --git a/prng/src/splitmix/tests/dune b/prng/src/splitmix/tests/dune new file mode 100644 index 0000000..8b5b3e7 --- /dev/null +++ b/prng/src/splitmix/tests/dune @@ -0,0 +1,3 @@ +(test + (name sm64) + (libraries splitmix)) diff --git a/prng/src/splitmix/tests.ml b/prng/src/splitmix/tests/sm64.ml similarity index 100% rename from prng/src/splitmix/tests.ml rename to prng/src/splitmix/tests/sm64.ml From 7bed7ad37851dc8f8af861f726fbbde58ffa55aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 15 May 2020 17:24:45 +0200 Subject: [PATCH 17/26] mv dune-project back to the root of the repository --- prng/dune-project => dune-project | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename prng/dune-project => dune-project (100%) diff --git a/prng/dune-project b/dune-project similarity index 100% rename from prng/dune-project rename to dune-project From c452226407198d50945d5a3cb8380661199a2b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 18 May 2020 14:34:13 +0200 Subject: [PATCH 18/26] Internal testutils library --- .../splitmix/tests/{sm64.ml => sm64_ocaml.ml} | 0 prng/src/testutils/dune | 8 +++ prng/src/testutils/testutils.c | 52 +++++++++++++++++++ prng/src/testutils/testutils.h | 7 +++ prng/src/testutils/testutils.ml | 27 ++++++++++ 5 files changed, 94 insertions(+) rename prng/src/splitmix/tests/{sm64.ml => sm64_ocaml.ml} (100%) create mode 100644 prng/src/testutils/dune create mode 100644 prng/src/testutils/testutils.c create mode 100644 prng/src/testutils/testutils.h create mode 100644 prng/src/testutils/testutils.ml diff --git a/prng/src/splitmix/tests/sm64.ml b/prng/src/splitmix/tests/sm64_ocaml.ml similarity index 100% rename from prng/src/splitmix/tests/sm64.ml rename to prng/src/splitmix/tests/sm64_ocaml.ml diff --git a/prng/src/testutils/dune b/prng/src/testutils/dune new file mode 100644 index 0000000..f7f5dce --- /dev/null +++ b/prng/src/testutils/dune @@ -0,0 +1,8 @@ +(foreign_library + (archive_name testutils) + (language c) + (flags -Wall -Wextra -Wpedantic) + (names testutils)) + +(library + (name testutils)) diff --git a/prng/src/testutils/testutils.c b/prng/src/testutils/testutils.c new file mode 100644 index 0000000..9b7b233 --- /dev/null +++ b/prng/src/testutils/testutils.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +int usage(const char* prog_name) { + fprintf(stderr, "%s [--seed=SEED] [--nb=N]\n", prog_name); + return 1; +} + +int testutils_run( + int argc, char* argv[], void(*seed_func)(uint64_t), uint64_t(*next)(void) +) { + long nb_iterations = -1; + uint64_t seed = 0xdd6d0e2e849e51ad; + + struct option long_options[] = { + {"seed", optional_argument, NULL, 's'}, + {"nb", optional_argument, NULL, 'n'}, + }; + + while (1) { + int option_index = 0; + int c = getopt_long(argc, argv, "", long_options, &option_index); + char* endptr; + if (c == -1) break; + switch (c) { + case 's': + if (optarg == NULL) return usage(argv[0]); + seed = strtoull(optarg, &endptr, 10); + if (endptr == optarg || *endptr != '\0') return usage(argv[0]); + break; + case 'n': + if (optarg == NULL) return usage(argv[0]); + nb_iterations = strtol(optarg, &endptr, 10); + if (endptr == optarg || *endptr != '\0') return usage(argv[0]); + break; + default: + return usage(argv[0]); + } + } + + if (optind < argc) return usage(argv[0]); + + seed_func(seed); + for (long i = 0; i != nb_iterations; i++) { + printf("%lx\n", next()); + } + + return 0; +} + diff --git a/prng/src/testutils/testutils.h b/prng/src/testutils/testutils.h new file mode 100644 index 0000000..d60fcc4 --- /dev/null +++ b/prng/src/testutils/testutils.h @@ -0,0 +1,7 @@ +#ifndef TESTUTILS_H +#define TESTUTILS_H 1 + +#include +int testutils_run(int, char**, void(*)(uint64_t), uint64_t(*)(void)); + +#endif diff --git a/prng/src/testutils/testutils.ml b/prng/src/testutils/testutils.ml new file mode 100644 index 0000000..2e43091 --- /dev/null +++ b/prng/src/testutils/testutils.ml @@ -0,0 +1,27 @@ +let run ~seed_f ~next = + let seed = ref 0xdd6d0e2e849e51adL in + let nb_iterations = ref (-1) in + + let speclist = [ + ( + "--nb", + Arg.Set_int nb_iterations, + " Number of random integers to generate" + ); + ( + "--seed", + Arg.String (fun n -> seed := Int64.of_string n), + " 64-bit seed"); + ] in + + let usage = + Format.sprintf "%s [--seed=SEED] [--nb=N]" Sys.argv.(0) + in + + Arg.parse speclist (fun _ -> invalid_arg "run") usage; + seed_f !seed; + let i = ref 0 in + while !i <> !nb_iterations do + Format.printf "%Lx@." (next ()); + incr i + done From f3d83cf1a717ca595d6e2865ce493a5a99dff6b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 18 May 2020 14:36:04 +0200 Subject: [PATCH 19/26] Test: diff the output pure ocaml vs. pure C code --- prng/src/splitmix/tests/dune | 16 +++++++++++++--- prng/src/splitmix/tests/sm64_c.c | 11 +++++++++++ prng/src/splitmix/tests/sm64_ocaml.ml | 19 ++++--------------- 3 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 prng/src/splitmix/tests/sm64_c.c diff --git a/prng/src/splitmix/tests/dune b/prng/src/splitmix/tests/dune index 8b5b3e7..0b7f5c0 100644 --- a/prng/src/splitmix/tests/dune +++ b/prng/src/splitmix/tests/dune @@ -1,3 +1,13 @@ -(test - (name sm64) - (libraries splitmix)) +(executable + (name sm64_ocaml) + (libraries splitmix testutils)) + +(rule + (target sm64_c.exe) + (deps sm64_c.c ../../testutils/libtestutils.a ../libsplitmix.a) + (action + (run cc -Wall -Wextra -Wpedantic -o %{target} %{deps}))) + +(rule (with-stdout-to sm64_ocaml.output (run ./sm64_ocaml.exe --nb=1000))) +(rule (with-stdout-to sm64_c.output (run ./sm64_c.exe --nb=1000))) +(rule (alias runtest) (action (diff sm64_ocaml.output sm64_c.output))) diff --git a/prng/src/splitmix/tests/sm64_c.c b/prng/src/splitmix/tests/sm64_c.c new file mode 100644 index 0000000..57d89e2 --- /dev/null +++ b/prng/src/splitmix/tests/sm64_c.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +#include "../reference.h" +#include "../../testutils/testutils.h" + +int main(int argc, char* argv[]) { + return testutils_run(argc, argv, sm64_seed, sm64_next); +} diff --git a/prng/src/splitmix/tests/sm64_ocaml.ml b/prng/src/splitmix/tests/sm64_ocaml.ml index 197f7b0..8cd0e97 100644 --- a/prng/src/splitmix/tests/sm64_ocaml.ml +++ b/prng/src/splitmix/tests/sm64_ocaml.ml @@ -1,18 +1,7 @@ open Splitmix -let test_compare nb f g = - for _ = 1 to nb do - assert (f () = g ()) - done - let () = - (* Chosen by fair dice rool. Guaranted to be random. *) - (* https://xkcd.com/221/ *) - let seed = 0xdd6d0e2e849e51adL in - let ocaml_state = ref seed in - - Reference.seed seed; - test_compare - 1_000_000 - Reference.next - (fun () -> Sm64.next ocaml_state) + let state = ref 0L in + Testutils.run + ~seed_f:(fun s -> state := s) + ~next:(fun () -> Sm64.next state) From 05999c06320fb854bb59f522c4717bd7f4f8d0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 18 May 2020 17:46:17 +0200 Subject: [PATCH 20/26] Test all rngs against a C reference implementation --- prng/src/bench.ml | 11 ------ prng/src/splitmix/dune | 7 ---- prng/src/splitmix/reference.ml | 2 -- prng/src/splitmix/stubs.c | 24 ------------- prng/src/splitmix/tests/dune | 2 +- prng/src/splitmix/{ => tests}/reference.c | 17 ++++----- prng/src/splitmix/{ => tests}/reference.h | 3 -- prng/src/splitmix/tests/sm64_c.c | 7 +--- prng/src/xoshiro/dune | 9 +---- prng/src/xoshiro/reference.ml | 2 -- prng/src/xoshiro/stubs.c | 42 ----------------------- prng/src/xoshiro/tests/dune | 18 ++++++++-- prng/src/xoshiro/tests/int.ml | 18 ++-------- prng/src/xoshiro/tests/int64.ml | 24 ++++--------- prng/src/xoshiro/{ => tests}/reference.c | 8 ++++- prng/src/xoshiro/{ => tests}/reference.h | 0 prng/src/xoshiro/tests/x256pp_c.c | 6 ++++ prng/src/xoshiro/x256pp.ml | 6 ---- 18 files changed, 48 insertions(+), 158 deletions(-) delete mode 100644 prng/src/splitmix/reference.ml delete mode 100644 prng/src/splitmix/stubs.c rename prng/src/splitmix/{ => tests}/reference.c (53%) rename prng/src/splitmix/{ => tests}/reference.h (73%) delete mode 100644 prng/src/xoshiro/reference.ml delete mode 100644 prng/src/xoshiro/stubs.c rename prng/src/xoshiro/{ => tests}/reference.c (80%) rename prng/src/xoshiro/{ => tests}/reference.h (100%) create mode 100644 prng/src/xoshiro/tests/x256pp_c.c diff --git a/prng/src/bench.ml b/prng/src/bench.ml index 1ed8f82..2177d43 100644 --- a/prng/src/bench.ml +++ b/prng/src/bench.ml @@ -5,7 +5,6 @@ module Command = Core.Command module Random_Stdlib = MakeRandom.Basic(Random) module Random_X256pp_Int = MakeRandom.Basic(X256pp.Int) module Random_X256pp_Int64 = MakeRandom.Basic(X256pp.Int64) -module Random_X256pp_C = MakeRandom.Basic(X256pp.C) let bits () = Command.run @@ B.make_command [ @@ -17,10 +16,6 @@ let bits () = X256pp.Int64.bits ()); B.Test.create ~name:"X256pp.Int64.bits62" (fun () -> X256pp.Int64.bits62 ()); - B.Test.create ~name:"X256pp.C.bits" (fun () -> - X256pp.C.bits ()); - B.Test.create ~name:"X256pp.C.bits62" (fun () -> - X256pp.C.bits62 ()); ] let float () = @@ -29,14 +24,10 @@ let float () = Random.float 1.); B.Test.create ~name:"X256pp.Int64.float" (fun () -> X256pp.Int64.float 1.); - B.Test.create ~name:"X256pp.C.float" (fun () -> - X256pp.C.float 1.); B.Test.create ~name:"MakeRandom(X256pp.Int).float" (fun () -> Random_X256pp_Int.float 1.); B.Test.create ~name:"MakeRandom(X256pp.Int64).float" (fun () -> Random_X256pp_Int64.float 1.); - B.Test.create ~name:"MakeRandom(X256pp.C).float" (fun () -> - Random_X256pp_C.float 1.); ] let int64 () = @@ -47,8 +38,6 @@ let int64 () = Random_X256pp_Int.int64 Int64.max_int); B.Test.create ~name:"MakeRandom(X256pp.Int64).int64" (fun () -> Random_X256pp_Int64.int64 Int64.max_int); - B.Test.create ~name:"MakeRandom(X256pp.C).int64" (fun () -> - Random_X256pp_C.int64 Int64.max_int); ] let () = diff --git a/prng/src/splitmix/dune b/prng/src/splitmix/dune index a314c11..72ad3ae 100644 --- a/prng/src/splitmix/dune +++ b/prng/src/splitmix/dune @@ -1,10 +1,3 @@ (library (name splitmix) - (foreign_archives splitmix) - (foreign_stubs (language c) (names stubs)) (libraries makeRandom)) - -(foreign_library - (archive_name splitmix) - (language c) - (names reference)) diff --git a/prng/src/splitmix/reference.ml b/prng/src/splitmix/reference.ml deleted file mode 100644 index 79543d1..0000000 --- a/prng/src/splitmix/reference.ml +++ /dev/null @@ -1,2 +0,0 @@ -external seed: int64 -> unit = "caml_sm64_seed" -external next: unit -> int64 = "caml_sm64_next" diff --git a/prng/src/splitmix/stubs.c b/prng/src/splitmix/stubs.c deleted file mode 100644 index d03b481..0000000 --- a/prng/src/splitmix/stubs.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include "reference.h" - -#define CAML_NAME_SPACE -#include -#include -#include - -value caml_sm64_seed(value seed) { - CAMLparam1(seed); - int64_t x = Int64_val(seed); - // reinterpret x as an unsigned integer - sm64_seed(*(uint64_t*)(&x)); - CAMLreturn(Val_unit); -} - -value caml_sm64_next(value unit) { - CAMLparam1(unit); - uint64_t x = sm64_next(); - // reinterpret x as a signed integer - value res = caml_copy_int64(*(int64_t*)(&x)); - CAMLreturn(res); -} - diff --git a/prng/src/splitmix/tests/dune b/prng/src/splitmix/tests/dune index 0b7f5c0..f60baad 100644 --- a/prng/src/splitmix/tests/dune +++ b/prng/src/splitmix/tests/dune @@ -4,7 +4,7 @@ (rule (target sm64_c.exe) - (deps sm64_c.c ../../testutils/libtestutils.a ../libsplitmix.a) + (deps sm64_c.c reference.c ../../testutils/libtestutils.a) (action (run cc -Wall -Wextra -Wpedantic -o %{target} %{deps}))) diff --git a/prng/src/splitmix/reference.c b/prng/src/splitmix/tests/reference.c similarity index 53% rename from prng/src/splitmix/reference.c rename to prng/src/splitmix/tests/reference.c index f9897ff..18450d3 100644 --- a/prng/src/splitmix/reference.c +++ b/prng/src/splitmix/tests/reference.c @@ -2,20 +2,15 @@ #include -uint64_t sm64_next_stateless(uint64_t* s) { - uint64_t z = (*s += 0x9e3779b97f4a7c15); - z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; - z = (z ^ (z >> 27)) * 0x94d049bb133111eb; - return z ^ (z >> 31); -} - -static uint64_t state; /* The state can be seeded with any value. */ +static uint64_t s; /* The state can be seeded with any value. */ void sm64_seed(uint64_t seed) { - state = seed; + s = seed; } uint64_t sm64_next() { - return sm64_next_stateless(&state); + uint64_t z = (s += 0x9e3779b97f4a7c15); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); } - diff --git a/prng/src/splitmix/reference.h b/prng/src/splitmix/tests/reference.h similarity index 73% rename from prng/src/splitmix/reference.h rename to prng/src/splitmix/tests/reference.h index 311677c..ff5442c 100644 --- a/prng/src/splitmix/reference.h +++ b/prng/src/splitmix/tests/reference.h @@ -3,10 +3,7 @@ #include -uint64_t sm64_next_stateless(uint64_t*); - void sm64_seed(uint64_t); uint64_t sm64_next(); #endif - diff --git a/prng/src/splitmix/tests/sm64_c.c b/prng/src/splitmix/tests/sm64_c.c index 57d89e2..da4338a 100644 --- a/prng/src/splitmix/tests/sm64_c.c +++ b/prng/src/splitmix/tests/sm64_c.c @@ -1,9 +1,4 @@ -#include -#include -#include -#include - -#include "../reference.h" +#include "reference.h" #include "../../testutils/testutils.h" int main(int argc, char* argv[]) { diff --git a/prng/src/xoshiro/dune b/prng/src/xoshiro/dune index ef6506c..fa217cc 100644 --- a/prng/src/xoshiro/dune +++ b/prng/src/xoshiro/dune @@ -1,10 +1,3 @@ (library (name xoshiro) - (foreign_archives xoshiro) - (foreign_stubs (language c) (names stubs))) - -(foreign_library - (archive_name xoshiro) - (language c) - (flags -lsplitmix) - (names reference)) + (libraries splitmix)) diff --git a/prng/src/xoshiro/reference.ml b/prng/src/xoshiro/reference.ml deleted file mode 100644 index ee7ef1e..0000000 --- a/prng/src/xoshiro/reference.ml +++ /dev/null @@ -1,2 +0,0 @@ -external seed: int64 -> unit = "caml_x256pp_seed" -external next: unit -> int64 = "caml_x256pp_next" diff --git a/prng/src/xoshiro/stubs.c b/prng/src/xoshiro/stubs.c deleted file mode 100644 index ab46b1a..0000000 --- a/prng/src/xoshiro/stubs.c +++ /dev/null @@ -1,42 +0,0 @@ -#define CAML_NAME_SPACE -#include -#include -#include -#include -#include "reference.h" - -value caml_x256pp_seed(value seed) { - CAMLparam1(seed); - int64_t x = Int64_val(seed); - // reinterpret x as an unsigned integer - x256pp_seed(*(uint64_t*)(&x)); - CAMLreturn(Val_unit); -} - -value caml_x256pp_next(value unit) { - CAMLparam1(unit); - uint64_t x = x256pp_next(); - // reinterpret x as a signed integer - value res = caml_copy_int64(*(int64_t*)(&x)); - CAMLreturn(res); -} - -value caml_x256pp_bits(value unit) { - CAMLparam1(unit); - intnat res = x256pp_next() >> 34; - CAMLreturn(Val_long(res)); -} - -value caml_x256pp_bits62(value unit) { - CAMLparam1(unit); - long res = x256pp_next() >> 2; - CAMLreturn(Val_long(res)); -} - -value caml_x256pp_double(value f) { - CAMLparam1(f); - double res = (x256pp_next() >> 11) * 0x1.0p-53; - res *= Double_val(f); - CAMLreturn(caml_copy_double(res)); -} - diff --git a/prng/src/xoshiro/tests/dune b/prng/src/xoshiro/tests/dune index b7aba12..6a9ddd5 100644 --- a/prng/src/xoshiro/tests/dune +++ b/prng/src/xoshiro/tests/dune @@ -1,3 +1,17 @@ -(tests +(executables (names int64 int) - (libraries splitmix xoshiro)) + (libraries xoshiro testutils splitmix)) + +(rule + (target x256pp_c.exe) + (deps x256pp_c.c reference.c ../../testutils/libtestutils.a) + (action + (run cc -Wall -Wextra -Wpedantic -o %{target} %{deps}))) + + +(rule (with-stdout-to x256pp_int.output (run ./int.exe --nb=1000))) +(rule (with-stdout-to x256pp_int64.output (run ./int64.exe --nb=1000))) +(rule (with-stdout-to x256pp_c.output (run ./x256pp_c.exe --nb=1000))) + +(rule (alias runtest) (action (diff x256pp_int.output x256pp_c.output))) +(rule (alias runtest) (action (diff x256pp_int64.output x256pp_c.output))) diff --git a/prng/src/xoshiro/tests/int.ml b/prng/src/xoshiro/tests/int.ml index 52460b0..dbe2568 100644 --- a/prng/src/xoshiro/tests/int.ml +++ b/prng/src/xoshiro/tests/int.ml @@ -10,11 +10,6 @@ let int64_to_int_int n = let lo = to_int (logand n 0xFFFFFFFFL) in (hi, lo) -let test_compare nb f g = - for _ = 1 to nb do - assert (f () = g ()) - done - let ocaml_seed seed = let s = ref seed in for i = 0 to 3 do @@ -24,13 +19,6 @@ let ocaml_seed seed = done let () = - (* Chosen by fair dice rool. Guaranted to be random. *) - (* https://xkcd.com/221/ *) - let seed = 0xc6bb1e08d1a33f2fL in - Reference.seed seed; - ocaml_seed seed; - test_compare - 1_000_000 - Reference.next - (fun () -> - X256pp.Int.next () |> int_int_to_int64) + Testutils.run + ~seed_f:ocaml_seed + ~next:(fun () -> X256pp.Int.next () |> int_int_to_int64) diff --git a/prng/src/xoshiro/tests/int64.ml b/prng/src/xoshiro/tests/int64.ml index 4a9a9de..cf247e8 100644 --- a/prng/src/xoshiro/tests/int64.ml +++ b/prng/src/xoshiro/tests/int64.ml @@ -1,20 +1,10 @@ open Xoshiro -let test_compare nb f g = - for _ = 1 to nb do - assert (f () = g ()) - done - -let ocaml_seed seed = - let s = ref seed in - for i = 0 to 3 do - X256pp.Int64.s.(i) <- Splitmix.Sm64.next s - done - let () = - (* Chosen by fair dice rool. Guaranted to be random. *) - (* https://xkcd.com/221/ *) - let seed = 0xc6bb1e08d1a33f2fL in - Reference.seed seed; - ocaml_seed seed; - test_compare 1_000_000 Reference.next X256pp.Int64.next + Testutils.run + ~seed_f:(fun seed -> + let s = ref seed in + for i = 0 to 3 do + X256pp.Int64.s.(i) <- Splitmix.Sm64.next s + done) + ~next:X256pp.Int64.next diff --git a/prng/src/xoshiro/reference.c b/prng/src/xoshiro/tests/reference.c similarity index 80% rename from prng/src/xoshiro/reference.c rename to prng/src/xoshiro/tests/reference.c index 6811f84..0b7c4ed 100644 --- a/prng/src/xoshiro/reference.c +++ b/prng/src/xoshiro/tests/reference.c @@ -1,7 +1,6 @@ // Reference: http://prng.di.unimi.it/xoshiro256plusplus.c #include -#include "../splitmix/reference.h" static inline uint64_t rotl(const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); @@ -31,6 +30,13 @@ uint64_t x256pp_next(void) { return result; } +static uint64_t sm64_next_stateless(uint64_t* s) { + uint64_t z = (*s += 0x9e3779b97f4a7c15); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); +} + void x256pp_seed(uint64_t seed) { uint64_t s = seed; for (int i = 0; i < 4; i++) diff --git a/prng/src/xoshiro/reference.h b/prng/src/xoshiro/tests/reference.h similarity index 100% rename from prng/src/xoshiro/reference.h rename to prng/src/xoshiro/tests/reference.h diff --git a/prng/src/xoshiro/tests/x256pp_c.c b/prng/src/xoshiro/tests/x256pp_c.c new file mode 100644 index 0000000..02b2206 --- /dev/null +++ b/prng/src/xoshiro/tests/x256pp_c.c @@ -0,0 +1,6 @@ +#include "reference.h" +#include "../../testutils/testutils.h" + +int main(int argc, char* argv[]) { + return testutils_run(argc, argv, x256pp_seed, x256pp_next); +} diff --git a/prng/src/xoshiro/x256pp.ml b/prng/src/xoshiro/x256pp.ml index cd69cf7..23dbaca 100644 --- a/prng/src/xoshiro/x256pp.ml +++ b/prng/src/xoshiro/x256pp.ml @@ -162,9 +162,3 @@ module Int64 = struct let float x = Int64.(to_float (shift_right_logical (next ()) 11)) *. 0x1.0p-53 *. x end - -module C = struct - external bits: unit -> int = "caml_x256pp_bits" - external bits62: unit -> int = "caml_x256pp_bits62" - external float: float -> float = "caml_x256pp_double" -end From 822a6e8ece20701d0e111e45b4a9a2dcd0a74ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 18 May 2020 18:05:04 +0200 Subject: [PATCH 21/26] "True" reference implementations --- prng/src/splitmix/tests/dune | 4 +- prng/src/splitmix/tests/reference.c | 31 ++++++++++---- prng/src/splitmix/tests/reference.h | 4 +- prng/src/splitmix/tests/sm64_c.c | 6 +-- prng/src/xoshiro/tests/dune | 4 +- prng/src/xoshiro/tests/reference.c | 65 +++++++++++++++++++---------- prng/src/xoshiro/tests/reference.h | 4 +- prng/src/xoshiro/tests/x256pp_c.c | 6 +-- 8 files changed, 80 insertions(+), 44 deletions(-) diff --git a/prng/src/splitmix/tests/dune b/prng/src/splitmix/tests/dune index f60baad..5c98517 100644 --- a/prng/src/splitmix/tests/dune +++ b/prng/src/splitmix/tests/dune @@ -4,9 +4,9 @@ (rule (target sm64_c.exe) - (deps sm64_c.c reference.c ../../testutils/libtestutils.a) + (deps reference.h (:codedeps sm64_c.c reference.c ../../testutils/libtestutils.a)) (action - (run cc -Wall -Wextra -Wpedantic -o %{target} %{deps}))) + (run cc -Wall -Wextra -Wpedantic -o %{target} %{codedeps}))) (rule (with-stdout-to sm64_ocaml.output (run ./sm64_ocaml.exe --nb=1000))) (rule (with-stdout-to sm64_c.output (run ./sm64_c.exe --nb=1000))) diff --git a/prng/src/splitmix/tests/reference.c b/prng/src/splitmix/tests/reference.c index 18450d3..b99dbeb 100644 --- a/prng/src/splitmix/tests/reference.c +++ b/prng/src/splitmix/tests/reference.c @@ -1,16 +1,33 @@ -// Reference: http://prng.di.unimi.it/splitmix64.c +/* Written in 2015 by Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ #include -static uint64_t s; /* The state can be seeded with any value. */ +/* This is a fixed-increment version of Java 8's SplittableRandom generator + See http://dx.doi.org/10.1145/2714064.2660195 and + http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html -void sm64_seed(uint64_t seed) { - s = seed; -} + It is a very fast generator passing BigCrush, and it can be useful if + for some reason you absolutely want 64 bits of state. */ + +static uint64_t x; /* The state can be seeded with any value. */ -uint64_t sm64_next() { - uint64_t z = (s += 0x9e3779b97f4a7c15); +uint64_t next() { + uint64_t z = (x += 0x9e3779b97f4a7c15); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } + +// -------------------------------- +// Above this line: verbatim copy of http://prng.di.unimi.it/splitmix64.c +// Below: added by us for testing purpose + +void seed(uint64_t s) { + x = s; +} diff --git a/prng/src/splitmix/tests/reference.h b/prng/src/splitmix/tests/reference.h index ff5442c..536a2b9 100644 --- a/prng/src/splitmix/tests/reference.h +++ b/prng/src/splitmix/tests/reference.h @@ -3,7 +3,7 @@ #include -void sm64_seed(uint64_t); -uint64_t sm64_next(); +uint64_t next(); +void seed(uint64_t); #endif diff --git a/prng/src/splitmix/tests/sm64_c.c b/prng/src/splitmix/tests/sm64_c.c index da4338a..24bdbb3 100644 --- a/prng/src/splitmix/tests/sm64_c.c +++ b/prng/src/splitmix/tests/sm64_c.c @@ -1,6 +1,6 @@ -#include "reference.h" -#include "../../testutils/testutils.h" +#include "reference.h" // seed, next +#include "../../testutils/testutils.h" // testutils_run int main(int argc, char* argv[]) { - return testutils_run(argc, argv, sm64_seed, sm64_next); + return testutils_run(argc, argv, seed, next); } diff --git a/prng/src/xoshiro/tests/dune b/prng/src/xoshiro/tests/dune index 6a9ddd5..dce35c4 100644 --- a/prng/src/xoshiro/tests/dune +++ b/prng/src/xoshiro/tests/dune @@ -4,9 +4,9 @@ (rule (target x256pp_c.exe) - (deps x256pp_c.c reference.c ../../testutils/libtestutils.a) + (deps reference.h (:codedeps x256pp_c.c reference.c ../../testutils/libtestutils.a)) (action - (run cc -Wall -Wextra -Wpedantic -o %{target} %{deps}))) + (run cc -Wall -Wextra -Wpedantic -o %{target} %{codedeps}))) (rule (with-stdout-to x256pp_int.output (run ./int.exe --nb=1000))) diff --git a/prng/src/xoshiro/tests/reference.c b/prng/src/xoshiro/tests/reference.c index 0b7c4ed..29d78f8 100644 --- a/prng/src/xoshiro/tests/reference.c +++ b/prng/src/xoshiro/tests/reference.c @@ -1,45 +1,64 @@ -// Reference: http://prng.di.unimi.it/xoshiro256plusplus.c +/* Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ #include +/* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. + It has excellent (sub-ns) speed, a state (256 bits) that is large + enough for any parallel application, and it passes all tests we are + aware of. + + For generating just floating-point numbers, xoshiro256+ is even faster. + + The state must be seeded so that it is not everywhere zero. If you have + a 64-bit seed, we suggest to seed a splitmix64 generator and use its + output to fill s. */ + static inline uint64_t rotl(const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); } -static uint64_t x256pp_state[4] = { - 0xd430ebc2f4d9e7b8, - 0x44815975ddd19d52, - 0xbb18c2b0b18d0bfe, - 0x6c0681407295c644 -}; -uint64_t x256pp_next(void) { - const uint64_t result = rotl(x256pp_state[0] + x256pp_state[3], 23) + x256pp_state[0]; +static uint64_t s[4]; + +uint64_t next(void) { + const uint64_t result = rotl(s[0] + s[3], 23) + s[0]; - const uint64_t t = x256pp_state[1] << 17; + const uint64_t t = s[1] << 17; - x256pp_state[2] ^= x256pp_state[0]; - x256pp_state[3] ^= x256pp_state[1]; - x256pp_state[1] ^= x256pp_state[2]; - x256pp_state[0] ^= x256pp_state[3]; + s[2] ^= s[0]; + s[3] ^= s[1]; + s[1] ^= s[2]; + s[0] ^= s[3]; - x256pp_state[2] ^= t; + s[2] ^= t; - x256pp_state[3] = rotl(x256pp_state[3], 45); + s[3] = rotl(s[3], 45); return result; } -static uint64_t sm64_next_stateless(uint64_t* s) { - uint64_t z = (*s += 0x9e3779b97f4a7c15); +// -------------------------------- +// Above this line: verbatim copy of http://prng.di.unimi.it/xoshiro256plusplus.c +// without the `jump` and `long_jump` functions. +// Below: added by us for testing purpose + +// Use splitmix64 to seed xoshiro as suggested by Vigna. +uint64_t splitmix64_stateless(uint64_t* state) { + uint64_t z = (*state += 0x9e3779b97f4a7c15); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } -void x256pp_seed(uint64_t seed) { - uint64_t s = seed; - for (int i = 0; i < 4; i++) - x256pp_state[i] = sm64_next_stateless(&s); +void seed(uint64_t x) { + uint64_t splitmix_state = x; + for (int i = 0; i < 4; i++) { + s[i] = splitmix64_stateless(&splitmix_state); + } } - diff --git a/prng/src/xoshiro/tests/reference.h b/prng/src/xoshiro/tests/reference.h index fe5ad8f..1995ec0 100644 --- a/prng/src/xoshiro/tests/reference.h +++ b/prng/src/xoshiro/tests/reference.h @@ -3,8 +3,8 @@ #include -uint64_t x256pp_next(); -void x256pp_seed(uint64_t); +uint64_t next(); +void seed(uint64_t); #endif diff --git a/prng/src/xoshiro/tests/x256pp_c.c b/prng/src/xoshiro/tests/x256pp_c.c index 02b2206..24bdbb3 100644 --- a/prng/src/xoshiro/tests/x256pp_c.c +++ b/prng/src/xoshiro/tests/x256pp_c.c @@ -1,6 +1,6 @@ -#include "reference.h" -#include "../../testutils/testutils.h" +#include "reference.h" // seed, next +#include "../../testutils/testutils.h" // testutils_run int main(int argc, char* argv[]) { - return testutils_run(argc, argv, x256pp_seed, x256pp_next); + return testutils_run(argc, argv, seed, next); } From ad76e7ecdfd8e792f43e952cf68fb5eee2fe4ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 18 May 2020 20:38:55 +0200 Subject: [PATCH 22/26] Remove old benchmark --- prng/src/bench.ml | 46 ---------------------------------------------- prng/src/dune | 4 ---- 2 files changed, 50 deletions(-) delete mode 100644 prng/src/bench.ml delete mode 100644 prng/src/dune diff --git a/prng/src/bench.ml b/prng/src/bench.ml deleted file mode 100644 index 2177d43..0000000 --- a/prng/src/bench.ml +++ /dev/null @@ -1,46 +0,0 @@ -open Xoshiro -module B = Core_bench.Bench -module Command = Core.Command - -module Random_Stdlib = MakeRandom.Basic(Random) -module Random_X256pp_Int = MakeRandom.Basic(X256pp.Int) -module Random_X256pp_Int64 = MakeRandom.Basic(X256pp.Int64) - -let bits () = - Command.run @@ B.make_command [ - B.Test.create ~name:"Random.bits" (fun () -> - Random.bits ()); - B.Test.create ~name:"X256pp.Int.bits" (fun () -> - X256pp.Int.bits ()); - B.Test.create ~name:"X256pp.Int64.bits" (fun () -> - X256pp.Int64.bits ()); - B.Test.create ~name:"X256pp.Int64.bits62" (fun () -> - X256pp.Int64.bits62 ()); - ] - -let float () = - Command.run @@ B.make_command [ - B.Test.create ~name:"Random.float" (fun () -> - Random.float 1.); - B.Test.create ~name:"X256pp.Int64.float" (fun () -> - X256pp.Int64.float 1.); - B.Test.create ~name:"MakeRandom(X256pp.Int).float" (fun () -> - Random_X256pp_Int.float 1.); - B.Test.create ~name:"MakeRandom(X256pp.Int64).float" (fun () -> - Random_X256pp_Int64.float 1.); - ] - -let int64 () = - Command.run @@ B.make_command [ - B.Test.create ~name:"MakeRandom(Random).int64" (fun () -> - Random_Stdlib.int64 Int64.max_int); - B.Test.create ~name:"MakeRandom(X256pp.Int).int64" (fun () -> - Random_X256pp_Int.int64 Int64.max_int); - B.Test.create ~name:"MakeRandom(X256pp.Int64).int64" (fun () -> - Random_X256pp_Int64.int64 Int64.max_int); - ] - -let () = - bits (); - float (); - int64 () diff --git a/prng/src/dune b/prng/src/dune deleted file mode 100644 index 094d9f8..0000000 --- a/prng/src/dune +++ /dev/null @@ -1,4 +0,0 @@ -(executable - (name bench) - (libraries xoshiro makeRandom core_bench) - (modules Bench)) From f873f3ea49b3717d01d2bb58da855f8f5b5fff2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Mon, 18 May 2020 20:54:06 +0200 Subject: [PATCH 23/26] Arg.align is nice! --- prng/src/testutils/testutils.ml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/prng/src/testutils/testutils.ml b/prng/src/testutils/testutils.ml index 2e43091..1f1dc5f 100644 --- a/prng/src/testutils/testutils.ml +++ b/prng/src/testutils/testutils.ml @@ -6,13 +6,14 @@ let run ~seed_f ~next = ( "--nb", Arg.Set_int nb_iterations, - " Number of random integers to generate" + "Number of random integers to generate" ); ( "--seed", Arg.String (fun n -> seed := Int64.of_string n), - " 64-bit seed"); - ] in + "64-bit seed"); + ] |> Arg.align + in let usage = Format.sprintf "%s [--seed=SEED] [--nb=N]" Sys.argv.(0) From 0e6951b2554c061441185ca530f7946109dcc763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 24 May 2020 17:30:20 +0200 Subject: [PATCH 24/26] Cleaner dune files --- dune | 2 ++ prng/src/splitmix/tests/dune | 4 ++-- prng/src/testutils/dune | 1 - prng/src/xoshiro/tests/dune | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 dune diff --git a/dune b/dune new file mode 100644 index 0000000..11d8778 --- /dev/null +++ b/dune @@ -0,0 +1,2 @@ +(env + (_ (c_flags :standard -Wall -Werror -Wpedantic))) diff --git a/prng/src/splitmix/tests/dune b/prng/src/splitmix/tests/dune index 5c98517..58d28fa 100644 --- a/prng/src/splitmix/tests/dune +++ b/prng/src/splitmix/tests/dune @@ -4,9 +4,9 @@ (rule (target sm64_c.exe) - (deps reference.h (:codedeps sm64_c.c reference.c ../../testutils/libtestutils.a)) + (deps reference.h (:codedeps sm64_c.c reference.c %{lib-private:testutils:libtestutils.a})) (action - (run cc -Wall -Wextra -Wpedantic -o %{target} %{codedeps}))) + (run %{cc} -o %{target} %{codedeps}))) (rule (with-stdout-to sm64_ocaml.output (run ./sm64_ocaml.exe --nb=1000))) (rule (with-stdout-to sm64_c.output (run ./sm64_c.exe --nb=1000))) diff --git a/prng/src/testutils/dune b/prng/src/testutils/dune index f7f5dce..ef7ee52 100644 --- a/prng/src/testutils/dune +++ b/prng/src/testutils/dune @@ -1,7 +1,6 @@ (foreign_library (archive_name testutils) (language c) - (flags -Wall -Wextra -Wpedantic) (names testutils)) (library diff --git a/prng/src/xoshiro/tests/dune b/prng/src/xoshiro/tests/dune index dce35c4..9794944 100644 --- a/prng/src/xoshiro/tests/dune +++ b/prng/src/xoshiro/tests/dune @@ -4,9 +4,9 @@ (rule (target x256pp_c.exe) - (deps reference.h (:codedeps x256pp_c.c reference.c ../../testutils/libtestutils.a)) + (deps reference.h (:codedeps x256pp_c.c reference.c %{lib-private:testutils:libtestutils.a})) (action - (run cc -Wall -Wextra -Wpedantic -o %{target} %{codedeps}))) + (run %{cc} -o %{target} %{codedeps}))) (rule (with-stdout-to x256pp_int.output (run ./int.exe --nb=1000))) From 096214d93f3141c3615c963cd26dc910d33a402d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 24 May 2020 17:30:42 +0200 Subject: [PATCH 25/26] Remove some warnings --- testu01/testu01/bbattery_stubs.c | 11 ++++++----- testu01/testu01/unif01_stubs.h | 2 -- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/testu01/testu01/bbattery_stubs.c b/testu01/testu01/bbattery_stubs.c index acc5ab7..3e6f627 100644 --- a/testu01/testu01/bbattery_stubs.c +++ b/testu01/testu01/bbattery_stubs.c @@ -37,7 +37,7 @@ value caml_bbattery_SmallCrush(value bgen) { value caml_bbattery_SmallCrushFile(value filename) { CAMLparam1(filename); - bbattery_SmallCrushFile(Bytes_val(filename)); + bbattery_SmallCrushFile((char*)Bytes_val(filename)); CAMLreturn(Val_unit); } @@ -97,7 +97,7 @@ value caml_bbattery_Rabbit(value bgen, value nb) { value caml_bbattery_RabbitFile(value filename, value nb) { CAMLparam2(filename, nb); - bbattery_RabbitFile (Bytes_val(filename), Double_val(nb)); + bbattery_RabbitFile ((char*)Bytes_val(filename), Double_val(nb)); CAMLreturn(Val_unit); } @@ -121,7 +121,7 @@ value caml_bbattery_Alphabit(value bgen, value nb, value r, value s) { value caml_bbattery_AlphabitFile(value filename, value nb) { CAMLparam2(filename, nb); - bbattery_AlphabitFile (Bytes_val(filename), Double_val(nb)); + bbattery_AlphabitFile ((char*)Bytes_val(filename), Double_val(nb)); CAMLreturn(Val_unit); } @@ -143,7 +143,7 @@ value caml_bbattery_BlockAlphabit(value bgen, value nb, value r, value s) { value caml_bbattery_BlockAlphabitFile(value filename, value nb) { CAMLparam2(filename, nb); - bbattery_BlockAlphabitFile (Bytes_val(filename), Double_val(nb)); + bbattery_BlockAlphabitFile ((char*)Bytes_val(filename), Double_val(nb)); CAMLreturn(Val_unit); } @@ -156,6 +156,7 @@ value caml_bbattery_RepeatBlockAlphabit(value bgen, value nb, value r, value s, CAMLreturn(Val_unit); } value camlbytecode_bbattery_RepeatBlockAlphabit(value * argv, int argn) { + IGNORE(argn); return caml_bbattery_RepeatBlockAlphabit(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); } @@ -179,6 +180,6 @@ value caml_bbattery_FIPS_140_2(value bgen) { value caml_bbattery_FIPS_140_2File(value filename) { CAMLparam1(filename); - bbattery_FIPS_140_2File(Bytes_val(filename)); + bbattery_FIPS_140_2File((char*)Bytes_val(filename)); CAMLreturn(Val_unit); } diff --git a/testu01/testu01/unif01_stubs.h b/testu01/testu01/unif01_stubs.h index 09e07a0..91af9f7 100644 --- a/testu01/testu01/unif01_stubs.h +++ b/testu01/testu01/unif01_stubs.h @@ -12,8 +12,6 @@ #include -static struct custom_operations unif01_Gen_boxed; - #define IGNORE(x) (void)(x) #define unif01_Gen_unbox(bgen) (* (unif01_Gen**) Data_custom_val(bgen)) From 2e264aad72e776d4080696962af4c7992583de6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 24 May 2020 17:40:18 +0200 Subject: [PATCH 26/26] More C compiler warnings --- dune | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dune b/dune index 11d8778..75bd9a5 100644 --- a/dune +++ b/dune @@ -1,2 +1,2 @@ (env - (_ (c_flags :standard -Wall -Werror -Wpedantic))) + (_ (c_flags :standard -Werror -Wall -Wextra -pedantic)))