github.com/bhojpur/cache@v0.0.4/pkg/hack/runtime.go (about)

     1  package hack
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"reflect"
    25  	"unsafe"
    26  )
    27  
    28  //go:noescape
    29  //go:linkname memhash runtime.memhash
    30  func memhash(p unsafe.Pointer, h, s uintptr) uintptr
    31  
    32  //go:noescape
    33  //go:linkname strhash runtime.strhash
    34  func strhash(p unsafe.Pointer, h uintptr) uintptr
    35  
    36  // RuntimeMemhash provides access to the Go runtime's default hash function for arbitrary bytes.
    37  // This is an optimal hash function which takes an input seed and is potentially implemented in hardware
    38  // for most architectures. This is the same hash function that the language's `map` uses.
    39  func RuntimeMemhash(b []byte, seed uint64) uint64 {
    40  	pstring := (*reflect.SliceHeader)(unsafe.Pointer(&b))
    41  	return uint64(memhash(unsafe.Pointer(pstring.Data), uintptr(seed), uintptr(pstring.Len)))
    42  }
    43  
    44  // RuntimeStrhash provides access to the Go runtime's default hash function for strings.
    45  // This is an optimal hash function which takes an input seed and is potentially implemented in hardware
    46  // for most architectures. This is the same hash function that the language's `map` uses.
    47  func RuntimeStrhash(str string, seed uint64) uint64 {
    48  	return uint64(strhash(unsafe.Pointer(&str), uintptr(seed)))
    49  }
    50  
    51  //go:linkname roundupsize runtime.roundupsize
    52  func roundupsize(size uintptr) uintptr
    53  
    54  // RuntimeAllocSize returns size of the memory block that mallocgc will allocate if you ask for the size.
    55  func RuntimeAllocSize(size int64) int64 {
    56  	return int64(roundupsize(uintptr(size)))
    57  }
    58  
    59  //go:linkname ParseFloatPrefix strconv.parseFloatPrefix
    60  func ParseFloatPrefix(s string, bitSize int) (float64, int, error)