From a905526f71a48dc8fb227a5aba28322d2683c451 Mon Sep 17 00:00:00 2001
From: Alexander Smorkalov <alexander.smorkalov@xperience.ai>
Date: Fri, 30 Aug 2024 12:09:04 +0300
Subject: [PATCH] Got rid of CAROTENE_NEON_ARCH and use standard __ARM_ARCH
 check.

---
 3rdparty/carotene/CMakeLists.txt                      | 10 +---------
 3rdparty/carotene/hal/tegra_hal.hpp                   |  4 ++--
 3rdparty/carotene/src/common.hpp                      | 11 -----------
 3rdparty/carotene/src/vround_helper.hpp               |  8 ++++----
 CMakeLists.txt                                        | 10 +---------
 .../config_reference/config_reference.markdown        |  1 -
 6 files changed, 8 insertions(+), 36 deletions(-)

diff --git a/3rdparty/carotene/CMakeLists.txt b/3rdparty/carotene/CMakeLists.txt
index dc780fd645..e229cbc022 100644
--- a/3rdparty/carotene/CMakeLists.txt
+++ b/3rdparty/carotene/CMakeLists.txt
@@ -42,17 +42,9 @@ endif()
 
 if(WITH_NEON)
     target_compile_definitions(carotene_objs PRIVATE "-DWITH_NEON")
-    if(NOT DEFINED CAROTENE_NEON_ARCH )
-    elseif(CAROTENE_NEON_ARCH EQUAL 8)
-	    target_compile_definitions(carotene_objs PRIVATE "-DCAROTENE_NEON_ARCH=8")
-    elseif(CAROTENE_NEON_ARCH EQUAL 7)
-	    target_compile_definitions(carotene_objs PRIVATE "-DCAROTENE_NEON_ARCH=7")
-    else()
-	    target_compile_definitions(carotene_objs PRIVATE "-DCAROTENE_NEON_ARCH=0")
-    endif()
 endif()
 
- if(MINGW) 
+ if(MINGW)
     target_compile_definitions(carotene_objs PRIVATE "-D_USE_MATH_DEFINES=1")
  endif()
 
diff --git a/3rdparty/carotene/hal/tegra_hal.hpp b/3rdparty/carotene/hal/tegra_hal.hpp
index 7f67ecf1a3..cb658e8af0 100644
--- a/3rdparty/carotene/hal/tegra_hal.hpp
+++ b/3rdparty/carotene/hal/tegra_hal.hpp
@@ -1857,7 +1857,7 @@ TegraCvtColor_Invoker(bgrx2hsvf, bgrx2hsv, src_data + static_cast<size_t>(range.
 #endif
 
 // The optimized branch was developed for old armv7 processors and leads to perf degradation on armv8
-#if defined(DCAROTENE_NEON_ARCH) && (DCAROTENE_NEON_ARCH == 7)
+#if defined(__ARM_ARCH) && (__ARM_ARCH == 7)
 inline CAROTENE_NS::BORDER_MODE borderCV2Carotene(int borderType)
 {
     switch(borderType)
@@ -1928,7 +1928,7 @@ inline int TEGRA_GaussianBlurBinomial(const uchar* src_data, size_t src_step, uc
 #undef cv_hal_gaussianBlurBinomial
 #define cv_hal_gaussianBlurBinomial TEGRA_GaussianBlurBinomial
 
-#endif // DCAROTENE_NEON_ARCH=7
+#endif // __ARM_ARCH=7
 
 #endif // OPENCV_IMGPROC_HAL_INTERFACE_H
 
diff --git a/3rdparty/carotene/src/common.hpp b/3rdparty/carotene/src/common.hpp
index b9de371a6a..823ddf1ccf 100644
--- a/3rdparty/carotene/src/common.hpp
+++ b/3rdparty/carotene/src/common.hpp
@@ -58,17 +58,6 @@
 
 namespace CAROTENE_NS { namespace internal {
 
-#ifndef CAROTENE_NEON_ARCH
-#    if defined(__aarch64__) || defined(__aarch32__)
-#        define CAROTENE_NEON_ARCH 8
-#    else
-#        define CAROTENE_NEON_ARCH 7
-#    endif
-#endif
-#if ( !defined(__aarch64__) && !defined(__aarch32__) ) && (CAROTENE_NEON_ARCH == 8 )
-#    error("ARMv7 doen't support A32/A64 Neon instructions")
-#endif
-
 inline void prefetch(const void *ptr, size_t offset = 32*10)
 {
 #if defined __GNUC__
diff --git a/3rdparty/carotene/src/vround_helper.hpp b/3rdparty/carotene/src/vround_helper.hpp
index 89a6254510..f931a20984 100644
--- a/3rdparty/carotene/src/vround_helper.hpp
+++ b/3rdparty/carotene/src/vround_helper.hpp
@@ -57,7 +57,7 @@ namespace CAROTENE_NS { namespace internal {
 
 inline uint32x4_t vroundq_u32_f32(const float32x4_t val)
 {
-#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
     return vcvtnq_u32_f32(val);
 #else
     const float32x4_t delta = vdupq_n_f32(CAROTENE_ROUND_DELTA);
@@ -67,7 +67,7 @@ inline uint32x4_t vroundq_u32_f32(const float32x4_t val)
 
 inline uint32x2_t vround_u32_f32(const float32x2_t val)
 {
-#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
     return vcvtn_u32_f32(val);
 #else
     const float32x2_t delta = vdup_n_f32(CAROTENE_ROUND_DELTA);
@@ -77,7 +77,7 @@ inline uint32x2_t vround_u32_f32(const float32x2_t val)
 
 inline int32x4_t vroundq_s32_f32(const float32x4_t val)
 {
-#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
     return vcvtnq_s32_f32(val);
 #else
     const float32x4_t delta = vdupq_n_f32(CAROTENE_ROUND_DELTA);
@@ -87,7 +87,7 @@ inline int32x4_t vroundq_s32_f32(const float32x4_t val)
 
 inline int32x2_t vround_s32_f32(const float32x2_t val)
 {
-#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */
+#if defined(__ARM_ARCH) && (__ARM_ARCH >= 8)
     return vcvtn_s32_f32(val);
 #else
     const float32x2_t delta = vdup_n_f32(CAROTENE_ROUND_DELTA);
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ecc56e547a..e4ea325d69 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1018,15 +1018,7 @@ foreach(hal ${OpenCV_HAL})
     if(";${CPU_BASELINE_FINAL};" MATCHES ";NEON;")
       add_subdirectory(3rdparty/carotene/hal)
       ocv_hal_register(CAROTENE_HAL_LIBRARIES CAROTENE_HAL_HEADERS CAROTENE_HAL_INCLUDE_DIRS)
-
-      if( NOT DEFINED CAROTENE_NEON_ARCH)
-          set(CAROTENE_NEON_MSG "Auto detected")
-      elseif( CAROTENE_NEON_ARCH GREATER 7)
-          set(CAROTENE_NEON_MSG "Force ARMv8+")
-      else()
-          set(CAROTENE_NEON_MSG "Force ARMv7")
-      endif()
-      list(APPEND OpenCV_USED_HAL "carotene (ver ${CAROTENE_HAL_VERSION}, ${CAROTENE_NEON_MSG})")
+      list(APPEND OpenCV_USED_HAL "carotene (ver ${CAROTENE_HAL_VERSION})")
     else()
       message(STATUS "Carotene: NEON is not available, disabling carotene...")
     endif()
diff --git a/doc/tutorials/introduction/config_reference/config_reference.markdown b/doc/tutorials/introduction/config_reference/config_reference.markdown
index 6beee31201..ab8bdee229 100644
--- a/doc/tutorials/introduction/config_reference/config_reference.markdown
+++ b/doc/tutorials/introduction/config_reference/config_reference.markdown
@@ -588,7 +588,6 @@ Following options can be used to change installation layout for common scenarios
 | `BUILD_FAT_JAVA_LIB` | _ON_ (for static Android builds) | Build single _opencv_java_ dynamic library containing all library functionality bundled with Java bindings. |
 | `BUILD_opencv_python2` | _ON_ | Build python2 bindings (deprecated). Python with development files and numpy must be installed. |
 | `BUILD_opencv_python3` | _ON_ | Build python3 bindings. Python with development files and numpy must be installed. |
-| `CAROTENE_NEON_ARCH` | '(auto)' | Switch NEON Arch for Carotene. If it sets nothing, it will be auto-detected. If it sets 8, ARMv8(and later) is used. Otherwise, ARMv7 is used. |
 
 TODO: need separate tutorials covering bindings builds