1
0
mirror of https://github.com/fumiama/blake2b-simd.git synced 2026-06-05 02:00:26 +08:00
Files
blake2b-simd/cpuid.go
Harshavardhana cf4f8e0c34 Fix benchmarks to report proper values. (#7)
```
$ go test -run=NONE -bench .
PASS
BenchmarkHash64-4  	 1000000	      1036 ns/op	  61.77 MB/s
BenchmarkHash128-4 	 2000000	       801 ns/op	 159.67 MB/s
BenchmarkHash1K-4  	  500000	      2464 ns/op	 415.53 MB/s
BenchmarkHash8K-4  	  200000	     11212 ns/op	 730.60 MB/s
BenchmarkHash32K-4 	   30000	     40766 ns/op	 803.80 MB/s
BenchmarkHash128K-4	   10000	    163170 ns/op	 803.28 MB/s
ok  	github.com/minio/blake2b-simd	10.298s
```
2016-07-03 21:58:02 +02:00

52 lines
1.4 KiB
Go

// +build 386,!gccgo amd64,!gccgo
// Copyright 2016 Frank Wessels <fwessels@xs4all.nl>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package blake2b
func cpuid(op uint32) (eax, ebx, ecx, edx uint32)
func cpuidex(op, op2 uint32) (eax, ebx, ecx, edx uint32)
func xgetbv(index uint32) (eax, edx uint32)
// True when SIMD instructions are available.
var avx2 = haveAVX2()
var avx = haveAVX()
// haveAVX returns true if when there is AVX support
func haveAVX() bool {
_, _, c, _ := cpuid(1)
// Check XGETBV, OXSAVE and AVX bits
if c&(1<<26) != 0 && c&(1<<27) != 0 && c&(1<<28) != 0 {
// Check for OS support
eax, _ := xgetbv(0)
return (eax & 0x6) == 0x6
}
return false
}
// haveAVX2 returns true if when there is AVX2 support
func haveAVX2() bool {
mfi, _, _, _ := cpuid(0)
// Check AVX2, AVX2 requires OS support, but BMI1/2 don't.
if mfi >= 7 && haveAVX() {
_, ebx, _, _ := cpuidex(7, 0)
return (ebx & 0x00000020) != 0
}
return false
}