cpuid.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Minio Cloud Storage, (C) 2016 Minio, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package sha256
  16. // True when SIMD instructions are available.
  17. var avx512 bool
  18. var avx2 bool
  19. var avx bool
  20. var sse bool
  21. var sse2 bool
  22. var sse3 bool
  23. var ssse3 bool
  24. var sse41 bool
  25. var sse42 bool
  26. var popcnt bool
  27. var sha bool
  28. var armSha = haveArmSha()
  29. func init() {
  30. var _xsave bool
  31. var _osxsave bool
  32. var _avx bool
  33. var _avx2 bool
  34. var _avx512f bool
  35. var _avx512dq bool
  36. // var _avx512pf bool
  37. // var _avx512er bool
  38. // var _avx512cd bool
  39. var _avx512bw bool
  40. var _avx512vl bool
  41. var _sseState bool
  42. var _avxState bool
  43. var _opmaskState bool
  44. var _zmmHI256State bool
  45. var _hi16ZmmState bool
  46. mfi, _, _, _ := cpuid(0)
  47. if mfi >= 1 {
  48. _, _, c, d := cpuid(1)
  49. sse = (d & (1 << 25)) != 0
  50. sse2 = (d & (1 << 26)) != 0
  51. sse3 = (c & (1 << 0)) != 0
  52. ssse3 = (c & (1 << 9)) != 0
  53. sse41 = (c & (1 << 19)) != 0
  54. sse42 = (c & (1 << 20)) != 0
  55. popcnt = (c & (1 << 23)) != 0
  56. _xsave = (c & (1 << 26)) != 0
  57. _osxsave = (c & (1 << 27)) != 0
  58. _avx = (c & (1 << 28)) != 0
  59. }
  60. if mfi >= 7 {
  61. _, b, _, _ := cpuid(7)
  62. _avx2 = (b & (1 << 5)) != 0
  63. _avx512f = (b & (1 << 16)) != 0
  64. _avx512dq = (b & (1 << 17)) != 0
  65. // _avx512pf = (b & (1 << 26)) != 0
  66. // _avx512er = (b & (1 << 27)) != 0
  67. // _avx512cd = (b & (1 << 28)) != 0
  68. _avx512bw = (b & (1 << 30)) != 0
  69. _avx512vl = (b & (1 << 31)) != 0
  70. sha = (b & (1 << 29)) != 0
  71. }
  72. // Stop here if XSAVE unsupported or not enabled
  73. if !_xsave || !_osxsave {
  74. return
  75. }
  76. if _xsave && _osxsave {
  77. a, _ := xgetbv(0)
  78. _sseState = (a & (1 << 1)) != 0
  79. _avxState = (a & (1 << 2)) != 0
  80. _opmaskState = (a & (1 << 5)) != 0
  81. _zmmHI256State = (a & (1 << 6)) != 0
  82. _hi16ZmmState = (a & (1 << 7)) != 0
  83. } else {
  84. _sseState = true
  85. }
  86. // Very unlikely that OS would enable XSAVE and then disable SSE
  87. if !_sseState {
  88. sse = false
  89. sse2 = false
  90. sse3 = false
  91. ssse3 = false
  92. sse41 = false
  93. sse42 = false
  94. }
  95. if _avxState {
  96. avx = _avx
  97. avx2 = _avx2
  98. }
  99. if _opmaskState && _zmmHI256State && _hi16ZmmState {
  100. avx512 = (_avx512f &&
  101. _avx512dq &&
  102. _avx512bw &&
  103. _avx512vl)
  104. }
  105. }