github.com/rolandhe/saber@v0.0.4/hash/crc/crc64_amd64.go (about)

     1  // Golang concurrent tools like java juc.
     2  //
     3  // Copyright 2023 The saber Authors. All rights reserved.
     4  
     5  // Package crc 通过汇编调用intel _mm_crc32_u64指令来加速crc的计算,
     6  // 要求机器必须支持SSE42指令
     7  package crc
     8  
     9  import (
    10  	"runtime"
    11  	"unsafe"
    12  )
    13  
    14  const (
    15  	cpuidSSE42 = 1 << 20
    16  )
    17  
    18  var withSSE42 bool
    19  
    20  //go:noescape
    21  //go:nosplit
    22  func _crc32u64(a, b uint64, result unsafe.Pointer)
    23  
    24  // cpuid is implemented in cpu_x86.s.
    25  //
    26  //go:noescape
    27  //go:nosplit
    28  func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)
    29  
    30  func init() {
    31  	withSSE42 = _withSSE42()
    32  }
    33  
    34  func _withSSE42() bool {
    35  	if runtime.GOARCH != "amd64" {
    36  		return false
    37  	}
    38  	_, _, ecx1, _ := cpuid(1, 0)
    39  	return ecx1&cpuidSSE42 != 0
    40  }
    41  
    42  // Crc32u64  通过 _mm_crc32_u64 指令完成crc算法
    43  func Crc32u64(a, b uint64) uint64 {
    44  	var sum uint64
    45  	_crc32u64(a, b, unsafe.Pointer(&sum))
    46  	return sum
    47  }
    48  
    49  // WithSSE42 判断当前系统是否支持 SSE42 指令
    50  func WithSSE42() bool {
    51  	return withSSE42
    52  }