cpu.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Package cpu implements processor feature detection for
  5. // various CPU architectures.
  6. package cpu
  7. // Initialized reports whether the CPU features were initialized.
  8. //
  9. // For some GOOS/GOARCH combinations initialization of the CPU features depends
  10. // on reading an operating specific file, e.g. /proc/self/auxv on linux/arm
  11. // Initialized will report false if reading the file fails.
  12. var Initialized bool
  13. // CacheLinePad is used to pad structs to avoid false sharing.
  14. type CacheLinePad struct{ _ [cacheLineSize]byte }
  15. // X86 contains the supported CPU features of the
  16. // current X86/AMD64 platform. If the current platform
  17. // is not X86/AMD64 then all feature flags are false.
  18. //
  19. // X86 is padded to avoid false sharing. Further the HasAVX
  20. // and HasAVX2 are only set if the OS supports XMM and YMM
  21. // registers in addition to the CPUID feature bit being set.
  22. var X86 struct {
  23. _ CacheLinePad
  24. HasAES bool // AES hardware implementation (AES NI)
  25. HasADX bool // Multi-precision add-carry instruction extensions
  26. HasAVX bool // Advanced vector extension
  27. HasAVX2 bool // Advanced vector extension 2
  28. HasBMI1 bool // Bit manipulation instruction set 1
  29. HasBMI2 bool // Bit manipulation instruction set 2
  30. HasERMS bool // Enhanced REP for MOVSB and STOSB
  31. HasFMA bool // Fused-multiply-add instructions
  32. HasOSXSAVE bool // OS supports XSAVE/XRESTOR for saving/restoring XMM registers.
  33. HasPCLMULQDQ bool // PCLMULQDQ instruction - most often used for AES-GCM
  34. HasPOPCNT bool // Hamming weight instruction POPCNT.
  35. HasRDRAND bool // RDRAND instruction (on-chip random number generator)
  36. HasRDSEED bool // RDSEED instruction (on-chip random number generator)
  37. HasSSE2 bool // Streaming SIMD extension 2 (always available on amd64)
  38. HasSSE3 bool // Streaming SIMD extension 3
  39. HasSSSE3 bool // Supplemental streaming SIMD extension 3
  40. HasSSE41 bool // Streaming SIMD extension 4 and 4.1
  41. HasSSE42 bool // Streaming SIMD extension 4 and 4.2
  42. _ CacheLinePad
  43. }
  44. // ARM64 contains the supported CPU features of the
  45. // current ARMv8(aarch64) platform. If the current platform
  46. // is not arm64 then all feature flags are false.
  47. var ARM64 struct {
  48. _ CacheLinePad
  49. HasFP bool // Floating-point instruction set (always available)
  50. HasASIMD bool // Advanced SIMD (always available)
  51. HasEVTSTRM bool // Event stream support
  52. HasAES bool // AES hardware implementation
  53. HasPMULL bool // Polynomial multiplication instruction set
  54. HasSHA1 bool // SHA1 hardware implementation
  55. HasSHA2 bool // SHA2 hardware implementation
  56. HasCRC32 bool // CRC32 hardware implementation
  57. HasATOMICS bool // Atomic memory operation instruction set
  58. HasFPHP bool // Half precision floating-point instruction set
  59. HasASIMDHP bool // Advanced SIMD half precision instruction set
  60. HasCPUID bool // CPUID identification scheme registers
  61. HasASIMDRDM bool // Rounding double multiply add/subtract instruction set
  62. HasJSCVT bool // Javascript conversion from floating-point to integer
  63. HasFCMA bool // Floating-point multiplication and addition of complex numbers
  64. HasLRCPC bool // Release Consistent processor consistent support
  65. HasDCPOP bool // Persistent memory support
  66. HasSHA3 bool // SHA3 hardware implementation
  67. HasSM3 bool // SM3 hardware implementation
  68. HasSM4 bool // SM4 hardware implementation
  69. HasASIMDDP bool // Advanced SIMD double precision instruction set
  70. HasSHA512 bool // SHA512 hardware implementation
  71. HasSVE bool // Scalable Vector Extensions
  72. HasASIMDFHM bool // Advanced SIMD multiplication FP16 to FP32
  73. _ CacheLinePad
  74. }
  75. // ARM contains the supported CPU features of the current ARM (32-bit) platform.
  76. // All feature flags are false if:
  77. // 1. the current platform is not arm, or
  78. // 2. the current operating system is not Linux.
  79. var ARM struct {
  80. _ CacheLinePad
  81. HasSWP bool // SWP instruction support
  82. HasHALF bool // Half-word load and store support
  83. HasTHUMB bool // ARM Thumb instruction set
  84. Has26BIT bool // Address space limited to 26-bits
  85. HasFASTMUL bool // 32-bit operand, 64-bit result multiplication support
  86. HasFPA bool // Floating point arithmetic support
  87. HasVFP bool // Vector floating point support
  88. HasEDSP bool // DSP Extensions support
  89. HasJAVA bool // Java instruction set
  90. HasIWMMXT bool // Intel Wireless MMX technology support
  91. HasCRUNCH bool // MaverickCrunch context switching and handling
  92. HasTHUMBEE bool // Thumb EE instruction set
  93. HasNEON bool // NEON instruction set
  94. HasVFPv3 bool // Vector floating point version 3 support
  95. HasVFPv3D16 bool // Vector floating point version 3 D8-D15
  96. HasTLS bool // Thread local storage support
  97. HasVFPv4 bool // Vector floating point version 4 support
  98. HasIDIVA bool // Integer divide instruction support in ARM mode
  99. HasIDIVT bool // Integer divide instruction support in Thumb mode
  100. HasVFPD32 bool // Vector floating point version 3 D15-D31
  101. HasLPAE bool // Large Physical Address Extensions
  102. HasEVTSTRM bool // Event stream support
  103. HasAES bool // AES hardware implementation
  104. HasPMULL bool // Polynomial multiplication instruction set
  105. HasSHA1 bool // SHA1 hardware implementation
  106. HasSHA2 bool // SHA2 hardware implementation
  107. HasCRC32 bool // CRC32 hardware implementation
  108. _ CacheLinePad
  109. }
  110. // PPC64 contains the supported CPU features of the current ppc64/ppc64le platforms.
  111. // If the current platform is not ppc64/ppc64le then all feature flags are false.
  112. //
  113. // For ppc64/ppc64le, it is safe to check only for ISA level starting on ISA v3.00,
  114. // since there are no optional categories. There are some exceptions that also
  115. // require kernel support to work (DARN, SCV), so there are feature bits for
  116. // those as well. The minimum processor requirement is POWER8 (ISA 2.07).
  117. // The struct is padded to avoid false sharing.
  118. var PPC64 struct {
  119. _ CacheLinePad
  120. HasDARN bool // Hardware random number generator (requires kernel enablement)
  121. HasSCV bool // Syscall vectored (requires kernel enablement)
  122. IsPOWER8 bool // ISA v2.07 (POWER8)
  123. IsPOWER9 bool // ISA v3.00 (POWER9)
  124. _ CacheLinePad
  125. }
  126. // S390X contains the supported CPU features of the current IBM Z
  127. // (s390x) platform. If the current platform is not IBM Z then all
  128. // feature flags are false.
  129. //
  130. // S390X is padded to avoid false sharing. Further HasVX is only set
  131. // if the OS supports vector registers in addition to the STFLE
  132. // feature bit being set.
  133. var S390X struct {
  134. _ CacheLinePad
  135. HasZARCH bool // z/Architecture mode is active [mandatory]
  136. HasSTFLE bool // store facility list extended
  137. HasLDISP bool // long (20-bit) displacements
  138. HasEIMM bool // 32-bit immediates
  139. HasDFP bool // decimal floating point
  140. HasETF3EH bool // ETF-3 enhanced
  141. HasMSA bool // message security assist (CPACF)
  142. HasAES bool // KM-AES{128,192,256} functions
  143. HasAESCBC bool // KMC-AES{128,192,256} functions
  144. HasAESCTR bool // KMCTR-AES{128,192,256} functions
  145. HasAESGCM bool // KMA-GCM-AES{128,192,256} functions
  146. HasGHASH bool // KIMD-GHASH function
  147. HasSHA1 bool // K{I,L}MD-SHA-1 functions
  148. HasSHA256 bool // K{I,L}MD-SHA-256 functions
  149. HasSHA512 bool // K{I,L}MD-SHA-512 functions
  150. HasSHA3 bool // K{I,L}MD-SHA3-{224,256,384,512} and K{I,L}MD-SHAKE-{128,256} functions
  151. HasVX bool // vector facility
  152. HasVXE bool // vector-enhancements facility 1
  153. _ CacheLinePad
  154. }