Move cpuid facility to separate package

This commit is contained in:
Evgenii Stratonikov 2019-10-09 17:51:41 +03:00
parent 2470efda43
commit 648b1deca7
4 changed files with 15 additions and 8 deletions

View file

@ -28,7 +28,7 @@
// +build 386 amd64 amd64p32 // +build 386 amd64 amd64p32
package tz package cpuid
const ( const (
bitOSXSAVE = 1 << 27 bitOSXSAVE = 1 << 27
@ -36,14 +36,19 @@ const (
bitAVX2 = 1 << 5 bitAVX2 = 1 << 5
) )
func setFeatures() { var (
hasAVX bool
hasAVX2 bool
)
func init() {
maxID, _, _, _ := cpuid(0, 0) maxID, _, _, _ := cpuid(0, 0)
if maxID < 1 { if maxID < 1 {
return return
} }
_, _, ecx1, _ := cpuid(1, 0) _, _, ecx1, _ := cpuid(1, 0)
hasOSXSAVE = isSet(ecx1, bitOSXSAVE) hasOSXSAVE := isSet(ecx1, bitOSXSAVE)
osSupportsAVX := false osSupportsAVX := false
if hasOSXSAVE { if hasOSXSAVE {
@ -61,6 +66,9 @@ func setFeatures() {
hasAVX2 = isSet(ebx7, bitAVX2) && osSupportsAVX hasAVX2 = isSet(ebx7, bitAVX2) && osSupportsAVX
} }
func HasAVX() bool { return hasAVX }
func HasAVX2() bool { return hasAVX2 }
func isSet(hwc uint32, value uint32) bool { func isSet(hwc uint32, value uint32) bool {
return hwc&value != 0 return hwc&value != 0
} }

View file

@ -6,6 +6,8 @@ package tz
import ( import (
"errors" "errors"
"hash" "hash"
"github.com/nspcc-dev/tzhash/internal/cpuid"
) )
type Implementation int type Implementation int
@ -22,9 +24,8 @@ const (
) )
var ( var (
hasAVX bool hasAVX = cpuid.HasAVX()
hasAVX2 bool hasAVX2 = cpuid.HasAVX2()
hasOSXSAVE bool
) )
func (impl Implementation) String() string { func (impl Implementation) String() string {

View file

@ -17,8 +17,6 @@ var id = sl2{
var mul func(a, b, c *sl2, x *[4]gf127.GF127) var mul func(a, b, c *sl2, x *[4]gf127.GF127)
func init() { func init() {
setFeatures()
if hasAVX { if hasAVX {
mul = mulSL2AVX mul = mulSL2AVX
} else { } else {