github.com/team-ide/go-dialect@v1.9.20/vitess/hack/runtime.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package hack
    18  
    19  import (
    20  	"reflect"
    21  	"unsafe"
    22  )
    23  
    24  //go:noescape
    25  //go:linkname memhash runtime.memhash
    26  func memhash(p unsafe.Pointer, h, s uintptr) uintptr
    27  
    28  //go:noescape
    29  //go:linkname strhash runtime.strhash
    30  func strhash(p unsafe.Pointer, h uintptr) uintptr
    31  
    32  // RuntimeMemhash provides access to the Go runtime's default hash function for arbitrary bytes.
    33  // This is an optimal hash function which takes an input seed and is potentially implemented in hardware
    34  // for most architectures. This is the same hash function that the language's `map` uses.
    35  func RuntimeMemhash(b []byte, seed uint64) uint64 {
    36  	pstring := (*reflect.SliceHeader)(unsafe.Pointer(&b))
    37  	return uint64(memhash(unsafe.Pointer(pstring.Data), uintptr(seed), uintptr(pstring.Len)))
    38  }
    39  
    40  // RuntimeStrhash provides access to the Go runtime's default hash function for strings.
    41  // This is an optimal hash function which takes an input seed and is potentially implemented in hardware
    42  // for most architectures. This is the same hash function that the language's `map` uses.
    43  func RuntimeStrhash(str string, seed uint64) uint64 {
    44  	return uint64(strhash(unsafe.Pointer(&str), uintptr(seed)))
    45  }
    46  
    47  //go:linkname roundupsize runtime.roundupsize
    48  func roundupsize(size uintptr) uintptr
    49  
    50  // RuntimeAllocSize returns size of the memory block that mallocgc will allocate if you ask for the size.
    51  func RuntimeAllocSize(size int64) int64 {
    52  	return int64(roundupsize(uintptr(size)))
    53  }
    54  
    55  //go:linkname ParseFloatPrefix strconv.parseFloatPrefix
    56  func ParseFloatPrefix(s string, bitSize int) (float64, int, error)