no more repeating code in xoshiro/xoroshiro
This commit is contained in:
parent
f33804f5b8
commit
66935ae437
@ -5,82 +5,52 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "../base/std.h"
|
#include "../base/std.h"
|
||||||
|
#include "splitmix64/splitmix64.h"
|
||||||
|
#include "xoroshiro/xoroshiro.h"
|
||||||
|
#include "xoshiro/xoshiro.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
You can choose any algorithm that has required functions:
|
You can choose any algorithm that has required functions:
|
||||||
|
|
||||||
some_alg64_state some_alg64_init(uint64 seed);
|
some_alg32_state some_alg32_init(uint32 seed);
|
||||||
uint64 some_alg64_next(some_alg64_state);
|
uint32 some_alg32_next(some_alg32_state);
|
||||||
|
|
||||||
#define KRAND_ALG64 some_alg64
|
#define KRAND_ALG32_next some_alg32_next
|
||||||
|
#define KRAND_ALG32_init some_alg32_init
|
||||||
#include "kerep/random/krandom.h"
|
#include "kerep/random/krandom.h"
|
||||||
|
|
||||||
The same way it works for 32-bit RNGs:
|
The same way it works for 64-bit RNGs
|
||||||
|
|
||||||
some_alg64_state some_alg32_init(uint32 seed);
|
|
||||||
uint32 some_alg32_next(some_alg64_state);
|
|
||||||
|
|
||||||
#define KRAND_ALG32 some_alg32
|
|
||||||
#include "kerep/random/krandom.h"
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// default rng_next function
|
// default rng_next function
|
||||||
#ifndef KRAND_ALG32
|
#ifndef KRAND_ALG32_next
|
||||||
#define KRAND_ALG32 xoroshiro128plus
|
#define KRAND_ALG32_next xoshiro128plus##_next
|
||||||
#endif
|
#endif
|
||||||
#ifndef KRAND_ALG64
|
#ifndef KRAND_ALG32_init
|
||||||
#define KRAND_ALG64 xoshiro128plus
|
#define KRAND_ALG32_init xoshiro128plus##_init
|
||||||
|
#endif
|
||||||
|
#ifndef KRAND_ALG64_next
|
||||||
|
#define KRAND_ALG64_next xoshiro256plus##_next
|
||||||
|
#endif
|
||||||
|
#ifndef KRAND_ALG64_init
|
||||||
|
#define KRAND_ALG64_init xoshiro256plus##_init
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef void* KRAND_ALG32_state;
|
typedef void* krand_statePtr;
|
||||||
typedef void* KRAND_ALG64_state;
|
#define KRAND_ALG32_initFromTime xoshiro128plus##_initFromTime
|
||||||
#define KRAND_ALG32_next(STATE) xoshiro128plus##_next(STATE)
|
#define KRAND_ALG64_initFromTime xoshiro256plus##_initFromTime
|
||||||
#define KRAND_ALG32_init(SEED) xoshiro128plus##_init(SEED)
|
|
||||||
#define KRAND_ALG32_initFromTime() xoshiro128plus##_initFromTime()
|
|
||||||
#define KRAND_ALG64_next(STATE) xoshiro256plus##_next(STATE)
|
|
||||||
#define KRAND_ALG64_init(SEED) xoshiro256plus##_init(SEED)
|
|
||||||
#define KRAND_ALG64_initFromTime() xoshiro256plus##_initFromTime()
|
|
||||||
|
|
||||||
|
|
||||||
#define __krand_declare_alg(ALG, VALUE_SIZE)\
|
|
||||||
typedef void* ALG##_state;\
|
|
||||||
ALG##_state ALG##_init(uint64 seed);\
|
|
||||||
static inline ALG##_state ALG##_initFromTime(void) { return ALG##_init(time(NULL)); }\
|
|
||||||
uint##VALUE_SIZE ALG##_next(ALG##_state);
|
|
||||||
|
|
||||||
// different algorithm declarations
|
|
||||||
// for ALG32
|
|
||||||
// xoroshiro64
|
|
||||||
__krand_declare_alg(xoroshiro64star, 32)
|
|
||||||
__krand_declare_alg(xoroshiro64starstar, 32)
|
|
||||||
// xoshiro128
|
|
||||||
__krand_declare_alg(xoshiro128plus, 32)
|
|
||||||
__krand_declare_alg(xoshiro128plusplus, 32)
|
|
||||||
__krand_declare_alg(xoshiro128starstar, 32)
|
|
||||||
// for ALG64
|
|
||||||
// xoroshiro128
|
|
||||||
__krand_declare_alg(xoroshiro128plus, 64)
|
|
||||||
__krand_declare_alg(xoroshiro128plusplus, 64)
|
|
||||||
__krand_declare_alg(xoroshiro128starstar, 64)
|
|
||||||
// xoshiro256
|
|
||||||
__krand_declare_alg(xoshiro256plus, 64)
|
|
||||||
__krand_declare_alg(xoshiro256plusplus, 64)
|
|
||||||
__krand_declare_alg(xoshiro256starstar, 64)
|
|
||||||
// splitmix64
|
|
||||||
__krand_declare_alg(splitmix64, 64)
|
|
||||||
|
|
||||||
|
|
||||||
#define __krand_next_definition(VALUE_SIZE) { return from+KRAND_ALG##VALUE_SIZE##_next(state)%(to-from); }
|
#define __krand_next_definition(VALUE_SIZE) { return from+KRAND_ALG##VALUE_SIZE##_next(state)%(to-from); }
|
||||||
|
|
||||||
// ready-to-use functions
|
// ready-to-use functions
|
||||||
static inline int8 krand_next8 (KRAND_ALG32_state state, int8 from, int8 to) __krand_next_definition(32)
|
static inline int8 krand_next8 (krand_statePtr state, int8 from, int8 to) __krand_next_definition(32)
|
||||||
static inline int16 krand_next16(KRAND_ALG32_state state, int16 from, int16 to) __krand_next_definition(32)
|
static inline int16 krand_next16(krand_statePtr state, int16 from, int16 to) __krand_next_definition(32)
|
||||||
static inline int32 krand_next32(KRAND_ALG32_state state, int32 from, int32 to) __krand_next_definition(32)
|
static inline int32 krand_next32(krand_statePtr state, int32 from, int32 to) __krand_next_definition(32)
|
||||||
static inline int64 krand_next64(KRAND_ALG64_state state, int64 from, int64 to) __krand_next_definition(64)
|
static inline int64 krand_next64(krand_statePtr state, int64 from, int64 to) __krand_next_definition(64)
|
||||||
|
|
||||||
// divides random number by 2^64 to return a value between 0 and 1
|
// divides random number by 2^64 to return a value between 0 and 1
|
||||||
static inline float32 krand_nextFloat32(KRAND_ALG32_state state) {return (uint32)KRAND_ALG32_next(state)/0xffffffff; }
|
static inline float32 krand_nextFloat32(krand_statePtr state) {return (uint32)KRAND_ALG32_next(state)/0xffffffff; }
|
||||||
static inline float64 krand_nextFloat64(KRAND_ALG64_state state) {return KRAND_ALG64_next(state)/0xffffffff; }
|
static inline float64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG64_next(state)/0xffffffff; }
|
||||||
|
|
||||||
|
|
||||||
///@param chance (0-1.0) is probability of success
|
///@param chance (0-1.0) is probability of success
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
#include "krandom.h"
|
#include "splitmix64.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This is a fixed-increment version of Java 8's SplittableRandom generator
|
This is a fixed-increment version of Java 8's SplittableRandom generator
|
||||||
@ -12,16 +12,15 @@ generator.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// The state can be seeded with any (upto) 64 bit integer value.
|
// The state can be seeded with any (upto) 64 bit integer value.
|
||||||
typedef uint64 _state_t;
|
|
||||||
|
|
||||||
void* splitmix64_init(uint64 seed){
|
void* splitmix64_init(uint64 seed){
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
splitmix64_state* state=malloc(sizeof(splitmix64_state));
|
||||||
*state=seed;
|
*state=seed;
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64 splitmix64_next(void* _state) {
|
uint64 splitmix64_next(void* _state) {
|
||||||
_state_t* state=_state;
|
splitmix64_state* state=_state;
|
||||||
// increment the state variable
|
// increment the state variable
|
||||||
*state += 0x9e3779b97f4a7c15;
|
*state += 0x9e3779b97f4a7c15;
|
||||||
// copy the state to a working variable
|
// copy the state to a working variable
|
||||||
19
src/random/splitmix64/splitmix64.h
Normal file
19
src/random/splitmix64/splitmix64.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../../base/base.h"
|
||||||
|
|
||||||
|
typedef uint64 splitmix64_state;
|
||||||
|
typedef void* splitmix64_statePtr;
|
||||||
|
|
||||||
|
splitmix64_statePtr splitmix64_init(uint64 seed);
|
||||||
|
static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); }
|
||||||
|
|
||||||
|
uint64 splitmix64_next(splitmix64_statePtr);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
29
src/random/xoroshiro/32bitValue/xoroshiro64.h
Normal file
29
src/random/xoroshiro/32bitValue/xoroshiro64.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../../../base/std.h"
|
||||||
|
#include "../../splitmix64/splitmix64.h"
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint64 merged;
|
||||||
|
uint32 s[2];
|
||||||
|
} xoroshiro64_state;
|
||||||
|
typedef void* xoroshiro64_statePtr;
|
||||||
|
|
||||||
|
xoroshiro64_statePtr xoroshiro64_init(uint64 seed);
|
||||||
|
#define xoroshiro64star_init xoroshiro64_init
|
||||||
|
#define xoroshiro64starstar_init xoroshiro64_init
|
||||||
|
|
||||||
|
static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoroshiro64_init(time(NULL)); }
|
||||||
|
#define xoroshiro64star_initFromTime xoroshiro64_initFromTime
|
||||||
|
#define xoroshiro64starstar_initFromTime xoroshiro64_initFromTime
|
||||||
|
|
||||||
|
uint32 xoroshiro64star_next(xoroshiro64_statePtr);
|
||||||
|
uint32 xoroshiro64starstar_next(xoroshiro64_statePtr);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoroshiro64.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This is xoroshiro64* 1.0, our best and fastest 32-bit small-state
|
This is xoroshiro64* 1.0, our best and fastest 32-bit small-state
|
||||||
@ -27,13 +27,8 @@ static inline uint32 rotl(const uint32 x, int k) {
|
|||||||
return (x << k) | (x >> (32 - k));
|
return (x << k) | (x >> (32 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 merged;
|
|
||||||
uint32 s[2];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint32 xoroshiro64star_next(void* _state) {
|
uint32 xoroshiro64star_next(void* _state) {
|
||||||
_state_t* state=_state;
|
xoroshiro64_state* state=_state;
|
||||||
const uint32 s0 = state->s[0];
|
const uint32 s0 = state->s[0];
|
||||||
uint32 s1 = state->s[1];
|
uint32 s1 = state->s[1];
|
||||||
const uint32 result = s0 * 0x9E3779BB;
|
const uint32 result = s0 * 0x9E3779BB;
|
||||||
@ -45,9 +40,9 @@ uint32 xoroshiro64star_next(void* _state) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoroshiro64star_init(uint64 seed){
|
void* xoroshiro64_init(uint64 seed){
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
xoroshiro64_state* state=malloc(sizeof(xoroshiro64_state));
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
splitmix64_state* splitmix=splitmix64_init(seed);
|
||||||
state->merged=splitmix64_next(splitmix);
|
state->merged=splitmix64_next(splitmix);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoroshiro64.h"
|
||||||
|
|
||||||
/* This is xoroshiro64** 1.0, our 32-bit all-purpose, rock-solid,
|
/* This is xoroshiro64** 1.0, our 32-bit all-purpose, rock-solid,
|
||||||
small-state generator. It is extremely fast and it passes all tests we
|
small-state generator. It is extremely fast and it passes all tests we
|
||||||
@ -23,13 +23,8 @@ static inline uint32 rotl(const uint32 x, int k) {
|
|||||||
return (x << k) | (x >> (32 - k));
|
return (x << k) | (x >> (32 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 merged;
|
|
||||||
uint32 s[2];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint32 xoroshiro64starstar_next(void* _state) {
|
uint32 xoroshiro64starstar_next(void* _state) {
|
||||||
_state_t* state=_state;
|
xoroshiro64_state* state=_state;
|
||||||
const uint32 s0 = state->s[0];
|
const uint32 s0 = state->s[0];
|
||||||
uint32 s1 = state->s[1];
|
uint32 s1 = state->s[1];
|
||||||
const uint32 result = rotl(s0 * 0x9E3779BB, 5) * 5;
|
const uint32 result = rotl(s0 * 0x9E3779BB, 5) * 5;
|
||||||
@ -40,10 +35,3 @@ uint32 xoroshiro64starstar_next(void* _state) {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoroshiro64starstar_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->merged=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
32
src/random/xoroshiro/64bitValue/xoroshiro128.h
Normal file
32
src/random/xoroshiro/64bitValue/xoroshiro128.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../../../base/std.h"
|
||||||
|
#include "../../splitmix64/splitmix64.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint32 s[2];
|
||||||
|
} xoroshiro128_state;
|
||||||
|
typedef void* xoroshiro128_statePtr;
|
||||||
|
|
||||||
|
xoroshiro128_statePtr xoroshiro128_init(uint64 seed);
|
||||||
|
#define xoroshiro128plus_init xoroshiro128_init
|
||||||
|
#define xoroshiro128plusplus_init xoroshiro128_init
|
||||||
|
#define xoroshiro128starstar_init xoroshiro128_init
|
||||||
|
|
||||||
|
static inline xoroshiro128_statePtr xoroshiro128_initFromTime(void) { return xoroshiro128_init(time(NULL)); }
|
||||||
|
#define xoroshiro128plus_initFromTime xoroshiro128_initFromTime
|
||||||
|
#define xoroshiro128plusplus_initFromTime xoroshiro128_initFromTime
|
||||||
|
#define xoroshiro128starstar_initFromTime xoroshiro128_initFromTime
|
||||||
|
|
||||||
|
uint64 xoroshiro128plus_next(xoroshiro128_statePtr);
|
||||||
|
uint64 xoroshiro128plusplus_next(xoroshiro128_statePtr);
|
||||||
|
uint64 xoroshiro128starstar_next(xoroshiro128_statePtr);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoroshiro128.h"
|
||||||
|
|
||||||
/* This is xoroshiro128+ 1.0, our best and fastest small-state generator
|
/* This is xoroshiro128+ 1.0, our best and fastest small-state generator
|
||||||
for floating-point numbers, but its state space is large enough only
|
for floating-point numbers, but its state space is large enough only
|
||||||
@ -36,12 +36,8 @@ static inline uint64 rotl(const uint64 x, int k) {
|
|||||||
return (x << k) | (x >> (64 - k));
|
return (x << k) | (x >> (64 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint32 s[2];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint64 xoroshiro128plus_next(void* _state){
|
uint64 xoroshiro128plus_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoroshiro128_state* state=_state;
|
||||||
const uint64 s0 = state->s[0];
|
const uint64 s0 = state->s[0];
|
||||||
uint64 s1 = state->s[1];
|
uint64 s1 = state->s[1];
|
||||||
const uint64 result = s0 + s1;
|
const uint64 result = s0 + s1;
|
||||||
@ -53,9 +49,9 @@ uint64 xoroshiro128plus_next(void* _state){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoroshiro128plus_init(uint64 seed){
|
void* xoroshiro128_init(uint64 seed){
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
xoroshiro128_state* state=malloc(sizeof(xoroshiro128_state));
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
splitmix64_state* splitmix=splitmix64_init(seed);
|
||||||
state->s[0]=splitmix64_next(splitmix);
|
state->s[0]=splitmix64_next(splitmix);
|
||||||
state->s[1]=splitmix64_next(splitmix);
|
state->s[1]=splitmix64_next(splitmix);
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoroshiro128.h"
|
||||||
|
|
||||||
/* This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid,
|
/* This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid,
|
||||||
small-state generators. It is extremely (sub-ns) fast and it passes all
|
small-state generators. It is extremely (sub-ns) fast and it passes all
|
||||||
@ -25,12 +25,8 @@ static inline uint64 rotl(const uint64 x, int k) {
|
|||||||
return (x << k) | (x >> (64 - k));
|
return (x << k) | (x >> (64 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint32 s[2];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint64 xoroshiro128plusplus_next(void* _state){
|
uint64 xoroshiro128plusplus_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoroshiro128_state* state=_state;
|
||||||
const uint64 s0 = state->s[0];
|
const uint64 s0 = state->s[0];
|
||||||
uint64 s1 = state->s[1];
|
uint64 s1 = state->s[1];
|
||||||
const uint64 result = rotl(s0 + s1, 17) + s0;
|
const uint64 result = rotl(s0 + s1, 17) + s0;
|
||||||
@ -41,11 +37,3 @@ uint64 xoroshiro128plusplus_next(void* _state){
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoroshiro128plusplus_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->s[0]=splitmix64_next(splitmix);
|
|
||||||
state->s[1]=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoroshiro128.h"
|
||||||
|
|
||||||
/* This is xoroshiro128** 1.0, one of our all-purpose, rock-solid,
|
/* This is xoroshiro128** 1.0, one of our all-purpose, rock-solid,
|
||||||
small-state generators. It is extremely (sub-ns) fast and it passes all
|
small-state generators. It is extremely (sub-ns) fast and it passes all
|
||||||
@ -25,12 +25,8 @@ static inline uint64 rotl(const uint64 x, int k) {
|
|||||||
return (x << k) | (x >> (64 - k));
|
return (x << k) | (x >> (64 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint32 s[2];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint64 xoroshiro128starstar_next(void* _state){
|
uint64 xoroshiro128starstar_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoroshiro128_state* state=_state;
|
||||||
const uint64 s0 = state->s[0];
|
const uint64 s0 = state->s[0];
|
||||||
uint64 s1 = state->s[1];
|
uint64 s1 = state->s[1];
|
||||||
const uint64 result = rotl(s0 * 5, 7) * 9;
|
const uint64 result = rotl(s0 * 5, 7) * 9;
|
||||||
@ -41,11 +37,3 @@ uint64 xoroshiro128starstar_next(void* _state){
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoroshiro128starstar_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->s[0]=splitmix64_next(splitmix);
|
|
||||||
state->s[1]=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
2
src/random/xoroshiro/xoroshiro.h
Normal file
2
src/random/xoroshiro/xoroshiro.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#include "32bitValue/xoroshiro64.h"
|
||||||
|
#include "64bitValue/xoroshiro128.h"
|
||||||
33
src/random/xoshiro/32bitValue/xoshiro128.h
Normal file
33
src/random/xoshiro/32bitValue/xoshiro128.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../../../base/std.h"
|
||||||
|
#include "../../splitmix64/splitmix64.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint64 merged[2];
|
||||||
|
uint32 s[4];
|
||||||
|
} xoshiro128_state;
|
||||||
|
typedef void* xoshiro128_statePtr;
|
||||||
|
|
||||||
|
xoshiro128_statePtr xoshiro128_init(uint64 seed);
|
||||||
|
#define xoshiro128plus_init xoshiro128_init
|
||||||
|
#define xoshiro128plusplus_init xoshiro128_init
|
||||||
|
#define xoshiro128starstar_init xoshiro128_init
|
||||||
|
|
||||||
|
static inline xoshiro128_statePtr xoshiro128_initFromTime(void) { return xoshiro128_init(time(NULL)); }
|
||||||
|
#define xoshiro128plus_initFromTime xoshiro128_initFromTime
|
||||||
|
#define xoshiro128plusplus_initFromTime xoshiro128_initFromTime
|
||||||
|
#define xoshiro128starstar_initFromTime xoshiro128_initFromTime
|
||||||
|
|
||||||
|
uint32 xoshiro128plus_next(xoshiro128_statePtr);
|
||||||
|
uint32 xoshiro128plusplus_next(xoshiro128_statePtr);
|
||||||
|
uint32 xoshiro128starstar_next(xoshiro128_statePtr);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoshiro128.h"
|
||||||
|
|
||||||
/* This is xoshiro128+ 1.0, our best and fastest 32-bit generator for 32-bit
|
/* This is xoshiro128+ 1.0, our best and fastest 32-bit generator for 32-bit
|
||||||
floating-point numbers. We suggest to use its upper bits for
|
floating-point numbers. We suggest to use its upper bits for
|
||||||
@ -26,13 +26,8 @@ static inline uint32 rotl(const uint32 x, int k) {
|
|||||||
return (x << k) | (x >> (32 - k));
|
return (x << k) | (x >> (32 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 merged[2];
|
|
||||||
uint32 s[4];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint32 xoshiro128plus_next(void* _state){
|
uint32 xoshiro128plus_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoshiro128_state* state=_state;
|
||||||
const uint32 result = state->s[0] + state->s[3];
|
const uint32 result = state->s[0] + state->s[3];
|
||||||
|
|
||||||
const uint32 t = state->s[1] << 9;
|
const uint32 t = state->s[1] << 9;
|
||||||
@ -49,9 +44,9 @@ uint32 xoshiro128plus_next(void* _state){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoshiro128plus_init(uint64 seed){
|
void* xoshiro128_init(uint64 seed){
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
xoshiro128_state* state=malloc(sizeof(xoshiro128_state));
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
splitmix64_state* splitmix=splitmix64_init(seed);
|
||||||
state->merged[0]=splitmix64_next(splitmix);
|
state->merged[0]=splitmix64_next(splitmix);
|
||||||
state->merged[1]=splitmix64_next(splitmix);
|
state->merged[1]=splitmix64_next(splitmix);
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoshiro128.h"
|
||||||
|
|
||||||
/* This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid
|
/* This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid
|
||||||
generators. It has excellent speed, a state size (128 bits) that is
|
generators. It has excellent speed, a state size (128 bits) that is
|
||||||
@ -23,13 +23,8 @@ static inline uint32 rotl(const uint32 x, int k) {
|
|||||||
return (x << k) | (x >> (32 - k));
|
return (x << k) | (x >> (32 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 merged[2];
|
|
||||||
uint32 s[4];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint32 xoshiro128plusplus_next(void* _state){
|
uint32 xoshiro128plusplus_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoshiro128_state* state=_state;
|
||||||
const uint32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0];
|
const uint32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0];
|
||||||
|
|
||||||
const uint32 t = state->s[1] << 9;
|
const uint32 t = state->s[1] << 9;
|
||||||
@ -45,11 +40,3 @@ uint32 xoshiro128plusplus_next(void* _state){
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoshiro128plusplus_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->merged[0]=splitmix64_next(splitmix);
|
|
||||||
state->merged[1]=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoshiro128.h"
|
||||||
|
|
||||||
/* This is xoshiro128** 1.1, one of our 32-bit all-purpose, rock-solid
|
/* This is xoshiro128** 1.1, one of our 32-bit all-purpose, rock-solid
|
||||||
generators. It has excellent speed, a state size (128 bits) that is
|
generators. It has excellent speed, a state size (128 bits) that is
|
||||||
@ -26,13 +26,8 @@ static inline uint32 rotl(const uint32 x, int k) {
|
|||||||
return (x << k) | (x >> (32 - k));
|
return (x << k) | (x >> (32 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 merged[2];
|
|
||||||
uint32 s[4];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint32 xoshiro128starstar_next(void* _state){
|
uint32 xoshiro128starstar_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoshiro128_state* state=_state;
|
||||||
const uint32 result = rotl(state->s[1] * 5, 7) * 9;
|
const uint32 result = rotl(state->s[1] * 5, 7) * 9;
|
||||||
|
|
||||||
const uint32 t = state->s[1] << 9;
|
const uint32 t = state->s[1] << 9;
|
||||||
@ -48,11 +43,3 @@ uint32 xoshiro128starstar_next(void* _state){
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoshiro128starstar_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->merged[0]=splitmix64_next(splitmix);
|
|
||||||
state->merged[1]=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
32
src/random/xoshiro/64bitValue/xoshiro256.h
Normal file
32
src/random/xoshiro/64bitValue/xoshiro256.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../../../base/std.h"
|
||||||
|
#include "../../splitmix64/splitmix64.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
uint64 s[4];
|
||||||
|
} xoshiro256_state;
|
||||||
|
typedef void* xoshiro256_statePtr;
|
||||||
|
|
||||||
|
xoshiro256_statePtr xoshiro256_init(uint64 seed);
|
||||||
|
#define xoshiro256plus_init xoshiro256_init
|
||||||
|
#define xoshiro256plusplus_init xoshiro256_init
|
||||||
|
#define xoshiro256starstar_init xoshiro256_init
|
||||||
|
|
||||||
|
static inline xoshiro256_statePtr xoshiro256_initFromTime(void) { return xoshiro256_init(time(NULL)); }
|
||||||
|
#define xoshiro256plus_initFromTime xoshiro256_initFromTime
|
||||||
|
#define xoshiro256plusplus_initFromTime xoshiro256_initFromTime
|
||||||
|
#define xoshiro256starstar_initFromTime xoshiro256_initFromTime
|
||||||
|
|
||||||
|
uint64 xoshiro256plus_next(xoshiro256_statePtr);
|
||||||
|
uint64 xoshiro256plusplus_next(xoshiro256_statePtr);
|
||||||
|
uint64 xoshiro256starstar_next(xoshiro256_statePtr);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoshiro256.h"
|
||||||
|
|
||||||
/* This is xoshiro256+ 1.0, our best and fastest generator for floating-point
|
/* This is xoshiro256+ 1.0, our best and fastest generator for floating-point
|
||||||
numbers. We suggest to use its upper bits for floating-point
|
numbers. We suggest to use its upper bits for floating-point
|
||||||
@ -28,12 +28,8 @@ static inline uint64 rotl(const uint64 x, int k) {
|
|||||||
return (x << k) | (x >> (64 - k));
|
return (x << k) | (x >> (64 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 s[4];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint64 xoshiro256plus_next(void* _state){
|
uint64 xoshiro256plus_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoshiro256_state* state=_state;
|
||||||
const uint64 result = state->s[0] + state->s[3];
|
const uint64 result = state->s[0] + state->s[3];
|
||||||
|
|
||||||
const uint64 t = state->s[1] << 17;
|
const uint64 t = state->s[1] << 17;
|
||||||
@ -50,9 +46,9 @@ uint64 xoshiro256plus_next(void* _state){
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoshiro256plus_init(uint64 seed){
|
void* xoshiro256_init(uint64 seed){
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
xoshiro256_state* state=malloc(sizeof(xoshiro256_state));
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
splitmix64_state* splitmix=splitmix64_init(seed);
|
||||||
state->s[0]=splitmix64_next(splitmix);
|
state->s[0]=splitmix64_next(splitmix);
|
||||||
state->s[1]=splitmix64_next(splitmix);
|
state->s[1]=splitmix64_next(splitmix);
|
||||||
state->s[2]=splitmix64_next(splitmix);
|
state->s[2]=splitmix64_next(splitmix);
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoshiro256.h"
|
||||||
|
|
||||||
/* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators.
|
/* 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
|
It has excellent (sub-ns) speed, a state (256 bits) that is large
|
||||||
@ -23,12 +23,8 @@ static inline uint64 rotl(const uint64 x, int k) {
|
|||||||
return (x << k) | (x>>(64 - k));
|
return (x << k) | (x>>(64 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 s[4];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint64 xoshiro256plusplus_next(void* _state) {
|
uint64 xoshiro256plusplus_next(void* _state) {
|
||||||
_state_t* state=_state;
|
xoshiro256_state* state=_state;
|
||||||
const uint64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0];
|
const uint64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0];
|
||||||
const uint64 t=state->s[1] << 17;
|
const uint64 t=state->s[1] << 17;
|
||||||
state->s[2] ^= state->s[0];
|
state->s[2] ^= state->s[0];
|
||||||
@ -39,13 +35,3 @@ uint64 xoshiro256plusplus_next(void* _state) {
|
|||||||
state->s[3]=rotl(state->s[3], 45);
|
state->s[3]=rotl(state->s[3], 45);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoshiro256plusplus_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->s[0]=splitmix64_next(splitmix);
|
|
||||||
state->s[1]=splitmix64_next(splitmix);
|
|
||||||
state->s[2]=splitmix64_next(splitmix);
|
|
||||||
state->s[3]=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty.
|
|||||||
|
|
||||||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
|
||||||
|
|
||||||
#include "../../krandom.h"
|
#include "xoshiro256.h"
|
||||||
|
|
||||||
/* This is xoshiro256** 1.0, one of our all-purpose, rock-solid
|
/* 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
|
generators. It has excellent (sub-ns) speed, a state (256 bits) that is
|
||||||
@ -23,12 +23,8 @@ static inline uint64 rotl(const uint64 x, int k) {
|
|||||||
return (x << k) | (x >> (64 - k));
|
return (x << k) | (x >> (64 - k));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint64 s[4];
|
|
||||||
} _state_t;
|
|
||||||
|
|
||||||
uint64 xoshiro256starstar_next(void* _state){
|
uint64 xoshiro256starstar_next(void* _state){
|
||||||
_state_t* state=_state;
|
xoshiro256_state* state=_state;
|
||||||
const uint64 result = rotl(state->s[1] * 5, 7) * 9;
|
const uint64 result = rotl(state->s[1] * 5, 7) * 9;
|
||||||
|
|
||||||
const uint64 t = state->s[1] << 17;
|
const uint64 t = state->s[1] << 17;
|
||||||
@ -44,13 +40,3 @@ uint64 xoshiro256starstar_next(void* _state){
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* xoshiro256starstar_init(uint64 seed){
|
|
||||||
_state_t* state=malloc(sizeof(_state_t));
|
|
||||||
splitmix64_state splitmix=splitmix64_init(seed);
|
|
||||||
state->s[0]=splitmix64_next(splitmix);
|
|
||||||
state->s[1]=splitmix64_next(splitmix);
|
|
||||||
state->s[2]=splitmix64_next(splitmix);
|
|
||||||
state->s[3]=splitmix64_next(splitmix);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|||||||
2
src/random/xoshiro/xoshiro.h
Normal file
2
src/random/xoshiro/xoshiro.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#include "32bitValue/xoshiro128.h"
|
||||||
|
#include "64bitValue/xoshiro256.h"
|
||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\
|
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\
|
||||||
printf("\e[94mrng algorithm: \e[96m" #ALG "\n");\
|
printf("\e[94mrng algorithm: \e[96m" #ALG "\n");\
|
||||||
ALG##_state s= ALG##_init(0);\
|
void* s= ALG##_init(0);\
|
||||||
uint##VALUE_SIZE r=ALG##_next(s);\
|
uint##VALUE_SIZE r=ALG##_next(s);\
|
||||||
printf("\e[97m next from zero seed:");\
|
printf("\e[97m next from zero seed:");\
|
||||||
if(r!=EXPECTED_FROM_ZERO){\
|
if(r!=EXPECTED_FROM_ZERO){\
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user