cpu_detect_x86.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// Generic version of the x86 CPU extension detection routine.
  4. ///
  5. /// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp'
  6. /// for the Microsoft compiler version.
  7. ///
  8. /// Author : Copyright (c) Olli Parviainen
  9. /// Author e-mail : oparviai 'at' iki.fi
  10. /// SoundTouch WWW: http://www.surina.net/soundtouch
  11. ///
  12. ////////////////////////////////////////////////////////////////////////////////
  13. //
  14. // License :
  15. //
  16. // SoundTouch audio processing library
  17. // Copyright (c) Olli Parviainen
  18. //
  19. // This library is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Lesser General Public
  21. // License as published by the Free Software Foundation; either
  22. // version 2.1 of the License, or (at your option) any later version.
  23. //
  24. // This library is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. // Lesser General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU Lesser General Public
  30. // License along with this library; if not, write to the Free Software
  31. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. //
  33. ////////////////////////////////////////////////////////////////////////////////
  34. #include "cpu_detect.h"
  35. #include "STTypes.h"
  36. #if defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
  37. #if defined(__GNUC__) && defined(__i386__)
  38. // gcc
  39. #include "cpuid.h"
  40. #elif defined(_M_IX86)
  41. // windows non-gcc
  42. #include <intrin.h>
  43. #endif
  44. #define bit_MMX (1 << 23)
  45. #define bit_SSE (1 << 25)
  46. #define bit_SSE2 (1 << 26)
  47. #endif
  48. //////////////////////////////////////////////////////////////////////////////
  49. //
  50. // processor instructions extension detection routines
  51. //
  52. //////////////////////////////////////////////////////////////////////////////
  53. // Flag variable indicating whick ISA extensions are disabled (for debugging)
  54. static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions
  55. // Disables given set of instruction extensions. See SUPPORT_... defines.
  56. void disableExtensions(uint dwDisableMask)
  57. {
  58. _dwDisabledISA = dwDisableMask;
  59. }
  60. /// Checks which instruction set extensions are supported by the CPU.
  61. uint detectCPUextensions(void)
  62. {
  63. /// If building for a 64bit system (no Itanium) and the user wants optimizations.
  64. /// Return the OR of SUPPORT_{MMX,SSE,SSE2}. 11001 or 0x19.
  65. /// Keep the _dwDisabledISA test (2 more operations, could be eliminated).
  66. #if ((defined(__GNUC__) && defined(__x86_64__)) \
  67. || defined(_M_X64)) \
  68. && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
  69. return 0x19 & ~_dwDisabledISA;
  70. /// If building for a 32bit system and the user wants optimizations.
  71. /// Keep the _dwDisabledISA test (2 more operations, could be eliminated).
  72. #elif ((defined(__GNUC__) && defined(__i386__)) \
  73. || defined(_M_IX86)) \
  74. && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
  75. if (_dwDisabledISA == 0xffffffff) return 0;
  76. uint res = 0;
  77. #if defined(__GNUC__)
  78. // GCC version of cpuid. Requires GCC 4.3.0 or later for __cpuid intrinsic support.
  79. uint eax, ebx, ecx, edx; // unsigned int is the standard type. uint is defined by the compiler and not guaranteed to be portable.
  80. // Check if no cpuid support.
  81. if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; // always disable extensions.
  82. if (edx & bit_MMX) res = res | SUPPORT_MMX;
  83. if (edx & bit_SSE) res = res | SUPPORT_SSE;
  84. if (edx & bit_SSE2) res = res | SUPPORT_SSE2;
  85. #else
  86. // Window / VS version of cpuid. Notice that Visual Studio 2005 or later required
  87. // for __cpuid intrinsic support.
  88. int reg[4] = {-1};
  89. // Check if no cpuid support.
  90. __cpuid(reg,0);
  91. if ((unsigned int)reg[0] == 0) return 0; // always disable extensions.
  92. __cpuid(reg,1);
  93. if ((unsigned int)reg[3] & bit_MMX) res = res | SUPPORT_MMX;
  94. if ((unsigned int)reg[3] & bit_SSE) res = res | SUPPORT_SSE;
  95. if ((unsigned int)reg[3] & bit_SSE2) res = res | SUPPORT_SSE2;
  96. #endif
  97. return res & ~_dwDisabledISA;
  98. #else
  99. /// One of these is true:
  100. /// 1) We don't want optimizations.
  101. /// 2) Using an unsupported compiler.
  102. /// 3) Running on a non-x86 platform.
  103. return 0;
  104. #endif
  105. }