本站源代码
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.

60 lines
1.1KB

  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 !amd64,!amd64p32,!386
  5. package cpu
  6. import (
  7. "io/ioutil"
  8. )
  9. const (
  10. _AT_HWCAP = 16
  11. _AT_HWCAP2 = 26
  12. procAuxv = "/proc/self/auxv"
  13. uintSize = int(32 << (^uint(0) >> 63))
  14. )
  15. // For those platforms don't have a 'cpuid' equivalent we use HWCAP/HWCAP2
  16. // These are initialized in cpu_$GOARCH.go
  17. // and should not be changed after they are initialized.
  18. var hwCap uint
  19. var hwCap2 uint
  20. func init() {
  21. buf, err := ioutil.ReadFile(procAuxv)
  22. if err != nil {
  23. // e.g. on android /proc/self/auxv is not accessible, so silently
  24. // ignore the error and leave Initialized = false
  25. return
  26. }
  27. bo := hostByteOrder()
  28. for len(buf) >= 2*(uintSize/8) {
  29. var tag, val uint
  30. switch uintSize {
  31. case 32:
  32. tag = uint(bo.Uint32(buf[0:]))
  33. val = uint(bo.Uint32(buf[4:]))
  34. buf = buf[8:]
  35. case 64:
  36. tag = uint(bo.Uint64(buf[0:]))
  37. val = uint(bo.Uint64(buf[8:]))
  38. buf = buf[16:]
  39. }
  40. switch tag {
  41. case _AT_HWCAP:
  42. hwCap = val
  43. case _AT_HWCAP2:
  44. hwCap2 = val
  45. }
  46. }
  47. doinit()
  48. Initialized = true
  49. }
上海开阖软件有限公司 沪ICP备12045867号-1