Kaynağa Gözat

remove unused functionality from the pcg rng

Julius Pfrommer 8 yıl önce
ebeveyn
işleme
15835935f0
2 değiştirilmiş dosya ile 7 ekleme ve 114 silme
  1. 2 78
      deps/pcg_basic.c
  2. 5 36
      deps/pcg_basic.h

+ 2 - 78
deps/pcg_basic.c

@@ -21,26 +21,9 @@
  *       http://www.pcg-random.org
  */
 
-/*
- * This code is derived from the full C implementation, which is in turn
- * derived from the canonical C++ PCG implementation. The C++ version
- * has many additional features and is preferable if you can use C++ in
- * your project.
- */
-
 #include "pcg_basic.h"
 
-// state for global RNGs
-
-static pcg32_random_t pcg32_global = PCG32_INITIALIZER;
-
-// pcg32_srandom(initial_state, initseq)
-// pcg32_srandom_r(rng, initial_state, initseq):
-//     Seed the rng.  Specified in two parts, state initializer and a
-//     sequence selection constant (a.k.a. stream id)
-
-void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state, uint64_t initseq)
-{
+void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state, uint64_t initseq) {
     rng->state = 0U;
     rng->inc = (initseq << 1u) | 1u;
     pcg32_random_r(rng);
@@ -48,69 +31,10 @@ void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state, uint64_t inits
     pcg32_random_r(rng);
 }
 
-void pcg32_srandom(uint64_t seed, uint64_t seq)
-{
-    pcg32_srandom_r(&pcg32_global, seed, seq);
-}
-
-// pcg32_random()
-// pcg32_random_r(rng)
-//     Generate a uniformly distributed 32-bit random number
-
-uint32_t pcg32_random_r(pcg32_random_t* rng)
-{
+uint32_t pcg32_random_r(pcg32_random_t* rng) {
     uint64_t oldstate = rng->state;
     rng->state = oldstate * 6364136223846793005ULL + rng->inc;
     uint32_t xorshifted = (uint32_t)(((oldstate >> 18u) ^ oldstate) >> 27u);
     uint32_t rot = (uint32_t)(oldstate >> 59u);
     return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
 }
-
-uint32_t pcg32_random()
-{
-    return pcg32_random_r(&pcg32_global);
-}
-
-
-// pcg32_boundedrand(bound):
-// pcg32_boundedrand_r(rng, bound):
-//     Generate a uniformly distributed number, r, where 0 <= r < bound
-
-uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound)
-{
-    // To avoid bias, we need to make the range of the RNG a multiple of
-    // bound, which we do by dropping output less than a threshold.
-    // A naive scheme to calculate the threshold would be to do
-    //
-    //     uint32_t threshold = 0x100000000ull % bound;
-    //
-    // but 64-bit div/mod is slower than 32-bit div/mod (especially on
-    // 32-bit platforms).  In essence, we do
-    //
-    //     uint32_t threshold = (0x100000000ull-bound) % bound;
-    //
-    // because this version will calculate the same modulus, but the LHS
-    // value is less than 2^32.
-
-    uint32_t threshold = -bound % bound;
-
-    // Uniformity guarantees that this loop will terminate.  In practice, it
-    // should usually terminate quickly; on average (assuming all bounds are
-    // equally likely), 82.25% of the time, we can expect it to require just
-    // one iteration.  In the worst case, someone passes a bound of 2^31 + 1
-    // (i.e., 2147483649), which invalidates almost 50% of the range.  In 
-    // practice, bounds are typically small and only a tiny amount of the range
-    // is eliminated.
-    for (;;) {
-        uint32_t r = pcg32_random_r(rng);
-        if (r >= threshold)
-            return r % bound;
-    }
-}
-
-
-uint32_t pcg32_boundedrand(uint32_t bound)
-{
-    return pcg32_boundedrand_r(&pcg32_global, bound);
-}
-

+ 5 - 36
deps/pcg_basic.h

@@ -21,13 +21,6 @@
  *     http://www.pcg-random.org
  */
 
-/*
- * This code is derived from the full C implementation, which is in turn
- * derived from the canonical C++ PCG implementation. The C++ version
- * has many additional features and is preferable if you can use C++ in
- * your project.
- */
-
 #ifndef PCG_BASIC_H_INCLUDED
 #define PCG_BASIC_H_INCLUDED 1
 
@@ -37,40 +30,16 @@
 extern "C" {
 #endif
 
-struct pcg_state_setseq_64 {    // Internals are *Private*.
+typedef struct pcg_state_setseq_64 {
     uint64_t state;             // RNG state.  All values are possible.
-    uint64_t inc;               // Controls which RNG sequence (stream) is
-                                // selected. Must *always* be odd.
-};
-typedef struct pcg_state_setseq_64 pcg32_random_t;
-
-// If you *must* statically initialize it, here's one.
-
-#define PCG32_INITIALIZER   { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
+    uint64_t inc;               // Controls which RNG sequence (stream) is selected. Must *always* be odd.
+} pcg32_random_t;
 
-// pcg32_srandom(initial_state, initseq)
-// pcg32_srandom_r(rng, initial_state, initseq):
-//     Seed the rng.  Specified in two parts, state initializer and a
-//     sequence selection constant (a.k.a. stream id)
+#define PCG32_INITIALIZER { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
 
-void pcg32_srandom(uint64_t initial_state, uint64_t initseq);
-void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state,
-                     uint64_t initseq);
-
-// pcg32_random()
-// pcg32_random_r(rng)
-//     Generate a uniformly distributed 32-bit random number
-
-uint32_t pcg32_random(void);
+void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state, uint64_t initseq);
 uint32_t pcg32_random_r(pcg32_random_t* rng);
 
-// pcg32_boundedrand(bound):
-// pcg32_boundedrand_r(rng, bound):
-//     Generate a uniformly distributed number, r, where 0 <= r < bound
-
-uint32_t pcg32_boundedrand(uint32_t bound);
-uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound);
-
 #if __cplusplus
 }
 #endif