Skip to content

Commit 9f18958

Browse files
authored
flambda-backend: Backport 5 PRNG (#2069)
* upgrade runtime4 to 5 prng * update tests
1 parent fa7e3e2 commit 9f18958

File tree

15 files changed

+553
-339
lines changed

15 files changed

+553
-339
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ runtime4_COMMON_C_SOURCES = \
646646
obj \
647647
parsing \
648648
printexc \
649+
prng \
649650
signals \
650651
simd \
651652
skiplist \

boot/ocamlc

19.1 KB
Binary file not shown.

boot/ocamllex

12.7 KB
Binary file not shown.

ocamltest/.depend

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@ ocaml_flags.cmi : \
260260
environments.cmi
261261
ocaml_modifiers.cmo : \
262262
ocamltest_stdlib.cmi \
263+
ocamltest_config.cmi \
263264
ocaml_variables.cmi \
264265
ocaml_directories.cmi \
265266
environments.cmi \
266267
ocaml_modifiers.cmi
267268
ocaml_modifiers.cmx : \
268269
ocamltest_stdlib.cmx \
270+
ocamltest_config.cmx \
269271
ocaml_variables.cmx \
270272
ocaml_directories.cmx \
271273
environments.cmx \

otherlibs/systhreads4/.depend

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
1-
condition.cmo : \
2-
mutex.cmi \
3-
condition.cmi
4-
condition.cmx : \
5-
mutex.cmx \
6-
condition.cmi
7-
condition.cmi : \
8-
mutex.cmi
91
event.cmo : \
10-
mutex.cmi \
11-
condition.cmi \
122
event.cmi
133
event.cmx : \
14-
mutex.cmx \
15-
condition.cmx \
164
event.cmi
175
event.cmi :
18-
mutex.cmo : \
19-
mutex.cmi
20-
mutex.cmx : \
21-
mutex.cmi
22-
mutex.cmi :
23-
semaphore.cmo : \
24-
mutex.cmi \
25-
condition.cmi \
26-
semaphore.cmi
27-
semaphore.cmx : \
28-
mutex.cmx \
29-
condition.cmx \
30-
semaphore.cmi
31-
semaphore.cmi :
326
thread.cmo : \
337
thread.cmi
348
thread.cmx : \

runtime4/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ include $(ROOTDIR)/Makefile.common
2222
BYTECODE_C_SOURCES := $(addsuffix .c, \
2323
interp misc stacks fix_code startup_aux startup_byt freelist major_gc \
2424
minor_gc memory alloc roots_byt globroots fail_byt signals \
25-
signals_byt printexc backtrace_byt backtrace compare ints eventlog \
25+
signals_byt printexc backtrace_byt backtrace compare ints eventlog prng \
2626
floats simd str array io extern intern hash sys meta parsing gc_ctrl md5 obj \
2727
lexing callback debugger weak compact finalise custom dynlink \
2828
afl $(UNIX_OR_WIN32) bigarray main memprof domain \
@@ -31,7 +31,7 @@ BYTECODE_C_SOURCES := $(addsuffix .c, \
3131
NATIVE_C_SOURCES := $(addsuffix .c, \
3232
startup_aux startup_nat main fail_nat roots_nat signals \
3333
signals_nat misc freelist major_gc minor_gc memory alloc compare ints \
34-
floats simd str array io extern intern hash sys parsing gc_ctrl eventlog md5 obj \
34+
floats simd str array io extern intern hash sys parsing gc_ctrl eventlog prng md5 obj \
3535
lexing $(UNIX_OR_WIN32) printexc callback weak compact finalise custom \
3636
globroots backtrace_nat backtrace dynlink_nat debugger meta \
3737
dynlink clambda_checks afl bigarray \

runtime4/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
callback.c weak.c
2424
finalise.c stacks.c dynlink.c backtrace_byt.c backtrace.c
2525
afl.c
26-
bigarray.c eventlog.c misc.c domain.c)
26+
bigarray.c prng.c eventlog.c misc.c domain.c)
2727
(action (with-stdout-to %{targets} (run %{dep:gen_primitives.sh}))))
2828

2929
(rule

runtime4/gen_primitives.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export LC_ALL=C
2525
alloc array compare extern floats gc_ctrl hash intern interp ints io \
2626
lexing md5 meta memprof obj parsing signals str sys callback weak \
2727
finalise stacks dynlink backtrace_byt backtrace afl \
28-
bigarray eventlog misc domain
28+
bigarray eventlog misc domain prng
2929
do
3030
sed -n -e 's/^CAMLprim value \([a-z0-9_][a-z0-9_]*\).*/\1/p' "$prim.c"
3131
done

runtime4/prng.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**************************************************************************/
2+
/* */
3+
/* OCaml */
4+
/* */
5+
/* Xavier Leroy, projet Cambium, College de France and Inria */
6+
/* */
7+
/* Copyright 2021 Institut National de Recherche en Informatique et */
8+
/* en Automatique. */
9+
/* */
10+
/* All rights reserved. This file is distributed under the terms of */
11+
/* the GNU Lesser General Public License version 2.1, with the */
12+
/* special exception on linking described in the file LICENSE. */
13+
/* */
14+
/**************************************************************************/
15+
16+
#define CAML_INTERNALS
17+
18+
#include <string.h>
19+
#include "caml/alloc.h"
20+
#include "caml/bigarray.h"
21+
#include "caml/mlvalues.h"
22+
23+
/* The L64X128 member of the LXM family. Taken from figure 1 in
24+
"LXM: Better Splittable Pseudorandom Number Generators
25+
(and Almost as Fast)" by Guy L. Steele Jr. and Sebastiano Vigna,
26+
OOPSLA 2021. */
27+
28+
static const uint64_t M = 0xd1342543de82ef95;
29+
30+
struct LXM_state {
31+
uint64_t a; /* per-instance additive parameter (odd) */
32+
uint64_t s; /* state of the LCG subgenerator */
33+
uint64_t x[2]; /* state of the XBG subgenerator (not 0) */
34+
};
35+
36+
/* In OCaml, states are represented as a 1D big array of 64-bit integers */
37+
38+
#define LXM_val(v) ((struct LXM_state *) Caml_ba_data_val(v))
39+
40+
Caml_inline uint64_t rotl(const uint64_t x, int k) {
41+
return (x << k) | (x >> (64 - k));
42+
}
43+
44+
CAMLprim uint64_t caml_lxm_next_unboxed(value v)
45+
{
46+
uint64_t z, q0, q1;
47+
struct LXM_state * st = LXM_val(v);
48+
49+
/* Combining operation */
50+
z = st->s + st->x[0];
51+
/* Mixing function */
52+
z = (z ^ (z >> 32)) * 0xdaba0b6eb09322e3;
53+
z = (z ^ (z >> 32)) * 0xdaba0b6eb09322e3;
54+
z = (z ^ (z >> 32));
55+
/* LCG update */
56+
st->s = st->s * M + st->a;
57+
/* XBG update */
58+
q0 = st->x[0]; q1 = st->x[1];
59+
q1 ^= q0;
60+
q0 = rotl(q0, 24);
61+
q0 = q0 ^ q1 ^ (q1 << 16);
62+
q1 = rotl(q1, 37);
63+
st->x[0] = q0; st->x[1] = q1;
64+
/* Return result */
65+
return z;
66+
}
67+
68+
CAMLprim value caml_lxm_next(value v)
69+
{
70+
return caml_copy_int64(caml_lxm_next_unboxed(v));
71+
}

stdlib/.depend

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,15 @@ stdlib__Complex.cmx : complex.ml \
242242
stdlib__Complex.cmi : complex.mli \
243243
stdlib.cmi
244244
stdlib__Condition.cmo : condition.ml \
245+
stdlib.cmi \
245246
stdlib__Mutex.cmi \
246247
stdlib__Condition.cmi
247248
stdlib__Condition.cmx : condition.ml \
249+
stdlib.cmx \
248250
stdlib__Mutex.cmx \
249251
stdlib__Condition.cmi
250252
stdlib__Condition.cmi : condition.mli \
253+
stdlib.cmi \
251254
stdlib__Mutex.cmi
252255
stdlib__Digest.cmo : digest.ml \
253256
stdlib__String.cmi \
@@ -751,25 +754,29 @@ stdlib__Queue.cmi : queue.mli \
751754
stdlib.cmi \
752755
stdlib__Seq.cmi
753756
stdlib__Random.cmo : random.ml \
757+
stdlib__Sys.cmi \
754758
stdlib__String.cmi \
755759
stdlib.cmi \
756760
stdlib__Nativeint.cmi \
757761
stdlib__Int64.cmi \
758762
stdlib__Int32.cmi \
759-
stdlib__Int.cmi \
763+
stdlib__Domain.cmi \
760764
stdlib__Digest.cmi \
761-
stdlib__Char.cmi \
765+
stdlib__Bytes.cmi \
766+
stdlib__Bigarray.cmi \
762767
stdlib__Array.cmi \
763768
stdlib__Random.cmi
764769
stdlib__Random.cmx : random.ml \
770+
stdlib__Sys.cmx \
765771
stdlib__String.cmx \
766772
stdlib.cmx \
767773
stdlib__Nativeint.cmx \
768774
stdlib__Int64.cmx \
769775
stdlib__Int32.cmx \
770-
stdlib__Int.cmx \
776+
stdlib__Domain.cmx \
771777
stdlib__Digest.cmx \
772-
stdlib__Char.cmx \
778+
stdlib__Bytes.cmx \
779+
stdlib__Bigarray.cmx \
773780
stdlib__Array.cmx \
774781
stdlib__Random.cmi
775782
stdlib__Random.cmi : random.mli \
@@ -811,14 +818,17 @@ stdlib__Scanf.cmx : scanf.ml \
811818
stdlib__Scanf.cmi : scanf.mli \
812819
stdlib.cmi
813820
stdlib__Semaphore.cmo : semaphore.ml \
821+
stdlib.cmi \
814822
stdlib__Mutex.cmi \
815823
stdlib__Condition.cmi \
816824
stdlib__Semaphore.cmi
817825
stdlib__Semaphore.cmx : semaphore.ml \
826+
stdlib.cmx \
818827
stdlib__Mutex.cmx \
819828
stdlib__Condition.cmx \
820829
stdlib__Semaphore.cmi
821-
stdlib__Semaphore.cmi : semaphore.mli
830+
stdlib__Semaphore.cmi : semaphore.mli \
831+
stdlib.cmi
822832
stdlib__Seq.cmo : seq.ml \
823833
stdlib.cmi \
824834
stdlib__Lazy.cmi \

0 commit comments

Comments
 (0)