x86cpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (c) 2014, Cisco Systems, INC
  2. Written by XiangMingZhu WeiZhou MinPeng YanWang
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  12. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  13. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  14. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  15. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  20. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "cpu_support.h"
  27. #include "macros.h"
  28. #include "main.h"
  29. #include "pitch.h"
  30. #include "x86cpu.h"
  31. #if defined(OPUS_HAVE_RTCD) && \
  32. ((defined(OPUS_X86_MAY_HAVE_SSE) && !defined(OPUS_X86_PRESUME_SSE)) || \
  33. (defined(OPUS_X86_MAY_HAVE_SSE2) && !defined(OPUS_X86_PRESUME_SSE2)) || \
  34. (defined(OPUS_X86_MAY_HAVE_SSE4_1) && !defined(OPUS_X86_PRESUME_SSE4_1)) || \
  35. (defined(OPUS_X86_MAY_HAVE_AVX) && !defined(OPUS_X86_PRESUME_AVX)))
  36. #if defined(_MSC_VER)
  37. #include <intrin.h>
  38. static _inline void cpuid(unsigned int CPUInfo[4], unsigned int InfoType)
  39. {
  40. __cpuid((int*)CPUInfo, InfoType);
  41. }
  42. #else
  43. #if defined(CPU_INFO_BY_C)
  44. #include <cpuid.h>
  45. #endif
  46. static void cpuid(unsigned int CPUInfo[4], unsigned int InfoType)
  47. {
  48. #if defined(CPU_INFO_BY_ASM)
  49. #if defined(__i386__) && defined(__PIC__)
  50. /* %ebx is PIC register in 32-bit, so mustn't clobber it. */
  51. __asm__ __volatile__ (
  52. "xchg %%ebx, %1\n"
  53. "cpuid\n"
  54. "xchg %%ebx, %1\n":
  55. "=a" (CPUInfo[0]),
  56. "=r" (CPUInfo[1]),
  57. "=c" (CPUInfo[2]),
  58. "=d" (CPUInfo[3]) :
  59. /* We clear ECX to avoid a valgrind false-positive prior to v3.17.0. */
  60. "0" (InfoType), "2" (0)
  61. );
  62. #else
  63. __asm__ __volatile__ (
  64. "cpuid":
  65. "=a" (CPUInfo[0]),
  66. "=b" (CPUInfo[1]),
  67. "=c" (CPUInfo[2]),
  68. "=d" (CPUInfo[3]) :
  69. /* We clear ECX to avoid a valgrind false-positive prior to v3.17.0. */
  70. "0" (InfoType), "2" (0)
  71. );
  72. #endif
  73. #elif defined(CPU_INFO_BY_C)
  74. /* We use __get_cpuid_count to clear ECX to avoid a valgrind false-positive
  75. prior to v3.17.0.*/
  76. if (!__get_cpuid_count(InfoType, 0, &(CPUInfo[0]), &(CPUInfo[1]), &(CPUInfo[2]), &(CPUInfo[3]))) {
  77. /* Our function cannot fail, but __get_cpuid{_count} can.
  78. Returning all zeroes will effectively disable all SIMD, which is
  79. what we want on CPUs that don't support CPUID. */
  80. CPUInfo[3] = CPUInfo[2] = CPUInfo[1] = CPUInfo[0] = 0;
  81. }
  82. #else
  83. # error "Configured to use x86 RTCD, but no CPU detection method available. " \
  84. "Reconfigure with --disable-rtcd (or send patches)."
  85. #endif
  86. }
  87. #endif
  88. typedef struct CPU_Feature{
  89. /* SIMD: 128-bit */
  90. int HW_SSE;
  91. int HW_SSE2;
  92. int HW_SSE41;
  93. /* SIMD: 256-bit */
  94. int HW_AVX;
  95. } CPU_Feature;
  96. static void opus_cpu_feature_check(CPU_Feature *cpu_feature)
  97. {
  98. unsigned int info[4];
  99. unsigned int nIds = 0;
  100. cpuid(info, 0);
  101. nIds = info[0];
  102. if (nIds >= 1){
  103. cpuid(info, 1);
  104. cpu_feature->HW_SSE = (info[3] & (1 << 25)) != 0;
  105. cpu_feature->HW_SSE2 = (info[3] & (1 << 26)) != 0;
  106. cpu_feature->HW_SSE41 = (info[2] & (1 << 19)) != 0;
  107. cpu_feature->HW_AVX = (info[2] & (1 << 28)) != 0;
  108. }
  109. else {
  110. cpu_feature->HW_SSE = 0;
  111. cpu_feature->HW_SSE2 = 0;
  112. cpu_feature->HW_SSE41 = 0;
  113. cpu_feature->HW_AVX = 0;
  114. }
  115. }
  116. static int opus_select_arch_impl(void)
  117. {
  118. CPU_Feature cpu_feature;
  119. int arch;
  120. opus_cpu_feature_check(&cpu_feature);
  121. arch = 0;
  122. if (!cpu_feature.HW_SSE)
  123. {
  124. return arch;
  125. }
  126. arch++;
  127. if (!cpu_feature.HW_SSE2)
  128. {
  129. return arch;
  130. }
  131. arch++;
  132. if (!cpu_feature.HW_SSE41)
  133. {
  134. return arch;
  135. }
  136. arch++;
  137. if (!cpu_feature.HW_AVX)
  138. {
  139. return arch;
  140. }
  141. arch++;
  142. return arch;
  143. }
  144. int opus_select_arch(void) {
  145. int arch = opus_select_arch_impl();
  146. #ifdef FUZZING
  147. /* Randomly downgrade the architecture. */
  148. arch = rand()%(arch+1);
  149. #endif
  150. return arch;
  151. }
  152. #endif