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.

114 lines
3.1KB

  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. #define AT_HWCAP 16
  33. /* Relevant HWCAP values from kernel headers */
  34. #define HWCAP_VFP (1 << 6)
  35. #define HWCAP_EDSP (1 << 7)
  36. #define HWCAP_THUMBEE (1 << 11)
  37. #define HWCAP_NEON (1 << 12)
  38. #define HWCAP_VFPv3 (1 << 13)
  39. #define HWCAP_TLS (1 << 15)
  40. static int get_hwcap(uint32_t *hwcap)
  41. {
  42. struct { uint32_t a_type; uint32_t a_val; } auxv;
  43. FILE *f = fopen("/proc/self/auxv", "r");
  44. int err = -1;
  45. if (!f)
  46. return -1;
  47. while (fread(&auxv, sizeof(auxv), 1, f) > 0) {
  48. if (auxv.a_type == AT_HWCAP) {
  49. *hwcap = auxv.a_val;
  50. err = 0;
  51. break;
  52. }
  53. }
  54. fclose(f);
  55. return err;
  56. }
  57. int ff_get_cpu_flags_arm(void)
  58. {
  59. int flags = CORE_CPU_FLAGS;
  60. uint32_t hwcap;
  61. if (get_hwcap(&hwcap) < 0)
  62. return flags;
  63. #define check_cap(cap, flag) do { \
  64. if (hwcap & HWCAP_ ## cap) \
  65. flags |= AV_CPU_FLAG_ ## flag; \
  66. } while (0)
  67. /* No flags explicitly indicate v6 or v6T2 so check others which
  68. imply support. */
  69. check_cap(EDSP, ARMV5TE);
  70. check_cap(TLS, ARMV6);
  71. check_cap(THUMBEE, ARMV6T2);
  72. check_cap(VFP, VFP);
  73. check_cap(VFPv3, VFPV3);
  74. check_cap(NEON, NEON);
  75. /* The v6 checks above are not reliable so let higher flags
  76. trickle down. */
  77. if (flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON))
  78. flags |= AV_CPU_FLAG_ARMV6T2;
  79. if (flags & AV_CPU_FLAG_ARMV6T2)
  80. flags |= AV_CPU_FLAG_ARMV6;
  81. return flags;
  82. }
  83. #else
  84. int ff_get_cpu_flags_arm(void)
  85. {
  86. return AV_CPU_FLAG_ARMV5TE * HAVE_ARMV5TE |
  87. AV_CPU_FLAG_ARMV6 * HAVE_ARMV6 |
  88. AV_CPU_FLAG_ARMV6T2 * HAVE_ARMV6T2 |
  89. AV_CPU_FLAG_VFP * HAVE_VFP |
  90. AV_CPU_FLAG_VFPV3 * HAVE_VFPV3 |
  91. AV_CPU_FLAG_NEON * HAVE_NEON;
  92. }
  93. #endif