bursavich.dev/fastrand@v0.2.1/fastrand_safe.go (about)

     1  // SPDX-License-Identifier: MIT
     2  //
     3  // Copyright 2023 Andrew Bursavich. All rights reserved.
     4  // Use of this source code is governed by The MIT License
     5  // which can be found in the LICENSE file.
     6  
     7  //go:build safe
     8  
     9  package fastrand
    10  
    11  import "hash/maphash"
    12  
    13  func u32() uint32 {
    14  	return uint32(maphash.Bytes(maphash.MakeSeed(), nil) >> 32)
    15  }
    16  
    17  func u64() uint64 {
    18  	return maphash.Bytes(maphash.MakeSeed(), nil)
    19  }
    20  
    21  func putU64(p []byte, v uint64) {
    22  	_ = p[7] // Early bounds check to guarantee safety of writes below.
    23  	b[0] = byte(v)
    24  	b[1] = byte(v >> 8)
    25  	b[2] = byte(v >> 16)
    26  	b[3] = byte(v >> 24)
    27  	b[4] = byte(v >> 32)
    28  	b[5] = byte(v >> 40)
    29  	b[6] = byte(v >> 48)
    30  	b[7] = byte(v >> 56)
    31  }