Skip to content

Commit

Permalink
flambda-backend: Backport 5 PRNG (#2069)
Browse files Browse the repository at this point in the history
* upgrade runtime4 to 5 prng

* update tests
  • Loading branch information
TheNumbat authored Nov 28, 2023
1 parent fa7e3e2 commit 9f18958
Show file tree
Hide file tree
Showing 15 changed files with 553 additions and 339 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ runtime4_COMMON_C_SOURCES = \
obj \
parsing \
printexc \
prng \
signals \
simd \
skiplist \
Expand Down
Binary file modified boot/ocamlc
Binary file not shown.
Binary file modified boot/ocamllex
Binary file not shown.
2 changes: 2 additions & 0 deletions ocamltest/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ ocaml_flags.cmi : \
environments.cmi
ocaml_modifiers.cmo : \
ocamltest_stdlib.cmi \
ocamltest_config.cmi \
ocaml_variables.cmi \
ocaml_directories.cmi \
environments.cmi \
ocaml_modifiers.cmi
ocaml_modifiers.cmx : \
ocamltest_stdlib.cmx \
ocamltest_config.cmx \
ocaml_variables.cmx \
ocaml_directories.cmx \
environments.cmx \
Expand Down
26 changes: 0 additions & 26 deletions otherlibs/systhreads4/.depend
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
condition.cmo : \
mutex.cmi \
condition.cmi
condition.cmx : \
mutex.cmx \
condition.cmi
condition.cmi : \
mutex.cmi
event.cmo : \
mutex.cmi \
condition.cmi \
event.cmi
event.cmx : \
mutex.cmx \
condition.cmx \
event.cmi
event.cmi :
mutex.cmo : \
mutex.cmi
mutex.cmx : \
mutex.cmi
mutex.cmi :
semaphore.cmo : \
mutex.cmi \
condition.cmi \
semaphore.cmi
semaphore.cmx : \
mutex.cmx \
condition.cmx \
semaphore.cmi
semaphore.cmi :
thread.cmo : \
thread.cmi
thread.cmx : \
Expand Down
4 changes: 2 additions & 2 deletions runtime4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ include $(ROOTDIR)/Makefile.common
BYTECODE_C_SOURCES := $(addsuffix .c, \
interp misc stacks fix_code startup_aux startup_byt freelist major_gc \
minor_gc memory alloc roots_byt globroots fail_byt signals \
signals_byt printexc backtrace_byt backtrace compare ints eventlog \
signals_byt printexc backtrace_byt backtrace compare ints eventlog prng \
floats simd str array io extern intern hash sys meta parsing gc_ctrl md5 obj \
lexing callback debugger weak compact finalise custom dynlink \
afl $(UNIX_OR_WIN32) bigarray main memprof domain \
Expand All @@ -31,7 +31,7 @@ BYTECODE_C_SOURCES := $(addsuffix .c, \
NATIVE_C_SOURCES := $(addsuffix .c, \
startup_aux startup_nat main fail_nat roots_nat signals \
signals_nat misc freelist major_gc minor_gc memory alloc compare ints \
floats simd str array io extern intern hash sys parsing gc_ctrl eventlog md5 obj \
floats simd str array io extern intern hash sys parsing gc_ctrl eventlog prng md5 obj \
lexing $(UNIX_OR_WIN32) printexc callback weak compact finalise custom \
globroots backtrace_nat backtrace dynlink_nat debugger meta \
dynlink clambda_checks afl bigarray \
Expand Down
2 changes: 1 addition & 1 deletion runtime4/dune
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
callback.c weak.c
finalise.c stacks.c dynlink.c backtrace_byt.c backtrace.c
afl.c
bigarray.c eventlog.c misc.c domain.c)
bigarray.c prng.c eventlog.c misc.c domain.c)
(action (with-stdout-to %{targets} (run %{dep:gen_primitives.sh}))))

(rule
Expand Down
2 changes: 1 addition & 1 deletion runtime4/gen_primitives.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export LC_ALL=C
alloc array compare extern floats gc_ctrl hash intern interp ints io \
lexing md5 meta memprof obj parsing signals str sys callback weak \
finalise stacks dynlink backtrace_byt backtrace afl \
bigarray eventlog misc domain
bigarray eventlog misc domain prng
do
sed -n -e 's/^CAMLprim value \([a-z0-9_][a-z0-9_]*\).*/\1/p' "$prim.c"
done
Expand Down
71 changes: 71 additions & 0 deletions runtime4/prng.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cambium, College de France and Inria */
/* */
/* Copyright 2021 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/

#define CAML_INTERNALS

#include <string.h>
#include "caml/alloc.h"
#include "caml/bigarray.h"
#include "caml/mlvalues.h"

/* The L64X128 member of the LXM family. Taken from figure 1 in
"LXM: Better Splittable Pseudorandom Number Generators
(and Almost as Fast)" by Guy L. Steele Jr. and Sebastiano Vigna,
OOPSLA 2021. */

static const uint64_t M = 0xd1342543de82ef95;

struct LXM_state {
uint64_t a; /* per-instance additive parameter (odd) */
uint64_t s; /* state of the LCG subgenerator */
uint64_t x[2]; /* state of the XBG subgenerator (not 0) */
};

/* In OCaml, states are represented as a 1D big array of 64-bit integers */

#define LXM_val(v) ((struct LXM_state *) Caml_ba_data_val(v))

Caml_inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
}

CAMLprim uint64_t caml_lxm_next_unboxed(value v)
{
uint64_t z, q0, q1;
struct LXM_state * st = LXM_val(v);

/* Combining operation */
z = st->s + st->x[0];
/* Mixing function */
z = (z ^ (z >> 32)) * 0xdaba0b6eb09322e3;
z = (z ^ (z >> 32)) * 0xdaba0b6eb09322e3;
z = (z ^ (z >> 32));
/* LCG update */
st->s = st->s * M + st->a;
/* XBG update */
q0 = st->x[0]; q1 = st->x[1];
q1 ^= q0;
q0 = rotl(q0, 24);
q0 = q0 ^ q1 ^ (q1 << 16);
q1 = rotl(q1, 37);
st->x[0] = q0; st->x[1] = q1;
/* Return result */
return z;
}

CAMLprim value caml_lxm_next(value v)
{
return caml_copy_int64(caml_lxm_next_unboxed(v));
}
20 changes: 15 additions & 5 deletions stdlib/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,15 @@ stdlib__Complex.cmx : complex.ml \
stdlib__Complex.cmi : complex.mli \
stdlib.cmi
stdlib__Condition.cmo : condition.ml \
stdlib.cmi \
stdlib__Mutex.cmi \
stdlib__Condition.cmi
stdlib__Condition.cmx : condition.ml \
stdlib.cmx \
stdlib__Mutex.cmx \
stdlib__Condition.cmi
stdlib__Condition.cmi : condition.mli \
stdlib.cmi \
stdlib__Mutex.cmi
stdlib__Digest.cmo : digest.ml \
stdlib__String.cmi \
Expand Down Expand Up @@ -751,25 +754,29 @@ stdlib__Queue.cmi : queue.mli \
stdlib.cmi \
stdlib__Seq.cmi
stdlib__Random.cmo : random.ml \
stdlib__Sys.cmi \
stdlib__String.cmi \
stdlib.cmi \
stdlib__Nativeint.cmi \
stdlib__Int64.cmi \
stdlib__Int32.cmi \
stdlib__Int.cmi \
stdlib__Domain.cmi \
stdlib__Digest.cmi \
stdlib__Char.cmi \
stdlib__Bytes.cmi \
stdlib__Bigarray.cmi \
stdlib__Array.cmi \
stdlib__Random.cmi
stdlib__Random.cmx : random.ml \
stdlib__Sys.cmx \
stdlib__String.cmx \
stdlib.cmx \
stdlib__Nativeint.cmx \
stdlib__Int64.cmx \
stdlib__Int32.cmx \
stdlib__Int.cmx \
stdlib__Domain.cmx \
stdlib__Digest.cmx \
stdlib__Char.cmx \
stdlib__Bytes.cmx \
stdlib__Bigarray.cmx \
stdlib__Array.cmx \
stdlib__Random.cmi
stdlib__Random.cmi : random.mli \
Expand Down Expand Up @@ -811,14 +818,17 @@ stdlib__Scanf.cmx : scanf.ml \
stdlib__Scanf.cmi : scanf.mli \
stdlib.cmi
stdlib__Semaphore.cmo : semaphore.ml \
stdlib.cmi \
stdlib__Mutex.cmi \
stdlib__Condition.cmi \
stdlib__Semaphore.cmi
stdlib__Semaphore.cmx : semaphore.ml \
stdlib.cmx \
stdlib__Mutex.cmx \
stdlib__Condition.cmx \
stdlib__Semaphore.cmi
stdlib__Semaphore.cmi : semaphore.mli
stdlib__Semaphore.cmi : semaphore.mli \
stdlib.cmi
stdlib__Seq.cmo : seq.ml \
stdlib.cmi \
stdlib__Lazy.cmi \
Expand Down
Loading

0 comments on commit 9f18958

Please sign in to comment.