-
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.
Move to platform, test Ctz, add interfaces for testing
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
183 additions
and
80 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
package platform | ||
|
||
const ( | ||
CpuFeatureSSE3 = uint64(1) | ||
CpuFeatureSSE4_1 = uint64(1) << 19 | ||
CpuFeatureSSE4_2 = uint64(1) << 20 | ||
) | ||
|
||
const ( | ||
CpuExtraFeatureABM = uint64(1) << 5 // Advanced Bit Manipulation | ||
) | ||
|
||
var CpuFeatures CpuFeatureFlags = loadCpuFeatureFlags() | ||
|
||
type CpuFeatureFlags interface { | ||
// Has returns true when the specified flag (represented as uint64) is supported | ||
Has(cpuFeature uint64) bool | ||
// HasExtra returns true when the specified extraFlag (represented as uint64) is supported | ||
HasExtra(cpuFeature uint64) bool | ||
} | ||
|
||
// cpuFeatureFlags implements CpuFeatureFlags interface | ||
type cpuFeatureFlags struct { | ||
flags uint64 | ||
extraFlags uint64 | ||
} | ||
|
||
// 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(arg1, arg2) | ||
return (uint64(edx) << 32) | uint64(ecx) | ||
} | ||
|
||
// loadStandardRange load flags from the standard range, panics otherwise | ||
func loadStandardRange(id uint32) uint64 { | ||
// ensure that the id is in the valid range, returned by cpuid(0,0) | ||
maxRange, _, _, _ := cpuid(0, 0) | ||
if id > maxRange { | ||
panic("cannot query standard CPU flags") | ||
} | ||
return cpuidAsBitmap(id, 0) | ||
} | ||
|
||
// loadStandardRange load flags from the extended range, panics otherwise | ||
func loadExtendedRange(id uint32) uint64 { | ||
// ensure that the id is in the valid range, returned by cpuid(0x80000000,0) | ||
maxRange, _, _, _ := cpuid(0x80000000, 0) | ||
if id > maxRange { | ||
panic("cannot query extended CPU flags") | ||
} | ||
return cpuidAsBitmap(id, 0) | ||
} | ||
|
||
func loadCpuFeatureFlags() CpuFeatureFlags { | ||
return &cpuFeatureFlags{ | ||
flags: loadStandardRange(1), | ||
extraFlags: loadExtendedRange(0x80000001), | ||
} | ||
} | ||
|
||
// Has implements the same method on the CpuFeatureFlags interface | ||
func (f *cpuFeatureFlags) Has(cpuFeature uint64) bool { | ||
return (f.flags & cpuFeature) != 0 | ||
} | ||
|
||
// HasExtra implements the same method on the CpuFeatureFlags interface | ||
func (f *cpuFeatureFlags) HasExtra(cpuFeature uint64) bool { | ||
return (f.extraFlags & cpuFeature) != 0 | ||
} |
File renamed without changes.
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 platform | ||
|
||
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 | ||
} |