OTBCheckSSEFeatures.cmake 1.71 KB
Newer Older
1 2 3 4 5
function(check_sse_features sse_flags )
  include(CheckCXXSourceRuns)
  include(CheckCXXCompilerFlag)

  set(${sse_flags} "0" PARENT_SCOPE)
6 7 8 9 10
  # For apple assume sse2 is on for all intel builds, check for 64 and 32 bit versions
  if(APPLE)
      message(STATUS "For apple assume sse2 is on for all intel builds")
    return()
  endif()
11 12
  set(sse_flags_detected)
  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
13
    set(CMAKE_REQUIRED_FLAGS "-msse2")
14 15 16 17 18 19 20
    set(sse_flags_detected "-msse2 -mfpmath=sse")
  elseif(MSVC AND NOT CMAKE_CL_64)
    set(CMAKE_REQUIRED_FLAGS "/arch:SSE2")
    set(sse_flags_detected "/arch:SSE2")
  endif()

  check_cxx_source_runs("
21
    #include <emmintrin.h>
22
    int main ()
23
    {
24 25 26 27 28 29 30 31 32 33 34 35 36
     __m128d a, b;
     double vals[2] = {0};
     a = _mm_loadu_pd (vals);
     b = _mm_add_pd (a,a);
     _mm_storeu_pd (vals,b);
     return (0);
    }"
    HAVE_SSE2_EXTENSIONS)

  if(HAVE_SSE2_EXTENSIONS)
    set(${sse_flags} "${sse_flags_detected}" PARENT_SCOPE)
    return()
  endif()
37

38
  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
39
    set(CMAKE_REQUIRED_FLAGS "-msse")
40 41 42 43 44 45 46
    set(sse_flags_detected "-msse -mfpmath=sse")
  elseif(MSVC AND NOT CMAKE_CL_64)
    set(CMAKE_REQUIRED_FLAGS "/arch:SSE")
    set(sse_flags_detected "/arch:SSE")
  endif()

  check_cxx_source_runs("
47 48 49 50 51 52 53 54 55 56
    #include <xmmintrin.h>
    int main()
    {
        __m128 a, b;
        float vals[4] = {0};
        a = _mm_loadu_ps(vals);
        b = a;
        b = _mm_add_ps(a,b);
        _mm_storeu_ps(vals,b);
        return 0;
57
    }" HAVE_SSE_EXTENSIONS)
58

59 60 61 62
  if(HAVE_SSE_EXTENSIONS)
    set(${sse_flags} "${sse_flags_detected}" PARENT_SCOPE)
    return()
  endif()
63
endfunction() #check_sse_features