diff options
Diffstat (limited to 'libgo/go/internal/cpu')
-rw-r--r-- | libgo/go/internal/cpu/cpu.go | 32 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_arm.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_arm64.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_mips.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_mips64.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_mips64le.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_mipsle.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_ppc64.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_ppc64le.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_s390x.go | 7 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_test.go | 27 | ||||
-rw-r--r-- | libgo/go/internal/cpu/cpu_x86.go | 59 |
12 files changed, 181 insertions, 0 deletions
diff --git a/libgo/go/internal/cpu/cpu.go b/libgo/go/internal/cpu/cpu.go new file mode 100644 index 00000000000..2226b777e23 --- /dev/null +++ b/libgo/go/internal/cpu/cpu.go @@ -0,0 +1,32 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package cpu implements processor feature detection +// used by the Go standard libary. +package cpu + +var X86 x86 + +// The booleans in x86 contain the correspondingly named cpuid feature bit. +// HasAVX and HasAVX2 are only set if the OS does support XMM and YMM registers +// in addition to the cpuid feature bit being set. +// The struct is padded to avoid false sharing. +type x86 struct { + _ [CacheLineSize]byte + HasAES bool + HasAVX bool + HasAVX2 bool + HasBMI1 bool + HasBMI2 bool + HasERMS bool + HasOSXSAVE bool + HasPCLMULQDQ bool + HasPOPCNT bool + HasSSE2 bool + HasSSE3 bool + HasSSSE3 bool + HasSSE41 bool + HasSSE42 bool + _ [CacheLineSize]byte +} diff --git a/libgo/go/internal/cpu/cpu_arm.go b/libgo/go/internal/cpu/cpu_arm.go new file mode 100644 index 00000000000..078a6c3b80a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_arm.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 32 diff --git a/libgo/go/internal/cpu/cpu_arm64.go b/libgo/go/internal/cpu/cpu_arm64.go new file mode 100644 index 00000000000..078a6c3b80a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_arm64.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 32 diff --git a/libgo/go/internal/cpu/cpu_mips.go b/libgo/go/internal/cpu/cpu_mips.go new file mode 100644 index 00000000000..078a6c3b80a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_mips.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 32 diff --git a/libgo/go/internal/cpu/cpu_mips64.go b/libgo/go/internal/cpu/cpu_mips64.go new file mode 100644 index 00000000000..078a6c3b80a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_mips64.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 32 diff --git a/libgo/go/internal/cpu/cpu_mips64le.go b/libgo/go/internal/cpu/cpu_mips64le.go new file mode 100644 index 00000000000..078a6c3b80a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_mips64le.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 32 diff --git a/libgo/go/internal/cpu/cpu_mipsle.go b/libgo/go/internal/cpu/cpu_mipsle.go new file mode 100644 index 00000000000..078a6c3b80a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_mipsle.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 32 diff --git a/libgo/go/internal/cpu/cpu_ppc64.go b/libgo/go/internal/cpu/cpu_ppc64.go new file mode 100644 index 00000000000..5b151508479 --- /dev/null +++ b/libgo/go/internal/cpu/cpu_ppc64.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 128 diff --git a/libgo/go/internal/cpu/cpu_ppc64le.go b/libgo/go/internal/cpu/cpu_ppc64le.go new file mode 100644 index 00000000000..5b151508479 --- /dev/null +++ b/libgo/go/internal/cpu/cpu_ppc64le.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 128 diff --git a/libgo/go/internal/cpu/cpu_s390x.go b/libgo/go/internal/cpu/cpu_s390x.go new file mode 100644 index 00000000000..4455809d53a --- /dev/null +++ b/libgo/go/internal/cpu/cpu_s390x.go @@ -0,0 +1,7 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu + +const CacheLineSize = 256 diff --git a/libgo/go/internal/cpu/cpu_test.go b/libgo/go/internal/cpu/cpu_test.go new file mode 100644 index 00000000000..ab9836ac2f6 --- /dev/null +++ b/libgo/go/internal/cpu/cpu_test.go @@ -0,0 +1,27 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cpu_test + +import ( + "internal/cpu" + "runtime" + "testing" +) + +func TestAMD64minimalFeatures(t *testing.T) { + if runtime.GOARCH == "amd64" { + if !cpu.X86.HasSSE2 { + t.Fatalf("HasSSE2 expected true, got false") + } + } +} + +func TestAVX2hasAVX(t *testing.T) { + if runtime.GOARCH == "amd64" { + if cpu.X86.HasAVX2 && !cpu.X86.HasAVX { + t.Fatalf("HasAVX expected true, got false") + } + } +} diff --git a/libgo/go/internal/cpu/cpu_x86.go b/libgo/go/internal/cpu/cpu_x86.go new file mode 100644 index 00000000000..31e7084e78b --- /dev/null +++ b/libgo/go/internal/cpu/cpu_x86.go @@ -0,0 +1,59 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build 386 amd64 amd64p32 + +package cpu + +const CacheLineSize = 64 + +// cpuid is implemented in cpu_x86.s. +func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32) + +// xgetbv with ecx = 0 is implemented in cpu_x86.s. +func xgetbv() (eax, edx uint32) + +func init() { + maxId, _, _, _ := cpuid(0, 0) + + if maxId < 1 { + return + } + + _, _, ecx1, edx1 := cpuid(1, 0) + X86.HasSSE2 = isSet(26, edx1) + + X86.HasSSE3 = isSet(0, ecx1) + X86.HasPCLMULQDQ = isSet(1, ecx1) + X86.HasSSSE3 = isSet(9, ecx1) + X86.HasSSE41 = isSet(19, ecx1) + X86.HasSSE42 = isSet(20, ecx1) + X86.HasPOPCNT = isSet(23, ecx1) + X86.HasAES = isSet(25, ecx1) + X86.HasOSXSAVE = isSet(27, ecx1) + + osSupportsAVX := false + // For XGETBV, OSXSAVE bit is required and sufficient. + if X86.HasOSXSAVE { + eax, _ := xgetbv() + // Check if XMM and YMM registers have OS support. + osSupportsAVX = isSet(1, eax) && isSet(2, eax) + } + + X86.HasAVX = isSet(28, ecx1) && osSupportsAVX + + if maxId < 7 { + return + } + + _, ebx7, _, _ := cpuid(7, 0) + X86.HasBMI1 = isSet(3, ebx7) + X86.HasAVX2 = isSet(5, ebx7) && osSupportsAVX + X86.HasBMI2 = isSet(8, ebx7) + X86.HasERMS = isSet(9, ebx7) +} + +func isSet(bitpos uint, value uint32) bool { + return value&(1<<bitpos) != 0 +} |