MixFunctions.java

  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.  * Performs mixing of bits.
  20.  *
  21.  * @since 1.5
  22.  */
  23. final class MixFunctions {
  24.     /**
  25.      * The fractional part of the golden ratio, phi, scaled to 64-bits and rounded to odd.
  26.      * This can be used as an increment for a Weyl sequence.
  27.      *
  28.      * @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a>
  29.      */
  30.     static final long GOLDEN_RATIO_64 = 0x9e3779b97f4a7c15L;
  31.     /**
  32.      * The fractional part of the golden ratio, phi, scaled to 32-bits and rounded to odd.
  33.      * This can be used as an increment for a Weyl sequence.
  34.      *
  35.      * @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a>
  36.      */
  37.     static final int GOLDEN_RATIO_32 = 0x9e3779b9;

  38.     /** No instances. */
  39.     private MixFunctions() {}

  40.     /**
  41.      * Perform variant 13 of David Stafford's 64-bit mix function.
  42.      * This is the mix function used in the
  43.      * {@link org.apache.commons.rng.core.source64.SplitMix64 SplitMix64} RNG.
  44.      *
  45.      * <p>This is ranked first of the top 14 Stafford mixers.
  46.      *
  47.      * @param x the input value
  48.      * @return the output value
  49.      * @see <a href="http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html">Better
  50.      *      Bit Mixing - Improving on MurmurHash3&#39;s 64-bit Finalizer.</a>
  51.      */
  52.     static long stafford13(long x) {
  53.         x = (x ^ (x >>> 30)) * 0xbf58476d1ce4e5b9L;
  54.         x = (x ^ (x >>> 27)) * 0x94d049bb133111ebL;
  55.         return x ^ (x >>> 31);
  56.     }

  57.     /**
  58.      * Perform the finalising 32-bit mix function of Austin Appleby's MurmurHash3.
  59.      *
  60.      * @param x the input value
  61.      * @return the output value
  62.      * @see <a href="https://github.com/aappleby/smhasher">SMHasher</a>
  63.      */
  64.     static int murmur3(int x) {
  65.         x = (x ^ (x >>> 16)) * 0x85ebca6b;
  66.         x = (x ^ (x >>> 13)) * 0xc2b2ae35;
  67.         return x ^ (x >>> 16);
  68.     }
  69. }