commit 84102d9b1265e7e2f3ea35e394e9fd9a1e5e47fa
parent 679bda82585987b739868a5fefdd64c662f4379f
Author: Michael Savage <mikejsavage@gmail.com>
Date: Tue, 21 Aug 2018 10:15:17 +0300
Actually add PCG files
Diffstat:
rng/pcg.cc | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
rng/pcg.h | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 101 insertions(+), 0 deletions(-)
diff --git a/rng/pcg.cc b/rng/pcg.cc
@@ -0,0 +1,56 @@
+/*
+ * PCG Random Number Generation for C.
+ *
+ * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * For additional information about the PCG random number generation scheme,
+ * including its license and other licensing options, visit
+ *
+ * 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 "rng/pcg.h"
+
+PCG new_pcg() {
+ PCG pcg;
+ pcg.state = U64( 0x853c49e6748fea9b );
+ pcg.inc = U64( 0xda3e39cb94b95bdb );
+ return pcg;
+}
+
+PCG new_pcg( u64 state, u64 seq ) {
+ PCG pcg;
+ pcg.state = 0;
+ pcg.inc = ( seq << 1 ) | 1;
+ rng_next( &pcg );
+ pcg.state += state;
+ rng_next( &pcg );
+ return pcg;
+}
+
+u32 rng_next( PCG * pcg ) {
+ u64 oldstate = pcg->state;
+ pcg->state = oldstate * U64( 6364136223846793005 ) + pcg->inc;
+ u32 xorshifted = ( ( oldstate >> 18 ) ^ oldstate ) >> 27;
+ u32 rot = oldstate >> 59u;
+ return ( xorshifted >> rot ) | ( xorshifted << ( ( -rot ) & 31 ) );
+}
diff --git a/rng/pcg.h b/rng/pcg.h
@@ -0,0 +1,45 @@
+/*
+ * PCG Random Number Generation for C.
+ *
+ * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * For additional information about the PCG random number generation scheme,
+ * including its license and other licensing options, visit
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include "intrinsics.h"
+#include "rng/rng_utils.h"
+
+struct PCG {
+ u64 state; // RNG state. All values are possible.
+ u64 inc; // Controls which RNG sequence (stream) is
+ // selected. Must *always* be odd.
+};
+
+PCG new_pcg();
+PCG new_pcg( u64 state, u64 seq );
+
+u32 rng_next( PCG * pcg );