From 9db805d439a25508de978c5a04e0e5881419a7b1 Mon Sep 17 00:00:00 2001 From: oparviai Date: Sun, 17 Jul 2011 11:17:57 +0000 Subject: [PATCH] Added configure flags to disable x86 optimizations --- configure.ac | 103 +++++++++++++++++++++++++++++++++- include/STTypes.h | 13 ++++- source/SoundTouch/Makefile.am | 39 ++++++++++--- 3 files changed, 146 insertions(+), 9 deletions(-) diff --git a/configure.ac b/configure.ac index 322acd3..f849247 100644 --- a/configure.ac +++ b/configure.ac @@ -77,7 +77,21 @@ AC_ARG_ENABLE(integer-samples, [default=yes]])],, [enable_integer_samples=no]) - + +# Let the user enable/disable the x86 optimizations. +# Useful when compiling on non-x86 architectures. +AC_ARG_ENABLE([x86-optimizations], + [AS_HELP_STRING([--enable-x86-optimizations], + [use MMX or SSE2 optimizations +[default=yes]])],[enable_x86_optimizations="${enableval}"], + [enable_x86_optimizations=yes]) + +# Tell the Makefile.am if the user wants to disable optimizations. +# Makefile.am will enable them by default if support is available. +# Note: We check if optimizations are supported a few lines down. +AM_CONDITIONAL([X86_OPTIMIZATIONS], [test "x$enable_x86_optimizations" = "xyes"]) + + if test "x$enable_integer_samples" = "xyes"; then echo "****** Integer sample type enabled ******" AC_DEFINE(SOUNDTOUCH_INTEGER_SAMPLES,1,[Use Integer as Sample type]) @@ -87,6 +101,93 @@ else fi +# Check if optimizations are supported in the system at build time. +if test "x$enable_x86_optimizations" = "xyes"; then + echo "****** x86 optimizations enabled ******" + + original_saved_CXXFLAGS=$CXXFLAGS + have_mmx_intrinsics=no + OPT_CXXFLAGS="-mmmx -Winline" + CXXFLAGS="$OPT_CXXFLAGS $CXXFLAGS" + + # Check if the user can compile MMX code using intrinsics. + # GCC supports MMX intrinsics since version 3.3 + # A more recent GCC (>= 4.3) is recommended. + AC_COMPILE_IFELSE([ + #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 3)) + #error "Need GCC >= 3.3 for MMX intrinsics" + #endif + #include + int main () { + __m64 loop = _mm_cvtsi32_si64 (1); + return _mm_cvtsi64_si32 (loop); + }],[have_mmx_intrinsics=yes]) + CXXFLAGS=$original_saved_CXXFLAGS + + # Inform the user if we did or did not find MMX support. + # + # If we enable optimization and integer samples we only require MMX. + # Disable optimizations in the SSTypes.h file if this is not the case. + if test "x$have_mmx_intrinsics" = "xyes" ; then + echo "****** MMX support found ******" + else + echo "****** No MMX support found ******" + if test "x$enable_integer_samples" = "xyes"; then + echo "****** Disabling optimizations. Using integer samples with no MMX support ******" + AC_DEFINE([SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS],[1],[Do not use x86 optimizations]) + fi + fi + + + have_sse2_intrinsics=no + OPT_CXXFLAGS="-msse2 -Winline" + CXXFLAGS="$OPT_CXXFLAGS $CXXFLAGS" + + # Check if the user can compile SSE2 code using intrinsics. + # GCC supports SSE2 intrinsics since version 3.3 + # A more recent GCC (>= 4.3) is recommended. + AC_COMPILE_IFELSE([ + #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 3)) + #error "Need GCC >= 3.3 for SSE2 intrinsics" + #endif + #include + int main () { + __m128i loop = _mm_cvtsi32_si128 (1); + return _mm_cvtsi128_si32 (loop); + }],[have_sse2_intrinsics=yes]) + CXXFLAGS=$original_saved_CXXFLAGS + + # Inform the user if we did or did not find SSE2 support. + # + # If we enable optimization and float samples we only require SSE2. + # Disable optimizations in the SSTypes.h file if this is not the case. + if test "x$have_sse2_intrinsics" = "xyes" ; then + echo "****** SSE2 support found ******" + else + echo "****** No SSE2 support found ******" + if test "x$enable_integer_samples" != "xyes"; then + echo "****** Disabling optimizations. Using float samples with no SSE2 support ******" + AC_DEFINE([SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS],[1],[Do not use x86 optimizations]) + fi + fi + +else + # Disable optimizations in SSTypes.h since the user requested it. + echo "****** x86 optimizations disabled ******" + AC_DEFINE([SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS],[1],[Do not use x86 optimizations]) +fi + +# SSTypes.h by default enables optimizations. Those already got disabled if +# the user requested for it or if the system does not support them. +# +# Now tell the Makefile.am the optimizations that are supported. +# Note: +# Makefile.am already knows if the user asked for optimizations. We apply +# optimizations by default (if support is available) and then disable all of +# them if the user requested it. +AM_CONDITIONAL([HAVE_MMX], [test "x$have_mmx_intrinsics" = "xyes"]) +AM_CONDITIONAL([HAVE_SSE2], [test "x$have_sse2_intrinsics" = "xyes"]) + dnl ############################################################################ dnl # Checks for library functions/classes # diff --git a/include/STTypes.h b/include/STTypes.h index 163d69c..e9ce847 100644 --- a/include/STTypes.h +++ b/include/STTypes.h @@ -86,7 +86,7 @@ namespace soundtouch #endif - #if (WIN32 || __i386__ || __x86_64__) + #if (_WIN32 || __i386__ || __x86_64__ || _M_X64) /// Define this to allow X86-specific assembler/intrinsic optimizations. /// Notice that library contains also usual C++ versions of each of these /// these routines, so if you're having difficulties getting the optimized @@ -95,6 +95,17 @@ namespace soundtouch #define SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS 1 + /// In GNU environment, allow the user to override this setting by + /// giving the following switch to the configure script: + /// ./configure --disable-x86-optimizations + /// ./configure --enable-x86-optimizations=no + #ifdef SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS + #undef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS + #endif + #else + /// Always disable optimizations when not using a x86 systems. + #undef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS + #endif // If defined, allows the SIMD-optimized routines to take minor shortcuts diff --git a/source/SoundTouch/Makefile.am b/source/SoundTouch/Makefile.am index 4eb6c23..3e4b3d9 100644 --- a/source/SoundTouch/Makefile.am +++ b/source/SoundTouch/Makefile.am @@ -2,8 +2,6 @@ ## ## $Id$ ## -## Copyright (C) 2003 - David W. Durham -## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch is free software; you can redistribute it and/or modify it under the @@ -30,13 +28,40 @@ noinst_HEADERS=AAFilter.h cpu_detect.h cpu_detect_x86_gcc.cpp FIRFilter.h RateTr lib_LTLIBRARIES=libSoundTouch.la # -libSoundTouch_la_SOURCES=AAFilter.cpp FIRFilter.cpp FIFOSampleBuffer.cpp mmx_optimized.cpp sse_optimized.cpp RateTransposer.cpp SoundTouch.cpp TDStretch.cpp cpu_detect_x86_gcc.cpp BPMDetect.cpp PeakFinder.cpp +libSoundTouch_la_SOURCES=AAFilter.cpp FIRFilter.cpp FIFOSampleBuffer.cpp RateTransposer.cpp SoundTouch.cpp TDStretch.cpp cpu_detect_x86_gcc.cpp BPMDetect.cpp PeakFinder.cpp -# Note by authore: '-msse2' might not work in non-X86 compilations. If someone can -# fix this script to automatically check for CPU architecture, please submit a patch -# to me. -AM_CXXFLAGS=-O3 -msse2 -fcheck-new -I../../include +# Compiler flags +AM_CXXFLAGS=-O3 -fcheck-new -I../../include + +# Compile the files that need MMX and SSE individually. +libSoundTouch_la_LIBADD=libSoundTouchMMX.la libSoundTouchSSE2.la +noinst_LTLIBRARIES=libSoundTouchMMX.la libSoundTouchSSE2.la +libSoundTouchMMX_la_SOURCES=mmx_optimized.cpp +libSoundTouchSSE2_la_SOURCES=sse_optimized.cpp + +# We enable optimizations by default. +# If MMX is supported compile with -mmmx. +# Do not assume -msse2 is also supported. +if HAVE_MMX +libSoundTouchMMX_la_CXXFLAGS = -mmmx $(AM_CXXFLAGS) +else +libSoundTouchMMX_la_CXXFLAGS = $(AM_CXXFLAGS) +endif + +# We enable optimizations by default. +# If SSE2 is supported compile with -msse2. +if HAVE_SSE2 +libSoundTouchSSE2_la_CXXFLAGS = -msse2 $(AM_CXXFLAGS) +else +libSoundTouchSSE2_la_CXXFLAGS = $(AM_CXXFLAGS) +endif + +# Let the user disable optimizations if he wishes to. +if !X86_OPTIMIZATIONS +libSoundTouchMMX_la_CXXFLAGS = $(AM_CXXFLAGS) +libSoundTouchSSE2_la_CXXFLAGS = $(AM_CXXFLAGS) +endif # other linking flags to add