cpu_x86.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //go:build 386 || amd64 || amd64p32
  5. // +build 386 amd64 amd64p32
  6. package cpu
  7. import "runtime"
  8. const cacheLineSize = 64
  9. func initOptions() {
  10. options = []option{
  11. {Name: "adx", Feature: &X86.HasADX},
  12. {Name: "aes", Feature: &X86.HasAES},
  13. {Name: "avx", Feature: &X86.HasAVX},
  14. {Name: "avx2", Feature: &X86.HasAVX2},
  15. {Name: "avx512", Feature: &X86.HasAVX512},
  16. {Name: "avx512f", Feature: &X86.HasAVX512F},
  17. {Name: "avx512cd", Feature: &X86.HasAVX512CD},
  18. {Name: "avx512er", Feature: &X86.HasAVX512ER},
  19. {Name: "avx512pf", Feature: &X86.HasAVX512PF},
  20. {Name: "avx512vl", Feature: &X86.HasAVX512VL},
  21. {Name: "avx512bw", Feature: &X86.HasAVX512BW},
  22. {Name: "avx512dq", Feature: &X86.HasAVX512DQ},
  23. {Name: "avx512ifma", Feature: &X86.HasAVX512IFMA},
  24. {Name: "avx512vbmi", Feature: &X86.HasAVX512VBMI},
  25. {Name: "avx512vnniw", Feature: &X86.HasAVX5124VNNIW},
  26. {Name: "avx5124fmaps", Feature: &X86.HasAVX5124FMAPS},
  27. {Name: "avx512vpopcntdq", Feature: &X86.HasAVX512VPOPCNTDQ},
  28. {Name: "avx512vpclmulqdq", Feature: &X86.HasAVX512VPCLMULQDQ},
  29. {Name: "avx512vnni", Feature: &X86.HasAVX512VNNI},
  30. {Name: "avx512gfni", Feature: &X86.HasAVX512GFNI},
  31. {Name: "avx512vaes", Feature: &X86.HasAVX512VAES},
  32. {Name: "avx512vbmi2", Feature: &X86.HasAVX512VBMI2},
  33. {Name: "avx512bitalg", Feature: &X86.HasAVX512BITALG},
  34. {Name: "avx512bf16", Feature: &X86.HasAVX512BF16},
  35. {Name: "bmi1", Feature: &X86.HasBMI1},
  36. {Name: "bmi2", Feature: &X86.HasBMI2},
  37. {Name: "cx16", Feature: &X86.HasCX16},
  38. {Name: "erms", Feature: &X86.HasERMS},
  39. {Name: "fma", Feature: &X86.HasFMA},
  40. {Name: "osxsave", Feature: &X86.HasOSXSAVE},
  41. {Name: "pclmulqdq", Feature: &X86.HasPCLMULQDQ},
  42. {Name: "popcnt", Feature: &X86.HasPOPCNT},
  43. {Name: "rdrand", Feature: &X86.HasRDRAND},
  44. {Name: "rdseed", Feature: &X86.HasRDSEED},
  45. {Name: "sse3", Feature: &X86.HasSSE3},
  46. {Name: "sse41", Feature: &X86.HasSSE41},
  47. {Name: "sse42", Feature: &X86.HasSSE42},
  48. {Name: "ssse3", Feature: &X86.HasSSSE3},
  49. // These capabilities should always be enabled on amd64:
  50. {Name: "sse2", Feature: &X86.HasSSE2, Required: runtime.GOARCH == "amd64"},
  51. }
  52. }
  53. func archInit() {
  54. Initialized = true
  55. maxID, _, _, _ := cpuid(0, 0)
  56. if maxID < 1 {
  57. return
  58. }
  59. _, _, ecx1, edx1 := cpuid(1, 0)
  60. X86.HasSSE2 = isSet(26, edx1)
  61. X86.HasSSE3 = isSet(0, ecx1)
  62. X86.HasPCLMULQDQ = isSet(1, ecx1)
  63. X86.HasSSSE3 = isSet(9, ecx1)
  64. X86.HasFMA = isSet(12, ecx1)
  65. X86.HasCX16 = isSet(13, ecx1)
  66. X86.HasSSE41 = isSet(19, ecx1)
  67. X86.HasSSE42 = isSet(20, ecx1)
  68. X86.HasPOPCNT = isSet(23, ecx1)
  69. X86.HasAES = isSet(25, ecx1)
  70. X86.HasOSXSAVE = isSet(27, ecx1)
  71. X86.HasRDRAND = isSet(30, ecx1)
  72. var osSupportsAVX, osSupportsAVX512 bool
  73. // For XGETBV, OSXSAVE bit is required and sufficient.
  74. if X86.HasOSXSAVE {
  75. eax, _ := xgetbv()
  76. // Check if XMM and YMM registers have OS support.
  77. osSupportsAVX = isSet(1, eax) && isSet(2, eax)
  78. if runtime.GOOS == "darwin" {
  79. // Check darwin commpage for AVX512 support. Necessary because:
  80. // https://github.com/apple/darwin-xnu/blob/0a798f6738bc1db01281fc08ae024145e84df927/osfmk/i386/fpu.c#L175-L201
  81. osSupportsAVX512 = osSupportsAVX && darwinSupportsAVX512()
  82. } else {
  83. // Check if OPMASK and ZMM registers have OS support.
  84. osSupportsAVX512 = osSupportsAVX && isSet(5, eax) && isSet(6, eax) && isSet(7, eax)
  85. }
  86. }
  87. X86.HasAVX = isSet(28, ecx1) && osSupportsAVX
  88. if maxID < 7 {
  89. return
  90. }
  91. _, ebx7, ecx7, edx7 := cpuid(7, 0)
  92. X86.HasBMI1 = isSet(3, ebx7)
  93. X86.HasAVX2 = isSet(5, ebx7) && osSupportsAVX
  94. X86.HasBMI2 = isSet(8, ebx7)
  95. X86.HasERMS = isSet(9, ebx7)
  96. X86.HasRDSEED = isSet(18, ebx7)
  97. X86.HasADX = isSet(19, ebx7)
  98. X86.HasAVX512 = isSet(16, ebx7) && osSupportsAVX512 // Because avx-512 foundation is the core required extension
  99. if X86.HasAVX512 {
  100. X86.HasAVX512F = true
  101. X86.HasAVX512CD = isSet(28, ebx7)
  102. X86.HasAVX512ER = isSet(27, ebx7)
  103. X86.HasAVX512PF = isSet(26, ebx7)
  104. X86.HasAVX512VL = isSet(31, ebx7)
  105. X86.HasAVX512BW = isSet(30, ebx7)
  106. X86.HasAVX512DQ = isSet(17, ebx7)
  107. X86.HasAVX512IFMA = isSet(21, ebx7)
  108. X86.HasAVX512VBMI = isSet(1, ecx7)
  109. X86.HasAVX5124VNNIW = isSet(2, edx7)
  110. X86.HasAVX5124FMAPS = isSet(3, edx7)
  111. X86.HasAVX512VPOPCNTDQ = isSet(14, ecx7)
  112. X86.HasAVX512VPCLMULQDQ = isSet(10, ecx7)
  113. X86.HasAVX512VNNI = isSet(11, ecx7)
  114. X86.HasAVX512GFNI = isSet(8, ecx7)
  115. X86.HasAVX512VAES = isSet(9, ecx7)
  116. X86.HasAVX512VBMI2 = isSet(6, ecx7)
  117. X86.HasAVX512BITALG = isSet(12, ecx7)
  118. eax71, _, _, _ := cpuid(7, 1)
  119. X86.HasAVX512BF16 = isSet(5, eax71)
  120. }
  121. }
  122. func isSet(bitpos uint, value uint32) bool {
  123. return value&(1<<bitpos) != 0
  124. }