cpu_x86.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build 386 amd64 amd64p32
  5. package cpu
  6. const cacheLineSize = 64
  7. // cpuid is implemented in cpu_x86.s.
  8. func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)
  9. // xgetbv with ecx = 0 is implemented in cpu_x86.s.
  10. func xgetbv() (eax, edx uint32)
  11. func init() {
  12. maxID, _, _, _ := cpuid(0, 0)
  13. if maxID < 1 {
  14. return
  15. }
  16. _, _, ecx1, edx1 := cpuid(1, 0)
  17. X86.HasSSE2 = isSet(26, edx1)
  18. X86.HasSSE3 = isSet(0, ecx1)
  19. X86.HasPCLMULQDQ = isSet(1, ecx1)
  20. X86.HasSSSE3 = isSet(9, ecx1)
  21. X86.HasFMA = isSet(12, ecx1)
  22. X86.HasSSE41 = isSet(19, ecx1)
  23. X86.HasSSE42 = isSet(20, ecx1)
  24. X86.HasPOPCNT = isSet(23, ecx1)
  25. X86.HasAES = isSet(25, ecx1)
  26. X86.HasOSXSAVE = isSet(27, ecx1)
  27. osSupportsAVX := false
  28. // For XGETBV, OSXSAVE bit is required and sufficient.
  29. if X86.HasOSXSAVE {
  30. eax, _ := xgetbv()
  31. // Check if XMM and YMM registers have OS support.
  32. osSupportsAVX = isSet(1, eax) && isSet(2, eax)
  33. }
  34. X86.HasAVX = isSet(28, ecx1) && osSupportsAVX
  35. if maxID < 7 {
  36. return
  37. }
  38. _, ebx7, _, _ := cpuid(7, 0)
  39. X86.HasBMI1 = isSet(3, ebx7)
  40. X86.HasAVX2 = isSet(5, ebx7) && osSupportsAVX
  41. X86.HasBMI2 = isSet(8, ebx7)
  42. X86.HasERMS = isSet(9, ebx7)
  43. X86.HasADX = isSet(19, ebx7)
  44. }
  45. func isSet(bitpos uint, value uint32) bool {
  46. return value&(1<<bitpos) != 0
  47. }