From 6d289fa5f4759c68a189d9ad10ad0e366b31d32a Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 18 Apr 2017 09:45:58 +0200
Subject: [PATCH] [CMake] Added cpu arch flags for MSVC and option
 OGS_ENABLE_AVX2.

---
 CMakeLists.txt                    |  3 ++-
 scripts/cmake/CompilerSetup.cmake | 14 ++++++++++++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 29b1d57dfc0..50eaa95913f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,7 +23,8 @@ list(APPEND CMAKE_PREFIX_PATH
 option(BUILD_SHARED_LIBS "Create shared libraries?" OFF)
 set(CMAKE_LIBRARY_SEARCH_PATH "" CACHE PATH
     "Additional library installation path, e.g. /opt/local or C:/libs")
-set(OGS_CPU_ARCHITECTURE "native" CACHE STRING "Processor architecture, defaults to native.")
+set(OGS_CPU_ARCHITECTURE "native" CACHE STRING "Processor architecture, defaults to native (*nix) / blend (MSVC).")
+option(OGS_ENABLE_AVX2 "Enable the use of AVX2 instructions" OFF)
 option(OGS_BUILD_TESTS "Should the test executables be built?" ON)
 
 ### CMake includes ###
diff --git a/scripts/cmake/CompilerSetup.cmake b/scripts/cmake/CompilerSetup.cmake
index da29fca3cf5..b048f0b63af 100644
--- a/scripts/cmake/CompilerSetup.cmake
+++ b/scripts/cmake/CompilerSetup.cmake
@@ -26,7 +26,9 @@ endif()
 # Set additional user-given compiler flags
 set(CMAKE_CXX_FLAGS ${OGS_CXX_FLAGS})
 
-if(OGS_CPU_ARCHITECTURE STREQUAL "generic")
+if(OGS_ENABLE_AVX2)
+    set(CPU_FLAGS "-mavx2 -march=core-avx2")
+elseif(OGS_CPU_ARCHITECTURE STREQUAL "generic")
     set(CPU_FLAGS "-mtune=generic")
 else()
     set(CPU_FLAGS "-march=${OGS_CPU_ARCHITECTURE}")
@@ -93,6 +95,14 @@ endif ()
 if (WIN32)
     ## For Visual Studio compiler
     if (MSVC)
+        if(OGS_CPU_ARCHITECTURE STREQUAL "generic")
+            set(CPU_FLAGS "/favor:blend")
+        else()
+            set(CPU_FLAGS "/favor:${OGS_CPU_ARCHITECTURE}")
+        endif()
+        if(OGS_ENABLE_AVX2)
+            set(CPU_FLAGS "${CPU_FLAGS} /arch:AVX2")
+        endif()
         add_definitions(
             -D_CRT_SECURE_NO_WARNINGS
             -D_CRT_NONSTDC_NO_WARNINGS
@@ -101,7 +111,7 @@ if (WIN32)
             -DNOMINMAX # This fixes compile errors with std::numeric_limits<T>::min() / max()
         )
         # Sets warning level 3 and ignores some warnings
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /wd4290 /wd4267 /wd4996")
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CPU_FLAGS} /W3 /wd4290 /wd4267 /wd4996")
         # Allow big object files generated for template heavy code
         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
         set(CMAKE_CXX_FLAGS_RELWITHDEBINFO  "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /ZI /Od /Ob0")
-- 
GitLab