cpuid.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 avx2 = haveAVX2()
  18. var avx = haveAVX()
  19. var ssse3 = haveSSSE3()
  20. var armSha = haveArmSha()
  21. // haveAVX returns true when there is AVX support
  22. func haveAVX() bool {
  23. _, _, c, _ := cpuid(1)
  24. // Check XGETBV, OXSAVE and AVX bits
  25. if c&(1<<26) != 0 && c&(1<<27) != 0 && c&(1<<28) != 0 {
  26. // Check for OS support
  27. eax, _ := xgetbv(0)
  28. return (eax & 0x6) == 0x6
  29. }
  30. return false
  31. }
  32. // haveAVX2 returns true when there is AVX2 support
  33. func haveAVX2() bool {
  34. mfi, _, _, _ := cpuid(0)
  35. // Check AVX2, AVX2 requires OS support, but BMI1/2 don't.
  36. if mfi >= 7 && haveAVX() {
  37. _, ebx, _, _ := cpuidex(7, 0)
  38. return (ebx & 0x00000020) != 0
  39. }
  40. return false
  41. }
  42. // haveSSSE3 returns true when there is SSSE3 support
  43. func haveSSSE3() bool {
  44. _, _, c, _ := cpuid(1)
  45. return (c & 0x00000200) != 0
  46. }