cpuid_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // Generated, DO NOT EDIT,
  2. // but copy it to your own project and rename the package.
  3. // See more at http://github.com/klauspost/cpuid
  4. package cpuid
  5. import (
  6. "fmt"
  7. "testing"
  8. )
  9. // There is no real way to test a CPU identifier, since results will
  10. // obviously differ on each machine.
  11. func TestCPUID(t *testing.T) {
  12. n := maxFunctionID()
  13. t.Logf("Max Function:0x%x\n", n)
  14. n = maxExtendedFunction()
  15. t.Logf("Max Extended Function:0x%x\n", n)
  16. t.Log("Name:", cpu.brandname)
  17. t.Log("PhysicalCores:", cpu.physicalcores)
  18. t.Log("ThreadsPerCore:", cpu.threadspercore)
  19. t.Log("LogicalCores:", cpu.logicalcores)
  20. t.Log("Family", cpu.family, "Model:", cpu.model)
  21. t.Log("Features:", cpu.features)
  22. t.Log("Cacheline bytes:", cpu.cacheline)
  23. t.Log("L1 Instruction Cache:", cpu.cache.l1i, "bytes")
  24. t.Log("L1 Data Cache:", cpu.cache.l1d, "bytes")
  25. t.Log("L2 Cache:", cpu.cache.l2, "bytes")
  26. t.Log("L3 Cache:", cpu.cache.l3, "bytes")
  27. if cpu.sse2() {
  28. t.Log("We have SSE2")
  29. }
  30. }
  31. func TestDumpCPUID(t *testing.T) {
  32. n := int(maxFunctionID())
  33. for i := 0; i <= n; i++ {
  34. a, b, c, d := cpuidex(uint32(i), 0)
  35. t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a, b, c, d)
  36. ex := uint32(1)
  37. for {
  38. a2, b2, c2, d2 := cpuidex(uint32(i), ex)
  39. if a2 == a && b2 == b && d2 == d || ex > 50 || a2 == 0 {
  40. break
  41. }
  42. t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a2, b2, c2, d2)
  43. a, b, c, d = a2, b2, c2, d2
  44. ex++
  45. }
  46. }
  47. n2 := maxExtendedFunction()
  48. for i := uint32(0x80000000); i <= n2; i++ {
  49. a, b, c, d := cpuid(i)
  50. t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a, b, c, d)
  51. }
  52. }
  53. func example() {
  54. // Print basic CPU information:
  55. fmt.Println("Name:", cpu.brandname)
  56. fmt.Println("PhysicalCores:", cpu.physicalcores)
  57. fmt.Println("ThreadsPerCore:", cpu.threadspercore)
  58. fmt.Println("LogicalCores:", cpu.logicalcores)
  59. fmt.Println("Family", cpu.family, "Model:", cpu.model)
  60. fmt.Println("Features:", cpu.features)
  61. fmt.Println("Cacheline bytes:", cpu.cacheline)
  62. // Test if we have a specific feature:
  63. if cpu.sse() {
  64. fmt.Println("We have Streaming SIMD Extensions")
  65. }
  66. }
  67. func TestBrandNameZero(t *testing.T) {
  68. if len(cpu.brandname) > 0 {
  69. // Cut out last byte
  70. last := []byte(cpu.brandname[len(cpu.brandname)-1:])
  71. if last[0] == 0 {
  72. t.Fatal("last byte was zero")
  73. } else if last[0] == 32 {
  74. t.Fatal("whitespace wasn't trimmed")
  75. }
  76. }
  77. }
  78. // Generated here: http://play.golang.org/p/mko-0tFt0Q
  79. // TestCmov tests Cmov() function
  80. func TestCmov(t *testing.T) {
  81. got := cpu.cmov()
  82. expected := cpu.features&cmov == cmov
  83. if got != expected {
  84. t.Fatalf("Cmov: expected %v, got %v", expected, got)
  85. }
  86. t.Log("CMOV Support:", got)
  87. }
  88. // TestAmd3dnow tests Amd3dnow() function
  89. func TestAmd3dnow(t *testing.T) {
  90. got := cpu.amd3dnow()
  91. expected := cpu.features&amd3dnow == amd3dnow
  92. if got != expected {
  93. t.Fatalf("Amd3dnow: expected %v, got %v", expected, got)
  94. }
  95. t.Log("AMD3DNOW Support:", got)
  96. }
  97. // TestAmd3dnowExt tests Amd3dnowExt() function
  98. func TestAmd3dnowExt(t *testing.T) {
  99. got := cpu.amd3dnowext()
  100. expected := cpu.features&amd3dnowext == amd3dnowext
  101. if got != expected {
  102. t.Fatalf("Amd3dnowExt: expected %v, got %v", expected, got)
  103. }
  104. t.Log("AMD3DNOWEXT Support:", got)
  105. }
  106. // TestVMX tests VMX() function
  107. func TestVMX(t *testing.T) {
  108. got := cpu.vmx()
  109. expected := cpu.features&vmx == vmx
  110. if got != expected {
  111. t.Fatalf("VMX: expected %v, got %v", expected, got)
  112. }
  113. t.Log("VMX Support:", got)
  114. }
  115. // TestMMX tests MMX() function
  116. func TestMMX(t *testing.T) {
  117. got := cpu.mmx()
  118. expected := cpu.features&mmx == mmx
  119. if got != expected {
  120. t.Fatalf("MMX: expected %v, got %v", expected, got)
  121. }
  122. t.Log("MMX Support:", got)
  123. }
  124. // TestMMXext tests MMXext() function
  125. func TestMMXext(t *testing.T) {
  126. got := cpu.mmxext()
  127. expected := cpu.features&mmxext == mmxext
  128. if got != expected {
  129. t.Fatalf("MMXExt: expected %v, got %v", expected, got)
  130. }
  131. t.Log("MMXEXT Support:", got)
  132. }
  133. // TestSSE tests SSE() function
  134. func TestSSE(t *testing.T) {
  135. got := cpu.sse()
  136. expected := cpu.features&sse == sse
  137. if got != expected {
  138. t.Fatalf("SSE: expected %v, got %v", expected, got)
  139. }
  140. t.Log("SSE Support:", got)
  141. }
  142. // TestSSE2 tests SSE2() function
  143. func TestSSE2(t *testing.T) {
  144. got := cpu.sse2()
  145. expected := cpu.features&sse2 == sse2
  146. if got != expected {
  147. t.Fatalf("SSE2: expected %v, got %v", expected, got)
  148. }
  149. t.Log("SSE2 Support:", got)
  150. }
  151. // TestSSE3 tests SSE3() function
  152. func TestSSE3(t *testing.T) {
  153. got := cpu.sse3()
  154. expected := cpu.features&sse3 == sse3
  155. if got != expected {
  156. t.Fatalf("SSE3: expected %v, got %v", expected, got)
  157. }
  158. t.Log("SSE3 Support:", got)
  159. }
  160. // TestSSSE3 tests SSSE3() function
  161. func TestSSSE3(t *testing.T) {
  162. got := cpu.ssse3()
  163. expected := cpu.features&ssse3 == ssse3
  164. if got != expected {
  165. t.Fatalf("SSSE3: expected %v, got %v", expected, got)
  166. }
  167. t.Log("SSSE3 Support:", got)
  168. }
  169. // TestSSE4 tests SSE4() function
  170. func TestSSE4(t *testing.T) {
  171. got := cpu.sse4()
  172. expected := cpu.features&sse4 == sse4
  173. if got != expected {
  174. t.Fatalf("SSE4: expected %v, got %v", expected, got)
  175. }
  176. t.Log("SSE4 Support:", got)
  177. }
  178. // TestSSE42 tests SSE42() function
  179. func TestSSE42(t *testing.T) {
  180. got := cpu.sse42()
  181. expected := cpu.features&sse42 == sse42
  182. if got != expected {
  183. t.Fatalf("SSE42: expected %v, got %v", expected, got)
  184. }
  185. t.Log("SSE42 Support:", got)
  186. }
  187. // TestAVX tests AVX() function
  188. func TestAVX(t *testing.T) {
  189. got := cpu.avx()
  190. expected := cpu.features&avx == avx
  191. if got != expected {
  192. t.Fatalf("AVX: expected %v, got %v", expected, got)
  193. }
  194. t.Log("AVX Support:", got)
  195. }
  196. // TestAVX2 tests AVX2() function
  197. func TestAVX2(t *testing.T) {
  198. got := cpu.avx2()
  199. expected := cpu.features&avx2 == avx2
  200. if got != expected {
  201. t.Fatalf("AVX2: expected %v, got %v", expected, got)
  202. }
  203. t.Log("AVX2 Support:", got)
  204. }
  205. // TestFMA3 tests FMA3() function
  206. func TestFMA3(t *testing.T) {
  207. got := cpu.fma3()
  208. expected := cpu.features&fma3 == fma3
  209. if got != expected {
  210. t.Fatalf("FMA3: expected %v, got %v", expected, got)
  211. }
  212. t.Log("FMA3 Support:", got)
  213. }
  214. // TestFMA4 tests FMA4() function
  215. func TestFMA4(t *testing.T) {
  216. got := cpu.fma4()
  217. expected := cpu.features&fma4 == fma4
  218. if got != expected {
  219. t.Fatalf("FMA4: expected %v, got %v", expected, got)
  220. }
  221. t.Log("FMA4 Support:", got)
  222. }
  223. // TestXOP tests XOP() function
  224. func TestXOP(t *testing.T) {
  225. got := cpu.xop()
  226. expected := cpu.features&xop == xop
  227. if got != expected {
  228. t.Fatalf("XOP: expected %v, got %v", expected, got)
  229. }
  230. t.Log("XOP Support:", got)
  231. }
  232. // TestF16C tests F16C() function
  233. func TestF16C(t *testing.T) {
  234. got := cpu.f16c()
  235. expected := cpu.features&f16c == f16c
  236. if got != expected {
  237. t.Fatalf("F16C: expected %v, got %v", expected, got)
  238. }
  239. t.Log("F16C Support:", got)
  240. }
  241. // TestCX16 tests CX16() function
  242. func TestCX16(t *testing.T) {
  243. got := cpu.cx16()
  244. expected := cpu.features&cx16 == cx16
  245. if got != expected {
  246. t.Fatalf("CX16: expected %v, got %v", expected, got)
  247. }
  248. t.Log("CX16 Support:", got)
  249. }
  250. // TestSGX tests SGX detection
  251. func TestSGX(t *testing.T) {
  252. got := cpu.sgx.available
  253. expected := cpu.features&sgx == sgx
  254. if got != expected {
  255. t.Fatalf("SGX: expected %v, got %v", expected, got)
  256. }
  257. t.Log("SGX Support:", got)
  258. var total uint64 = 0
  259. if cpu.sgx.available {
  260. for _, s := range cpu.sgx.epcsections {
  261. t.Logf("SGX EPC section: base address 0x%x, size %v", s.baseaddress, s.epcsize)
  262. total += s.epcsize
  263. }
  264. if total == 0 {
  265. t.Fatal("SGX enabled without any available EPC memory")
  266. }
  267. }
  268. }
  269. // TestSGXLC tests SGX Launch Control detection
  270. func TestSGXLC(t *testing.T) {
  271. got := cpu.sgx.launchcontrol
  272. expected := cpu.features&sgxlc == sgxlc
  273. if got != expected {
  274. t.Fatalf("SGX: expected %v, got %v", expected, got)
  275. }
  276. t.Log("SGX Launch Control Support:", got)
  277. }
  278. // TestBMI1 tests BMI1() function
  279. func TestBMI1(t *testing.T) {
  280. got := cpu.bmi1()
  281. expected := cpu.features&bmi1 == bmi1
  282. if got != expected {
  283. t.Fatalf("BMI1: expected %v, got %v", expected, got)
  284. }
  285. t.Log("BMI1 Support:", got)
  286. }
  287. // TestBMI2 tests BMI2() function
  288. func TestBMI2(t *testing.T) {
  289. got := cpu.bmi2()
  290. expected := cpu.features&bmi2 == bmi2
  291. if got != expected {
  292. t.Fatalf("BMI2: expected %v, got %v", expected, got)
  293. }
  294. t.Log("BMI2 Support:", got)
  295. }
  296. // TestTBM tests TBM() function
  297. func TestTBM(t *testing.T) {
  298. got := cpu.tbm()
  299. expected := cpu.features&tbm == tbm
  300. if got != expected {
  301. t.Fatalf("TBM: expected %v, got %v", expected, got)
  302. }
  303. t.Log("TBM Support:", got)
  304. }
  305. // TestLzcnt tests Lzcnt() function
  306. func TestLzcnt(t *testing.T) {
  307. got := cpu.lzcnt()
  308. expected := cpu.features&lzcnt == lzcnt
  309. if got != expected {
  310. t.Fatalf("Lzcnt: expected %v, got %v", expected, got)
  311. }
  312. t.Log("LZCNT Support:", got)
  313. }
  314. // TestLzcnt tests Lzcnt() function
  315. func TestPopcnt(t *testing.T) {
  316. got := cpu.popcnt()
  317. expected := cpu.features&popcnt == popcnt
  318. if got != expected {
  319. t.Fatalf("Popcnt: expected %v, got %v", expected, got)
  320. }
  321. t.Log("POPCNT Support:", got)
  322. }
  323. // TestAesNi tests AesNi() function
  324. func TestAesNi(t *testing.T) {
  325. got := cpu.aesni()
  326. expected := cpu.features&aesni == aesni
  327. if got != expected {
  328. t.Fatalf("AesNi: expected %v, got %v", expected, got)
  329. }
  330. t.Log("AESNI Support:", got)
  331. }
  332. // TestHTT tests HTT() function
  333. func TestHTT(t *testing.T) {
  334. got := cpu.htt()
  335. expected := cpu.features&htt == htt
  336. if got != expected {
  337. t.Fatalf("HTT: expected %v, got %v", expected, got)
  338. }
  339. t.Log("HTT Support:", got)
  340. }
  341. // TestClmul tests Clmul() function
  342. func TestClmul(t *testing.T) {
  343. got := cpu.clmul()
  344. expected := cpu.features&clmul == clmul
  345. if got != expected {
  346. t.Fatalf("Clmul: expected %v, got %v", expected, got)
  347. }
  348. t.Log("CLMUL Support:", got)
  349. }
  350. // TestSSE2Slow tests SSE2Slow() function
  351. func TestSSE2Slow(t *testing.T) {
  352. got := cpu.sse2slow()
  353. expected := cpu.features&sse2slow == sse2slow
  354. if got != expected {
  355. t.Fatalf("SSE2Slow: expected %v, got %v", expected, got)
  356. }
  357. t.Log("SSE2SLOW Support:", got)
  358. }
  359. // TestSSE3Slow tests SSE3slow() function
  360. func TestSSE3Slow(t *testing.T) {
  361. got := cpu.sse3slow()
  362. expected := cpu.features&sse3slow == sse3slow
  363. if got != expected {
  364. t.Fatalf("SSE3slow: expected %v, got %v", expected, got)
  365. }
  366. t.Log("SSE3SLOW Support:", got)
  367. }
  368. // TestAtom tests Atom() function
  369. func TestAtom(t *testing.T) {
  370. got := cpu.atom()
  371. expected := cpu.features&atom == atom
  372. if got != expected {
  373. t.Fatalf("Atom: expected %v, got %v", expected, got)
  374. }
  375. t.Log("ATOM Support:", got)
  376. }
  377. // TestNX tests NX() function (NX (No-Execute) bit)
  378. func TestNX(t *testing.T) {
  379. got := cpu.nx()
  380. expected := cpu.features&nx == nx
  381. if got != expected {
  382. t.Fatalf("NX: expected %v, got %v", expected, got)
  383. }
  384. t.Log("NX Support:", got)
  385. }
  386. // TestSSE4A tests SSE4A() function (AMD Barcelona microarchitecture SSE4a instructions)
  387. func TestSSE4A(t *testing.T) {
  388. got := cpu.sse4a()
  389. expected := cpu.features&sse4a == sse4a
  390. if got != expected {
  391. t.Fatalf("SSE4A: expected %v, got %v", expected, got)
  392. }
  393. t.Log("SSE4A Support:", got)
  394. }
  395. // TestHLE tests HLE() function (Hardware Lock Elision)
  396. func TestHLE(t *testing.T) {
  397. got := cpu.hle()
  398. expected := cpu.features&hle == hle
  399. if got != expected {
  400. t.Fatalf("HLE: expected %v, got %v", expected, got)
  401. }
  402. t.Log("HLE Support:", got)
  403. }
  404. // TestRTM tests RTM() function (Restricted Transactional Memory)
  405. func TestRTM(t *testing.T) {
  406. got := cpu.rtm()
  407. expected := cpu.features&rtm == rtm
  408. if got != expected {
  409. t.Fatalf("RTM: expected %v, got %v", expected, got)
  410. }
  411. t.Log("RTM Support:", got)
  412. }
  413. // TestRdrand tests RDRAND() function (RDRAND instruction is available)
  414. func TestRdrand(t *testing.T) {
  415. got := cpu.rdrand()
  416. expected := cpu.features&rdrand == rdrand
  417. if got != expected {
  418. t.Fatalf("Rdrand: expected %v, got %v", expected, got)
  419. }
  420. t.Log("Rdrand Support:", got)
  421. }
  422. // TestRdseed tests RDSEED() function (RDSEED instruction is available)
  423. func TestRdseed(t *testing.T) {
  424. got := cpu.rdseed()
  425. expected := cpu.features&rdseed == rdseed
  426. if got != expected {
  427. t.Fatalf("Rdseed: expected %v, got %v", expected, got)
  428. }
  429. t.Log("Rdseed Support:", got)
  430. }
  431. // TestADX tests ADX() function (Intel ADX (Multi-Precision Add-Carry Instruction Extensions))
  432. func TestADX(t *testing.T) {
  433. got := cpu.adx()
  434. expected := cpu.features&adx == adx
  435. if got != expected {
  436. t.Fatalf("ADX: expected %v, got %v", expected, got)
  437. }
  438. t.Log("ADX Support:", got)
  439. }
  440. // TestSHA tests SHA() function (Intel SHA Extensions)
  441. func TestSHA(t *testing.T) {
  442. got := cpu.sha()
  443. expected := cpu.features&sha == sha
  444. if got != expected {
  445. t.Fatalf("SHA: expected %v, got %v", expected, got)
  446. }
  447. t.Log("SHA Support:", got)
  448. }
  449. // TestAVX512F tests AVX512F() function (AVX-512 Foundation)
  450. func TestAVX512F(t *testing.T) {
  451. got := cpu.avx512f()
  452. expected := cpu.features&avx512f == avx512f
  453. if got != expected {
  454. t.Fatalf("AVX512F: expected %v, got %v", expected, got)
  455. }
  456. t.Log("AVX512F Support:", got)
  457. }
  458. // TestAVX512DQ tests AVX512DQ() function (AVX-512 Doubleword and Quadword Instructions)
  459. func TestAVX512DQ(t *testing.T) {
  460. got := cpu.avx512dq()
  461. expected := cpu.features&avx512dq == avx512dq
  462. if got != expected {
  463. t.Fatalf("AVX512DQ: expected %v, got %v", expected, got)
  464. }
  465. t.Log("AVX512DQ Support:", got)
  466. }
  467. // TestAVX512IFMA tests AVX512IFMA() function (AVX-512 Integer Fused Multiply-Add Instructions)
  468. func TestAVX512IFMA(t *testing.T) {
  469. got := cpu.avx512ifma()
  470. expected := cpu.features&avx512ifma == avx512ifma
  471. if got != expected {
  472. t.Fatalf("AVX512IFMA: expected %v, got %v", expected, got)
  473. }
  474. t.Log("AVX512IFMA Support:", got)
  475. }
  476. // TestAVX512PF tests AVX512PF() function (AVX-512 Prefetch Instructions)
  477. func TestAVX512PF(t *testing.T) {
  478. got := cpu.avx512pf()
  479. expected := cpu.features&avx512pf == avx512pf
  480. if got != expected {
  481. t.Fatalf("AVX512PF: expected %v, got %v", expected, got)
  482. }
  483. t.Log("AVX512PF Support:", got)
  484. }
  485. // TestAVX512ER tests AVX512ER() function (AVX-512 Exponential and Reciprocal Instructions)
  486. func TestAVX512ER(t *testing.T) {
  487. got := cpu.avx512er()
  488. expected := cpu.features&avx512er == avx512er
  489. if got != expected {
  490. t.Fatalf("AVX512ER: expected %v, got %v", expected, got)
  491. }
  492. t.Log("AVX512ER Support:", got)
  493. }
  494. // TestAVX512CD tests AVX512CD() function (AVX-512 Conflict Detection Instructions)
  495. func TestAVX512CD(t *testing.T) {
  496. got := cpu.avx512cd()
  497. expected := cpu.features&avx512cd == avx512cd
  498. if got != expected {
  499. t.Fatalf("AVX512CD: expected %v, got %v", expected, got)
  500. }
  501. t.Log("AVX512CD Support:", got)
  502. }
  503. // TestAVX512BW tests AVX512BW() function (AVX-512 Byte and Word Instructions)
  504. func TestAVX512BW(t *testing.T) {
  505. got := cpu.avx512bw()
  506. expected := cpu.features&avx512bw == avx512bw
  507. if got != expected {
  508. t.Fatalf("AVX512BW: expected %v, got %v", expected, got)
  509. }
  510. t.Log("AVX512BW Support:", got)
  511. }
  512. // TestAVX512VL tests AVX512VL() function (AVX-512 Vector Length Extensions)
  513. func TestAVX512VL(t *testing.T) {
  514. got := cpu.avx512vl()
  515. expected := cpu.features&avx512vl == avx512vl
  516. if got != expected {
  517. t.Fatalf("AVX512VL: expected %v, got %v", expected, got)
  518. }
  519. t.Log("AVX512VL Support:", got)
  520. }
  521. // TestAVX512VBMI tests AVX512VBMI() function (AVX-512 Vector Bit Manipulation Instructions)
  522. func TestAVX512VBMI(t *testing.T) {
  523. got := cpu.avx512vbmi()
  524. expected := cpu.features&avx512vbmi == avx512vbmi
  525. if got != expected {
  526. t.Fatalf("AVX512VBMI: expected %v, got %v", expected, got)
  527. }
  528. t.Log("AVX512VBMI Support:", got)
  529. }
  530. // TestAVX512_VBMI2 tests AVX512VBMI2 function (AVX-512 Vector Bit Manipulation Instructions, Version 2)
  531. func TestAVX512_VBMI2(t *testing.T) {
  532. got := cpu.avx512vbmi2()
  533. expected := cpu.features&avx512vbmi2 == avx512vbmi2
  534. if got != expected {
  535. t.Fatalf("AVX512VBMI2: expected %v, got %v", expected, got)
  536. }
  537. t.Log("AVX512VBMI2 Support:", got)
  538. }
  539. // TestAVX512_VNNI tests AVX512VNNI() function (AVX-512 Vector Neural Network Instructions)
  540. func TestAVX512_VNNI(t *testing.T) {
  541. got := cpu.avx512vnni()
  542. expected := cpu.features&avx512vnni == avx512vnni
  543. if got != expected {
  544. t.Fatalf("AVX512VNNI: expected %v, got %v", expected, got)
  545. }
  546. t.Log("AVX512VNNI Support:", got)
  547. }
  548. // TestAVX512_VPOPCNTDQ tests AVX512VPOPCNTDQ() function (AVX-512 Vector Population Count Doubleword and Quadword)
  549. func TestAVX512_VPOPCNTDQ(t *testing.T) {
  550. got := cpu.avx512vpopcntdq()
  551. expected := cpu.features&avx512vpopcntdq == avx512vpopcntdq
  552. if got != expected {
  553. t.Fatalf("AVX512VPOPCNTDQ: expected %v, got %v", expected, got)
  554. }
  555. t.Log("AVX512VPOPCNTDQ Support:", got)
  556. }
  557. // TestGFNI tests GFNI() function (Galois Field New Instructions)
  558. func TestGFNI(t *testing.T) {
  559. got := cpu.gfni()
  560. expected := cpu.features&gfni == gfni
  561. if got != expected {
  562. t.Fatalf("GFNI: expected %v, got %v", expected, got)
  563. }
  564. t.Log("GFNI Support:", got)
  565. }
  566. // TestVAES tests VAES() function (Vector AES)
  567. func TestVAES(t *testing.T) {
  568. got := cpu.vaes()
  569. expected := cpu.features&vaes == vaes
  570. if got != expected {
  571. t.Fatalf("VAES: expected %v, got %v", expected, got)
  572. }
  573. t.Log("VAES Support:", got)
  574. }
  575. // TestAVX512_BITALG tests AVX512BITALG() function (AVX-512 Bit Algorithms)
  576. func TestAVX512_BITALG(t *testing.T) {
  577. got := cpu.avx512bitalg()
  578. expected := cpu.features&avx512bitalg == avx512bitalg
  579. if got != expected {
  580. t.Fatalf("AVX512BITALG: expected %v, got %v", expected, got)
  581. }
  582. t.Log("AVX512BITALG Support:", got)
  583. }
  584. // TestVPCLMULQDQ tests VPCLMULQDQ() function (Carry-Less Multiplication Quadword)
  585. func TestVPCLMULQDQ(t *testing.T) {
  586. got := cpu.vpclmulqdq()
  587. expected := cpu.features&vpclmulqdq == vpclmulqdq
  588. if got != expected {
  589. t.Fatalf("VPCLMULQDQ: expected %v, got %v", expected, got)
  590. }
  591. t.Log("VPCLMULQDQ Support:", got)
  592. }
  593. // TestAVX512_BF16 tests AVX512BF16() function (AVX-512 BFLOAT16 Instructions)
  594. func TestAVX512_BF16(t *testing.T) {
  595. got := cpu.avx512bf16()
  596. expected := cpu.features&avx512bf16 == avx512bf16
  597. if got != expected {
  598. t.Fatalf("AVX512BF16: expected %v, got %v", expected, got)
  599. }
  600. t.Log("AVX512BF16 Support:", got)
  601. }
  602. // TestAVX512_VP2INTERSECT tests AVX512VP2INTERSECT() function (AVX-512 Intersect for D/Q)
  603. func TestAVX512_VP2INTERSECT(t *testing.T) {
  604. got := cpu.avx512vp2intersect()
  605. expected := cpu.features&avx512vp2intersect == avx512vp2intersect
  606. if got != expected {
  607. t.Fatalf("AVX512VP2INTERSECT: expected %v, got %v", expected, got)
  608. }
  609. t.Log("AVX512VP2INTERSECT Support:", got)
  610. }
  611. // TestMPX tests MPX() function (Intel MPX (Memory Protection Extensions))
  612. func TestMPX(t *testing.T) {
  613. got := cpu.mpx()
  614. expected := cpu.features&mpx == mpx
  615. if got != expected {
  616. t.Fatalf("MPX: expected %v, got %v", expected, got)
  617. }
  618. t.Log("MPX Support:", got)
  619. }
  620. // TestERMS tests ERMS() function (Enhanced REP MOVSB/STOSB)
  621. func TestERMS(t *testing.T) {
  622. got := cpu.erms()
  623. expected := cpu.features&erms == erms
  624. if got != expected {
  625. t.Fatalf("ERMS: expected %v, got %v", expected, got)
  626. }
  627. t.Log("ERMS Support:", got)
  628. }
  629. // TestVendor writes the detected vendor. Will be 0 if unknown
  630. func TestVendor(t *testing.T) {
  631. t.Log("Vendor ID:", cpu.vendorid)
  632. }
  633. // Intel returns true if vendor is recognized as Intel
  634. func TestIntel(t *testing.T) {
  635. got := cpu.intel()
  636. expected := cpu.vendorid == intel
  637. if got != expected {
  638. t.Fatalf("TestIntel: expected %v, got %v", expected, got)
  639. }
  640. t.Log("TestIntel:", got)
  641. }
  642. // AMD returns true if vendor is recognized as AMD
  643. func TestAMD(t *testing.T) {
  644. got := cpu.amd()
  645. expected := cpu.vendorid == amd
  646. if got != expected {
  647. t.Fatalf("TestAMD: expected %v, got %v", expected, got)
  648. }
  649. t.Log("TestAMD:", got)
  650. }
  651. // Hygon returns true if vendor is recognized as Hygon
  652. func TestHygon(t *testing.T) {
  653. got := cpu.hygon()
  654. expected := cpu.vendorid == hygon
  655. if got != expected {
  656. t.Fatalf("TestHygon: expected %v, got %v", expected, got)
  657. }
  658. t.Log("TestHygon:", got)
  659. }
  660. // Transmeta returns true if vendor is recognized as Transmeta
  661. func TestTransmeta(t *testing.T) {
  662. got := cpu.transmeta()
  663. expected := cpu.vendorid == transmeta
  664. if got != expected {
  665. t.Fatalf("TestTransmeta: expected %v, got %v", expected, got)
  666. }
  667. t.Log("TestTransmeta:", got)
  668. }
  669. // NSC returns true if vendor is recognized as National Semiconductor
  670. func TestNSC(t *testing.T) {
  671. got := cpu.nsc()
  672. expected := cpu.vendorid == nsc
  673. if got != expected {
  674. t.Fatalf("TestNSC: expected %v, got %v", expected, got)
  675. }
  676. t.Log("TestNSC:", got)
  677. }
  678. // VIA returns true if vendor is recognized as VIA
  679. func TestVIA(t *testing.T) {
  680. got := cpu.via()
  681. expected := cpu.vendorid == via
  682. if got != expected {
  683. t.Fatalf("TestVIA: expected %v, got %v", expected, got)
  684. }
  685. t.Log("TestVIA:", got)
  686. }
  687. // Test VM function
  688. func TestVM(t *testing.T) {
  689. t.Log("Vendor ID:", cpu.vm())
  690. }
  691. // TSX returns true if cpu supports transactional sync extensions.
  692. func TestCPUInfo_TSX(t *testing.T) {
  693. got := cpu.tsx()
  694. expected := cpu.hle() && cpu.rtm()
  695. if got != expected {
  696. t.Fatalf("TestCPUInfo_TSX: expected %v, got %v", expected, got)
  697. }
  698. t.Log("TestCPUInfo_TSX:", got)
  699. }
  700. // Test RTCounter function
  701. func TestRtCounter(t *testing.T) {
  702. a := cpu.rtcounter()
  703. b := cpu.rtcounter()
  704. t.Log("CPU Counter:", a, b, b-a)
  705. }
  706. // Prints the value of Ia32TscAux()
  707. func TestIa32TscAux(t *testing.T) {
  708. ecx := cpu.ia32tscaux()
  709. t.Logf("Ia32TscAux:0x%x\n", ecx)
  710. if ecx != 0 {
  711. chip := (ecx & 0xFFF000) >> 12
  712. core := ecx & 0xFFF
  713. t.Log("Likely chip, core:", chip, core)
  714. }
  715. }
  716. func TestThreadsPerCoreNZ(t *testing.T) {
  717. if cpu.threadspercore == 0 {
  718. t.Fatal("threads per core is zero")
  719. }
  720. }
  721. // Prints the value of LogicalCPU()
  722. func TestLogicalCPU(t *testing.T) {
  723. t.Log("Currently executing on cpu:", cpu.logicalcpu())
  724. }
  725. func TestMaxFunction(t *testing.T) {
  726. expect := maxFunctionID()
  727. if cpu.maxFunc != expect {
  728. t.Fatal("Max function does not match, expected", expect, "but got", cpu.maxFunc)
  729. }
  730. expect = maxExtendedFunction()
  731. if cpu.maxExFunc != expect {
  732. t.Fatal("Max Extended function does not match, expected", expect, "but got", cpu.maxFunc)
  733. }
  734. }
  735. // This example will calculate the chip/core number on Linux
  736. // Linux encodes numa id (<<12) and core id (8bit) into TSC_AUX.
  737. func examplecpuinfo_ia32tscaux() {
  738. ecx := cpu.ia32tscaux()
  739. if ecx == 0 {
  740. fmt.Println("Unknown CPU ID")
  741. return
  742. }
  743. chip := (ecx & 0xFFF000) >> 12
  744. core := ecx & 0xFFF
  745. fmt.Println("Chip, Core:", chip, core)
  746. }
  747. /*
  748. func TestPhysical(t *testing.T) {
  749. var test16 = "CPUID 00000000: 0000000d-756e6547-6c65746e-49656e69 \nCPUID 00000001: 000206d7-03200800-1fbee3ff-bfebfbff \nCPUID 00000002: 76035a01-00f0b2ff-00000000-00ca0000 \nCPUID 00000003: 00000000-00000000-00000000-00000000 \nCPUID 00000004: 3c004121-01c0003f-0000003f-00000000 \nCPUID 00000004: 3c004122-01c0003f-0000003f-00000000 \nCPUID 00000004: 3c004143-01c0003f-000001ff-00000000 \nCPUID 00000004: 3c07c163-04c0003f-00003fff-00000006 \nCPUID 00000005: 00000040-00000040-00000003-00021120 \nCPUID 00000006: 00000075-00000002-00000009-00000000 \nCPUID 00000007: 00000000-00000000-00000000-00000000 \nCPUID 00000008: 00000000-00000000-00000000-00000000 \nCPUID 00000009: 00000001-00000000-00000000-00000000 \nCPUID 0000000a: 07300403-00000000-00000000-00000603 \nCPUID 0000000b: 00000000-00000000-00000003-00000003 \nCPUID 0000000b: 00000005-00000010-00000201-00000003 \nCPUID 0000000c: 00000000-00000000-00000000-00000000 \nCPUID 0000000d: 00000007-00000340-00000340-00000000 \nCPUID 0000000d: 00000001-00000000-00000000-00000000 \nCPUID 0000000d: 00000100-00000240-00000000-00000000 \nCPUID 80000000: 80000008-00000000-00000000-00000000 \nCPUID 80000001: 00000000-00000000-00000001-2c100800 \nCPUID 80000002: 20202020-49202020-6c65746e-20295228 \nCPUID 80000003: 6e6f6558-20295228-20555043-322d3545 \nCPUID 80000004: 20303636-20402030-30322e32-007a4847 \nCPUID 80000005: 00000000-00000000-00000000-00000000 \nCPUID 80000006: 00000000-00000000-01006040-00000000 \nCPUID 80000007: 00000000-00000000-00000000-00000100 \nCPUID 80000008: 0000302e-00000000-00000000-00000000"
  750. restore := mockCPU([]byte(test16))
  751. Detect()
  752. t.Log("Name:", CPU.BrandName)
  753. n := maxFunctionID()
  754. t.Logf("Max Function:0x%x\n", n)
  755. n = maxExtendedFunction()
  756. t.Logf("Max Extended Function:0x%x\n", n)
  757. t.Log("PhysicalCores:", CPU.PhysicalCores)
  758. t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
  759. t.Log("LogicalCores:", CPU.LogicalCores)
  760. t.Log("Family", CPU.Family, "Model:", CPU.Model)
  761. t.Log("Features:", CPU.Features)
  762. t.Log("Cacheline bytes:", CPU.CacheLine)
  763. t.Log("L1 Instruction Cache:", CPU.Cache.L1I, "bytes")
  764. t.Log("L1 Data Cache:", CPU.Cache.L1D, "bytes")
  765. t.Log("L2 Cache:", CPU.Cache.L2, "bytes")
  766. t.Log("L3 Cache:", CPU.Cache.L3, "bytes")
  767. if CPU.LogicalCores > 0 && CPU.PhysicalCores > 0 {
  768. if CPU.LogicalCores != CPU.PhysicalCores*CPU.ThreadsPerCore {
  769. t.Fatalf("Core count mismatch, LogicalCores (%d) != PhysicalCores (%d) * CPU.ThreadsPerCore (%d)",
  770. CPU.LogicalCores, CPU.PhysicalCores, CPU.ThreadsPerCore)
  771. }
  772. }
  773. if CPU.ThreadsPerCore > 1 && !CPU.HTT() {
  774. t.Fatalf("Hyperthreading not detected")
  775. }
  776. if CPU.ThreadsPerCore == 1 && CPU.HTT() {
  777. t.Fatalf("Hyperthreading detected, but only 1 Thread per core")
  778. }
  779. restore()
  780. Detect()
  781. TestCPUID(t)
  782. }
  783. */