Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerl13 committed May 15, 2020
1 parent 640b0a2 commit adc429d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/splitmix/dune
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 12 additions & 4 deletions src/splitmix/reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

#include <stdint.h>

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);
}

4 changes: 3 additions & 1 deletion src/splitmix/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <stdint.h>

uint64_t sm64_state;
uint64_t sm64_next_stateless(uint64_t*);

void sm64_seed(uint64_t);
uint64_t sm64_next();

#endif
Expand Down
7 changes: 4 additions & 3 deletions src/splitmix/stubs.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include <stdint.h>
#include "reference.h"

#define CAML_NAME_SPACE
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <stdint.h>
#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);
}

Expand Down
9 changes: 8 additions & 1 deletion src/xoshiro/dune
Original file line number Diff line number Diff line change
@@ -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))
10 changes: 2 additions & 8 deletions src/xoshiro/reference.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Reference: http://prng.di.unimi.it/xoshiro256plusplus.c

#include <stdint.h>
#include "../splitmix/reference.h"

static inline uint64_t rotl(const uint64_t x, int k) {
return (x << k) | (x >> (64 - k));
Expand Down Expand Up @@ -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);
}

0 comments on commit adc429d

Please sign in to comment.