v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/fastrand/alg/splitmix64/splitmix64.go (about)

     1  /******************************************************************************
     2  
     3  Original C code: https://xorshift.di.unimi.it/splitmix64.c
     4  Written in 2015 by Sebastiano Vigna (vigna@acm.org)
     5  
     6  To the extent possible under law, the author has dedicated all copyright
     7  and related and neighboring rights to this software to the public domain
     8  worldwide. This software is distributed without any warranty.
     9  
    10  See <http://creativecommons.org/publicdomain/zero/1.0/>.
    11  
    12  *******************************************************************************/
    13  
    14  package splitmix64
    15  
    16  import "sync/atomic"
    17  
    18  var x uint64 = 11920197720133568065
    19  
    20  const IncrementConstant = 0x9e3779b97f4a7c15
    21  
    22  func next(x0 uint64) uint64 {
    23  	x0 = (x0 ^ (x0 >> 30)) * 0xbf58476d1ce4e5b9
    24  	x0 = (x0 ^ (x0 >> 27)) * 0x94d049bb133111eb
    25  	return x0 ^ (x0 >> 31)
    26  }
    27  
    28  func Next() uint64 {
    29  	n := atomic.AddUint64(&x, IncrementConstant)
    30  	return next(n)
    31  }
    32  
    33  func Splitmix64(state *uint64) uint64 {
    34  	*state += IncrementConstant
    35  	return next(*state)
    36  }