diff --git a/cmake/checks/cpu_fp16.cpp b/cmake/checks/cpu_fp16.cpp index c57b5d47b6..99ac93ef8c 100644 --- a/cmake/checks/cpu_fp16.cpp +++ b/cmake/checks/cpu_fp16.cpp @@ -11,8 +11,7 @@ int test() _mm_storel_epi64((__m128i*)dst, v_dst); return (int)dst[0]; } -#elif (defined __GNUC__ && (defined __arm__ || defined __aarch64__)) /*|| (defined _MSC_VER && defined _M_ARM64)*/ -// Windows + ARM64 case disabled: https://github.com/opencv/opencv/issues/25052 +#elif (defined __GNUC__ && (defined __arm__ || defined __aarch64__)) #include "arm_neon.h" int test() { @@ -23,6 +22,17 @@ int test() *(float16x4_t*)dst = v_dst; return (int)dst[0]; } +#elif defined _MSC_VER && (defined _M_ARM64 || defined _M_ARM64EC) +// OpenCV is built with _ARM64_DISTINCT_NEON_TYPES, which hides the vcvt_*_f16 wrappers. +#define _ARM64_DISTINCT_NEON_TYPES +#include "arm_neon.h" +int test() +{ + short dst[8] = { 0 }; + __n128 v_src = neon_fcvtl_32(neon_ld1m_16((const __int16*)dst)); + neon_st1m_16((__int16*)dst, neon_fcvtn_32(v_src)); + return (int)dst[0]; +} #else #error "FP16 is not supported" #endif diff --git a/modules/core/include/opencv2/core/hal/intrin_neon.hpp b/modules/core/include/opencv2/core/hal/intrin_neon.hpp index 7697ca76cd..b5d4173bca 100644 --- a/modules/core/include/opencv2/core/hal/intrin_neon.hpp +++ b/modules/core/include/opencv2/core/hal/intrin_neon.hpp @@ -2651,6 +2651,23 @@ inline void v_lut_deinterleave(const double* tab, const v_int32x4& idxvec, v_flo ////// FP16 support /////// #if CV_FP16 +#if defined(_MSC_VER) && !defined(__clang__) +// _ARM64_DISTINCT_NEON_TYPES, which this build requires for distinct NEON vector types, hides +// float16x4_t and the vcvt_*_f16 wrappers; the same instructions stay reachable as raw neon_*. +inline v_float32x4 v_load_expand(const hfloat* ptr) +{ + __n128 f32 = neon_fcvtl_32(neon_ld1m_16((const __int16*)ptr)); + // memcpy is the only conversion MSVC accepts between __n128 and a distinct NEON type. + float32x4_t v; std::memcpy(&v, &f32, sizeof(v)); + return v_float32x4(v); +} + +inline void v_pack_store(hfloat* ptr, const v_float32x4& v) +{ + __n128 f32; std::memcpy(&f32, &v.val, sizeof(f32)); + neon_st1m_16((__int16*)ptr, neon_fcvtn_32(f32)); +} +#else inline v_float32x4 v_load_expand(const hfloat* ptr) { float16x4_t v = @@ -2672,6 +2689,7 @@ inline void v_pack_store(hfloat* ptr, const v_float32x4& v) vst1_f16((__fp16*)ptr, hv); #endif } +#endif #else inline v_float32x4 v_load_expand(const hfloat* ptr) { diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index efe7a999d2..53aef57b89 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -688,6 +688,7 @@ struct HWFeatures #endif #if defined _M_ARM64 || defined _M_ARM64EC have[CV_CPU_NEON] = true; + have[CV_CPU_FP16] = true; #endif #ifdef __riscv_vector have[CV_CPU_RISCVV] = true;