armcpu.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Copyright (c) 2010 Xiph.Org Foundation
  2. * Copyright (c) 2013 Parrot */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* Original code from libtheora modified to suit to Opus */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #ifdef OPUS_HAVE_RTCD
  29. #include "armcpu.h"
  30. #include "cpu_support.h"
  31. #include "os_support.h"
  32. #include "opus_types.h"
  33. #include "arch.h"
  34. #define OPUS_CPU_ARM_V4_FLAG (1<<OPUS_ARCH_ARM_V4)
  35. #define OPUS_CPU_ARM_EDSP_FLAG (1<<OPUS_ARCH_ARM_EDSP)
  36. #define OPUS_CPU_ARM_MEDIA_FLAG (1<<OPUS_ARCH_ARM_MEDIA)
  37. #define OPUS_CPU_ARM_NEON_FLAG (1<<OPUS_ARCH_ARM_NEON)
  38. #if defined(_MSC_VER)
  39. /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
  40. # define WIN32_LEAN_AND_MEAN
  41. # define WIN32_EXTRA_LEAN
  42. # include <windows.h>
  43. static OPUS_INLINE opus_uint32 opus_cpu_capabilities(void){
  44. opus_uint32 flags;
  45. flags=0;
  46. /* MSVC has no OPUS_INLINE __asm support for ARM, but it does let you __emit
  47. * instructions via their assembled hex code.
  48. * All of these instructions should be essentially nops. */
  49. # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \
  50. || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  51. __try{
  52. /*PLD [r13]*/
  53. __emit(0xF5DDF000);
  54. flags|=OPUS_CPU_ARM_EDSP_FLAG;
  55. }
  56. __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){
  57. /*Ignore exception.*/
  58. }
  59. # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \
  60. || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  61. __try{
  62. /*SHADD8 r3,r3,r3*/
  63. __emit(0xE6333F93);
  64. flags|=OPUS_CPU_ARM_MEDIA_FLAG;
  65. }
  66. __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){
  67. /*Ignore exception.*/
  68. }
  69. # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  70. __try{
  71. /*VORR q0,q0,q0*/
  72. __emit(0xF2200150);
  73. flags|=OPUS_CPU_ARM_NEON_FLAG;
  74. }
  75. __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION){
  76. /*Ignore exception.*/
  77. }
  78. # endif
  79. # endif
  80. # endif
  81. return flags;
  82. }
  83. #elif defined(__linux__)
  84. /* Linux based */
  85. opus_uint32 opus_cpu_capabilities(void)
  86. {
  87. opus_uint32 flags = 0;
  88. FILE *cpuinfo;
  89. /* Reading /proc/self/auxv would be easier, but that doesn't work reliably on
  90. * Android */
  91. cpuinfo = fopen("/proc/cpuinfo", "r");
  92. if(cpuinfo != NULL)
  93. {
  94. /* 512 should be enough for anybody (it's even enough for all the flags that
  95. * x86 has accumulated... so far). */
  96. char buf[512];
  97. while(fgets(buf, 512, cpuinfo) != NULL)
  98. {
  99. # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \
  100. || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  101. /* Search for edsp and neon flag */
  102. if(memcmp(buf, "Features", 8) == 0)
  103. {
  104. char *p;
  105. p = strstr(buf, " edsp");
  106. if(p != NULL && (p[5] == ' ' || p[5] == '\n'))
  107. flags |= OPUS_CPU_ARM_EDSP_FLAG;
  108. # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  109. p = strstr(buf, " neon");
  110. if(p != NULL && (p[5] == ' ' || p[5] == '\n'))
  111. flags |= OPUS_CPU_ARM_NEON_FLAG;
  112. # endif
  113. }
  114. # endif
  115. # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \
  116. || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  117. /* Search for media capabilities (>= ARMv6) */
  118. if(memcmp(buf, "CPU architecture:", 17) == 0)
  119. {
  120. int version;
  121. version = atoi(buf+17);
  122. if(version >= 6)
  123. flags |= OPUS_CPU_ARM_MEDIA_FLAG;
  124. }
  125. # endif
  126. }
  127. fclose(cpuinfo);
  128. }
  129. return flags;
  130. }
  131. #else
  132. /* The feature registers which can tell us what the processor supports are
  133. * accessible in priveleged modes only, so we can't have a general user-space
  134. * detection method like on x86.*/
  135. # error "Configured to use ARM asm but no CPU detection method available for " \
  136. "your platform. Reconfigure with --disable-rtcd (or send patches)."
  137. #endif
  138. int opus_select_arch(void)
  139. {
  140. opus_uint32 flags = opus_cpu_capabilities();
  141. int arch = 0;
  142. if(!(flags & OPUS_CPU_ARM_EDSP_FLAG)) {
  143. /* Asserts ensure arch values are sequential */
  144. celt_assert(arch == OPUS_ARCH_ARM_V4);
  145. return arch;
  146. }
  147. arch++;
  148. if(!(flags & OPUS_CPU_ARM_MEDIA_FLAG)) {
  149. celt_assert(arch == OPUS_ARCH_ARM_EDSP);
  150. return arch;
  151. }
  152. arch++;
  153. if(!(flags & OPUS_CPU_ARM_NEON_FLAG)) {
  154. celt_assert(arch == OPUS_ARCH_ARM_MEDIA);
  155. return arch;
  156. }
  157. arch++;
  158. celt_assert(arch == OPUS_ARCH_ARM_NEON);
  159. return arch;
  160. }
  161. #endif