1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.rng.simple.internal;
18
19 /**
20 * Performs mixing of bits.
21 *
22 * @since 1.5
23 */
24 final class MixFunctions {
25 /**
26 * The fractional part of the golden ratio, phi, scaled to 64-bits and rounded to odd.
27 * This can be used as an increment for a Weyl sequence.
28 *
29 * @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a>
30 */
31 static final long GOLDEN_RATIO_64 = 0x9e3779b97f4a7c15L;
32 /**
33 * The fractional part of the golden ratio, phi, scaled to 32-bits and rounded to odd.
34 * This can be used as an increment for a Weyl sequence.
35 *
36 * @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a>
37 */
38 static final int GOLDEN_RATIO_32 = 0x9e3779b9;
39
40 /** No instances. */
41 private MixFunctions() {}
42
43 /**
44 * Perform variant 13 of David Stafford's 64-bit mix function.
45 * This is the mix function used in the
46 * {@link org.apache.commons.rng.core.source64.SplitMix64 SplitMix64} RNG.
47 *
48 * <p>This is ranked first of the top 14 Stafford mixers.
49 *
50 * @param x the input value
51 * @return the output value
52 * @see <a href="http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html">Better
53 * Bit Mixing - Improving on MurmurHash3's 64-bit Finalizer.</a>
54 */
55 static long stafford13(long x) {
56 x = (x ^ (x >>> 30)) * 0xbf58476d1ce4e5b9L;
57 x = (x ^ (x >>> 27)) * 0x94d049bb133111ebL;
58 return x ^ (x >>> 31);
59 }
60
61 /**
62 * Perform the finalising 32-bit mix function of Austin Appleby's MurmurHash3.
63 *
64 * @param x the input value
65 * @return the output value
66 * @see <a href="https://github.com/aappleby/smhasher">SMHasher</a>
67 */
68 static int murmur3(int x) {
69 x = (x ^ (x >>> 16)) * 0x85ebca6b;
70 x = (x ^ (x >>> 13)) * 0xc2b2ae35;
71 return x ^ (x >>> 16);
72 }
73 }