x86cpu.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_X86_MAY_HAVE_SSE) && !defined(OPUS_X86_PRESUME_SSE)) || \
  32. (defined(OPUS_X86_MAY_HAVE_SSE2) && !defined(OPUS_X86_PRESUME_SSE2)) || \
  33. (defined(OPUS_X86_MAY_HAVE_SSE4_1) && !defined(OPUS_X86_PRESUME_SSE4_1)) || \
  34. (defined(OPUS_X86_MAY_HAVE_AVX) && !defined(OPUS_X86_PRESUME_AVX))
  35. #if defined(_MSC_VER)
  36. #include <intrin.h>
  37. static _inline void cpuid(unsigned int CPUInfo[4], unsigned int InfoType)
  38. {
  39. __cpuid((int*)CPUInfo, InfoType);
  40. }
  41. #else
  42. #if defined(CPU_INFO_BY_C)
  43. #include <cpuid.h>
  44. #endif
  45. static void cpuid(unsigned int CPUInfo[4], unsigned int InfoType)
  46. {
  47. #if defined(CPU_INFO_BY_ASM)
  48. #if defined(__i386__) && defined(__PIC__)
  49. /* %ebx is PIC register in 32-bit, so mustn't clobber it. */
  50. __asm__ __volatile__ (
  51. "xchg %%ebx, %1\n"
  52. "cpuid\n"
  53. "xchg %%ebx, %1\n":
  54. "=a" (CPUInfo[0]),
  55. "=r" (CPUInfo[1]),
  56. "=c" (CPUInfo[2]),
  57. "=d" (CPUInfo[3]) :
  58. "0" (InfoType)
  59. );
  60. #else
  61. __asm__ __volatile__ (
  62. "cpuid":
  63. "=a" (CPUInfo[0]),
  64. "=b" (CPUInfo[1]),
  65. "=c" (CPUInfo[2]),
  66. "=d" (CPUInfo[3]) :
  67. "0" (InfoType)
  68. );
  69. #endif
  70. #elif defined(CPU_INFO_BY_C)
  71. __get_cpuid(InfoType, &(CPUInfo[0]), &(CPUInfo[1]), &(CPUInfo[2]), &(CPUInfo[3]));
  72. #endif
  73. }
  74. #endif
  75. typedef struct CPU_Feature{
  76. /* SIMD: 128-bit */
  77. int HW_SSE;
  78. int HW_SSE2;
  79. int HW_SSE41;
  80. /* SIMD: 256-bit */
  81. int HW_AVX;
  82. } CPU_Feature;
  83. static void opus_cpu_feature_check(CPU_Feature *cpu_feature)
  84. {
  85. unsigned int info[4] = {0};
  86. unsigned int nIds = 0;
  87. cpuid(info, 0);
  88. nIds = info[0];
  89. if (nIds >= 1){
  90. cpuid(info, 1);
  91. cpu_feature->HW_SSE = (info[3] & (1 << 25)) != 0;
  92. cpu_feature->HW_SSE2 = (info[3] & (1 << 26)) != 0;
  93. cpu_feature->HW_SSE41 = (info[2] & (1 << 19)) != 0;
  94. cpu_feature->HW_AVX = (info[2] & (1 << 28)) != 0;
  95. }
  96. else {
  97. cpu_feature->HW_SSE = 0;
  98. cpu_feature->HW_SSE2 = 0;
  99. cpu_feature->HW_SSE41 = 0;
  100. cpu_feature->HW_AVX = 0;
  101. }
  102. }
  103. int opus_select_arch(void)
  104. {
  105. CPU_Feature cpu_feature;
  106. int arch;
  107. opus_cpu_feature_check(&cpu_feature);
  108. arch = 0;
  109. if (!cpu_feature.HW_SSE)
  110. {
  111. return arch;
  112. }
  113. arch++;
  114. if (!cpu_feature.HW_SSE2)
  115. {
  116. return arch;
  117. }
  118. arch++;
  119. if (!cpu_feature.HW_SSE41)
  120. {
  121. return arch;
  122. }
  123. arch++;
  124. if (!cpu_feature.HW_AVX)
  125. {
  126. return arch;
  127. }
  128. arch++;
  129. return arch;
  130. }
  131. #endif