github.com/vedadiyan/sqlparser@v1.0.0/pkg/hack/runtime.go (about)

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