Implement new CPU detection using cpuid, cpuidex plan9 instructions from klauspost/cpuid project, remove C code

This commit is contained in:
Harshavardhana
2015-07-04 14:22:05 -07:00
parent 9977888972
commit aa67a19e99
10 changed files with 44 additions and 74 deletions

View File

@@ -1,59 +0,0 @@
/*
* Minimalist Object Storage, (C) 2014 Minio, Inc.
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
static void cpuid(int cpuinfo[4] ,int infotype) {
__asm__ __volatile__ (
"cpuid":
"=a" (cpuinfo[0]),
"=b" (cpuinfo[1]),
"=c" (cpuinfo[2]),
"=d" (cpuinfo[3]) :
"a" (infotype), "c" (0)
);
}
/*
SSE41 : return true
no SSE41 : return false
*/
int has_sse41 (void) {
int info[4];
cpuid(info, 0x00000001);
return ((info[2] & ((int)1 << 19)) != 0);
}
/*
AVX : return true
no AVX : return false
*/
int has_avx (void) {
int info[4];
cpuid(info, 0x00000001);
return ((info[2] & ((int)1 << 28)) != 0);
}
/*
AVX2 : return true
no AVX2 : return false
*/
int has_avx2 (void) {
int info[4];
cpuid(info, 0x00000007);
return ((info[1] & ((int)1 << 5)) != 0);
}

View File

@@ -1,37 +0,0 @@
/*
* Minimalist Object Storage, (C) 2014 Minio, Inc.
*
* 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 cpu
// int has_sse41 (void);
// int has_avx (void);
// int has_avx2 (void);
import "C"
// HasSSE41 - CPUID instruction verification wrapper for SSE41 extensions
func HasSSE41() bool {
return int(C.has_sse41()) == 1
}
// HasAVX - CPUID instruction verification wrapper for AVX extensions
func HasAVX() bool {
return int(C.has_avx()) == 1
}
// HasAVX2 - CPUID instruction verification wrapper for AVX2 extensions
func HasAVX2() bool {
return int(C.has_avx2()) == 1
}

View File

@@ -1,76 +0,0 @@
/*
* Minimalist Object Storage, (C) 2014 Minio, Inc.
*
* 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 cpu_test
import (
"errors"
"os/exec"
"runtime"
"strings"
"testing"
. "github.com/minio/check"
"github.com/minio/minio/pkg/utils/cpu"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func hasCPUFeatureFromOS(feature string) (bool, error) {
if runtime.GOOS == "linux" {
command := exec.Command("/bin/cat", "/proc/cpuinfo")
output, err := command.Output()
if err != nil {
return false, err
}
if strings.Contains(string(output), feature) {
return true, nil
}
return false, nil
}
return false, errors.New("Not Implemented on this platform")
}
func (s *MySuite) TestHasSSE41(c *C) {
if runtime.GOOS == "linux" {
var flag = cpu.HasSSE41()
osCheck, err := hasCPUFeatureFromOS("sse4_1")
c.Assert(err, IsNil)
c.Check(flag, Equals, osCheck)
}
}
func (s *MySuite) TestHasAVX(c *C) {
if runtime.GOOS == "linux" {
var flag = cpu.HasAVX()
osFlag, err := hasCPUFeatureFromOS("avx")
c.Assert(err, IsNil)
c.Check(osFlag, Equals, flag)
}
}
func (s *MySuite) TestHasAVX2(c *C) {
if runtime.GOOS == "linux" {
var flag = cpu.HasAVX2()
osFlag, err := hasCPUFeatureFromOS("avx2")
c.Assert(err, IsNil)
c.Check(osFlag, Equals, flag)
}
}

View File

@@ -1,11 +0,0 @@
// Package cpu provides wrapper around assembly functions for checking processor
// instruction capabilities for SSE4.1, AVX, AVX2 support
//
// Example
//
// ``cpu.HasSSE41()`` returns true for SSE4.1 instruction support, false otherwise
//
// ``cpu.HasAVX()`` returns true for AVX instruction support, false otherwise
//
// ``cpu.HasAVX2()`` returns true for AVX2 instruction support, false otherwise
package cpu