From b6ec9b9d8c6b1a03e8fbed494951b2625740e92f Mon Sep 17 00:00:00 2001 From: Tomoaki Teshima Date: Tue, 19 Dec 2023 09:40:35 +0900 Subject: [PATCH] prepare to build for ARM64 on Windows with Visual Studio --- cmake/OpenCVCompilerOptimizations.cmake | 28 ++++++++++++++++--------- cmake/checks/cpu_fp16.cpp | 2 +- cmake/checks/cpu_neon_bf16.cpp | 2 +- cmake/checks/cpu_neon_dotprod.cpp | 2 +- cmake/checks/cpu_neon_fp16.cpp | 2 +- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/cmake/OpenCVCompilerOptimizations.cmake b/cmake/OpenCVCompilerOptimizations.cmake index 8a46a97ae0..ea577e4a1c 100644 --- a/cmake/OpenCVCompilerOptimizations.cmake +++ b/cmake/OpenCVCompilerOptimizations.cmake @@ -349,12 +349,18 @@ elseif(ARM OR AARCH64) ocv_update(CPU_KNOWN_OPTIMIZATIONS "NEON;FP16;NEON_DOTPROD;NEON_FP16;NEON_BF16") ocv_update(CPU_NEON_FLAGS_ON "") ocv_update(CPU_FP16_IMPLIES "NEON") - ocv_update(CPU_NEON_DOTPROD_FLAGS_ON "-march=armv8.2-a+dotprod") ocv_update(CPU_NEON_DOTPROD_IMPLIES "NEON") - ocv_update(CPU_NEON_FP16_FLAGS_ON "-march=armv8.2-a+fp16") ocv_update(CPU_NEON_FP16_IMPLIES "NEON") - ocv_update(CPU_NEON_BF16_FLAGS_ON "-march=armv8.2-a+bf16") ocv_update(CPU_NEON_BF16_IMPLIES "NEON") + if(MSVC) + ocv_update(CPU_NEON_DOTPROD_FLAGS_ON "") + ocv_update(CPU_NEON_FP16_FLAGS_ON "") + ocv_update(CPU_NEON_BF16_FLAGS_ON "") + else() + ocv_update(CPU_NEON_DOTPROD_FLAGS_ON "-march=armv8.2-a+dotprod") + ocv_update(CPU_NEON_FP16_FLAGS_ON "-march=armv8.2-a+fp16") + ocv_update(CPU_NEON_BF16_FLAGS_ON "-march=armv8.2-a+bf16") + endif() set(CPU_BASELINE "NEON;FP16" CACHE STRING "${HELP_CPU_BASELINE}") set(CPU_DISPATCH "NEON_FP16;NEON_BF16;NEON_DOTPROD" CACHE STRING "${HELP_CPU_DISPATCH}") endif() @@ -478,20 +484,22 @@ macro(ocv_check_compiler_optimization OPT) endmacro() macro(ocv_cpu_aarch64_baseline_merge_feature_options FEATURE_NAME_LIST FLAG_STRING COMMON_OPTION) + if(NOT MSVC) unset(_POSTFIX) # Check each feature option foreach(OPT IN LISTS ${FEATURE_NAME_LIST}) - string(FIND "${${FLAG_STRING}}" "${CPU_${OPT}_FLAGS_ON}" OPT_FOUND) - if(NOT ${OPT_FOUND} EQUAL -1) - string(REPLACE "${COMMON_OPTION}" "" TRAILING_PART "${CPU_${OPT}_FLAGS_ON}") - string(APPEND _POSTFIX "${TRAILING_PART}") - string(REPLACE " ${CPU_${OPT}_FLAGS_ON}" "" ${FLAG_STRING} ${${FLAG_STRING}}) - endif() + string(FIND "${${FLAG_STRING}}" "${CPU_${OPT}_FLAGS_ON}" OPT_FOUND) + if(NOT ${OPT_FOUND} EQUAL -1) + string(REPLACE "${COMMON_OPTION}" "" TRAILING_PART "${CPU_${OPT}_FLAGS_ON}") + string(APPEND _POSTFIX "${TRAILING_PART}") + string(REPLACE " ${CPU_${OPT}_FLAGS_ON}" "" ${FLAG_STRING} ${${FLAG_STRING}}) + endif() endforeach() # If more than one option found, merge them if(NOT "x${_POSTFIX}" STREQUAL "x") - set(${FLAG_STRING} "${${FLAG_STRING}} ${COMMON_OPTION}${_POSTFIX}") + set(${FLAG_STRING} "${${FLAG_STRING}} ${COMMON_OPTION}${_POSTFIX}") endif() + endif() endmacro() foreach(OPT ${CPU_KNOWN_OPTIMIZATIONS}) diff --git a/cmake/checks/cpu_fp16.cpp b/cmake/checks/cpu_fp16.cpp index f12cb10f4d..6bffc4792a 100644 --- a/cmake/checks/cpu_fp16.cpp +++ b/cmake/checks/cpu_fp16.cpp @@ -11,7 +11,7 @@ int test() _mm_storel_epi64((__m128i*)dst, v_dst); return (int)dst[0]; } -#elif defined __GNUC__ && (defined __arm__ || defined __aarch64__) +#elif (defined __GNUC__ && (defined __arm__ || defined __aarch64__)) || (defined _MSC_VER && defined _M_ARM64) #include "arm_neon.h" int test() { diff --git a/cmake/checks/cpu_neon_bf16.cpp b/cmake/checks/cpu_neon_bf16.cpp index c18d63c579..a9045d7117 100644 --- a/cmake/checks/cpu_neon_bf16.cpp +++ b/cmake/checks/cpu_neon_bf16.cpp @@ -1,4 +1,4 @@ -#if defined __GNUC__ && (defined __arm__ || defined __aarch64__) +#if (defined __GNUC__ && (defined __arm__ || defined __aarch64__)) || (defined _MSC_VER && defined _M_ARM64) #include #include "arm_neon.h" diff --git a/cmake/checks/cpu_neon_dotprod.cpp b/cmake/checks/cpu_neon_dotprod.cpp index 4f39c50659..74f44a1832 100644 --- a/cmake/checks/cpu_neon_dotprod.cpp +++ b/cmake/checks/cpu_neon_dotprod.cpp @@ -1,6 +1,6 @@ #include -#if defined __GNUC__ && (defined __arm__ || defined __aarch64__) +#if (defined __GNUC__ && (defined __arm__ || defined __aarch64__)) || (defined _MSC_VER && defined _M_ARM64) #include "arm_neon.h" int test() { diff --git a/cmake/checks/cpu_neon_fp16.cpp b/cmake/checks/cpu_neon_fp16.cpp index 20fbab25d5..bba5b97026 100644 --- a/cmake/checks/cpu_neon_fp16.cpp +++ b/cmake/checks/cpu_neon_fp16.cpp @@ -1,6 +1,6 @@ #include -#if defined __GNUC__ && (defined __arm__ || defined __aarch64__) +#if (defined __GNUC__ && (defined __arm__ || defined __aarch64__)) || (defined _MSC_VER && defined _M_ARM64) #include "arm_neon.h" float16x8_t vld1q_as_f16(const float* src)