armcpu.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include <stdio.h>
  86. opus_uint32 opus_cpu_capabilities(void)
  87. {
  88. opus_uint32 flags = 0;
  89. FILE *cpuinfo;
  90. /* Reading /proc/self/auxv would be easier, but that doesn't work reliably on
  91. * Android */
  92. cpuinfo = fopen("/proc/cpuinfo", "r");
  93. if(cpuinfo != NULL)
  94. {
  95. /* 512 should be enough for anybody (it's even enough for all the flags that
  96. * x86 has accumulated... so far). */
  97. char buf[512];
  98. while(fgets(buf, 512, cpuinfo) != NULL)
  99. {
  100. # if defined(OPUS_ARM_MAY_HAVE_EDSP) || defined(OPUS_ARM_MAY_HAVE_MEDIA) \
  101. || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  102. /* Search for edsp and neon flag */
  103. if(memcmp(buf, "Features", 8) == 0)
  104. {
  105. char *p;
  106. p = strstr(buf, " edsp");
  107. if(p != NULL && (p[5] == ' ' || p[5] == '\n'))
  108. flags |= OPUS_CPU_ARM_EDSP_FLAG;
  109. # if defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  110. p = strstr(buf, " neon");
  111. if(p != NULL && (p[5] == ' ' || p[5] == '\n'))
  112. flags |= OPUS_CPU_ARM_NEON_FLAG;
  113. # endif
  114. }
  115. # endif
  116. # if defined(OPUS_ARM_MAY_HAVE_MEDIA) \
  117. || defined(OPUS_ARM_MAY_HAVE_NEON) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  118. /* Search for media capabilities (>= ARMv6) */
  119. if(memcmp(buf, "CPU architecture:", 17) == 0)
  120. {
  121. int version;
  122. version = atoi(buf+17);
  123. if(version >= 6)
  124. flags |= OPUS_CPU_ARM_MEDIA_FLAG;
  125. }
  126. # endif
  127. }
  128. fclose(cpuinfo);
  129. }
  130. return flags;
  131. }
  132. #else
  133. /* The feature registers which can tell us what the processor supports are
  134. * accessible in priveleged modes only, so we can't have a general user-space
  135. * detection method like on x86.*/
  136. # error "Configured to use ARM asm but no CPU detection method available for " \
  137. "your platform. Reconfigure with --disable-rtcd (or send patches)."
  138. #endif
  139. int opus_select_arch(void)
  140. {
  141. opus_uint32 flags = opus_cpu_capabilities();
  142. int arch = 0;
  143. if(!(flags & OPUS_CPU_ARM_EDSP_FLAG)) {
  144. /* Asserts ensure arch values are sequential */
  145. celt_assert(arch == OPUS_ARCH_ARM_V4);
  146. return arch;
  147. }
  148. arch++;
  149. if(!(flags & OPUS_CPU_ARM_MEDIA_FLAG)) {
  150. celt_assert(arch == OPUS_ARCH_ARM_EDSP);
  151. return arch;
  152. }
  153. arch++;
  154. if(!(flags & OPUS_CPU_ARM_NEON_FLAG)) {
  155. celt_assert(arch == OPUS_ARCH_ARM_MEDIA);
  156. return arch;
  157. }
  158. arch++;
  159. celt_assert(arch == OPUS_ARCH_ARM_NEON);
  160. return arch;
  161. }
  162. #endif