You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu_x86.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import "runtime"
  7. const cacheLineSize = 64
  8. func initOptions() {
  9. options = []option{
  10. {Name: "adx", Feature: &X86.HasADX},
  11. {Name: "aes", Feature: &X86.HasAES},
  12. {Name: "avx", Feature: &X86.HasAVX},
  13. {Name: "avx2", Feature: &X86.HasAVX2},
  14. {Name: "bmi1", Feature: &X86.HasBMI1},
  15. {Name: "bmi2", Feature: &X86.HasBMI2},
  16. {Name: "erms", Feature: &X86.HasERMS},
  17. {Name: "fma", Feature: &X86.HasFMA},
  18. {Name: "osxsave", Feature: &X86.HasOSXSAVE},
  19. {Name: "pclmulqdq", Feature: &X86.HasPCLMULQDQ},
  20. {Name: "popcnt", Feature: &X86.HasPOPCNT},
  21. {Name: "rdrand", Feature: &X86.HasRDRAND},
  22. {Name: "rdseed", Feature: &X86.HasRDSEED},
  23. {Name: "sse3", Feature: &X86.HasSSE3},
  24. {Name: "sse41", Feature: &X86.HasSSE41},
  25. {Name: "sse42", Feature: &X86.HasSSE42},
  26. {Name: "ssse3", Feature: &X86.HasSSSE3},
  27. // These capabilities should always be enabled on amd64:
  28. {Name: "sse2", Feature: &X86.HasSSE2, Required: runtime.GOARCH == "amd64"},
  29. }
  30. }
  31. func archInit() {
  32. Initialized = true
  33. maxID, _, _, _ := cpuid(0, 0)
  34. if maxID < 1 {
  35. return
  36. }
  37. _, _, ecx1, edx1 := cpuid(1, 0)
  38. X86.HasSSE2 = isSet(26, edx1)
  39. X86.HasSSE3 = isSet(0, ecx1)
  40. X86.HasPCLMULQDQ = isSet(1, ecx1)
  41. X86.HasSSSE3 = isSet(9, ecx1)
  42. X86.HasFMA = isSet(12, ecx1)
  43. X86.HasSSE41 = isSet(19, ecx1)
  44. X86.HasSSE42 = isSet(20, ecx1)
  45. X86.HasPOPCNT = isSet(23, ecx1)
  46. X86.HasAES = isSet(25, ecx1)
  47. X86.HasOSXSAVE = isSet(27, ecx1)
  48. X86.HasRDRAND = isSet(30, ecx1)
  49. osSupportsAVX := false
  50. // For XGETBV, OSXSAVE bit is required and sufficient.
  51. if X86.HasOSXSAVE {
  52. eax, _ := xgetbv()
  53. // Check if XMM and YMM registers have OS support.
  54. osSupportsAVX = isSet(1, eax) && isSet(2, eax)
  55. }
  56. X86.HasAVX = isSet(28, ecx1) && osSupportsAVX
  57. if maxID < 7 {
  58. return
  59. }
  60. _, ebx7, _, _ := cpuid(7, 0)
  61. X86.HasBMI1 = isSet(3, ebx7)
  62. X86.HasAVX2 = isSet(5, ebx7) && osSupportsAVX
  63. X86.HasBMI2 = isSet(8, ebx7)
  64. X86.HasERMS = isSet(9, ebx7)
  65. X86.HasRDSEED = isSet(18, ebx7)
  66. X86.HasADX = isSet(19, ebx7)
  67. }
  68. func isSet(bitpos uint, value uint32) bool {
  69. return value&(1<<bitpos) != 0
  70. }