cpu_detect_x86.cpp 4.7 KB

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