cpu.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/cpu.h"
  19. #include "config.h"
  20. #define CORE_FLAG(f) \
  21. (AV_CPU_FLAG_ ## f * (HAVE_ ## f ## _EXTERNAL || HAVE_ ## f ## _INLINE))
  22. #define CORE_CPU_FLAGS \
  23. (CORE_FLAG(ARMV5TE) | \
  24. CORE_FLAG(ARMV6) | \
  25. CORE_FLAG(ARMV6T2) | \
  26. CORE_FLAG(VFP) | \
  27. CORE_FLAG(VFPV3) | \
  28. CORE_FLAG(NEON))
  29. #if defined __linux__ || defined __ANDROID__
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "libavutil/avstring.h"
  34. #define AT_HWCAP 16
  35. /* Relevant HWCAP values from kernel headers */
  36. #define HWCAP_VFP (1 << 6)
  37. #define HWCAP_EDSP (1 << 7)
  38. #define HWCAP_THUMBEE (1 << 11)
  39. #define HWCAP_NEON (1 << 12)
  40. #define HWCAP_VFPv3 (1 << 13)
  41. #define HWCAP_TLS (1 << 15)
  42. static int get_hwcap(uint32_t *hwcap)
  43. {
  44. struct { uint32_t a_type; uint32_t a_val; } auxv;
  45. FILE *f = fopen("/proc/self/auxv", "r");
  46. int err = -1;
  47. if (!f)
  48. return -1;
  49. while (fread(&auxv, sizeof(auxv), 1, f) > 0) {
  50. if (auxv.a_type == AT_HWCAP) {
  51. *hwcap = auxv.a_val;
  52. err = 0;
  53. break;
  54. }
  55. }
  56. fclose(f);
  57. return err;
  58. }
  59. static int get_cpuinfo(uint32_t *hwcap)
  60. {
  61. FILE *f = fopen("/proc/cpuinfo", "r");
  62. char buf[200];
  63. if (!f)
  64. return -1;
  65. *hwcap = 0;
  66. while (fgets(buf, sizeof(buf), f)) {
  67. if (av_strstart(buf, "Features", NULL)) {
  68. if (strstr(buf, " edsp "))
  69. *hwcap |= HWCAP_EDSP;
  70. if (strstr(buf, " tls "))
  71. *hwcap |= HWCAP_TLS;
  72. if (strstr(buf, " thumbee "))
  73. *hwcap |= HWCAP_THUMBEE;
  74. if (strstr(buf, " vfp "))
  75. *hwcap |= HWCAP_VFP;
  76. if (strstr(buf, " vfpv3 "))
  77. *hwcap |= HWCAP_VFPv3;
  78. if (strstr(buf, " neon "))
  79. *hwcap |= HWCAP_NEON;
  80. break;
  81. }
  82. }
  83. fclose(f);
  84. return 0;
  85. }
  86. int ff_get_cpu_flags_arm(void)
  87. {
  88. int flags = CORE_CPU_FLAGS;
  89. uint32_t hwcap;
  90. if (get_hwcap(&hwcap) < 0)
  91. if (get_cpuinfo(&hwcap) < 0)
  92. return flags;
  93. #define check_cap(cap, flag) do { \
  94. if (hwcap & HWCAP_ ## cap) \
  95. flags |= AV_CPU_FLAG_ ## flag; \
  96. } while (0)
  97. /* No flags explicitly indicate v6 or v6T2 so check others which
  98. imply support. */
  99. check_cap(EDSP, ARMV5TE);
  100. check_cap(TLS, ARMV6);
  101. check_cap(THUMBEE, ARMV6T2);
  102. check_cap(VFP, VFP);
  103. check_cap(VFPv3, VFPV3);
  104. check_cap(NEON, NEON);
  105. /* The v6 checks above are not reliable so let higher flags
  106. trickle down. */
  107. if (flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON))
  108. flags |= AV_CPU_FLAG_ARMV6T2;
  109. if (flags & AV_CPU_FLAG_ARMV6T2)
  110. flags |= AV_CPU_FLAG_ARMV6;
  111. return flags;
  112. }
  113. #else
  114. int ff_get_cpu_flags_arm(void)
  115. {
  116. return AV_CPU_FLAG_ARMV5TE * HAVE_ARMV5TE |
  117. AV_CPU_FLAG_ARMV6 * HAVE_ARMV6 |
  118. AV_CPU_FLAG_ARMV6T2 * HAVE_ARMV6T2 |
  119. AV_CPU_FLAG_VFP * HAVE_VFP |
  120. AV_CPU_FLAG_VFPV3 * HAVE_VFPV3 |
  121. AV_CPU_FLAG_NEON * HAVE_NEON;
  122. }
  123. #endif