diff --git a/tz/cpuid_x86.go b/internal/cpuid/cpuid_x86.go similarity index 92% rename from tz/cpuid_x86.go rename to internal/cpuid/cpuid_x86.go index 1d7630e..bf19137 100644 --- a/tz/cpuid_x86.go +++ b/internal/cpuid/cpuid_x86.go @@ -28,7 +28,7 @@ // +build 386 amd64 amd64p32 -package tz +package cpuid const ( bitOSXSAVE = 1 << 27 @@ -36,14 +36,19 @@ const ( bitAVX2 = 1 << 5 ) -func setFeatures() { +var ( + hasAVX bool + hasAVX2 bool +) + +func init() { maxID, _, _, _ := cpuid(0, 0) if maxID < 1 { return } _, _, ecx1, _ := cpuid(1, 0) - hasOSXSAVE = isSet(ecx1, bitOSXSAVE) + hasOSXSAVE := isSet(ecx1, bitOSXSAVE) osSupportsAVX := false if hasOSXSAVE { @@ -61,6 +66,9 @@ func setFeatures() { hasAVX2 = isSet(ebx7, bitAVX2) && osSupportsAVX } +func HasAVX() bool { return hasAVX } +func HasAVX2() bool { return hasAVX2 } + func isSet(hwc uint32, value uint32) bool { return hwc&value != 0 } diff --git a/tz/cpuid_x86.s b/internal/cpuid/cpuid_x86.s similarity index 100% rename from tz/cpuid_x86.s rename to internal/cpuid/cpuid_x86.s diff --git a/tz/hash.go b/tz/hash.go index b617d40..863aa6c 100644 --- a/tz/hash.go +++ b/tz/hash.go @@ -6,6 +6,8 @@ package tz import ( "errors" "hash" + + "github.com/nspcc-dev/tzhash/internal/cpuid" ) type Implementation int @@ -22,9 +24,8 @@ const ( ) var ( - hasAVX bool - hasAVX2 bool - hasOSXSAVE bool + hasAVX = cpuid.HasAVX() + hasAVX2 = cpuid.HasAVX2() ) func (impl Implementation) String() string { diff --git a/tz/sl2.go b/tz/sl2.go index 231de4e..30c0264 100644 --- a/tz/sl2.go +++ b/tz/sl2.go @@ -17,8 +17,6 @@ var id = sl2{ var mul func(a, b, c *sl2, x *[4]gf127.GF127) func init() { - setFeatures() - if hasAVX { mul = mulSL2AVX } else {