-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reimplementation of Random using an LXM pseudo-random number generator #10742
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ca278b
Remove tests/lib-random/full_int.ml
xavierleroy 1e9ec33
Reimplementation of Random stdlib module based on L64X128 from the LX…
xavierleroy bfb2659
Update reference test outputs after change of PRNG
xavierleroy f2ebc04
Address review comments
xavierleroy 82547ea
Update documentation of module Random
xavierleroy b3dc20c
Changes entry for #10742
xavierleroy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/**************************************************************************/ | ||
/* */ | ||
/* 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. */ | ||
/* */ | ||
/**************************************************************************/ | ||
|
||
#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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the paper uses |
||
/* 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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the mixing function (lea64) should be mentioned as well. (The LXM authors describe "better mixing functions" as interesting future work, so it may be that in ten years what people understand as "The L64X128 member" is not that one anymore.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reference to figure 1 in the paper makes it clear which variant is being implemented.