lemire / testingRNG

Testing common random-number generators (RNG)
Apache License 2.0
175 stars 23 forks source link

try also this #17

Open Zibri opened 5 years ago

Zibri commented 5 years ago
#ifndef ZIBRI128_H
#define ZIBRI128_H

/* Modified by D. Lemire, August 2017 */
#include "splitmix64.h"
#include <stdint.h>

// state for zibri128
uint64_t zibri128_s[2];

static inline uint64_t rotr(const uint64_t x, int k) {
  return (x >> k) | (x << (64 - k));
}

// call this one before calling zibri128
static inline void zibri128_seed(uint64_t seed) {
  zibri128_s[0] = splitmix64_stateless(seed);
  zibri128_s[1] = splitmix64_stateless(seed + 1);
}

// returns random number, modifies zibri128_s
static inline uint64_t zibri128(void) {
  const uint64_t s0 = zibri128_s[0];
  const uint64_t s1 = zibri128_s[1];
  const uint64_t result = rotr(s0 + s1,8);

  zibri128_s[0] = result;
  zibri128_s[1] = s0;                 

  return result;
}

#endif // ZIBRI128_H

is the fastest so far...

Zibri commented 5 years ago

And check my optimized code at https://github.com/zibri/rand2

Zibri commented 5 years ago

Or also:

#ifndef ZIBRI192_H
#define ZIBRI192_H

/* Modified by D. Lemire, August 2017 */
#include "splitmix64.h"
#include <stdint.h>

// state for zibri192
uint64_t zibri192_s[3];

static inline uint64_t rotr(const uint64_t x, int k) {
  return (x >> k) | (x << (64 - k));
}

// call this one before calling zibri192
static inline void zibri192_seed(uint64_t seed) {
  zibri192_s[0] = splitmix64_stateless(seed);
  zibri192_s[1] = splitmix64_stateless(seed + 1);
  zibri192_s[2] = splitmix64_stateless(seed + 2);
}

// returns random number, modifies zibri192_s
static inline uint64_t zibri192(void) {
  const uint64_t s0 = zibri192_s[0];
  const uint64_t s1 = zibri192_s[1];
  const uint64_t s2 = zibri192_s[2];

  zibri192_s[0] = rotr(s0 + s1 + s2,16);
  zibri192_s[1] = s0;
  zibri192_s[2] = s1;

  return zibri192_s[0];
}

#endif // ZIBRI192_H
lemire commented 5 years ago

Thank you.