-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//go:build gc | ||
|
||
package platform | ||
|
||
import "runtime" | ||
|
||
// CpuFeatures exposes the capabilities for this CPU, queried via the Has, HasExtra methods. | ||
var CpuFeatures = loadCpuFeatureFlags() | ||
|
||
// cpuFeatureFlags implements CpuFeatureFlags interface. | ||
type cpuFeatureFlags struct { | ||
isar0 uint64 | ||
isar1 uint64 | ||
} | ||
|
||
// implemented in cpuid_arm64.s | ||
func getisar0() uint64 | ||
|
||
// implemented in cpuid_arm64.s | ||
func getisar1() uint64 | ||
|
||
func loadCpuFeatureFlags() CpuFeatureFlags { | ||
switch runtime.GOOS { | ||
case "darwin", "windows": | ||
// These OSes require ARMv8.1, which includes atomic instructions. | ||
return &cpuFeatureFlags{ | ||
isar0: uint64(CpuFeatureArm64Atomic), | ||
isar1: 0, | ||
} | ||
case "linux", "freebsd": | ||
// These OSes allow reading the instruction set attribute registers. | ||
return &cpuFeatureFlags{ | ||
isar0: getisar0(), | ||
isar1: getisar1(), | ||
} | ||
default: | ||
return &cpuFeatureFlags{} | ||
} | ||
} | ||
|
||
// Has implements the same method on the CpuFeatureFlags interface. | ||
func (f *cpuFeatureFlags) Has(cpuFeature CpuFeature) bool { | ||
return (f.isar0 & uint64(cpuFeature)) != 0 | ||
} | ||
|
||
// HasExtra implements the same method on the CpuFeatureFlags interface. | ||
func (f *cpuFeatureFlags) HasExtra(cpuFeature CpuFeature) bool { | ||
return (f.isar1 & uint64(cpuFeature)) != 0 | ||
} | ||
|
||
// Raw implements the same method on the CpuFeatureFlags interface. | ||
func (f *cpuFeatureFlags) Raw() uint64 { | ||
// Below, we only set the first 4 bits for the features we care about, | ||
// instead of setting all the unnecessary bits obtained from the CPUID instruction. | ||
var ret uint64 | ||
switch runtime.GOARCH { | ||
case "arm64": | ||
if f.Has(CpuFeatureArm64Atomic) { | ||
ret = 1 << 0 | ||
} | ||
case "amd64": | ||
if f.Has(CpuFeatureAmd64SSE3) { | ||
ret = 1 << 0 | ||
} | ||
if f.Has(CpuFeatureAmd64SSE4_1) { | ||
ret |= 1 << 1 | ||
} | ||
if f.Has(CpuFeatureAmd64SSE4_2) { | ||
ret |= 1 << 2 | ||
} | ||
if f.HasExtra(CpuExtraFeatureAmd64ABM) { | ||
ret |= 1 << 3 | ||
} | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//go:build gc | ||
|
||
#include "textflag.h" | ||
|
||
// lifted from github.com/golang/sys and cpu/cpu_arm64.s | ||
|
||
// func getisar0() uint64 | ||
TEXT ·getisar0(SB), NOSPLIT, $0-8 | ||
// get Instruction Set Attributes 0 into x0 | ||
// mrs x0, ID_AA64ISAR0_EL1 = d5380600 | ||
WORD $0xd5380600 | ||
MOVD R0, ret+0(FP) | ||
RET | ||
|
||
// func getisar1() uint64 | ||
TEXT ·getisar1(SB), NOSPLIT, $0-8 | ||
// get Instruction Set Attributes 1 into x0 | ||
// mrs x0, ID_AA64ISAR1_EL1 = d5380620 | ||
WORD $0xd5380620 | ||
MOVD R0, ret+0(FP) | ||
RET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build !amd64 || tinygo | ||
//go:build !(amd64 || arm64) || !gc | ||
|
||
package platform | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters