-
Notifications
You must be signed in to change notification settings - Fork 238
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: Edoardo Vacchi <evacchi@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
137 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package compiler | ||
|
||
const ( | ||
cpuFeatureSSE3 = uint64(1) | ||
cpuFeatureSSE4_1 = uint64(1) << 19 | ||
cpuFeatureSSE4_2 = uint64(1) << 20 | ||
) | ||
|
||
const ( | ||
cpuExtraFeatureABM = uint64(1) << 5 // Advanced Bit Manipulation | ||
) | ||
|
||
type cpuFeatureFlags struct { | ||
flags uint64 | ||
extraFlags uint64 | ||
} | ||
|
||
func loadCpuFeatureFlags() cpuFeatureFlags { | ||
return cpuFeatureFlags{ | ||
flags: cpuidAsBitmap(1, 0), | ||
extraFlags: cpuidAsBitmap(0x80000001, 0), | ||
} | ||
} | ||
|
||
// cpuid exposes the CPUID instruction to the Go layer (https://www.amd.com/system/files/TechDocs/25481.pdf) | ||
// implemented in impl_amd64.s | ||
func cpuid(arg1, arg2 uint32) (eax, ebx, ecx, edx uint32) | ||
|
||
// cpuidAsBitmap combines the result of invoking cpuid to uint64 bitmap | ||
func cpuidAsBitmap(arg1, arg2 uint32) uint64 { | ||
_ /* eax */, _ /* ebx */, ecx, edx := cpuid(1, 0) | ||
return (uint64(edx) << 32) | uint64(ecx) | ||
} | ||
|
||
// has invokes cpuid and returns true when the specified flag (represented as uint64) is supported | ||
func (f *cpuFeatureFlags) has(cpuFeature uint64) bool { | ||
return (f.flags & cpuFeature) != 0 | ||
} | ||
|
||
// has invokes cpuid and returns true when the specified extraFlag (represented as uint64) is supported | ||
func (f *cpuFeatureFlags) hasExtra(cpuFeature uint64) bool { | ||
return (f.extraFlags & cpuFeature) != 0 | ||
} |
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,14 @@ | ||
#include "textflag.h" | ||
|
||
// lifted from github.com/intel-go/cpuid and src/internal/cpu/cpu_x86.s | ||
// func cpuid(arg1, arg2 uint32) (eax, ebx, ecx, edx uint32) | ||
TEXT ·cpuid(SB), NOSPLIT, $0-24 | ||
MOVL arg1+0(FP), AX | ||
MOVL arg2+4(FP), CX | ||
CPUID | ||
MOVL AX, eax+8(FP) | ||
MOVL BX, ebx+12(FP) | ||
MOVL CX, ecx+16(FP) | ||
MOVL DX, edx+20(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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package compiler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/tetratelabs/wazero/internal/testing/require" | ||
) | ||
|
||
func TestAmd64CpuId_cpuHasFeature(t *testing.T) { | ||
flags := cpuFeatureFlags{ | ||
flags: cpuFeatureSSE3, | ||
extraFlags: cpuExtraFeatureABM, | ||
} | ||
require.True(t, flags.has(cpuFeatureSSE3)) | ||
require.False(t, flags.has(cpuFeatureSSE4_2)) | ||
require.True(t, flags.hasExtra(cpuExtraFeatureABM)) | ||
require.False(t, flags.hasExtra(1<<6)) // some other value | ||
} |
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