File: pcl_find_avx.cmake

package info (click to toggle)
pcl 1.15.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 143,128 kB
  • sloc: cpp: 520,234; xml: 28,792; ansic: 8,212; python: 334; lisp: 93; sh: 49; makefile: 30
file content (59 lines) | stat: -rw-r--r-- 1,819 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
###############################################################################
# Check for the presence of AVX and figure out the flags to use for it.
function(PCL_CHECK_FOR_AVX)
  set(AVX_FLAGS)

  include(CheckCXXSourceRuns)
  
  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
    # Setting -march & -mtune just as required flags for check_cxx_source_runs,
    # and CMAKE_REQUIRED_FLAGS will be restored after test runs.
    set(CMAKE_REQUIRED_FLAGS "-march=native -mtune=native")
  endif()

  check_cxx_source_runs("    
    #include <immintrin.h>
    int main()
    {
      __m256i a = {0};
      a = _mm256_abs_epi16(a);
      return 0;
    }"
  HAVE_AVX2)

  if(NOT HAVE_AVX2)
    check_cxx_source_runs("
      #include <immintrin.h>
      int main()
      {
        __m256 a;
        a = _mm256_set1_ps(0);
        return 0;
      }"
    HAVE_AVX)
  endif()

  set(CMAKE_REQUIRED_FLAGS)

# Setting the -mavx/-mavx2 defines __AVX(2)__, see here https://stackoverflow.com/a/28939692
# and this allows the compiler to use the codes for AVX behind code guards.
  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
    if(HAVE_AVX2)
      set(AVX_FLAGS "-mavx2" PARENT_SCOPE)
    elseif(HAVE_AVX)
      set(AVX_FLAGS "-mavx" PARENT_SCOPE)
    endif()
  endif()

# Setting the /arch defines __AVX(2)__, see here https://docs.microsoft.com/en-us/cpp/build/reference/arch-x64?view=msvc-160
# AVX2 extends and includes AVX.
# Setting these defines allows the compiler to use AVX instructions as well as code guarded with the defines.
# TODO: Add AVX512 variant if needed.
  if(MSVC)
    if(HAVE_AVX2)
      set(AVX_FLAGS "/arch:AVX2" PARENT_SCOPE)
    elseif(HAVE_AVX)
      set(AVX_FLAGS "/arch:AVX" PARENT_SCOPE)
    endif()
  endif()
endfunction()