Skip to content

Commit 05a8198

Browse files
hygonsocklauspost
authored andcommitted
add Hygon Dhyana CPU Vendor ID support (#29)
Signed-off-by: hygonsoc <hygonsoc@gmail.com>
1 parent 0da0211 commit 05a8198

5 files changed

Lines changed: 41 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Package home: https://github.com/klauspost/cpuid
8484
* **VMware**
8585
* **XenHVM**
8686
* **Bhyve**
87+
* **Hygon**
8788

8889
# installing
8990

cpuid.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
VMware
2828
XenHVM
2929
Bhyve
30+
Hygon
3031
)
3132

3233
const (
@@ -473,6 +474,11 @@ func (c CPUInfo) AMD() bool {
473474
return c.VendorID == AMD
474475
}
475476

477+
// Hygon returns true if vendor is recognized as Hygon
478+
func (c CPUInfo) Hygon() bool {
479+
return c.VendorID == Hygon
480+
}
481+
476482
// Transmeta returns true if vendor is recognized as Transmeta
477483
func (c CPUInfo) Transmeta() bool {
478484
return c.VendorID == Transmeta
@@ -626,7 +632,7 @@ func logicalCores() int {
626632
}
627633
_, b, _, _ := cpuidex(0xb, 1)
628634
return int(b & 0xffff)
629-
case AMD:
635+
case AMD, Hygon:
630636
_, b, _, _ := cpuid(1)
631637
return int((b >> 16) & 0xff)
632638
default:
@@ -648,7 +654,7 @@ func physicalCores() int {
648654
switch vendorID() {
649655
case Intel:
650656
return logicalCores() / threadsPerCore()
651-
case AMD:
657+
case AMD, Hygon:
652658
if maxExtendedFunction() >= 0x80000008 {
653659
_, _, c, _ := cpuid(0x80000008)
654660
return int(c&0xff) + 1
@@ -672,6 +678,7 @@ var vendorMapping = map[string]Vendor{
672678
"VMwareVMware": VMware,
673679
"XenVMMXenVMM": XenHVM,
674680
"bhyve bhyve ": Bhyve,
681+
"HygonGenuine": Hygon,
675682
}
676683

677684
func vendorID() Vendor {
@@ -744,7 +751,7 @@ func (c *CPUInfo) cacheSize() {
744751
c.Cache.L3 = size
745752
}
746753
}
747-
case AMD:
754+
case AMD, Hygon:
748755
// Untested.
749756
if maxExtendedFunction() < 0x80000005 {
750757
return

cpuid_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,16 @@ func TestAMD(t *testing.T) {
599599
t.Log("TestAMD:", got)
600600
}
601601

602+
// Hygon returns true if vendor is recognized as Hygon
603+
func TestHygon(t *testing.T) {
604+
got := CPU.Hygon()
605+
expected := CPU.VendorID == Hygon
606+
if got != expected {
607+
t.Fatalf("TestHygon: expected %v, got %v", expected, got)
608+
}
609+
t.Log("TestHygon:", got)
610+
}
611+
602612
// Transmeta returns true if vendor is recognized as Transmeta
603613
func TestTransmeta(t *testing.T) {
604614
got := CPU.Transmeta()

private/cpuid.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
msvm // Microsoft Hyper-V or Windows Virtual PC
2121
vmware
2222
xenhvm
23+
hygon
2324
)
2425

2526
const (
@@ -466,6 +467,11 @@ func (c cpuInfo) amd() bool {
466467
return c.vendorid == amd
467468
}
468469

470+
// Hygon returns true if vendor is recognized as Hygon
471+
func (c cpuInfo) hygon() bool {
472+
return c.vendorid == hygon
473+
}
474+
469475
// Transmeta returns true if vendor is recognized as Transmeta
470476
func (c cpuInfo) transmeta() bool {
471477
return c.vendorid == transmeta
@@ -619,7 +625,7 @@ func logicalCores() int {
619625
}
620626
_, b, _, _ := cpuidex(0xb, 1)
621627
return int(b & 0xffff)
622-
case amd:
628+
case amd, hygon:
623629
_, b, _, _ := cpuid(1)
624630
return int((b >> 16) & 0xff)
625631
default:
@@ -641,7 +647,7 @@ func physicalCores() int {
641647
switch vendorID() {
642648
case intel:
643649
return logicalCores() / threadsPerCore()
644-
case amd:
650+
case amd, hygon:
645651
if maxExtendedFunction() >= 0x80000008 {
646652
_, _, c, _ := cpuid(0x80000008)
647653
return int(c&0xff) + 1
@@ -664,6 +670,7 @@ var vendorMapping = map[string]vendor{
664670
"Microsoft Hv": msvm,
665671
"VMwareVMware": vmware,
666672
"XenVMMXenVMM": xenhvm,
673+
"HygonGenuine": hygon,
667674
}
668675

669676
func vendorID() vendor {
@@ -736,7 +743,7 @@ func (c *cpuInfo) cacheSize() {
736743
c.cache.l3 = size
737744
}
738745
}
739-
case amd:
746+
case amd, hygon:
740747
// Untested.
741748
if maxExtendedFunction() < 0x80000005 {
742749
return

private/cpuid_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,16 @@ func TestAMD(t *testing.T) {
601601
t.Log("TestAMD:", got)
602602
}
603603

604+
// Hygon returns true if vendor is recognized as Hygon
605+
func TestHygon(t *testing.T) {
606+
got := cpu.hygon()
607+
expected := cpu.vendorid == hygon
608+
if got != expected {
609+
t.Fatalf("TestHygon: expected %v, got %v", expected, got)
610+
}
611+
t.Log("TestHygon:", got)
612+
}
613+
604614
// Transmeta returns true if vendor is recognized as Transmeta
605615
func TestTransmeta(t *testing.T) {
606616
got := cpu.transmeta()

0 commit comments

Comments
 (0)