Skip to content

Commit

Permalink
use golang internal/cpu to detect cpu feature
Browse files Browse the repository at this point in the history
  • Loading branch information
templexxx committed Sep 26, 2017
1 parent 3b5a60a commit 979895f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
35 changes: 19 additions & 16 deletions rs_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package reedsolomon
import (
"errors"
"sync"
"github.com/klauspost/cpuid"

"github.com/templexxx/cpufeat"
)

// SIMD Instruction Extensions
Expand All @@ -13,22 +14,25 @@ const (
ssse3
)

func getEXT() int {
if cpuid.CPU.AVX2() {
return avx2
} else if cpuid.CPU.SSSE3() {
return ssse3
var extension = none

func init() {
getEXT()
}

func getEXT() {
if cpufeat.X86.HasAVX2 {
extension = avx2
return
} else if cpufeat.X86.HasSSSE3 {
extension = ssse3
return
} else {
return none
extension = none
return
}
}

//go:noescape
func hasAVX2() bool

//go:noescape
func hasSSSE3() bool

//go:noescape
func copy32B(dst, src []byte) // Need SSE2(introduced in 2001)

Expand Down Expand Up @@ -76,14 +80,13 @@ type (

func newRS(d, p int, em matrix) (enc Encoder) {
g := em[d*d:]
ext := getEXT()
if ext == none {
if extension == none {
return &encBase{data: d, parity: p, encode: em, gen: g}
}
t := make([]byte, d*p*32)
initTbl(g, p, d, t)
ok := okCache(d, p)
if ext == avx2 {
if extension == avx2 {
e := &encAVX2{data: d, parity: p, encode: em, gen: g, tbl: t, enableCache: ok,
inverseCache: iCache{data: make(map[uint32][]byte)}}
return e
Expand Down
19 changes: 0 additions & 19 deletions rs_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,6 @@ one16b:
JNE ymm
RET

TEXT ·hasAVX2(SB), NOSPLIT, $0
XORQ AX, AX
XORQ CX, CX
ADDL $7, AX
CPUID
SHRQ $5, BX
ANDQ $1, BX
MOVB BX, ret+0(FP)
RET

// func mulVectSSSE3(tbl, d, p []byte)
TEXT ·mulVectSSSE3(SB), NOSPLIT, $0
MOVQ i+24(FP), in
Expand Down Expand Up @@ -399,15 +389,6 @@ loop:
JNZ loop
RET

TEXT ·hasSSSE3(SB), NOSPLIT, $0
XORQ AX, AX
INCL AX
CPUID
SHRQ $9, CX
ANDQ $1, CX
MOVB CX, ret+0(FP)
RET

// func copy32B(dst, src []byte)
TEXT ·copy32B(SB), NOSPLIT, $0
MOVQ dst+0(FP), SI
Expand Down

0 comments on commit 979895f

Please sign in to comment.