tgollogly commited on
Commit
dffe123
·
verified ·
1 Parent(s): e490926

Upload 7492 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +9 -0
  2. 3rdparty/carotene/CMakeLists.txt +52 -0
  3. 3rdparty/carotene/README.md +2 -0
  4. 3rdparty/carotene/hal/CMakeLists.txt +102 -0
  5. 3rdparty/carotene/hal/dummy.cpp +2 -0
  6. 3rdparty/carotene/hal/tegra_hal.hpp +1935 -0
  7. 3rdparty/carotene/include/carotene/definitions.hpp +47 -0
  8. 3rdparty/carotene/include/carotene/functions.hpp +0 -0
  9. 3rdparty/carotene/include/carotene/types.hpp +125 -0
  10. 3rdparty/carotene/src/absdiff.cpp +241 -0
  11. 3rdparty/carotene/src/accumulate.cpp +408 -0
  12. 3rdparty/carotene/src/add.cpp +475 -0
  13. 3rdparty/carotene/src/add_weighted.cpp +266 -0
  14. 3rdparty/carotene/src/bitwise.cpp +225 -0
  15. 3rdparty/carotene/src/blur.cpp +1336 -0
  16. 3rdparty/carotene/src/canny.cpp +773 -0
  17. 3rdparty/carotene/src/channel_extract.cpp +486 -0
  18. 3rdparty/carotene/src/channels_combine.cpp +389 -0
  19. 3rdparty/carotene/src/cmp.cpp +340 -0
  20. 3rdparty/carotene/src/colorconvert.cpp +0 -0
  21. 3rdparty/carotene/src/common.cpp +108 -0
  22. 3rdparty/carotene/src/common.hpp +97 -0
  23. 3rdparty/carotene/src/convert.cpp +1331 -0
  24. 3rdparty/carotene/src/convert_depth.cpp +399 -0
  25. 3rdparty/carotene/src/convert_scale.cpp +0 -0
  26. 3rdparty/carotene/src/convolution.cpp +340 -0
  27. 3rdparty/carotene/src/count_nonzero.cpp +430 -0
  28. 3rdparty/carotene/src/div.cpp +681 -0
  29. 3rdparty/carotene/src/dot_product.cpp +260 -0
  30. 3rdparty/carotene/src/dummy.cpp +2 -0
  31. 3rdparty/carotene/src/fast.cpp +428 -0
  32. 3rdparty/carotene/src/fill_minmaxloc.cpp +442 -0
  33. 3rdparty/carotene/src/flip.cpp +222 -0
  34. 3rdparty/carotene/src/gaussian_blur.cpp +1059 -0
  35. 3rdparty/carotene/src/in_range.cpp +195 -0
  36. 3rdparty/carotene/src/integral.cpp +238 -0
  37. 3rdparty/carotene/src/intrinsics.hpp +112 -0
  38. 3rdparty/carotene/src/laplacian.cpp +713 -0
  39. 3rdparty/carotene/src/magnitude.cpp +160 -0
  40. 3rdparty/carotene/src/meanstddev.cpp +163 -0
  41. 3rdparty/carotene/src/median_filter.cpp +227 -0
  42. 3rdparty/carotene/src/min_max.cpp +139 -0
  43. 3rdparty/carotene/src/minmaxloc.cpp +1340 -0
  44. 3rdparty/carotene/src/morph.cpp +728 -0
  45. 3rdparty/carotene/src/mul.cpp +1572 -0
  46. 3rdparty/carotene/src/norm.cpp +1310 -0
  47. 3rdparty/carotene/src/opticalflow.cpp +539 -0
  48. 3rdparty/carotene/src/phase.cpp +273 -0
  49. 3rdparty/carotene/src/pyramid.cpp +1414 -0
  50. 3rdparty/carotene/src/reduce.cpp +460 -0
.gitattributes CHANGED
@@ -33,3 +33,12 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data/vec_files/trainingfaces_24-24.vec filter=lfs diff=lfs merge=lfs -text
37
+ doc/js_tutorials/js_assets/cup.mp4 filter=lfs diff=lfs merge=lfs -text
38
+ doc/tutorials/features2d/akaze_matching/images/graf.png filter=lfs diff=lfs merge=lfs -text
39
+ doc/tutorials/features2d/akaze_matching/images/res.png filter=lfs diff=lfs merge=lfs -text
40
+ samples/cpp/tutorial_code/calib3d/real_time_pose_estimation/Data/box.mp4 filter=lfs diff=lfs merge=lfs -text
41
+ samples/data/Megamind.avi filter=lfs diff=lfs merge=lfs -text
42
+ samples/data/tree.avi filter=lfs diff=lfs merge=lfs -text
43
+ samples/data/vtest.avi filter=lfs diff=lfs merge=lfs -text
44
+ samples/java/sbt/sbt/sbt-launch.jar filter=lfs diff=lfs merge=lfs -text
3rdparty/carotene/CMakeLists.txt ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ cmake_minimum_required(VERSION ${MIN_VER_CMAKE} FATAL_ERROR)
2
+
3
+ project(Carotene)
4
+
5
+ set(CAROTENE_NS "carotene" CACHE STRING "Namespace for Carotene definitions")
6
+
7
+ set(CAROTENE_INCLUDE_DIR include)
8
+ set(CAROTENE_SOURCE_DIR src)
9
+
10
+ file(GLOB_RECURSE carotene_headers RELATIVE "${CMAKE_CURRENT_LIST_DIR}" "${CAROTENE_INCLUDE_DIR}/*.hpp")
11
+ file(GLOB_RECURSE carotene_sources RELATIVE "${CMAKE_CURRENT_LIST_DIR}" "${CAROTENE_SOURCE_DIR}/*.cpp"
12
+ "${CAROTENE_SOURCE_DIR}/*.hpp")
13
+
14
+ include_directories(${CAROTENE_INCLUDE_DIR})
15
+
16
+ if(CMAKE_COMPILER_IS_GNUCC)
17
+ set(CMAKE_CXX_FLAGS "-fvisibility=hidden ${CMAKE_CXX_FLAGS}")
18
+
19
+ # allow more inlines - these parameters improve performance for:
20
+ # - matchTemplate about 5-10%
21
+ # - goodFeaturesToTrack 10-20%
22
+ # - cornerHarris 30% for some cases
23
+ if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "10.0.0")
24
+ set_source_files_properties(${carotene_sources} COMPILE_FLAGS "--param ipcp-unit-growth=100000 --param inline-unit-growth=100000 --param large-stack-frame-growth=5000")
25
+ else()
26
+ set_source_files_properties(${carotene_sources} COMPILE_FLAGS "--param ipa-cp-unit-growth=100000 --param inline-unit-growth=100000 --param large-stack-frame-growth=5000")
27
+ endif()
28
+ endif()
29
+
30
+ if(APPLE AND CV_CLANG AND WITH_NEON)
31
+ ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-unused-function)
32
+ endif()
33
+
34
+ add_library(carotene_objs OBJECT EXCLUDE_FROM_ALL
35
+ ${carotene_headers}
36
+ ${carotene_sources}
37
+ )
38
+
39
+ if(NOT CAROTENE_NS STREQUAL "carotene")
40
+ target_compile_definitions(carotene_objs PUBLIC "-DCAROTENE_NS=${CAROTENE_NS}")
41
+ endif()
42
+
43
+ if(WITH_NEON)
44
+ target_compile_definitions(carotene_objs PRIVATE "-DWITH_NEON")
45
+ endif()
46
+
47
+ if(MINGW)
48
+ target_compile_definitions(carotene_objs PRIVATE "-D_USE_MATH_DEFINES=1")
49
+ endif()
50
+
51
+ # we add dummy file to fix XCode build
52
+ add_library(carotene STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} "$<TARGET_OBJECTS:carotene_objs>" "${CAROTENE_SOURCE_DIR}/dummy.cpp")
3rdparty/carotene/README.md ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ This is Carotene, a low-level library containing optimized CPU routines
2
+ that are useful for computer vision algorithms.
3rdparty/carotene/hal/CMakeLists.txt ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ cmake_minimum_required(VERSION ${MIN_VER_CMAKE} FATAL_ERROR)
2
+
3
+ include(CheckCCompilerFlag)
4
+ include(CheckCXXCompilerFlag)
5
+
6
+ set(TEGRA_HAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
7
+ set(CAROTENE_DIR "${TEGRA_HAL_DIR}/../")
8
+
9
+ if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm.*|ARM.*)")
10
+ set(ARM TRUE)
11
+ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64.*|AARCH64.*")
12
+ set(AARCH64 TRUE)
13
+ endif()
14
+
15
+ ocv_warnings_disable(CMAKE_CXX_FLAGS -Wunused-function)
16
+
17
+ set(TEGRA_COMPILER_FLAGS "")
18
+
19
+ if(CV_GCC OR CV_CLANG)
20
+ # Generate unwind information even for functions that can't throw/propagate exceptions.
21
+ # This lets debuggers and such get non-broken backtraces for such functions, even without debugging symbols.
22
+ list(APPEND TEGRA_COMPILER_FLAGS -funwind-tables)
23
+ endif()
24
+
25
+ if(CV_GCC OR CV_CLANG)
26
+ if(X86 OR ARMEABI_V6 OR (MIPS AND ANDROID_COMPILER_VERSION VERSION_LESS "4.6"))
27
+ list(APPEND TEGRA_COMPILER_FLAGS -fweb -fwrapv -frename-registers -fsched-stalled-insns-dep=100 -fsched-stalled-insns=2)
28
+ elseif(CV_CLANG)
29
+ list(APPEND TEGRA_COMPILER_FLAGS -fwrapv)
30
+ else()
31
+ list(APPEND TEGRA_COMPILER_FLAGS -fweb -fwrapv -frename-registers -fsched2-use-superblocks -fsched2-use-traces
32
+ -fsched-stalled-insns-dep=100 -fsched-stalled-insns=2)
33
+ endif()
34
+ if((ANDROID_COMPILER_IS_CLANG OR NOT ANDROID_COMPILER_VERSION VERSION_LESS "4.7") AND ANDROID_NDK_RELEASE STRGREATER "r8d" )
35
+ list(APPEND TEGRA_COMPILER_FLAGS -fgraphite -fgraphite-identity -floop-block -floop-flatten -floop-interchange
36
+ -floop-strip-mine -floop-parallelize-all -ftree-loop-linear)
37
+ endif()
38
+ endif()
39
+
40
+ string(REPLACE ";" " " TEGRA_COMPILER_FLAGS "${TEGRA_COMPILER_FLAGS}")
41
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEGRA_COMPILER_FLAGS}")
42
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TEGRA_COMPILER_FLAGS}")
43
+
44
+ if(ARMEABI_V7A)
45
+ if(CV_GCC OR CV_CLANG)
46
+ set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-tree-vectorize" )
47
+ set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-tree-vectorize" )
48
+ endif()
49
+ endif()
50
+
51
+ if(WITH_LOGS)
52
+ add_definitions(-DHAVE_LOGS)
53
+ endif()
54
+
55
+ set(CAROTENE_NS "carotene_o4t" CACHE STRING "" FORCE)
56
+
57
+ function(compile_carotene)
58
+ if(";${CPU_BASELINE_FINAL};" MATCHES ";NEON;")
59
+ set(WITH_NEON ON)
60
+ endif()
61
+
62
+ add_subdirectory("${CAROTENE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/carotene")
63
+ endfunction()
64
+
65
+ compile_carotene()
66
+
67
+ include_directories("${CAROTENE_DIR}/include")
68
+
69
+ get_target_property(carotene_defs carotene_objs INTERFACE_COMPILE_DEFINITIONS)
70
+ set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${carotene_defs})
71
+
72
+ if(CV_GCC)
73
+ # allow more inlines - these parameters improve performance for:
74
+ # matchTemplate about 5-10%
75
+ # goodFeaturesToTrack 10-20%
76
+ # cornerHarris 30% for some cases
77
+ if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "10.0.0")
78
+ set_source_files_properties(impl.cpp $<TARGET_OBJECTS:carotene_objs> COMPILE_FLAGS "--param ipcp-unit-growth=100000 --param inline-unit-growth=100000 --param large-stack-frame-growth=5000")
79
+ else()
80
+ set_source_files_properties(impl.cpp $<TARGET_OBJECTS:carotene_objs> COMPILE_FLAGS "--param ipa-cp-unit-growth=100000 --param inline-unit-growth=100000 --param large-stack-frame-growth=5000")
81
+ endif()
82
+ # set_source_files_properties(impl.cpp $<TARGET_OBJECTS:carotene_objs> COMPILE_FLAGS "--param ipcp-unit-growth=100000 --param inline-unit-growth=100000 --param large-stack-frame-growth=5000")
83
+ endif()
84
+
85
+ # we add dummy file to fix XCode build
86
+ add_library(tegra_hal STATIC $<TARGET_OBJECTS:carotene_objs> "dummy.cpp")
87
+ set_target_properties(tegra_hal PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH})
88
+ set(OPENCV_SRC_DIR "${CMAKE_SOURCE_DIR}")
89
+ if(NOT BUILD_SHARED_LIBS)
90
+ ocv_install_target(tegra_hal EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
91
+ endif()
92
+ target_include_directories(tegra_hal PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${OPENCV_SRC_DIR}/modules/core/include)
93
+
94
+ set(CAROTENE_HAL_VERSION "0.0.1" PARENT_SCOPE)
95
+ set(CAROTENE_HAL_LIBRARIES "tegra_hal" PARENT_SCOPE)
96
+ set(CAROTENE_HAL_HEADERS "carotene/tegra_hal.hpp" PARENT_SCOPE)
97
+ set(CAROTENE_HAL_INCLUDE_DIRS "${CMAKE_BINARY_DIR}" PARENT_SCOPE)
98
+
99
+ configure_file("tegra_hal.hpp" "${CMAKE_BINARY_DIR}/carotene/tegra_hal.hpp" COPYONLY)
100
+ configure_file("${CAROTENE_DIR}/include/carotene/definitions.hpp" "${CMAKE_BINARY_DIR}/carotene/definitions.hpp" COPYONLY)
101
+ configure_file("${CAROTENE_DIR}/include/carotene/functions.hpp" "${CMAKE_BINARY_DIR}/carotene/functions.hpp" COPYONLY)
102
+ configure_file("${CAROTENE_DIR}/include/carotene/types.hpp" "${CMAKE_BINARY_DIR}/carotene/types.hpp" COPYONLY)
3rdparty/carotene/hal/dummy.cpp ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ // This file is needed for compilation on some platforms e.g. with XCode generator
2
+ // Related issue: https://gitlab.kitware.com/cmake/cmake/-/issues/17457
3rdparty/carotene/hal/tegra_hal.hpp ADDED
@@ -0,0 +1,1935 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2016, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #ifndef _tegra_hal_H_INCLUDED_
41
+ #define _tegra_hal_H_INCLUDED_
42
+
43
+ #define CAROTENE_NS carotene_o4t
44
+
45
+ #include "carotene/functions.hpp"
46
+ #include <cstddef>
47
+ #include <cstring>
48
+ #include <vector>
49
+ #include <opencv2/core/base.hpp>
50
+
51
+ #define RANGE_DATA(type, base, step) reinterpret_cast<type*>(const_cast<char *>(reinterpret_cast<const char *>(base)) + static_cast<size_t>(range.start) * step)
52
+
53
+ #define PARALLEL_CORE 0
54
+ #if PARALLEL_CORE
55
+
56
+ #define SRC_ARG1 ST * src1_data_, size_t src1_step_,
57
+ #define SRC_STORE1 src1_data(src1_data_), src1_step(src1_step_),
58
+ #define SRC_VAR1 ST * src1_data; \
59
+ size_t src1_step;
60
+ #define SRC_ARG2 ST * src1_data_, size_t src1_step_, \
61
+ ST * src2_data_, size_t src2_step_,
62
+ #define SRC_STORE2 src1_data(src1_data_), src1_step(src1_step_), \
63
+ src2_data(src2_data_), src2_step(src2_step_),
64
+ #define SRC_VAR2 ST * src1_data; \
65
+ size_t src1_step; \
66
+ ST * src2_data; \
67
+ size_t src2_step;
68
+
69
+ #define DST_ARG1 DT * dst1_data_, size_t dst1_step_,
70
+ #define DST_STORE1 dst1_data(dst1_data_), dst1_step(dst1_step_),
71
+ #define DST_VAR1 DT * dst1_data; \
72
+ size_t dst1_step;
73
+
74
+ #define SCALE_ARG0
75
+ #define SCALE_STORE0
76
+ #define SCALE_VAR0
77
+ #define SCALE_ARG1 , double scale_
78
+ #define SCALE_STORE1 , scale(scale_)
79
+ #define SCALE_VAR1 double scale;
80
+ #define SCALE_ARG3 , const double *scales_
81
+ #define SCALE_STORE3 , scales(scales_, scales_ + 3)
82
+ #define SCALE_VAR3 std::vector<double> scales;
83
+
84
+ #define TegraGenOp_Invoker(name, func, src_cnt, dst_cnt, scale_cnt, ...) \
85
+ template <typename ST, typename DT> \
86
+ class TegraGenOp_##name##_Invoker : public cv::ParallelLoopBody \
87
+ { \
88
+ public: \
89
+ TegraGenOp_##name##_Invoker(SRC_ARG##src_cnt \
90
+ DST_ARG##dst_cnt \
91
+ int width_, int height_ \
92
+ SCALE_ARG##scale_cnt) : \
93
+ cv::ParallelLoopBody(), SRC_STORE##src_cnt \
94
+ DST_STORE##dst_cnt \
95
+ width(width_), height(height_) \
96
+ SCALE_STORE##scale_cnt {} \
97
+ virtual void operator()(const cv::Range& range) const \
98
+ { \
99
+ CAROTENE_NS::func(CAROTENE_NS::Size2D(width, range.end-range.start), __VA_ARGS__); \
100
+ } \
101
+ private: \
102
+ SRC_VAR##src_cnt \
103
+ DST_VAR##dst_cnt \
104
+ int width, height; \
105
+ SCALE_VAR##scale_cnt \
106
+ const TegraGenOp_##name##_Invoker& operator= (const TegraGenOp_##name##_Invoker&); \
107
+ };
108
+
109
+ #define TegraBinaryOp_Invoker(name, func) TegraGenOp_Invoker(name, func, 2, 1, 0, \
110
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
111
+ RANGE_DATA(ST, src2_data, src2_step), src2_step, \
112
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step )
113
+
114
+ #define TegraBinaryOp_InvokerVAArg(name, func, ...) TegraGenOp_Invoker(name, func, 2, 1, 0, \
115
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
116
+ RANGE_DATA(ST, src2_data, src2_step), src2_step, \
117
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step, __VA_ARGS__)
118
+
119
+ #define TEGRA_BINARYOP(type, op, src1, sz1, src2, sz2, dst, sz, w, h) \
120
+ ( \
121
+ CAROTENE_NS::isSupportedConfiguration() ? \
122
+ parallel_for_(Range(0, h), \
123
+ TegraGenOp_##op##_Invoker<const type, type>(src1, sz1, src2, sz2, dst, sz, w, h), \
124
+ (w * h) / static_cast<double>(1<<16)), \
125
+ CV_HAL_ERROR_OK \
126
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
127
+ )
128
+
129
+ TegraBinaryOp_InvokerVAArg(add, add, CAROTENE_NS::CONVERT_POLICY_SATURATE) /*Original addition use saturated operator, so use the same from CAROTENE*/
130
+
131
+ TegraBinaryOp_Invoker(addf, add)
132
+
133
+ TegraBinaryOp_InvokerVAArg(sub, sub, CAROTENE_NS::CONVERT_POLICY_SATURATE) /*Original addition use saturated operator, so use the same from CAROTENE*/
134
+
135
+ TegraBinaryOp_Invoker(subf, sub)
136
+
137
+ TegraBinaryOp_Invoker(max, max)
138
+
139
+ TegraBinaryOp_Invoker(min, min)
140
+
141
+ TegraBinaryOp_Invoker(absDiff, absDiff)
142
+
143
+ TegraBinaryOp_Invoker(bitwiseAnd, bitwiseAnd)
144
+
145
+ TegraBinaryOp_Invoker(bitwiseOr, bitwiseOr)
146
+
147
+ TegraBinaryOp_Invoker(bitwiseXor, bitwiseXor)
148
+
149
+ #define TegraUnaryOp_Invoker(name, func) TegraGenOp_Invoker(name, func, 1, 1, 0, \
150
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
151
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step )
152
+
153
+ TegraUnaryOp_Invoker(bitwiseNot, bitwiseNot)
154
+ #define TEGRA_UNARYOP(type, op, src1, sz1, dst, sz, w, h) \
155
+ ( \
156
+ CAROTENE_NS::isSupportedConfiguration() ? \
157
+ parallel_for_(Range(0, h), \
158
+ TegraGenOp_##op##_Invoker<const type, type>(src1, sz1, dst, sz, w, h), \
159
+ (w * h) / static_cast<double>(1<<16)), \
160
+ CV_HAL_ERROR_OK \
161
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
162
+ )
163
+
164
+ #undef cv_hal_add8u
165
+ #define cv_hal_add8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, add, src1, sz1, src2, sz2, dst, sz, w, h)
166
+ #undef cv_hal_add8s
167
+ #define cv_hal_add8s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s8, add, src1, sz1, src2, sz2, dst, sz, w, h)
168
+ #undef cv_hal_add16u
169
+ #define cv_hal_add16u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u16, add, src1, sz1, src2, sz2, dst, sz, w, h)
170
+ #undef cv_hal_add16s
171
+ #define cv_hal_add16s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s16, add, src1, sz1, src2, sz2, dst, sz, w, h)
172
+ #undef cv_hal_add32s
173
+ #define cv_hal_add32s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s32, add, src1, sz1, src2, sz2, dst, sz, w, h)
174
+ #undef cv_hal_add32f
175
+ #define cv_hal_add32f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f32, addf, src1, sz1, src2, sz2, dst, sz, w, h)
176
+ //#undef cv_hal_add64f
177
+ //#define cv_hal_add64f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f64, addf, src1, sz1, src2, sz2, dst, sz, w, h)
178
+ #undef cv_hal_sub8u
179
+ #define cv_hal_sub8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, sub, src1, sz1, src2, sz2, dst, sz, w, h)
180
+ #undef cv_hal_sub8s
181
+ #define cv_hal_sub8s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s8, sub, src1, sz1, src2, sz2, dst, sz, w, h)
182
+ #undef cv_hal_sub16u
183
+ #define cv_hal_sub16u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u16, sub, src1, sz1, src2, sz2, dst, sz, w, h)
184
+ #undef cv_hal_sub16s
185
+ #define cv_hal_sub16s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s16, sub, src1, sz1, src2, sz2, dst, sz, w, h)
186
+ #undef cv_hal_sub32s
187
+ #define cv_hal_sub32s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s32, sub, src1, sz1, src2, sz2, dst, sz, w, h)
188
+ #undef cv_hal_sub32f
189
+ #define cv_hal_sub32f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f32, subf, src1, sz1, src2, sz2, dst, sz, w, h)
190
+ //#undef cv_hal_sub64f
191
+ //#define cv_hal_sub64f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f64, subf, src1, sz1, src2, sz2, dst, sz, w, h)
192
+ #undef cv_hal_max8u
193
+ #define cv_hal_max8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, max, src1, sz1, src2, sz2, dst, sz, w, h)
194
+ #undef cv_hal_max8s
195
+ #define cv_hal_max8s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s8, max, src1, sz1, src2, sz2, dst, sz, w, h)
196
+ #undef cv_hal_max16u
197
+ #define cv_hal_max16u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u16, max, src1, sz1, src2, sz2, dst, sz, w, h)
198
+ #undef cv_hal_max16s
199
+ #define cv_hal_max16s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s16, max, src1, sz1, src2, sz2, dst, sz, w, h)
200
+ #undef cv_hal_max32s
201
+ #define cv_hal_max32s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s32, max, src1, sz1, src2, sz2, dst, sz, w, h)
202
+ #undef cv_hal_max32f
203
+ #define cv_hal_max32f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f32, max, src1, sz1, src2, sz2, dst, sz, w, h)
204
+ //#undef cv_hal_max64f
205
+ //#define cv_hal_max64f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f64, max, src1, sz1, src2, sz2, dst, sz, w, h)
206
+ #undef cv_hal_min8u
207
+ #define cv_hal_min8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, min, src1, sz1, src2, sz2, dst, sz, w, h)
208
+ #undef cv_hal_min8s
209
+ #define cv_hal_min8s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s8, min, src1, sz1, src2, sz2, dst, sz, w, h)
210
+ #undef cv_hal_min16u
211
+ #define cv_hal_min16u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u16, min, src1, sz1, src2, sz2, dst, sz, w, h)
212
+ #undef cv_hal_min16s
213
+ #define cv_hal_min16s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s16, min, src1, sz1, src2, sz2, dst, sz, w, h)
214
+ #undef cv_hal_min32s
215
+ #define cv_hal_min32s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s32, min, src1, sz1, src2, sz2, dst, sz, w, h)
216
+ #undef cv_hal_min32f
217
+ #define cv_hal_min32f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f32, min, src1, sz1, src2, sz2, dst, sz, w, h)
218
+ //#undef cv_hal_min64f
219
+ //#define cv_hal_min64f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f64, min, src1, sz1, src2, sz2, dst, sz, w, h)
220
+ #undef cv_hal_absdiff8u
221
+ #define cv_hal_absdiff8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
222
+ #undef cv_hal_absdiff8s
223
+ #define cv_hal_absdiff8s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s8, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
224
+ #undef cv_hal_absdiff16u
225
+ #define cv_hal_absdiff16u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u16, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
226
+ #undef cv_hal_absdiff16s
227
+ #define cv_hal_absdiff16s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s16, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
228
+ #undef cv_hal_absdiff32s
229
+ #define cv_hal_absdiff32s(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::s32, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
230
+ #undef cv_hal_absdiff32f
231
+ #define cv_hal_absdiff32f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f32, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
232
+ //#undef cv_hal_absdiff64f
233
+ //#define cv_hal_absdiff64f(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::f64, absDiff, src1, sz1, src2, sz2, dst, sz, w, h)
234
+ #undef cv_hal_and8u
235
+ #define cv_hal_and8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, bitwiseAnd, src1, sz1, src2, sz2, dst, sz, w, h)
236
+ #undef cv_hal_or8u
237
+ #define cv_hal_or8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, bitwiseOr, src1, sz1, src2, sz2, dst, sz, w, h)
238
+ #undef cv_hal_xor8u
239
+ #define cv_hal_xor8u(src1, sz1, src2, sz2, dst, sz, w, h) TEGRA_BINARYOP(CAROTENE_NS::u8, bitwiseXor, src1, sz1, src2, sz2, dst, sz, w, h)
240
+ #undef cv_hal_not8u
241
+ #define cv_hal_not8u(src1, sz1, dst, sz, w, h) TEGRA_UNARYOP(CAROTENE_NS::u8, bitwiseNot, src1, sz1, dst, sz, w, h)
242
+
243
+ TegraBinaryOp_Invoker(cmpEQ, cmpEQ)
244
+ TegraBinaryOp_Invoker(cmpNE, cmpNE)
245
+ TegraBinaryOp_Invoker(cmpGT, cmpGT)
246
+ TegraBinaryOp_Invoker(cmpGE, cmpGE)
247
+ TegraGenOp_Invoker(cmpLT, cmpGT, 2, 1, 0, RANGE_DATA(ST, src2_data, src2_step), src2_step, \
248
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
249
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step)
250
+ TegraGenOp_Invoker(cmpLE, cmpGE, 2, 1, 0, RANGE_DATA(ST, src2_data, src2_step), src2_step, \
251
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
252
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step)
253
+ #define TEGRA_CMP(type, src1, sz1, src2, sz2, dst, sz, w, h, op) \
254
+ ( \
255
+ CAROTENE_NS::isSupportedConfiguration() ? \
256
+ ((op) == cv::CMP_EQ) ? \
257
+ parallel_for_(Range(0, h), \
258
+ TegraGenOp_cmpEQ_Invoker<const type, CAROTENE_NS::u8>(src1, sz1, src2, sz2, dst, sz, w, h), \
259
+ (w * h) / static_cast<double>(1<<16)), \
260
+ CV_HAL_ERROR_OK : \
261
+ ((op) == cv::CMP_NE) ? \
262
+ parallel_for_(Range(0, h), \
263
+ TegraGenOp_cmpNE_Invoker<const type, CAROTENE_NS::u8>(src1, sz1, src2, sz2, dst, sz, w, h), \
264
+ (w * h) / static_cast<double>(1<<16)), \
265
+ CV_HAL_ERROR_OK : \
266
+ ((op) == cv::CMP_GT) ? \
267
+ parallel_for_(Range(0, h), \
268
+ TegraGenOp_cmpGT_Invoker<const type, CAROTENE_NS::u8>(src1, sz1, src2, sz2, dst, sz, w, h), \
269
+ (w * h) / static_cast<double>(1<<16)), \
270
+ CV_HAL_ERROR_OK : \
271
+ ((op) == cv::CMP_GE) ? \
272
+ parallel_for_(Range(0, h), \
273
+ TegraGenOp_cmpGE_Invoker<const type, CAROTENE_NS::u8>(src1, sz1, src2, sz2, dst, sz, w, h), \
274
+ (w * h) / static_cast<double>(1<<16)), \
275
+ CV_HAL_ERROR_OK : \
276
+ ((op) == cv::CMP_LT) ? \
277
+ parallel_for_(Range(0, h), \
278
+ TegraGenOp_cmpLT_Invoker<const type, CAROTENE_NS::u8>(src1, sz1, src2, sz2, dst, sz, w, h), \
279
+ (w * h) / static_cast<double>(1<<16)), \
280
+ CV_HAL_ERROR_OK : \
281
+ ((op) == cv::CMP_LE) ? \
282
+ parallel_for_(Range(0, h), \
283
+ TegraGenOp_cmpLE_Invoker<const type, CAROTENE_NS::u8>(src1, sz1, src2, sz2, dst, sz, w, h), \
284
+ (w * h) / static_cast<double>(1<<16)), \
285
+ CV_HAL_ERROR_OK : \
286
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
287
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
288
+ )
289
+
290
+ #undef cv_hal_cmp8u
291
+ #define cv_hal_cmp8u(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::u8, src1, sz1, src2, sz2, dst, sz, w, h, op)
292
+ #undef cv_hal_cmp8s
293
+ #define cv_hal_cmp8s(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::s8, src1, sz1, src2, sz2, dst, sz, w, h, op)
294
+ #undef cv_hal_cmp16u
295
+ #define cv_hal_cmp16u(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::u16, src1, sz1, src2, sz2, dst, sz, w, h, op)
296
+ #undef cv_hal_cmp16s
297
+ #define cv_hal_cmp16s(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::s16, src1, sz1, src2, sz2, dst, sz, w, h, op)
298
+ #undef cv_hal_cmp32s
299
+ #define cv_hal_cmp32s(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::s32, src1, sz1, src2, sz2, dst, sz, w, h, op)
300
+ #undef cv_hal_cmp32f
301
+ #define cv_hal_cmp32f(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::f32, src1, sz1, src2, sz2, dst, sz, w, h, op)
302
+ //#undef cv_hal_cmp64f
303
+ //#define cv_hal_cmp64f(src1, sz1, src2, sz2, dst, sz, w, h, op) TEGRA_CMP(CAROTENE_NS::f64, src1, sz1, src2, sz2, dst, sz, w, h, op)
304
+
305
+ #define TegraBinaryOpScale_Invoker(name, func, scale_cnt, ...) TegraGenOp_Invoker(name, func, 2, 1, scale_cnt, \
306
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
307
+ RANGE_DATA(ST, src2_data, src2_step), src2_step, \
308
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step, __VA_ARGS__)
309
+
310
+ #define TEGRA_BINARYOPSCALE(type, op, src1, sz1, src2, sz2, dst, sz, w, h, scales) \
311
+ ( \
312
+ CAROTENE_NS::isSupportedConfiguration() ? \
313
+ parallel_for_(Range(0, h), \
314
+ TegraGenOp_##op##_Invoker<const type, type>(src1, sz1, src2, sz2, dst, sz, w, h, scales), \
315
+ (w * h) / static_cast<double>(1<<16)), \
316
+ CV_HAL_ERROR_OK \
317
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
318
+ )
319
+
320
+ TegraBinaryOpScale_Invoker(mul, mul, 1, scale, CAROTENE_NS::CONVERT_POLICY_SATURATE)
321
+
322
+ TegraBinaryOpScale_Invoker(mulf, mul, 1, scale)
323
+
324
+ TegraBinaryOpScale_Invoker(div, div, 1, scale, CAROTENE_NS::CONVERT_POLICY_SATURATE)
325
+
326
+ TegraBinaryOpScale_Invoker(divf, div, 1, scale)
327
+
328
+ #define TegraUnaryOpScale_Invoker(name, func, scale_cnt, ...) TegraGenOp_Invoker(name, func, 1, 1, scale_cnt, \
329
+ RANGE_DATA(ST, src1_data, src1_step), src1_step, \
330
+ RANGE_DATA(DT, dst1_data, dst1_step), dst1_step, __VA_ARGS__)
331
+
332
+ #define TEGRA_UNARYOPSCALE(type, op, src1, sz1, dst, sz, w, h, scales) \
333
+ ( \
334
+ CAROTENE_NS::isSupportedConfiguration() ? \
335
+ parallel_for_(Range(0, h), \
336
+ TegraGenOp_##op##_Invoker<const type, type>(src1, sz1, dst, sz, w, h, scales), \
337
+ (w * h) / static_cast<double>(1<<16)), \
338
+ CV_HAL_ERROR_OK \
339
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
340
+ )
341
+
342
+ TegraUnaryOpScale_Invoker(recip, reciprocal, 1, scale, CAROTENE_NS::CONVERT_POLICY_SATURATE)
343
+
344
+ TegraUnaryOpScale_Invoker(recipf, reciprocal, 1, scale)
345
+
346
+ #undef cv_hal_mul8u
347
+ #define cv_hal_mul8u(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::u8, mul, src1, sz1, src2, sz2, dst, sz, w, h, scales)
348
+ #undef cv_hal_mul8s
349
+ #define cv_hal_mul8s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s8, mul, src1, sz1, src2, sz2, dst, sz, w, h, scales)
350
+ #undef cv_hal_mul16u
351
+ #define cv_hal_mul16u(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::u16, mul, src1, sz1, src2, sz2, dst, sz, w, h, scales)
352
+ #undef cv_hal_mul16s
353
+ #define cv_hal_mul16s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s16, mul, src1, sz1, src2, sz2, dst, sz, w, h, scales)
354
+ #undef cv_hal_mul32s
355
+ #define cv_hal_mul32s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s32, mul, src1, sz1, src2, sz2, dst, sz, w, h, scales)
356
+ #undef cv_hal_mul32f
357
+ #define cv_hal_mul32f(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::f32, mulf, src1, sz1, src2, sz2, dst, sz, w, h, scales)
358
+ //#undef cv_hal_mul64f
359
+ //#define cv_hal_mul64f(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::f64, mulf, src1, sz1, src2, sz2, dst, sz, w, h, scales)
360
+ #undef cv_hal_div8u
361
+ #define cv_hal_div8u(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::u8, div, src1, sz1, src2, sz2, dst, sz, w, h, scales)
362
+ #undef cv_hal_div8s
363
+ #define cv_hal_div8s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s8, div, src1, sz1, src2, sz2, dst, sz, w, h, scales)
364
+ #undef cv_hal_div16u
365
+ #define cv_hal_div16u(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::u16, div, src1, sz1, src2, sz2, dst, sz, w, h, scales)
366
+ #undef cv_hal_div16s
367
+ #define cv_hal_div16s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s16, div, src1, sz1, src2, sz2, dst, sz, w, h, scales)
368
+ #undef cv_hal_div32s
369
+ #define cv_hal_div32s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s32, div, src1, sz1, src2, sz2, dst, sz, w, h, scales)
370
+ #undef cv_hal_div32f
371
+ #define cv_hal_div32f(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::f32, divf, src1, sz1, src2, sz2, dst, sz, w, h, scales)
372
+ //#undef cv_hal_div64f
373
+ //#define cv_hal_div64f(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::f64, divf, src1, sz1, src2, sz2, dst, sz, w, h, scales)
374
+ #undef cv_hal_recip8u
375
+ #define cv_hal_recip8u(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::u8, recip, src1, sz1, dst, sz, w, h, scales)
376
+ #undef cv_hal_recip8s
377
+ #define cv_hal_recip8s(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::s8, recip, src1, sz1, dst, sz, w, h, scales)
378
+ #undef cv_hal_recip16u
379
+ #define cv_hal_recip16u(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::u16, recip, src1, sz1, dst, sz, w, h, scales)
380
+ #undef cv_hal_recip16s
381
+ #define cv_hal_recip16s(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::s16, recip, src1, sz1, dst, sz, w, h, scales)
382
+ #undef cv_hal_recip32s
383
+ #define cv_hal_recip32s(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::s32, recip, src1, sz1, dst, sz, w, h, scales)
384
+ #undef cv_hal_recip32f
385
+ #define cv_hal_recip32f(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::f32, recipf, src1, sz1, dst, sz, w, h, scales)
386
+ //#undef cv_hal_recip64f
387
+ //#define cv_hal_recip64f(src1, sz1, dst, sz, w, h, scales) TEGRA_UNARYOPSCALE(CAROTENE_NS::f64, recipf, src1, sz1, dst, sz, w, h, scales)
388
+
389
+ TegraBinaryOpScale_Invoker(addWeighted, addWeighted, 3, scales[0], scales[1], scales[2])
390
+
391
+ #undef cv_hal_addWeighted8u
392
+ #define cv_hal_addWeighted8u(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::u8, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
393
+ #undef cv_hal_addWeighted8s
394
+ #define cv_hal_addWeighted8s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s8, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
395
+ #undef cv_hal_addWeighted16u
396
+ #define cv_hal_addWeighted16u(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::u16, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
397
+ #undef cv_hal_addWeighted16s
398
+ #define cv_hal_addWeighted16s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s16, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
399
+ #undef cv_hal_addWeighted32s
400
+ #define cv_hal_addWeighted32s(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::s32, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
401
+ //#undef cv_hal_addWeighted32f
402
+ //#define cv_hal_addWeighted32f(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::f32, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
403
+ //#undef cv_hal_addWeighted64f
404
+ //#define cv_hal_addWeighted64f(src1, sz1, src2, sz2, dst, sz, w, h, scales) TEGRA_BINARYOPSCALE(CAROTENE_NS::f64, addWeighted, src1, sz1, src2, sz2, dst, sz, w, h, scales)
405
+
406
+ #else
407
+
408
+ #define TEGRA_ADD(src1, sz1, src2, sz2, dst, sz, w, h) \
409
+ ( \
410
+ CAROTENE_NS::isSupportedConfiguration() ? \
411
+ CAROTENE_NS::add(CAROTENE_NS::Size2D(w, h), \
412
+ src1, sz1, \
413
+ src2, sz2, \
414
+ dst, sz, \
415
+ CAROTENE_NS::CONVERT_POLICY_SATURATE), /*Original addition use saturated operator*/ \
416
+ /*so use the same from CAROTENE*/ \
417
+ CV_HAL_ERROR_OK \
418
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
419
+ )
420
+
421
+ #define TEGRA_ADDF(src1, sz1, src2, sz2, dst, sz, w, h) \
422
+ ( \
423
+ CAROTENE_NS::isSupportedConfiguration() ? \
424
+ CAROTENE_NS::add(CAROTENE_NS::Size2D(w, h), \
425
+ src1, sz1, \
426
+ src2, sz2, \
427
+ dst, sz), \
428
+ CV_HAL_ERROR_OK \
429
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
430
+ )
431
+
432
+ #define TEGRA_SUB(src1, sz1, src2, sz2, dst, sz, w, h) \
433
+ ( \
434
+ CAROTENE_NS::isSupportedConfiguration() ? \
435
+ CAROTENE_NS::sub(CAROTENE_NS::Size2D(w, h), \
436
+ src1, sz1, \
437
+ src2, sz2, \
438
+ dst, sz, \
439
+ CAROTENE_NS::CONVERT_POLICY_SATURATE), /*Original addition use saturated operator*/ \
440
+ /*so use the same from CAROTENE*/ \
441
+ CV_HAL_ERROR_OK \
442
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
443
+ )
444
+
445
+ #define TEGRA_SUBF(src1, sz1, src2, sz2, dst, sz, w, h) \
446
+ ( \
447
+ CAROTENE_NS::isSupportedConfiguration() ? \
448
+ CAROTENE_NS::sub(CAROTENE_NS::Size2D(w, h), \
449
+ src1, sz1, \
450
+ src2, sz2, \
451
+ dst, sz), \
452
+ CV_HAL_ERROR_OK \
453
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
454
+ )
455
+
456
+ #define TEGRA_MAX(src1, sz1, src2, sz2, dst, sz, w, h) \
457
+ ( \
458
+ CAROTENE_NS::isSupportedConfiguration() ? \
459
+ CAROTENE_NS::max(CAROTENE_NS::Size2D(w, h), \
460
+ src1, sz1, \
461
+ src2, sz2, \
462
+ dst, sz), \
463
+ CV_HAL_ERROR_OK \
464
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
465
+ )
466
+
467
+ #define TEGRA_MIN(src1, sz1, src2, sz2, dst, sz, w, h) \
468
+ ( \
469
+ CAROTENE_NS::isSupportedConfiguration() ? \
470
+ CAROTENE_NS::min(CAROTENE_NS::Size2D(w, h), \
471
+ src1, sz1, \
472
+ src2, sz2, \
473
+ dst, sz), \
474
+ CV_HAL_ERROR_OK \
475
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
476
+ )
477
+
478
+ #define TEGRA_ABSDIFF(src1, sz1, src2, sz2, dst, sz, w, h) \
479
+ ( \
480
+ CAROTENE_NS::isSupportedConfiguration() ? \
481
+ CAROTENE_NS::absDiff(CAROTENE_NS::Size2D(w, h), \
482
+ src1, sz1, \
483
+ src2, sz2, \
484
+ dst, sz), \
485
+ CV_HAL_ERROR_OK \
486
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
487
+ )
488
+
489
+ #define TEGRA_AND(src1, sz1, src2, sz2, dst, sz, w, h) \
490
+ ( \
491
+ CAROTENE_NS::isSupportedConfiguration() ? \
492
+ CAROTENE_NS::bitwiseAnd(CAROTENE_NS::Size2D(w, h), \
493
+ src1, sz1, \
494
+ src2, sz2, \
495
+ dst, sz), \
496
+ CV_HAL_ERROR_OK \
497
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
498
+ )
499
+ #define TEGRA_OR(src1, sz1, src2, sz2, dst, sz, w, h) \
500
+ ( \
501
+ CAROTENE_NS::isSupportedConfiguration() ? \
502
+ CAROTENE_NS::bitwiseOr(CAROTENE_NS::Size2D(w, h), \
503
+ src1, sz1, \
504
+ src2, sz2, \
505
+ dst, sz), \
506
+ CV_HAL_ERROR_OK \
507
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
508
+ )
509
+
510
+ #define TEGRA_XOR(src1, sz1, src2, sz2, dst, sz, w, h) \
511
+ ( \
512
+ CAROTENE_NS::isSupportedConfiguration() ? \
513
+ CAROTENE_NS::bitwiseXor(CAROTENE_NS::Size2D(w, h), \
514
+ src1, sz1, \
515
+ src2, sz2, \
516
+ dst, sz), \
517
+ CV_HAL_ERROR_OK \
518
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
519
+ )
520
+
521
+ #define TEGRA_NOT(src1, sz1, dst, sz, w, h) \
522
+ ( \
523
+ CAROTENE_NS::isSupportedConfiguration() ? \
524
+ CAROTENE_NS::bitwiseNot(CAROTENE_NS::Size2D(w, h), \
525
+ src1, sz1, \
526
+ dst, sz), \
527
+ CV_HAL_ERROR_OK \
528
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
529
+ )
530
+
531
+ #undef cv_hal_add8u
532
+ #define cv_hal_add8u TEGRA_ADD
533
+ #undef cv_hal_add8s
534
+ #define cv_hal_add8s TEGRA_ADD
535
+ #undef cv_hal_add16u
536
+ #define cv_hal_add16u TEGRA_ADD
537
+ #undef cv_hal_add16s
538
+ #define cv_hal_add16s TEGRA_ADD
539
+ #undef cv_hal_add32s
540
+ #define cv_hal_add32s TEGRA_ADD
541
+ #undef cv_hal_add32f
542
+ #define cv_hal_add32f TEGRA_ADDF
543
+ //#undef cv_hal_add64f
544
+ //#define cv_hal_add64f TEGRA_ADDF
545
+ #undef cv_hal_sub8u
546
+ #define cv_hal_sub8u TEGRA_SUB
547
+ #undef cv_hal_sub8s
548
+ #define cv_hal_sub8s TEGRA_SUB
549
+ #undef cv_hal_sub16u
550
+ #define cv_hal_sub16u TEGRA_SUB
551
+ #undef cv_hal_sub16s
552
+ #define cv_hal_sub16s TEGRA_SUB
553
+ #undef cv_hal_sub32s
554
+ #define cv_hal_sub32s TEGRA_SUB
555
+ #undef cv_hal_sub32f
556
+ #define cv_hal_sub32f TEGRA_SUBF
557
+ //#undef cv_hal_sub64f
558
+ //#define cv_hal_sub64f TEGRA_SUBF
559
+ #undef cv_hal_max8u
560
+ #define cv_hal_max8u TEGRA_MAX
561
+ #undef cv_hal_max8s
562
+ #define cv_hal_max8s TEGRA_MAX
563
+ #undef cv_hal_max16u
564
+ #define cv_hal_max16u TEGRA_MAX
565
+ #undef cv_hal_max16s
566
+ #define cv_hal_max16s TEGRA_MAX
567
+ #undef cv_hal_max32s
568
+ #define cv_hal_max32s TEGRA_MAX
569
+ #undef cv_hal_max32f
570
+ #define cv_hal_max32f TEGRA_MAX
571
+ //#undef cv_hal_max64f
572
+ //#define cv_hal_max64f TEGRA_MAX
573
+ #undef cv_hal_min8u
574
+ #define cv_hal_min8u TEGRA_MIN
575
+ #undef cv_hal_min8s
576
+ #define cv_hal_min8s TEGRA_MIN
577
+ #undef cv_hal_min16u
578
+ #define cv_hal_min16u TEGRA_MIN
579
+ #undef cv_hal_min16s
580
+ #define cv_hal_min16s TEGRA_MIN
581
+ #undef cv_hal_min32s
582
+ #define cv_hal_min32s TEGRA_MIN
583
+ #undef cv_hal_min32f
584
+ #define cv_hal_min32f TEGRA_MIN
585
+ //#undef cv_hal_min64f
586
+ //#define cv_hal_min64f TEGRA_MIN
587
+ #undef cv_hal_absdiff8u
588
+ #define cv_hal_absdiff8u TEGRA_ABSDIFF
589
+ #undef cv_hal_absdiff8s
590
+ #define cv_hal_absdiff8s TEGRA_ABSDIFF
591
+ #undef cv_hal_absdiff16u
592
+ #define cv_hal_absdiff16u TEGRA_ABSDIFF
593
+ #undef cv_hal_absdiff16s
594
+ #define cv_hal_absdiff16s TEGRA_ABSDIFF
595
+ #undef cv_hal_absdiff32s
596
+ #define cv_hal_absdiff32s TEGRA_ABSDIFF
597
+ #undef cv_hal_absdiff32f
598
+ #define cv_hal_absdiff32f TEGRA_ABSDIFF
599
+ //#undef cv_hal_absdiff64f
600
+ //#define cv_hal_absdiff64f TEGRA_ABSDIFF
601
+ #undef cv_hal_and8u
602
+ #define cv_hal_and8u TEGRA_AND
603
+ #undef cv_hal_or8u
604
+ #define cv_hal_or8u TEGRA_OR
605
+ #undef cv_hal_xor8u
606
+ #define cv_hal_xor8u TEGRA_XOR
607
+ #undef cv_hal_not8u
608
+ #define cv_hal_not8u TEGRA_NOT
609
+
610
+ #define TEGRA_CMP(src1, sz1, src2, sz2, dst, sz, w, h, op) \
611
+ ( \
612
+ CAROTENE_NS::isSupportedConfiguration() ? \
613
+ ((op) == cv::CMP_EQ) ? \
614
+ CAROTENE_NS::cmpEQ(CAROTENE_NS::Size2D(w, h), \
615
+ src1, sz1, \
616
+ src2, sz2, \
617
+ dst, sz), \
618
+ CV_HAL_ERROR_OK : \
619
+ ((op) == cv::CMP_NE) ? \
620
+ CAROTENE_NS::cmpNE(CAROTENE_NS::Size2D(w, h), \
621
+ src1, sz1, \
622
+ src2, sz2, \
623
+ dst, sz), \
624
+ CV_HAL_ERROR_OK : \
625
+ ((op) == cv::CMP_GT) ? \
626
+ CAROTENE_NS::cmpGT(CAROTENE_NS::Size2D(w, h), \
627
+ src1, sz1, \
628
+ src2, sz2, \
629
+ dst, sz), \
630
+ CV_HAL_ERROR_OK : \
631
+ ((op) == cv::CMP_GE) ? \
632
+ CAROTENE_NS::cmpGE(CAROTENE_NS::Size2D(w, h), \
633
+ src1, sz1, \
634
+ src2, sz2, \
635
+ dst, sz), \
636
+ CV_HAL_ERROR_OK : \
637
+ ((op) == cv::CMP_LT) ? \
638
+ CAROTENE_NS::cmpGT(CAROTENE_NS::Size2D(w, h), \
639
+ src2, sz2, \
640
+ src1, sz1, \
641
+ dst, sz), \
642
+ CV_HAL_ERROR_OK : \
643
+ ((op) == cv::CMP_LE) ? \
644
+ CAROTENE_NS::cmpGE(CAROTENE_NS::Size2D(w, h), \
645
+ src2, sz2, \
646
+ src1, sz1, \
647
+ dst, sz), \
648
+ CV_HAL_ERROR_OK : \
649
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
650
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
651
+ )
652
+
653
+ #undef cv_hal_cmp8u
654
+ #define cv_hal_cmp8u TEGRA_CMP
655
+ #undef cv_hal_cmp8s
656
+ #define cv_hal_cmp8s TEGRA_CMP
657
+ #undef cv_hal_cmp16u
658
+ #define cv_hal_cmp16u TEGRA_CMP
659
+ #undef cv_hal_cmp16s
660
+ #define cv_hal_cmp16s TEGRA_CMP
661
+ #undef cv_hal_cmp32s
662
+ #define cv_hal_cmp32s TEGRA_CMP
663
+ #undef cv_hal_cmp32f
664
+ #define cv_hal_cmp32f TEGRA_CMP
665
+ //#undef cv_hal_cmp64f
666
+ //#define cv_hal_cmp64f TEGRA_CMP
667
+
668
+ #define TEGRA_MUL(src1, sz1, src2, sz2, dst, sz, w, h, scale) \
669
+ ( \
670
+ CAROTENE_NS::isSupportedConfiguration() ? \
671
+ CAROTENE_NS::mul(CAROTENE_NS::Size2D(w, h), \
672
+ src1, sz1, \
673
+ src2, sz2, \
674
+ dst, sz, \
675
+ scale, \
676
+ CAROTENE_NS::CONVERT_POLICY_SATURATE), \
677
+ CV_HAL_ERROR_OK \
678
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
679
+ )
680
+
681
+ #define TEGRA_MULF(src1, sz1, src2, sz2, dst, sz, w, h, scale) \
682
+ ( \
683
+ CAROTENE_NS::isSupportedConfiguration() ? \
684
+ CAROTENE_NS::mul(CAROTENE_NS::Size2D(w, h), \
685
+ src1, sz1, \
686
+ src2, sz2, \
687
+ dst, sz, \
688
+ (float)scale), \
689
+ CV_HAL_ERROR_OK \
690
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
691
+ )
692
+
693
+ #define TEGRA_DIV(src1, sz1, src2, sz2, dst, sz, w, h, scale) \
694
+ ( \
695
+ CAROTENE_NS::isSupportedConfiguration() ? \
696
+ CAROTENE_NS::div(CAROTENE_NS::Size2D(w, h), \
697
+ src1, sz1, \
698
+ src2, sz2, \
699
+ dst, sz, \
700
+ scale, \
701
+ CAROTENE_NS::CONVERT_POLICY_SATURATE), \
702
+ CV_HAL_ERROR_OK \
703
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
704
+ )
705
+
706
+ #define TEGRA_DIVF(src1, sz1, src2, sz2, dst, sz, w, h, scale) \
707
+ ( \
708
+ CAROTENE_NS::isSupportedConfiguration() ? \
709
+ CAROTENE_NS::div(CAROTENE_NS::Size2D(w, h), \
710
+ src1, sz1, \
711
+ src2, sz2, \
712
+ dst, sz, \
713
+ (float)scale), \
714
+ CV_HAL_ERROR_OK \
715
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
716
+ )
717
+
718
+ #define TEGRA_RECIP(src2, sz2, dst, sz, w, h, scale) \
719
+ ( \
720
+ CAROTENE_NS::isSupportedConfiguration() ? \
721
+ CAROTENE_NS::reciprocal(CAROTENE_NS::Size2D(w, h), \
722
+ src2, sz2, \
723
+ dst, sz, \
724
+ scale, \
725
+ CAROTENE_NS::CONVERT_POLICY_SATURATE), \
726
+ CV_HAL_ERROR_OK \
727
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
728
+ )
729
+
730
+ #define TEGRA_RECIPF(src2, sz2, dst, sz, w, h, scale) \
731
+ ( \
732
+ CAROTENE_NS::isSupportedConfiguration() ? \
733
+ CAROTENE_NS::reciprocal(CAROTENE_NS::Size2D(w, h), \
734
+ src2, sz2, \
735
+ dst, sz, \
736
+ (float)scale), \
737
+ CV_HAL_ERROR_OK \
738
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
739
+ )
740
+
741
+ #undef cv_hal_mul8u
742
+ #define cv_hal_mul8u TEGRA_MUL
743
+ #undef cv_hal_mul8s
744
+ #define cv_hal_mul8s TEGRA_MUL
745
+ #undef cv_hal_mul16u
746
+ #define cv_hal_mul16u TEGRA_MUL
747
+ #undef cv_hal_mul16s
748
+ #define cv_hal_mul16s TEGRA_MUL
749
+ #undef cv_hal_mul32s
750
+ #define cv_hal_mul32s TEGRA_MUL
751
+ #undef cv_hal_mul32f
752
+ #define cv_hal_mul32f TEGRA_MULF
753
+ //#undef cv_hal_mul64f
754
+ //#define cv_hal_mul64f TEGRA_MULF
755
+ #undef cv_hal_div8u
756
+ #define cv_hal_div8u TEGRA_DIV
757
+ #undef cv_hal_div8s
758
+ #define cv_hal_div8s TEGRA_DIV
759
+ #undef cv_hal_div16u
760
+ #define cv_hal_div16u TEGRA_DIV
761
+ #undef cv_hal_div16s
762
+ #define cv_hal_div16s TEGRA_DIV
763
+ #undef cv_hal_div32s
764
+ #define cv_hal_div32s TEGRA_DIV
765
+ #undef cv_hal_div32f
766
+ #define cv_hal_div32f TEGRA_DIVF
767
+ //#undef cv_hal_div64f
768
+ //#define cv_hal_div64f TEGRA_DIVF
769
+ #undef cv_hal_recip8u
770
+ #define cv_hal_recip8u TEGRA_RECIP
771
+ #undef cv_hal_recip8s
772
+ #define cv_hal_recip8s TEGRA_RECIP
773
+ #undef cv_hal_recip16u
774
+ #define cv_hal_recip16u TEGRA_RECIP
775
+ #undef cv_hal_recip16s
776
+ #define cv_hal_recip16s TEGRA_RECIP
777
+ #undef cv_hal_recip32s
778
+ #define cv_hal_recip32s TEGRA_RECIP
779
+ #undef cv_hal_recip32f
780
+ #define cv_hal_recip32f TEGRA_RECIPF
781
+ //#undef cv_hal_recip64f
782
+ //#define cv_hal_recip64f TEGRA_RECIPF
783
+
784
+ #define TEGRA_ADDWEIGHTED(src1, sz1, src2, sz2, dst, sz, w, h, scales) \
785
+ ( \
786
+ CAROTENE_NS::isSupportedConfiguration() ? \
787
+ CAROTENE_NS::addWeighted(CAROTENE_NS::Size2D(w, h), \
788
+ src1, sz1, \
789
+ src2, sz2, \
790
+ dst, sz, \
791
+ ((double *)scales)[0], ((double *)scales)[1], ((double *)scales)[2]), \
792
+ CV_HAL_ERROR_OK \
793
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
794
+ )
795
+
796
+ #undef cv_hal_addWeighted8u
797
+ #define cv_hal_addWeighted8u TEGRA_ADDWEIGHTED
798
+ #undef cv_hal_addWeighted8s
799
+ #define cv_hal_addWeighted8s TEGRA_ADDWEIGHTED
800
+ #undef cv_hal_addWeighted16u
801
+ #define cv_hal_addWeighted16u TEGRA_ADDWEIGHTED
802
+ #undef cv_hal_addWeighted16s
803
+ #define cv_hal_addWeighted16s TEGRA_ADDWEIGHTED
804
+ #undef cv_hal_addWeighted32s
805
+ #define cv_hal_addWeighted32s TEGRA_ADDWEIGHTED
806
+ //#undef cv_hal_addWeighted32f
807
+ //#define cv_hal_addWeighted32f TEGRA_ADDWEIGHTED
808
+ //#undef cv_hal_addWeighted64f
809
+ //#define cv_hal_addWeighted64f TEGRA_ADDWEIGHTED
810
+
811
+ #endif //PARALLEL_CORE
812
+
813
+ #define ROW_SRC_ARG1 const ST * src1_data_
814
+ #define ROW_SRC_STORE1 , src1_data(src1_data_)
815
+ #define ROW_SRC_VAR1 const ST * src1_data;
816
+ #define ROW_SRC_ARG2 ROW_SRC_ARG1 \
817
+ , const ST * src2_data_
818
+ #define ROW_SRC_STORE2 ROW_SRC_STORE1 \
819
+ , src2_data(src2_data_)
820
+ #define ROW_SRC_VAR2 ROW_SRC_VAR1 \
821
+ const ST * src2_data;
822
+ #define ROW_SRC_ARG3 ROW_SRC_ARG2 \
823
+ , const ST * src3_data_
824
+ #define ROW_SRC_STORE3 ROW_SRC_STORE2 \
825
+ , src3_data(src3_data_)
826
+ #define ROW_SRC_VAR3 ROW_SRC_VAR2 \
827
+ const ST * src3_data;
828
+ #define ROW_SRC_ARG4 ROW_SRC_ARG3 \
829
+ , const ST * src4_data_
830
+ #define ROW_SRC_STORE4 ROW_SRC_STORE3 \
831
+ , src4_data(src4_data_)
832
+ #define ROW_SRC_VAR4 ROW_SRC_VAR3 \
833
+ const ST * src4_data;
834
+
835
+ #define ROW_DST_ARG1 , DT * dst1_data_
836
+ #define ROW_DST_STORE1 , dst1_data(dst1_data_)
837
+ #define ROW_DST_VAR1 DT * dst1_data;
838
+ #define ROW_DST_ARG2 ROW_DST_ARG1 \
839
+ , DT * dst2_data_
840
+ #define ROW_DST_STORE2 ROW_DST_STORE1 \
841
+ , dst2_data(dst2_data_)
842
+ #define ROW_DST_VAR2 ROW_DST_VAR1 \
843
+ DT * dst2_data;
844
+ #define ROW_DST_ARG3 ROW_DST_ARG2 \
845
+ , DT * dst3_data_
846
+ #define ROW_DST_STORE3 ROW_DST_STORE2 \
847
+ , dst3_data(dst3_data_)
848
+ #define ROW_DST_VAR3 ROW_DST_VAR2 \
849
+ DT * dst3_data;
850
+ #define ROW_DST_ARG4 ROW_DST_ARG3 \
851
+ , DT * dst4_data_
852
+ #define ROW_DST_STORE4 ROW_DST_STORE3 \
853
+ , dst4_data(dst4_data_)
854
+ #define ROW_DST_VAR4 ROW_DST_VAR3 \
855
+ DT * dst4_data;
856
+
857
+ #define ROW_VAL_ARG0
858
+ #define ROW_VAL_STORE0
859
+ #define ROW_VAL_VAR0
860
+ #define ROW_VAL_ARG1 , double val_
861
+ #define ROW_VAL_STORE1 , val(val_)
862
+ #define ROW_VAL_VAR1 double val;
863
+
864
+ #define TegraRowOp_Invoker(name, func, src_cnt, dst_cnt, val_cnt, ...) \
865
+ template <typename ST, typename DT> \
866
+ class TegraRowOp_##name##_Invoker : public cv::ParallelLoopBody \
867
+ { \
868
+ public: \
869
+ TegraRowOp_##name##_Invoker(ROW_SRC_ARG##src_cnt \
870
+ ROW_DST_ARG##dst_cnt \
871
+ ROW_VAL_ARG##val_cnt) : \
872
+ cv::ParallelLoopBody() ROW_SRC_STORE##src_cnt \
873
+ ROW_DST_STORE##dst_cnt \
874
+ ROW_VAL_STORE##val_cnt {} \
875
+ virtual void operator()(const cv::Range& range) const \
876
+ { \
877
+ CAROTENE_NS::func(CAROTENE_NS::Size2D(range.end-range.start, 1), __VA_ARGS__); \
878
+ } \
879
+ private: \
880
+ ROW_SRC_VAR##src_cnt \
881
+ ROW_DST_VAR##dst_cnt \
882
+ ROW_VAL_VAR##val_cnt \
883
+ const TegraRowOp_##name##_Invoker& operator= (const TegraRowOp_##name##_Invoker&); \
884
+ };
885
+
886
+
887
+ #define TEGRA_SPLIT(src, dst, len, cn) \
888
+ ( \
889
+ CAROTENE_NS::isSupportedConfiguration() ? \
890
+ cn == 2 ? \
891
+ CAROTENE_NS::split2(CAROTENE_NS::Size2D(len, 1), \
892
+ src, len, \
893
+ dst[0], len, \
894
+ dst[1], len), \
895
+ CV_HAL_ERROR_OK : \
896
+ cn == 3 ? \
897
+ CAROTENE_NS::split3(CAROTENE_NS::Size2D(len, 1), \
898
+ src, len, \
899
+ dst[0], len, \
900
+ dst[1], len, \
901
+ dst[2], len), \
902
+ CV_HAL_ERROR_OK : \
903
+ cn == 4 ? \
904
+ CAROTENE_NS::split4(CAROTENE_NS::Size2D(len, 1), \
905
+ src, len, \
906
+ dst[0], len, \
907
+ dst[1], len, \
908
+ dst[2], len, \
909
+ dst[3], len), \
910
+ CV_HAL_ERROR_OK : \
911
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
912
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
913
+ )
914
+
915
+ TegraRowOp_Invoker(split2, split2, 1, 2, 0, RANGE_DATA(ST, src1_data, 2*sizeof(ST)), range.end-range.start,
916
+ RANGE_DATA(DT, dst1_data, sizeof(DT)), range.end-range.start,
917
+ RANGE_DATA(DT, dst2_data, sizeof(DT)), range.end-range.start)
918
+ TegraRowOp_Invoker(split3, split3, 1, 3, 0, RANGE_DATA(ST, src1_data, 3*sizeof(ST)), range.end-range.start,
919
+ RANGE_DATA(DT, dst1_data, sizeof(DT)), range.end-range.start,
920
+ RANGE_DATA(DT, dst2_data, sizeof(DT)), range.end-range.start,
921
+ RANGE_DATA(DT, dst3_data, sizeof(DT)), range.end-range.start)
922
+ TegraRowOp_Invoker(split4, split4, 1, 4, 0, RANGE_DATA(ST, src1_data, 4*sizeof(ST)), range.end-range.start,
923
+ RANGE_DATA(DT, dst1_data, sizeof(DT)), range.end-range.start,
924
+ RANGE_DATA(DT, dst2_data, sizeof(DT)), range.end-range.start,
925
+ RANGE_DATA(DT, dst3_data, sizeof(DT)), range.end-range.start,
926
+ RANGE_DATA(DT, dst4_data, sizeof(DT)), range.end-range.start)
927
+ #define TEGRA_SPLIT64S(type, src, dst, len, cn) \
928
+ ( \
929
+ CAROTENE_NS::isSupportedConfiguration() ? \
930
+ cn == 2 ? \
931
+ parallel_for_(Range(0, len), \
932
+ TegraRowOp_split2_Invoker<const type, type>(src, dst[0], dst[1]), \
933
+ (len) / static_cast<double>(1<<16)), \
934
+ CV_HAL_ERROR_OK : \
935
+ cn == 3 ? \
936
+ parallel_for_(Range(0, len), \
937
+ TegraRowOp_split3_Invoker<const type, type>(src, dst[0], dst[1], dst[2]), \
938
+ (len) / static_cast<double>(1<<16)), \
939
+ CV_HAL_ERROR_OK : \
940
+ cn == 4 ? \
941
+ parallel_for_(Range(0, len), \
942
+ TegraRowOp_split4_Invoker<const type, type>(src, dst[0], dst[1], dst[2], dst[3]), \
943
+ (len) / static_cast<double>(1<<16)), \
944
+ CV_HAL_ERROR_OK : \
945
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
946
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
947
+ )
948
+
949
+ #define TEGRA_MERGE(src, dst, len, cn) \
950
+ ( \
951
+ CAROTENE_NS::isSupportedConfiguration() ? \
952
+ cn == 2 ? \
953
+ CAROTENE_NS::combine2(CAROTENE_NS::Size2D(len, 1), \
954
+ src[0], len, \
955
+ src[1], len, \
956
+ dst, len), \
957
+ CV_HAL_ERROR_OK : \
958
+ cn == 3 ? \
959
+ CAROTENE_NS::combine3(CAROTENE_NS::Size2D(len, 1), \
960
+ src[0], len, \
961
+ src[1], len, \
962
+ src[2], len, \
963
+ dst, len), \
964
+ CV_HAL_ERROR_OK : \
965
+ cn == 4 ? \
966
+ CAROTENE_NS::combine4(CAROTENE_NS::Size2D(len, 1), \
967
+ src[0], len, \
968
+ src[1], len, \
969
+ src[2], len, \
970
+ src[3], len, \
971
+ dst, len), \
972
+ CV_HAL_ERROR_OK : \
973
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
974
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
975
+ )
976
+
977
+ TegraRowOp_Invoker(combine2, combine2, 2, 1, 0, RANGE_DATA(ST, src1_data, sizeof(ST)), range.end-range.start,
978
+ RANGE_DATA(ST, src2_data, sizeof(ST)), range.end-range.start,
979
+ RANGE_DATA(DT, dst1_data, 2*sizeof(DT)), range.end-range.start)
980
+ TegraRowOp_Invoker(combine3, combine3, 3, 1, 0, RANGE_DATA(ST, src1_data, sizeof(ST)), range.end-range.start,
981
+ RANGE_DATA(ST, src2_data, sizeof(ST)), range.end-range.start,
982
+ RANGE_DATA(ST, src3_data, sizeof(ST)), range.end-range.start,
983
+ RANGE_DATA(DT, dst1_data, 3*sizeof(DT)), range.end-range.start)
984
+ TegraRowOp_Invoker(combine4, combine4, 4, 1, 0, RANGE_DATA(ST, src1_data, sizeof(ST)), range.end-range.start,
985
+ RANGE_DATA(ST, src2_data, sizeof(ST)), range.end-range.start,
986
+ RANGE_DATA(ST, src3_data, sizeof(ST)), range.end-range.start,
987
+ RANGE_DATA(ST, src4_data, sizeof(ST)), range.end-range.start,
988
+ RANGE_DATA(DT, dst1_data, 4*sizeof(DT)), range.end-range.start)
989
+ #define TEGRA_MERGE64S(type, src, dst, len, cn) \
990
+ ( \
991
+ CAROTENE_NS::isSupportedConfiguration() ? \
992
+ cn == 2 ? \
993
+ parallel_for_(Range(0, len), \
994
+ TegraRowOp_combine2_Invoker<const type, type>(src[0], src[1], dst), \
995
+ (len) / static_cast<double>(1<<16)), \
996
+ CV_HAL_ERROR_OK : \
997
+ cn == 3 ? \
998
+ parallel_for_(Range(0, len), \
999
+ TegraRowOp_combine3_Invoker<const type, type>(src[0], src[1], src[2], dst), \
1000
+ (len) / static_cast<double>(1<<16)), \
1001
+ CV_HAL_ERROR_OK : \
1002
+ cn == 4 ? \
1003
+ parallel_for_(Range(0, len), \
1004
+ TegraRowOp_combine4_Invoker<const type, type>(src[0], src[1], src[2], src[3], dst), \
1005
+ (len) / static_cast<double>(1<<16)), \
1006
+ CV_HAL_ERROR_OK : \
1007
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1008
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1009
+ )
1010
+
1011
+ #undef cv_hal_split8u
1012
+ #define cv_hal_split8u TEGRA_SPLIT
1013
+ #undef cv_hal_split16u
1014
+ #define cv_hal_split16u TEGRA_SPLIT
1015
+ #undef cv_hal_split32s
1016
+ #define cv_hal_split32s TEGRA_SPLIT
1017
+ #undef cv_hal_split64s
1018
+ #define cv_hal_split64s(src, dst, len, cn) TEGRA_SPLIT64S(CAROTENE_NS::s64, src, dst, len, cn)
1019
+
1020
+ #undef cv_hal_merge8u
1021
+ #define cv_hal_merge8u TEGRA_MERGE
1022
+ #undef cv_hal_merge16u
1023
+ #define cv_hal_merge16u TEGRA_MERGE
1024
+ #undef cv_hal_merge32s
1025
+ #define cv_hal_merge32s TEGRA_MERGE
1026
+ #undef cv_hal_merge64s
1027
+ #define cv_hal_merge64s(src, dst, len, cn) TEGRA_MERGE64S(CAROTENE_NS::s64, src, dst, len, cn)
1028
+
1029
+
1030
+ TegraRowOp_Invoker(phase, phase, 2, 1, 1, RANGE_DATA(ST, src1_data, sizeof(CAROTENE_NS::f32)), range.end-range.start,
1031
+ RANGE_DATA(ST, src2_data, sizeof(CAROTENE_NS::f32)), range.end-range.start,
1032
+ RANGE_DATA(DT, dst1_data, sizeof(CAROTENE_NS::f32)), range.end-range.start, val)
1033
+ #define TEGRA_FASTATAN(y, x, dst, len, angleInDegrees) \
1034
+ ( \
1035
+ CAROTENE_NS::isSupportedConfiguration() ? \
1036
+ parallel_for_(Range(0, len), \
1037
+ TegraRowOp_phase_Invoker<const CAROTENE_NS::f32, CAROTENE_NS::f32>(x, y, dst, angleInDegrees ? 1.0f : M_PI/180), \
1038
+ (len) / static_cast<double>(1<<16)), \
1039
+ CV_HAL_ERROR_OK \
1040
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1041
+ )
1042
+
1043
+ #undef cv_hal_fastAtan32f
1044
+ #define cv_hal_fastAtan32f TEGRA_FASTATAN
1045
+
1046
+ TegraRowOp_Invoker(magnitude, magnitude, 2, 1, 0, RANGE_DATA(ST, src1_data, sizeof(CAROTENE_NS::f32)), range.end-range.start,
1047
+ RANGE_DATA(ST, src2_data, sizeof(CAROTENE_NS::f32)), range.end-range.start,
1048
+ RANGE_DATA(DT, dst1_data, sizeof(CAROTENE_NS::f32)), range.end-range.start)
1049
+ #define TEGRA_MAGNITUDE(x, y, dst, len) \
1050
+ ( \
1051
+ CAROTENE_NS::isSupportedConfiguration() ? \
1052
+ parallel_for_(Range(0, len), \
1053
+ TegraRowOp_magnitude_Invoker<const CAROTENE_NS::f32, CAROTENE_NS::f32>(x, y, dst), \
1054
+ (len) / static_cast<double>(1<<16)), \
1055
+ CV_HAL_ERROR_OK \
1056
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1057
+ )
1058
+
1059
+ #undef cv_hal_magnitude32f
1060
+ #define cv_hal_magnitude32f TEGRA_MAGNITUDE
1061
+
1062
+
1063
+ #if defined OPENCV_IMGPROC_HAL_INTERFACE_H
1064
+
1065
+ struct cvhalFilter2D;
1066
+
1067
+ struct FilterCtx
1068
+ {
1069
+ CAROTENE_NS::Size2D ksize;
1070
+ CAROTENE_NS::s16* kernel_data;
1071
+ CAROTENE_NS::BORDER_MODE border;
1072
+ };
1073
+ inline int TEGRA_FILTERINIT(cvhalFilter2D **context, uchar *kernel_data, size_t kernel_step, int kernel_type, int kernel_width, int kernel_height,
1074
+ int max_width, int max_height, int src_type, int dst_type, int borderType, double delta, int anchor_x, int anchor_y, bool allowSubmatrix, bool allowInplace)
1075
+ {
1076
+ if(!context || !kernel_data || allowSubmatrix || allowInplace ||
1077
+ src_type != CV_8UC1 || dst_type != CV_8UC1 ||
1078
+ delta != 0 || anchor_x != kernel_width / 2 || anchor_y != kernel_height / 2 )
1079
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1080
+
1081
+ FilterCtx* ctx = new FilterCtx;
1082
+ if(!ctx)
1083
+ return CV_HAL_ERROR_UNKNOWN;
1084
+ ctx->ksize.width = kernel_width;
1085
+ ctx->ksize.height = kernel_height;
1086
+ switch(borderType)
1087
+ {
1088
+ case CV_HAL_BORDER_CONSTANT:
1089
+ ctx->border = CAROTENE_NS::BORDER_MODE_CONSTANT;
1090
+ break;
1091
+ case CV_HAL_BORDER_REPLICATE:
1092
+ ctx->border = CAROTENE_NS::BORDER_MODE_REPLICATE;
1093
+ break;
1094
+ case CV_HAL_BORDER_REFLECT:
1095
+ ctx->border = CAROTENE_NS::BORDER_MODE_REFLECT;
1096
+ break;
1097
+ case CV_HAL_BORDER_WRAP:
1098
+ ctx->border = CAROTENE_NS::BORDER_MODE_WRAP;
1099
+ break;
1100
+ case CV_HAL_BORDER_REFLECT_101:
1101
+ ctx->border = CAROTENE_NS::BORDER_MODE_REFLECT101;
1102
+ break;
1103
+ default:
1104
+ delete ctx;
1105
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1106
+ }
1107
+
1108
+ if(!CAROTENE_NS::isConvolutionSupported(CAROTENE_NS::Size2D(max_width, max_height), ctx->ksize, ctx->border))
1109
+ {
1110
+ delete ctx;
1111
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1112
+ }
1113
+
1114
+ ctx->kernel_data = new CAROTENE_NS::s16[kernel_width*kernel_height];
1115
+ if(!ctx->kernel_data)
1116
+ return CV_HAL_ERROR_UNKNOWN;
1117
+ switch(kernel_type)
1118
+ {
1119
+ case CV_8UC1:
1120
+ convert(ctx->ksize, (CAROTENE_NS::u8*)kernel_data, kernel_step, ctx->kernel_data, kernel_width);
1121
+ break;
1122
+ case CV_8SC1:
1123
+ convert(ctx->ksize, (CAROTENE_NS::s8*)kernel_data, kernel_step, ctx->kernel_data, kernel_width);
1124
+ break;
1125
+ case CV_16UC1:
1126
+ for(int j = 0; j < kernel_height; ++j)
1127
+ {
1128
+ std::memcpy(ctx->kernel_data + kernel_width * j, kernel_data + kernel_step * j, kernel_width * sizeof(int16_t));
1129
+ }
1130
+ break;
1131
+ default:
1132
+ delete[] ctx->kernel_data;
1133
+ delete ctx;
1134
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1135
+ }
1136
+
1137
+ *context = (cvhalFilter2D*)(ctx);
1138
+ return CV_HAL_ERROR_OK;
1139
+ }
1140
+ inline int TEGRA_FILTERFREE(cvhalFilter2D *context)
1141
+ {
1142
+ if(context)
1143
+ {
1144
+ if(((FilterCtx*)context)->kernel_data)
1145
+ delete[] ((FilterCtx*)context)->kernel_data;
1146
+ delete (FilterCtx*)context;
1147
+ return CV_HAL_ERROR_OK;
1148
+ }
1149
+ else
1150
+ {
1151
+ return CV_HAL_ERROR_UNKNOWN;
1152
+ }
1153
+ }
1154
+ #define TEGRA_FILTERIMPL(context, src_data, src_step, dst_data, dst_step, width, height, full_width, full_height, offset_x, offset_y) \
1155
+ ( \
1156
+ (void)full_width, (void)full_height, (void)offset_x, (void)offset_y, \
1157
+ context && CAROTENE_NS::isConvolutionSupported(CAROTENE_NS::Size2D(width, height), ((FilterCtx*)context)->ksize, ((FilterCtx*)context)->border) ? \
1158
+ CAROTENE_NS::convolution(CAROTENE_NS::Size2D(width, height), \
1159
+ src_data, src_step, \
1160
+ dst_data, dst_step, \
1161
+ ((FilterCtx*)context)->border, 0, \
1162
+ ((FilterCtx*)context)->ksize, ((FilterCtx*)context)->kernel_data, 1), \
1163
+ CV_HAL_ERROR_OK \
1164
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1165
+ )
1166
+
1167
+ #undef cv_hal_filterInit
1168
+ #define cv_hal_filterInit TEGRA_FILTERINIT
1169
+ #undef cv_hal_filter
1170
+ #define cv_hal_filter TEGRA_FILTERIMPL
1171
+ #undef cv_hal_filterFree
1172
+ #define cv_hal_filterFree TEGRA_FILTERFREE
1173
+
1174
+
1175
+ struct SepFilterCtx
1176
+ {
1177
+ int16_t kernelx_data[3];
1178
+ int16_t kernely_data[3];
1179
+ CAROTENE_NS::BORDER_MODE border;
1180
+ };
1181
+ inline int TEGRA_SEPFILTERINIT(cvhalFilter2D **context, int src_type, int dst_type, int kernel_type,
1182
+ uchar *kernelx_data, int kernelx_length,
1183
+ uchar *kernely_data, int kernely_length,
1184
+ int anchor_x, int anchor_y, double delta, int borderType)
1185
+ {
1186
+ if(!context || !kernelx_data || !kernely_data || src_type != CV_8UC1 || dst_type != CV_16SC1 ||
1187
+ kernelx_length != 3 || kernely_length != 3 ||
1188
+ delta != 0 || anchor_x != 1 || anchor_y != 1)
1189
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1190
+
1191
+ SepFilterCtx* ctx = new SepFilterCtx;
1192
+ if(!ctx)
1193
+ return CV_HAL_ERROR_UNKNOWN;
1194
+ switch(borderType)
1195
+ {
1196
+ case CV_HAL_BORDER_CONSTANT:
1197
+ ctx->border = CAROTENE_NS::BORDER_MODE_CONSTANT;
1198
+ break;
1199
+ case CV_HAL_BORDER_REPLICATE:
1200
+ ctx->border = CAROTENE_NS::BORDER_MODE_REPLICATE;
1201
+ break;
1202
+ case CV_HAL_BORDER_REFLECT:
1203
+ ctx->border = CAROTENE_NS::BORDER_MODE_REFLECT;
1204
+ break;
1205
+ case CV_HAL_BORDER_WRAP:
1206
+ ctx->border = CAROTENE_NS::BORDER_MODE_WRAP;
1207
+ break;
1208
+ case CV_HAL_BORDER_REFLECT_101:
1209
+ ctx->border = CAROTENE_NS::BORDER_MODE_REFLECT101;
1210
+ break;
1211
+ default:
1212
+ delete ctx;
1213
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1214
+ }
1215
+
1216
+ if(!CAROTENE_NS::isSeparableFilter3x3Supported(CAROTENE_NS::Size2D(16, 16), ctx->border, 3, 3))
1217
+ {
1218
+ delete ctx;
1219
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1220
+ }
1221
+
1222
+ switch(kernel_type)
1223
+ {
1224
+ case CV_8UC1:
1225
+ ctx->kernelx_data[0]=kernelx_data[0];
1226
+ ctx->kernelx_data[1]=kernelx_data[1];
1227
+ ctx->kernelx_data[2]=kernelx_data[2];
1228
+ ctx->kernely_data[0]=kernely_data[0];
1229
+ ctx->kernely_data[1]=kernely_data[1];
1230
+ ctx->kernely_data[2]=kernely_data[2];
1231
+ break;
1232
+ case CV_8SC1:
1233
+ ctx->kernelx_data[0]=((char*)kernelx_data)[0];
1234
+ ctx->kernelx_data[1]=((char*)kernelx_data)[1];
1235
+ ctx->kernelx_data[2]=((char*)kernelx_data)[2];
1236
+ ctx->kernely_data[0]=((char*)kernely_data)[0];
1237
+ ctx->kernely_data[1]=((char*)kernely_data)[1];
1238
+ ctx->kernely_data[2]=((char*)kernely_data)[2];
1239
+ break;
1240
+ case CV_16UC1:
1241
+ ctx->kernelx_data[0]=((int16_t*)kernelx_data)[0];
1242
+ ctx->kernelx_data[1]=((int16_t*)kernelx_data)[1];
1243
+ ctx->kernelx_data[2]=((int16_t*)kernelx_data)[2];
1244
+ ctx->kernely_data[0]=((int16_t*)kernely_data)[0];
1245
+ ctx->kernely_data[1]=((int16_t*)kernely_data)[1];
1246
+ ctx->kernely_data[2]=((int16_t*)kernely_data)[2];
1247
+ break;
1248
+ default:
1249
+ delete ctx;
1250
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1251
+ }
1252
+
1253
+ *context = (cvhalFilter2D*)(ctx);
1254
+ return CV_HAL_ERROR_OK;
1255
+ }
1256
+ inline int TEGRA_SEPFILTERFREE(cvhalFilter2D *context)
1257
+ {
1258
+ if(context)
1259
+ {
1260
+ delete (SepFilterCtx*)context;
1261
+ return CV_HAL_ERROR_OK;
1262
+ }
1263
+ else
1264
+ {
1265
+ return CV_HAL_ERROR_UNKNOWN;
1266
+ }
1267
+ }
1268
+ #define TEGRA_SEPFILTERIMPL(context, src_data, src_step, dst_data, dst_step, width, height, full_width, full_height, offset_x, offset_y) \
1269
+ ( \
1270
+ context && CAROTENE_NS::isSeparableFilter3x3Supported(CAROTENE_NS::Size2D(width, height), ((SepFilterCtx*)context)->border, 3, 3, \
1271
+ CAROTENE_NS::Margin(offset_x, full_width - width - offset_x, offset_y, full_height - height - offset_y)) ? \
1272
+ CAROTENE_NS::SeparableFilter3x3(CAROTENE_NS::Size2D(width, height), \
1273
+ src_data, src_step, \
1274
+ (CAROTENE_NS::s16*)dst_data, dst_step, \
1275
+ 3, 3, ((SepFilterCtx*)context)->kernelx_data, ((SepFilterCtx*)context)->kernely_data, \
1276
+ ((SepFilterCtx*)context)->border, 0, \
1277
+ CAROTENE_NS::Margin(offset_x, full_width - width - offset_x, offset_y, full_height - height - offset_y)), \
1278
+ CV_HAL_ERROR_OK \
1279
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1280
+ )
1281
+
1282
+ #undef cv_hal_sepFilterInit
1283
+ #define cv_hal_sepFilterInit TEGRA_SEPFILTERINIT
1284
+ #undef cv_hal_sepFilter
1285
+ #define cv_hal_sepFilter TEGRA_SEPFILTERIMPL
1286
+ #undef cv_hal_sepFilterFree
1287
+ #define cv_hal_sepFilterFree TEGRA_SEPFILTERFREE
1288
+
1289
+ struct MorphCtx
1290
+ {
1291
+ int operation;
1292
+ int channels;
1293
+ CAROTENE_NS::Size2D ksize;
1294
+ int anchor_x, anchor_y;
1295
+ CAROTENE_NS::BORDER_MODE border;
1296
+ uchar borderValues[4];
1297
+ };
1298
+ inline int TEGRA_MORPHINIT(cvhalFilter2D **context, int operation, int src_type, int dst_type, int width, int height,
1299
+ int kernel_type, uchar *kernel_data, size_t kernel_step, int kernel_width, int kernel_height, int anchor_x, int anchor_y,
1300
+ int borderType, const double borderValue[4], int iterations, bool allowSubmatrix, bool allowInplace)
1301
+ {
1302
+ if(!context || !kernel_data || src_type != dst_type ||
1303
+ CV_MAT_DEPTH(src_type) != CV_8U || src_type < 0 || (src_type >> CV_CN_SHIFT) > 3 ||
1304
+ width < kernel_width || height < kernel_height ||
1305
+ allowSubmatrix || allowInplace || iterations != 1 ||
1306
+ !CAROTENE_NS::isSupportedConfiguration())
1307
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1308
+
1309
+ switch(CV_MAT_DEPTH(kernel_type))
1310
+ {
1311
+ case CV_8U:
1312
+ if(CAROTENE_NS::countNonZero(CAROTENE_NS::Size2D(kernel_width, kernel_height), kernel_data, kernel_step) != kernel_width * kernel_height)
1313
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1314
+ break;
1315
+ case CV_16U:
1316
+ if(CAROTENE_NS::countNonZero(CAROTENE_NS::Size2D(kernel_width, kernel_height), (uint16_t*)kernel_data, kernel_step) != kernel_width * kernel_height)
1317
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1318
+ break;
1319
+ case CV_32S:
1320
+ if(CAROTENE_NS::countNonZero(CAROTENE_NS::Size2D(kernel_width, kernel_height), (int32_t*)kernel_data, kernel_step) != kernel_width * kernel_height)
1321
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1322
+ break;
1323
+ case CV_32F:
1324
+ if(CAROTENE_NS::countNonZero(CAROTENE_NS::Size2D(kernel_width, kernel_height), (float*)kernel_data, kernel_step) != kernel_width * kernel_height)
1325
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1326
+ break;
1327
+ case CV_64F:
1328
+ if(CAROTENE_NS::countNonZero(CAROTENE_NS::Size2D(kernel_width, kernel_height), (double*)kernel_data, kernel_step) != kernel_width * kernel_height)
1329
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1330
+ break;
1331
+ default:
1332
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1333
+ }
1334
+
1335
+ MorphCtx* ctx = new MorphCtx;
1336
+ if(!ctx)
1337
+ return CV_HAL_ERROR_UNKNOWN;
1338
+ ctx->channels = (src_type >> CV_CN_SHIFT) + 1;
1339
+ ctx->ksize.width = kernel_width;
1340
+ ctx->ksize.height = kernel_height;
1341
+ ctx->anchor_x = anchor_x;
1342
+ ctx->anchor_y = anchor_y;
1343
+ switch(operation)
1344
+ {
1345
+ case CV_HAL_MORPH_ERODE:
1346
+ case CV_HAL_MORPH_DILATE:
1347
+ ctx->operation = operation;
1348
+ break;
1349
+ default:
1350
+ delete ctx;
1351
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1352
+ }
1353
+ switch(borderType)
1354
+ {
1355
+ case CV_HAL_BORDER_CONSTANT:
1356
+ ctx->border = CAROTENE_NS::BORDER_MODE_CONSTANT;
1357
+ if( borderValue[0] == DBL_MAX && borderValue[1] == DBL_MAX && borderValue[2] == DBL_MAX && borderValue[3] == DBL_MAX )
1358
+ {
1359
+ if( operation == CV_HAL_MORPH_ERODE )
1360
+ for(int i = 0; i < ctx->channels; ++i)
1361
+ ctx->borderValues[i] = (CAROTENE_NS::u8)UCHAR_MAX;
1362
+ else
1363
+ for(int i = 0; i < ctx->channels; ++i)
1364
+ ctx->borderValues[i] = 0;
1365
+ }
1366
+ else
1367
+ {
1368
+ for(int i = 0; i < ctx->channels; ++i)
1369
+ ctx->borderValues[i] = (CAROTENE_NS::u8)cv::saturate_cast<uchar>(borderValue[i]);
1370
+ }
1371
+ break;
1372
+ case CV_HAL_BORDER_REPLICATE:
1373
+ ctx->border = CAROTENE_NS::BORDER_MODE_REPLICATE;
1374
+ break;
1375
+ case CV_HAL_BORDER_REFLECT:
1376
+ ctx->border = CAROTENE_NS::BORDER_MODE_REFLECT;
1377
+ break;
1378
+ case CV_HAL_BORDER_WRAP:
1379
+ ctx->border = CAROTENE_NS::BORDER_MODE_WRAP;
1380
+ break;
1381
+ case CV_HAL_BORDER_REFLECT_101:
1382
+ ctx->border = CAROTENE_NS::BORDER_MODE_REFLECT101;
1383
+ break;
1384
+ default:
1385
+ delete ctx;
1386
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1387
+ }
1388
+
1389
+ *context = (cvhalFilter2D*)(ctx);
1390
+ return CV_HAL_ERROR_OK;
1391
+ }
1392
+ inline int TEGRA_MORPHFREE(cvhalFilter2D *context)
1393
+ {
1394
+ if(context)
1395
+ {
1396
+ delete (MorphCtx*)context;
1397
+ return CV_HAL_ERROR_OK;
1398
+ }
1399
+ else
1400
+ {
1401
+ return CV_HAL_ERROR_UNKNOWN;
1402
+ }
1403
+ }
1404
+ #define TEGRA_MORPHIMPL(context, src_data, src_step, dst_data, dst_step, width, height, src_full_width, src_full_height, src_roi_x, src_roi_y, dst_full_width, dst_full_height, dst_roi_x, dst_roi_y) \
1405
+ ( \
1406
+ (void)dst_full_width, (void)dst_full_height, (void)dst_roi_x, (void)dst_roi_y, \
1407
+ context && CAROTENE_NS::isSupportedConfiguration() ? \
1408
+ ((MorphCtx*)context)->operation == CV_HAL_MORPH_ERODE ? \
1409
+ CAROTENE_NS::erode(CAROTENE_NS::Size2D(width, height), ((MorphCtx*)context)->channels, \
1410
+ src_data, src_step, dst_data, dst_step, \
1411
+ ((MorphCtx*)context)->ksize, ((MorphCtx*)context)->anchor_x, ((MorphCtx*)context)->anchor_y, \
1412
+ ((MorphCtx*)context)->border, ((MorphCtx*)context)->border, ((MorphCtx*)context)->borderValues, \
1413
+ CAROTENE_NS::Margin(src_roi_x, src_full_width - width - src_roi_x, src_roi_y, src_full_height - height - src_roi_y)), \
1414
+ CV_HAL_ERROR_OK : \
1415
+ ((MorphCtx*)context)->operation == CV_HAL_MORPH_DILATE ? \
1416
+ CAROTENE_NS::dilate(CAROTENE_NS::Size2D(width, height), ((MorphCtx*)context)->channels, \
1417
+ src_data, src_step, dst_data, dst_step, \
1418
+ ((MorphCtx*)context)->ksize, ((MorphCtx*)context)->anchor_x, ((MorphCtx*)context)->anchor_y, \
1419
+ ((MorphCtx*)context)->border, ((MorphCtx*)context)->border, ((MorphCtx*)context)->borderValues, \
1420
+ CAROTENE_NS::Margin(src_roi_x, src_full_width - width - src_roi_x, src_roi_y, src_full_height - height - src_roi_y)), \
1421
+ CV_HAL_ERROR_OK : \
1422
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1423
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1424
+ )
1425
+
1426
+ #undef cv_hal_morphInit
1427
+ #define cv_hal_morphInit TEGRA_MORPHINIT
1428
+ #undef cv_hal_morph
1429
+ #define cv_hal_morph TEGRA_MORPHIMPL
1430
+ #undef cv_hal_morphFree
1431
+ #define cv_hal_morphFree TEGRA_MORPHFREE
1432
+
1433
+
1434
+
1435
+ #define TEGRA_RESIZE(src_type, src_data, src_step, src_width, src_height, dst_data, dst_step, dst_width, dst_height, inv_scale_x, inv_scale_y, interpolation) \
1436
+ ( \
1437
+ interpolation == CV_HAL_INTER_LINEAR ? \
1438
+ CV_MAT_DEPTH(src_type) == CV_8U && CAROTENE_NS::isResizeLinearOpenCVSupported(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), ((src_type >> CV_CN_SHIFT) + 1)) && \
1439
+ inv_scale_x > 0 && inv_scale_y > 0 && \
1440
+ (dst_width - 0.5)/inv_scale_x - 0.5 < src_width && (dst_height - 0.5)/inv_scale_y - 0.5 < src_height && \
1441
+ (dst_width + 0.5)/inv_scale_x + 0.5 >= src_width && (dst_height + 0.5)/inv_scale_y + 0.5 >= src_height && \
1442
+ std::abs(dst_width / inv_scale_x - src_width) < 0.1 && std::abs(dst_height / inv_scale_y - src_height) < 0.1 ? \
1443
+ CAROTENE_NS::resizeLinearOpenCV(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1444
+ src_data, src_step, dst_data, dst_step, 1.0/inv_scale_x, 1.0/inv_scale_y, ((src_type >> CV_CN_SHIFT) + 1)), \
1445
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED : \
1446
+ interpolation == CV_HAL_INTER_AREA ? \
1447
+ CV_MAT_DEPTH(src_type) == CV_8U && CAROTENE_NS::isResizeAreaSupported(1.0/inv_scale_x, 1.0/inv_scale_y, ((src_type >> CV_CN_SHIFT) + 1)) && \
1448
+ std::abs(dst_width / inv_scale_x - src_width) < 0.1 && std::abs(dst_height / inv_scale_y - src_height) < 0.1 ? \
1449
+ CAROTENE_NS::resizeAreaOpenCV(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1450
+ src_data, src_step, dst_data, dst_step, 1.0/inv_scale_x, 1.0/inv_scale_y, ((src_type >> CV_CN_SHIFT) + 1)), \
1451
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED : \
1452
+ /*nearest neighbour interpolation disabled due to rounding accuracy issues*/ \
1453
+ /*interpolation == CV_HAL_INTER_NEAREST ? \
1454
+ (src_type == CV_8UC1 || src_type == CV_8SC1) && CAROTENE_NS::isResizeNearestNeighborSupported(CAROTENE_NS::Size2D(src_width, src_height), 1) ? \
1455
+ CAROTENE_NS::resizeNearestNeighbor(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1456
+ src_data, src_step, dst_data, dst_step, 1.0/inv_scale_x, 1.0/inv_scale_y, 1), \
1457
+ CV_HAL_ERROR_OK : \
1458
+ (src_type == CV_8UC3 || src_type == CV_8SC3) && CAROTENE_NS::isResizeNearestNeighborSupported(CAROTENE_NS::Size2D(src_width, src_height), 3) ? \
1459
+ CAROTENE_NS::resizeNearestNeighbor(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1460
+ src_data, src_step, dst_data, dst_step, 1.0/inv_scale_x, 1.0/inv_scale_y, 3), \
1461
+ CV_HAL_ERROR_OK : \
1462
+ (src_type == CV_8UC4 || src_type == CV_8SC4 || src_type == CV_16UC2 || src_type == CV_16SC2 || src_type == CV_32SC1) && \
1463
+ CAROTENE_NS::isResizeNearestNeighborSupported(CAROTENE_NS::Size2D(src_width, src_height), 4) ? \
1464
+ CAROTENE_NS::resizeNearestNeighbor(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1465
+ src_data, src_step, dst_data, dst_step, 1.0/inv_scale_x, 1.0/inv_scale_y, 4), \
1466
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED :*/ \
1467
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1468
+ )
1469
+
1470
+ #define TEGRA_WARPAFFINE(src_type, src_data, src_step, src_width, src_height, dst_data, dst_step, dst_width, dst_height, M, interpolation, borderType, borderValue) \
1471
+ ( \
1472
+ interpolation == CV_HAL_INTER_NEAREST ? \
1473
+ (src_type == CV_8UC1 || src_type == CV_8SC1) && (borderType == CV_HAL_BORDER_REPLICATE || borderType == CV_HAL_BORDER_CONSTANT) && \
1474
+ CAROTENE_NS::isWarpAffineNearestNeighborSupported(CAROTENE_NS::Size2D(src_width, src_height)) ? \
1475
+ CAROTENE_NS::warpAffineNearestNeighbor(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1476
+ src_data, src_step, \
1477
+ std::vector<float>(M+0,M+6).data(), \
1478
+ dst_data, dst_step, \
1479
+ borderType == CV_HAL_BORDER_REPLICATE ? CAROTENE_NS::BORDER_MODE_REPLICATE : CAROTENE_NS::BORDER_MODE_CONSTANT, \
1480
+ (CAROTENE_NS::u8)borderValue[0]), \
1481
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED : \
1482
+ interpolation == CV_HAL_INTER_LINEAR ? \
1483
+ (src_type == CV_8UC1 || src_type == CV_8SC1) && (borderType == CV_HAL_BORDER_REPLICATE || borderType == CV_HAL_BORDER_CONSTANT) && \
1484
+ CAROTENE_NS::isWarpAffineLinearSupported(CAROTENE_NS::Size2D(src_width, src_height)) ? \
1485
+ CAROTENE_NS::warpAffineLinear(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1486
+ src_data, src_step, \
1487
+ std::vector<float>(M+0,M+6).data(), \
1488
+ dst_data, dst_step, \
1489
+ borderType == CV_HAL_BORDER_REPLICATE ? CAROTENE_NS::BORDER_MODE_REPLICATE : CAROTENE_NS::BORDER_MODE_CONSTANT, \
1490
+ (CAROTENE_NS::u8)borderValue[0]), \
1491
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED : \
1492
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1493
+ )
1494
+
1495
+ #define TEGRA_WARPPERSPECTIVE(src_type, src_data, src_step, src_width, src_height, dst_data, dst_step, dst_width, dst_height, M, interpolation, borderType, borderValue) \
1496
+ ( \
1497
+ interpolation == CV_HAL_INTER_NEAREST ? \
1498
+ (src_type == CV_8UC1 || src_type == CV_8SC1) && (borderType == CV_HAL_BORDER_REPLICATE || borderType == CV_HAL_BORDER_CONSTANT) && \
1499
+ CAROTENE_NS::isWarpPerspectiveNearestNeighborSupported(CAROTENE_NS::Size2D(src_width, src_height)) ? \
1500
+ CAROTENE_NS::warpPerspectiveNearestNeighbor(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1501
+ src_data, src_step, \
1502
+ std::vector<float>(M+0,M+9).data(), \
1503
+ dst_data, dst_step, \
1504
+ borderType == CV_HAL_BORDER_REPLICATE ? CAROTENE_NS::BORDER_MODE_REPLICATE : CAROTENE_NS::BORDER_MODE_CONSTANT, \
1505
+ (CAROTENE_NS::u8)borderValue[0]), \
1506
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED : \
1507
+ interpolation == CV_HAL_INTER_LINEAR ? \
1508
+ (src_type == CV_8UC1 || src_type == CV_8SC1) && (borderType == CV_HAL_BORDER_REPLICATE || borderType == CV_HAL_BORDER_CONSTANT) && \
1509
+ CAROTENE_NS::isWarpPerspectiveLinearSupported(CAROTENE_NS::Size2D(src_width, src_height)) ? \
1510
+ CAROTENE_NS::warpPerspectiveLinear(CAROTENE_NS::Size2D(src_width, src_height), CAROTENE_NS::Size2D(dst_width, dst_height), \
1511
+ src_data, src_step, \
1512
+ std::vector<float>(M+0,M+9).data(), \
1513
+ dst_data, dst_step, \
1514
+ borderType == CV_HAL_BORDER_REPLICATE ? CAROTENE_NS::BORDER_MODE_REPLICATE : CAROTENE_NS::BORDER_MODE_CONSTANT, \
1515
+ (CAROTENE_NS::u8)borderValue[0]), \
1516
+ CV_HAL_ERROR_OK : CV_HAL_ERROR_NOT_IMPLEMENTED : \
1517
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1518
+ )
1519
+
1520
+ #undef cv_hal_resize
1521
+ #define cv_hal_resize TEGRA_RESIZE
1522
+ //warpAffine/warpPerspective disabled due to rounding accuracy issue
1523
+ //#undef cv_hal_warpAffine
1524
+ //#define cv_hal_warpAffine TEGRA_WARPAFFINE
1525
+ //#undef cv_hal_warpPerspective
1526
+ //#define cv_hal_warpPerspective TEGRA_WARPPERSPECTIVE
1527
+
1528
+
1529
+ #define TegraCvtColor_Invoker(name, func, ...) \
1530
+ class TegraCvtColor_##name##_Invoker : public cv::ParallelLoopBody \
1531
+ { \
1532
+ public: \
1533
+ TegraCvtColor_##name##_Invoker(const uchar * src_data_, size_t src_step_, uchar * dst_data_, size_t dst_step_, int width_, int height_) : \
1534
+ cv::ParallelLoopBody(), src_data(src_data_), src_step(src_step_), dst_data(dst_data_), dst_step(dst_step_), width(width_), height(height_) {} \
1535
+ virtual void operator()(const cv::Range& range) const CV_OVERRIDE \
1536
+ { \
1537
+ CAROTENE_NS::func(CAROTENE_NS::Size2D(width, range.end-range.start), __VA_ARGS__); \
1538
+ } \
1539
+ private: \
1540
+ const uchar * src_data; \
1541
+ size_t src_step; \
1542
+ uchar * dst_data; \
1543
+ size_t dst_step; \
1544
+ int width, height; \
1545
+ const TegraCvtColor_##name##_Invoker& operator= (const TegraCvtColor_##name##_Invoker&); \
1546
+ };
1547
+
1548
+ TegraCvtColor_Invoker(rgb2bgr, rgb2bgr, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1549
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1550
+ TegraCvtColor_Invoker(rgb2bgrx, rgb2bgrx, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1551
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1552
+ TegraCvtColor_Invoker(rgb2rgbx, rgb2rgbx, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1553
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1554
+ TegraCvtColor_Invoker(rgbx2bgr, rgbx2bgr, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1555
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1556
+ TegraCvtColor_Invoker(rgbx2rgb, rgbx2rgb, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1557
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1558
+ TegraCvtColor_Invoker(rgbx2bgrx, rgbx2bgrx, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1559
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1560
+ #define TEGRA_CVTBGRTOBGR(src_data, src_step, dst_data, dst_step, width, height, depth, scn, dcn, swapBlue) \
1561
+ ( \
1562
+ depth == CV_8U && CAROTENE_NS::isSupportedConfiguration() ? \
1563
+ scn == 3 ? \
1564
+ dcn == 3 ? \
1565
+ swapBlue ? \
1566
+ parallel_for_(Range(0, height), \
1567
+ TegraCvtColor_rgb2bgr_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1568
+ (width * height) / static_cast<double>(1<<16)), \
1569
+ CV_HAL_ERROR_OK : \
1570
+ CV_HAL_ERROR_NOT_IMPLEMENTED : \
1571
+ dcn == 4 ? \
1572
+ (swapBlue ? \
1573
+ parallel_for_(Range(0, height), \
1574
+ TegraCvtColor_rgb2bgrx_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1575
+ (width * height) / static_cast<double>(1<<16)) : \
1576
+ parallel_for_(Range(0, height), \
1577
+ TegraCvtColor_rgb2rgbx_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1578
+ (width * height) / static_cast<double>(1<<16)) ), \
1579
+ CV_HAL_ERROR_OK : \
1580
+ CV_HAL_ERROR_NOT_IMPLEMENTED : \
1581
+ scn == 4 ? \
1582
+ dcn == 3 ? \
1583
+ (swapBlue ? \
1584
+ parallel_for_(Range(0, height), \
1585
+ TegraCvtColor_rgbx2bgr_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1586
+ (width * height) / static_cast<double>(1<<16)) : \
1587
+ parallel_for_(Range(0, height), \
1588
+ TegraCvtColor_rgbx2rgb_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1589
+ (width * height) / static_cast<double>(1<<16)) ), \
1590
+ CV_HAL_ERROR_OK : \
1591
+ dcn == 4 ? \
1592
+ swapBlue ? \
1593
+ parallel_for_(Range(0, height), \
1594
+ TegraCvtColor_rgbx2bgrx_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1595
+ (width * height) / static_cast<double>(1<<16)), \
1596
+ CV_HAL_ERROR_OK : \
1597
+ CV_HAL_ERROR_NOT_IMPLEMENTED : \
1598
+ CV_HAL_ERROR_NOT_IMPLEMENTED : \
1599
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1600
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1601
+ )
1602
+
1603
+ TegraCvtColor_Invoker(rgb2bgr565, rgb2bgr565, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1604
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1605
+ TegraCvtColor_Invoker(rgb2rgb565, rgb2rgb565, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1606
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1607
+ TegraCvtColor_Invoker(rgbx2bgr565, rgbx2bgr565, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1608
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1609
+ TegraCvtColor_Invoker(rgbx2rgb565, rgbx2rgb565, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1610
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1611
+ #define TEGRA_CVTBGRTOBGR565(src_data, src_step, dst_data, dst_step, width, height, scn, swapBlue, greenBits) \
1612
+ ( \
1613
+ greenBits == 6 && CAROTENE_NS::isSupportedConfiguration() ? \
1614
+ scn == 3 ? \
1615
+ (swapBlue ? \
1616
+ parallel_for_(Range(0, height), \
1617
+ TegraCvtColor_rgb2bgr565_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1618
+ (width * height) / static_cast<double>(1<<16)) : \
1619
+ parallel_for_(Range(0, height), \
1620
+ TegraCvtColor_rgb2rgb565_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1621
+ (width * height) / static_cast<double>(1<<16)) ), \
1622
+ CV_HAL_ERROR_OK : \
1623
+ scn == 4 ? \
1624
+ (swapBlue ? \
1625
+ parallel_for_(Range(0, height), \
1626
+ TegraCvtColor_rgbx2bgr565_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1627
+ (width * height) / static_cast<double>(1<<16)) : \
1628
+ parallel_for_(Range(0, height), \
1629
+ TegraCvtColor_rgbx2rgb565_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1630
+ (width * height) / static_cast<double>(1<<16)) ), \
1631
+ CV_HAL_ERROR_OK : \
1632
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1633
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1634
+ )
1635
+
1636
+ TegraCvtColor_Invoker(rgb2gray, rgb2gray, CAROTENE_NS::COLOR_SPACE_BT601, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1637
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1638
+ TegraCvtColor_Invoker(bgr2gray, bgr2gray, CAROTENE_NS::COLOR_SPACE_BT601, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1639
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1640
+ TegraCvtColor_Invoker(rgbx2gray, rgbx2gray, CAROTENE_NS::COLOR_SPACE_BT601, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1641
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1642
+ TegraCvtColor_Invoker(bgrx2gray, bgrx2gray, CAROTENE_NS::COLOR_SPACE_BT601, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1643
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1644
+ #define TEGRA_CVTBGRTOGRAY(src_data, src_step, dst_data, dst_step, width, height, depth, scn, swapBlue) \
1645
+ ( \
1646
+ depth == CV_8U && CAROTENE_NS::isSupportedConfiguration() ? \
1647
+ scn == 3 ? \
1648
+ (swapBlue ? \
1649
+ parallel_for_(Range(0, height), \
1650
+ TegraCvtColor_rgb2gray_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1651
+ (width * height) / static_cast<double>(1<<16)) : \
1652
+ parallel_for_(Range(0, height), \
1653
+ TegraCvtColor_bgr2gray_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1654
+ (width * height) / static_cast<double>(1<<16)) ), \
1655
+ CV_HAL_ERROR_OK : \
1656
+ scn == 4 ? \
1657
+ (swapBlue ? \
1658
+ parallel_for_(Range(0, height), \
1659
+ TegraCvtColor_rgbx2gray_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1660
+ (width * height) / static_cast<double>(1<<16)) : \
1661
+ parallel_for_(Range(0, height), \
1662
+ TegraCvtColor_bgrx2gray_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1663
+ (width * height) / static_cast<double>(1<<16)) ), \
1664
+ CV_HAL_ERROR_OK : \
1665
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1666
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1667
+ )
1668
+
1669
+ TegraCvtColor_Invoker(gray2rgb, gray2rgb, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1670
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1671
+ TegraCvtColor_Invoker(gray2rgbx, gray2rgbx, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1672
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1673
+ #define TEGRA_CVTGRAYTOBGR(src_data, src_step, dst_data, dst_step, width, height, depth, dcn) \
1674
+ ( \
1675
+ depth == CV_8U && CAROTENE_NS::isSupportedConfiguration() ? \
1676
+ dcn == 3 ? \
1677
+ parallel_for_(Range(0, height), \
1678
+ TegraCvtColor_gray2rgb_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1679
+ (width * height) / static_cast<double>(1<<16)), \
1680
+ CV_HAL_ERROR_OK : \
1681
+ dcn == 4 ? \
1682
+ parallel_for_(Range(0, height), \
1683
+ TegraCvtColor_gray2rgbx_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1684
+ (width * height) / static_cast<double>(1<<16)), \
1685
+ CV_HAL_ERROR_OK : \
1686
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1687
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1688
+ )
1689
+
1690
+ TegraCvtColor_Invoker(rgb2ycrcb, rgb2ycrcb, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1691
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1692
+ TegraCvtColor_Invoker(bgr2ycrcb, bgr2ycrcb, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1693
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1694
+ TegraCvtColor_Invoker(rgbx2ycrcb, rgbx2ycrcb, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1695
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1696
+ TegraCvtColor_Invoker(bgrx2ycrcb, bgrx2ycrcb, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1697
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step)
1698
+ #define TEGRA_CVTBGRTOYUV(src_data, src_step, dst_data, dst_step, width, height, depth, scn, swapBlue, isCbCr) \
1699
+ ( \
1700
+ isCbCr && depth == CV_8U && CAROTENE_NS::isSupportedConfiguration() ? \
1701
+ scn == 3 ? \
1702
+ (swapBlue ? \
1703
+ parallel_for_(Range(0, height), \
1704
+ TegraCvtColor_rgb2ycrcb_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1705
+ (width * height) / static_cast<double>(1<<16)) : \
1706
+ parallel_for_(Range(0, height), \
1707
+ TegraCvtColor_bgr2ycrcb_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1708
+ (width * height) / static_cast<double>(1<<16)) ), \
1709
+ CV_HAL_ERROR_OK : \
1710
+ scn == 4 ? \
1711
+ (swapBlue ? \
1712
+ parallel_for_(Range(0, height), \
1713
+ TegraCvtColor_rgbx2ycrcb_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1714
+ (width * height) / static_cast<double>(1<<16)) : \
1715
+ parallel_for_(Range(0, height), \
1716
+ TegraCvtColor_bgrx2ycrcb_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1717
+ (width * height) / static_cast<double>(1<<16)) ), \
1718
+ CV_HAL_ERROR_OK : \
1719
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1720
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1721
+ )
1722
+
1723
+ TegraCvtColor_Invoker(rgb2hsv, rgb2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1724
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 180)
1725
+ TegraCvtColor_Invoker(bgr2hsv, bgr2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1726
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 180)
1727
+ TegraCvtColor_Invoker(rgbx2hsv, rgbx2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1728
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 180)
1729
+ TegraCvtColor_Invoker(bgrx2hsv, bgrx2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1730
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 180)
1731
+ TegraCvtColor_Invoker(rgb2hsvf, rgb2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1732
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 256)
1733
+ TegraCvtColor_Invoker(bgr2hsvf, bgr2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1734
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 256)
1735
+ TegraCvtColor_Invoker(rgbx2hsvf, rgbx2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1736
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 256)
1737
+ TegraCvtColor_Invoker(bgrx2hsvf, bgrx2hsv, src_data + static_cast<size_t>(range.start) * src_step, src_step, \
1738
+ dst_data + static_cast<size_t>(range.start) * dst_step, dst_step, 256)
1739
+ #define TEGRA_CVTBGRTOHSV(src_data, src_step, dst_data, dst_step, width, height, depth, scn, swapBlue, isFullRange, isHSV) \
1740
+ ( \
1741
+ isHSV && depth == CV_8U && CAROTENE_NS::isSupportedConfiguration() ? \
1742
+ scn == 3 ? \
1743
+ (swapBlue ? \
1744
+ isFullRange ? \
1745
+ parallel_for_(Range(0, height), \
1746
+ TegraCvtColor_rgb2hsvf_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1747
+ (width * height) / static_cast<double>(1<<16)) : \
1748
+ parallel_for_(Range(0, height), \
1749
+ TegraCvtColor_rgb2hsv_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1750
+ (width * height) / static_cast<double>(1<<16)) : \
1751
+ isFullRange ? \
1752
+ parallel_for_(Range(0, height), \
1753
+ TegraCvtColor_bgr2hsvf_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1754
+ (width * height) / static_cast<double>(1<<16)) : \
1755
+ parallel_for_(Range(0, height), \
1756
+ TegraCvtColor_bgr2hsv_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1757
+ (width * height) / static_cast<double>(1<<16)) ), \
1758
+ CV_HAL_ERROR_OK : \
1759
+ scn == 4 ? \
1760
+ (swapBlue ? \
1761
+ isFullRange ? \
1762
+ parallel_for_(Range(0, height), \
1763
+ TegraCvtColor_rgbx2hsvf_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1764
+ (width * height) / static_cast<double>(1<<16)) : \
1765
+ parallel_for_(Range(0, height), \
1766
+ TegraCvtColor_rgbx2hsv_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1767
+ (width * height) / static_cast<double>(1<<16)) : \
1768
+ isFullRange ? \
1769
+ parallel_for_(Range(0, height), \
1770
+ TegraCvtColor_bgrx2hsvf_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1771
+ (width * height) / static_cast<double>(1<<16)) : \
1772
+ parallel_for_(Range(0, height), \
1773
+ TegraCvtColor_bgrx2hsv_Invoker(src_data, src_step, dst_data, dst_step, width, height), \
1774
+ (width * height) / static_cast<double>(1<<16)) ), \
1775
+ CV_HAL_ERROR_OK : \
1776
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1777
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1778
+ )
1779
+
1780
+ #define TEGRA_CVT2PYUVTOBGR_EX(y_data, y_step, uv_data, uv_step, dst_data, dst_step, dst_width, dst_height, dcn, swapBlue, uIdx) \
1781
+ ( \
1782
+ CAROTENE_NS::isSupportedConfiguration() ? \
1783
+ dcn == 3 ? \
1784
+ uIdx == 0 ? \
1785
+ (swapBlue ? \
1786
+ CAROTENE_NS::yuv420i2rgb(CAROTENE_NS::Size2D(dst_width, dst_height), \
1787
+ y_data, y_step, \
1788
+ uv_data, uv_step, \
1789
+ dst_data, dst_step) : \
1790
+ CAROTENE_NS::yuv420i2bgr(CAROTENE_NS::Size2D(dst_width, dst_height), \
1791
+ y_data, y_step, \
1792
+ uv_data, uv_step, \
1793
+ dst_data, dst_step)), \
1794
+ CV_HAL_ERROR_OK : \
1795
+ uIdx == 1 ? \
1796
+ (swapBlue ? \
1797
+ CAROTENE_NS::yuv420sp2rgb(CAROTENE_NS::Size2D(dst_width, dst_height), \
1798
+ y_data, y_step, \
1799
+ uv_data, uv_step, \
1800
+ dst_data, dst_step) : \
1801
+ CAROTENE_NS::yuv420sp2bgr(CAROTENE_NS::Size2D(dst_width, dst_height), \
1802
+ y_data, y_step, \
1803
+ uv_data, uv_step, \
1804
+ dst_data, dst_step)), \
1805
+ CV_HAL_ERROR_OK : \
1806
+ CV_HAL_ERROR_NOT_IMPLEMENTED : \
1807
+ dcn == 4 ? \
1808
+ uIdx == 0 ? \
1809
+ (swapBlue ? \
1810
+ CAROTENE_NS::yuv420i2rgbx(CAROTENE_NS::Size2D(dst_width, dst_height), \
1811
+ y_data, y_step, \
1812
+ uv_data, uv_step, \
1813
+ dst_data, dst_step) : \
1814
+ CAROTENE_NS::yuv420i2bgrx(CAROTENE_NS::Size2D(dst_width, dst_height), \
1815
+ y_data, y_step, \
1816
+ uv_data, uv_step, \
1817
+ dst_data, dst_step)), \
1818
+ CV_HAL_ERROR_OK : \
1819
+ uIdx == 1 ? \
1820
+ (swapBlue ? \
1821
+ CAROTENE_NS::yuv420sp2rgbx(CAROTENE_NS::Size2D(dst_width, dst_height), \
1822
+ y_data, y_step, \
1823
+ uv_data, uv_step, \
1824
+ dst_data, dst_step) : \
1825
+ CAROTENE_NS::yuv420sp2bgrx(CAROTENE_NS::Size2D(dst_width, dst_height), \
1826
+ y_data, y_step, \
1827
+ uv_data, uv_step, \
1828
+ dst_data, dst_step)), \
1829
+ CV_HAL_ERROR_OK : \
1830
+ CV_HAL_ERROR_NOT_IMPLEMENTED : \
1831
+ CV_HAL_ERROR_NOT_IMPLEMENTED \
1832
+ : CV_HAL_ERROR_NOT_IMPLEMENTED \
1833
+ )
1834
+ #define TEGRA_CVT2PYUVTOBGR(src_data, src_step, dst_data, dst_step, dst_width, dst_height, dcn, swapBlue, uIdx) \
1835
+ TEGRA_CVT2PYUVTOBGR_EX(src_data, src_step, src_data + src_step * dst_height, src_step, dst_data, dst_step, \
1836
+ dst_width, dst_height, dcn, swapBlue, uIdx);
1837
+
1838
+ #undef cv_hal_cvtBGRtoBGR
1839
+ #define cv_hal_cvtBGRtoBGR TEGRA_CVTBGRTOBGR
1840
+ #undef cv_hal_cvtBGRtoBGR5x5
1841
+ #define cv_hal_cvtBGRtoBGR5x5 TEGRA_CVTBGRTOBGR565
1842
+ #undef cv_hal_cvtBGRtoGray
1843
+ #define cv_hal_cvtBGRtoGray TEGRA_CVTBGRTOGRAY
1844
+ #undef cv_hal_cvtGraytoBGR
1845
+ #define cv_hal_cvtGraytoBGR TEGRA_CVTGRAYTOBGR
1846
+ #if 0 // bit-exact tests are failed
1847
+ #undef cv_hal_cvtBGRtoYUV
1848
+ #define cv_hal_cvtBGRtoYUV TEGRA_CVTBGRTOYUV
1849
+ #endif
1850
+ #undef cv_hal_cvtBGRtoHSV
1851
+ #define cv_hal_cvtBGRtoHSV TEGRA_CVTBGRTOHSV
1852
+ #if 0 // bit-exact tests are failed
1853
+ #undef cv_hal_cvtTwoPlaneYUVtoBGR
1854
+ #define cv_hal_cvtTwoPlaneYUVtoBGR TEGRA_CVT2PYUVTOBGR
1855
+ #undef cv_hal_cvtTwoPlaneYUVtoBGREx
1856
+ #define cv_hal_cvtTwoPlaneYUVtoBGREx TEGRA_CVT2PYUVTOBGR_EX
1857
+ #endif
1858
+
1859
+ // The optimized branch was developed for old armv7 processors and leads to perf degradation on armv8
1860
+ #if defined(__ARM_ARCH) && (__ARM_ARCH == 7)
1861
+ inline CAROTENE_NS::BORDER_MODE borderCV2Carotene(int borderType)
1862
+ {
1863
+ switch(borderType)
1864
+ {
1865
+ case CV_HAL_BORDER_CONSTANT:
1866
+ return CAROTENE_NS::BORDER_MODE_CONSTANT;
1867
+ case CV_HAL_BORDER_REPLICATE:
1868
+ return CAROTENE_NS::BORDER_MODE_REPLICATE;
1869
+ case CV_HAL_BORDER_REFLECT:
1870
+ return CAROTENE_NS::BORDER_MODE_REFLECT;
1871
+ case CV_HAL_BORDER_WRAP:
1872
+ return CAROTENE_NS::BORDER_MODE_WRAP;
1873
+ case CV_HAL_BORDER_REFLECT_101:
1874
+ return CAROTENE_NS::BORDER_MODE_REFLECT101;
1875
+ }
1876
+
1877
+ return CAROTENE_NS::BORDER_MODE_UNDEFINED;
1878
+ }
1879
+
1880
+ inline int TEGRA_GaussianBlurBinomial(const uchar* src_data, size_t src_step, uchar* dst_data, size_t dst_step,
1881
+ int width, int height, int depth, int cn, size_t margin_left, size_t margin_top,
1882
+ size_t margin_right, size_t margin_bottom, size_t ksize, int border_type)
1883
+ {
1884
+ CAROTENE_NS::Size2D sz(width, height);
1885
+ CAROTENE_NS::BORDER_MODE border = borderCV2Carotene(border_type);
1886
+ CAROTENE_NS::Margin mg(margin_left, margin_right, margin_top, margin_bottom);
1887
+
1888
+ if (ksize == 3)
1889
+ {
1890
+ if ((depth != CV_8U) || (cn != 1))
1891
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1892
+
1893
+ if (CAROTENE_NS::isGaussianBlur3x3MarginSupported(sz, border, mg))
1894
+ {
1895
+ CAROTENE_NS::gaussianBlur3x3Margin(sz, src_data, src_step, dst_data, dst_step,
1896
+ border, 0, mg);
1897
+ return CV_HAL_ERROR_OK;
1898
+ }
1899
+ }
1900
+ else if (ksize == 5)
1901
+ {
1902
+ if (!CAROTENE_NS::isGaussianBlur5x5Supported(sz, cn, border))
1903
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1904
+
1905
+ if (depth == CV_8U)
1906
+ {
1907
+ CAROTENE_NS::gaussianBlur5x5(sz, cn, (uint8_t*)src_data, src_step,
1908
+ (uint8_t*)dst_data, dst_step, border, 0, mg);
1909
+ return CV_HAL_ERROR_OK;
1910
+ }
1911
+ else if (depth == CV_16U)
1912
+ {
1913
+ CAROTENE_NS::gaussianBlur5x5(sz, cn, (uint16_t*)src_data, src_step,
1914
+ (uint16_t*)dst_data, dst_step, border, 0, mg);
1915
+ return CV_HAL_ERROR_OK;
1916
+ }
1917
+ else if (depth == CV_16S)
1918
+ {
1919
+ CAROTENE_NS::gaussianBlur5x5(sz, cn, (int16_t*)src_data, src_step,
1920
+ (int16_t*)dst_data, dst_step, border, 0, mg);
1921
+ return CV_HAL_ERROR_OK;
1922
+ }
1923
+ }
1924
+
1925
+ return CV_HAL_ERROR_NOT_IMPLEMENTED;
1926
+ }
1927
+
1928
+ #undef cv_hal_gaussianBlurBinomial
1929
+ #define cv_hal_gaussianBlurBinomial TEGRA_GaussianBlurBinomial
1930
+
1931
+ #endif // __ARM_ARCH=7
1932
+
1933
+ #endif // OPENCV_IMGPROC_HAL_INTERFACE_H
1934
+
1935
+ #endif
3rdparty/carotene/include/carotene/definitions.hpp ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #ifndef CAROTENE_DEFINITIONS_HPP
41
+ #define CAROTENE_DEFINITIONS_HPP
42
+
43
+ #ifndef CAROTENE_NS
44
+ #define CAROTENE_NS carotene
45
+ #endif
46
+
47
+ #endif
3rdparty/carotene/include/carotene/functions.hpp ADDED
The diff for this file is too large to render. See raw diff
 
3rdparty/carotene/include/carotene/types.hpp ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #ifndef CAROTENE_TYPES_HPP
41
+ #define CAROTENE_TYPES_HPP
42
+
43
+ #include <carotene/definitions.hpp>
44
+ #include <stdint.h>
45
+ #include <cstddef>
46
+
47
+ #ifndef UINT32_MAX
48
+ #define UINT32_MAX (4294967295U)
49
+ #endif
50
+
51
+ namespace CAROTENE_NS {
52
+ using std::size_t;
53
+ using std::ptrdiff_t;
54
+
55
+ typedef int8_t s8;
56
+ typedef uint8_t u8;
57
+ typedef int16_t s16;
58
+ typedef uint16_t u16;
59
+ typedef int32_t s32;
60
+ typedef uint32_t u32;
61
+ typedef float f32;
62
+ typedef int64_t s64;
63
+ typedef uint64_t u64;
64
+ typedef double f64;
65
+
66
+ typedef ptrdiff_t stride_t;
67
+
68
+ enum CONVERT_POLICY
69
+ {
70
+ CONVERT_POLICY_WRAP,
71
+ CONVERT_POLICY_SATURATE
72
+ };
73
+
74
+ enum BORDER_MODE
75
+ {
76
+ BORDER_MODE_UNDEFINED,
77
+ BORDER_MODE_CONSTANT,
78
+ BORDER_MODE_REPLICATE,
79
+ BORDER_MODE_REFLECT,
80
+ BORDER_MODE_REFLECT101,
81
+ BORDER_MODE_WRAP
82
+ };
83
+
84
+ enum FLIP_MODE
85
+ {
86
+ FLIP_HORIZONTAL_MODE = 1,
87
+ FLIP_VERTICAL_MODE = 2,
88
+ FLIP_BOTH_MODE = FLIP_HORIZONTAL_MODE | FLIP_VERTICAL_MODE
89
+ };
90
+
91
+ enum COLOR_SPACE
92
+ {
93
+ COLOR_SPACE_BT601,
94
+ COLOR_SPACE_BT709
95
+ };
96
+
97
+ struct Size2D {
98
+ Size2D() : width(0), height(0) {}
99
+ Size2D(size_t width_, size_t height_) : width(width_), height(height_) {}
100
+
101
+ size_t width;
102
+ size_t height;
103
+
104
+ inline size_t total() const
105
+ {
106
+ return width * height;
107
+ }
108
+ };
109
+
110
+ struct Margin {
111
+ Margin() : left(0), right(0), top(0), bottom(0) {}
112
+ Margin(size_t left_, size_t right_, size_t top_, size_t bottom_)
113
+ : left(left_), right(right_), top(top_), bottom(bottom_) {}
114
+
115
+ // these are measured in elements
116
+ size_t left, right, top, bottom;
117
+ };
118
+
119
+ struct KeypointStore {
120
+ virtual void push(f32 kpX, f32 kpY, f32 kpSize, f32 kpAngle=-1, f32 kpResponse=0, s32 kpOctave=0, s32 kpClass_id=-1) = 0;
121
+ virtual ~KeypointStore() {};
122
+ };
123
+ }
124
+
125
+ #endif
3rdparty/carotene/src/absdiff.cpp ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include <algorithm>
41
+
42
+ #include "common.hpp"
43
+ #include "vtransform.hpp"
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ #ifdef CAROTENE_NEON
48
+
49
+ namespace {
50
+
51
+ template <typename T>
52
+ struct AbsDiff
53
+ {
54
+ typedef T type;
55
+
56
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0,
57
+ const typename internal::VecTraits<T>::vec128 & v_src1,
58
+ typename internal::VecTraits<T>::vec128 & v_dst) const
59
+ {
60
+ v_dst = internal::vabdq(v_src0, v_src1);
61
+ }
62
+
63
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0,
64
+ const typename internal::VecTraits<T>::vec64 & v_src1,
65
+ typename internal::VecTraits<T>::vec64 & v_dst) const
66
+ {
67
+ v_dst = internal::vabd(v_src0, v_src1);
68
+ }
69
+
70
+ void operator() (const T * src0, const T * src1, T * dst) const
71
+ {
72
+ dst[0] = src0[0] >= src1[0] ? src0[0] - src1[0] : src1[0] - src0[0];
73
+ }
74
+ };
75
+
76
+ template <typename T>
77
+ struct AbsDiffSigned
78
+ {
79
+ typedef T type;
80
+
81
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0,
82
+ const typename internal::VecTraits<T>::vec128 & v_src1,
83
+ typename internal::VecTraits<T>::vec128 & v_dst) const
84
+ {
85
+ typename internal::VecTraits<T>::vec128 v_min = internal::vminq(v_src0, v_src1);
86
+ typename internal::VecTraits<T>::vec128 v_max = internal::vmaxq(v_src0, v_src1);
87
+ v_dst = internal::vqsubq(v_max, v_min);
88
+ }
89
+
90
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0,
91
+ const typename internal::VecTraits<T>::vec64 & v_src1,
92
+ typename internal::VecTraits<T>::vec64 & v_dst) const
93
+ {
94
+ typename internal::VecTraits<T>::vec64 v_min = internal::vmin(v_src0, v_src1);
95
+ typename internal::VecTraits<T>::vec64 v_max = internal::vmax(v_src0, v_src1);
96
+ v_dst = internal::vqsub(v_max, v_min);
97
+ }
98
+
99
+ void operator() (const T * src0, const T * src1, T * dst) const
100
+ {
101
+ dst[0] = internal::saturate_cast<T>(src0[0] >= src1[0] ? (s64)src0[0] - src1[0] : (s64)src1[0] - src0[0]);
102
+ }
103
+ };
104
+
105
+ } // namespace
106
+
107
+ #endif
108
+
109
+ void absDiff(const Size2D &size,
110
+ const u8 *src0Base, ptrdiff_t src0Stride,
111
+ const u8 *src1Base, ptrdiff_t src1Stride,
112
+ u8 *dstBase, ptrdiff_t dstStride)
113
+ {
114
+ internal::assertSupportedConfiguration();
115
+ #ifdef CAROTENE_NEON
116
+ internal::vtransform(size,
117
+ src0Base, src0Stride,
118
+ src1Base, src1Stride,
119
+ dstBase, dstStride, AbsDiff<u8>());
120
+ #else
121
+ (void)size;
122
+ (void)src0Base;
123
+ (void)src0Stride;
124
+ (void)src1Base;
125
+ (void)src1Stride;
126
+ (void)dstBase;
127
+ (void)dstStride;
128
+ #endif
129
+ }
130
+
131
+ void absDiff(const Size2D &size,
132
+ const u16 *src0Base, ptrdiff_t src0Stride,
133
+ const u16 *src1Base, ptrdiff_t src1Stride,
134
+ u16 *dstBase, ptrdiff_t dstStride)
135
+ {
136
+ internal::assertSupportedConfiguration();
137
+ #ifdef CAROTENE_NEON
138
+ internal::vtransform(size,
139
+ src0Base, src0Stride,
140
+ src1Base, src1Stride,
141
+ dstBase, dstStride, AbsDiff<u16>());
142
+ #else
143
+ (void)size;
144
+ (void)src0Base;
145
+ (void)src0Stride;
146
+ (void)src1Base;
147
+ (void)src1Stride;
148
+ (void)dstBase;
149
+ (void)dstStride;
150
+ #endif
151
+ }
152
+
153
+ void absDiff(const Size2D &size,
154
+ const s8 *src0Base, ptrdiff_t src0Stride,
155
+ const s8 *src1Base, ptrdiff_t src1Stride,
156
+ s8 *dstBase, ptrdiff_t dstStride)
157
+ {
158
+ internal::assertSupportedConfiguration();
159
+ #ifdef CAROTENE_NEON
160
+ internal::vtransform(size,
161
+ src0Base, src0Stride,
162
+ src1Base, src1Stride,
163
+ dstBase, dstStride, AbsDiffSigned<s8>());
164
+ #else
165
+ (void)size;
166
+ (void)src0Base;
167
+ (void)src0Stride;
168
+ (void)src1Base;
169
+ (void)src1Stride;
170
+ (void)dstBase;
171
+ (void)dstStride;
172
+ #endif
173
+ }
174
+
175
+ void absDiff(const Size2D &size,
176
+ const s16 *src0Base, ptrdiff_t src0Stride,
177
+ const s16 *src1Base, ptrdiff_t src1Stride,
178
+ s16 *dstBase, ptrdiff_t dstStride)
179
+ {
180
+ internal::assertSupportedConfiguration();
181
+ #ifdef CAROTENE_NEON
182
+ internal::vtransform(size,
183
+ src0Base, src0Stride,
184
+ src1Base, src1Stride,
185
+ dstBase, dstStride, AbsDiffSigned<s16>());
186
+ #else
187
+ (void)size;
188
+ (void)src0Base;
189
+ (void)src0Stride;
190
+ (void)src1Base;
191
+ (void)src1Stride;
192
+ (void)dstBase;
193
+ (void)dstStride;
194
+ #endif
195
+ }
196
+
197
+ void absDiff(const Size2D &size,
198
+ const s32 *src0Base, ptrdiff_t src0Stride,
199
+ const s32 *src1Base, ptrdiff_t src1Stride,
200
+ s32 *dstBase, ptrdiff_t dstStride)
201
+ {
202
+ internal::assertSupportedConfiguration();
203
+ #ifdef CAROTENE_NEON
204
+ internal::vtransform(size,
205
+ src0Base, src0Stride,
206
+ src1Base, src1Stride,
207
+ dstBase, dstStride, AbsDiffSigned<s32>());
208
+ #else
209
+ (void)size;
210
+ (void)src0Base;
211
+ (void)src0Stride;
212
+ (void)src1Base;
213
+ (void)src1Stride;
214
+ (void)dstBase;
215
+ (void)dstStride;
216
+ #endif
217
+ }
218
+
219
+ void absDiff(const Size2D &size,
220
+ const f32 * src0Base, ptrdiff_t src0Stride,
221
+ const f32 * src1Base, ptrdiff_t src1Stride,
222
+ f32 * dstBase, ptrdiff_t dstStride)
223
+ {
224
+ internal::assertSupportedConfiguration();
225
+ #ifdef CAROTENE_NEON
226
+ internal::vtransform(size,
227
+ src0Base, src0Stride,
228
+ src1Base, src1Stride,
229
+ dstBase, dstStride, AbsDiff<f32>());
230
+ #else
231
+ (void)size;
232
+ (void)src0Base;
233
+ (void)src0Stride;
234
+ (void)src1Base;
235
+ (void)src1Stride;
236
+ (void)dstBase;
237
+ (void)dstStride;
238
+ #endif
239
+ }
240
+
241
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/accumulate.cpp ADDED
@@ -0,0 +1,408 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+
41
+ #include "common.hpp"
42
+ #include "vtransform.hpp"
43
+
44
+ #include <cstring>
45
+
46
+ namespace CAROTENE_NS {
47
+
48
+ void accumulate(const Size2D &size,
49
+ const u8 *srcBase, ptrdiff_t srcStride,
50
+ s16 *dstBase, ptrdiff_t dstStride)
51
+ {
52
+ internal::assertSupportedConfiguration();
53
+ #ifdef CAROTENE_NEON
54
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
55
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
56
+
57
+ for (size_t i = 0; i < size.height; ++i)
58
+ {
59
+ const u8* src = internal::getRowPtr(srcBase, srcStride, i);
60
+ s16* dst = internal::getRowPtr(dstBase, dstStride, i);
61
+ size_t j = 0;
62
+
63
+ for (; j < roiw16; j += 16)
64
+ {
65
+ internal::prefetch(src + j);
66
+ internal::prefetch(dst + j);
67
+ uint8x16_t v_src = vld1q_u8(src + j);
68
+ int16x8_t v_dst0 = vld1q_s16(dst + j);
69
+ int16x8_t v_dst1 = vld1q_s16(dst + j + 8);
70
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src)));
71
+ int16x8_t v_src1 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src)));
72
+ v_dst0 = vqaddq_s16(v_dst0, v_src0);
73
+ v_dst1 = vqaddq_s16(v_dst1, v_src1);
74
+ vst1q_s16(dst + j, v_dst0);
75
+ vst1q_s16(dst + j + 8, v_dst1);
76
+ }
77
+ for (; j < roiw8; j += 8)
78
+ {
79
+ uint8x8_t v_src = vld1_u8(src + j);
80
+ int16x8_t v_src16 = vreinterpretq_s16_u16(vmovl_u8(v_src));
81
+ int16x8_t v_dst = vld1q_s16(dst + j);
82
+ v_dst = vqaddq_s16(v_dst, v_src16);
83
+ vst1q_s16(dst + j, v_dst);
84
+ }
85
+
86
+ for (; j < size.width; j++)
87
+ dst[j] = internal::saturate_cast<s16>(src[j] + dst[j]);
88
+ }
89
+ #else
90
+ (void)size;
91
+ (void)srcBase;
92
+ (void)srcStride;
93
+ (void)dstBase;
94
+ (void)dstStride;
95
+ #endif
96
+ }
97
+
98
+ #ifdef CAROTENE_NEON
99
+
100
+ namespace {
101
+
102
+ template <int shift>
103
+ void accumulateSquareConst(const Size2D &size,
104
+ const u8 *srcBase, ptrdiff_t srcStride,
105
+ s16 *dstBase, ptrdiff_t dstStride)
106
+ {
107
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
108
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
109
+
110
+ for (size_t i = 0; i < size.height; ++i)
111
+ {
112
+ const u8* src = internal::getRowPtr(srcBase, srcStride, i);
113
+ s16* dst = internal::getRowPtr(dstBase, dstStride, i);
114
+ size_t j = 0;
115
+
116
+ for (; j < roiw16; j += 16)
117
+ {
118
+ internal::prefetch(src + j);
119
+ internal::prefetch(dst + j);
120
+ uint8x16_t v_src = vld1q_u8(src + j);
121
+ int16x8_t v_dst0 = vld1q_s16(dst + j), v_dst1 = vld1q_s16(dst + j + 8);
122
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src)));
123
+ int16x8_t v_src1 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src)));
124
+
125
+ int16x4_t v_srclo = vget_low_s16(v_src0), v_srchi = vget_high_s16(v_src0);
126
+ v_dst0 = vcombine_s16(vqmovn_s32(vaddw_s16(vshrq_n_s32(vmull_s16(v_srclo, v_srclo), shift), vget_low_s16(v_dst0))),
127
+ vqmovn_s32(vaddw_s16(vshrq_n_s32(vmull_s16(v_srchi, v_srchi), shift), vget_high_s16(v_dst0))));
128
+
129
+ v_srclo = vget_low_s16(v_src1);
130
+ v_srchi = vget_high_s16(v_src1);
131
+ v_dst1 = vcombine_s16(vqmovn_s32(vaddw_s16(vshrq_n_s32(vmull_s16(v_srclo, v_srclo), shift), vget_low_s16(v_dst1))),
132
+ vqmovn_s32(vaddw_s16(vshrq_n_s32(vmull_s16(v_srchi, v_srchi), shift), vget_high_s16(v_dst1))));
133
+
134
+ vst1q_s16(dst + j, v_dst0);
135
+ vst1q_s16(dst + j + 8, v_dst1);
136
+ }
137
+ for (; j < roiw8; j += 8)
138
+ {
139
+ int16x8_t v_src = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(src + j)));
140
+ int16x8_t v_dst = vld1q_s16(dst + j);
141
+ int16x4_t v_srclo = vget_low_s16(v_src), v_srchi = vget_high_s16(v_src);
142
+ v_dst = vcombine_s16(vqmovn_s32(vaddw_s16(vshrq_n_s32(vmull_s16(v_srclo, v_srclo), shift), vget_low_s16(v_dst))),
143
+ vqmovn_s32(vaddw_s16(vshrq_n_s32(vmull_s16(v_srchi, v_srchi), shift), vget_high_s16(v_dst))));
144
+ vst1q_s16(dst + j, v_dst);
145
+ }
146
+
147
+ for (; j < size.width; j++)
148
+ {
149
+ s32 srcVal = src[j];
150
+ dst[j] = internal::saturate_cast<s16>(dst[j] + ((srcVal * srcVal) >> shift));
151
+ }
152
+ }
153
+ }
154
+
155
+ template <>
156
+ void accumulateSquareConst<0>(const Size2D &size,
157
+ const u8 *srcBase, ptrdiff_t srcStride,
158
+ s16 *dstBase, ptrdiff_t dstStride)
159
+ {
160
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
161
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
162
+
163
+ for (size_t i = 0; i < size.height; ++i)
164
+ {
165
+ const u8* src = internal::getRowPtr(srcBase, srcStride, i);
166
+ s16* dst = internal::getRowPtr(dstBase, dstStride, i);
167
+ size_t j = 0;
168
+
169
+ for (; j < roiw16; j += 16)
170
+ {
171
+ internal::prefetch(src + j);
172
+ internal::prefetch(dst + j);
173
+ uint8x16_t v_src = vld1q_u8(src + j);
174
+ int16x8_t v_dst0 = vld1q_s16(dst + j), v_dst1 = vld1q_s16(dst + j + 8);
175
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src)));
176
+ int16x8_t v_src1 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src)));
177
+
178
+ int16x4_t v_srclo = vget_low_s16(v_src0), v_srchi = vget_high_s16(v_src0);
179
+ v_dst0 = vcombine_s16(vqmovn_s32(vaddw_s16(vmull_s16(v_srclo, v_srclo), vget_low_s16(v_dst0))),
180
+ vqmovn_s32(vaddw_s16(vmull_s16(v_srchi, v_srchi), vget_high_s16(v_dst0))));
181
+
182
+ v_srclo = vget_low_s16(v_src1);
183
+ v_srchi = vget_high_s16(v_src1);
184
+ v_dst1 = vcombine_s16(vqmovn_s32(vaddw_s16(vmull_s16(v_srclo, v_srclo), vget_low_s16(v_dst1))),
185
+ vqmovn_s32(vaddw_s16(vmull_s16(v_srchi, v_srchi), vget_high_s16(v_dst1))));
186
+
187
+ vst1q_s16(dst + j, v_dst0);
188
+ vst1q_s16(dst + j + 8, v_dst1);
189
+ }
190
+ for (; j < roiw8; j += 8)
191
+ {
192
+ int16x8_t v_src = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(src + j)));
193
+ int16x8_t v_dst = vld1q_s16(dst + j);
194
+ int16x4_t v_srclo = vget_low_s16(v_src), v_srchi = vget_high_s16(v_src);
195
+ v_dst = vcombine_s16(vqmovn_s32(vaddw_s16(vmull_s16(v_srclo, v_srclo), vget_low_s16(v_dst))),
196
+ vqmovn_s32(vaddw_s16(vmull_s16(v_srchi, v_srchi), vget_high_s16(v_dst))));
197
+ vst1q_s16(dst + j, v_dst);
198
+ }
199
+
200
+ for (; j < size.width; j++)
201
+ {
202
+ s32 srcVal = src[j];
203
+ dst[j] = internal::saturate_cast<s16>(dst[j] + srcVal * srcVal);
204
+ }
205
+ }
206
+ }
207
+
208
+ typedef void (* accumulateSquareConstFunc)(const Size2D &size,
209
+ const u8 *srcBase, ptrdiff_t srcStride,
210
+ s16 *dstBase, ptrdiff_t dstStride);
211
+
212
+ } // namespace
213
+
214
+ #endif
215
+
216
+ void accumulateSquare(const Size2D &size,
217
+ const u8 *srcBase, ptrdiff_t srcStride,
218
+ s16 *dstBase, ptrdiff_t dstStride,
219
+ u32 shift)
220
+ {
221
+ if (shift >= 16)
222
+ {
223
+ for (size_t i = 0; i < size.height; ++i)
224
+ {
225
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
226
+ std::memset(dst, 0, sizeof(s16) * size.width);
227
+ }
228
+ return;
229
+ }
230
+
231
+ internal::assertSupportedConfiguration();
232
+
233
+ #ifdef CAROTENE_NEON
234
+ // this ugly contruction is needed to avoid:
235
+ // /usr/lib/gcc/arm-linux-gnueabihf/4.8/include/arm_neon.h:3581:59: error: argument must be a constant
236
+ // return (int16x8_t)__builtin_neon_vshr_nv8hi (__a, __b, 1);
237
+
238
+ accumulateSquareConstFunc funcs[16] =
239
+ {
240
+ accumulateSquareConst<0>,
241
+ accumulateSquareConst<1>,
242
+ accumulateSquareConst<2>,
243
+ accumulateSquareConst<3>,
244
+ accumulateSquareConst<4>,
245
+ accumulateSquareConst<5>,
246
+ accumulateSquareConst<6>,
247
+ accumulateSquareConst<7>,
248
+ accumulateSquareConst<8>,
249
+ accumulateSquareConst<9>,
250
+ accumulateSquareConst<10>,
251
+ accumulateSquareConst<11>,
252
+ accumulateSquareConst<12>,
253
+ accumulateSquareConst<13>,
254
+ accumulateSquareConst<14>,
255
+ accumulateSquareConst<15>
256
+ }, func = funcs[shift];
257
+
258
+ func(size, srcBase, srcStride, dstBase, dstStride);
259
+ #else
260
+ (void)size;
261
+ (void)srcBase;
262
+ (void)srcStride;
263
+ (void)dstBase;
264
+ (void)dstStride;
265
+ (void)shift;
266
+ #endif
267
+ }
268
+
269
+ #ifdef CAROTENE_NEON
270
+
271
+ namespace {
272
+
273
+ struct AccumulateWeightedHalf
274
+ {
275
+ typedef u8 type;
276
+
277
+ void operator() (const uint8x16_t & v_src0, const uint8x16_t & v_src1,
278
+ uint8x16_t & v_dst) const
279
+ {
280
+ v_dst = vhaddq_u8(v_src0, v_src1);
281
+ }
282
+
283
+ void operator() (const uint8x8_t & v_src0, const uint8x8_t & v_src1,
284
+ uint8x8_t & v_dst) const
285
+ {
286
+ v_dst = vhadd_u8(v_src0, v_src1);
287
+ }
288
+
289
+ void operator() (const u8 * src0, const u8 * src1, u8 * dst) const
290
+ {
291
+ dst[0] = ((u16)(src0[0]) + src1[0]) >> 1;
292
+ }
293
+ };
294
+
295
+ struct AccumulateWeighted
296
+ {
297
+ typedef u8 type;
298
+
299
+ float alpha, beta;
300
+ float32x4_t v_alpha, v_beta;
301
+
302
+ explicit AccumulateWeighted(float _alpha) :
303
+ alpha(_alpha), beta(1 - _alpha)
304
+ {
305
+ v_alpha = vdupq_n_f32(alpha);
306
+ v_beta = vdupq_n_f32(beta);
307
+ }
308
+
309
+ void operator() (const uint8x16_t & v_src0, const uint8x16_t & v_src1,
310
+ uint8x16_t & v_dst) const
311
+ {
312
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
313
+ uint16x8_t v_src1_p = vmovl_u8(vget_low_u8(v_src1));
314
+ float32x4_t v_dst0f = vmlaq_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p))), v_beta),
315
+ v_alpha, vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))));
316
+ float32x4_t v_dst1f = vmlaq_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p))), v_beta),
317
+ v_alpha, vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))));
318
+ uint16x8_t v_dst0 = vcombine_u16(vmovn_u32(vcvtq_u32_f32(v_dst0f)),
319
+ vmovn_u32(vcvtq_u32_f32(v_dst1f)));
320
+
321
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
322
+ v_src1_p = vmovl_u8(vget_high_u8(v_src1));
323
+ v_dst0f = vmlaq_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p))), v_beta),
324
+ v_alpha, vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))));
325
+ v_dst1f = vmlaq_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p))), v_beta),
326
+ v_alpha, vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))));
327
+ uint16x8_t v_dst1 = vcombine_u16(vmovn_u32(vcvtq_u32_f32(v_dst0f)),
328
+ vmovn_u32(vcvtq_u32_f32(v_dst1f)));
329
+
330
+ v_dst = vcombine_u8(vmovn_u16(v_dst0), vmovn_u16(v_dst1));
331
+ }
332
+
333
+ void operator() (const uint8x8_t & _v_src0, const uint8x8_t & _v_src1,
334
+ uint8x8_t & v_dst) const
335
+ {
336
+ uint16x8_t v_src0 = vmovl_u8(_v_src0), v_src1 = vmovl_u8(_v_src1);
337
+
338
+ float32x4_t v_dst0f = vmlaq_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1))), v_beta),
339
+ v_alpha, vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))));
340
+ float32x4_t v_dst1f = vmlaq_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1))), v_beta),
341
+ v_alpha, vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))));
342
+ uint16x8_t _v_dst = vcombine_u16(vmovn_u32(vcvtq_u32_f32(v_dst0f)),
343
+ vmovn_u32(vcvtq_u32_f32(v_dst1f)));
344
+
345
+ v_dst = vmovn_u16(_v_dst);
346
+ }
347
+
348
+ void operator() (const u8 * src0, const u8 * src1, u8 * dst) const
349
+ {
350
+ dst[0] = beta * src1[0] + alpha * src0[0];
351
+ }
352
+ };
353
+
354
+ } // namespace
355
+
356
+ #endif
357
+
358
+ void accumulateWeighted(const Size2D &size,
359
+ const u8 *srcBase, ptrdiff_t srcStride,
360
+ u8 *dstBase, ptrdiff_t dstStride,
361
+ f32 alpha)
362
+ {
363
+ if (alpha == 0.0f)
364
+ return;
365
+ if (alpha == 1.0f)
366
+ {
367
+ for (size_t i = 0; i < size.height; ++i)
368
+ {
369
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
370
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
371
+ std::memcpy(dst, src, sizeof(u8) * size.width);
372
+ }
373
+ return;
374
+ }
375
+
376
+ internal::assertSupportedConfiguration();
377
+
378
+ #ifdef CAROTENE_NEON
379
+ // in this case we can use the following scheme:
380
+ // dst[p] = (src[p] + dst[p]) >> 1
381
+ // which is faster
382
+ if (alpha == 0.5f)
383
+ {
384
+ internal::vtransform(size,
385
+ srcBase, srcStride,
386
+ dstBase, dstStride,
387
+ dstBase, dstStride,
388
+ AccumulateWeightedHalf());
389
+
390
+ return;
391
+ }
392
+
393
+ internal::vtransform(size,
394
+ srcBase, srcStride,
395
+ dstBase, dstStride,
396
+ dstBase, dstStride,
397
+ AccumulateWeighted(alpha));
398
+ #else
399
+ (void)size;
400
+ (void)srcBase;
401
+ (void)srcStride;
402
+ (void)dstBase;
403
+ (void)dstStride;
404
+ (void)alpha;
405
+ #endif
406
+ }
407
+
408
+ } //namespace CAROTENE_NS
3rdparty/carotene/src/add.cpp ADDED
@@ -0,0 +1,475 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ namespace CAROTENE_NS {
44
+
45
+ #ifdef CAROTENE_NEON
46
+
47
+ namespace {
48
+
49
+ template <typename T, typename WT>
50
+ struct AddWrap
51
+ {
52
+ typedef T type;
53
+
54
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0,
55
+ const typename internal::VecTraits<T>::vec128 & v_src1,
56
+ typename internal::VecTraits<T>::vec128 & v_dst) const
57
+ {
58
+ v_dst = internal::vaddq(v_src0, v_src1);
59
+ }
60
+
61
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0,
62
+ const typename internal::VecTraits<T>::vec64 & v_src1,
63
+ typename internal::VecTraits<T>::vec64 & v_dst) const
64
+ {
65
+ v_dst = internal::vadd(v_src0, v_src1);
66
+ }
67
+
68
+ void operator() (const T * src0, const T * src1, T * dst) const
69
+ {
70
+ dst[0] = (T)((WT)src0[0] + (WT)src1[0]);
71
+ }
72
+ };
73
+
74
+ template <typename T, typename WT>
75
+ struct AddSaturate
76
+ {
77
+ typedef T type;
78
+
79
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0,
80
+ const typename internal::VecTraits<T>::vec128 & v_src1,
81
+ typename internal::VecTraits<T>::vec128 & v_dst) const
82
+ {
83
+ v_dst = internal::vqaddq(v_src0, v_src1);
84
+ }
85
+
86
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0,
87
+ const typename internal::VecTraits<T>::vec64 & v_src1,
88
+ typename internal::VecTraits<T>::vec64 & v_dst) const
89
+ {
90
+ v_dst = internal::vqadd(v_src0, v_src1);
91
+ }
92
+
93
+ void operator() (const T * src0, const T * src1, T * dst) const
94
+ {
95
+ dst[0] = internal::saturate_cast<T>((WT)src0[0] + (WT)src1[0]);
96
+ }
97
+ };
98
+
99
+ } // namespace
100
+
101
+ #endif
102
+
103
+ void add(const Size2D &size,
104
+ const u8 * src0Base, ptrdiff_t src0Stride,
105
+ const u8 * src1Base, ptrdiff_t src1Stride,
106
+ u8 *dstBase, ptrdiff_t dstStride,
107
+ CONVERT_POLICY policy)
108
+ {
109
+ internal::assertSupportedConfiguration();
110
+ #ifdef CAROTENE_NEON
111
+ if (policy == CONVERT_POLICY_SATURATE)
112
+ {
113
+ internal::vtransform(size,
114
+ src0Base, src0Stride,
115
+ src1Base, src1Stride,
116
+ dstBase, dstStride,
117
+ AddSaturate<u8, u16>());
118
+ }
119
+ else
120
+ {
121
+ internal::vtransform(size,
122
+ src0Base, src0Stride,
123
+ src1Base, src1Stride,
124
+ dstBase, dstStride,
125
+ AddWrap<u8, u16>());
126
+ }
127
+ #else
128
+ (void)size;
129
+ (void)src0Base;
130
+ (void)src0Stride;
131
+ (void)src1Base;
132
+ (void)src1Stride;
133
+ (void)dstBase;
134
+ (void)dstStride;
135
+ (void)policy;
136
+ #endif
137
+ }
138
+
139
+ void add(const Size2D &size,
140
+ const s8 * src0Base, ptrdiff_t src0Stride,
141
+ const s8 * src1Base, ptrdiff_t src1Stride,
142
+ s8 *dstBase, ptrdiff_t dstStride,
143
+ CONVERT_POLICY policy)
144
+ {
145
+ internal::assertSupportedConfiguration();
146
+ #ifdef CAROTENE_NEON
147
+ if (policy == CONVERT_POLICY_SATURATE)
148
+ {
149
+ internal::vtransform(size,
150
+ src0Base, src0Stride,
151
+ src1Base, src1Stride,
152
+ dstBase, dstStride,
153
+ AddSaturate<s8, s16>());
154
+ }
155
+ else
156
+ {
157
+ internal::vtransform(size,
158
+ src0Base, src0Stride,
159
+ src1Base, src1Stride,
160
+ dstBase, dstStride,
161
+ AddWrap<s8, s16>());
162
+ }
163
+ #else
164
+ (void)size;
165
+ (void)src0Base;
166
+ (void)src0Stride;
167
+ (void)src1Base;
168
+ (void)src1Stride;
169
+ (void)dstBase;
170
+ (void)dstStride;
171
+ (void)policy;
172
+ #endif
173
+ }
174
+
175
+ void add(const Size2D &size,
176
+ const u8 * src0Base, ptrdiff_t src0Stride,
177
+ const u8 * src1Base, ptrdiff_t src1Stride,
178
+ s16 *dstBase, ptrdiff_t dstStride,
179
+ CONVERT_POLICY)
180
+ {
181
+ internal::assertSupportedConfiguration();
182
+ #ifdef CAROTENE_NEON
183
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
184
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
185
+
186
+ for (size_t i = 0; i < size.height; ++i)
187
+ {
188
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
189
+ const u8 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
190
+ u16 * dst = internal::getRowPtr((u16 *)dstBase, dstStride, i);
191
+ size_t j = 0;
192
+
193
+ for (; j < roiw32; j += 32)
194
+ {
195
+ internal::prefetch(src0 + j);
196
+ internal::prefetch(src1 + j);
197
+ uint8x16_t v_src00 = vld1q_u8(src0 + j), v_src01 = vld1q_u8(src0 + j + 16);
198
+ uint8x16_t v_src10 = vld1q_u8(src1 + j), v_src11 = vld1q_u8(src1 + j + 16);
199
+ vst1q_u16(dst + j, vaddl_u8(vget_low_u8(v_src00), vget_low_u8(v_src10)));
200
+ vst1q_u16(dst + j + 8, vaddl_u8(vget_high_u8(v_src00), vget_high_u8(v_src10)));
201
+ vst1q_u16(dst + j + 16, vaddl_u8(vget_low_u8(v_src01), vget_low_u8(v_src11)));
202
+ vst1q_u16(dst + j + 24, vaddl_u8(vget_high_u8(v_src01), vget_high_u8(v_src11)));
203
+ }
204
+ for (; j < roiw8; j += 8)
205
+ {
206
+ uint8x8_t v_src0 = vld1_u8(src0 + j);
207
+ uint8x8_t v_src1 = vld1_u8(src1 + j);
208
+ vst1q_u16(dst + j, vaddl_u8(v_src0, v_src1));
209
+ }
210
+
211
+ for (; j < size.width; j++)
212
+ dst[j] = (u16)src0[j] + (u16)src1[j];
213
+ }
214
+ #else
215
+ (void)size;
216
+ (void)src0Base;
217
+ (void)src0Stride;
218
+ (void)src1Base;
219
+ (void)src1Stride;
220
+ (void)dstBase;
221
+ (void)dstStride;
222
+ #endif
223
+ }
224
+
225
+ void add(const Size2D &size,
226
+ const u8 * src0Base, ptrdiff_t src0Stride,
227
+ const s16 * src1Base, ptrdiff_t src1Stride,
228
+ s16 *dstBase, ptrdiff_t dstStride,
229
+ CONVERT_POLICY policy)
230
+ {
231
+ internal::assertSupportedConfiguration();
232
+ #ifdef CAROTENE_NEON
233
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
234
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
235
+
236
+ for (size_t i = 0; i < size.height; ++i)
237
+ {
238
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
239
+ const s16 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
240
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
241
+ size_t j = 0;
242
+
243
+ if (policy == CONVERT_POLICY_SATURATE)
244
+ {
245
+ for (; j < roiw16; j += 16)
246
+ {
247
+ internal::prefetch(src0 + j);
248
+ internal::prefetch(src1 + j);
249
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
250
+ int16x8_t v_src00 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src0)));
251
+ int16x8_t v_src01 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src0)));
252
+ int16x8_t v_src10 = vld1q_s16(src1 + j), v_src11 = vld1q_s16(src1 + j + 8);
253
+ int16x8_t v_dst0 = vqaddq_s16(v_src00, v_src10);
254
+ int16x8_t v_dst1 = vqaddq_s16(v_src01, v_src11);
255
+ vst1q_s16(dst + j, v_dst0);
256
+ vst1q_s16(dst + j + 8, v_dst1);
257
+ }
258
+ for (; j < roiw8; j += 8)
259
+ {
260
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(src0 + j)));
261
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
262
+ int16x8_t v_dst = vqaddq_s16(v_src0, v_src1);
263
+ vst1q_s16(dst + j, v_dst);
264
+ }
265
+
266
+ for (; j < size.width; j++)
267
+ dst[j] = internal::saturate_cast<s16>((s32)src0[j] + (s32)src1[j]);
268
+ }
269
+ else
270
+ {
271
+ for (; j < roiw16; j += 16)
272
+ {
273
+ internal::prefetch(src0 + j);
274
+ internal::prefetch(src1 + j);
275
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
276
+ int16x8_t v_src00 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src0)));
277
+ int16x8_t v_src01 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src0)));
278
+ int16x8_t v_src10 = vld1q_s16(src1 + j), v_src11 = vld1q_s16(src1 + j + 8);
279
+ int16x8_t v_dst0 = vaddq_s16(v_src00, v_src10);
280
+ int16x8_t v_dst1 = vaddq_s16(v_src01, v_src11);
281
+ vst1q_s16(dst + j, v_dst0);
282
+ vst1q_s16(dst + j + 8, v_dst1);
283
+ }
284
+ for (; j < roiw8; j += 8)
285
+ {
286
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(src0 + j)));
287
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
288
+ int16x8_t v_dst = vaddq_s16(v_src0, v_src1);
289
+ vst1q_s16(dst + j, v_dst);
290
+ }
291
+
292
+ for (; j < size.width; j++)
293
+ dst[j] = (s16)((s32)src0[j] + (s32)src1[j]);
294
+ }
295
+ }
296
+ #else
297
+ (void)size;
298
+ (void)src0Base;
299
+ (void)src0Stride;
300
+ (void)src1Base;
301
+ (void)src1Stride;
302
+ (void)dstBase;
303
+ (void)dstStride;
304
+ (void)policy;
305
+ #endif
306
+ }
307
+
308
+ void add(const Size2D &size,
309
+ const s16 * src0Base, ptrdiff_t src0Stride,
310
+ const s16 * src1Base, ptrdiff_t src1Stride,
311
+ s16 *dstBase, ptrdiff_t dstStride,
312
+ CONVERT_POLICY policy)
313
+ {
314
+ internal::assertSupportedConfiguration();
315
+ #ifdef CAROTENE_NEON
316
+ if (policy == CONVERT_POLICY_SATURATE)
317
+ {
318
+ internal::vtransform(size,
319
+ src0Base, src0Stride,
320
+ src1Base, src1Stride,
321
+ dstBase, dstStride,
322
+ AddSaturate<s16, s32>());
323
+ }
324
+ else
325
+ {
326
+ internal::vtransform(size,
327
+ src0Base, src0Stride,
328
+ src1Base, src1Stride,
329
+ dstBase, dstStride,
330
+ AddWrap<s16, s32>());
331
+ }
332
+ #else
333
+ (void)size;
334
+ (void)src0Base;
335
+ (void)src0Stride;
336
+ (void)src1Base;
337
+ (void)src1Stride;
338
+ (void)dstBase;
339
+ (void)dstStride;
340
+ (void)policy;
341
+ #endif
342
+ }
343
+
344
+ void add(const Size2D &size,
345
+ const u16 * src0Base, ptrdiff_t src0Stride,
346
+ const u16 * src1Base, ptrdiff_t src1Stride,
347
+ u16 * dstBase, ptrdiff_t dstStride,
348
+ CONVERT_POLICY policy)
349
+ {
350
+ internal::assertSupportedConfiguration();
351
+ #ifdef CAROTENE_NEON
352
+ if (policy == CONVERT_POLICY_SATURATE)
353
+ {
354
+ internal::vtransform(size,
355
+ src0Base, src0Stride,
356
+ src1Base, src1Stride,
357
+ dstBase, dstStride,
358
+ AddSaturate<u16, u32>());
359
+ }
360
+ else
361
+ {
362
+ internal::vtransform(size,
363
+ src0Base, src0Stride,
364
+ src1Base, src1Stride,
365
+ dstBase, dstStride,
366
+ AddWrap<u16, u32>());
367
+ }
368
+ #else
369
+ (void)size;
370
+ (void)src0Base;
371
+ (void)src0Stride;
372
+ (void)src1Base;
373
+ (void)src1Stride;
374
+ (void)dstBase;
375
+ (void)dstStride;
376
+ (void)policy;
377
+ #endif
378
+ }
379
+
380
+ void add(const Size2D &size,
381
+ const s32 * src0Base, ptrdiff_t src0Stride,
382
+ const s32 * src1Base, ptrdiff_t src1Stride,
383
+ s32 *dstBase, ptrdiff_t dstStride,
384
+ CONVERT_POLICY policy)
385
+ {
386
+ internal::assertSupportedConfiguration();
387
+ #ifdef CAROTENE_NEON
388
+ if (policy == CONVERT_POLICY_SATURATE)
389
+ {
390
+ internal::vtransform(size,
391
+ src0Base, src0Stride,
392
+ src1Base, src1Stride,
393
+ dstBase, dstStride,
394
+ AddSaturate<s32, s64>());
395
+ }
396
+ else
397
+ {
398
+ internal::vtransform(size,
399
+ src0Base, src0Stride,
400
+ src1Base, src1Stride,
401
+ dstBase, dstStride,
402
+ AddWrap<s32, s64>());
403
+ }
404
+ #else
405
+ (void)size;
406
+ (void)src0Base;
407
+ (void)src0Stride;
408
+ (void)src1Base;
409
+ (void)src1Stride;
410
+ (void)dstBase;
411
+ (void)dstStride;
412
+ (void)policy;
413
+ #endif
414
+ }
415
+
416
+ void add(const Size2D &size,
417
+ const u32 * src0Base, ptrdiff_t src0Stride,
418
+ const u32 * src1Base, ptrdiff_t src1Stride,
419
+ u32 * dstBase, ptrdiff_t dstStride,
420
+ CONVERT_POLICY policy)
421
+ {
422
+ internal::assertSupportedConfiguration();
423
+ #ifdef CAROTENE_NEON
424
+ if (policy == CONVERT_POLICY_SATURATE)
425
+ {
426
+ internal::vtransform(size,
427
+ src0Base, src0Stride,
428
+ src1Base, src1Stride,
429
+ dstBase, dstStride,
430
+ AddSaturate<u32, u64>());
431
+ }
432
+ else
433
+ {
434
+ internal::vtransform(size,
435
+ src0Base, src0Stride,
436
+ src1Base, src1Stride,
437
+ dstBase, dstStride,
438
+ AddWrap<u32, u64>());
439
+ }
440
+ #else
441
+ (void)size;
442
+ (void)src0Base;
443
+ (void)src0Stride;
444
+ (void)src1Base;
445
+ (void)src1Stride;
446
+ (void)dstBase;
447
+ (void)dstStride;
448
+ (void)policy;
449
+ #endif
450
+ }
451
+
452
+ void add(const Size2D &size,
453
+ const f32 * src0Base, ptrdiff_t src0Stride,
454
+ const f32 * src1Base, ptrdiff_t src1Stride,
455
+ f32 * dstBase, ptrdiff_t dstStride)
456
+ {
457
+ internal::assertSupportedConfiguration();
458
+ #ifdef CAROTENE_NEON
459
+ internal::vtransform(size,
460
+ src0Base, src0Stride,
461
+ src1Base, src1Stride,
462
+ dstBase, dstStride,
463
+ AddWrap<f32, f32>());
464
+ #else
465
+ (void)size;
466
+ (void)src0Base;
467
+ (void)src0Stride;
468
+ (void)src1Base;
469
+ (void)src1Stride;
470
+ (void)dstBase;
471
+ (void)dstStride;
472
+ #endif
473
+ }
474
+
475
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/add_weighted.cpp ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+ #include "vround_helper.hpp"
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ #ifdef CAROTENE_NEON
47
+
48
+ namespace {
49
+
50
+ using namespace internal;
51
+
52
+ template <typename T> struct TypeTraits;
53
+ template <> struct TypeTraits< u8> { typedef u16 wide; typedef u8 unsign; typedef uint8x16_t vec128; };
54
+ template <> struct TypeTraits< s8> { typedef s16 wide; typedef u8 unsign; typedef int8x16_t vec128; };
55
+ template <> struct TypeTraits<u16> { typedef u32 wide; typedef u8 narrow; typedef u16 unsign; typedef uint16x8_t vec128; };
56
+ template <> struct TypeTraits<s16> { typedef s32 wide; typedef s8 narrow; typedef u16 unsign; typedef int16x8_t vec128; };
57
+ template <> struct TypeTraits<u32> { typedef u64 wide; typedef u16 narrow; typedef u32 unsign; typedef uint32x4_t vec128; };
58
+ template <> struct TypeTraits<s32> { typedef s64 wide; typedef s16 narrow; typedef u32 unsign; typedef int32x4_t vec128; };
59
+ template <> struct TypeTraits<f32> { typedef f64 wide; typedef float32x4_t vec128; };
60
+
61
+ template <typename T> struct wAdd
62
+ {
63
+ typedef T type;
64
+
65
+ f32 alpha, beta, gamma;
66
+ typedef typename TypeTraits<T>::wide wtype;
67
+ wAdd<wtype> wideAdd;
68
+ wAdd(f32 _alpha, f32 _beta, f32 _gamma):
69
+ alpha(_alpha), beta(_beta), gamma(_gamma),
70
+ wideAdd(_alpha, _beta, _gamma) {}
71
+
72
+ void operator() (const typename VecTraits<T>::vec128 & v_src0,
73
+ const typename VecTraits<T>::vec128 & v_src1,
74
+ typename VecTraits<T>::vec128 & v_dst) const
75
+ {
76
+ typename VecTraits<wtype>::vec128 vrl, vrh;
77
+ wideAdd(vmovl( vget_low(v_src0)), vmovl( vget_low(v_src1)), vrl);
78
+ wideAdd(vmovl(vget_high(v_src0)), vmovl(vget_high(v_src1)), vrh);
79
+
80
+ v_dst = vcombine(vqmovn(vrl), vqmovn(vrh));
81
+ }
82
+
83
+ void operator() (const typename VecTraits<T>::vec64 & v_src0,
84
+ const typename VecTraits<T>::vec64 & v_src1,
85
+ typename VecTraits<T>::vec64 & v_dst) const
86
+ {
87
+ typename VecTraits<wtype>::vec128 vr;
88
+ wideAdd(vmovl(v_src0), vmovl(v_src1), vr);
89
+
90
+ v_dst = vqmovn(vr);
91
+ }
92
+
93
+ void operator() (const T * src0, const T * src1, T * dst) const
94
+ {
95
+ dst[0] = saturate_cast<T>(alpha*src0[0] + beta*src1[0] + gamma);
96
+ }
97
+ };
98
+
99
+ template <> struct wAdd<s32>
100
+ {
101
+ typedef s32 type;
102
+
103
+ f32 alpha, beta, gamma;
104
+ float32x4_t valpha, vbeta, vgamma;
105
+ wAdd(f32 _alpha, f32 _beta, f32 _gamma):
106
+ alpha(_alpha), beta(_beta), gamma(_gamma)
107
+ {
108
+ valpha = vdupq_n_f32(_alpha);
109
+ vbeta = vdupq_n_f32(_beta);
110
+ vgamma = vdupq_n_f32(_gamma);
111
+ }
112
+
113
+ void operator() (const VecTraits<s32>::vec128 & v_src0,
114
+ const VecTraits<s32>::vec128 & v_src1,
115
+ VecTraits<s32>::vec128 & v_dst) const
116
+ {
117
+ float32x4_t vs1 = vcvtq_f32_s32(v_src0);
118
+ float32x4_t vs2 = vcvtq_f32_s32(v_src1);
119
+
120
+ vs1 = vmlaq_f32(vgamma, vs1, valpha);
121
+ vs1 = vmlaq_f32(vs1, vs2, vbeta);
122
+ v_dst = vroundq_s32_f32(vs1);
123
+ }
124
+
125
+ void operator() (const VecTraits<s32>::vec64 & v_src0,
126
+ const VecTraits<s32>::vec64 & v_src1,
127
+ VecTraits<s32>::vec64 & v_dst) const
128
+ {
129
+ float32x2_t vs1 = vcvt_f32_s32(v_src0);
130
+ float32x2_t vs2 = vcvt_f32_s32(v_src1);
131
+
132
+ vs1 = vmla_f32(vget_low(vgamma), vs1, vget_low(valpha));
133
+ vs1 = vmla_f32(vs1, vs2, vget_low(vbeta));
134
+ v_dst = vround_s32_f32(vs1);
135
+ }
136
+
137
+ void operator() (const s32 * src0, const s32 * src1, s32 * dst) const
138
+ {
139
+ dst[0] = saturate_cast<s32>(alpha*src0[0] + beta*src1[0] + gamma);
140
+ }
141
+ };
142
+
143
+ template <> struct wAdd<u32>
144
+ {
145
+ typedef u32 type;
146
+
147
+ f32 alpha, beta, gamma;
148
+ float32x4_t valpha, vbeta, vgamma;
149
+ wAdd(f32 _alpha, f32 _beta, f32 _gamma):
150
+ alpha(_alpha), beta(_beta), gamma(_gamma)
151
+ {
152
+ valpha = vdupq_n_f32(_alpha);
153
+ vbeta = vdupq_n_f32(_beta);
154
+ vgamma = vdupq_n_f32(_gamma);
155
+ }
156
+
157
+ void operator() (const VecTraits<u32>::vec128 & v_src0,
158
+ const VecTraits<u32>::vec128 & v_src1,
159
+ VecTraits<u32>::vec128 & v_dst) const
160
+ {
161
+ float32x4_t vs1 = vcvtq_f32_u32(v_src0);
162
+ float32x4_t vs2 = vcvtq_f32_u32(v_src1);
163
+
164
+ vs1 = vmlaq_f32(vgamma, vs1, valpha);
165
+ vs1 = vmlaq_f32(vs1, vs2, vbeta);
166
+ v_dst = vroundq_u32_f32(vs1);
167
+ }
168
+
169
+ void operator() (const VecTraits<u32>::vec64 & v_src0,
170
+ const VecTraits<u32>::vec64 & v_src1,
171
+ VecTraits<u32>::vec64 & v_dst) const
172
+ {
173
+ float32x2_t vs1 = vcvt_f32_u32(v_src0);
174
+ float32x2_t vs2 = vcvt_f32_u32(v_src1);
175
+
176
+ vs1 = vmla_f32(vget_low(vgamma), vs1, vget_low(valpha));
177
+ vs1 = vmla_f32(vs1, vs2, vget_low(vbeta));
178
+ v_dst = vround_u32_f32(vs1);
179
+ }
180
+
181
+ void operator() (const u32 * src0, const u32 * src1, u32 * dst) const
182
+ {
183
+ dst[0] = saturate_cast<u32>(alpha*src0[0] + beta*src1[0] + gamma);
184
+ }
185
+ };
186
+
187
+ template <> struct wAdd<f32>
188
+ {
189
+ typedef f32 type;
190
+
191
+ f32 alpha, beta, gamma;
192
+ float32x4_t valpha, vbeta, vgamma;
193
+ wAdd(f32 _alpha, f32 _beta, f32 _gamma):
194
+ alpha(_alpha), beta(_beta), gamma(_gamma)
195
+ {
196
+ valpha = vdupq_n_f32(_alpha);
197
+ vbeta = vdupq_n_f32(_beta);
198
+ vgamma = vdupq_n_f32(_gamma + 0.5);
199
+ }
200
+
201
+ void operator() (const VecTraits<f32>::vec128 & v_src0,
202
+ const VecTraits<f32>::vec128 & v_src1,
203
+ VecTraits<f32>::vec128 & v_dst) const
204
+ {
205
+ float32x4_t vs1 = vmlaq_f32(vgamma, v_src0, valpha);
206
+ v_dst = vmlaq_f32(vs1, v_src1, vbeta);
207
+ }
208
+
209
+ void operator() (const VecTraits<f32>::vec64 & v_src0,
210
+ const VecTraits<f32>::vec64 & v_src1,
211
+ VecTraits<f32>::vec64 & v_dst) const
212
+ {
213
+ float32x2_t vs1 = vmla_f32(vget_low(vgamma), v_src0, vget_low(valpha));
214
+ v_dst = vmla_f32(vs1, v_src1, vget_low(vbeta));
215
+
216
+ }
217
+
218
+ void operator() (const f32 * src0, const f32 * src1, f32 * dst) const
219
+ {
220
+ dst[0] = alpha*src0[0] + beta*src1[0] + gamma;
221
+ }
222
+ };
223
+
224
+ } // namespace
225
+
226
+ #define IMPL_ADDWEIGHTED(type) \
227
+ void addWeighted(const Size2D &size, \
228
+ const type * src0Base, ptrdiff_t src0Stride, \
229
+ const type * src1Base, ptrdiff_t src1Stride, \
230
+ type * dstBase, ptrdiff_t dstStride, \
231
+ f32 alpha, f32 beta, f32 gamma) \
232
+ { \
233
+ internal::assertSupportedConfiguration(); \
234
+ wAdd<type> wgtAdd(alpha, \
235
+ beta, \
236
+ gamma); \
237
+ internal::vtransform(size, \
238
+ src0Base, src0Stride, \
239
+ src1Base, src1Stride, \
240
+ dstBase, dstStride, \
241
+ wgtAdd); \
242
+ }
243
+
244
+ #else
245
+
246
+ #define IMPL_ADDWEIGHTED(type) \
247
+ void addWeighted(const Size2D &, \
248
+ const type *, ptrdiff_t, \
249
+ const type *, ptrdiff_t, \
250
+ type *, ptrdiff_t, \
251
+ f32, f32, f32) \
252
+ { \
253
+ internal::assertSupportedConfiguration(); \
254
+ }
255
+
256
+ #endif
257
+
258
+ IMPL_ADDWEIGHTED(u8)
259
+ IMPL_ADDWEIGHTED(s8)
260
+ IMPL_ADDWEIGHTED(u16)
261
+ IMPL_ADDWEIGHTED(s16)
262
+ IMPL_ADDWEIGHTED(u32)
263
+ IMPL_ADDWEIGHTED(s32)
264
+ IMPL_ADDWEIGHTED(f32)
265
+
266
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/bitwise.cpp ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ namespace CAROTENE_NS {
44
+
45
+ #ifdef CAROTENE_NEON
46
+
47
+ struct BitwiseAnd
48
+ {
49
+ typedef u8 type;
50
+
51
+ void operator() (const uint8x16_t & v_src0, const uint8x16_t & v_src1,
52
+ uint8x16_t & v_dst) const
53
+ {
54
+ v_dst = vandq_u8(v_src0, v_src1);
55
+ }
56
+
57
+ void operator() (const uint8x8_t & v_src0, const uint8x8_t & v_src1,
58
+ uint8x8_t & v_dst) const
59
+ {
60
+ v_dst = vand_u8(v_src0, v_src1);
61
+ }
62
+
63
+ void operator() (const u8 * src0, const u8 * src1, u8 * dst) const
64
+ {
65
+ dst[0] = src0[0] & src1[0];
66
+ }
67
+ };
68
+
69
+ struct BitwiseOr
70
+ {
71
+ typedef u8 type;
72
+
73
+ void operator() (const uint8x16_t & v_src0, const uint8x16_t & v_src1,
74
+ uint8x16_t & v_dst) const
75
+ {
76
+ v_dst = vorrq_u8(v_src0, v_src1);
77
+ }
78
+
79
+ void operator() (const uint8x8_t & v_src0, const uint8x8_t & v_src1,
80
+ uint8x8_t & v_dst) const
81
+ {
82
+ v_dst = vorr_u8(v_src0, v_src1);
83
+ }
84
+
85
+ void operator() (const u8 * src0, const u8 * src1, u8 * dst) const
86
+ {
87
+ dst[0] = src0[0] | src1[0];
88
+ }
89
+ };
90
+
91
+ struct BitwiseXor
92
+ {
93
+ typedef u8 type;
94
+
95
+ void operator() (const uint8x16_t & v_src0, const uint8x16_t & v_src1,
96
+ uint8x16_t & v_dst) const
97
+ {
98
+ v_dst = veorq_u8(v_src0, v_src1);
99
+ }
100
+
101
+ void operator() (const uint8x8_t & v_src0, const uint8x8_t & v_src1,
102
+ uint8x8_t & v_dst) const
103
+ {
104
+ v_dst = veor_u8(v_src0, v_src1);
105
+ }
106
+
107
+ void operator() (const u8 * src0, const u8 * src1, u8 * dst) const
108
+ {
109
+ dst[0] = src0[0] ^ src1[0];
110
+ }
111
+ };
112
+
113
+ #endif
114
+
115
+ void bitwiseNot(const Size2D &size,
116
+ const u8 *srcBase, ptrdiff_t srcStride,
117
+ u8 *dstBase, ptrdiff_t dstStride)
118
+ {
119
+ internal::assertSupportedConfiguration();
120
+ #ifdef CAROTENE_NEON
121
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
122
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
123
+
124
+ for (size_t i = 0; i < size.height; ++i)
125
+ {
126
+ const u8* src = internal::getRowPtr(srcBase, srcStride, i);
127
+ u8* dst = internal::getRowPtr(dstBase, dstStride, i);
128
+ size_t j = 0;
129
+
130
+ for (; j < roiw32; j += 32)
131
+ {
132
+ internal::prefetch(src + j);
133
+ uint8x16_t v_src0 = vld1q_u8(src + j), v_src1 = vld1q_u8(src + j + 16);
134
+ uint8x16_t v_dst0 = vmvnq_u8(v_src0), v_dst1 = vmvnq_u8(v_src1);
135
+ vst1q_u8(dst + j, v_dst0);
136
+ vst1q_u8(dst + j + 16, v_dst1);
137
+ }
138
+ for (; j < roiw8; j += 8)
139
+ {
140
+ uint8x8_t v_src = vld1_u8(src + j);
141
+ uint8x8_t v_dst = vmvn_u8(v_src);
142
+ vst1_u8(dst + j, v_dst);
143
+ }
144
+
145
+ for (; j < size.width; j++)
146
+ {
147
+ dst[j] = ~src[j];
148
+ }
149
+ }
150
+ #else
151
+ (void)size;
152
+ (void)srcBase;
153
+ (void)srcStride;
154
+ (void)dstBase;
155
+ (void)dstStride;
156
+ #endif
157
+ }
158
+
159
+ void bitwiseAnd(const Size2D &size,
160
+ const u8 *src0Base, ptrdiff_t src0Stride,
161
+ const u8 *src1Base, ptrdiff_t src1Stride,
162
+ u8 *dstBase, ptrdiff_t dstStride)
163
+ {
164
+ internal::assertSupportedConfiguration();
165
+ #ifdef CAROTENE_NEON
166
+ internal::vtransform(size,
167
+ src0Base, src0Stride,
168
+ src1Base, src1Stride,
169
+ dstBase, dstStride, BitwiseAnd());
170
+ #else
171
+ (void)size;
172
+ (void)src0Base;
173
+ (void)src0Stride;
174
+ (void)src1Base;
175
+ (void)src1Stride;
176
+ (void)dstBase;
177
+ (void)dstStride;
178
+ #endif
179
+ }
180
+
181
+ void bitwiseOr(const Size2D &size,
182
+ const u8 *src0Base, ptrdiff_t src0Stride,
183
+ const u8 *src1Base, ptrdiff_t src1Stride,
184
+ u8 *dstBase, ptrdiff_t dstStride)
185
+ {
186
+ internal::assertSupportedConfiguration();
187
+ #ifdef CAROTENE_NEON
188
+ internal::vtransform(size,
189
+ src0Base, src0Stride,
190
+ src1Base, src1Stride,
191
+ dstBase, dstStride, BitwiseOr());
192
+ #else
193
+ (void)size;
194
+ (void)src0Base;
195
+ (void)src0Stride;
196
+ (void)src1Base;
197
+ (void)src1Stride;
198
+ (void)dstBase;
199
+ (void)dstStride;
200
+ #endif
201
+ }
202
+
203
+ void bitwiseXor(const Size2D &size,
204
+ const u8 *src0Base, ptrdiff_t src0Stride,
205
+ const u8 *src1Base, ptrdiff_t src1Stride,
206
+ u8 *dstBase, ptrdiff_t dstStride)
207
+ {
208
+ internal::assertSupportedConfiguration();
209
+ #ifdef CAROTENE_NEON
210
+ internal::vtransform(size,
211
+ src0Base, src0Stride,
212
+ src1Base, src1Stride,
213
+ dstBase, dstStride, BitwiseXor());
214
+ #else
215
+ (void)size;
216
+ (void)src0Base;
217
+ (void)src0Stride;
218
+ (void)src1Base;
219
+ (void)src1Stride;
220
+ (void)dstBase;
221
+ (void)dstStride;
222
+ #endif
223
+ }
224
+
225
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/blur.cpp ADDED
@@ -0,0 +1,1336 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include <vector>
41
+
42
+ #include "common.hpp"
43
+ #include "saturate_cast.hpp"
44
+ #include "vround_helper.hpp"
45
+
46
+ namespace CAROTENE_NS {
47
+
48
+ bool isBlur3x3Supported(const Size2D &size, BORDER_MODE border)
49
+ {
50
+ return isSupportedConfiguration() && size.width >= 8 &&
51
+ (border == BORDER_MODE_CONSTANT ||
52
+ border == BORDER_MODE_REPLICATE);
53
+ }
54
+
55
+ void blur3x3(const Size2D &size,
56
+ const u8 * srcBase, ptrdiff_t srcStride,
57
+ u8 * dstBase, ptrdiff_t dstStride,
58
+ BORDER_MODE border, u8 borderValue)
59
+ {
60
+ internal::assertSupportedConfiguration(isBlur3x3Supported(size, border));
61
+ #ifdef CAROTENE_NEON
62
+ const int16x8_t v_scale = vmovq_n_s16(3640);
63
+ const uint16x8_t v_border_x3 = vdupq_n_u16(borderValue * 3);
64
+ const uint16x8_t v_zero = vdupq_n_u16(0);
65
+ const uint8x8_t v_border = vdup_n_u8(borderValue);
66
+
67
+ uint16x8_t tprev = v_zero, tcurr = v_zero, tnext = v_zero;
68
+ uint16x8_t t0 = v_zero, t1 = v_zero, t2 = v_zero;
69
+
70
+ ptrdiff_t width = (ptrdiff_t)size.width, height = (ptrdiff_t)size.height;
71
+
72
+ for (ptrdiff_t y = 0; y < height; ++y)
73
+ {
74
+ const u8 * srow0 = y == 0 && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::max<ptrdiff_t>(y - 1, 0));
75
+ const u8 * srow1 = internal::getRowPtr(srcBase, srcStride, y);
76
+ const u8 * srow2 = y + 1 == height && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::min(y + 1, height - 1));
77
+ u8 * drow = internal::getRowPtr(dstBase, dstStride, y);
78
+
79
+ s16 prevx = 0, currx = 0, nextx = 0;
80
+ ptrdiff_t x = 0;
81
+ const ptrdiff_t bwidth = y + 2 < height ? width : (width - 8);
82
+
83
+ // perform vertical convolution
84
+ for ( ; x <= bwidth; x += 8)
85
+ {
86
+ internal::prefetch(srow0 + x);
87
+ internal::prefetch(srow1 + x);
88
+ internal::prefetch(srow2 + x);
89
+
90
+ uint8x8_t x0 = !srow0 ? v_border : vld1_u8(srow0 + x);
91
+ uint8x8_t x1 = vld1_u8(srow1 + x);
92
+ uint8x8_t x2 = !srow2 ? v_border : vld1_u8(srow2 + x);
93
+
94
+ // calculate values for plain CPU part below if needed
95
+ if (x + 8 >= bwidth)
96
+ {
97
+ ptrdiff_t x3 = x == width ? width - 1 : x;
98
+ ptrdiff_t x4 = border == BORDER_MODE_CONSTANT ? x3 - 1 : std::max<ptrdiff_t>(x3 - 1, 0);
99
+
100
+ if (border == BORDER_MODE_CONSTANT && x4 < 0)
101
+ prevx = borderValue;
102
+ else
103
+ prevx = (srow2 ? srow2[x4] : borderValue) + srow1[x4] + (srow0 ? srow0[x4] : borderValue);
104
+
105
+ currx = (srow2 ? srow2[x3] : borderValue) + srow1[x3] + (srow0 ? srow0[x3] : borderValue);
106
+ }
107
+
108
+ // make shift
109
+ if (x)
110
+ {
111
+ tprev = tcurr;
112
+ tcurr = tnext;
113
+ }
114
+
115
+ // and calculate next value
116
+ tnext = vaddw_u8(vaddl_u8(x0, x1), x2);
117
+
118
+ // make extrapolation for the first elements
119
+ if (!x)
120
+ {
121
+ // make border
122
+ if (border == BORDER_MODE_CONSTANT)
123
+ tcurr = v_border_x3;
124
+ else if (border == BORDER_MODE_REPLICATE)
125
+ tcurr = vdupq_n_u16(vgetq_lane_u16(tnext, 0));
126
+
127
+ continue;
128
+ }
129
+
130
+ // combine 3 "shifted" vectors
131
+ t0 = vextq_u16(tprev, tcurr, 7);
132
+ t1 = tcurr;
133
+ t2 = vextq_u16(tcurr, tnext, 1);
134
+
135
+ // and add them
136
+ t0 = vqaddq_u16(t0, vqaddq_u16(t1, t2));
137
+
138
+ int16x8_t tt0 = vqrdmulhq_s16(vreinterpretq_s16_u16(t0), v_scale);
139
+ uint8x8_t it0 = vmovn_u16(vreinterpretq_u16_s16(tt0));
140
+ vst1_u8(drow + x - 8, it0);
141
+ }
142
+
143
+ x -= 8;
144
+ if (x == width)
145
+ --x;
146
+
147
+ for ( ; x < width; ++x)
148
+ {
149
+ // make extrapolation for the last elements
150
+ if (x + 1 >= width)
151
+ {
152
+ if (border == BORDER_MODE_CONSTANT)
153
+ nextx = borderValue * 3;
154
+ else if (border == BORDER_MODE_REPLICATE)
155
+ nextx = srow2[x] + srow1[x] + srow0[x];
156
+ }
157
+ else
158
+ nextx = (srow2 ? srow2[x + 1] : borderValue) +
159
+ srow1[x + 1] +
160
+ (srow0 ? srow0[x + 1] : borderValue);
161
+
162
+ f32 val = (prevx + currx + nextx) * (1 / 9.f) + 0.5f;
163
+ drow[x] = internal::saturate_cast<u8>((s32)val);
164
+
165
+ // make shift
166
+ prevx = currx;
167
+ currx = nextx;
168
+ }
169
+ }
170
+ #else
171
+ (void)size;
172
+ (void)srcBase;
173
+ (void)srcStride;
174
+ (void)dstBase;
175
+ (void)dstStride;
176
+ (void)border;
177
+ (void)borderValue;
178
+ #endif
179
+ }
180
+
181
+ bool isBlurU8Supported(const Size2D &size, s32 cn, BORDER_MODE border)
182
+ {
183
+ return isSupportedConfiguration() &&
184
+ cn > 0 && cn <= 4 &&
185
+ size.width*cn >= 8 && size.height >= 2 &&
186
+ (border == BORDER_MODE_CONSTANT ||
187
+ border == BORDER_MODE_REFLECT101 ||
188
+ border == BORDER_MODE_REFLECT ||
189
+ border == BORDER_MODE_REPLICATE);
190
+ }
191
+
192
+ void blur3x3(const Size2D &size, s32 cn,
193
+ const u8 * srcBase, ptrdiff_t srcStride,
194
+ u8 * dstBase, ptrdiff_t dstStride,
195
+ BORDER_MODE borderType, u8 borderValue)
196
+ {
197
+ internal::assertSupportedConfiguration(isBlurU8Supported(size, cn, borderType));
198
+ #ifdef CAROTENE_NEON
199
+ //#define FLOAT_VARIANT_1_9
200
+ #ifdef FLOAT_VARIANT_1_9
201
+ float32x4_t v1_9 = vdupq_n_f32 (1.0/9.0);
202
+ #else
203
+ const int16x8_t vScale = vmovq_n_s16(3640);
204
+ #endif
205
+
206
+ size_t colsn = size.width*cn;
207
+
208
+ std::vector<u8> _tmp;
209
+ u8 *tmp = 0;
210
+ if (borderType == BORDER_MODE_CONSTANT)
211
+ {
212
+ _tmp.assign(colsn + 2*cn, borderValue);
213
+ tmp = &_tmp[cn];
214
+ }
215
+
216
+ uint16x8_t tprev = vdupq_n_u16(0x0);
217
+ uint16x8_t tcurr = tprev;
218
+ uint16x8_t tnext = tprev;
219
+ uint16x8_t t0, t1, t2;
220
+ if(cn == 1)
221
+ {
222
+ for( size_t y = 0; y < size.height; y++ )
223
+ {
224
+ const u8* srow0;
225
+ const u8* srow1 = internal::getRowPtr(srcBase, srcStride, y);
226
+ const u8* srow2;
227
+ u8* drow = internal::getRowPtr(dstBase, dstStride, y);
228
+ if (borderType == BORDER_MODE_REFLECT101) {
229
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 1);
230
+ srow2 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-2);
231
+ } else if (borderType == BORDER_MODE_CONSTANT) {
232
+ srow0 = y > 0 ? internal::getRowPtr(srcBase, srcStride, y-1) : tmp;
233
+ srow2 = y < size.height-1 ? internal::getRowPtr(srcBase, srcStride, y+1) : tmp;
234
+ } else { // BORDER_MODE_REFLECT || BORDER_MODE_REPLICATE
235
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
236
+ srow2 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-1);
237
+ }
238
+
239
+ // do vertical convolution
240
+ size_t x = 0;
241
+ const size_t bcols = y + 2 < size.height ? colsn : (colsn - 8);
242
+ for( ; x <= bcols; x += 8 )
243
+ {
244
+ internal::prefetch(srow0 + x);
245
+ internal::prefetch(srow1 + x);
246
+ internal::prefetch(srow2 + x);
247
+
248
+ uint8x8_t x0 = vld1_u8(srow0 + x);
249
+ uint8x8_t x1 = vld1_u8(srow1 + x);
250
+ uint8x8_t x2 = vld1_u8(srow2 + x);
251
+
252
+ tprev = tcurr;
253
+ tcurr = tnext;
254
+ tnext = vaddw_u8(vaddl_u8(x0, x1), x2);
255
+
256
+ if(!x) {
257
+ tcurr = tnext;
258
+
259
+ // make border
260
+ if (borderType == BORDER_MODE_CONSTANT)
261
+ {
262
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
263
+ }
264
+ else if (borderType == BORDER_MODE_REFLECT101)
265
+ {
266
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 7);
267
+ }
268
+ else // borderType == BORDER_MODE_REFLECT || borderType == BORDER_MODE_REPLICATE
269
+ {
270
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 7);
271
+ }
272
+ continue;
273
+ }
274
+
275
+ t0 = vextq_u16(tprev, tcurr, 7);
276
+ t1 = tcurr;
277
+ t2 = vextq_u16(tcurr, tnext, 1);
278
+
279
+ t0 = vqaddq_u16(t0, vqaddq_u16(t1, t2));
280
+
281
+ #ifdef FLOAT_VARIANT_1_9
282
+ uint32x4_t tres1 = vmovl_u16(vget_low_u16(t0));
283
+ uint32x4_t tres2 = vmovl_u16(vget_high_u16(t0));
284
+ float32x4_t vf1 = vmulq_f32(v1_9, vcvtq_f32_u32(tres1));
285
+ float32x4_t vf2 = vmulq_f32(v1_9, vcvtq_f32_u32(tres2));
286
+ tres1 = internal::vroundq_u32_f32(vf1);
287
+ tres2 = internal::vroundq_u32_f32(vf2);
288
+ t0 = vcombine_u16(vmovn_u32(tres1),vmovn_u32(tres2));
289
+ vst1_u8(drow + x - 8, vmovn_u16(t0));
290
+ #else
291
+ int16x8_t tt0 = vqrdmulhq_s16(vreinterpretq_s16_u16(t0), vScale);
292
+ uint8x8_t it0 = vmovn_u16(vreinterpretq_u16_s16(tt0));
293
+ vst1_u8(drow + x - 8, it0);
294
+ #endif
295
+ }
296
+
297
+ x -= 8;
298
+ if(x == colsn){
299
+ x--;
300
+ }
301
+ s16 prevx, rowx, nextx;
302
+ prevx = srow2[x-1] + srow1[x-1] + srow0[x-1];
303
+ rowx = srow2[x] + srow1[x] + srow0[x];
304
+ for( ; x < colsn; x++ )
305
+ {
306
+ if(x+1 >= colsn) {
307
+ // make border
308
+ if (borderType == BORDER_MODE_CONSTANT)
309
+ {
310
+ nextx = borderValue;
311
+ } else if (borderType == BORDER_MODE_REFLECT101)
312
+ {
313
+ nextx = srow2[x-1] + srow1[x-1] + srow0[x-1];
314
+ } else {
315
+ nextx = srow2[x] + srow1[x] + srow0[x];
316
+ }
317
+ } else {
318
+ nextx = srow2[x+1] + srow1[x+1] + srow0[x+1];
319
+ }
320
+ *(drow+x) = internal::saturate_cast<u8>((prevx + rowx + nextx)*(1/9.));
321
+ prevx = rowx;
322
+ rowx = nextx;
323
+ }
324
+ }
325
+ }
326
+ else
327
+ {
328
+ for( size_t y = 0; y < size.height; y++ )
329
+ {
330
+ const u8* srow0;
331
+ const u8* srow1 = internal::getRowPtr(srcBase, srcStride, y);
332
+ const u8* srow2;
333
+ u8* drow = internal::getRowPtr(dstBase, dstStride, y);
334
+ if (borderType == BORDER_MODE_REFLECT101) {
335
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 1);
336
+ srow2 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-2);
337
+ } else if (borderType == BORDER_MODE_CONSTANT) {
338
+ srow0 = y > 0 ? internal::getRowPtr(srcBase, srcStride, y-1) : tmp;
339
+ srow2 = y < size.height-1 ? internal::getRowPtr(srcBase, srcStride, y+1) : tmp;
340
+ } else { // BORDER_MODE_REFLECT || BORDER_MODE_REPLICATE
341
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
342
+ srow2 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-1);
343
+ }
344
+
345
+ // do vertical convolution
346
+ size_t x = 0;
347
+ const size_t bcols = y + 2 < size.height ? colsn : (colsn - 8);
348
+ for( ; x <= bcols; x += 8 )
349
+ {
350
+ internal::prefetch(srow0 + x);
351
+ internal::prefetch(srow1 + x);
352
+ internal::prefetch(srow2 + x);
353
+
354
+ uint8x8_t x0 = vld1_u8(srow0 + x);
355
+ uint8x8_t x1 = vld1_u8(srow1 + x);
356
+ uint8x8_t x2 = vld1_u8(srow2 + x);
357
+
358
+ tprev = tcurr;
359
+ tcurr = tnext;
360
+ tnext = vaddw_u8(vaddl_u8(x0, x1), x2);
361
+
362
+ if(!x) {
363
+ tcurr = tnext;
364
+
365
+ // make border
366
+ switch(cn)
367
+ {
368
+ case 2:
369
+ if (borderType == BORDER_MODE_CONSTANT)
370
+ {
371
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
372
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
373
+ }
374
+ else if (borderType == BORDER_MODE_REFLECT101)
375
+ {
376
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tcurr, 6);
377
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tcurr, 6);
378
+ }
379
+ else
380
+ {
381
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 6);
382
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 7);
383
+ }
384
+ break;
385
+ case 3:
386
+ if (borderType == BORDER_MODE_CONSTANT)
387
+ {
388
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 5);
389
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
390
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
391
+ }
392
+ else if (borderType == BORDER_MODE_REFLECT101)
393
+ {
394
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 4),tcurr, 6);
395
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 5),tcurr, 7);
396
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tcurr, 5);
397
+ }
398
+ else
399
+ {
400
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 5);
401
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 6);
402
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tcurr, 7);
403
+ }
404
+ break;
405
+ case 4:
406
+ if (borderType == BORDER_MODE_CONSTANT)
407
+ {
408
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 4);
409
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 5);
410
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
411
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
412
+ }
413
+ else if (borderType != BORDER_MODE_REFLECT101)
414
+ {
415
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 4);
416
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 5);
417
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tcurr, 6);
418
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tcurr, 7);
419
+ }
420
+ break;
421
+ }
422
+ continue;
423
+ }
424
+
425
+ if(cn==2)
426
+ t0 = vextq_u16(tprev, tcurr, 6);
427
+ else if(cn==3)
428
+ t0 = vextq_u16(tprev, tcurr, 5);
429
+ else if(cn==4)
430
+ t0 = vextq_u16(tprev, tcurr, 4);
431
+
432
+ t1 = tcurr;
433
+
434
+ if(cn==2)
435
+ t2 = vextq_u16(tcurr, tnext, 2);
436
+ else if(cn==3)
437
+ t2 = vextq_u16(tcurr, tnext, 3);
438
+ else if(cn==4)
439
+ t2 = vextq_u16(tcurr, tnext, 4);
440
+
441
+ t0 = vqaddq_u16(t0, vqaddq_u16(t1, t2));
442
+
443
+ #ifdef FLOAT_VARIANT_1_9
444
+ uint32x4_t tres1 = vmovl_u16(vget_low_u16(t0));
445
+ uint32x4_t tres2 = vmovl_u16(vget_high_u16(t0));
446
+ float32x4_t vf1 = vmulq_f32(v1_9, vcvtq_f32_u32(tres1));
447
+ float32x4_t vf2 = vmulq_f32(v1_9, vcvtq_f32_u32(tres2));
448
+ tres1 = internal::vroundq_u32_f32(vf1);
449
+ tres2 = internal::vroundq_u32_f32(vf2);
450
+ t0 = vcombine_u16(vmovn_u32(tres1),vmovn_u32(tres2));
451
+ vst1_u8(drow + x - 8, vmovn_u16(t0));
452
+ #else
453
+ int16x8_t tt0 = vqrdmulhq_s16(vreinterpretq_s16_u16(t0), vScale);
454
+ uint8x8_t it0 = vmovn_u16(vreinterpretq_u16_s16(tt0));
455
+ vst1_u8(drow + x - 8, it0);
456
+ #endif
457
+ }
458
+
459
+ x -= 8;
460
+ if(x == colsn){
461
+ x -= cn;
462
+ }
463
+ s16 prevx[4], rowx[4], nextx[4];
464
+ for( s32 k = 0; k < cn; k++ )
465
+ {
466
+ prevx[(k + x%cn)%cn] = srow2[x+k-cn] + srow1[x+k-cn] + srow0[x+k-cn];
467
+ rowx[(k + x%cn)%cn] = srow2[x+k] + srow1[x+k] + srow0[x+k];
468
+ }
469
+ for( ; x < colsn; x++ )
470
+ {
471
+ size_t xx = x%cn;
472
+ if(x+cn >= colsn) {
473
+ // make border
474
+ if (borderType == BORDER_MODE_CONSTANT)
475
+ {
476
+ nextx[xx] = borderValue;
477
+ } else if (borderType == BORDER_MODE_REFLECT101)
478
+ {
479
+ nextx[xx] = srow2[x-cn] + srow1[x-cn] + srow0[x-cn];
480
+ } else {
481
+ nextx[xx] = srow2[x] + srow1[x] + srow0[x];
482
+ }
483
+ } else {
484
+ nextx[xx] = srow2[x+cn] + srow1[x+cn] + srow0[x+cn];
485
+ }
486
+ *(drow+x) = internal::saturate_cast<u8>((prevx[xx] + rowx[xx] + nextx[xx])*(1/9.));
487
+ prevx[xx] = rowx[xx];
488
+ rowx[xx] = nextx[xx];
489
+ }
490
+ }
491
+ }
492
+ #else
493
+ (void)srcBase;
494
+ (void)srcStride;
495
+ (void)dstBase;
496
+ (void)dstStride;
497
+ (void)borderValue;
498
+ #endif
499
+ }
500
+
501
+ void blur5x5(const Size2D &size, s32 cn,
502
+ const u8 * srcBase, ptrdiff_t srcStride,
503
+ u8 * dstBase, ptrdiff_t dstStride,
504
+ BORDER_MODE borderType, u8 borderValue)
505
+ {
506
+ internal::assertSupportedConfiguration(isBlurU8Supported(size, cn, borderType));
507
+ #ifdef CAROTENE_NEON
508
+ #define FLOAT_VARIANT_1_25
509
+ #ifdef FLOAT_VARIANT_1_25
510
+ float32x4_t v1_25 = vdupq_n_f32 (1.0f/25.0f);
511
+ #else
512
+ const int16x8_t vScale = vmovq_n_s16(1310);
513
+ #endif
514
+ size_t colsn = size.width*cn;
515
+
516
+ std::vector<u8> _tmp;
517
+ u8 *tmp = 0;
518
+ if (borderType == BORDER_MODE_CONSTANT)
519
+ {
520
+ _tmp.assign(colsn + 2*cn, borderValue);
521
+ tmp = &_tmp[cn];
522
+ }
523
+
524
+ uint16x8_t tprev = vdupq_n_u16(0x0);
525
+ uint16x8_t tcurr = tprev;
526
+ uint16x8_t tnext = tprev;
527
+ uint16x8_t t0, t1, t2, t3, t4;
528
+ for( size_t y = 0; y < size.height; y++ )
529
+ {
530
+ const u8 *srow0, *srow1;
531
+ const u8 *srow2 = internal::getRowPtr(srcBase, srcStride, y);
532
+ const u8 *srow3, *srow4;
533
+ u8 *drow = internal::getRowPtr(dstBase, dstStride, y);
534
+ if (borderType == BORDER_MODE_REFLECT101) {
535
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 1 ? y-2 : 2-y);
536
+ srow1 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 1);
537
+ srow3 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-2);
538
+ srow4 = internal::getRowPtr(srcBase, srcStride, y < size.height-2 ? y+2 : (size.height<<1)-4-y);
539
+ } else if (borderType == BORDER_MODE_CONSTANT) {
540
+ srow0 = y > 1 ? internal::getRowPtr(srcBase, srcStride, y-2) : tmp;
541
+ srow1 = y > 0 ? internal::getRowPtr(srcBase, srcStride, y-1) : tmp;
542
+ srow3 = y < size.height-1 ? internal::getRowPtr(srcBase, srcStride, y+1) : tmp;
543
+ srow4 = y < size.height-2 ? internal::getRowPtr(srcBase, srcStride, y+2) : tmp;
544
+ } else if (borderType == BORDER_MODE_REFLECT) {
545
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 1 ? y-2 : 1-y);
546
+ srow1 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
547
+ srow3 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-1);
548
+ srow4 = internal::getRowPtr(srcBase, srcStride, y < size.height-2 ? y+2 : (size.height<<1)-3-y);
549
+ } else { // BORDER_MODE_REPLICATE
550
+ srow0 = internal::getRowPtr(srcBase, srcStride, y > 1 ? y-2 : 0);
551
+ srow1 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
552
+ srow3 = internal::getRowPtr(srcBase, srcStride, y < size.height-1 ? y+1 : size.height-1);
553
+ srow4 = internal::getRowPtr(srcBase, srcStride, y < size.height-2 ? y+2 : size.height-1);
554
+ }
555
+
556
+ // do vertical convolution
557
+ size_t x = 0;
558
+ const size_t bcols = y + 3 < size.height ? colsn : (colsn - 8);
559
+ for( ; x <= bcols; x += 8 )
560
+ {
561
+ internal::prefetch(srow0 + x);
562
+ internal::prefetch(srow1 + x);
563
+ internal::prefetch(srow2 + x);
564
+ internal::prefetch(srow3 + x);
565
+ internal::prefetch(srow4 + x);
566
+
567
+ uint8x8_t x0 = vld1_u8(srow0 + x);
568
+ uint8x8_t x1 = vld1_u8(srow1 + x);
569
+ uint8x8_t x2 = vld1_u8(srow2 + x);
570
+ uint8x8_t x3 = vld1_u8(srow3 + x);
571
+ uint8x8_t x4 = vld1_u8(srow4 + x);
572
+
573
+ tprev = tcurr;
574
+ tcurr = tnext;
575
+ tnext = vaddw_u8(vaddq_u16(vaddl_u8(x0, x1), vaddl_u8(x2, x3)), x4);
576
+
577
+ if(!x) {
578
+ tcurr = tnext;
579
+
580
+ if(borderType == BORDER_MODE_REFLECT101 && size.width < 3)
581
+ {
582
+ x = 8;
583
+ break;
584
+ }
585
+
586
+ // make border
587
+ switch(cn)
588
+ {
589
+ case 1:
590
+ if (borderType == BORDER_MODE_CONSTANT)
591
+ {
592
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
593
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
594
+ }
595
+ else if (borderType == BORDER_MODE_REFLECT101)
596
+ {
597
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tcurr, 6);
598
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 7);
599
+ }
600
+ else if (borderType == BORDER_MODE_REFLECT)
601
+ {
602
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 6);
603
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 7);
604
+ }
605
+ else
606
+ {
607
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 6);
608
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 7);
609
+ }
610
+ break;
611
+ case 2:
612
+ if (borderType == BORDER_MODE_CONSTANT)
613
+ {
614
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 4);
615
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 5);
616
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
617
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
618
+ }
619
+ else if (borderType == BORDER_MODE_REFLECT101)
620
+ {
621
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tcurr, 6);
622
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tcurr, 7);
623
+ }
624
+ else if (borderType == BORDER_MODE_REFLECT)
625
+ {
626
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tcurr, 4);
627
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tcurr, 5);
628
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 6);
629
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 7);
630
+ }
631
+ else
632
+ {
633
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 4);
634
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 5);
635
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 6);
636
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tcurr, 7);
637
+ }
638
+ break;
639
+ case 3:
640
+ if (borderType == BORDER_MODE_CONSTANT)
641
+ {
642
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 2);
643
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 3);
644
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 4);
645
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 5);
646
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
647
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
648
+ }
649
+ else if (borderType == BORDER_MODE_REFLECT101)
650
+ {
651
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 6),tcurr, 2);
652
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 7),tprev, 3);
653
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tprev, 5);
654
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 4),tprev, 6);
655
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 5),tprev, 7);
656
+ s16 lane8 = srow4[8] + srow3[8] + srow2[8] + srow1[8] + srow0[8];
657
+ tcurr = vsetq_lane_u16(lane8,tprev, 4);
658
+ }
659
+ else if (borderType == BORDER_MODE_REFLECT)
660
+ {
661
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 3),tcurr, 2);
662
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 4),tprev, 3);
663
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 5),tprev, 4);
664
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tprev, 5);
665
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tprev, 6);
666
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tprev, 7);
667
+ }
668
+ else
669
+ {
670
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tcurr, 2);
671
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tprev, 3);
672
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tprev, 4);
673
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 0),tprev, 5);
674
+ tprev = vsetq_lane_u16(vgetq_lane_u16(tcurr, 1),tprev, 6);
675
+ tcurr = vsetq_lane_u16(vgetq_lane_u16(tcurr, 2),tprev, 7);
676
+ }
677
+ break;
678
+ case 4:
679
+ if (borderType == BORDER_MODE_CONSTANT)
680
+ {
681
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 0);
682
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 1);
683
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 2);
684
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 3);
685
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 4);
686
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 5);
687
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 6);
688
+ tcurr = vsetq_lane_u16(borderValue, tcurr, 7);
689
+ }
690
+ else if (borderType == BORDER_MODE_REFLECT101)
691
+ {
692
+ s16 lane8 = srow4[ 8] + srow3[ 8] + srow2[ 8] + srow1[ 8] + srow0[ 8];
693
+ s16 lane9 = srow4[ 9] + srow3[ 9] + srow2[ 9] + srow1[ 9] + srow0[ 9];
694
+ s16 lane10 = srow4[10] + srow3[10] + srow2[10] + srow1[10] + srow0[10];
695
+ s16 lane11 = srow4[11] + srow3[11] + srow2[11] + srow1[11] + srow0[11];
696
+ tprev = vsetq_lane_u16( lane8,tcurr, 0);
697
+ tprev = vsetq_lane_u16( lane9,tprev, 1);
698
+ tprev = vsetq_lane_u16(lane10,tprev, 2);
699
+ tcurr = vsetq_lane_u16(lane11,tprev, 3);
700
+ }
701
+ else if (borderType == BORDER_MODE_REFLECT)
702
+ {
703
+ tcurr = vcombine_u16(vget_high_u16(tcurr),vget_low_u16(tcurr));//swap 64-bit parts
704
+ }
705
+ else
706
+ {
707
+ tcurr = vcombine_u16(vget_low_u16(tcurr),vget_low_u16(tcurr));//double 64-bit part
708
+ }
709
+ break;
710
+ }
711
+ continue;
712
+ }
713
+ switch(cn)
714
+ {
715
+ case 1:
716
+ t0 = vextq_u16(tprev, tcurr, 6);
717
+ t1 = vextq_u16(tprev, tcurr, 7);
718
+ t2 = tcurr;
719
+ t3 = vextq_u16(tcurr, tnext, 1);
720
+ t4 = vextq_u16(tcurr, tnext, 2);
721
+ break;
722
+ case 2:
723
+ t0 = vextq_u16(tprev, tcurr, 4);
724
+ t1 = vextq_u16(tprev, tcurr, 6);
725
+ t2 = tcurr;
726
+ t3 = vextq_u16(tcurr, tnext, 2);
727
+ t4 = vextq_u16(tcurr, tnext, 4);
728
+ break;
729
+ case 3:
730
+ t0 = vextq_u16(tprev, tcurr, 2);
731
+ t1 = vextq_u16(tprev, tcurr, 5);
732
+ t2 = tcurr;
733
+ t3 = vextq_u16(tcurr, tnext, 3);
734
+ t4 = vextq_u16(tcurr, tnext, 6);
735
+ break;
736
+ case 4:
737
+ t0 = tprev;
738
+ t1 = vextq_u16(tprev, tcurr, 4);
739
+ t2 = tcurr;
740
+ t3 = vextq_u16(tcurr, tnext, 4);
741
+ t4 = tnext;
742
+ break;
743
+ default:
744
+ internal::assertSupportedConfiguration(false);//Unsupported channels number
745
+ return;
746
+ }
747
+ t0 = vqaddq_u16(vqaddq_u16(vqaddq_u16(t0, t1), vqaddq_u16(t2, t3)), t4);
748
+
749
+ #ifdef FLOAT_VARIANT_1_25
750
+ uint32x4_t tres1 = vmovl_u16(vget_low_u16(t0));
751
+ uint32x4_t tres2 = vmovl_u16(vget_high_u16(t0));
752
+ float32x4_t vf1 = vmulq_f32(v1_25, vcvtq_f32_u32(tres1));
753
+ float32x4_t vf2 = vmulq_f32(v1_25, vcvtq_f32_u32(tres2));
754
+ tres1 = internal::vroundq_u32_f32(vf1);
755
+ tres2 = internal::vroundq_u32_f32(vf2);
756
+ t0 = vcombine_u16(vmovn_u32(tres1),vmovn_u32(tres2));
757
+ vst1_u8(drow + x - 8, vmovn_u16(t0));
758
+ #else
759
+ int16x8_t tt0 = vqrdmulhq_s16(vreinterpretq_s16_u16(t0), vScale);
760
+ uint8x8_t it0 = vmovn_u16(vreinterpretq_u16_s16(tt0));
761
+ vst1_u8(drow + x - 8, it0);
762
+ #endif
763
+ }
764
+
765
+ x -= 8;
766
+ if(x == colsn){
767
+ x -= cn;
768
+ }
769
+ s16 pprevx[4], prevx[4], rowx[4], nextx[4], nnextx[4];
770
+ ptrdiff_t px = x / cn;
771
+ for( s32 k = 0; k < cn; k++ )
772
+ {
773
+ ptrdiff_t ploc;
774
+ ploc = internal::borderInterpolate(px-2, size.width, borderType);
775
+ pprevx[k] = ploc < 0 ? 5*borderValue :
776
+ srow4[ploc*cn+k] + srow3[ploc*cn+k] + srow2[ploc*cn+k] + srow1[ploc*cn+k] + srow0[ploc*cn+k];
777
+
778
+ ploc = internal::borderInterpolate(px-1, size.width, borderType);
779
+ prevx[k] = ploc < 0 ? 5*borderValue :
780
+ srow4[ploc*cn+k] + srow3[ploc*cn+k] + srow2[ploc*cn+k] + srow1[ploc*cn+k] + srow0[ploc*cn+k];
781
+
782
+ rowx[k] = srow4[px*cn+k] + srow3[px*cn+k] + srow2[px*cn+k] + srow1[px*cn+k] + srow0[px*cn+k];
783
+
784
+ ploc = internal::borderInterpolate(px+1, size.width, borderType);
785
+ nextx[k] = ploc < 0 ? 5*borderValue :
786
+ srow4[ploc*cn+k] + srow3[ploc*cn+k] + srow2[ploc*cn+k] + srow1[ploc*cn+k] + srow0[ploc*cn+k];
787
+ }
788
+ x = px*cn;
789
+ for( ; x < colsn; x+=cn, px++ )
790
+ {
791
+ for( s32 k = 0; k < cn; k++ )
792
+ {
793
+ ptrdiff_t ploc = internal::borderInterpolate(px+2, size.width, borderType);
794
+ nnextx[k] = ploc < 0 ? 5*borderValue :
795
+ srow4[ploc*cn+k] + srow3[ploc*cn+k] + srow2[ploc*cn+k] + srow1[ploc*cn+k] + srow0[ploc*cn+k];
796
+ *(drow+x+k) = internal::saturate_cast<u8>((pprevx[k] + prevx[k] + rowx[k] + nextx[k] +nnextx[k])*(1/25.));
797
+ pprevx[k] = prevx[k];
798
+ prevx[k] = rowx[k];
799
+ rowx[k] = nextx[k];
800
+ nextx[k] = nnextx[k];
801
+ }
802
+ }
803
+ }
804
+ #else
805
+ (void)srcBase;
806
+ (void)srcStride;
807
+ (void)dstBase;
808
+ (void)dstStride;
809
+ (void)borderValue;
810
+ #endif
811
+ }
812
+
813
+ bool isBlurF32Supported(const Size2D &size, s32 cn, BORDER_MODE border)
814
+ {
815
+ return isSupportedConfiguration() &&
816
+ cn > 0 && cn <= 4 &&
817
+ size.width*cn >= 4 && size.height >= 2 &&
818
+ (border == BORDER_MODE_CONSTANT ||
819
+ border == BORDER_MODE_REFLECT101 ||
820
+ border == BORDER_MODE_REFLECT ||
821
+ border == BORDER_MODE_REPLICATE ||
822
+ border == BORDER_MODE_WRAP);
823
+ }
824
+
825
+ void blur3x3(const Size2D &size, s32 cn,
826
+ const f32 * srcBase, ptrdiff_t srcStride,
827
+ f32 * dstBase, ptrdiff_t dstStride,
828
+ BORDER_MODE borderType, f32 borderValue, Margin borderMargin)
829
+ {
830
+ internal::assertSupportedConfiguration(isBlurF32Supported(size, cn, borderType));
831
+ #ifdef CAROTENE_NEON
832
+ size_t colsn = size.width * cn;
833
+
834
+ std::vector<f32> _tmp;
835
+ f32 *tmp = 0;
836
+ if (borderType == BORDER_MODE_CONSTANT)
837
+ {
838
+ _tmp.assign(colsn + 2*cn, borderValue);
839
+ tmp = &_tmp[cn];
840
+ }
841
+
842
+ ptrdiff_t idx_l = internal::borderInterpolate(-1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
843
+ ptrdiff_t idx_r = internal::borderInterpolate(size.width, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
844
+
845
+ //2-line buffer
846
+ std::vector<f32> _buf(4*(cn * (size.width + 2) + 32 / sizeof(f32)));
847
+ f32* lanea = internal::alignPtr(&_buf[cn], 32);
848
+ f32* laneA = internal::alignPtr(lanea + cn * (size.width + 2), 32);
849
+
850
+ f32* laneb = internal::alignPtr(laneA + cn * (size.width + 2), 32);
851
+ f32* laneB = internal::alignPtr(laneb + cn * (size.width + 2), 32);
852
+
853
+ if (borderType == BORDER_MODE_CONSTANT)
854
+ for (s32 k = 0; k < cn; ++k)
855
+ {
856
+ lanea[-cn+k] = borderValue;
857
+ lanea[colsn+k] = borderValue;
858
+ laneA[-cn+k] = borderValue;
859
+ laneA[colsn+k] = borderValue;
860
+ laneb[-cn+k] = borderValue;
861
+ laneb[colsn+k] = borderValue;
862
+ laneB[-cn+k] = borderValue;
863
+ laneB[colsn+k] = borderValue;
864
+ }
865
+
866
+ size_t i = 0;
867
+ f32* dsta = internal::getRowPtr(dstBase, dstStride, 0);
868
+ for (; i < size.height-1; i+=2)
869
+ {
870
+ //vertical convolution
871
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
872
+ ptrdiff_t idx_rp2 = internal::borderInterpolate(i + 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
873
+
874
+ const f32* ln0 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
875
+ const f32* ln1 = internal::getRowPtr(srcBase, srcStride, i);
876
+ const f32* ln2 = internal::getRowPtr(srcBase, srcStride, i + 1);
877
+ const f32* ln3 = idx_rp2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp2) : tmp;
878
+
879
+ size_t x = 0;
880
+ for (; x <= colsn - 4; x += 4)
881
+ {
882
+ internal::prefetch(ln1 + x);
883
+ internal::prefetch(ln2 + x);
884
+ internal::prefetch(ln0 + x);
885
+ internal::prefetch(ln3 + x);
886
+ box3x3f32_vert:
887
+ float32x4_t v1 = vld1q_f32(ln1 + x);
888
+ float32x4_t v2 = vld1q_f32(ln2 + x);
889
+ float32x4_t v0 = vld1q_f32(ln0 + x);
890
+ float32x4_t v3 = vld1q_f32(ln3 + x);
891
+
892
+ float32x4_t v = vaddq_f32(v1, v2);
893
+ float32x4_t w0 = vaddq_f32(v, v0);
894
+ float32x4_t w1 = vaddq_f32(v, v3);
895
+
896
+ vst1q_f32(lanea + x, w0);
897
+ vst1q_f32(laneb + x, w1);
898
+ }
899
+ if(x < colsn)
900
+ {
901
+ x = colsn-4;
902
+ goto box3x3f32_vert;
903
+ }
904
+
905
+ //left&right borders
906
+ if (borderType != BORDER_MODE_CONSTANT)
907
+ for (s32 k = 0; k < cn; ++k)
908
+ {
909
+ lanea[-cn+k] = lanea[idx_l + k];
910
+ lanea[colsn+k] = lanea[idx_r + k];
911
+ laneb[-cn+k] = laneb[idx_l + k];
912
+ laneb[colsn+k] = laneb[idx_r + k];
913
+ }
914
+
915
+ //horizontal convolution (2 lines from previous iteration)
916
+ if (i > 0)
917
+ {
918
+ f32* dstb = internal::getRowPtr(dstBase, dstStride, i-1);
919
+ x = 0;
920
+ for (; x <= colsn - 4; x += 4)
921
+ {
922
+ internal::prefetch(laneA + x + cn);
923
+ internal::prefetch(laneB + x + cn);
924
+ box3x3f32_horiz:
925
+ float32x4_t lane0a = vld1q_f32(laneA + x - cn);
926
+ float32x4_t lane2a = vld1q_f32(laneA + x + cn);
927
+ float32x4_t lane1a = vld1q_f32(laneA + x);
928
+
929
+ float32x4_t lane0b = vld1q_f32(laneB + x - cn);
930
+ float32x4_t lane2b = vld1q_f32(laneB + x + cn);
931
+ float32x4_t lane1b = vld1q_f32(laneB + x);
932
+
933
+ float32x4_t va = vaddq_f32(lane0a, lane2a);
934
+ float32x4_t vb = vaddq_f32(lane0b, lane2b);
935
+ float32x4_t wa = vaddq_f32(va, lane1a);
936
+ float32x4_t wb = vaddq_f32(vb, lane1b);
937
+
938
+ vst1q_f32(dsta + x, wa);
939
+ vst1q_f32(dstb + x, wb);
940
+ }
941
+ if(x < colsn)
942
+ {
943
+ x = colsn-4;
944
+ goto box3x3f32_horiz;
945
+ }
946
+ dsta = internal::getRowPtr(dstBase, dstStride, i);
947
+ }
948
+
949
+ std::swap(lanea, laneA);
950
+ std::swap(laneb, laneB);
951
+ }
952
+
953
+ //last line
954
+ if(i < size.height)
955
+ {
956
+ //vertical convolution
957
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
958
+ ptrdiff_t idx_rp1 = internal::borderInterpolate(i + 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
959
+
960
+ const f32* ln0 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
961
+ const f32* ln1 = internal::getRowPtr(srcBase, srcStride, i);
962
+ const f32* ln2 = idx_rp1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp1) : tmp;
963
+
964
+ size_t x = 0;
965
+ for (; x <= colsn - 4; x += 4)
966
+ {
967
+ internal::prefetch(ln0 + x);
968
+ internal::prefetch(ln1 + x);
969
+ internal::prefetch(ln2 + x);
970
+ box3x3f32_vert_ll:
971
+ float32x4_t v0 = vld1q_f32(ln0+x);
972
+ float32x4_t v1 = vld1q_f32(ln1+x);
973
+ float32x4_t v2 = vld1q_f32(ln2+x);
974
+
975
+ float32x4_t v = vaddq_f32(v0, v1);
976
+ float32x4_t w = vaddq_f32(v, v2);
977
+
978
+ vst1q_f32(lanea + x, w);
979
+ }
980
+ if(x < colsn)
981
+ {
982
+ x = colsn-4;
983
+ goto box3x3f32_vert_ll;
984
+ }
985
+
986
+ //left&right borders
987
+ if (borderType != BORDER_MODE_CONSTANT)
988
+ for (s32 k = 0; k < cn; ++k)
989
+ {
990
+ lanea[-cn+k] = lanea[idx_l + k];
991
+ lanea[colsn+k] = lanea[idx_r + k];
992
+ }
993
+
994
+ //horizontal convolution (last 3 lines)
995
+ x = 0;
996
+ f32* dstb = internal::getRowPtr(dstBase, dstStride, i-1);
997
+ f32* dstc = internal::getRowPtr(dstBase, dstStride, i);
998
+ for (; x <= colsn - 4; x += 4)
999
+ {
1000
+ internal::prefetch(laneA + x + cn);
1001
+ internal::prefetch(laneB + x + cn);
1002
+ internal::prefetch(lanea + x + cn);
1003
+ box3x3f32_horiz_ll:
1004
+ float32x4_t lane0a = vld1q_f32(laneA + x - cn);
1005
+ float32x4_t lane2a = vld1q_f32(laneA + x + cn);
1006
+ float32x4_t lane1a = vld1q_f32(laneA + x);
1007
+
1008
+ float32x4_t lane0b = vld1q_f32(laneB + x - cn);
1009
+ float32x4_t lane2b = vld1q_f32(laneB + x + cn);
1010
+ float32x4_t lane1b = vld1q_f32(laneB + x);
1011
+
1012
+ float32x4_t lane0c = vld1q_f32(lanea + x - cn);
1013
+ float32x4_t lane2c = vld1q_f32(lanea + x + cn);
1014
+ float32x4_t lane1c = vld1q_f32(lanea + x);
1015
+
1016
+ float32x4_t va = vaddq_f32(lane0a, lane2a);
1017
+ float32x4_t vb = vaddq_f32(lane0b, lane2b);
1018
+ float32x4_t vc = vaddq_f32(lane0c, lane2c);
1019
+ float32x4_t wa = vaddq_f32(va, lane1a);
1020
+ float32x4_t wb = vaddq_f32(vb, lane1b);
1021
+ float32x4_t wc = vaddq_f32(vc, lane1c);
1022
+
1023
+ vst1q_f32(dsta + x, wa);
1024
+ vst1q_f32(dstb + x, wb);
1025
+ vst1q_f32(dstc + x, wc);
1026
+ }
1027
+ if(x < colsn)
1028
+ {
1029
+ x = colsn-4;
1030
+ goto box3x3f32_horiz_ll;
1031
+ }
1032
+ }
1033
+ else
1034
+ {
1035
+ //horizontal convolution (last 2 lines)
1036
+ f32* dstb = internal::getRowPtr(dstBase, dstStride, i-1);
1037
+ size_t x = 0;
1038
+ for (; x <= colsn - 4; x += 4)
1039
+ {
1040
+ internal::prefetch(laneA + x + cn);
1041
+ internal::prefetch(laneB + x + cn);
1042
+ box3x3f32_horiz_last2:
1043
+ float32x4_t lane0a = vld1q_f32(laneA + x - cn);
1044
+ float32x4_t lane2a = vld1q_f32(laneA + x + cn);
1045
+ float32x4_t lane1a = vld1q_f32(laneA + x);
1046
+
1047
+ float32x4_t lane0b = vld1q_f32(laneB + x - cn);
1048
+ float32x4_t lane2b = vld1q_f32(laneB + x + cn);
1049
+ float32x4_t lane1b = vld1q_f32(laneB + x);
1050
+
1051
+ float32x4_t va = vaddq_f32(lane0a, lane2a);
1052
+ float32x4_t vb = vaddq_f32(lane0b, lane2b);
1053
+ float32x4_t wa = vaddq_f32(va, lane1a);
1054
+ float32x4_t wb = vaddq_f32(vb, lane1b);
1055
+
1056
+ vst1q_f32(dsta + x, wa);
1057
+ vst1q_f32(dstb + x, wb);
1058
+ }
1059
+ if(x < colsn)
1060
+ {
1061
+ x = colsn-4;
1062
+ goto box3x3f32_horiz_last2;
1063
+ }
1064
+ }
1065
+ #else
1066
+ (void)srcBase;
1067
+ (void)srcStride;
1068
+ (void)dstBase;
1069
+ (void)dstStride;
1070
+ (void)borderValue;
1071
+ (void)borderMargin;
1072
+ #endif
1073
+ }
1074
+
1075
+ bool isBlurS32Supported(const Size2D &size, s32 cn, BORDER_MODE border)
1076
+ {
1077
+ return isSupportedConfiguration() &&
1078
+ cn > 0 && cn <= 4 &&
1079
+ size.width*cn >= 4 && size.height >= 2 &&
1080
+ (border == BORDER_MODE_CONSTANT ||
1081
+ border == BORDER_MODE_REFLECT101 ||
1082
+ border == BORDER_MODE_REFLECT ||
1083
+ border == BORDER_MODE_REPLICATE ||
1084
+ border == BORDER_MODE_WRAP);
1085
+ }
1086
+
1087
+ void blur3x3(const Size2D &size, s32 cn,
1088
+ const s32 * srcBase, ptrdiff_t srcStride,
1089
+ s32 * dstBase, ptrdiff_t dstStride,
1090
+ BORDER_MODE borderType, s32 borderValue, Margin borderMargin)
1091
+ {
1092
+ internal::assertSupportedConfiguration(isBlurS32Supported(size, cn, borderType));
1093
+ #ifdef CAROTENE_NEON
1094
+ size_t colsn = size.width * cn;
1095
+
1096
+ std::vector<s32> _tmp;
1097
+ s32 *tmp = 0;
1098
+ if (borderType == BORDER_MODE_CONSTANT)
1099
+ {
1100
+ _tmp.assign(colsn + 2*cn, borderValue);
1101
+ tmp = &_tmp[cn];
1102
+ }
1103
+
1104
+ ptrdiff_t idx_l = internal::borderInterpolate(-1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
1105
+ ptrdiff_t idx_r = internal::borderInterpolate(size.width, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
1106
+
1107
+ //2-line buffer
1108
+ std::vector<s32> _buf(4*(cn * (size.width + 2) + 32 / sizeof(s32)));
1109
+ s32* lanea = internal::alignPtr(&_buf[cn], 32);
1110
+ s32* laneA = internal::alignPtr(lanea + cn * (size.width + 2), 32);
1111
+
1112
+ s32* laneb = internal::alignPtr(laneA + cn * (size.width + 2), 32);
1113
+ s32* laneB = internal::alignPtr(laneb + cn * (size.width + 2), 32);
1114
+
1115
+ if (borderType == BORDER_MODE_CONSTANT)
1116
+ for (s32 k = 0; k < cn; ++k)
1117
+ {
1118
+ lanea[-cn+k] = borderValue;
1119
+ lanea[colsn+k] = borderValue;
1120
+ laneA[-cn+k] = borderValue;
1121
+ laneA[colsn+k] = borderValue;
1122
+ laneb[-cn+k] = borderValue;
1123
+ laneb[colsn+k] = borderValue;
1124
+ laneB[-cn+k] = borderValue;
1125
+ laneB[colsn+k] = borderValue;
1126
+ }
1127
+
1128
+ size_t i = 0;
1129
+ s32* dsta = internal::getRowPtr(dstBase, dstStride, 0);
1130
+ for (; i < size.height-1; i+=2)
1131
+ {
1132
+ //vertical convolution
1133
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
1134
+ ptrdiff_t idx_rp2 = internal::borderInterpolate(i + 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
1135
+
1136
+ const s32* ln0 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
1137
+ const s32* ln1 = internal::getRowPtr(srcBase, srcStride, i);
1138
+ const s32* ln2 = internal::getRowPtr(srcBase, srcStride, i + 1);
1139
+ const s32* ln3 = idx_rp2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp2) : tmp;
1140
+
1141
+ size_t x = 0;
1142
+ for (; x <= colsn - 4; x += 4)
1143
+ {
1144
+ internal::prefetch(ln1 + x);
1145
+ internal::prefetch(ln2 + x);
1146
+ internal::prefetch(ln0 + x);
1147
+ internal::prefetch(ln3 + x);
1148
+ box3x3s32_vert:
1149
+ int32x4_t v1 = vld1q_s32(ln1 + x);
1150
+ int32x4_t v2 = vld1q_s32(ln2 + x);
1151
+ int32x4_t v0 = vld1q_s32(ln0 + x);
1152
+ int32x4_t v3 = vld1q_s32(ln3 + x);
1153
+
1154
+ int32x4_t v = vaddq_s32(v1, v2);
1155
+ int32x4_t w0 = vaddq_s32(v, v0);
1156
+ int32x4_t w1 = vaddq_s32(v, v3);
1157
+
1158
+ vst1q_s32(lanea + x, w0);
1159
+ vst1q_s32(laneb + x, w1);
1160
+ }
1161
+ if(x < colsn)
1162
+ {
1163
+ x = colsn-4;
1164
+ goto box3x3s32_vert;
1165
+ }
1166
+
1167
+ //left&right borders
1168
+ if (borderType != BORDER_MODE_CONSTANT)
1169
+ for (s32 k = 0; k < cn; ++k)
1170
+ {
1171
+ lanea[-cn+k] = lanea[idx_l + k];
1172
+ lanea[colsn+k] = lanea[idx_r + k];
1173
+ laneb[-cn+k] = laneb[idx_l + k];
1174
+ laneb[colsn+k] = laneb[idx_r + k];
1175
+ }
1176
+
1177
+ //horizontal convolution (2 lines from previous iteration)
1178
+ if (i > 0)
1179
+ {
1180
+ s32* dstb = internal::getRowPtr(dstBase, dstStride, i-1);
1181
+ x = 0;
1182
+ for (; x <= colsn - 4; x += 4)
1183
+ {
1184
+ internal::prefetch(laneA + x + cn);
1185
+ internal::prefetch(laneB + x + cn);
1186
+ box3x3s32_horiz:
1187
+ int32x4_t lane0a = vld1q_s32(laneA + x - cn);
1188
+ int32x4_t lane2a = vld1q_s32(laneA + x + cn);
1189
+ int32x4_t lane1a = vld1q_s32(laneA + x);
1190
+
1191
+ int32x4_t lane0b = vld1q_s32(laneB + x - cn);
1192
+ int32x4_t lane2b = vld1q_s32(laneB + x + cn);
1193
+ int32x4_t lane1b = vld1q_s32(laneB + x);
1194
+
1195
+ int32x4_t va = vaddq_s32(lane0a, lane2a);
1196
+ int32x4_t vb = vaddq_s32(lane0b, lane2b);
1197
+ int32x4_t wa = vaddq_s32(va, lane1a);
1198
+ int32x4_t wb = vaddq_s32(vb, lane1b);
1199
+
1200
+ vst1q_s32(dsta + x, wa);
1201
+ vst1q_s32(dstb + x, wb);
1202
+ }
1203
+ if(x < colsn)
1204
+ {
1205
+ x = colsn-4;
1206
+ goto box3x3s32_horiz;
1207
+ }
1208
+ dsta = internal::getRowPtr(dstBase, dstStride, i);
1209
+ }
1210
+
1211
+ std::swap(lanea, laneA);
1212
+ std::swap(laneb, laneB);
1213
+ }
1214
+ //last line
1215
+ if(i < size.height)
1216
+ {
1217
+ //vertical convolution
1218
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
1219
+ ptrdiff_t idx_rp1 = internal::borderInterpolate(i + 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
1220
+
1221
+ const s32* ln0 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
1222
+ const s32* ln1 = internal::getRowPtr(srcBase, srcStride, i);
1223
+ const s32* ln2 = idx_rp1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp1) : tmp;
1224
+
1225
+ size_t x = 0;
1226
+ for (; x <= colsn - 4; x += 4)
1227
+ {
1228
+ internal::prefetch(ln0 + x);
1229
+ internal::prefetch(ln1 + x);
1230
+ internal::prefetch(ln2 + x);
1231
+ box3x3s32_vert_ll:
1232
+ int32x4_t v0 = vld1q_s32(ln0+x);
1233
+ int32x4_t v1 = vld1q_s32(ln1+x);
1234
+ int32x4_t v2 = vld1q_s32(ln2+x);
1235
+
1236
+ int32x4_t v = vaddq_s32(v0, v1);
1237
+ int32x4_t w = vaddq_s32(v, v2);
1238
+
1239
+ vst1q_s32(lanea + x, w);
1240
+ }
1241
+ if(x < colsn)
1242
+ {
1243
+ x = colsn-4;
1244
+ goto box3x3s32_vert_ll;
1245
+ }
1246
+
1247
+ //left&right borders
1248
+ if (borderType != BORDER_MODE_CONSTANT)
1249
+ for (s32 k = 0; k < cn; ++k)
1250
+ {
1251
+ lanea[-cn+k] = lanea[idx_l + k];
1252
+ lanea[colsn+k] = lanea[idx_r + k];
1253
+ }
1254
+
1255
+ //horizontal convolution (last 3 lines)
1256
+ x = 0;
1257
+ s32* dstb = internal::getRowPtr(dstBase, dstStride, i-1);
1258
+ s32* dstc = internal::getRowPtr(dstBase, dstStride, i);
1259
+ for (; x <= colsn - 4; x += 4)
1260
+ {
1261
+ internal::prefetch(laneA + x + cn);
1262
+ internal::prefetch(laneB + x + cn);
1263
+ internal::prefetch(lanea + x + cn);
1264
+ box3x3s32_horiz_ll:
1265
+ int32x4_t lane0a = vld1q_s32(laneA + x - cn);
1266
+ int32x4_t lane2a = vld1q_s32(laneA + x + cn);
1267
+ int32x4_t lane1a = vld1q_s32(laneA + x);
1268
+
1269
+ int32x4_t lane0b = vld1q_s32(laneB + x - cn);
1270
+ int32x4_t lane2b = vld1q_s32(laneB + x + cn);
1271
+ int32x4_t lane1b = vld1q_s32(laneB + x);
1272
+
1273
+ int32x4_t lane0c = vld1q_s32(lanea + x - cn);
1274
+ int32x4_t lane2c = vld1q_s32(lanea + x + cn);
1275
+ int32x4_t lane1c = vld1q_s32(lanea + x);
1276
+
1277
+ int32x4_t va = vaddq_s32(lane0a, lane2a);
1278
+ int32x4_t vb = vaddq_s32(lane0b, lane2b);
1279
+ int32x4_t vc = vaddq_s32(lane0c, lane2c);
1280
+ int32x4_t wa = vaddq_s32(va, lane1a);
1281
+ int32x4_t wb = vaddq_s32(vb, lane1b);
1282
+ int32x4_t wc = vaddq_s32(vc, lane1c);
1283
+
1284
+ vst1q_s32(dsta + x, wa);
1285
+ vst1q_s32(dstb + x, wb);
1286
+ vst1q_s32(dstc + x, wc);
1287
+ }
1288
+ if(x < colsn)
1289
+ {
1290
+ x = colsn-4;
1291
+ goto box3x3s32_horiz_ll;
1292
+ }
1293
+ }
1294
+ else
1295
+ {
1296
+ //horizontal convolution (last 2 lines)
1297
+ s32* dstb = internal::getRowPtr(dstBase, dstStride, i-1);
1298
+ size_t x = 0;
1299
+ for (; x <= colsn - 4; x += 4)
1300
+ {
1301
+ internal::prefetch(laneA + x + cn);
1302
+ internal::prefetch(laneB + x + cn);
1303
+ box3x3s32_horiz_last2:
1304
+ int32x4_t lane0a = vld1q_s32(laneA + x - cn);
1305
+ int32x4_t lane2a = vld1q_s32(laneA + x + cn);
1306
+ int32x4_t lane1a = vld1q_s32(laneA + x);
1307
+
1308
+ int32x4_t lane0b = vld1q_s32(laneB + x - cn);
1309
+ int32x4_t lane2b = vld1q_s32(laneB + x + cn);
1310
+ int32x4_t lane1b = vld1q_s32(laneB + x);
1311
+
1312
+ int32x4_t va = vaddq_s32(lane0a, lane2a);
1313
+ int32x4_t vb = vaddq_s32(lane0b, lane2b);
1314
+ int32x4_t wa = vaddq_s32(va, lane1a);
1315
+ int32x4_t wb = vaddq_s32(vb, lane1b);
1316
+
1317
+ vst1q_s32(dsta + x, wa);
1318
+ vst1q_s32(dstb + x, wb);
1319
+ }
1320
+ if(x < colsn)
1321
+ {
1322
+ x = colsn-4;
1323
+ goto box3x3s32_horiz_last2;
1324
+ }
1325
+ }
1326
+ #else
1327
+ (void)srcBase;
1328
+ (void)srcStride;
1329
+ (void)dstBase;
1330
+ (void)dstStride;
1331
+ (void)borderValue;
1332
+ (void)borderMargin;
1333
+ #endif
1334
+ }
1335
+
1336
+ } //namespace CAROTENE_NS
3rdparty/carotene/src/canny.cpp ADDED
@@ -0,0 +1,773 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include "saturate_cast.hpp"
43
+ #include <vector>
44
+ #include <cstring>
45
+
46
+ namespace CAROTENE_NS {
47
+
48
+ #ifdef CAROTENE_NEON
49
+ namespace {
50
+ struct RowFilter3x3Canny
51
+ {
52
+ inline RowFilter3x3Canny(const ptrdiff_t borderxl, const ptrdiff_t borderxr)
53
+ {
54
+ vfmask = vreinterpret_u8_u64(vmov_n_u64(borderxl ? 0x0000FFffFFffFFffULL : 0x0100FFffFFffFFffULL));
55
+ vtmask = vreinterpret_u8_u64(vmov_n_u64(borderxr ? 0x0707060504030201ULL : 0x0706050403020100ULL));
56
+ lookLeft = offsetk - borderxl;
57
+ lookRight = offsetk - borderxr;
58
+ }
59
+
60
+ inline void operator()(const u8* src, s16* dstx, s16* dsty, ptrdiff_t width)
61
+ {
62
+ uint8x8_t l = vtbl1_u8(vld1_u8(src - lookLeft), vfmask);
63
+ ptrdiff_t i = 0;
64
+ for (; i < width - 8 + lookRight; i += 8)
65
+ {
66
+ internal::prefetch(src + i);
67
+ uint8x8_t l18u = vld1_u8(src + i + 1);
68
+
69
+ uint8x8_t l2 = l18u;
70
+ uint8x8_t l0 = vext_u8(l, l18u, 6);
71
+ int16x8_t l1x2 = vreinterpretq_s16_u16(vshll_n_u8(vext_u8(l, l18u, 7), 1));
72
+
73
+ l = l18u;
74
+
75
+ int16x8_t l02 = vreinterpretq_s16_u16(vaddl_u8(l2, l0));
76
+ int16x8_t ldx = vreinterpretq_s16_u16(vsubl_u8(l2, l0));
77
+ int16x8_t ldy = vaddq_s16(l02, l1x2);
78
+
79
+ vst1q_s16(dstx + i, ldx);
80
+ vst1q_s16(dsty + i, ldy);
81
+ }
82
+
83
+ //tail
84
+ if (lookRight == 0 || i != width)
85
+ {
86
+ uint8x8_t tail0 = vld1_u8(src + (width - 9));//can't get left 1 pixel another way if width==8*k+1
87
+ uint8x8_t tail2 = vtbl1_u8(vld1_u8(src + (width - 8 + lookRight)), vtmask);
88
+ uint8x8_t tail1 = vext_u8(vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(tail0), 8*6)), tail2, 7);
89
+
90
+ int16x8_t tail02 = vreinterpretq_s16_u16(vaddl_u8(tail2, tail0));
91
+ int16x8_t tail1x2 = vreinterpretq_s16_u16(vshll_n_u8(tail1, 1));
92
+ int16x8_t taildx = vreinterpretq_s16_u16(vsubl_u8(tail2, tail0));
93
+ int16x8_t taildy = vqaddq_s16(tail02, tail1x2);
94
+
95
+ vst1q_s16(dstx + (width - 8), taildx);
96
+ vst1q_s16(dsty + (width - 8), taildy);
97
+ }
98
+ }
99
+
100
+ uint8x8_t vfmask;
101
+ uint8x8_t vtmask;
102
+ enum { offsetk = 1};
103
+ ptrdiff_t lookLeft;
104
+ ptrdiff_t lookRight;
105
+ };
106
+
107
+ template <bool L2gradient>
108
+ inline void ColFilter3x3Canny(const s16* src0, const s16* src1, const s16* src2, s16* dstx, s16* dsty, s32* mag, ptrdiff_t width)
109
+ {
110
+ ptrdiff_t j = 0;
111
+ for (; j <= width - 8; j += 8)
112
+ {
113
+ ColFilter3x3CannyL1Loop:
114
+ int16x8_t line0x = vld1q_s16(src0 + j);
115
+ int16x8_t line1x = vld1q_s16(src1 + j);
116
+ int16x8_t line2x = vld1q_s16(src2 + j);
117
+ int16x8_t line0y = vld1q_s16(src0 + j + width);
118
+ int16x8_t line2y = vld1q_s16(src2 + j + width);
119
+
120
+ int16x8_t l02 = vaddq_s16(line0x, line2x);
121
+ int16x8_t l1x2 = vshlq_n_s16(line1x, 1);
122
+ int16x8_t dy = vsubq_s16(line2y, line0y);
123
+ int16x8_t dx = vaddq_s16(l1x2, l02);
124
+
125
+ int16x8_t dya = vabsq_s16(dy);
126
+ int16x8_t dxa = vabsq_s16(dx);
127
+ int16x8_t norm = vaddq_s16(dya, dxa);
128
+
129
+ int32x4_t normh = vmovl_s16(vget_high_s16(norm));
130
+ int32x4_t norml = vmovl_s16(vget_low_s16(norm));
131
+
132
+ vst1q_s16(dsty + j, dy);
133
+ vst1q_s16(dstx + j, dx);
134
+ vst1q_s32(mag + j + 4, normh);
135
+ vst1q_s32(mag + j, norml);
136
+ }
137
+ if (j != width)
138
+ {
139
+ j = width - 8;
140
+ goto ColFilter3x3CannyL1Loop;
141
+ }
142
+ }
143
+ template <>
144
+ inline void ColFilter3x3Canny<true>(const s16* src0, const s16* src1, const s16* src2, s16* dstx, s16* dsty, s32* mag, ptrdiff_t width)
145
+ {
146
+ ptrdiff_t j = 0;
147
+ for (; j <= width - 8; j += 8)
148
+ {
149
+ ColFilter3x3CannyL2Loop:
150
+ int16x8_t line0x = vld1q_s16(src0 + j);
151
+ int16x8_t line1x = vld1q_s16(src1 + j);
152
+ int16x8_t line2x = vld1q_s16(src2 + j);
153
+ int16x8_t line0y = vld1q_s16(src0 + j + width);
154
+ int16x8_t line2y = vld1q_s16(src2 + j + width);
155
+
156
+ int16x8_t l02 = vaddq_s16(line0x, line2x);
157
+ int16x8_t l1x2 = vshlq_n_s16(line1x, 1);
158
+ int16x8_t dy = vsubq_s16(line2y, line0y);
159
+ int16x8_t dx = vaddq_s16(l1x2, l02);
160
+
161
+ int32x4_t norml = vmull_s16(vget_low_s16(dx), vget_low_s16(dx));
162
+ int32x4_t normh = vmull_s16(vget_high_s16(dy), vget_high_s16(dy));
163
+
164
+ norml = vmlal_s16(norml, vget_low_s16(dy), vget_low_s16(dy));
165
+ normh = vmlal_s16(normh, vget_high_s16(dx), vget_high_s16(dx));
166
+
167
+ vst1q_s16(dsty + j, dy);
168
+ vst1q_s16(dstx + j, dx);
169
+ vst1q_s32(mag + j, norml);
170
+ vst1q_s32(mag + j + 4, normh);
171
+ }
172
+ if (j != width)
173
+ {
174
+ j = width - 8;
175
+ goto ColFilter3x3CannyL2Loop;
176
+ }
177
+ }
178
+
179
+ template <bool L2gradient>
180
+ inline void NormCanny(const ptrdiff_t colscn, s16* _dx, s16* _dy, s32* _norm)
181
+ {
182
+ ptrdiff_t j = 0;
183
+ if (colscn >= 8)
184
+ {
185
+ int16x8_t vx = vld1q_s16(_dx);
186
+ int16x8_t vy = vld1q_s16(_dy);
187
+ for (; j <= colscn - 16; j+=8)
188
+ {
189
+ internal::prefetch(_dx);
190
+ internal::prefetch(_dy);
191
+
192
+ int16x8_t vx2 = vld1q_s16(_dx + j + 8);
193
+ int16x8_t vy2 = vld1q_s16(_dy + j + 8);
194
+
195
+ int16x8_t vabsx = vabsq_s16(vx);
196
+ int16x8_t vabsy = vabsq_s16(vy);
197
+
198
+ int16x8_t norm = vaddq_s16(vabsx, vabsy);
199
+
200
+ int32x4_t normh = vmovl_s16(vget_high_s16(norm));
201
+ int32x4_t norml = vmovl_s16(vget_low_s16(norm));
202
+
203
+ vst1q_s32(_norm + j + 4, normh);
204
+ vst1q_s32(_norm + j + 0, norml);
205
+
206
+ vx = vx2;
207
+ vy = vy2;
208
+ }
209
+ int16x8_t vabsx = vabsq_s16(vx);
210
+ int16x8_t vabsy = vabsq_s16(vy);
211
+
212
+ int16x8_t norm = vaddq_s16(vabsx, vabsy);
213
+
214
+ int32x4_t normh = vmovl_s16(vget_high_s16(norm));
215
+ int32x4_t norml = vmovl_s16(vget_low_s16(norm));
216
+
217
+ vst1q_s32(_norm + j + 4, normh);
218
+ vst1q_s32(_norm + j + 0, norml);
219
+ }
220
+ for (; j < colscn; j++)
221
+ _norm[j] = std::abs(s32(_dx[j])) + std::abs(s32(_dy[j]));
222
+ }
223
+
224
+ template <>
225
+ inline void NormCanny<true>(const ptrdiff_t colscn, s16* _dx, s16* _dy, s32* _norm)
226
+ {
227
+ ptrdiff_t j = 0;
228
+ if (colscn >= 8)
229
+ {
230
+ int16x8_t vx = vld1q_s16(_dx);
231
+ int16x8_t vy = vld1q_s16(_dy);
232
+
233
+ for (; j <= colscn - 16; j+=8)
234
+ {
235
+ internal::prefetch(_dx);
236
+ internal::prefetch(_dy);
237
+
238
+ int16x8_t vxnext = vld1q_s16(_dx + j + 8);
239
+ int16x8_t vynext = vld1q_s16(_dy + j + 8);
240
+
241
+ int32x4_t norml = vmull_s16(vget_low_s16(vx), vget_low_s16(vx));
242
+ int32x4_t normh = vmull_s16(vget_high_s16(vy), vget_high_s16(vy));
243
+
244
+ norml = vmlal_s16(norml, vget_low_s16(vy), vget_low_s16(vy));
245
+ normh = vmlal_s16(normh, vget_high_s16(vx), vget_high_s16(vx));
246
+
247
+ vst1q_s32(_norm + j + 0, norml);
248
+ vst1q_s32(_norm + j + 4, normh);
249
+
250
+ vx = vxnext;
251
+ vy = vynext;
252
+ }
253
+ int32x4_t norml = vmull_s16(vget_low_s16(vx), vget_low_s16(vx));
254
+ int32x4_t normh = vmull_s16(vget_high_s16(vy), vget_high_s16(vy));
255
+
256
+ norml = vmlal_s16(norml, vget_low_s16(vy), vget_low_s16(vy));
257
+ normh = vmlal_s16(normh, vget_high_s16(vx), vget_high_s16(vx));
258
+
259
+ vst1q_s32(_norm + j + 0, norml);
260
+ vst1q_s32(_norm + j + 4, normh);
261
+ }
262
+ for (; j < colscn; j++)
263
+ _norm[j] = s32(_dx[j])*_dx[j] + s32(_dy[j])*_dy[j];
264
+ }
265
+
266
+ template <bool L2gradient>
267
+ inline void prepareThresh(f64 low_thresh, f64 high_thresh,
268
+ s32 &low, s32 &high)
269
+ {
270
+ if (low_thresh > high_thresh)
271
+ std::swap(low_thresh, high_thresh);
272
+ #if defined __GNUC__
273
+ low = (s32)low_thresh;
274
+ high = (s32)high_thresh;
275
+ low -= (low > low_thresh);
276
+ high -= (high > high_thresh);
277
+ #else
278
+ low = internal::round(low_thresh);
279
+ high = internal::round(high_thresh);
280
+ f32 ldiff = (f32)(low_thresh - low);
281
+ f32 hdiff = (f32)(high_thresh - high);
282
+ low -= (ldiff < 0);
283
+ high -= (hdiff < 0);
284
+ #endif
285
+ }
286
+ template <>
287
+ inline void prepareThresh<true>(f64 low_thresh, f64 high_thresh,
288
+ s32 &low, s32 &high)
289
+ {
290
+ if (low_thresh > high_thresh)
291
+ std::swap(low_thresh, high_thresh);
292
+ if (low_thresh > 0) low_thresh *= low_thresh;
293
+ if (high_thresh > 0) high_thresh *= high_thresh;
294
+ #if defined __GNUC__
295
+ low = (s32)low_thresh;
296
+ high = (s32)high_thresh;
297
+ low -= (low > low_thresh);
298
+ high -= (high > high_thresh);
299
+ #else
300
+ low = internal::round(low_thresh);
301
+ high = internal::round(high_thresh);
302
+ f32 ldiff = (f32)(low_thresh - low);
303
+ f32 hdiff = (f32)(high_thresh - high);
304
+ low -= (ldiff < 0);
305
+ high -= (hdiff < 0);
306
+ #endif
307
+ }
308
+
309
+ template <bool L2gradient, bool externalSobel>
310
+ struct _normEstimator
311
+ {
312
+ ptrdiff_t magstep;
313
+ ptrdiff_t dxOffset;
314
+ ptrdiff_t dyOffset;
315
+ ptrdiff_t shxOffset;
316
+ ptrdiff_t shyOffset;
317
+ std::vector<u8> buffer;
318
+ const ptrdiff_t offsetk;
319
+ ptrdiff_t borderyt, borderyb;
320
+ RowFilter3x3Canny sobelRow;
321
+
322
+ inline _normEstimator(const Size2D &size, s32, Margin borderMargin,
323
+ ptrdiff_t &mapstep, s32** mag_buf, u8* &map):
324
+ offsetk(1),
325
+ sobelRow(std::max<ptrdiff_t>(0, offsetk - (ptrdiff_t)borderMargin.left),
326
+ std::max<ptrdiff_t>(0, offsetk - (ptrdiff_t)borderMargin.right))
327
+ {
328
+ mapstep = size.width + 2;
329
+ magstep = size.width + 2 + size.width * (4 * sizeof(s16)/sizeof(s32));
330
+ dxOffset = mapstep * sizeof(s32)/sizeof(s16);
331
+ dyOffset = dxOffset + size.width * 1;
332
+ shxOffset = dxOffset + size.width * 2;
333
+ shyOffset = dxOffset + size.width * 3;
334
+ buffer.resize( (size.width+2)*(size.height+2) + magstep*3*sizeof(s32) );
335
+ mag_buf[0] = (s32*)&buffer[0];
336
+ mag_buf[1] = mag_buf[0] + magstep;
337
+ mag_buf[2] = mag_buf[1] + magstep;
338
+ memset(mag_buf[0], 0, mapstep * sizeof(s32));
339
+
340
+ map = (u8*)(mag_buf[2] + magstep);
341
+ memset(map, 1, mapstep);
342
+ memset(map + mapstep*(size.height + 1), 1, mapstep);
343
+ borderyt = std::max<ptrdiff_t>(0, offsetk - (ptrdiff_t)borderMargin.top);
344
+ borderyb = std::max<ptrdiff_t>(0, offsetk - (ptrdiff_t)borderMargin.bottom);
345
+ }
346
+ inline void firstRow(const Size2D &size, s32,
347
+ const u8 *srcBase, ptrdiff_t srcStride,
348
+ s16*, ptrdiff_t,
349
+ s16*, ptrdiff_t,
350
+ s32** mag_buf)
351
+ {
352
+ //sobelH row #0
353
+ const u8* _src = internal::getRowPtr(srcBase, srcStride, 0);
354
+ sobelRow(_src, ((s16*)mag_buf[0]) + shxOffset, ((s16*)mag_buf[0]) + shyOffset, size.width);
355
+ //sobelH row #1
356
+ _src = internal::getRowPtr(srcBase, srcStride, 1);
357
+ sobelRow(_src, ((s16*)mag_buf[1]) + shxOffset, ((s16*)mag_buf[1]) + shyOffset, size.width);
358
+
359
+ mag_buf[1][0] = mag_buf[1][size.width+1] = 0;
360
+ if (borderyt == 0)
361
+ {
362
+ //sobelH row #-1
363
+ _src = internal::getRowPtr(srcBase, srcStride, -1);
364
+ sobelRow(_src, ((s16*)mag_buf[2]) + shxOffset, ((s16*)mag_buf[2]) + shyOffset, size.width);
365
+
366
+ ColFilter3x3Canny<L2gradient>( ((s16*)mag_buf[2]) + shxOffset, ((s16*)mag_buf[0]) + shxOffset, ((s16*)mag_buf[1]) + shxOffset,
367
+ ((s16*)mag_buf[1]) + dxOffset, ((s16*)mag_buf[1]) + dyOffset, mag_buf[1] + 1, size.width);
368
+ }
369
+ else
370
+ {
371
+ ColFilter3x3Canny<L2gradient>( ((s16*)mag_buf[0]) + shxOffset, ((s16*)mag_buf[0]) + shxOffset, ((s16*)mag_buf[1]) + shxOffset,
372
+ ((s16*)mag_buf[1]) + dxOffset, ((s16*)mag_buf[1]) + dyOffset, mag_buf[1] + 1, size.width);
373
+ }
374
+ }
375
+ inline void nextRow(const Size2D &size, s32,
376
+ const u8 *srcBase, ptrdiff_t srcStride,
377
+ s16*, ptrdiff_t,
378
+ s16*, ptrdiff_t,
379
+ const ptrdiff_t &mapstep, s32** mag_buf,
380
+ size_t i, const s16* &_x, const s16* &_y)
381
+ {
382
+ mag_buf[2][0] = mag_buf[2][size.width+1] = 0;
383
+ if (i < size.height - borderyb)
384
+ {
385
+ const u8* _src = internal::getRowPtr(srcBase, srcStride, i+1);
386
+ //sobelH row #i+1
387
+ sobelRow(_src, ((s16*)mag_buf[2]) + shxOffset, ((s16*)mag_buf[2]) + shyOffset, size.width);
388
+
389
+ ColFilter3x3Canny<L2gradient>( ((s16*)mag_buf[0]) + shxOffset, ((s16*)mag_buf[1]) + shxOffset, ((s16*)mag_buf[2]) + shxOffset,
390
+ ((s16*)mag_buf[2]) + dxOffset, ((s16*)mag_buf[2]) + dyOffset, mag_buf[2] + 1, size.width);
391
+ }
392
+ else if (i < size.height)
393
+ {
394
+ ColFilter3x3Canny<L2gradient>( ((s16*)mag_buf[0]) + shxOffset, ((s16*)mag_buf[1]) + shxOffset, ((s16*)mag_buf[1]) + shxOffset,
395
+ ((s16*)mag_buf[2]) + dxOffset, ((s16*)mag_buf[2]) + dyOffset, mag_buf[2] + 1, size.width);
396
+ }
397
+ else
398
+ memset(mag_buf[2], 0, mapstep*sizeof(s32));
399
+ _x = ((s16*)mag_buf[1]) + dxOffset;
400
+ _y = ((s16*)mag_buf[1]) + dyOffset;
401
+ }
402
+ };
403
+ template <bool L2gradient>
404
+ struct _normEstimator<L2gradient, true>
405
+ {
406
+ std::vector<u8> buffer;
407
+
408
+ inline _normEstimator(const Size2D &size, s32 cn, Margin,
409
+ ptrdiff_t &mapstep, s32** mag_buf, u8* &map)
410
+ {
411
+ mapstep = size.width + 2;
412
+ buffer.resize( (size.width+2)*(size.height+2) + cn*mapstep*3*sizeof(s32) );
413
+ mag_buf[0] = (s32*)&buffer[0];
414
+ mag_buf[1] = mag_buf[0] + mapstep*cn;
415
+ mag_buf[2] = mag_buf[1] + mapstep*cn;
416
+ memset(mag_buf[0], 0, /* cn* */mapstep * sizeof(s32));
417
+
418
+ map = (u8*)(mag_buf[2] + mapstep*cn);
419
+ memset(map, 1, mapstep);
420
+ memset(map + mapstep*(size.height + 1), 1, mapstep);
421
+ }
422
+ inline void firstRow(const Size2D &size, s32 cn,
423
+ const u8 *, ptrdiff_t,
424
+ s16* dxBase, ptrdiff_t dxStride,
425
+ s16* dyBase, ptrdiff_t dyStride,
426
+ s32** mag_buf)
427
+ {
428
+ s32* _norm = mag_buf[1] + 1;
429
+
430
+ s16* _dx = internal::getRowPtr(dxBase, dxStride, 0);
431
+ s16* _dy = internal::getRowPtr(dyBase, dyStride, 0);
432
+
433
+ NormCanny<L2gradient>(size.width*cn, _dx, _dy, _norm);
434
+
435
+ if(cn > 1)
436
+ {
437
+ for(size_t j = 0, jn = 0; j < size.width; ++j, jn += cn)
438
+ {
439
+ size_t maxIdx = jn;
440
+ for(s32 k = 1; k < cn; ++k)
441
+ if(_norm[jn + k] > _norm[maxIdx]) maxIdx = jn + k;
442
+ _norm[j] = _norm[maxIdx];
443
+ _dx[j] = _dx[maxIdx];
444
+ _dy[j] = _dy[maxIdx];
445
+ }
446
+ }
447
+
448
+ _norm[-1] = _norm[size.width] = 0;
449
+ }
450
+ inline void nextRow(const Size2D &size, s32 cn,
451
+ const u8 *, ptrdiff_t,
452
+ s16* dxBase, ptrdiff_t dxStride,
453
+ s16* dyBase, ptrdiff_t dyStride,
454
+ const ptrdiff_t &mapstep, s32** mag_buf,
455
+ size_t i, const s16* &_x, const s16* &_y)
456
+ {
457
+ s32* _norm = mag_buf[(i > 0) + 1] + 1;
458
+ if (i < size.height)
459
+ {
460
+ s16* _dx = internal::getRowPtr(dxBase, dxStride, i);
461
+ s16* _dy = internal::getRowPtr(dyBase, dyStride, i);
462
+
463
+ NormCanny<L2gradient>(size.width*cn, _dx, _dy, _norm);
464
+
465
+ if(cn > 1)
466
+ {
467
+ for(size_t j = 0, jn = 0; j < size.width; ++j, jn += cn)
468
+ {
469
+ size_t maxIdx = jn;
470
+ for(s32 k = 1; k < cn; ++k)
471
+ if(_norm[jn + k] > _norm[maxIdx]) maxIdx = jn + k;
472
+ _norm[j] = _norm[maxIdx];
473
+ _dx[j] = _dx[maxIdx];
474
+ _dy[j] = _dy[maxIdx];
475
+ }
476
+ }
477
+
478
+ _norm[-1] = _norm[size.width] = 0;
479
+ }
480
+ else
481
+ memset(_norm-1, 0, /* cn* */mapstep*sizeof(s32));
482
+
483
+ _x = internal::getRowPtr(dxBase, dxStride, i-1);
484
+ _y = internal::getRowPtr(dyBase, dyStride, i-1);
485
+ }
486
+ };
487
+
488
+ template <bool L2gradient, bool externalSobel>
489
+ inline void Canny3x3(const Size2D &size, s32 cn,
490
+ const u8 * srcBase, ptrdiff_t srcStride,
491
+ u8 * dstBase, ptrdiff_t dstStride,
492
+ s16 * dxBase, ptrdiff_t dxStride,
493
+ s16 * dyBase, ptrdiff_t dyStride,
494
+ f64 low_thresh, f64 high_thresh,
495
+ Margin borderMargin)
496
+ {
497
+ s32 low, high;
498
+ prepareThresh<L2gradient>(low_thresh, high_thresh, low, high);
499
+
500
+ ptrdiff_t mapstep;
501
+ s32* mag_buf[3];
502
+ u8* map;
503
+ _normEstimator<L2gradient, externalSobel> normEstimator(size, cn, borderMargin, mapstep, mag_buf, map);
504
+
505
+ size_t maxsize = std::max<size_t>( 1u << 10, size.width * size.height / 10 );
506
+ std::vector<u8*> stack( maxsize );
507
+ u8 **stack_top = &stack[0];
508
+ u8 **stack_bottom = &stack[0];
509
+
510
+ /* sector numbers
511
+ (Top-Left Origin)
512
+
513
+ 1 2 3
514
+ * * *
515
+ * * *
516
+ 0*******0
517
+ * * *
518
+ * * *
519
+ 3 2 1
520
+ */
521
+
522
+ #define CANNY_PUSH(d) *(d) = u8(2), *stack_top++ = (d)
523
+ #define CANNY_POP(d) (d) = *--stack_top
524
+
525
+ //i == 0
526
+ normEstimator.firstRow(size, cn, srcBase, srcStride, dxBase, dxStride, dyBase, dyStride, mag_buf);
527
+ // calculate magnitude and angle of gradient, perform non-maxima supression.
528
+ // fill the map with one of the following values:
529
+ // 0 - the pixel might belong to an edge
530
+ // 1 - the pixel can not belong to an edge
531
+ // 2 - the pixel does belong to an edge
532
+ for (size_t i = 1; i <= size.height; i++)
533
+ {
534
+ const s16 *_x, *_y;
535
+ normEstimator.nextRow(size, cn, srcBase, srcStride, dxBase, dxStride, dyBase, dyStride, mapstep, mag_buf, i, _x, _y);
536
+
537
+ u8* _map = map + mapstep*i + 1;
538
+ _map[-1] = _map[size.width] = 1;
539
+
540
+ s32* _mag = mag_buf[1] + 1; // take the central row
541
+ ptrdiff_t magstep1 = mag_buf[2] - mag_buf[1];
542
+ ptrdiff_t magstep2 = mag_buf[0] - mag_buf[1];
543
+
544
+ if ((stack_top - stack_bottom) + size.width > maxsize)
545
+ {
546
+ ptrdiff_t sz = (ptrdiff_t)(stack_top - stack_bottom);
547
+ maxsize = maxsize * 3/2;
548
+ stack.resize(maxsize);
549
+ stack_bottom = &stack[0];
550
+ stack_top = stack_bottom + sz;
551
+ }
552
+
553
+ s32 prev_flag = 0;
554
+ for (ptrdiff_t j = 0; j < (ptrdiff_t)size.width; j++)
555
+ {
556
+ #define CANNY_SHIFT 15
557
+ const s32 TG22 = (s32)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5);
558
+
559
+ s32 m = _mag[j];
560
+
561
+ if (m > low)
562
+ {
563
+ s32 xs = _x[j];
564
+ s32 ys = _y[j];
565
+ s32 x = abs(xs);
566
+ s32 y = abs(ys) << CANNY_SHIFT;
567
+
568
+ s32 tg22x = x * TG22;
569
+
570
+ if (y < tg22x)
571
+ {
572
+ if (m > _mag[j-1] && m >= _mag[j+1]) goto __push;
573
+ }
574
+ else
575
+ {
576
+ s32 tg67x = tg22x + (x << (CANNY_SHIFT+1));
577
+ if (y > tg67x)
578
+ {
579
+ if (m > _mag[j+magstep2] && m >= _mag[j+magstep1]) goto __push;
580
+ }
581
+ else
582
+ {
583
+ s32 s = (xs ^ ys) < 0 ? -1 : 1;
584
+ if(m > _mag[j+magstep2-s] && m > _mag[j+magstep1+s]) goto __push;
585
+ }
586
+ }
587
+ }
588
+ prev_flag = 0;
589
+ _map[j] = u8(1);
590
+ continue;
591
+ __push:
592
+ if (!prev_flag && m > high && _map[j-mapstep] != 2)
593
+ {
594
+ CANNY_PUSH(_map + j);
595
+ prev_flag = 1;
596
+ }
597
+ else
598
+ _map[j] = 0;
599
+ }
600
+
601
+ // scroll the ring buffer
602
+ _mag = mag_buf[0];
603
+ mag_buf[0] = mag_buf[1];
604
+ mag_buf[1] = mag_buf[2];
605
+ mag_buf[2] = _mag;
606
+ }
607
+
608
+ // now track the edges (hysteresis thresholding)
609
+ while (stack_top > stack_bottom)
610
+ {
611
+ u8* m;
612
+ if ((size_t)(stack_top - stack_bottom) + 8u > maxsize)
613
+ {
614
+ ptrdiff_t sz = (ptrdiff_t)(stack_top - stack_bottom);
615
+ maxsize = maxsize * 3/2;
616
+ stack.resize(maxsize);
617
+ stack_bottom = &stack[0];
618
+ stack_top = stack_bottom + sz;
619
+ }
620
+
621
+ CANNY_POP(m);
622
+
623
+ if (!m[-1]) CANNY_PUSH(m - 1);
624
+ if (!m[1]) CANNY_PUSH(m + 1);
625
+ if (!m[-mapstep-1]) CANNY_PUSH(m - mapstep - 1);
626
+ if (!m[-mapstep]) CANNY_PUSH(m - mapstep);
627
+ if (!m[-mapstep+1]) CANNY_PUSH(m - mapstep + 1);
628
+ if (!m[mapstep-1]) CANNY_PUSH(m + mapstep - 1);
629
+ if (!m[mapstep]) CANNY_PUSH(m + mapstep);
630
+ if (!m[mapstep+1]) CANNY_PUSH(m + mapstep + 1);
631
+ }
632
+
633
+ // the final pass, form the final image
634
+ uint8x16_t v2 = vmovq_n_u8(2);
635
+ const u8* ptrmap = map + mapstep + 1;
636
+ for (size_t i = 0; i < size.height; i++, ptrmap += mapstep)
637
+ {
638
+ u8* _dst = internal::getRowPtr(dstBase, dstStride, i);
639
+ ptrdiff_t j = 0;
640
+ for (; j < (ptrdiff_t)size.width - 16; j += 16)
641
+ {
642
+ internal::prefetch(ptrmap);
643
+ uint8x16_t vmap = vld1q_u8(ptrmap + j);
644
+ uint8x16_t vdst = vceqq_u8(vmap, v2);
645
+ vst1q_u8(_dst+j, vdst);
646
+ }
647
+ for (; j < (ptrdiff_t)size.width; j++)
648
+ _dst[j] = (u8)-(ptrmap[j] >> 1);
649
+ }
650
+ }
651
+
652
+ } // namespace
653
+ #endif
654
+
655
+ bool isCanny3x3Supported(const Size2D &size)
656
+ {
657
+ return isSupportedConfiguration() &&
658
+ size.height >= 2 && size.width >= 9;
659
+ }
660
+
661
+ void Canny3x3L1(const Size2D &size,
662
+ const u8 * srcBase, ptrdiff_t srcStride,
663
+ u8 * dstBase, ptrdiff_t dstStride,
664
+ f64 low_thresh, f64 high_thresh,
665
+ Margin borderMargin)
666
+ {
667
+ internal::assertSupportedConfiguration(isCanny3x3Supported(size));
668
+ #ifdef CAROTENE_NEON
669
+ Canny3x3<false, false>(size, 1,
670
+ srcBase, srcStride,
671
+ dstBase, dstStride,
672
+ NULL, 0,
673
+ NULL, 0,
674
+ low_thresh, high_thresh,
675
+ borderMargin);
676
+ #else
677
+ (void)size;
678
+ (void)srcBase;
679
+ (void)srcStride;
680
+ (void)dstBase;
681
+ (void)dstStride;
682
+ (void)low_thresh;
683
+ (void)high_thresh;
684
+ (void)borderMargin;
685
+ #endif
686
+ }
687
+
688
+ void Canny3x3L2(const Size2D &size,
689
+ const u8 * srcBase, ptrdiff_t srcStride,
690
+ u8 * dstBase, ptrdiff_t dstStride,
691
+ f64 low_thresh, f64 high_thresh,
692
+ Margin borderMargin)
693
+ {
694
+ internal::assertSupportedConfiguration(isCanny3x3Supported(size));
695
+ #ifdef CAROTENE_NEON
696
+ Canny3x3<true, false>(size, 1,
697
+ srcBase, srcStride,
698
+ dstBase, dstStride,
699
+ NULL, 0,
700
+ NULL, 0,
701
+ low_thresh, high_thresh,
702
+ borderMargin);
703
+ #else
704
+ (void)size;
705
+ (void)srcBase;
706
+ (void)srcStride;
707
+ (void)dstBase;
708
+ (void)dstStride;
709
+ (void)low_thresh;
710
+ (void)high_thresh;
711
+ (void)borderMargin;
712
+ #endif
713
+ }
714
+
715
+ void Canny3x3L1(const Size2D &size, s32 cn,
716
+ s16 * dxBase, ptrdiff_t dxStride,
717
+ s16 * dyBase, ptrdiff_t dyStride,
718
+ u8 * dstBase, ptrdiff_t dstStride,
719
+ f64 low_thresh, f64 high_thresh)
720
+ {
721
+ internal::assertSupportedConfiguration();
722
+ #ifdef CAROTENE_NEON
723
+ Canny3x3<false, true>(size, cn,
724
+ NULL, 0,
725
+ dstBase, dstStride,
726
+ dxBase, dxStride,
727
+ dyBase, dyStride,
728
+ low_thresh, high_thresh,
729
+ Margin());
730
+ #else
731
+ (void)size;
732
+ (void)cn;
733
+ (void)dstBase;
734
+ (void)dstStride;
735
+ (void)dxBase;
736
+ (void)dxStride;
737
+ (void)dyBase;
738
+ (void)dyStride;
739
+ (void)low_thresh;
740
+ (void)high_thresh;
741
+ #endif
742
+ }
743
+
744
+ void Canny3x3L2(const Size2D &size, s32 cn,
745
+ s16 * dxBase, ptrdiff_t dxStride,
746
+ s16 * dyBase, ptrdiff_t dyStride,
747
+ u8 * dstBase, ptrdiff_t dstStride,
748
+ f64 low_thresh, f64 high_thresh)
749
+ {
750
+ internal::assertSupportedConfiguration();
751
+ #ifdef CAROTENE_NEON
752
+ Canny3x3<true, true>(size, cn,
753
+ NULL, 0,
754
+ dstBase, dstStride,
755
+ dxBase, dxStride,
756
+ dyBase, dyStride,
757
+ low_thresh, high_thresh,
758
+ Margin());
759
+ #else
760
+ (void)size;
761
+ (void)cn;
762
+ (void)dstBase;
763
+ (void)dstStride;
764
+ (void)dxBase;
765
+ (void)dxStride;
766
+ (void)dyBase;
767
+ (void)dyStride;
768
+ (void)low_thresh;
769
+ (void)high_thresh;
770
+ #endif
771
+ }
772
+
773
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/channel_extract.cpp ADDED
@@ -0,0 +1,486 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ namespace CAROTENE_NS {
44
+
45
+ void extract2(const Size2D &size,
46
+ const u8 * srcBase, ptrdiff_t srcStride,
47
+ u8 * dstBase, ptrdiff_t dstStride,
48
+ u32 coi)
49
+ {
50
+ internal::assertSupportedConfiguration();
51
+ #ifdef CAROTENE_NEON
52
+ #ifndef __ANDROID__
53
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
54
+ #endif
55
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
56
+
57
+ for (size_t i = 0u; i < size.height; ++i)
58
+ {
59
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
60
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
61
+ size_t sj = 0u, dj = 0u;
62
+
63
+ #ifndef __ANDROID__
64
+ for (; dj < roiw32; sj += 64, dj += 32)
65
+ {
66
+ internal::prefetch(src + sj);
67
+
68
+ uint8x16x2_t v_src = vld2q_u8(src + sj);
69
+ vst1q_u8(dst + dj, v_src.val[coi]);
70
+
71
+ v_src = vld2q_u8(src + sj + 32);
72
+ vst1q_u8(dst + dj + 16, v_src.val[coi]);
73
+ }
74
+ #endif
75
+
76
+ for (; dj < roiw8; sj += 16, dj += 8)
77
+ {
78
+ uint8x8x2_t v_src = vld2_u8(src + sj);
79
+ vst1_u8(dst + dj, v_src.val[coi]);
80
+ }
81
+
82
+ for (; dj < size.width; sj += 2, ++dj)
83
+ {
84
+ dst[dj] = src[sj + coi];
85
+ }
86
+ }
87
+ #else
88
+ (void)size;
89
+ (void)srcBase;
90
+ (void)srcStride;
91
+ (void)dstBase;
92
+ (void)dstStride;
93
+ (void)coi;
94
+ #endif
95
+ }
96
+
97
+ void extract3(const Size2D &size,
98
+ const u8 * srcBase, ptrdiff_t srcStride,
99
+ u8 * dstBase, ptrdiff_t dstStride,
100
+ u32 coi)
101
+ {
102
+ internal::assertSupportedConfiguration();
103
+ #ifdef CAROTENE_NEON
104
+ #ifndef __ANDROID__
105
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
106
+ #endif
107
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
108
+
109
+ for (size_t i = 0u; i < size.height; ++i)
110
+ {
111
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
112
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
113
+ size_t sj = 0u, dj = 0u;
114
+
115
+ #ifndef __ANDROID__
116
+ for (; dj < roiw32; sj += 96, dj += 32)
117
+ {
118
+ internal::prefetch(src + sj);
119
+
120
+ uint8x16x3_t v_src = vld3q_u8(src + sj);
121
+ vst1q_u8(dst + dj, v_src.val[coi]);
122
+
123
+ v_src = vld3q_u8(src + sj + 48);
124
+ vst1q_u8(dst + dj + 16, v_src.val[coi]);
125
+ }
126
+ #endif
127
+
128
+ for (; dj < roiw8; sj += 24, dj += 8)
129
+ {
130
+ uint8x8x3_t v_src = vld3_u8(src + sj);
131
+ vst1_u8(dst + dj, v_src.val[coi]);
132
+ }
133
+
134
+ for (; dj < size.width; sj += 3, ++dj)
135
+ {
136
+ dst[dj] = src[sj + coi];
137
+ }
138
+ }
139
+ #else
140
+ (void)size;
141
+ (void)srcBase;
142
+ (void)srcStride;
143
+ (void)dstBase;
144
+ (void)dstStride;
145
+ (void)coi;
146
+ #endif
147
+ }
148
+
149
+ void extract4(const Size2D &size,
150
+ const u8 * srcBase, ptrdiff_t srcStride,
151
+ u8 * dstBase, ptrdiff_t dstStride,
152
+ u32 coi)
153
+ {
154
+ internal::assertSupportedConfiguration();
155
+ #ifdef CAROTENE_NEON
156
+ #ifndef __ANDROID__
157
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
158
+ #endif
159
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
160
+
161
+ for (size_t i = 0u; i < size.height; ++i)
162
+ {
163
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
164
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
165
+ size_t sj = 0u, dj = 0u;
166
+
167
+ #ifndef __ANDROID__
168
+ for (; dj < roiw32; sj += 128, dj += 32)
169
+ {
170
+ internal::prefetch(src + sj);
171
+
172
+ uint8x16x4_t v_src = vld4q_u8(src + sj);
173
+ vst1q_u8(dst + dj, v_src.val[coi]);
174
+
175
+ v_src = vld4q_u8(src + sj + 64);
176
+ vst1q_u8(dst + dj + 16, v_src.val[coi]);
177
+ }
178
+ #endif
179
+
180
+ for (; dj < roiw8; sj += 32, dj += 8)
181
+ {
182
+ uint8x8x4_t v_src = vld4_u8(src + sj);
183
+ vst1_u8(dst + dj, v_src.val[coi]);
184
+ }
185
+
186
+ for (; dj < size.width; sj += 4, ++dj)
187
+ {
188
+ dst[dj] = src[sj + coi];
189
+ }
190
+ }
191
+ #else
192
+ (void)size;
193
+ (void)srcBase;
194
+ (void)srcStride;
195
+ (void)dstBase;
196
+ (void)dstStride;
197
+ (void)coi;
198
+ #endif
199
+ }
200
+
201
+ #define FILL_LINES2(macro,type) \
202
+ macro##_LINE(type,0) \
203
+ macro##_LINE(type,1)
204
+ #define FILL_LINES3(macro,type) \
205
+ FILL_LINES2(macro,type) \
206
+ macro##_LINE(type,2)
207
+ #define FILL_LINES4(macro,type) \
208
+ FILL_LINES3(macro,type) \
209
+ macro##_LINE(type,3)
210
+
211
+ #define FARG_LINE(type, n) , type * dst##n##Base, ptrdiff_t dst##n##Stride
212
+
213
+ #ifdef CAROTENE_NEON
214
+
215
+ #define VROW_LINE(type, n) type * dst##n = internal::getRowPtr(dst##n##Base, dst##n##Stride, i);
216
+ #define VST1Q_LINE(type, n) vst1q_##type(dst##n + dj, v_src.val[n]);
217
+ #define VST1_LINE(type, n) vst1_##type(dst##n + dj, v_src.val[n]);
218
+ #define SST_LINE(type, n) dst##n[dj] = src[sj + n];
219
+
220
+ #define MUL2(val) (val << 1)
221
+ #define MUL3(val) (MUL2(val) + val)
222
+ #define MUL4(val) (val << 2)
223
+
224
+ #define CONTDST2 srcStride == dst0Stride && \
225
+ srcStride == dst1Stride &&
226
+ #define CONTDST3 srcStride == dst0Stride && \
227
+ srcStride == dst1Stride && \
228
+ srcStride == dst2Stride &&
229
+ #define CONTDST4 srcStride == dst0Stride && \
230
+ srcStride == dst1Stride && \
231
+ srcStride == dst2Stride && \
232
+ srcStride == dst3Stride &&
233
+
234
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
235
+
236
+ #define SPLIT_ASM2(sgn, bits) __asm__ ( \
237
+ "vld2." #bits " {d0, d2}, [%[in0]] \n\t" \
238
+ "vld2." #bits " {d1, d3}, [%[in1]] \n\t" \
239
+ "vst1." #bits " {d0-d1}, [%[out0]] \n\t" \
240
+ "vst1." #bits " {d2-d3}, [%[out1]] \n\t" \
241
+ : \
242
+ : [out0] "r" (dst0 + dj), [out1] "r" (dst1 + dj), \
243
+ [in0] "r" (src + sj), [in1] "r" (src + sj + MUL2(8)/sizeof(sgn##bits)) \
244
+ : "d0","d1","d2","d3" \
245
+ );
246
+ #define SPLIT_ASM3(sgn, bits) __asm__ ( \
247
+ "vld3." #bits " {d0, d2, d4}, [%[in0]] \n\t" \
248
+ "vld3." #bits " {d1, d3, d5}, [%[in1]] \n\t" \
249
+ "vst1." #bits " {d0-d1}, [%[out0]] \n\t" \
250
+ "vst1." #bits " {d2-d3}, [%[out1]] \n\t" \
251
+ "vst1." #bits " {d4-d5}, [%[out2]] \n\t" \
252
+ : \
253
+ : [out0] "r" (dst0 + dj), [out1] "r" (dst1 + dj), [out2] "r" (dst2 + dj), \
254
+ [in0] "r" (src + sj), [in1] "r" (src + sj + MUL3(8)/sizeof(sgn##bits)) \
255
+ : "d0","d1","d2","d3","d4","d5" \
256
+ );
257
+ #define SPLIT_ASM4(sgn, bits) __asm__ ( \
258
+ "vld4." #bits " {d0, d2, d4, d6}, [%[in0]] \n\t" \
259
+ "vld4." #bits " {d1, d3, d5, d7}, [%[in1]] \n\t" \
260
+ "vst1." #bits " {d0-d1}, [%[out0]] \n\t" \
261
+ "vst1." #bits " {d2-d3}, [%[out1]] \n\t" \
262
+ "vst1." #bits " {d4-d5}, [%[out2]] \n\t" \
263
+ "vst1." #bits " {d6-d7}, [%[out3]] \n\t" \
264
+ : \
265
+ : [out0] "r" (dst0 + dj), [out1] "r" (dst1 + dj), [out2] "r" (dst2 + dj), [out3] "r" (dst3 + dj), \
266
+ [in0] "r" (src + sj), [in1] "r" (src + sj + MUL4(8)/sizeof(sgn##bits)) \
267
+ : "d0","d1","d2","d3","d4","d5","d6","d7" \
268
+ );
269
+
270
+ #define SPLIT_QUAD(sgn, bits, n) { \
271
+ internal::prefetch(src + sj); \
272
+ SPLIT_ASM##n(sgn, bits) \
273
+ }
274
+
275
+ #else
276
+
277
+ #define SPLIT_QUAD(sgn, bits, n) { \
278
+ internal::prefetch(src + sj); \
279
+ vec128 v_src = vld##n##q_##sgn##bits(src + sj); \
280
+ FILL_LINES##n(VST1Q, sgn##bits) \
281
+ }
282
+
283
+ #endif
284
+
285
+ #define SPLIT(sgn,bits,n) void split##n(const Size2D &_size, \
286
+ const sgn##bits * srcBase, ptrdiff_t srcStride \
287
+ FILL_LINES##n(FARG, sgn##bits) ) \
288
+ { \
289
+ internal::assertSupportedConfiguration(); \
290
+ Size2D size(_size); \
291
+ if (CONTDST##n \
292
+ dst0Stride == (ptrdiff_t)(size.width)) \
293
+ { \
294
+ size.width *= size.height; \
295
+ size.height = 1; \
296
+ } \
297
+ typedef internal::VecTraits<sgn##bits, n>::vec128 vec128; \
298
+ size_t roiw16 = size.width >= (16/sizeof(sgn##bits)-1) ? size.width - (16/sizeof(sgn##bits)-1) : 0; \
299
+ typedef internal::VecTraits<sgn##bits, n>::vec64 vec64; \
300
+ size_t roiw8 = size.width >= (8/sizeof(sgn##bits)-1) ? size.width - (8/sizeof(sgn##bits)-1) : 0; \
301
+ \
302
+ for (size_t i = 0u; i < size.height; ++i) \
303
+ { \
304
+ const sgn##bits * src = internal::getRowPtr(srcBase, srcStride, i); \
305
+ FILL_LINES##n(VROW, sgn##bits) \
306
+ size_t sj = 0u, dj = 0u; \
307
+ \
308
+ for (; dj < roiw16; sj += MUL##n(16)/sizeof(sgn##bits), dj += 16/sizeof(sgn##bits)) \
309
+ SPLIT_QUAD(sgn, bits, n) \
310
+ \
311
+ if (dj < roiw8) \
312
+ { \
313
+ vec64 v_src = vld##n##_##sgn##bits(src + sj); \
314
+ FILL_LINES##n(VST1, sgn##bits) \
315
+ sj += MUL##n(8)/sizeof(sgn##bits); \
316
+ dj += 8/sizeof(sgn##bits); \
317
+ } \
318
+ \
319
+ for (; dj < size.width; sj += n, ++dj) \
320
+ { \
321
+ FILL_LINES##n(SST, sgn##bits) \
322
+ } \
323
+ } \
324
+ }
325
+
326
+ #define SPLIT64(sgn,n) void split##n(const Size2D &_size, \
327
+ const sgn##64 * srcBase, ptrdiff_t srcStride \
328
+ FILL_LINES##n(FARG, sgn##64) ) \
329
+ { \
330
+ internal::assertSupportedConfiguration(); \
331
+ Size2D size(_size); \
332
+ if (CONTDST##n \
333
+ dst0Stride == (ptrdiff_t)(size.width)) \
334
+ { \
335
+ size.width *= size.height; \
336
+ size.height = 1; \
337
+ } \
338
+ typedef internal::VecTraits<sgn##64, n>::vec64 vec64; \
339
+ \
340
+ for (size_t i = 0u; i < size.height; ++i) \
341
+ { \
342
+ const sgn##64 * src = internal::getRowPtr(srcBase, srcStride, i); \
343
+ FILL_LINES##n(VROW, sgn##64) \
344
+ size_t sj = 0u, dj = 0u; \
345
+ \
346
+ for (; dj < size.width; sj += n, ++dj) \
347
+ { \
348
+ vec64 v_src = vld##n##_##sgn##64(src + sj); \
349
+ FILL_LINES##n(VST1, sgn##64) \
350
+ } \
351
+ } \
352
+ }
353
+
354
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
355
+
356
+ #define ALPHA_QUAD(sgn, bits) { \
357
+ internal::prefetch(src + sj); \
358
+ __asm__ ( \
359
+ "vld4." #bits " {d0, d2, d4, d6}, [%[in0]] \n\t" \
360
+ "vld4." #bits " {d1, d3, d5, d7}, [%[in1]] \n\t" \
361
+ "vst3." #bits " {d0, d2, d4}, [%[out3_1]] \n\t" \
362
+ "vst3." #bits " {d1, d3, d5}, [%[out3_2]] \n\t" \
363
+ "vst1." #bits " {d6-d7}, [%[out1]] \n\t" \
364
+ : \
365
+ : [out3_1] "r" (dst3 + d3j), [out3_2] "r" (dst3 + d3j + 24/sizeof(sgn##bits)), [out1] "r" (dst1 + d1j), \
366
+ [in0] "r" (src + sj), [in1] "r" (src + sj + 32/sizeof(sgn##bits)) \
367
+ : "d0","d1","d2","d3","d4","d5","d6","d7" \
368
+ ); \
369
+ }
370
+
371
+ #else
372
+
373
+ #define ALPHA_QUAD(sgn, bits) { \
374
+ internal::prefetch(src + sj); \
375
+ union { vec128_4 v4; vec128_3 v3; } vals; \
376
+ vals.v4 = vld4q_##sgn##bits(src + sj); \
377
+ vst3q_##sgn##bits(dst3 + d3j, vals.v3); \
378
+ vst1q_##sgn##bits(dst1 + d1j, vals.v4.val[3]); \
379
+ }
380
+
381
+ #endif
382
+
383
+ #define SPLIT4ALPHA(sgn,bits) void split4(const Size2D &_size, \
384
+ const sgn##bits * srcBase, ptrdiff_t srcStride, \
385
+ sgn##bits * dst3Base, ptrdiff_t dst3Stride, \
386
+ sgn##bits * dst1Base, ptrdiff_t dst1Stride) \
387
+ { \
388
+ internal::assertSupportedConfiguration(); \
389
+ Size2D size(_size); \
390
+ if (srcStride == dst3Stride && \
391
+ srcStride == dst1Stride && \
392
+ srcStride == (ptrdiff_t)(size.width)) \
393
+ { \
394
+ size.width *= size.height; \
395
+ size.height = 1; \
396
+ } \
397
+ typedef internal::VecTraits<sgn##bits, 4>::vec128 vec128_4; \
398
+ typedef internal::VecTraits<sgn##bits, 3>::vec128 vec128_3; \
399
+ size_t roiw16 = size.width >= (16/sizeof(sgn##bits)-1) ? size.width - (16/sizeof(sgn##bits)-1) : 0; \
400
+ typedef internal::VecTraits<sgn##bits, 4>::vec64 vec64_4; \
401
+ typedef internal::VecTraits<sgn##bits, 3>::vec64 vec64_3; \
402
+ size_t roiw8 = size.width >= (8/sizeof(sgn##bits)-1) ? size.width - (8/sizeof(sgn##bits)-1) : 0; \
403
+ \
404
+ for (size_t i = 0u; i < size.height; ++i) \
405
+ { \
406
+ const sgn##bits * src = internal::getRowPtr(srcBase, srcStride, i); \
407
+ sgn##bits * dst3 = internal::getRowPtr(dst3Base, dst3Stride, i); \
408
+ sgn##bits * dst1 = internal::getRowPtr(dst1Base, dst1Stride, i); \
409
+ size_t sj = 0u, d3j = 0u, d1j = 0u; \
410
+ \
411
+ for (; d1j < roiw16; sj += MUL4(16)/sizeof(sgn##bits), d3j += MUL3(16)/sizeof(sgn##bits), \
412
+ d1j += 16/sizeof(sgn##bits)) \
413
+ ALPHA_QUAD(sgn, bits) \
414
+ \
415
+ if (d1j < roiw8) \
416
+ { \
417
+ union { vec64_4 v4; vec64_3 v3; } vals; \
418
+ vals.v4 = vld4_##sgn##bits(src + sj); \
419
+ vst3_u8(dst3 + d3j, vals.v3); \
420
+ vst1_u8(dst1 + d1j, vals.v4.val[3]); \
421
+ sj += MUL4(8)/sizeof(sgn##bits); \
422
+ d3j += MUL3(8)/sizeof(sgn##bits); \
423
+ d1j += 8/sizeof(sgn##bits); \
424
+ } \
425
+ \
426
+ for (; d1j < size.width; sj += 4, d3j += 3, ++d1j) \
427
+ { \
428
+ dst3[d3j+0] = src[sj + 0]; \
429
+ dst3[d3j+1] = src[sj + 1]; \
430
+ dst3[d3j+2] = src[sj + 2]; \
431
+ dst1[d1j] = src[sj + 3]; \
432
+ } \
433
+ } \
434
+ }
435
+
436
+ #else
437
+
438
+ #define VOID_LINE(type, n) (void)dst##n##Base; (void)dst##n##Stride;
439
+
440
+ #define SPLIT(sgn,bits,n) void split##n(const Size2D &size, \
441
+ const sgn##bits * srcBase, ptrdiff_t srcStride \
442
+ FILL_LINES##n(FARG, sgn##bits) ) \
443
+ { \
444
+ internal::assertSupportedConfiguration(); \
445
+ (void)size; \
446
+ (void)srcBase; \
447
+ (void)srcStride; \
448
+ FILL_LINES##n(VOID, sgn##bits) \
449
+ }
450
+
451
+ #define SPLIT64(sgn,n) SPLIT(sgn,64,n)
452
+
453
+ #define SPLIT4ALPHA(sgn,bits) void split4(const Size2D &size, \
454
+ const sgn##bits * srcBase, ptrdiff_t srcStride, \
455
+ sgn##bits * dst3Base, ptrdiff_t dst3Stride, \
456
+ sgn##bits * dst1Base, ptrdiff_t dst1Stride) \
457
+ { \
458
+ internal::assertSupportedConfiguration(); \
459
+ (void)size; \
460
+ (void)srcBase; \
461
+ (void)srcStride; \
462
+ (void)dst3Base; \
463
+ (void)dst3Stride; \
464
+ (void)dst1Base; \
465
+ (void)dst1Stride; \
466
+ }
467
+
468
+ #endif //CAROTENE_NEON
469
+
470
+ SPLIT(u, 8,2)
471
+ SPLIT(u, 8,3)
472
+ SPLIT(u, 8,4)
473
+ SPLIT(u,16,2)
474
+ SPLIT(u,16,3)
475
+ SPLIT(u,16,4)
476
+ SPLIT(s,32,2)
477
+ SPLIT(s,32,3)
478
+ SPLIT(s,32,4)
479
+
480
+ SPLIT64(s, 2)
481
+ SPLIT64(s, 3)
482
+ SPLIT64(s, 4)
483
+
484
+ SPLIT4ALPHA(u,8)
485
+
486
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/channels_combine.cpp ADDED
@@ -0,0 +1,389 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ namespace CAROTENE_NS {
44
+
45
+ #define FILL_LINES2(macro,type) \
46
+ macro##_LINE(type,0) \
47
+ macro##_LINE(type,1)
48
+ #define FILL_LINES3(macro,type) \
49
+ FILL_LINES2(macro,type) \
50
+ macro##_LINE(type,2)
51
+ #define FILL_LINES4(macro,type) \
52
+ FILL_LINES3(macro,type) \
53
+ macro##_LINE(type,3)
54
+
55
+ #define FARG_LINE(type, n) , const type * src##n##Base, ptrdiff_t src##n##Stride
56
+
57
+ #ifdef CAROTENE_NEON
58
+
59
+ #define VROW_LINE(type, n) const type * src##n = internal::getRowPtr(src##n##Base, src##n##Stride, i);
60
+ #define PREF_LINE(type, n) internal::prefetch(src##n + sj);
61
+ #define VLD1Q_LINE(type, n) v_dst.val[n] = vld1q_##type(src##n + sj);
62
+ #define PRLD_LINE(type, n) internal::prefetch(src##n + sj); v_dst.val[n] = vld1q_##type(src##n + sj);
63
+ #define VLD1_LINE(type, n) v_dst.val[n] = vld1_##type(src##n + sj);
64
+ #define SLD_LINE(type, n) dst[dj + n] = src##n[sj];
65
+
66
+ #define MUL2(val) (val << 1)
67
+ #define MUL3(val) (MUL2(val) + val)
68
+ #define MUL4(val) (val << 2)
69
+
70
+ #define CONTSRC2 dstStride == src0Stride && \
71
+ dstStride == src1Stride &&
72
+ #define CONTSRC3 dstStride == src0Stride && \
73
+ dstStride == src1Stride && \
74
+ dstStride == src2Stride &&
75
+ #define CONTSRC4 dstStride == src0Stride && \
76
+ dstStride == src1Stride && \
77
+ dstStride == src2Stride && \
78
+ dstStride == src3Stride &&
79
+
80
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
81
+
82
+ #define MERGE_ASM2(sgn, bits) __asm__ ( \
83
+ "vld1." #bits " {d0-d1}, [%[in0]] \n\t" \
84
+ "vld1." #bits " {d2-d3}, [%[in1]] \n\t" \
85
+ "vst2." #bits " {d0, d2}, [%[out0]] \n\t" \
86
+ "vst2." #bits " {d1, d3}, [%[out1]] \n\t" \
87
+ : \
88
+ : [in0] "r" (src0 + sj), [in1] "r" (src1 + sj), \
89
+ [out0] "r" (dst + dj), [out1] "r" (dst + dj + MUL2(8)/sizeof(sgn##bits)) \
90
+ : "d0","d1","d2","d3" \
91
+ );
92
+ #define MERGE_ASM3(sgn, bits) __asm__ ( \
93
+ "vld1." #bits " {d0-d1}, [%[in0]] \n\t" \
94
+ "vld1." #bits " {d2-d3}, [%[in1]] \n\t" \
95
+ "vld1." #bits " {d4-d5}, [%[in2]] \n\t" \
96
+ "vst3." #bits " {d0, d2, d4}, [%[out0]] \n\t" \
97
+ "vst3." #bits " {d1, d3, d5}, [%[out1]] \n\t" \
98
+ : \
99
+ : [in0] "r" (src0 + sj), [in1] "r" (src1 + sj), [in2] "r" (src2 + sj), \
100
+ [out0] "r" (dst + dj), [out1] "r" (dst + dj + MUL3(8)/sizeof(sgn##bits)) \
101
+ : "d0","d1","d2","d3","d4","d5" \
102
+ );
103
+ #define MERGE_ASM4(sgn, bits) __asm__ ( \
104
+ "vld1." #bits " {d0-d1}, [%[in0]] \n\t" \
105
+ "vld1." #bits " {d2-d3}, [%[in1]] \n\t" \
106
+ "vld1." #bits " {d4-d5}, [%[in2]] \n\t" \
107
+ "vld1." #bits " {d6-d7}, [%[in3]] \n\t" \
108
+ "vst4." #bits " {d0, d2, d4, d6}, [%[out0]] \n\t" \
109
+ "vst4." #bits " {d1, d3, d5, d7}, [%[out1]] \n\t" \
110
+ : \
111
+ : [in0] "r" (src0 + sj), [in1] "r" (src1 + sj), [in2] "r" (src2 + sj), [in3] "r" (src3 + sj), \
112
+ [out0] "r" (dst + dj), [out1] "r" (dst + dj + MUL4(8)/sizeof(sgn##bits)) \
113
+ : "d0","d1","d2","d3","d4","d5","d6","d7" \
114
+ );
115
+
116
+ #define MERGE_QUAD(sgn, bits, n) { \
117
+ FILL_LINES##n(PREF, sgn##bits) \
118
+ MERGE_ASM##n(sgn, bits) \
119
+ }
120
+
121
+ #else
122
+
123
+ #define MERGE_QUAD(sgn, bits, n) { \
124
+ vec128 v_dst; \
125
+ /*FILL_LINES##n(PREF, sgn##bits) \
126
+ FILL_LINES##n(VLD1Q, sgn##bits)*/ \
127
+ FILL_LINES##n(PRLD, sgn##bits) \
128
+ vst##n##q_##sgn##bits(dst + dj, v_dst); \
129
+ }
130
+
131
+ #endif
132
+
133
+ #define COMBINE(sgn,bits,n) void combine##n(const Size2D &_size \
134
+ FILL_LINES##n(FARG, sgn##bits), \
135
+ sgn##bits * dstBase, ptrdiff_t dstStride) \
136
+ { \
137
+ internal::assertSupportedConfiguration(); \
138
+ Size2D size(_size); \
139
+ if (CONTSRC##n \
140
+ dstStride == (ptrdiff_t)(size.width)) \
141
+ { \
142
+ size.width *= size.height; \
143
+ size.height = 1; \
144
+ } \
145
+ typedef internal::VecTraits<sgn##bits, n>::vec128 vec128; \
146
+ size_t roiw16 = size.width >= (16/sizeof(sgn##bits) - 1) ? size.width - (16/sizeof(sgn##bits) - 1) : 0; \
147
+ typedef internal::VecTraits<sgn##bits, n>::vec64 vec64; \
148
+ size_t roiw8 = size.width >= (8/sizeof(sgn##bits) - 1) ? size.width - (8/sizeof(sgn##bits) - 1) : 0; \
149
+ \
150
+ for (size_t i = 0u; i < size.height; ++i) \
151
+ { \
152
+ FILL_LINES##n(VROW, sgn##bits) \
153
+ sgn##bits * dst = internal::getRowPtr(dstBase, dstStride, i); \
154
+ size_t sj = 0u, dj = 0u; \
155
+ \
156
+ for (; sj < roiw16; sj += 16/sizeof(sgn##bits), dj += MUL##n(16)/sizeof(sgn##bits)) \
157
+ MERGE_QUAD(sgn, bits, n) \
158
+ \
159
+ if ( sj < roiw8 ) \
160
+ { \
161
+ vec64 v_dst; \
162
+ FILL_LINES##n(VLD1, sgn##bits) \
163
+ vst##n##_##sgn##bits(dst + dj, v_dst); \
164
+ sj += 8/sizeof(sgn##bits); dj += MUL##n(8)/sizeof(sgn##bits); \
165
+ } \
166
+ \
167
+ for (; sj < size.width; ++sj, dj += n) \
168
+ { \
169
+ FILL_LINES##n(SLD, sgn##bits) \
170
+ } \
171
+ } \
172
+ }
173
+
174
+ #define COMBINE64(sgn,n) void combine##n(const Size2D &_size \
175
+ FILL_LINES##n(FARG, sgn##64), \
176
+ sgn##64 * dstBase, ptrdiff_t dstStride) \
177
+ { \
178
+ internal::assertSupportedConfiguration(); \
179
+ Size2D size(_size); \
180
+ if (CONTSRC##n \
181
+ dstStride == (ptrdiff_t)(size.width)) \
182
+ { \
183
+ size.width *= size.height; \
184
+ size.height = 1; \
185
+ } \
186
+ typedef internal::VecTraits<sgn##64, n>::vec64 vec64; \
187
+ \
188
+ for (size_t i = 0u; i < size.height; ++i) \
189
+ { \
190
+ FILL_LINES##n(VROW, sgn##64) \
191
+ sgn##64 * dst = internal::getRowPtr(dstBase, dstStride, i); \
192
+ size_t sj = 0u, dj = 0u; \
193
+ \
194
+ for (; sj < size.width; ++sj, dj += n) \
195
+ { \
196
+ vec64 v_dst; \
197
+ FILL_LINES##n(VLD1, sgn##64) \
198
+ vst##n##_##sgn##64(dst + dj, v_dst); \
199
+ /*FILL_LINES##n(SLD, sgn##64)*/ \
200
+ } \
201
+ } \
202
+ }
203
+
204
+ #else
205
+
206
+ #define VOID_LINE(type, n) (void)src##n##Base; (void)src##n##Stride;
207
+
208
+ #define COMBINE(sgn,bits,n) void combine##n(const Size2D &size \
209
+ FILL_LINES##n(FARG, sgn##bits), \
210
+ sgn##bits * dstBase, ptrdiff_t dstStride) \
211
+ { \
212
+ internal::assertSupportedConfiguration(); \
213
+ (void)size; \
214
+ FILL_LINES##n(VOID, sgn##bits) \
215
+ (void)dstBase; \
216
+ (void)dstStride; \
217
+ }
218
+ #define COMBINE64(sgn,n) COMBINE(sgn,64,n)
219
+
220
+ #endif //CAROTENE_NEON
221
+
222
+ COMBINE(u, 8,2)
223
+ COMBINE(u, 8,3)
224
+ COMBINE(u, 8,4)
225
+ COMBINE(u,16,2)
226
+ COMBINE(u,16,3)
227
+ COMBINE(u,16,4)
228
+ COMBINE(s,32,2)
229
+ COMBINE(s,32,3)
230
+ COMBINE(s,32,4)
231
+ COMBINE64(s, 2)
232
+ COMBINE64(s, 3)
233
+ COMBINE64(s, 4)
234
+
235
+ void combineYUYV(const Size2D &size,
236
+ const u8 * srcyBase, ptrdiff_t srcyStride,
237
+ const u8 * srcuBase, ptrdiff_t srcuStride,
238
+ const u8 * srcvBase, ptrdiff_t srcvStride,
239
+ u8 * dstBase, ptrdiff_t dstStride)
240
+ {
241
+ internal::assertSupportedConfiguration();
242
+ #ifdef CAROTENE_NEON
243
+ #ifndef __ANDROID__
244
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
245
+ #endif
246
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
247
+
248
+ for (size_t i = 0u; i < size.height; i += 1)
249
+ {
250
+ const u8 * srcy = internal::getRowPtr(srcyBase, srcyStride, i);
251
+ const u8 * srcu = internal::getRowPtr(srcuBase, srcuStride, i);
252
+ const u8 * srcv = internal::getRowPtr(srcvBase, srcvStride, i);
253
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
254
+ size_t syj = 0u, sj = 0u, dj = 0u;
255
+
256
+ #ifndef __ANDROID__
257
+ for (; sj < roiw32; sj += 32, syj += 64, dj += 128)
258
+ {
259
+ internal::prefetch(srcy + syj);
260
+ internal::prefetch(srcu + sj);
261
+ internal::prefetch(srcv + sj);
262
+
263
+ uint8x16x2_t v_y = vld2q_u8(srcy + syj);
264
+ uint8x16x4_t v_dst;
265
+ v_dst.val[0] = v_y.val[0];
266
+ v_dst.val[1] = vld1q_u8(srcu + sj);
267
+ v_dst.val[2] = v_y.val[1];
268
+ v_dst.val[3] = vld1q_u8(srcv + sj);
269
+ vst4q_u8(dst + dj, v_dst);
270
+
271
+ v_y = vld2q_u8(srcy + syj + 32);
272
+ v_dst.val[0] = v_y.val[0];
273
+ v_dst.val[1] = vld1q_u8(srcu + sj + 16);
274
+ v_dst.val[2] = v_y.val[1];
275
+ v_dst.val[3] = vld1q_u8(srcv + sj + 16);
276
+ vst4q_u8(dst + dj + 64, v_dst);
277
+ }
278
+ #endif
279
+
280
+ for (; sj < roiw8; sj += 8, syj += 16, dj += 32)
281
+ {
282
+ uint8x8x2_t v_y = vld2_u8(srcy + syj);
283
+ uint8x8x4_t v_dst;
284
+ v_dst.val[0] = v_y.val[0];
285
+ v_dst.val[1] = vld1_u8(srcu + sj);
286
+ v_dst.val[2] = v_y.val[1];
287
+ v_dst.val[3] = vld1_u8(srcv + sj);
288
+ vst4_u8(dst + dj, v_dst);
289
+ }
290
+
291
+ for (; sj < size.width; ++sj, syj += 2, dj += 4)
292
+ {
293
+ dst[dj] = srcy[syj];
294
+ dst[dj + 1] = srcu[sj];
295
+ dst[dj + 2] = srcy[syj + 1];
296
+ dst[dj + 3] = srcv[sj];
297
+ }
298
+ }
299
+ #else
300
+ (void)size;
301
+ (void)srcyBase;
302
+ (void)srcyStride;
303
+ (void)srcuBase;
304
+ (void)srcuStride;
305
+ (void)srcvBase;
306
+ (void)srcvStride;
307
+ (void)dstBase;
308
+ (void)dstStride;
309
+ #endif
310
+ }
311
+
312
+ void combineUYVY(const Size2D &size,
313
+ const u8 * srcyBase, ptrdiff_t srcyStride,
314
+ const u8 * srcuBase, ptrdiff_t srcuStride,
315
+ const u8 * srcvBase, ptrdiff_t srcvStride,
316
+ u8 * dstBase, ptrdiff_t dstStride)
317
+ {
318
+ internal::assertSupportedConfiguration();
319
+ #ifdef CAROTENE_NEON
320
+ #ifndef __ANDROID__
321
+ size_t roiw32 = size.width >= 31 ? size.width - 31 : 0;
322
+ #endif
323
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
324
+
325
+ for (size_t i = 0u; i < size.height; ++i)
326
+ {
327
+ const u8 * srcy = internal::getRowPtr(srcyBase, srcyStride, i);
328
+ const u8 * srcu = internal::getRowPtr(srcuBase, srcuStride, i);
329
+ const u8 * srcv = internal::getRowPtr(srcvBase, srcvStride, i);
330
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
331
+ size_t syj = 0u, sj = 0u, dj = 0u;
332
+
333
+ #ifndef __ANDROID__
334
+ for (; sj < roiw32; sj += 32, syj += 64, dj += 128)
335
+ {
336
+ internal::prefetch(srcy + syj);
337
+ internal::prefetch(srcu + sj);
338
+ internal::prefetch(srcv + sj);
339
+
340
+ uint8x16x2_t v_y = vld2q_u8(srcy + syj);
341
+ uint8x16x4_t v_dst;
342
+ v_dst.val[0] = vld1q_u8(srcu + sj);
343
+ v_dst.val[1] = v_y.val[0];
344
+ v_dst.val[2] = vld1q_u8(srcv + sj);
345
+ v_dst.val[3] = v_y.val[1];
346
+ vst4q_u8(dst + dj, v_dst);
347
+
348
+ v_y = vld2q_u8(srcy + syj + 32);
349
+ v_dst.val[0] = vld1q_u8(srcu + sj + 16);
350
+ v_dst.val[1] = v_y.val[0];
351
+ v_dst.val[2] = vld1q_u8(srcv + sj + 16);
352
+ v_dst.val[3] = v_y.val[1];
353
+ vst4q_u8(dst + dj + 64, v_dst);
354
+ }
355
+ #endif
356
+
357
+ for (; sj < roiw8; sj += 8, syj += 16, dj += 32)
358
+ {
359
+ uint8x8x2_t v_y = vld2_u8(srcy + syj);
360
+ uint8x8x4_t v_dst;
361
+ v_dst.val[0] = vld1_u8(srcu + sj);
362
+ v_dst.val[1] = v_y.val[0];
363
+ v_dst.val[2] = vld1_u8(srcv + sj);
364
+ v_dst.val[3] = v_y.val[1];
365
+ vst4_u8(dst + dj, v_dst);
366
+ }
367
+
368
+ for (; sj < size.width; ++sj, syj += 2, dj += 4)
369
+ {
370
+ dst[dj] = srcu[sj];
371
+ dst[dj + 1] = srcy[syj];
372
+ dst[dj + 2] = srcv[sj];
373
+ dst[dj + 3] = srcy[syj + 1];
374
+ }
375
+ }
376
+ #else
377
+ (void)size;
378
+ (void)srcyBase;
379
+ (void)srcyStride;
380
+ (void)srcuBase;
381
+ (void)srcuStride;
382
+ (void)srcvBase;
383
+ (void)srcvStride;
384
+ (void)dstBase;
385
+ (void)dstStride;
386
+ #endif
387
+ }
388
+
389
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/cmp.cpp ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ namespace CAROTENE_NS {
44
+
45
+ #ifdef CAROTENE_NEON
46
+
47
+ namespace {
48
+
49
+ inline void vnst(u8* dst, uint8x16_t v1, uint8x16_t v2) { vst1q_u8(dst, v1); vst1q_u8(dst+16, v2); }
50
+ inline void vnst(u8* dst, uint16x8_t v1, uint16x8_t v2) { vst1q_u8(dst, vcombine_u8(vmovn_u16(v1), vmovn_u16(v2))); }
51
+ inline void vnst(u8* dst, uint32x4_t v1, uint32x4_t v2) { vst1_u8(dst, vmovn_u16(vcombine_u16(vmovn_u32(v1), vmovn_u32(v2)))); }
52
+
53
+ template <typename Op, int elsize> struct vtail
54
+ {
55
+ static inline void compare(const typename Op::type * src0, const typename Op::type * src1,
56
+ u8 * dst, const Op & op,
57
+ size_t &x, size_t width)
58
+ {
59
+ //do nothing since there couldn't be enough data
60
+ (void)src0;
61
+ (void)src1;
62
+ (void)dst;
63
+ (void)op;
64
+ (void)x;
65
+ (void)width;
66
+ }
67
+ };
68
+ template <typename Op> struct vtail<Op, 2>
69
+ {
70
+ static inline void compare(const typename Op::type * src0, const typename Op::type * src1,
71
+ u8 * dst, const Op & op,
72
+ size_t &x, size_t width)
73
+ {
74
+ typedef typename Op::type type;
75
+ typedef typename internal::VecTraits<type>::vec128 vec128;
76
+ typedef typename internal::VecTraits<type>::unsign::vec128 uvec128;
77
+ //There no more than 15 elements in the tail, so we could handle 8 element vector only once
78
+ if( x + 8 < width)
79
+ {
80
+ vec128 v_src0, v_src1;
81
+ uvec128 v_dst;
82
+
83
+ v_src0 = internal::vld1q(src0 + x);
84
+ v_src1 = internal::vld1q(src1 + x);
85
+ op(v_src0, v_src1, v_dst);
86
+ internal::vst1(dst + x, internal::vmovn(v_dst));
87
+ x+=8;
88
+ }
89
+ }
90
+ };
91
+ template <typename Op> struct vtail<Op, 1>
92
+ {
93
+ static inline void compare(const typename Op::type * src0, const typename Op::type * src1,
94
+ u8 * dst, const Op & op,
95
+ size_t &x, size_t width)
96
+ {
97
+ typedef typename Op::type type;
98
+ typedef typename internal::VecTraits<type>::vec128 vec128;
99
+ typedef typename internal::VecTraits<type>::unsign::vec128 uvec128;
100
+ typedef typename internal::VecTraits<type>::vec64 vec64;
101
+ typedef typename internal::VecTraits<type>::unsign::vec64 uvec64;
102
+ //There no more than 31 elements in the tail, so we could handle once 16+8 or 16 or 8 elements
103
+ if( x + 16 < width)
104
+ {
105
+ vec128 v_src0, v_src1;
106
+ uvec128 v_dst;
107
+
108
+ v_src0 = internal::vld1q(src0 + x);
109
+ v_src1 = internal::vld1q(src1 + x);
110
+ op(v_src0, v_src1, v_dst);
111
+ internal::vst1q(dst + x, v_dst);
112
+ x+=16;
113
+ }
114
+ if( x + 8 < width)
115
+ {
116
+ vec64 v_src0, v_src1;
117
+ uvec64 v_dst;
118
+
119
+ v_src0 = internal::vld1(src0 + x);
120
+ v_src1 = internal::vld1(src1 + x);
121
+ op(v_src0, v_src1, v_dst);
122
+ internal::vst1(dst + x, v_dst);
123
+ x+=8;
124
+ }
125
+ }
126
+ };
127
+
128
+ template <typename Op>
129
+ void vcompare(Size2D size,
130
+ const typename Op::type * src0Base, ptrdiff_t src0Stride,
131
+ const typename Op::type * src1Base, ptrdiff_t src1Stride,
132
+ u8 * dstBase, ptrdiff_t dstStride, const Op & op)
133
+ {
134
+ typedef typename Op::type type;
135
+ typedef typename internal::VecTraits<type>::vec128 vec128;
136
+ typedef typename internal::VecTraits<type>::unsign::vec128 uvec128;
137
+
138
+ if (src0Stride == src1Stride && src0Stride == dstStride &&
139
+ src0Stride == (ptrdiff_t)(size.width * sizeof(type)))
140
+ {
141
+ size.width *= size.height;
142
+ size.height = 1;
143
+ }
144
+
145
+ const u32 step_base = 32 / sizeof(type);
146
+ size_t roiw_base = size.width >= (step_base - 1) ? size.width - step_base + 1 : 0;
147
+
148
+ for (size_t y = 0; y < size.height; ++y)
149
+ {
150
+ const type * src0 = internal::getRowPtr(src0Base, src0Stride, y);
151
+ const type * src1 = internal::getRowPtr(src1Base, src1Stride, y);
152
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, y);
153
+ size_t x = 0;
154
+
155
+ for( ; x < roiw_base; x += step_base )
156
+ {
157
+ internal::prefetch(src0 + x);
158
+ internal::prefetch(src1 + x);
159
+
160
+ vec128 v_src00 = internal::vld1q(src0 + x), v_src01 = internal::vld1q(src0 + x + 16 / sizeof(type));
161
+ vec128 v_src10 = internal::vld1q(src1 + x), v_src11 = internal::vld1q(src1 + x + 16 / sizeof(type));
162
+ uvec128 v_dst0;
163
+ uvec128 v_dst1;
164
+
165
+ op(v_src00, v_src10, v_dst0);
166
+ op(v_src01, v_src11, v_dst1);
167
+
168
+ vnst(dst + x, v_dst0, v_dst1);
169
+ }
170
+
171
+ vtail<Op, sizeof(type)>::compare(src0, src1, dst, op, x, size.width);
172
+
173
+ for (; x < size.width; ++x)
174
+ {
175
+ op(src0 + x, src1 + x, dst + x);
176
+ }
177
+ }
178
+ }
179
+
180
+ template<typename T>
181
+ struct OpCmpEQ
182
+ {
183
+ typedef T type;
184
+
185
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0, const typename internal::VecTraits<T>::vec128 & v_src1,
186
+ typename internal::VecTraits<T>::unsign::vec128 & v_dst) const
187
+ {
188
+ v_dst = internal::vceqq(v_src0, v_src1);
189
+ }
190
+
191
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0, const typename internal::VecTraits<T>::vec64 & v_src1,
192
+ typename internal::VecTraits<T>::unsign::vec64 & v_dst) const
193
+ {
194
+ v_dst = internal::vceq(v_src0, v_src1);
195
+ }
196
+
197
+ void operator() (const T * src0, const T * src1, u8 * dst) const
198
+ {
199
+ dst[0] = src0[0] == src1[0] ? 255 : 0;
200
+ }
201
+ };
202
+
203
+ template<typename T>
204
+ struct OpCmpNE
205
+ {
206
+ typedef T type;
207
+
208
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0, const typename internal::VecTraits<T>::vec128 & v_src1,
209
+ typename internal::VecTraits<T>::unsign::vec128 & v_dst) const
210
+ {
211
+ v_dst = internal::vmvnq(internal::vceqq(v_src0, v_src1));
212
+ }
213
+
214
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0, const typename internal::VecTraits<T>::vec64 & v_src1,
215
+ typename internal::VecTraits<T>::unsign::vec64 & v_dst) const
216
+ {
217
+ v_dst = internal::vmvn(internal::vceq(v_src0, v_src1));
218
+ }
219
+
220
+ void operator() (const T * src0, const T * src1, u8 * dst) const
221
+ {
222
+ dst[0] = src0[0] == src1[0] ? 0 : 255;
223
+ }
224
+ };
225
+
226
+ template<typename T>
227
+ struct OpCmpGT
228
+ {
229
+ typedef T type;
230
+
231
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0, const typename internal::VecTraits<T>::vec128 & v_src1,
232
+ typename internal::VecTraits<T>::unsign::vec128 & v_dst) const
233
+ {
234
+ v_dst = internal::vcgtq(v_src0, v_src1);
235
+ }
236
+
237
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0, const typename internal::VecTraits<T>::vec64 & v_src1,
238
+ typename internal::VecTraits<T>::unsign::vec64 & v_dst) const
239
+ {
240
+ v_dst = internal::vcgt(v_src0, v_src1);
241
+ }
242
+
243
+ void operator() (const T * src0, const T * src1, u8 * dst) const
244
+ {
245
+ dst[0] = src0[0] > src1[0] ? 255 : 0;
246
+ }
247
+ };
248
+
249
+ template<typename T>
250
+ struct OpCmpGE
251
+ {
252
+ typedef T type;
253
+
254
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0, const typename internal::VecTraits<T>::vec128 & v_src1,
255
+ typename internal::VecTraits<T>::unsign::vec128 & v_dst) const
256
+ {
257
+ v_dst = internal::vcgeq(v_src0, v_src1);
258
+ }
259
+
260
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0, const typename internal::VecTraits<T>::vec64 & v_src1,
261
+ typename internal::VecTraits<T>::unsign::vec64 & v_dst) const
262
+ {
263
+ v_dst = internal::vcge(v_src0, v_src1);
264
+ }
265
+
266
+ void operator() (const T * src0, const T * src1, u8 * dst) const
267
+ {
268
+ dst[0] = src0[0] >= src1[0] ? 255 : 0;
269
+ }
270
+ };
271
+
272
+ }
273
+
274
+ #define IMPL_CMPOP(op, type) \
275
+ void cmp##op(const Size2D &size, \
276
+ const type * src0Base, ptrdiff_t src0Stride, \
277
+ const type * src1Base, ptrdiff_t src1Stride, \
278
+ u8 *dstBase, ptrdiff_t dstStride) \
279
+ { \
280
+ internal::assertSupportedConfiguration(); \
281
+ vcompare(size, \
282
+ src0Base, src0Stride, \
283
+ src1Base, src1Stride, \
284
+ dstBase, dstStride, \
285
+ OpCmp##op<type>()); \
286
+ }
287
+
288
+ #else
289
+
290
+ #define IMPL_CMPOP(op, type) \
291
+ void cmp##op(const Size2D &size, \
292
+ const type * src0Base, ptrdiff_t src0Stride, \
293
+ const type * src1Base, ptrdiff_t src1Stride, \
294
+ u8 *dstBase, ptrdiff_t dstStride) \
295
+ { \
296
+ internal::assertSupportedConfiguration(); \
297
+ (void)size; \
298
+ (void)src0Base; \
299
+ (void)src0Stride; \
300
+ (void)src1Base; \
301
+ (void)src1Stride; \
302
+ (void)dstBase; \
303
+ (void)dstStride; \
304
+ }
305
+
306
+ #endif
307
+
308
+ IMPL_CMPOP(EQ, u8)
309
+ IMPL_CMPOP(EQ, s8)
310
+ IMPL_CMPOP(EQ, u16)
311
+ IMPL_CMPOP(EQ, s16)
312
+ IMPL_CMPOP(EQ, u32)
313
+ IMPL_CMPOP(EQ, s32)
314
+ IMPL_CMPOP(EQ, f32)
315
+
316
+ IMPL_CMPOP(NE, u8)
317
+ IMPL_CMPOP(NE, s8)
318
+ IMPL_CMPOP(NE, u16)
319
+ IMPL_CMPOP(NE, s16)
320
+ IMPL_CMPOP(NE, u32)
321
+ IMPL_CMPOP(NE, s32)
322
+ IMPL_CMPOP(NE, f32)
323
+
324
+ IMPL_CMPOP(GT, u8)
325
+ IMPL_CMPOP(GT, s8)
326
+ IMPL_CMPOP(GT, u16)
327
+ IMPL_CMPOP(GT, s16)
328
+ IMPL_CMPOP(GT, u32)
329
+ IMPL_CMPOP(GT, s32)
330
+ IMPL_CMPOP(GT, f32)
331
+
332
+ IMPL_CMPOP(GE, u8)
333
+ IMPL_CMPOP(GE, s8)
334
+ IMPL_CMPOP(GE, u16)
335
+ IMPL_CMPOP(GE, s16)
336
+ IMPL_CMPOP(GE, u32)
337
+ IMPL_CMPOP(GE, s32)
338
+ IMPL_CMPOP(GE, f32)
339
+
340
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/colorconvert.cpp ADDED
The diff for this file is too large to render. See raw diff
 
3rdparty/carotene/src/common.cpp ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include <cstdlib>
41
+ #include <iostream>
42
+
43
+ #include "common.hpp"
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ bool isSupportedConfiguration()
48
+ {
49
+ #ifdef CAROTENE_NEON
50
+ return true;
51
+ #else
52
+ return false;
53
+ #endif
54
+ }
55
+
56
+ namespace internal {
57
+
58
+ void assertSupportedConfiguration(bool parametersSupported)
59
+ {
60
+ if (!isSupportedConfiguration()) {
61
+ std::cerr << "internal error: attempted to use an unavailable function" << std::endl;
62
+ std::abort();
63
+ }
64
+
65
+ if (!parametersSupported) {
66
+ std::cerr << "internal error: attempted to use a function with unsupported parameters" << std::endl;
67
+ std::abort();
68
+ }
69
+ }
70
+
71
+ ptrdiff_t borderInterpolate(ptrdiff_t _p, size_t _len, BORDER_MODE borderType, size_t startMargin, size_t endMargin)
72
+ {
73
+ ptrdiff_t p = _p + (ptrdiff_t)startMargin;
74
+ size_t len = _len + startMargin + endMargin;
75
+ if( (size_t)p < len )
76
+ return _p;
77
+ else if( borderType == BORDER_MODE_REPLICATE )
78
+ p = p < 0 ? 0 : (ptrdiff_t)len - 1;
79
+ else if( borderType == BORDER_MODE_REFLECT || borderType == BORDER_MODE_REFLECT101 )
80
+ {
81
+ s32 delta = borderType == BORDER_MODE_REFLECT101;
82
+ if( len == 1 )
83
+ return 0;
84
+ do
85
+ {
86
+ if( p < 0 )
87
+ p = -p - 1 + delta;
88
+ else
89
+ p = (ptrdiff_t)len - 1 - (p - (ptrdiff_t)len) - delta;
90
+ }
91
+ while( (size_t)p >= len );
92
+ }
93
+ else if( borderType == BORDER_MODE_WRAP )
94
+ {
95
+ if( p < 0 )
96
+ p -= ((p-(ptrdiff_t)len+1)/(ptrdiff_t)len)*(ptrdiff_t)len;
97
+ if( p >= (ptrdiff_t)len )
98
+ p %= (ptrdiff_t)len;
99
+ }
100
+ else if( borderType == BORDER_MODE_CONSTANT )
101
+ p = -1;
102
+ else
103
+ internal::assertSupportedConfiguration(false);
104
+ return p - (ptrdiff_t)startMargin;
105
+ }
106
+
107
+ } // namespace internal
108
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/common.hpp ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #ifndef CAROTENE_SRC_COMMON_HPP
41
+ #define CAROTENE_SRC_COMMON_HPP
42
+
43
+ #include <cstddef>
44
+ #include <cstdlib>
45
+ #include <algorithm>
46
+
47
+ #if defined WITH_NEON && (defined __ARM_NEON__ || defined __ARM_NEON)
48
+ #define CAROTENE_NEON
49
+ #endif
50
+
51
+ #ifdef CAROTENE_NEON
52
+ #include <arm_neon.h>
53
+ #include "intrinsics.hpp"
54
+ #endif
55
+
56
+ #include <carotene/functions.hpp>
57
+ #include "saturate_cast.hpp"
58
+
59
+ namespace CAROTENE_NS { namespace internal {
60
+
61
+ inline void prefetch(const void *ptr, size_t offset = 32*10)
62
+ {
63
+ #if defined __GNUC__
64
+ __builtin_prefetch(reinterpret_cast<const char*>(ptr) + offset);
65
+ #elif defined _MSC_VER && defined CAROTENE_NEON
66
+ __prefetch(reinterpret_cast<const char*>(ptr) + offset);
67
+ #else
68
+ (void)ptr;
69
+ (void)offset;
70
+ #endif
71
+ }
72
+
73
+ template <typename T>
74
+ inline T *getRowPtr(T *base, ptrdiff_t stride, size_t row)
75
+ {
76
+ char *baseRaw = const_cast<char *>(reinterpret_cast<const char *>(base));
77
+ return reinterpret_cast<T *>(baseRaw + ptrdiff_t(row) * stride);
78
+ }
79
+
80
+ void assertSupportedConfiguration(bool parametersSupported = true);
81
+
82
+ ptrdiff_t borderInterpolate(ptrdiff_t _p, size_t _len, BORDER_MODE borderType, size_t startMargin = 0, size_t endMargin = 0);
83
+
84
+ /*!
85
+ * Aligns pointer by the certain number of bytes
86
+ *
87
+ * This small inline function aligns the pointer by the certain number of bytes by shifting
88
+ * it forward by 0 or a positive offset.
89
+ */
90
+ template<typename T> inline T* alignPtr(T* ptr, size_t n=sizeof(T))
91
+ {
92
+ return (T*)(((size_t)ptr + n-1) & -n);
93
+ }
94
+
95
+ }}
96
+
97
+ #endif
3rdparty/carotene/src/convert.cpp ADDED
@@ -0,0 +1,1331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ namespace CAROTENE_NS {
43
+
44
+ #ifdef CAROTENE_NEON
45
+
46
+ #define CVT_FUNC(T1, T2, SIMD_SIZE, CVTINIT, CVTROW) \
47
+ void convert(const Size2D &_size, \
48
+ const T1 * srcBase, ptrdiff_t srcStride, \
49
+ T2 * dstBase, ptrdiff_t dstStride) \
50
+ { \
51
+ internal::assertSupportedConfiguration(); \
52
+ Size2D size(_size); \
53
+ if (srcStride == dstStride && \
54
+ srcStride == (ptrdiff_t)(size.width)) \
55
+ { \
56
+ size.width *= size.height; \
57
+ size.height = 1; \
58
+ } \
59
+ const ptrdiff_t sstep = srcStride / sizeof(T1); \
60
+ const ptrdiff_t dstep = dstStride / sizeof(T2); \
61
+ const size_t w = size.width & ~(SIMD_SIZE-1); \
62
+ if (size.width >= SIMD_SIZE) \
63
+ { \
64
+ const T1* _src = srcBase; \
65
+ T2* _dst = dstBase; \
66
+ CVTINIT \
67
+ for (ptrdiff_t h = size.height; h--; _src += sstep, _dst += dstep ) \
68
+ CVTROW \
69
+ } \
70
+ if(w < size.width) \
71
+ { \
72
+ const T1* _src = srcBase; \
73
+ T2* _dst = dstBase; \
74
+ for (ptrdiff_t h = size.height; h--; _src += sstep, _dst += dstep ) \
75
+ for(size_t i = w; i < size.width; i++ ) \
76
+ _dst[i] = internal::saturate_cast<T2>(_src[i]); \
77
+ } \
78
+ }
79
+
80
+ #else
81
+
82
+ #define CVT_FUNC(T1, T2, SIMD_SIZE, CVTINIT, CVTROW) \
83
+ void convert(const Size2D &, \
84
+ const T1 *, ptrdiff_t, \
85
+ T2 *, ptrdiff_t) \
86
+ { \
87
+ internal::assertSupportedConfiguration(); \
88
+ }
89
+
90
+ #endif
91
+
92
+ CVT_FUNC(u8, s8, 16,
93
+ uint8x16_t v127 = vdupq_n_u8(127);,
94
+ {
95
+ for (size_t i = 0; i < w; i += 16)
96
+ {
97
+ internal::prefetch(_src + i);
98
+ uint8x16_t vu8 = vld1q_u8(_src + i);
99
+ int8x16_t vu1 = vreinterpretq_s8_u8(vminq_u8(vu8, v127));
100
+ vst1q_s8(_dst + i, vu1);
101
+ }
102
+ })
103
+
104
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
105
+ CVT_FUNC(u8, u16, 16,
106
+ register uint8x16_t zero0 asm ("q1") = vmovq_n_u8(0);,
107
+ {
108
+ for (size_t i = 0; i < w; i += 16)
109
+ {
110
+ internal::prefetch(_src + i);
111
+ __asm__ (
112
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
113
+ "vst2.8 {d0,d2}, [%[dst1]] \n\t"
114
+ "vst2.8 {d1,d3}, [%[dst2]] \n\t"
115
+ : /*no output*/
116
+ : [src] "r" (_src + i),
117
+ [dst1] "r" (_dst + i + 0),
118
+ [dst2] "r" (_dst + i + 8),
119
+ "w" (zero0)
120
+ : "d0","d1"
121
+ );
122
+ }
123
+ })
124
+ #else
125
+ CVT_FUNC(u8, u16, 16,
126
+ uint8x16x2_t vline;
127
+ vline.val[1] = vmovq_n_u8(0);,
128
+ {
129
+ for (size_t i = 0; i < w; i += 16)
130
+ {
131
+ internal::prefetch(_src + i);
132
+ vline.val[0] = vld1q_u8(_src + i);
133
+ vst2q_u8((uint8_t*)(_dst + i), vline);
134
+ }
135
+ })
136
+ #endif
137
+
138
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
139
+ CVT_FUNC(u8, s32, 16,
140
+ register uint8x16_t zero0 asm ("q1") = vmovq_n_u8(0);
141
+ register uint8x16_t zero1 asm ("q2") = vmovq_n_u8(0);
142
+ register uint8x16_t zero2 asm ("q3") = vmovq_n_u8(0);,
143
+ {
144
+ for (size_t i = 0; i < w; i += 16)
145
+ {
146
+ internal::prefetch(_src + i);
147
+ __asm__ (
148
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
149
+ "vst4.8 {d0,d2,d4,d6}, [%[dst1]] \n\t"
150
+ "vst4.8 {d1,d3,d5,d7}, [%[dst2]] \n\t"
151
+ : /*no output*/
152
+ : [src] "r" (_src + i),
153
+ [dst1] "r" (_dst + i + 0),
154
+ [dst2] "r" (_dst + i + 8),
155
+ "w" (zero0), "w" (zero1), "w" (zero2)
156
+ : "d0","d1"
157
+ );
158
+ }
159
+ })
160
+ #else
161
+ CVT_FUNC(u8, s32, 16,
162
+ uint8x16x4_t vline;
163
+ vline.val[1] = vmovq_n_u8(0);
164
+ vline.val[2] = vmovq_n_u8(0);
165
+ vline.val[3] = vmovq_n_u8(0);,
166
+ {
167
+ for (size_t i = 0; i < w; i += 16)
168
+ {
169
+ internal::prefetch(_src + i);
170
+ vline.val[0] = vld1q_u8(_src + i);
171
+ vst4q_u8((uint8_t*)(_dst + i), vline);
172
+ }
173
+ })
174
+ #endif
175
+
176
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
177
+ CVT_FUNC(u8, f32, 16,
178
+ ,
179
+ {
180
+ for (size_t i = 0; i < w; i += 16)
181
+ {
182
+ internal::prefetch(_src + i);
183
+ __asm__ (
184
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
185
+ "vmovl.u8 q1, d0 \n\t"
186
+ "vmovl.u8 q2, d1 \n\t"
187
+ "vmovl.u16 q3, d2 \n\t"
188
+ "vmovl.u16 q4, d3 \n\t"
189
+ "vmovl.u16 q5, d4 \n\t"
190
+ "vmovl.u16 q6, d5 \n\t"
191
+ "vcvt.f32.u32 q7, q3 \n\t"
192
+ "vcvt.f32.u32 q8, q4 \n\t"
193
+ "vcvt.f32.u32 q9, q5 \n\t"
194
+ "vcvt.f32.u32 q10, q6 \n\t"
195
+ "vst1.32 {d14-d15}, [%[dst1]] \n\t"
196
+ "vst1.32 {d16-d17}, [%[dst2]] \n\t"
197
+ "vst1.32 {d18-d19}, [%[dst3]] \n\t"
198
+ "vst1.32 {d20-d21}, [%[dst4]] \n\t"
199
+ : /*no output*/
200
+ : [src] "r" (_src + i),
201
+ [dst1] "r" (_dst + i + 0),
202
+ [dst2] "r" (_dst + i + 4),
203
+ [dst3] "r" (_dst + i + 8),
204
+ [dst4] "r" (_dst + i + 12)
205
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21"
206
+ );
207
+ }
208
+ })
209
+ #else
210
+ CVT_FUNC(u8, f32, 16,
211
+ ,
212
+ {
213
+ for (size_t i = 0; i < w; i += 16)
214
+ {
215
+ internal::prefetch(_src + i);
216
+ uint8x16_t vline_u8 = vld1q_u8(_src + i);
217
+
218
+ uint16x8_t vline1_u16 = vmovl_u8(vget_low_u8(vline_u8));
219
+ uint16x8_t vline2_u16 = vmovl_u8(vget_high_u8(vline_u8));
220
+
221
+ uint32x4_t vline1_u32 = vmovl_u16(vget_low_u16(vline1_u16));
222
+ uint32x4_t vline2_u32 = vmovl_u16(vget_high_u16(vline1_u16));
223
+ uint32x4_t vline3_u32 = vmovl_u16(vget_low_u16(vline2_u16));
224
+ uint32x4_t vline4_u32 = vmovl_u16(vget_high_u16(vline2_u16));
225
+
226
+ float32x4_t vline1_f32 = vcvtq_f32_u32(vline1_u32);
227
+ float32x4_t vline2_f32 = vcvtq_f32_u32(vline2_u32);
228
+ float32x4_t vline3_f32 = vcvtq_f32_u32(vline3_u32);
229
+ float32x4_t vline4_f32 = vcvtq_f32_u32(vline4_u32);
230
+
231
+ vst1q_f32(_dst + i, vline1_f32);
232
+ vst1q_f32(_dst + i + 4, vline2_f32);
233
+ vst1q_f32(_dst + i + 8, vline3_f32);
234
+ vst1q_f32(_dst + i + 12, vline4_f32);
235
+ }
236
+ })
237
+ #endif
238
+
239
+ CVT_FUNC(s8, u8, 16,
240
+ int8x16_t vZero = vdupq_n_s8(0);,
241
+ {
242
+ for (size_t i = 0; i < w; i += 16)
243
+ {
244
+ internal::prefetch(_src + i);
245
+ int8x16_t vu8 = vld1q_s8(_src + i);
246
+ uint8x16_t vu1 = vreinterpretq_u8_s8(vmaxq_s8(vu8, vZero));
247
+ vst1q_u8(_dst + i, vu1);
248
+ }
249
+ })
250
+
251
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
252
+ CVT_FUNC(s8, u16, 16,
253
+ register uint8x16_t zero0 asm ("q1") = vmovq_n_u8(0);,
254
+ {
255
+ for (size_t i = 0; i < w; i += 16)
256
+ {
257
+ internal::prefetch(_src + i);
258
+ __asm__ (
259
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
260
+ "vmax.s8 q0, q1 \n\t"
261
+ "vst2.8 {d0,d2}, [%[dst1]] \n\t"
262
+ "vst2.8 {d1,d3}, [%[dst2]] \n\t"
263
+ : /*no output*/
264
+ : [src] "r" (_src + i),
265
+ [dst1] "r" (_dst + i + 0),
266
+ [dst2] "r" (_dst + i + 8),
267
+ "w" (zero0)
268
+ : "d0","d1"
269
+ );
270
+ }
271
+ })
272
+ #else
273
+ CVT_FUNC(s8, u16, 16,
274
+ int8x16x2_t vline_s8;
275
+ vline_s8.val[1] = vmovq_n_s8(0);,
276
+ {
277
+ for (size_t i = 0; i < w; i += 16)
278
+ {
279
+ internal::prefetch(_src + i);
280
+ vline_s8.val[0] = vld1q_s8(_src + i);
281
+ vline_s8.val[0] = vmaxq_s8(vline_s8.val[0], vline_s8.val[1]);
282
+ vst2q_s8((int8_t*)(_dst + i), vline_s8);
283
+ }
284
+ })
285
+ #endif
286
+
287
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
288
+ CVT_FUNC(s8, s16, 16,
289
+ ,
290
+ {
291
+ for (size_t i = 0; i < w; i += 16)
292
+ {
293
+ internal::prefetch(_src + i);
294
+ __asm__ (
295
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
296
+ "vmovl.s8 q1, d0 \n\t"
297
+ "vmovl.s8 q2, d1 \n\t"
298
+ "vst1.16 {d2-d3}, [%[dst1]] \n\t"
299
+ "vst1.16 {d4-d5}, [%[dst2]] \n\t"
300
+ : /*no output*/
301
+ : [src] "r" (_src + i),
302
+ [dst1] "r" (_dst + i + 0),
303
+ [dst2] "r" (_dst + i + 8)
304
+ : "d0","d1","d2","d3","d4","d5"
305
+ );
306
+ }
307
+ })
308
+ #else
309
+ CVT_FUNC(s8, s16, 16,
310
+ ,
311
+ {
312
+ for (size_t i = 0; i < w; i += 16)
313
+ {
314
+ internal::prefetch(_src + i);
315
+ int8x16_t vline_s8 = vld1q_s8(_src + i);
316
+
317
+ int16x8_t vline1_s16 = vmovl_s8(vget_low_s8(vline_s8));
318
+ int16x8_t vline2_s16 = vmovl_s8(vget_high_s8(vline_s8));
319
+
320
+ vst1q_s16(_dst + i, vline1_s16);
321
+ vst1q_s16(_dst + i + 8, vline2_s16);
322
+ }
323
+ })
324
+ #endif
325
+
326
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
327
+ CVT_FUNC(s8, s32, 16,
328
+ ,
329
+ {
330
+ for (size_t i = 0; i < w; i += 16)
331
+ {
332
+ internal::prefetch(_src + i);
333
+ __asm__ (
334
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
335
+ "vmovl.s8 q1, d0 \n\t"
336
+ "vmovl.s8 q2, d1 \n\t"
337
+ "vmovl.s16 q3, d2 \n\t"
338
+ "vmovl.s16 q4, d3 \n\t"
339
+ "vmovl.s16 q5, d4 \n\t"
340
+ "vmovl.s16 q6, d5 \n\t"
341
+ "vst1.32 {d6-d7}, [%[dst1]] \n\t"
342
+ "vst1.32 {d8-d9}, [%[dst2]] \n\t"
343
+ "vst1.32 {d10-d11}, [%[dst3]] \n\t"
344
+ "vst1.32 {d12-d13}, [%[dst4]] \n\t"
345
+ : /*no output*/
346
+ : [src] "r" (_src + i),
347
+ [dst1] "r" (_dst + i + 0),
348
+ [dst2] "r" (_dst + i + 4),
349
+ [dst3] "r" (_dst + i + 8),
350
+ [dst4] "r" (_dst + i + 12)
351
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13"
352
+ );
353
+ }
354
+ })
355
+ #else
356
+ CVT_FUNC(s8, s32, 16,
357
+ ,
358
+ {
359
+ for (size_t i = 0; i < w; i += 16)
360
+ {
361
+ internal::prefetch(_src + i);
362
+ int8x16_t vline_s8 = vld1q_s8(_src + i);
363
+
364
+ int16x8_t vline1_s16 = vmovl_s8(vget_low_s8(vline_s8));
365
+ int16x8_t vline2_s16 = vmovl_s8(vget_high_s8(vline_s8));
366
+
367
+ int32x4_t vline1_s32 = vmovl_s16(vget_low_s16(vline1_s16));
368
+ int32x4_t vline2_s32 = vmovl_s16(vget_high_s16(vline1_s16));
369
+ int32x4_t vline3_s32 = vmovl_s16(vget_low_s16(vline2_s16));
370
+ int32x4_t vline4_s32 = vmovl_s16(vget_high_s16(vline2_s16));
371
+
372
+ vst1q_s32(_dst + i, vline1_s32);
373
+ vst1q_s32(_dst + i + 4, vline2_s32);
374
+ vst1q_s32(_dst + i + 8, vline3_s32);
375
+ vst1q_s32(_dst + i + 12, vline4_s32);
376
+ }
377
+ })
378
+ #endif
379
+
380
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
381
+ CVT_FUNC(s8, f32, 16,
382
+ ,
383
+ {
384
+ for (size_t i = 0; i < w; i += 16)
385
+ {
386
+ internal::prefetch(_src + i);
387
+ __asm__ (
388
+ "vld1.8 {d0-d1}, [%[src]] \n\t"
389
+ "vmovl.s8 q1, d0 \n\t"
390
+ "vmovl.s8 q2, d1 \n\t"
391
+ "vmovl.s16 q3, d2 \n\t"
392
+ "vmovl.s16 q4, d3 \n\t"
393
+ "vmovl.s16 q5, d4 \n\t"
394
+ "vmovl.s16 q6, d5 \n\t"
395
+ "vcvt.f32.s32 q7, q3 \n\t"
396
+ "vcvt.f32.s32 q8, q4 \n\t"
397
+ "vcvt.f32.s32 q9, q5 \n\t"
398
+ "vcvt.f32.s32 q10, q6 \n\t"
399
+ "vst1.32 {d14-d15}, [%[dst1]] \n\t"
400
+ "vst1.32 {d16-d17}, [%[dst2]] \n\t"
401
+ "vst1.32 {d18-d19}, [%[dst3]] \n\t"
402
+ "vst1.32 {d20-d21}, [%[dst4]] \n\t"
403
+ : /*no output*/
404
+ : [src] "r" (_src + i),
405
+ [dst1] "r" (_dst + i + 0),
406
+ [dst2] "r" (_dst + i + 4),
407
+ [dst3] "r" (_dst + i + 8),
408
+ [dst4] "r" (_dst + i + 12)
409
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21"
410
+ );
411
+ }
412
+ })
413
+ #else
414
+ CVT_FUNC(s8, f32, 16,
415
+ ,
416
+ {
417
+ for (size_t i = 0; i < w; i += 16)
418
+ {
419
+ internal::prefetch(_src + i);
420
+ int8x16_t vline_s8 = vld1q_s8(_src + i);
421
+
422
+ int16x8_t vline1_s16 = vmovl_s8(vget_low_s8(vline_s8));
423
+ int16x8_t vline2_s16 = vmovl_s8(vget_high_s8(vline_s8));
424
+
425
+ int32x4_t vline1_s32 = vmovl_s16(vget_low_s16(vline1_s16));
426
+ int32x4_t vline2_s32 = vmovl_s16(vget_high_s16(vline1_s16));
427
+ int32x4_t vline3_s32 = vmovl_s16(vget_low_s16(vline2_s16));
428
+ int32x4_t vline4_s32 = vmovl_s16(vget_high_s16(vline2_s16));
429
+
430
+ float32x4_t vline1_f32 = vcvtq_f32_s32(vline1_s32);
431
+ float32x4_t vline2_f32 = vcvtq_f32_s32(vline2_s32);
432
+ float32x4_t vline3_f32 = vcvtq_f32_s32(vline3_s32);
433
+ float32x4_t vline4_f32 = vcvtq_f32_s32(vline4_s32);
434
+
435
+ vst1q_f32(_dst + i, vline1_f32);
436
+ vst1q_f32(_dst + i + 4, vline2_f32);
437
+ vst1q_f32(_dst + i + 8, vline3_f32);
438
+ vst1q_f32(_dst + i + 12, vline4_f32);
439
+ }
440
+ })
441
+ #endif
442
+
443
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
444
+ CVT_FUNC(u16, u8, 16,
445
+ ,
446
+ {
447
+ for (size_t i = 0; i < w; i += 16)
448
+ {
449
+ internal::prefetch(_src + i);
450
+ __asm__ (
451
+ "vld1.8 {d0-d1}, [%[src1]] \n\t"
452
+ "vqmovn.u16 d4, q0 \n\t"
453
+ "vld1.8 {d2-d3}, [%[src2]] \n\t"
454
+ "vqmovn.u16 d5, q1 \n\t"
455
+ "vst1.8 {d4-d5}, [%[dst]] \n\t"
456
+ : /*no output*/
457
+ : [src1] "r" (_src + i),
458
+ [src2] "r" (_src + i + 8),
459
+ [dst] "r" (_dst + i + 0)
460
+ : "d0","d1","d2","d3","d4","d5"
461
+ );
462
+ }
463
+ })
464
+ #else
465
+ CVT_FUNC(u16, u8, 16,
466
+ ,
467
+ {
468
+ for (size_t i = 0; i < w; i += 16)
469
+ {
470
+ internal::prefetch(_src + i);
471
+ uint16x8_t vline1_u16 = vld1q_u16(_src + i);
472
+ uint16x8_t vline2_u16 = vld1q_u16(_src + i + 8);
473
+
474
+ uint8x8_t vline1_u8 = vqmovn_u16(vline1_u16);
475
+ uint8x8_t vline2_u8 = vqmovn_u16(vline2_u16);
476
+
477
+ vst1q_u8(_dst + i, vcombine_u8(vline1_u8, vline2_u8));
478
+ }
479
+ })
480
+ #endif
481
+
482
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
483
+ CVT_FUNC(u16, s8, 16,
484
+ register uint8x16_t v127 asm ("q4") = vmovq_n_u8(127);,
485
+ {
486
+ for (size_t i = 0; i < w; i += 16)
487
+ {
488
+ internal::prefetch(_src + i);
489
+ __asm__ (
490
+ "vld1.8 {d0-d1}, [%[src1]] \n\t"
491
+ "vqmovn.u16 d4, q0 \n\t"
492
+ "vld1.8 {d2-d3}, [%[src2]] \n\t"
493
+ "vqmovn.u16 d5, q1 \n\t"
494
+ "vmin.u8 q3, q2, q4 \n\t"
495
+ "vst1.8 {d6-d7}, [%[dst]] \n\t"
496
+ : /*no output*/
497
+ : [src1] "r" (_src + i),
498
+ [src2] "r" (_src + i + 8),
499
+ [dst] "r" (_dst + i + 0),
500
+ "w" (v127)
501
+ : "d0","d1","d2","d3","d4","d5","d6","d7"
502
+ );
503
+ }
504
+ })
505
+ #else
506
+ CVT_FUNC(u16, s8, 16,
507
+ uint8x8_t v127 = vmov_n_u8(127);,
508
+ {
509
+ for (size_t i = 0; i < w; i += 16)
510
+ {
511
+ internal::prefetch(_src + i);
512
+ uint16x8_t vline1_u16 = vld1q_u16(_src + i);
513
+ uint16x8_t vline2_u16 = vld1q_u16(_src + i + 8);
514
+
515
+ uint8x8_t vline1_u8 = vqmovn_u16(vline1_u16);
516
+ uint8x8_t vline2_u8 = vqmovn_u16(vline2_u16);
517
+ vline1_u8 = vmin_u8(vline1_u8, v127);
518
+ vline2_u8 = vmin_u8(vline2_u8, v127);
519
+
520
+ vst1q_s8(_dst + i, vcombine_s8(vreinterpret_s8_u8(vline1_u8), vreinterpret_s8_u8(vline2_u8)));
521
+ }
522
+ })
523
+ #endif
524
+
525
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
526
+ CVT_FUNC(u16, s16, 8,
527
+ register uint16x8_t v32767 asm ("q4") = vmovq_n_u16(0x7FFF);,
528
+ {
529
+ for (size_t i = 0; i < w; i += 8)
530
+ {
531
+ internal::prefetch(_src + i);
532
+ __asm__ (
533
+ "vld1.16 {d0-d1}, [%[src]] \n\t"
534
+ "vmin.u16 q1, q0, q4 \n\t"
535
+ "vst1.16 {d2-d3}, [%[dst]] \n\t"
536
+ : /*no output*/
537
+ : [src] "r" (_src + i),
538
+ [dst] "r" (_dst + i + 0),
539
+ "w" (v32767)
540
+ : "d0","d1","d2","d3"
541
+ );
542
+ }
543
+ })
544
+ #else
545
+ CVT_FUNC(u16, s16, 8,
546
+ uint16x8_t v32767 = vmovq_n_u16(0x7FFF);,
547
+ {
548
+ for (size_t i = 0; i < w; i += 8)
549
+ {
550
+ internal::prefetch(_src + i);
551
+ uint16x8_t vline_u16 = vld1q_u16(_src + i);
552
+ vline_u16 = vminq_u16(vline_u16, v32767);
553
+ vst1q_s16((_dst + i), vreinterpretq_s16_u16(vline_u16));
554
+ }
555
+ })
556
+ #endif
557
+
558
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
559
+ CVT_FUNC(u16, s32, 8,
560
+ register uint16x8_t zero0 asm ("q1") = vmovq_n_u16(0);,
561
+ {
562
+ for (size_t i = 0; i < w; i += 8)
563
+ {
564
+ internal::prefetch(_src + i);
565
+ __asm__ (
566
+ "vld1.16 {d0-d1}, [%[src]] \n\t"
567
+ "vst2.16 {d0,d2}, [%[dst1]] \n\t"
568
+ "vst2.16 {d1,d3}, [%[dst2]] \n\t"
569
+ : /*no output*/
570
+ : [src] "r" (_src + i),
571
+ [dst1] "r" (_dst + i),
572
+ [dst2] "r" (_dst + i + 4),
573
+ "w" (zero0)
574
+ : "d0","d1"//,"d2","d3"//,"d4","d5","d6","d7"
575
+ );
576
+ }
577
+ })
578
+ #else
579
+ CVT_FUNC(u16, s32, 8,
580
+ uint16x8x2_t vline;
581
+ vline.val[1] = vmovq_n_u16(0);,
582
+ {
583
+ for (size_t i = 0; i < w; i += 8)
584
+ {
585
+ internal::prefetch(_src + i);
586
+ vline.val[0] = vld1q_u16(_src + i);
587
+ vst2q_u16((uint16_t*)(_dst + i), vline);
588
+ }
589
+ })
590
+ #endif
591
+
592
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
593
+ CVT_FUNC(u16, f32, 8,
594
+ ,
595
+ {
596
+ for (size_t i = 0; i < w; i += 8)
597
+ {
598
+ internal::prefetch(_src + i);
599
+ __asm__ (
600
+ "vld1.16 {d0-d1}, [%[src]] \n\t"
601
+ "vmovl.u16 q1, d0 \n\t"
602
+ "vmovl.u16 q2, d1 \n\t"
603
+ "vcvt.f32.u32 q3, q1 \n\t"
604
+ "vcvt.f32.u32 q4, q2 \n\t"
605
+ "vst1.32 {d6-d7}, [%[dst1]] \n\t"
606
+ "vst1.32 {d8-d9}, [%[dst2]] \n\t"
607
+ : /*no output*/
608
+ : [src] "r" (_src + i),
609
+ [dst1] "r" (_dst + i + 0),
610
+ [dst2] "r" (_dst + i + 4)
611
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9"
612
+ );
613
+ }
614
+ })
615
+ #else
616
+ CVT_FUNC(u16, f32, 8,
617
+ ,
618
+ {
619
+ for (size_t i = 0; i < w; i += 8)
620
+ {
621
+ internal::prefetch(_src + i);
622
+ uint16x8_t vline_u16 = vld1q_u16(_src + i);
623
+
624
+ uint32x4_t vline_u32_lo = vmovl_u16(vget_low_u16(vline_u16));
625
+ uint32x4_t vline_u32_hi = vmovl_u16(vget_high_u16(vline_u16));
626
+
627
+ float32x4_t vline_f32_lo = vcvtq_f32_u32(vline_u32_lo);
628
+ float32x4_t vline_f32_hi = vcvtq_f32_u32(vline_u32_hi);
629
+
630
+ vst1q_f32(_dst + i, vline_f32_lo);
631
+ vst1q_f32(_dst + i + 4, vline_f32_hi);
632
+ }
633
+ })
634
+ #endif
635
+
636
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
637
+ CVT_FUNC(s16, u8, 16,
638
+ ,
639
+ {
640
+ for (size_t i = 0; i < w; i += 16)
641
+ {
642
+ internal::prefetch(_src + i);
643
+ __asm__ (
644
+ "vld1.8 {d0-d1}, [%[src1]] \n\t"
645
+ "vld1.8 {d2-d3}, [%[src2]] \n\t"
646
+ "vqmovun.s16 d4, q0 \n\t"
647
+ "vqmovun.s16 d5, q1 \n\t"
648
+ "vst1.8 {d4-d5}, [%[dst]] \n\t"
649
+ : /*no output*/
650
+ : [src1] "r" (_src + i),
651
+ [src2] "r" (_src + i + 8),
652
+ [dst] "r" (_dst + i + 0)
653
+ : "d0","d1","d2","d3","d4","d5"
654
+ );
655
+ }
656
+ })
657
+ #else
658
+ CVT_FUNC(s16, u8, 16,
659
+ ,
660
+ {
661
+ for (size_t i = 0; i < w; i += 16)
662
+ {
663
+ internal::prefetch(_src + i);
664
+ int16x8_t vline1_s16 = vld1q_s16(_src + i);
665
+ int16x8_t vline2_s16 = vld1q_s16(_src + i + 8);
666
+
667
+ uint8x8_t vline1_u8 = vqmovun_s16(vline1_s16);
668
+ uint8x8_t vline2_u8 = vqmovun_s16(vline2_s16);
669
+
670
+ vst1q_u8(_dst + i, vcombine_u8(vline1_u8, vline2_u8));
671
+ }
672
+ })
673
+ #endif
674
+
675
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
676
+ CVT_FUNC(s16, s8, 16,
677
+ ,
678
+ {
679
+ for (size_t i = 0; i < w; i += 16)
680
+ {
681
+ internal::prefetch(_src + i);
682
+ __asm__ (
683
+ "vld1.8 {d0-d1}, [%[src1]] \n\t"
684
+ "vld1.8 {d2-d3}, [%[src2]] \n\t"
685
+ "vqmovn.s16 d4, q0 \n\t"
686
+ "vqmovn.s16 d5, q1 \n\t"
687
+ "vst1.8 {d4-d5}, [%[dst]] \n\t"
688
+ : /*no output*/
689
+ : [src1] "r" (_src + i),
690
+ [src2] "r" (_src + i + 8),
691
+ [dst] "r" (_dst + i + 0)
692
+ : "d0","d1","d2","d3","d4","d5"
693
+ );
694
+ }
695
+ })
696
+ #else
697
+ CVT_FUNC(s16, s8, 16,
698
+ ,
699
+ {
700
+ for (size_t i = 0; i < w; i += 16)
701
+ {
702
+ internal::prefetch(_src + i);
703
+ int16x8_t vline1_s16 = vld1q_s16(_src + i);
704
+ int16x8_t vline2_s16 = vld1q_s16(_src + i + 8);
705
+
706
+ int8x8_t vline1_s8 = vqmovn_s16(vline1_s16);
707
+ int8x8_t vline2_s8 = vqmovn_s16(vline2_s16);
708
+
709
+ vst1q_s8(_dst + i, vcombine_s8(vline1_s8, vline2_s8));
710
+ }
711
+ })
712
+ #endif
713
+
714
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
715
+ CVT_FUNC(s16, u16, 8,
716
+ register int16x8_t vZero asm ("q4") = vmovq_n_s16(0);,
717
+ {
718
+ for (size_t i = 0; i < w; i += 8)
719
+ {
720
+ internal::prefetch(_src + i);
721
+ __asm__ (
722
+ "vld1.16 {d0-d1}, [%[src]] \n\t"
723
+ "vmax.s16 q1, q0, q4 \n\t"
724
+ "vst1.16 {d2-d3}, [%[dst]] \n\t"
725
+ : /*no output*/
726
+ : [src] "r" (_src + i),
727
+ [dst] "r" (_dst + i + 0),
728
+ "w" (vZero)
729
+ : "d0","d1","d2","d3"
730
+ );
731
+ }
732
+ })
733
+ #else
734
+ CVT_FUNC(s16, u16, 8,
735
+ int16x4_t vZero = vmov_n_s16(0);,
736
+ {
737
+ for (size_t i = 0; i < w; i += 8)
738
+ {
739
+ internal::prefetch(_src + i);
740
+ int16x8_t vline_s16 = vld1q_s16(_src + i);
741
+
742
+ int16x4_t vline_s16_lo = vmax_s16(vget_low_s16(vline_s16), vZero);
743
+ int16x4_t vline_s16_hi = vmax_s16(vget_high_s16(vline_s16), vZero);
744
+
745
+ vst1q_u16(_dst + i, vcombine_u16(vreinterpret_u16_s16(vline_s16_lo), vreinterpret_u16_s16(vline_s16_hi)));
746
+ }
747
+ })
748
+ #endif
749
+
750
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
751
+ CVT_FUNC(s16, s32, 8,
752
+ ,
753
+ {
754
+ for (size_t i = 0; i < w; i += 8)
755
+ {
756
+ internal::prefetch(_src + i);
757
+ __asm__ (
758
+ "vld1.16 {d0-d1}, [%[src]] \n\t"
759
+ "vmovl.s16 q1, d0 \n\t"
760
+ "vmovl.s16 q2, d1 \n\t"
761
+ "vst1.32 {d2-d3}, [%[dst1]] \n\t"
762
+ "vst1.32 {d4-d5}, [%[dst2]] \n\t"
763
+ : /*no output*/
764
+ : [src] "r" (_src + i),
765
+ [dst1] "r" (_dst + i + 0),
766
+ [dst2] "r" (_dst + i + 4)
767
+ : "d0","d1","d2","d3","d4","d5"
768
+ );
769
+ }
770
+ })
771
+ #else
772
+ CVT_FUNC(s16, s32, 8,
773
+ ,
774
+ {
775
+ for (size_t i = 0; i < w; i += 8)
776
+ {
777
+ internal::prefetch(_src + i);
778
+ int16x8_t vline_s16 = vld1q_s16(_src + i);
779
+
780
+ int32x4_t vline_s32_lo = vmovl_s16(vget_low_s16(vline_s16));
781
+ int32x4_t vline_s32_hi = vmovl_s16(vget_high_s16(vline_s16));
782
+
783
+ vst1q_s32(_dst + i, vline_s32_lo);
784
+ vst1q_s32(_dst + i + 4, vline_s32_hi);
785
+ }
786
+ })
787
+ #endif
788
+
789
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
790
+ CVT_FUNC(s16, f32, 8,
791
+ ,
792
+ {
793
+ for (size_t i = 0; i < w; i += 8)
794
+ {
795
+ internal::prefetch(_src + i);
796
+ __asm__ (
797
+ "vld1.16 {d0-d1}, [%[src]] \n\t"
798
+ "vmovl.s16 q1, d0 \n\t"
799
+ "vmovl.s16 q2, d1 \n\t"
800
+ "vcvt.f32.s32 q3, q1 \n\t"
801
+ "vcvt.f32.s32 q4, q2 \n\t"
802
+ "vst1.32 {d6-d7}, [%[dst1]] \n\t"
803
+ "vst1.32 {d8-d9}, [%[dst2]] \n\t"
804
+ : /*no output*/
805
+ : [src] "r" (_src + i),
806
+ [dst1] "r" (_dst + i + 0),
807
+ [dst2] "r" (_dst + i + 4)
808
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9"
809
+ );
810
+ }
811
+ })
812
+ #else
813
+ CVT_FUNC(s16, f32, 8,
814
+ ,
815
+ {
816
+ for (size_t i = 0; i < w; i += 8)
817
+ {
818
+ internal::prefetch(_src + i);
819
+ int16x8_t vline_s16 = vld1q_s16(_src + i);
820
+
821
+ int32x4_t vline_s32_lo = vmovl_s16(vget_low_s16(vline_s16));
822
+ int32x4_t vline_s32_hi = vmovl_s16(vget_high_s16(vline_s16));
823
+ float32x4_t vline_f32_lo = vcvtq_f32_s32(vline_s32_lo);
824
+ float32x4_t vline_f32_hi = vcvtq_f32_s32(vline_s32_hi);
825
+
826
+ vst1q_f32(_dst + i, vline_f32_lo);
827
+ vst1q_f32(_dst + i + 4, vline_f32_hi);
828
+ }
829
+ })
830
+ #endif
831
+
832
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
833
+ CVT_FUNC(s32, u8, 8,
834
+ ,
835
+ {
836
+ for (size_t i = 0; i < w; i += 8)
837
+ {
838
+ internal::prefetch(_src + i);
839
+ __asm__ (
840
+ "vld1.32 {d0-d1}, [%[src1]] \n\t"
841
+ "vld1.32 {d2-d3}, [%[src2]] \n\t"
842
+ "vqmovun.s32 d4, q0 \n\t"
843
+ "vqmovun.s32 d5, q1 \n\t"
844
+ "vqmovn.u16 d6, q2 \n\t"
845
+ "vst1.8 {d6}, [%[dst]] \n\t"
846
+ : /*no output*/
847
+ : [src1] "r" (_src + i + 0),
848
+ [src2] "r" (_src + i + 4),
849
+ [dst] "r" (_dst + i)
850
+ : "d0","d1","d2","d3","d4","d5","d6"
851
+ );
852
+ }
853
+ })
854
+ #else
855
+ CVT_FUNC(s32, u8, 8,
856
+ ,
857
+ {
858
+ for (size_t i = 0; i < w; i += 8)
859
+ {
860
+ internal::prefetch(_src + i);
861
+ int32x4_t vline1_s32 = vld1q_s32(_src + i);
862
+ int32x4_t vline2_s32 = vld1q_s32(_src + i + 4);
863
+
864
+ uint16x4_t vline1_u16 = vqmovun_s32(vline1_s32);
865
+ uint16x4_t vline2_u16 = vqmovun_s32(vline2_s32);
866
+ uint8x8_t vline_u8 = vqmovn_u16(vcombine_u16(vline1_u16, vline2_u16));
867
+
868
+ vst1_u8(_dst + i, vline_u8);
869
+ }
870
+ })
871
+ #endif
872
+
873
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
874
+ CVT_FUNC(s32, s8, 8,
875
+ ,
876
+ {
877
+ for (size_t i = 0; i < w; i += 8)
878
+ {
879
+ internal::prefetch(_src + i);
880
+ __asm__ (
881
+ "vld1.32 {d0-d1}, [%[src1]] \n\t"
882
+ "vld1.32 {d2-d3}, [%[src2]] \n\t"
883
+ "vqmovn.s32 d4, q0 \n\t"
884
+ "vqmovn.s32 d5, q1 \n\t"
885
+ "vqmovn.s16 d6, q2 \n\t"
886
+ "vst1.8 {d6}, [%[dst]] \n\t"
887
+ : /*no output*/
888
+ : [src1] "r" (_src + i + 0),
889
+ [src2] "r" (_src + i + 4),
890
+ [dst] "r" (_dst + i)
891
+ : "d0","d1","d2","d3","d4","d5","d6"
892
+ );
893
+ }
894
+ })
895
+ #else
896
+ CVT_FUNC(s32, s8, 8,
897
+ ,
898
+ {
899
+ for (size_t i = 0; i < w; i += 8)
900
+ {
901
+ internal::prefetch(_src + i);
902
+ int32x4_t vline1_s32 = vld1q_s32(_src + i);
903
+ int32x4_t vline2_s32 = vld1q_s32(_src + i + 4);
904
+
905
+ int16x4_t vline1_s16 = vqmovn_s32(vline1_s32);
906
+ int16x4_t vline2_s16 = vqmovn_s32(vline2_s32);
907
+ int8x8_t vline_s8 = vqmovn_s16(vcombine_s16(vline1_s16, vline2_s16));
908
+
909
+ vst1_s8(_dst + i, vline_s8);
910
+ }
911
+ })
912
+ #endif
913
+
914
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
915
+ CVT_FUNC(s32, u16, 8,
916
+ ,
917
+ {
918
+ for (size_t i = 0; i < w; i += 8)
919
+ {
920
+ internal::prefetch(_src + i);
921
+ __asm__ (
922
+ "vld1.32 {d0-d1}, [%[src1]] \n\t"
923
+ "vld1.32 {d2-d3}, [%[src2]] \n\t"
924
+ "vqmovun.s32 d4, q0 \n\t"
925
+ "vqmovun.s32 d5, q1 \n\t"
926
+ "vst1.16 {d4-d5}, [%[dst]] \n\t"
927
+ : /*no output*/
928
+ : [src1] "r" (_src + i + 0),
929
+ [src2] "r" (_src + i + 4),
930
+ [dst] "r" (_dst + i)
931
+ : "d0","d1","d2","d3","d4","d5"
932
+ );
933
+ }
934
+ })
935
+ #else
936
+ CVT_FUNC(s32, u16, 8,
937
+ ,
938
+ {
939
+ for (size_t i = 0; i < w; i += 8)
940
+ {
941
+ internal::prefetch(_src + i);
942
+ int32x4_t vline1_s32 = vld1q_s32(_src + i);
943
+ int32x4_t vline2_s32 = vld1q_s32(_src + i + 4);
944
+
945
+ uint16x4_t vline1_u16 = vqmovun_s32(vline1_s32);
946
+ uint16x4_t vline2_u16 = vqmovun_s32(vline2_s32);
947
+
948
+ vst1q_u16(_dst + i, vcombine_u16(vline1_u16, vline2_u16));
949
+ }
950
+ })
951
+ #endif
952
+
953
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
954
+ CVT_FUNC(s32, s16, 8,
955
+ ,
956
+ {
957
+ for (size_t i = 0; i < w; i += 8)
958
+ {
959
+ internal::prefetch(_src + i);
960
+ __asm__ (
961
+ "vld1.32 {d0-d1}, [%[src1]] \n\t"
962
+ "vld1.32 {d2-d3}, [%[src2]] \n\t"
963
+ "vqmovn.s32 d4, q0 \n\t"
964
+ "vqmovn.s32 d5, q1 \n\t"
965
+ "vst1.8 {d4-d5}, [%[dst]] \n\t"
966
+ : /*no output*/
967
+ : [src1] "r" (_src + i + 0),
968
+ [src2] "r" (_src + i + 4),
969
+ [dst] "r" (_dst + i)
970
+ : "d0","d1","d2","d3","d4","d5"
971
+ );
972
+ }
973
+ })
974
+ #else
975
+ CVT_FUNC(s32, s16, 8,
976
+ ,
977
+ {
978
+ for (size_t i = 0; i < w; i += 8)
979
+ {
980
+ internal::prefetch(_src + i);
981
+ int32x4_t vline1_s32 = vld1q_s32(_src + i);
982
+ int32x4_t vline2_s32 = vld1q_s32(_src + i + 4);
983
+
984
+ int16x4_t vline1_s16 = vqmovn_s32(vline1_s32);
985
+ int16x4_t vline2_s16 = vqmovn_s32(vline2_s32);
986
+
987
+ vst1q_s16(_dst + i, vcombine_s16(vline1_s16, vline2_s16));
988
+ }
989
+ })
990
+ #endif
991
+
992
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
993
+ CVT_FUNC(s32, f32, 8,
994
+ ,
995
+ {
996
+ for (size_t i = 0; i < w; i += 8)
997
+ {
998
+ internal::prefetch(_src + i);
999
+ __asm__ (
1000
+ "vld1.32 {d0-d1}, [%[src]] \n\t"
1001
+ "vcvt.f32.s32 q1, q0 \n\t"
1002
+ "vst1.32 {d2-d3}, [%[dst]] \n\t"
1003
+ : /*no output*/
1004
+ : [src] "r" (_src + i),
1005
+ [dst] "r" (_dst + i)
1006
+ : "d0","d1","d2","d3"//,"d4","d5"
1007
+ );
1008
+ __asm__ (
1009
+ "vld1.32 {d0-d1}, [%[src]] \n\t"
1010
+ "vcvt.f32.s32 q1, q0 \n\t"
1011
+ "vst1.32 {d2-d3}, [%[dst]] \n\t"
1012
+ : /*no output*/
1013
+ : [src] "r" (_src + i + 4),
1014
+ [dst] "r" (_dst + i + 4)
1015
+ : "d0","d1","d2","d3"//,"d4","d5"
1016
+ );
1017
+ }
1018
+ })
1019
+ #else
1020
+ CVT_FUNC(s32, f32, 8,
1021
+ ,
1022
+ {
1023
+ for (size_t i = 0; i < w; i += 8)
1024
+ {
1025
+ internal::prefetch(_src + i);
1026
+ int32x4_t vline_s32 = vld1q_s32(_src + i);
1027
+ float32x4_t vline_f32 = vcvtq_f32_s32(vline_s32);
1028
+ vst1q_f32(_dst + i, vline_f32);
1029
+
1030
+ vline_s32 = vld1q_s32(_src + i + 4);
1031
+ vline_f32 = vcvtq_f32_s32(vline_s32);
1032
+ vst1q_f32(_dst + i + 4, vline_f32);
1033
+ }
1034
+ })
1035
+ #endif
1036
+
1037
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
1038
+ CVT_FUNC(f32, u8, 8,
1039
+ register float32x4_t vmult asm ("q0") = vdupq_n_f32((float)(1 << 16));
1040
+ register uint32x4_t vmask asm ("q1") = vdupq_n_u32(1<<16);,
1041
+ {
1042
+ for (size_t i = 0; i < w; i += 8)
1043
+ {
1044
+ internal::prefetch(_src + i);
1045
+ __asm__ (
1046
+ "vld1.32 {d4-d5}, [%[src1]] \n\t"
1047
+ "vld1.32 {d6-d7}, [%[src2]] \n\t"
1048
+ "vmul.f32 q4, q2, q0 \n\t"
1049
+ "vmul.f32 q5, q3, q0 \n\t"
1050
+ "vcvt.u32.f32 q6, q4 \n\t"
1051
+ "vcvt.u32.f32 q7, q5 \n\t"
1052
+ "vbic q8, q1, q6 \n\t"
1053
+ "vbic q9, q1, q7 \n\t"
1054
+ "vshr.u32 q10, q8, #16 \n\t"
1055
+ "vshr.u32 q11, q9, #16 \n\t"
1056
+ "vqsub.u32 q12, q6, q10 \n\t"
1057
+ "vqsub.u32 q13, q7, q11 \n\t"
1058
+ "vqrshrn.u32 d28, q12, #16 \n\t"
1059
+ "vqrshrn.u32 d29, q13, #16 \n\t"
1060
+ "vqmovn.u16 d30, q14 \n\t"
1061
+ "vst1.8 {d30}, [%[dst]] \n\t"
1062
+ : /*no output*/
1063
+ : [src1] "r" (_src + i + 0),
1064
+ [src2] "r" (_src + i + 4),
1065
+ [dst] "r" (_dst + i),
1066
+ "w" (vmult), "w" (vmask)
1067
+ : "d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23","d24","d25","d26","d27","d28","d29","d30"
1068
+ );
1069
+ }
1070
+ })
1071
+ #else
1072
+ CVT_FUNC(f32, u8, 8,
1073
+ float32x4_t vmult = vdupq_n_f32((float)(1 << 16));
1074
+ uint32x4_t vmask = vdupq_n_u32(1<<16);,
1075
+ {
1076
+ for (size_t i = 0; i < w; i += 8)
1077
+ {
1078
+ internal::prefetch(_src + i);
1079
+ float32x4_t vline1_f32 = vld1q_f32(_src + i);
1080
+ float32x4_t vline2_f32 = vld1q_f32(_src + i + 4);
1081
+
1082
+ float32x4_t vline1w_f32 = vmulq_f32(vline1_f32, vmult);
1083
+ float32x4_t vline2w_f32 = vmulq_f32(vline2_f32, vmult);
1084
+
1085
+ uint32x4_t vline1_u32 = vcvtq_u32_f32(vline1w_f32);
1086
+ uint32x4_t vline2_u32 = vcvtq_u32_f32(vline2w_f32);
1087
+
1088
+ uint32x4_t vl1_masked = vbicq_u32(vmask, vline1_u32);
1089
+ uint32x4_t vl2_masked = vbicq_u32(vmask, vline2_u32);
1090
+ uint32x4_t vl1_masked2 = vshrq_n_u32(vl1_masked, 16);
1091
+ uint32x4_t vl2_masked2 = vshrq_n_u32(vl2_masked, 16);
1092
+ uint32x4_t vline1r_u32 = vqsubq_u32(vline1_u32, vl1_masked2);
1093
+ uint32x4_t vline2r_u32 = vqsubq_u32(vline2_u32, vl2_masked2);
1094
+
1095
+ uint16x4_t vline1_u16 = vqrshrn_n_u32(vline1r_u32, 16);
1096
+ uint16x4_t vline2_u16 = vqrshrn_n_u32(vline2r_u32, 16);
1097
+
1098
+ uint8x8_t vline_u8 = vqmovn_u16(vcombine_u16(vline1_u16, vline2_u16));
1099
+ vst1_u8(_dst + i, vline_u8);
1100
+ }
1101
+ })
1102
+ #endif
1103
+
1104
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
1105
+ CVT_FUNC(f32, s8, 8,
1106
+ register float32x4_t vhalf asm ("q0") = vdupq_n_f32(0.5f);,
1107
+ {
1108
+ for (size_t i = 0; i < w; i += 8)
1109
+ {
1110
+ internal::prefetch(_src + i);
1111
+ __asm__ (
1112
+ "vld1.32 {d2-d3}, [%[src1]] \n\t"
1113
+ "vld1.32 {d4-d5}, [%[src2]] \n\t"
1114
+ "vadd.f32 q3, q1, q0 \n\t"
1115
+ "vadd.f32 q4, q2, q0 \n\t"
1116
+ "vcvt.s32.f32 q5, q3 \n\t"
1117
+ "vcvt.s32.f32 q6, q4 \n\t"
1118
+ "vqmovn.s32 d14, q5 \n\t"
1119
+ "vqmovn.s32 d15, q6 \n\t"
1120
+ "vqmovn.s16 d16, q7 \n\t"
1121
+ "vst1.8 {d16}, [%[dst]] \n\t"
1122
+ : /*no output*/
1123
+ : [src1] "r" (_src + i + 0),
1124
+ [src2] "r" (_src + i + 4),
1125
+ [dst] "r" (_dst + i),
1126
+ "w" (vhalf)
1127
+ : "d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17"
1128
+ );
1129
+ }
1130
+ })
1131
+ #else
1132
+ CVT_FUNC(f32, s8, 8,
1133
+ float32x4_t vhalf = vdupq_n_f32(0.5f);,
1134
+ {
1135
+ for (size_t i = 0; i < w; i += 8)
1136
+ {
1137
+ internal::prefetch(_src + i);
1138
+ float32x4_t vline1_f32 = vld1q_f32(_src + i);
1139
+ float32x4_t vline2_f32 = vld1q_f32(_src + i + 4);
1140
+
1141
+ vline1_f32 = vaddq_f32(vline1_f32, vhalf);
1142
+ vline2_f32 = vaddq_f32(vline2_f32, vhalf);
1143
+
1144
+ int32x4_t vline1_s32 = vcvtq_s32_f32(vline1_f32);
1145
+ int32x4_t vline2_s32 = vcvtq_s32_f32(vline2_f32);
1146
+ int16x4_t vline1_s16 = vqmovn_s32(vline1_s32);
1147
+ int16x4_t vline2_s16 = vqmovn_s32(vline2_s32);
1148
+
1149
+ int8x8_t vline_s8 = vqmovn_s16(vcombine_s16(vline1_s16, vline2_s16));
1150
+
1151
+ vst1_s8(_dst + i, vline_s8);
1152
+ }
1153
+ })
1154
+ #endif
1155
+
1156
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
1157
+ CVT_FUNC(f32, u16, 8,
1158
+ register float32x4_t vhalf asm ("q0") = vdupq_n_f32(0.5f);,
1159
+ {
1160
+ for (size_t i = 0; i < w; i += 8)
1161
+ {
1162
+ internal::prefetch(_src + i);
1163
+ __asm__ (
1164
+ "vld1.32 {d2-d3}, [%[src]] \n\t"
1165
+ "vadd.f32 q2, q1, q0 \n\t"
1166
+ "vcvt.u32.f32 q3, q2 \n\t"
1167
+ "vqmovn.u32 d8, q3 \n\t"
1168
+ "vst1.16 {d8}, [%[dst]] \n\t"
1169
+ : /*no output*/
1170
+ : [src] "r" (_src + i),
1171
+ [dst] "r" (_dst + i),
1172
+ "w" (vhalf)
1173
+ : "d2","d3","d4","d5","d6","d7","d8"
1174
+ );
1175
+ __asm__ (
1176
+ "vld1.32 {d2-d3}, [%[src]] \n\t"
1177
+ "vadd.f32 q2, q1, q0 \n\t"
1178
+ "vcvt.u32.f32 q3, q2 \n\t"
1179
+ "vqmovn.u32 d8, q3 \n\t"
1180
+ "vst1.16 {d8}, [%[dst]] \n\t"
1181
+ : /*no output*/
1182
+ : [src] "r" (_src + i + 4),
1183
+ [dst] "r" (_dst + i + 4),
1184
+ "w" (vhalf)
1185
+ : "d2","d3","d4","d5","d6","d7","d8"
1186
+ );
1187
+ }
1188
+ })
1189
+ #else
1190
+ CVT_FUNC(f32, u16, 8,
1191
+ float32x4_t vhalf = vdupq_n_f32(0.5f);,
1192
+ {
1193
+ for (size_t i = 0; i < w; i += 8)
1194
+ {
1195
+ internal::prefetch(_src + i);
1196
+ float32x4_t vline_f32 = vld1q_f32(_src + i);
1197
+
1198
+ vline_f32 = vaddq_f32(vline_f32, vhalf);
1199
+ uint32x4_t vline_u32 = vcvtq_u32_f32(vline_f32);
1200
+ uint16x4_t vline_u16 = vqmovn_u32(vline_u32);
1201
+
1202
+ vst1_u16(_dst + i, vline_u16);
1203
+
1204
+ vline_f32 = vld1q_f32(_src + i + 4);
1205
+
1206
+ vline_f32 = vaddq_f32(vline_f32, vhalf);
1207
+ vline_u32 = vcvtq_u32_f32(vline_f32);
1208
+ vline_u16 = vqmovn_u32(vline_u32);
1209
+
1210
+ vst1_u16(_dst + i + 4, vline_u16);
1211
+ }
1212
+ })
1213
+ #endif
1214
+
1215
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
1216
+ CVT_FUNC(f32, s16, 8,
1217
+ register float32x4_t vhalf asm ("q0") = vdupq_n_f32(0.5f);,
1218
+ {
1219
+ for (size_t i = 0; i < w; i += 8)
1220
+ {
1221
+ internal::prefetch(_src + i);
1222
+ __asm__ (
1223
+ "vld1.32 {d2-d3}, [%[src]] \n\t"
1224
+ "vadd.f32 q2, q1, q0 \n\t"
1225
+ "vcvt.s32.f32 q3, q2 \n\t"
1226
+ "vqmovn.s32 d8, q3 \n\t"
1227
+ "vst1.16 {d8}, [%[dst]] \n\t"
1228
+ : /*no output*/
1229
+ : [src] "r" (_src + i),
1230
+ [dst] "r" (_dst + i),
1231
+ "w" (vhalf)
1232
+ : "d2","d3","d4","d5","d6","d7","d8"
1233
+ );
1234
+ __asm__ (
1235
+ "vld1.32 {d2-d3}, [%[src]] \n\t"
1236
+ "vadd.f32 q2, q1, q0 \n\t"
1237
+ "vcvt.s32.f32 q3, q2 \n\t"
1238
+ "vqmovn.s32 d8, q3 \n\t"
1239
+ "vst1.16 {d8}, [%[dst]] \n\t"
1240
+ : /*no output*/
1241
+ : [src] "r" (_src + i + 4),
1242
+ [dst] "r" (_dst + i + 4),
1243
+ "w" (vhalf)
1244
+ : "d2","d3","d4","d5","d6","d7","d8"
1245
+ );
1246
+ }
1247
+ })
1248
+ #else
1249
+ CVT_FUNC(f32, s16, 8,
1250
+ float32x4_t vhalf = vdupq_n_f32(0.5f);,
1251
+ {
1252
+ for (size_t i = 0; i < w; i += 8)
1253
+ {
1254
+ internal::prefetch(_src + i);
1255
+ float32x4_t vline_f32 = vld1q_f32(_src + i);
1256
+
1257
+ vline_f32 = vaddq_f32(vline_f32, vhalf);
1258
+ int32x4_t vline_s32 = vcvtq_s32_f32(vline_f32);
1259
+ int16x4_t vline_s16 = vqmovn_s32(vline_s32);
1260
+
1261
+ vst1_s16(_dst + i, vline_s16);
1262
+
1263
+ vline_f32 = vld1q_f32(_src + i + 4);
1264
+
1265
+ vline_f32 = vaddq_f32(vline_f32, vhalf);
1266
+ vline_s32 = vcvtq_s32_f32(vline_f32);
1267
+ vline_s16 = vqmovn_s32(vline_s32);
1268
+
1269
+ vst1_s16(_dst + i + 4, vline_s16);
1270
+ }
1271
+ })
1272
+ #endif
1273
+
1274
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 6 && !defined(__clang__)
1275
+ CVT_FUNC(f32, s32, 8,
1276
+ register float32x4_t vhalf asm ("q0") = vdupq_n_f32(0.5f);,
1277
+ {
1278
+ for (size_t i = 0; i < w; i += 8)
1279
+ {
1280
+ internal::prefetch(_src + i);
1281
+ __asm__ (
1282
+ "vld1.32 {d2-d3}, [%[src1]] \n\t"
1283
+ "vld1.32 {d4-d5}, [%[src2]] \n\t"
1284
+ "vadd.f32 q3, q1, q0 \n\t"
1285
+ "vadd.f32 q4, q2, q0 \n\t"
1286
+ "vcvt.s32.f32 q5, q3 \n\t"
1287
+ "vcvt.s32.f32 q6, q4 \n\t"
1288
+ "vst1.32 {q5}, [%[dst1]] \n\t"
1289
+ "vst1.32 {q6}, [%[dst2]] \n\t"
1290
+ : /*no output*/
1291
+ : [src1] "r" (_src + i),
1292
+ [src2] "r" (_src + i + 4),
1293
+ [dst1] "r" (_dst + i),
1294
+ [dst2] "r" (_dst + i + 4),
1295
+ "w" (vhalf)
1296
+ : "d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13"
1297
+ );
1298
+ }
1299
+ })
1300
+ #else
1301
+ CVT_FUNC(f32, s32, 8,
1302
+ float32x4_t vhalf = vdupq_n_f32(0.5f);,
1303
+ {
1304
+ for (size_t i = 0; i < w; i += 8)
1305
+ {
1306
+ internal::prefetch(_src + i);
1307
+ float32x4_t vline_f32 = vld1q_f32(_src + i);
1308
+
1309
+ vline_f32 = vaddq_f32(vline_f32, vhalf);
1310
+ int32x4_t vline_s32 = vcvtq_s32_f32(vline_f32);
1311
+
1312
+ vst1q_s32(_dst + i, vline_s32);
1313
+
1314
+ vline_f32 = vld1q_f32(_src + i + 4);
1315
+
1316
+ vline_f32 = vaddq_f32(vline_f32, vhalf);
1317
+ vline_s32 = vcvtq_s32_f32(vline_f32);
1318
+
1319
+ vst1q_s32(_dst + i + 4, vline_s32);
1320
+ }
1321
+ })
1322
+ #endif
1323
+
1324
+ void convert(const Size2D &_size,
1325
+ const u8 * srcBase, ptrdiff_t srcStride,
1326
+ s16 * dstBase, ptrdiff_t dstStride)
1327
+ {
1328
+ convert(_size, srcBase, srcStride, (u16*)dstBase, dstStride);
1329
+ }
1330
+
1331
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/convert_depth.cpp ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include <cstring>
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ #ifdef CAROTENE_NEON
47
+
48
+ namespace {
49
+
50
+ template <int shift>
51
+ void lshiftConst(const Size2D &size,
52
+ const u8 * srcBase, ptrdiff_t srcStride,
53
+ s16 * dstBase, ptrdiff_t dstStride)
54
+ {
55
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
56
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
57
+
58
+ for (size_t i = 0; i < size.height; ++i)
59
+ {
60
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
61
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
62
+ size_t j = 0;
63
+
64
+ for (; j < roiw16; j += 16)
65
+ {
66
+ internal::prefetch(src + j);
67
+ uint8x16_t v_src = vld1q_u8(src + j);
68
+ int16x8_t v_dst0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src)));
69
+ int16x8_t v_dst1 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src)));
70
+
71
+ vst1q_s16(dst + j, vshlq_n_s16(v_dst0, shift));
72
+ vst1q_s16(dst + j + 8, vshlq_n_s16(v_dst1, shift));
73
+ }
74
+ for (; j < roiw8; j += 8)
75
+ {
76
+ int16x8_t v_dst = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(src + j)));
77
+ vst1q_s16(dst + j, vshlq_n_s16(v_dst, shift));
78
+ }
79
+
80
+ for (; j < size.width; j++)
81
+ {
82
+ dst[j] = ((s16)src[j] << shift);
83
+ }
84
+ }
85
+ }
86
+
87
+ template <>
88
+ void lshiftConst<0>(const Size2D &size,
89
+ const u8 * srcBase, ptrdiff_t srcStride,
90
+ s16 * dstBase, ptrdiff_t dstStride)
91
+ {
92
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
93
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
94
+
95
+ for (size_t i = 0; i < size.height; ++i)
96
+ {
97
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
98
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
99
+ size_t j = 0;
100
+
101
+ for (; j < roiw16; j += 16)
102
+ {
103
+ internal::prefetch(src + j);
104
+ uint8x16_t v_src = vld1q_u8(src + j);
105
+ int16x8_t v_dst0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src)));
106
+ int16x8_t v_dst1 = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src)));
107
+
108
+ vst1q_s16(dst + j, v_dst0);
109
+ vst1q_s16(dst + j + 8, v_dst1);
110
+ }
111
+ for (; j < roiw8; j += 8)
112
+ {
113
+ int16x8_t v_dst = vreinterpretq_s16_u16(vmovl_u8(vld1_u8(src + j)));
114
+ vst1q_s16(dst + j, v_dst);
115
+ }
116
+
117
+ for (; j < size.width; j++)
118
+ {
119
+ dst[j] = (s16)src[j];
120
+ }
121
+ }
122
+ }
123
+
124
+ template <int shift>
125
+ void rshiftConst(const Size2D &size,
126
+ const s16 * srcBase, ptrdiff_t srcStride,
127
+ u8 * dstBase, ptrdiff_t dstStride,
128
+ CONVERT_POLICY cpolicy)
129
+ {
130
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
131
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
132
+
133
+ for (size_t i = 0; i < size.height; ++i)
134
+ {
135
+ const s16 * src = internal::getRowPtr(srcBase, srcStride, i);
136
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
137
+ size_t j = 0;
138
+
139
+ if (cpolicy == CONVERT_POLICY_SATURATE)
140
+ {
141
+ for (; j < roiw16; j += 16)
142
+ {
143
+ internal::prefetch(src + j);
144
+ int16x8_t v_src0 = vshrq_n_s16(vld1q_s16(src + j), shift),
145
+ v_src1 = vshrq_n_s16(vld1q_s16(src + j + 8), shift);
146
+ uint8x16_t v_dst = vcombine_u8(vqmovun_s16(v_src0),
147
+ vqmovun_s16(v_src1));
148
+ vst1q_u8(dst + j, v_dst);
149
+ }
150
+ for (; j < roiw8; j += 8)
151
+ {
152
+ int16x8_t v_src = vshrq_n_s16(vld1q_s16(src + j), shift);
153
+ vst1_u8(dst + j, vqmovun_s16(v_src));
154
+ }
155
+
156
+ for (; j < size.width; j++)
157
+ {
158
+ dst[j] = internal::saturate_cast<u8>((src[j] >> shift));
159
+ }
160
+ }
161
+ else // CONVERT_POLICY_WRAP
162
+ {
163
+ for (; j < roiw16; j += 16)
164
+ {
165
+ internal::prefetch(src + j);
166
+ int16x8_t v_src0 = vshrq_n_s16(vld1q_s16(src + j), shift),
167
+ v_src1 = vshrq_n_s16(vld1q_s16(src + j + 8), shift);
168
+ int8x16_t v_dst = vcombine_s8(vmovn_s16(v_src0),
169
+ vmovn_s16(v_src1));
170
+ vst1q_u8(dst + j, vreinterpretq_u8_s8(v_dst));
171
+ }
172
+ for (; j < roiw8; j += 8)
173
+ {
174
+ int16x8_t v_src = vshrq_n_s16(vld1q_s16(src + j), shift);
175
+ vst1_u8(dst + j, vreinterpret_u8_s8(vmovn_s16(v_src)));
176
+ }
177
+
178
+ for (; j < size.width; j++)
179
+ {
180
+ dst[j] = (u8)((src[j] >> shift));
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ template <>
187
+ void rshiftConst<0>(const Size2D &size,
188
+ const s16 * srcBase, ptrdiff_t srcStride,
189
+ u8 * dstBase, ptrdiff_t dstStride,
190
+ CONVERT_POLICY cpolicy)
191
+ {
192
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
193
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
194
+
195
+ for (size_t i = 0; i < size.height; ++i)
196
+ {
197
+ const s16 * src = internal::getRowPtr(srcBase, srcStride, i);
198
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
199
+ size_t j = 0;
200
+
201
+ if (cpolicy == CONVERT_POLICY_SATURATE)
202
+ {
203
+ for (; j < roiw16; j += 16)
204
+ {
205
+ internal::prefetch(src + j);
206
+ int16x8_t v_src0 = vld1q_s16(src + j), v_src1 = vld1q_s16(src + j + 8);
207
+ uint8x16_t v_dst = vcombine_u8(vqmovun_s16(v_src0), vqmovun_s16(v_src1));
208
+ vst1q_u8(dst + j, v_dst);
209
+ }
210
+ for (; j < roiw8; j += 8)
211
+ {
212
+ int16x8_t v_src = vld1q_s16(src + j);
213
+ vst1_u8(dst + j, vqmovun_s16(v_src));
214
+ }
215
+
216
+ for (; j < size.width; j++)
217
+ {
218
+ dst[j] = internal::saturate_cast<u8>(src[j]);
219
+ }
220
+ }
221
+ else // CONVERT_POLICY_WRAP
222
+ {
223
+ for (; j < roiw16; j += 16)
224
+ {
225
+ internal::prefetch(src + j);
226
+ int16x8_t v_src0 = vld1q_s16(src + j), v_src1 = vld1q_s16(src + j + 8);
227
+ int8x16_t v_dst = vcombine_s8(vmovn_s16(v_src0), vmovn_s16(v_src1));
228
+ vst1q_u8(dst + j, vreinterpretq_u8_s8(v_dst));
229
+ }
230
+ for (; j < roiw8; j += 8)
231
+ {
232
+ int16x8_t v_src = vld1q_s16(src + j);
233
+ vst1_u8(dst + j, vreinterpret_u8_s8(vmovn_s16(v_src)));
234
+ }
235
+
236
+ for (; j < size.width; j++)
237
+ {
238
+ dst[j] = (u8)src[j];
239
+ }
240
+ }
241
+ }
242
+ }
243
+
244
+ typedef void (* lshiftConstFunc)(const Size2D &size,
245
+ const u8 * srcBase, ptrdiff_t srcStride,
246
+ s16 * dstBase, ptrdiff_t dstStride);
247
+
248
+ typedef void (* rshiftConstFunc)(const Size2D &size,
249
+ const s16 * srcBase, ptrdiff_t srcStride,
250
+ u8 * dstBase, ptrdiff_t dstStride,
251
+ CONVERT_POLICY cpolicy);
252
+
253
+ } // namespace
254
+
255
+ #endif
256
+
257
+ void lshift(const Size2D &size,
258
+ const u8 * srcBase, ptrdiff_t srcStride,
259
+ s16 * dstBase, ptrdiff_t dstStride,
260
+ u32 shift)
261
+ {
262
+ internal::assertSupportedConfiguration();
263
+
264
+ #ifdef CAROTENE_NEON
265
+ if (shift >= 16u)
266
+ {
267
+ for (size_t i = 0; i < size.height; ++i)
268
+ {
269
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
270
+ std::memset(dst, 0, sizeof(s16) * size.width);
271
+ }
272
+ return;
273
+ }
274
+
275
+ // this ugly contruction is needed to avoid:
276
+ // /usr/lib/gcc/arm-linux-gnueabihf/4.8/include/arm_neon.h:3581:59: error: argument must be a constant
277
+ // return (int16x8_t)__builtin_neon_vshl_nv8hi (__a, __b, 1);
278
+
279
+ lshiftConstFunc funcs[16] =
280
+ {
281
+ lshiftConst<0>,
282
+ lshiftConst<1>,
283
+ lshiftConst<2>,
284
+ lshiftConst<3>,
285
+ lshiftConst<4>,
286
+ lshiftConst<5>,
287
+ lshiftConst<6>,
288
+ lshiftConst<7>,
289
+ lshiftConst<8>,
290
+ lshiftConst<9>,
291
+ lshiftConst<10>,
292
+ lshiftConst<11>,
293
+ lshiftConst<12>,
294
+ lshiftConst<13>,
295
+ lshiftConst<14>,
296
+ lshiftConst<15>
297
+ }, func = funcs[shift];
298
+
299
+ func(size, srcBase, srcStride, dstBase, dstStride);
300
+ #else
301
+ (void)size;
302
+ (void)srcBase;
303
+ (void)srcStride;
304
+ (void)dstBase;
305
+ (void)dstStride;
306
+ (void)shift;
307
+ #endif
308
+ }
309
+
310
+ void rshift(const Size2D &size,
311
+ const s16 * srcBase, ptrdiff_t srcStride,
312
+ u8 * dstBase, ptrdiff_t dstStride,
313
+ u32 shift, CONVERT_POLICY cpolicy)
314
+ {
315
+ internal::assertSupportedConfiguration();
316
+
317
+ #ifdef CAROTENE_NEON
318
+ if (shift >= 16)
319
+ {
320
+ if (cpolicy == CONVERT_POLICY_WRAP)
321
+ {
322
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
323
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
324
+ int16x8_t v_zero = vdupq_n_s16(0);
325
+
326
+ for (size_t i = 0; i < size.height; ++i)
327
+ {
328
+ const s16 * src = internal::getRowPtr(srcBase, srcStride, i);
329
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
330
+ size_t j = 0;
331
+
332
+ for (; j < roiw16; j += 16)
333
+ {
334
+ internal::prefetch(src + j);
335
+ int16x8_t v_src0 = vld1q_s16(src + j), v_src1 = vld1q_s16(src + j + 8);
336
+ uint8x16_t v_dst = vcombine_u8(vmovn_u16(vcltq_s16(v_src0, v_zero)),
337
+ vmovn_u16(vcltq_s16(v_src1, v_zero)));
338
+ vst1q_u8(dst + j, v_dst);
339
+ }
340
+ for (; j < roiw8; j += 8)
341
+ {
342
+ int16x8_t v_src = vld1q_s16(src + j);
343
+ vst1_u8(dst + j, vmovn_u16(vcltq_s16(v_src, v_zero)));
344
+ }
345
+
346
+ for (; j < size.width; j++)
347
+ {
348
+ dst[j] = src[j] >= 0 ? 0 : 255;
349
+ }
350
+ }
351
+ }
352
+ else
353
+ {
354
+ for (size_t i = 0; i < size.height; ++i)
355
+ {
356
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
357
+ std::memset(dst, 0, sizeof(u8) * size.width);
358
+ }
359
+ }
360
+ return;
361
+ }
362
+
363
+ // this ugly contruction is needed to avoid:
364
+ // /usr/lib/gcc/arm-linux-gnueabihf/4.8/include/arm_neon.h:3581:59: error: argument must be a constant
365
+ // return (int16x8_t)__builtin_neon_vshr_nv8hi (__a, __b, 1);
366
+
367
+ rshiftConstFunc funcs[16] =
368
+ {
369
+ rshiftConst<0>,
370
+ rshiftConst<1>,
371
+ rshiftConst<2>,
372
+ rshiftConst<3>,
373
+ rshiftConst<4>,
374
+ rshiftConst<5>,
375
+ rshiftConst<6>,
376
+ rshiftConst<7>,
377
+ rshiftConst<8>,
378
+ rshiftConst<9>,
379
+ rshiftConst<10>,
380
+ rshiftConst<11>,
381
+ rshiftConst<12>,
382
+ rshiftConst<13>,
383
+ rshiftConst<14>,
384
+ rshiftConst<15>
385
+ }, func = funcs[shift];
386
+
387
+ func(size, srcBase, srcStride, dstBase, dstStride, cpolicy);
388
+ #else
389
+ (void)size;
390
+ (void)srcBase;
391
+ (void)srcStride;
392
+ (void)dstBase;
393
+ (void)dstStride;
394
+ (void)shift;
395
+ (void)cpolicy;
396
+ #endif
397
+ }
398
+
399
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/convert_scale.cpp ADDED
The diff for this file is too large to render. See raw diff
 
3rdparty/carotene/src/convolution.cpp ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "saturate_cast.hpp"
42
+
43
+ namespace CAROTENE_NS {
44
+
45
+ bool isConvolutionSupported(const Size2D &size, const Size2D &ksize,
46
+ BORDER_MODE border)
47
+ {
48
+ return isSupportedConfiguration() && size.width >= 8 &&
49
+ (border == BORDER_MODE_CONSTANT ||
50
+ border == BORDER_MODE_REPLICATE) &&
51
+ (ksize.width == 3) && (ksize.height == 3);
52
+ }
53
+
54
+ #ifdef CAROTENE_NEON
55
+
56
+ namespace {
57
+
58
+ template <int shift>
59
+ int32x4_t vshrq_s32(int32x4_t value)
60
+ {
61
+ return vshrq_n_s32(value, shift);
62
+ }
63
+
64
+ template <>
65
+ int32x4_t vshrq_s32<0>(int32x4_t value)
66
+ {
67
+ return value;
68
+ }
69
+
70
+ } // namespace
71
+
72
+ typedef int32x4_t (* vshrq_s32_func)(int32x4_t value);
73
+
74
+ #endif
75
+
76
+ void convolution(const Size2D &size,
77
+ const u8 * srcBase, ptrdiff_t srcStride,
78
+ u8 * dstBase, ptrdiff_t dstStride,
79
+ BORDER_MODE border, u8 borderValue,
80
+ const Size2D & ksize, s16 * kernelBase, u32 scale)
81
+ {
82
+ internal::assertSupportedConfiguration(isConvolutionSupported(size, ksize, border));
83
+ #ifdef CAROTENE_NEON
84
+ const uint8x8_t v_zero_u8 = vdup_n_u8(0);
85
+ const uint8x8_t v_border = vdup_n_u8(borderValue);
86
+ const int32x4_t v_zero_s32 = vdupq_n_s32(0);
87
+
88
+ uint8x8_t tprev[3] = { v_zero_u8, v_zero_u8, v_zero_u8 },
89
+ tcurr[3] = { v_zero_u8, v_zero_u8, v_zero_u8 },
90
+ tnext[3] = { v_zero_u8, v_zero_u8, v_zero_u8 };
91
+ uint8x8_t t0 = v_zero_u8, t1 = v_zero_u8, t2 = v_zero_u8;
92
+
93
+ ptrdiff_t width = (ptrdiff_t)size.width, height = (ptrdiff_t)size.height;
94
+ static const vshrq_s32_func vshrq_s32_a[33] =
95
+ {
96
+ vshrq_s32<0>,
97
+ vshrq_s32<1>,
98
+ vshrq_s32<2>,
99
+ vshrq_s32<3>,
100
+ vshrq_s32<4>,
101
+ vshrq_s32<5>,
102
+ vshrq_s32<6>,
103
+ vshrq_s32<7>,
104
+ vshrq_s32<8>,
105
+ vshrq_s32<9>,
106
+ vshrq_s32<10>,
107
+ vshrq_s32<11>,
108
+ vshrq_s32<12>,
109
+ vshrq_s32<13>,
110
+ vshrq_s32<14>,
111
+ vshrq_s32<15>,
112
+ vshrq_s32<16>,
113
+ vshrq_s32<17>,
114
+ vshrq_s32<18>,
115
+ vshrq_s32<19>,
116
+ vshrq_s32<20>,
117
+ vshrq_s32<21>,
118
+ vshrq_s32<22>,
119
+ vshrq_s32<23>,
120
+ vshrq_s32<24>,
121
+ vshrq_s32<25>,
122
+ vshrq_s32<26>,
123
+ vshrq_s32<27>,
124
+ vshrq_s32<28>,
125
+ vshrq_s32<29>,
126
+ vshrq_s32<30>,
127
+ vshrq_s32<31>,
128
+ vshrq_s32<32>
129
+ };
130
+ vshrq_s32_func vshrq_s32_p = vshrq_s32_a[scale];
131
+
132
+ for (ptrdiff_t y = 0; y < height; ++y)
133
+ {
134
+ const u8 * srow0 = y == 0 && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::max<ptrdiff_t>(y - 1, 0));
135
+ const u8 * srow1 = internal::getRowPtr(srcBase, srcStride, y);
136
+ const u8 * srow2 = y + 1 == height && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::min(y + 1, height - 1));
137
+ u8 * drow = internal::getRowPtr(dstBase, dstStride, y);
138
+
139
+ u8 prevx[3] = { 0, 0, 0 },
140
+ currx[3] = { 0, 0, 0 },
141
+ nextx[3] = { 0, 0, 0 };
142
+ ptrdiff_t x = 0;
143
+ const ptrdiff_t bwidth = y + 2 < height ? width : (width - 8);
144
+
145
+ // perform vertical convolution
146
+ for ( ; x <= bwidth; x += 8)
147
+ {
148
+ internal::prefetch(srow0 + x);
149
+ internal::prefetch(srow1 + x);
150
+ internal::prefetch(srow2 + x);
151
+
152
+ uint8x8_t x0 = !srow0 ? v_border : vld1_u8(srow0 + x);
153
+ uint8x8_t x1 = vld1_u8(srow1 + x);
154
+ uint8x8_t x2 = !srow2 ? v_border : vld1_u8(srow2 + x);
155
+
156
+ // calculate values for plain CPU part below if needed
157
+ if (x + 8 >= bwidth)
158
+ {
159
+ ptrdiff_t x3 = x == width ? width - 1 : x;
160
+ ptrdiff_t x4 = border == BORDER_MODE_CONSTANT ? x3 - 1 : std::max<ptrdiff_t>(x3 - 1, 0);
161
+
162
+ if (border == BORDER_MODE_CONSTANT && x4 < 0)
163
+ prevx[0] = prevx[1] = prevx[2] = borderValue;
164
+ else
165
+ {
166
+ prevx[0] = srow0 ? srow0[x4] : borderValue;
167
+ prevx[1] = srow1[x4] ;
168
+ prevx[2] = srow2 ? srow2[x4] : borderValue;
169
+ }
170
+
171
+ currx[0] = srow0 ? srow0[x3] : borderValue;
172
+ currx[1] = srow1[x3] ;
173
+ currx[2] = srow2 ? srow2[x3] : borderValue;
174
+ }
175
+
176
+ // make shift
177
+ if (x)
178
+ {
179
+ tprev[0] = tcurr[0];
180
+ tcurr[0] = tnext[0];
181
+
182
+ tprev[1] = tcurr[1];
183
+ tcurr[1] = tnext[1];
184
+
185
+ tprev[2] = tcurr[2];
186
+ tcurr[2] = tnext[2];
187
+ }
188
+
189
+ tnext[0] = x0;
190
+ tnext[1] = x1;
191
+ tnext[2] = x2;
192
+
193
+ // make extrapolation for the first elements
194
+ if (!x)
195
+ {
196
+ // make border
197
+ if (border == BORDER_MODE_CONSTANT)
198
+ tcurr[0] = tcurr[1] = tcurr[2] = v_border;
199
+ else if (border == BORDER_MODE_REPLICATE)
200
+ {
201
+ tcurr[0] = vdup_n_u8(vget_lane_u8(tnext[0], 0));
202
+ tcurr[1] = vdup_n_u8(vget_lane_u8(tnext[1], 0));
203
+ tcurr[2] = vdup_n_u8(vget_lane_u8(tnext[2], 0));
204
+ }
205
+
206
+ continue;
207
+ }
208
+
209
+ int32x4_t v_dst0 = v_zero_s32, v_dst1 = v_zero_s32;
210
+
211
+ {
212
+ // combine 3 "shifted" vectors
213
+ t0 = vext_u8(tprev[0], tcurr[0], 7);
214
+ t1 = tcurr[0];
215
+ t2 = vext_u8(tcurr[0], tnext[0], 1);
216
+
217
+ int16x8_t t0_16s = vreinterpretq_s16_u16(vmovl_u8(t0));
218
+ int16x8_t t1_16s = vreinterpretq_s16_u16(vmovl_u8(t1));
219
+ int16x8_t t2_16s = vreinterpretq_s16_u16(vmovl_u8(t2));
220
+
221
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t0_16s), kernelBase[8]);
222
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t1_16s), kernelBase[7]);
223
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t2_16s), kernelBase[6]);
224
+
225
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t0_16s), kernelBase[8]);
226
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t1_16s), kernelBase[7]);
227
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t2_16s), kernelBase[6]);
228
+ }
229
+
230
+ {
231
+ // combine 3 "shifted" vectors
232
+ t0 = vext_u8(tprev[1], tcurr[1], 7);
233
+ t1 = tcurr[1];
234
+ t2 = vext_u8(tcurr[1], tnext[1], 1);
235
+
236
+ int16x8_t t0_16s = vreinterpretq_s16_u16(vmovl_u8(t0));
237
+ int16x8_t t1_16s = vreinterpretq_s16_u16(vmovl_u8(t1));
238
+ int16x8_t t2_16s = vreinterpretq_s16_u16(vmovl_u8(t2));
239
+
240
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t0_16s), kernelBase[5]);
241
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t1_16s), kernelBase[4]);
242
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t2_16s), kernelBase[3]);
243
+
244
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t0_16s), kernelBase[5]);
245
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t1_16s), kernelBase[4]);
246
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t2_16s), kernelBase[3]);
247
+ }
248
+
249
+ {
250
+ // combine 3 "shifted" vectors
251
+ t0 = vext_u8(tprev[2], tcurr[2], 7);
252
+ t1 = tcurr[2];
253
+ t2 = vext_u8(tcurr[2], tnext[2], 1);
254
+
255
+ int16x8_t t0_16s = vreinterpretq_s16_u16(vmovl_u8(t0));
256
+ int16x8_t t1_16s = vreinterpretq_s16_u16(vmovl_u8(t1));
257
+ int16x8_t t2_16s = vreinterpretq_s16_u16(vmovl_u8(t2));
258
+
259
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t0_16s), kernelBase[2]);
260
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t1_16s), kernelBase[1]);
261
+ v_dst0 = vmlal_n_s16(v_dst0, vget_low_s16(t2_16s), kernelBase[0]);
262
+
263
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t0_16s), kernelBase[2]);
264
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t1_16s), kernelBase[1]);
265
+ v_dst1 = vmlal_n_s16(v_dst1, vget_high_s16(t2_16s), kernelBase[0]);
266
+ }
267
+
268
+
269
+ // make scale
270
+ v_dst0 = vshrq_s32_p(v_dst0);
271
+ v_dst1 = vshrq_s32_p(v_dst1);
272
+
273
+ // and add them
274
+ vst1_u8(drow + x - 8, vqmovn_u16(vcombine_u16(vqmovun_s32(v_dst0),
275
+ vqmovun_s32(v_dst1))));
276
+ }
277
+
278
+ x -= 8;
279
+ if (x == width)
280
+ --x;
281
+
282
+ for ( ; x < width; ++x)
283
+ {
284
+ // make extrapolation for the last elements
285
+ if (x + 1 >= width)
286
+ {
287
+ if (border == BORDER_MODE_CONSTANT)
288
+ {
289
+ nextx[0] = borderValue;
290
+ nextx[1] = borderValue;
291
+ nextx[2] = borderValue;
292
+ }
293
+ else if (border == BORDER_MODE_REPLICATE)
294
+ {
295
+ nextx[0] = srow0[x];
296
+ nextx[1] = srow1[x];
297
+ nextx[2] = srow2[x];
298
+ }
299
+ }
300
+ else
301
+ {
302
+ nextx[0] = srow0 ? srow0[x + 1] : borderValue;
303
+ nextx[1] = srow1[x + 1] ;
304
+ nextx[2] = srow2 ? srow2[x + 1] : borderValue;
305
+ }
306
+
307
+ s32 val = 0;
308
+ for (s32 _y = 0; _y < 3; ++_y)
309
+ val += prevx[_y] * kernelBase[(2 - _y) * 3 + 2] +
310
+ currx[_y] * kernelBase[(2 - _y) * 3 + 1] +
311
+ nextx[_y] * kernelBase[(2 - _y) * 3 + 0];
312
+
313
+ drow[x] = internal::saturate_cast<u8>(val >> scale);
314
+
315
+ // make shift
316
+ prevx[0] = currx[0];
317
+ currx[0] = nextx[0];
318
+
319
+ prevx[1] = currx[1];
320
+ currx[1] = nextx[1];
321
+
322
+ prevx[2] = currx[2];
323
+ currx[2] = nextx[2];
324
+ }
325
+ }
326
+ #else
327
+ (void)size;
328
+ (void)srcBase;
329
+ (void)srcStride;
330
+ (void)dstBase;
331
+ (void)dstStride;
332
+ (void)border;
333
+ (void)borderValue;
334
+ (void)ksize;
335
+ (void)kernelBase;
336
+ (void)scale;
337
+ #endif
338
+ }
339
+
340
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/count_nonzero.cpp ADDED
@@ -0,0 +1,430 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include <limits>
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ s32 countNonZero(const Size2D &_size,
47
+ const u8 * srcBase, ptrdiff_t srcStride)
48
+ {
49
+ internal::assertSupportedConfiguration();
50
+ #ifdef CAROTENE_NEON
51
+ Size2D size(_size);
52
+ if (srcStride == (ptrdiff_t)(size.width))
53
+ {
54
+ size.width *= size.height;
55
+ size.height = 1;
56
+ }
57
+ size_t roiw16 = size.width & ~15u;
58
+ s32 result = 0;
59
+ for(size_t k = 0; k < size.height; ++k)
60
+ {
61
+ const u8* src = internal::getRowPtr( srcBase, srcStride, k);
62
+ size_t i = 0;
63
+
64
+ #define COUNTNONZERO8U_BLOCK_SIZE (16*255)
65
+ uint8x16_t vc1 = vmovq_n_u8(1);
66
+ for (; i < roiw16;)
67
+ {
68
+ size_t lim = std::min(i + COUNTNONZERO8U_BLOCK_SIZE, size.width) - 16;
69
+ uint8x16_t vs = vmovq_n_u8(0);
70
+
71
+ for (; i <= lim; i+= 16)
72
+ {
73
+ internal::prefetch(src + i);
74
+ uint8x16_t vln = vld1q_u8(src + i);
75
+ uint8x16_t vnz = vminq_u8(vln, vc1);
76
+ vs = vaddq_u8(vs, vnz);
77
+ }
78
+
79
+ uint32x4_t vs4 = vpaddlq_u16(vpaddlq_u8(vs));
80
+ uint32x2_t vs2 = vadd_u32(vget_low_u32(vs4), vget_high_u32(vs4));
81
+
82
+ s32 s[2];
83
+ vst1_u32((u32*)s, vs2);
84
+
85
+ if (s[0] < 0 || s[1] < 0)//saturate in case of overflow ~ 2GB of non-zeros...
86
+ {
87
+ return 0x7fFFffFF;
88
+ }
89
+ result += (s[0] += s[1]);
90
+ if (s[0] < 0 || result < 0)
91
+ {
92
+ return 0x7fFFffFF;
93
+ }
94
+ }
95
+ for (; i < size.width; i++)
96
+ result += (src[i] != 0)?1:0;
97
+ if (result < 0)//saturate in case of overflow ~ 2GB of non-zeros...
98
+ {
99
+ return 0x7fFFffFF;
100
+ }
101
+ }
102
+ return result;
103
+ #else
104
+ (void)_size;
105
+ (void)srcBase;
106
+ (void)srcStride;
107
+
108
+ return 0;
109
+ #endif
110
+ }
111
+
112
+ s32 countNonZero(const Size2D &_size,
113
+ const u16 * srcBase, ptrdiff_t srcStride)
114
+ {
115
+ internal::assertSupportedConfiguration();
116
+ #ifdef CAROTENE_NEON
117
+ Size2D size(_size);
118
+ if (srcStride == (ptrdiff_t)(size.width))
119
+ {
120
+ size.width *= size.height;
121
+ size.height = 1;
122
+ }
123
+ size_t roiw8 = size.width & ~7u;
124
+ s32 result = 0;
125
+ for(size_t k = 0; k < size.height; ++k)
126
+ {
127
+ const u16* src = internal::getRowPtr( srcBase, srcStride, k);
128
+ size_t i = 0;
129
+
130
+ #define COUNTNONZERO16U_BLOCK_SIZE (8*(256*256-1))
131
+ uint16x8_t vc1 = vmovq_n_u16(1);
132
+ for (; i < roiw8;)
133
+ {
134
+ size_t lim = std::min(i + COUNTNONZERO16U_BLOCK_SIZE, size.width) - 8;
135
+ uint16x8_t vs = vmovq_n_u16(0);
136
+
137
+ for (; i <= lim; i+= 8)
138
+ {
139
+ internal::prefetch(src + i);
140
+ uint16x8_t vln = vld1q_u16(src + i);
141
+ uint16x8_t vnz = vminq_u16(vln, vc1);
142
+ vs = vaddq_u16(vs, vnz);
143
+ }
144
+
145
+ uint32x4_t vs4 = vpaddlq_u16(vs);
146
+ uint32x2_t vs2 = vadd_u32(vget_low_u32(vs4), vget_high_u32(vs4));
147
+
148
+ s32 s[2];
149
+ vst1_u32((u32*)s, vs2);
150
+
151
+ if (s[0] < 0 || s[1] < 0)//saturate in case of overflow ~ 4GB of non-zeros...
152
+ {
153
+ return 0x7fFFffFF;
154
+ }
155
+ result += (s[0] += s[1]);
156
+ if (s[0] < 0 || result < 0)
157
+ {
158
+ return 0x7fFFffFF;
159
+ }
160
+ }
161
+ for (; i < size.width; i++)
162
+ result += (src[i] != 0)?1:0;
163
+ if (result < 0)//saturate in case of overflow ~ 4GB of non-zeros...
164
+ {
165
+ return 0x7fFFffFF;
166
+ }
167
+ }
168
+ return result;
169
+ #else
170
+ (void)_size;
171
+ (void)srcBase;
172
+ (void)srcStride;
173
+
174
+ return 0;
175
+ #endif
176
+ }
177
+
178
+ s32 countNonZero(const Size2D &_size,
179
+ const s32 * srcBase, ptrdiff_t srcStride)
180
+ {
181
+ internal::assertSupportedConfiguration();
182
+ #ifdef CAROTENE_NEON
183
+ Size2D size(_size);
184
+ if (srcStride == (ptrdiff_t)(size.width))
185
+ {
186
+ size.width *= size.height;
187
+ size.height = 1;
188
+ }
189
+ size_t roiw4 = size.width & ~3u;
190
+ s32 result = 0;
191
+ for(size_t k = 0; k < size.height; ++k)
192
+ {
193
+ const u32* src = (const u32*)internal::getRowPtr( srcBase, srcStride, k);
194
+ u32 i = 0;
195
+
196
+ uint32x4_t vc1 = vmovq_n_u32(1);
197
+ uint32x4_t vs = vmovq_n_u32(0);
198
+
199
+ for (; i < roiw4; i += 4 )
200
+ {
201
+ internal::prefetch(src + i);
202
+ uint32x4_t vln = vld1q_u32(src + i);
203
+ uint32x4_t vnz = vminq_u32(vln, vc1);
204
+ vs = vqaddq_u32(vs, vnz);
205
+ }
206
+
207
+ uint32x2_t vs2 = vqadd_u32(vget_low_u32(vs), vget_high_u32(vs));
208
+
209
+ s32 s[2];
210
+ vst1_u32((u32*)s, vs2);
211
+
212
+ if (s[0] < 0 || s[1] < 0)//saturate in case of overflow ~ 8GB of non-zeros...
213
+ {
214
+ return 0x7fFFffFF;
215
+ }
216
+ result += (s[0] += s[1]);
217
+ if (s[0] < 0 || result < 0)
218
+ {
219
+ return 0x7fFFffFF;
220
+ }
221
+
222
+ for (; i < size.width; i++)
223
+ result += (src[i] != 0)?1:0;
224
+ if (result < 0)//saturate in case of overflow ~ 8GB of non-zeros...
225
+ {
226
+ return 0x7fFFffFF;
227
+ }
228
+ }
229
+ return result;
230
+ #else
231
+ (void)_size;
232
+ (void)srcBase;
233
+ (void)srcStride;
234
+
235
+ return 0;
236
+ #endif
237
+ }
238
+
239
+ s32 countNonZero(const Size2D &_size,
240
+ const f32 * srcBase, ptrdiff_t srcStride)
241
+ {
242
+ internal::assertSupportedConfiguration();
243
+ #ifdef CAROTENE_NEON
244
+ Size2D size(_size);
245
+ if (srcStride == (ptrdiff_t)(size.width))
246
+ {
247
+ size.width *= size.height;
248
+ size.height = 1;
249
+ }
250
+ size_t roiw4 = size.width & ~3u;
251
+ s32 result = 0;
252
+ for(size_t k = 0; k < size.height; ++k)
253
+ {
254
+ const f32* src = internal::getRowPtr( srcBase, srcStride, k);
255
+ size_t i = 0;
256
+
257
+ float32x4_t vc0 = vmovq_n_f32(0);
258
+ int32x4_t vs = vmovq_n_s32(0);
259
+
260
+ for (; i < roiw4; i += 4 )
261
+ {
262
+ internal::prefetch(src + i);
263
+ float32x4_t vln = vld1q_f32(src + i);
264
+ int32x4_t vnz = vreinterpretq_s32_u32(vmvnq_u32(vceqq_f32(vln, vc0)));
265
+ vs = vqaddq_s32(vs, vnz);
266
+ }
267
+
268
+ int32x2_t vs2 = vqneg_s32(vqadd_s32(vget_low_s32(vs), vget_high_s32(vs)));
269
+
270
+ int s[2];
271
+ vst1_s32(s, vs2);
272
+
273
+ result += (s[0] += s[1]);
274
+ if (s[0] < 0 || result < 0)//case of overflow ~ 8GB of non-zeros...
275
+ {
276
+ return 0x7fFFffFF;
277
+ }
278
+
279
+ for (; i < size.width; i++)
280
+ result += (src[i] < std::numeric_limits<float>::min() && src[i] > -std::numeric_limits<float>::min())?0:1;
281
+
282
+ if (result < 0)
283
+ {
284
+ return 0x7fFFffFF;
285
+ }
286
+ }
287
+ return result;
288
+ #else
289
+ (void)_size;
290
+ (void)srcBase;
291
+ (void)srcStride;
292
+
293
+ return 0;
294
+ #endif
295
+ }
296
+
297
+ s32 countNonZero(const Size2D &_size,
298
+ const f64 * srcBase, ptrdiff_t srcStride)
299
+ {
300
+ internal::assertSupportedConfiguration();
301
+ #ifdef CAROTENE_NEON
302
+ Size2D size(_size);
303
+ if (srcStride == (ptrdiff_t)(size.width))
304
+ {
305
+ size.width *= size.height;
306
+ size.height = 1;
307
+ }
308
+ size_t roiw8 = size.width & ~7u;
309
+ size_t roiw4 = size.width & ~3u;
310
+ size_t roiw2 = size.width & ~1u;
311
+ uint64x2_t vmask1 = vdupq_n_u64(0x7fFFffFFffFFffFFULL); //will treat denormals as non-zero
312
+ uint32x4_t vc0 = vmovq_n_u32(0);
313
+
314
+ s32 result = 0;
315
+ for(size_t k = 0; k < size.height; ++k)
316
+ {
317
+ const f64* src = internal::getRowPtr( srcBase, srcStride, k);
318
+ size_t i = 0;
319
+
320
+ int32x2_t vs1 = vmov_n_s32(0);
321
+ int32x2_t vs2 = vmov_n_s32(0);
322
+ int32x2_t vs3 = vmov_n_s32(0);
323
+ int32x2_t vs4 = vmov_n_s32(0);
324
+
325
+ for (; i < roiw8; i += 8 )
326
+ {
327
+ internal::prefetch(src + i + 6);
328
+ uint64x2_t vln1 = vld1q_u64((const u64*)(src + i));
329
+ uint64x2_t vln2 = vld1q_u64((const u64*)(src + i + 2));
330
+ uint64x2_t vln3 = vld1q_u64((const u64*)(src + i + 4));
331
+ uint64x2_t vln4 = vld1q_u64((const u64*)(src + i + 6));
332
+
333
+ uint64x2_t vm1 = vandq_u64(vln1, vmask1);
334
+ uint64x2_t vm2 = vandq_u64(vln2, vmask1);
335
+ uint64x2_t vm3 = vandq_u64(vln3, vmask1);
336
+ uint64x2_t vm4 = vandq_u64(vln4, vmask1);
337
+
338
+ uint32x4_t vequ1 = vceqq_u32(vreinterpretq_u32_u64(vm1), vc0);
339
+ uint32x4_t vequ2 = vceqq_u32(vreinterpretq_u32_u64(vm2), vc0);
340
+ uint32x4_t vequ3 = vceqq_u32(vreinterpretq_u32_u64(vm3), vc0);
341
+ uint32x4_t vequ4 = vceqq_u32(vreinterpretq_u32_u64(vm4), vc0);
342
+
343
+ uint32x4_t vlx1 = vmvnq_u32(vequ1);
344
+ uint32x4_t vlx2 = vmvnq_u32(vequ2);
345
+ uint32x4_t vlx3 = vmvnq_u32(vequ3);
346
+ uint32x4_t vlx4 = vmvnq_u32(vequ4);
347
+
348
+ int32x2_t vnz1 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx1), vget_high_u32(vlx1)));
349
+ int32x2_t vnz2 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx2), vget_high_u32(vlx2)));
350
+ int32x2_t vnz3 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx3), vget_high_u32(vlx3)));
351
+ int32x2_t vnz4 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx4), vget_high_u32(vlx4)));
352
+
353
+ vs1 = vqadd_s32(vs1, vnz1);
354
+ vs2 = vqadd_s32(vs2, vnz2);
355
+ vs3 = vqadd_s32(vs3, vnz3);
356
+ vs4 = vqadd_s32(vs4, vnz4);
357
+ }
358
+
359
+ if (i < roiw4)
360
+ {
361
+ internal::prefetch(src + i + 2);
362
+ uint64x2_t vln1 = vld1q_u64((const u64*)(src + i));
363
+ uint64x2_t vln2 = vld1q_u64((const u64*)(src + i + 2));
364
+
365
+ uint64x2_t vm1 = vandq_u64(vln1, vmask1);
366
+ uint64x2_t vm2 = vandq_u64(vln2, vmask1);
367
+
368
+ uint32x4_t vequ1 = vceqq_u32(vreinterpretq_u32_u64(vm1), vc0);
369
+ uint32x4_t vequ2 = vceqq_u32(vreinterpretq_u32_u64(vm2), vc0);
370
+
371
+ uint32x4_t vlx1 = vmvnq_u32(vequ1);
372
+ uint32x4_t vlx2 = vmvnq_u32(vequ2);
373
+
374
+ int32x2_t vnz1 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx1), vget_high_u32(vlx1)));
375
+ int32x2_t vnz2 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx2), vget_high_u32(vlx2)));
376
+
377
+ vs1 = vqadd_s32(vs1, vnz1);
378
+ vs2 = vqadd_s32(vs2, vnz2);
379
+ i += 4;
380
+ }
381
+
382
+ if (i < roiw2)
383
+ {
384
+ internal::prefetch(src + i);
385
+ uint64x2_t vln1 = vld1q_u64((const u64*)(src + i));
386
+
387
+ uint64x2_t vm1 = vandq_u64(vln1, vmask1);
388
+
389
+ uint32x4_t vequ1 = vceqq_u32(vreinterpretq_u32_u64(vm1), vc0);
390
+
391
+ uint32x4_t vlx1 = vmvnq_u32(vequ1);
392
+
393
+ int32x2_t vnz1 = vreinterpret_s32_u32(vpmax_u32(vget_low_u32(vlx1), vget_high_u32(vlx1)));
394
+
395
+ vs1 = vqadd_s32(vs1, vnz1);
396
+ i += 2;
397
+ }
398
+
399
+ vs1 = vqadd_s32(vs1, vs2);
400
+ vs3 = vqadd_s32(vs3, vs4);
401
+ vs1 = vqadd_s32(vs1, vs3);
402
+ int32x2_t vsneg = vqneg_s32(vs1);
403
+
404
+ s32 s[2];
405
+ vst1_s32(s, vsneg);
406
+
407
+ result += (s[0] += s[1]);
408
+ if (s[0] < 0 || result < 0)//case of overflow ~ 16GB of non-zeros...
409
+ {
410
+ return 0x7fFFffFF;
411
+ }
412
+
413
+ for (; i < size.width; i++)
414
+ result += (src[i] < std::numeric_limits<double>::min() && src[i] > -std::numeric_limits<double>::min())?0:1;
415
+ if (result < 0)
416
+ {
417
+ return 0x7fFFffFF;
418
+ }
419
+ }
420
+ return result;
421
+ #else
422
+ (void)_size;
423
+ (void)srcBase;
424
+ (void)srcStride;
425
+
426
+ return 0;
427
+ #endif
428
+ }
429
+
430
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/div.cpp ADDED
@@ -0,0 +1,681 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2016, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+ #include "vround_helper.hpp"
43
+
44
+ #include <cstring>
45
+ #include <cfloat>
46
+ #include <cmath>
47
+ #include <limits>
48
+
49
+ namespace CAROTENE_NS {
50
+
51
+ namespace {
52
+
53
+ #ifdef CAROTENE_NEON
54
+
55
+ template <typename T>
56
+ inline T divSaturateQ(const T &v1, const T &v2, const float scale)
57
+ {
58
+ return internal::vcombine(internal::vqmovn(divSaturateQ(internal::vmovl(internal::vget_low(v1)),
59
+ internal::vmovl(internal::vget_low(v2)), scale)),
60
+ internal::vqmovn(divSaturateQ(internal::vmovl(internal::vget_high(v1)),
61
+ internal::vmovl(internal::vget_high(v2)), scale))
62
+ );
63
+ }
64
+ template <>
65
+ inline int32x4_t divSaturateQ<int32x4_t>(const int32x4_t &v1, const int32x4_t &v2, const float scale)
66
+ { return internal::vroundq_s32_f32(vmulq_f32(vmulq_n_f32(vcvtq_f32_s32(v1), scale), internal::vrecpq_f32(vcvtq_f32_s32(v2)))); }
67
+ template <>
68
+ inline uint32x4_t divSaturateQ<uint32x4_t>(const uint32x4_t &v1, const uint32x4_t &v2, const float scale)
69
+ { return internal::vroundq_u32_f32(vmulq_f32(vmulq_n_f32(vcvtq_f32_u32(v1), scale), internal::vrecpq_f32(vcvtq_f32_u32(v2)))); }
70
+
71
+ template <typename T>
72
+ inline T divSaturate(const T &v1, const T &v2, const float scale)
73
+ {
74
+ return internal::vqmovn(divSaturateQ(internal::vmovl(v1), internal::vmovl(v2), scale));
75
+ }
76
+ template <>
77
+ inline int32x2_t divSaturate<int32x2_t>(const int32x2_t &v1, const int32x2_t &v2, const float scale)
78
+ { return internal::vround_s32_f32(vmul_f32(vmul_n_f32(vcvt_f32_s32(v1), scale), internal::vrecp_f32(vcvt_f32_s32(v2)))); }
79
+ template <>
80
+ inline uint32x2_t divSaturate<uint32x2_t>(const uint32x2_t &v1, const uint32x2_t &v2, const float scale)
81
+ { return internal::vround_u32_f32(vmul_f32(vmul_n_f32(vcvt_f32_u32(v1), scale), internal::vrecp_f32(vcvt_f32_u32(v2)))); }
82
+
83
+
84
+ template <typename T>
85
+ inline T divWrapQ(const T &v1, const T &v2, const float scale)
86
+ {
87
+ return internal::vcombine(internal::vmovn(divWrapQ(internal::vmovl(internal::vget_low(v1)),
88
+ internal::vmovl(internal::vget_low(v2)), scale)),
89
+ internal::vmovn(divWrapQ(internal::vmovl(internal::vget_high(v1)),
90
+ internal::vmovl(internal::vget_high(v2)), scale))
91
+ );
92
+ }
93
+ template <>
94
+ inline int32x4_t divWrapQ<int32x4_t>(const int32x4_t &v1, const int32x4_t &v2, const float scale)
95
+ { return vcvtq_s32_f32(vmulq_f32(vmulq_n_f32(vcvtq_f32_s32(v1), scale), internal::vrecpq_f32(vcvtq_f32_s32(v2)))); }
96
+ template <>
97
+ inline uint32x4_t divWrapQ<uint32x4_t>(const uint32x4_t &v1, const uint32x4_t &v2, const float scale)
98
+ { return vcvtq_u32_f32(vmulq_f32(vmulq_n_f32(vcvtq_f32_u32(v1), scale), internal::vrecpq_f32(vcvtq_f32_u32(v2)))); }
99
+
100
+ template <typename T>
101
+ inline T divWrap(const T &v1, const T &v2, const float scale)
102
+ {
103
+ return internal::vmovn(divWrapQ(internal::vmovl(v1), internal::vmovl(v2), scale));
104
+ }
105
+ template <>
106
+ inline int32x2_t divWrap<int32x2_t>(const int32x2_t &v1, const int32x2_t &v2, const float scale)
107
+ { return vcvt_s32_f32(vmul_f32(vmul_n_f32(vcvt_f32_s32(v1), scale), internal::vrecp_f32(vcvt_f32_s32(v2)))); }
108
+ template <>
109
+ inline uint32x2_t divWrap<uint32x2_t>(const uint32x2_t &v1, const uint32x2_t &v2, const float scale)
110
+ { return vcvt_u32_f32(vmul_f32(vmul_n_f32(vcvt_f32_u32(v1), scale), internal::vrecp_f32(vcvt_f32_u32(v2)))); }
111
+
112
+ inline uint8x16_t vtstq(const uint8x16_t & v0, const uint8x16_t & v1) { return vtstq_u8 (v0, v1); }
113
+ inline uint16x8_t vtstq(const uint16x8_t & v0, const uint16x8_t & v1) { return vtstq_u16(v0, v1); }
114
+ inline uint32x4_t vtstq(const uint32x4_t & v0, const uint32x4_t & v1) { return vtstq_u32(v0, v1); }
115
+ inline int8x16_t vtstq(const int8x16_t & v0, const int8x16_t & v1) { return vreinterpretq_s8_u8 (vtstq_s8 (v0, v1)); }
116
+ inline int16x8_t vtstq(const int16x8_t & v0, const int16x8_t & v1) { return vreinterpretq_s16_u16(vtstq_s16(v0, v1)); }
117
+ inline int32x4_t vtstq(const int32x4_t & v0, const int32x4_t & v1) { return vreinterpretq_s32_u32(vtstq_s32(v0, v1)); }
118
+
119
+ inline uint8x8_t vtst(const uint8x8_t & v0, const uint8x8_t & v1) { return vtst_u8 (v0, v1); }
120
+ inline uint16x4_t vtst(const uint16x4_t & v0, const uint16x4_t & v1) { return vtst_u16(v0, v1); }
121
+ inline uint32x2_t vtst(const uint32x2_t & v0, const uint32x2_t & v1) { return vtst_u32(v0, v1); }
122
+ inline int8x8_t vtst(const int8x8_t & v0, const int8x8_t & v1) { return vreinterpret_s8_u8 (vtst_s8 (v0, v1)); }
123
+ inline int16x4_t vtst(const int16x4_t & v0, const int16x4_t & v1) { return vreinterpret_s16_u16(vtst_s16(v0, v1)); }
124
+ inline int32x2_t vtst(const int32x2_t & v0, const int32x2_t & v1) { return vreinterpret_s32_u32(vtst_s32(v0, v1)); }
125
+ #endif
126
+
127
+ template <typename T>
128
+ void div(const Size2D &size,
129
+ const T * src0Base, ptrdiff_t src0Stride,
130
+ const T * src1Base, ptrdiff_t src1Stride,
131
+ T * dstBase, ptrdiff_t dstStride,
132
+ f32 scale,
133
+ CONVERT_POLICY cpolicy)
134
+ {
135
+ internal::assertSupportedConfiguration();
136
+
137
+ #ifdef CAROTENE_NEON
138
+ typedef typename internal::VecTraits<T>::vec128 vec128;
139
+ typedef typename internal::VecTraits<T>::vec64 vec64;
140
+
141
+ #if defined(__GNUC__) && (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L)
142
+ static_assert(std::numeric_limits<T>::is_integer, "template implementation is for integer types only");
143
+ #endif
144
+
145
+ if (scale == 0.0f ||
146
+ (std::numeric_limits<T>::is_integer &&
147
+ (scale * static_cast<float>(std::numeric_limits<T>::max())) < 1.0f &&
148
+ (scale * static_cast<float>(std::numeric_limits<T>::max())) > -1.0f))
149
+ {
150
+ for (size_t y = 0; y < size.height; ++y)
151
+ {
152
+ T * dst = internal::getRowPtr(dstBase, dstStride, y);
153
+ std::memset(dst, 0, sizeof(T) * size.width);
154
+ }
155
+ return;
156
+ }
157
+
158
+ const size_t step128 = 16 / sizeof(T);
159
+ size_t roiw128 = size.width >= (step128 - 1) ? size.width - step128 + 1 : 0;
160
+ const size_t step64 = 8 / sizeof(T);
161
+ size_t roiw64 = size.width >= (step64 - 1) ? size.width - step64 + 1 : 0;
162
+
163
+ for (size_t i = 0; i < size.height; ++i)
164
+ {
165
+ const T * src0 = internal::getRowPtr(src0Base, src0Stride, i);
166
+ const T * src1 = internal::getRowPtr(src1Base, src1Stride, i);
167
+ T * dst = internal::getRowPtr(dstBase, dstStride, i);
168
+ size_t j = 0;
169
+
170
+ if (cpolicy == CONVERT_POLICY_SATURATE)
171
+ {
172
+ for (; j < roiw128; j += step128)
173
+ {
174
+ internal::prefetch(src0 + j);
175
+ internal::prefetch(src1 + j);
176
+
177
+ vec128 v_src0 = internal::vld1q(src0 + j);
178
+ vec128 v_src1 = internal::vld1q(src1 + j);
179
+
180
+ vec128 v_mask = vtstq(v_src1,v_src1);
181
+ internal::vst1q(dst + j, internal::vandq(v_mask, divSaturateQ(v_src0, v_src1, scale)));
182
+ }
183
+ for (; j < roiw64; j += step64)
184
+ {
185
+ vec64 v_src0 = internal::vld1(src0 + j);
186
+ vec64 v_src1 = internal::vld1(src1 + j);
187
+
188
+ vec64 v_mask = vtst(v_src1,v_src1);
189
+ internal::vst1(dst + j, internal::vand(v_mask,divSaturate(v_src0, v_src1, scale)));
190
+ }
191
+ for (; j < size.width; j++)
192
+ {
193
+ dst[j] = src1[j] ? internal::saturate_cast<T>(scale * src0[j] / src1[j]) : 0;
194
+ }
195
+ }
196
+ else // CONVERT_POLICY_WRAP
197
+ {
198
+ for (; j < roiw128; j += step128)
199
+ {
200
+ internal::prefetch(src0 + j);
201
+ internal::prefetch(src1 + j);
202
+
203
+ vec128 v_src0 = internal::vld1q(src0 + j);
204
+ vec128 v_src1 = internal::vld1q(src1 + j);
205
+
206
+ vec128 v_mask = vtstq(v_src1,v_src1);
207
+ internal::vst1q(dst + j, internal::vandq(v_mask, divWrapQ(v_src0, v_src1, scale)));
208
+ }
209
+ for (; j < roiw64; j += step64)
210
+ {
211
+ vec64 v_src0 = internal::vld1(src0 + j);
212
+ vec64 v_src1 = internal::vld1(src1 + j);
213
+
214
+ vec64 v_mask = vtst(v_src1,v_src1);
215
+ internal::vst1(dst + j, internal::vand(v_mask,divWrap(v_src0, v_src1, scale)));
216
+ }
217
+ for (; j < size.width; j++)
218
+ {
219
+ dst[j] = src1[j] ? (T)((s32)trunc(scale * src0[j] / src1[j])) : 0;
220
+ }
221
+ }
222
+ }
223
+ #else
224
+ (void)size;
225
+ (void)src0Base;
226
+ (void)src0Stride;
227
+ (void)src1Base;
228
+ (void)src1Stride;
229
+ (void)dstBase;
230
+ (void)dstStride;
231
+ (void)cpolicy;
232
+ (void)scale;
233
+ #endif
234
+ }
235
+
236
+ #ifdef CAROTENE_NEON
237
+
238
+ template <typename T>
239
+ inline T recipSaturateQ(const T &v2, const float scale)
240
+ {
241
+ return internal::vcombine(internal::vqmovn(recipSaturateQ(internal::vmovl(internal::vget_low(v2)), scale)),
242
+ internal::vqmovn(recipSaturateQ(internal::vmovl(internal::vget_high(v2)), scale))
243
+ );
244
+ }
245
+ template <>
246
+ inline int32x4_t recipSaturateQ<int32x4_t>(const int32x4_t &v2, const float scale)
247
+ { return vcvtq_s32_f32(vmulq_n_f32(internal::vrecpq_f32(vcvtq_f32_s32(v2)), scale)); }
248
+ template <>
249
+ inline uint32x4_t recipSaturateQ<uint32x4_t>(const uint32x4_t &v2, const float scale)
250
+ { return vcvtq_u32_f32(vmulq_n_f32(internal::vrecpq_f32(vcvtq_f32_u32(v2)), scale)); }
251
+
252
+ template <typename T>
253
+ inline T recipSaturate(const T &v2, const float scale)
254
+ {
255
+ return internal::vqmovn(recipSaturateQ(internal::vmovl(v2), scale));
256
+ }
257
+ template <>
258
+ inline int32x2_t recipSaturate<int32x2_t>(const int32x2_t &v2, const float scale)
259
+ { return vcvt_s32_f32(vmul_n_f32(internal::vrecp_f32(vcvt_f32_s32(v2)), scale)); }
260
+ template <>
261
+ inline uint32x2_t recipSaturate<uint32x2_t>(const uint32x2_t &v2, const float scale)
262
+ { return vcvt_u32_f32(vmul_n_f32(internal::vrecp_f32(vcvt_f32_u32(v2)), scale)); }
263
+
264
+
265
+ template <typename T>
266
+ inline T recipWrapQ(const T &v2, const float scale)
267
+ {
268
+ return internal::vcombine(internal::vmovn(recipWrapQ(internal::vmovl(internal::vget_low(v2)), scale)),
269
+ internal::vmovn(recipWrapQ(internal::vmovl(internal::vget_high(v2)), scale))
270
+ );
271
+ }
272
+ template <>
273
+ inline int32x4_t recipWrapQ<int32x4_t>(const int32x4_t &v2, const float scale)
274
+ { return vcvtq_s32_f32(vmulq_n_f32(internal::vrecpq_f32(vcvtq_f32_s32(v2)), scale)); }
275
+ template <>
276
+ inline uint32x4_t recipWrapQ<uint32x4_t>(const uint32x4_t &v2, const float scale)
277
+ { return vcvtq_u32_f32(vmulq_n_f32(internal::vrecpq_f32(vcvtq_f32_u32(v2)), scale)); }
278
+
279
+ template <typename T>
280
+ inline T recipWrap(const T &v2, const float scale)
281
+ {
282
+ return internal::vmovn(recipWrapQ(internal::vmovl(v2), scale));
283
+ }
284
+ template <>
285
+ inline int32x2_t recipWrap<int32x2_t>(const int32x2_t &v2, const float scale)
286
+ { return vcvt_s32_f32(vmul_n_f32(internal::vrecp_f32(vcvt_f32_s32(v2)), scale)); }
287
+ template <>
288
+ inline uint32x2_t recipWrap<uint32x2_t>(const uint32x2_t &v2, const float scale)
289
+ { return vcvt_u32_f32(vmul_n_f32(internal::vrecp_f32(vcvt_f32_u32(v2)), scale)); }
290
+ #endif
291
+
292
+ template <typename T>
293
+ void recip(const Size2D &size,
294
+ const T * src1Base, ptrdiff_t src1Stride,
295
+ T * dstBase, ptrdiff_t dstStride,
296
+ f32 scale,
297
+ CONVERT_POLICY cpolicy)
298
+ {
299
+ internal::assertSupportedConfiguration();
300
+
301
+ #ifdef CAROTENE_NEON
302
+ typedef typename internal::VecTraits<T>::vec128 vec128;
303
+ typedef typename internal::VecTraits<T>::vec64 vec64;
304
+
305
+ #if defined(__GNUC__) && (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L)
306
+ static_assert(std::numeric_limits<T>::is_integer, "template implementation is for integer types only");
307
+ #endif
308
+
309
+ if (scale == 0.0f ||
310
+ (std::numeric_limits<T>::is_integer &&
311
+ scale < 1.0f &&
312
+ scale > -1.0f))
313
+ {
314
+ for (size_t y = 0; y < size.height; ++y)
315
+ {
316
+ T * dst = internal::getRowPtr(dstBase, dstStride, y);
317
+ std::memset(dst, 0, sizeof(T) * size.width);
318
+ }
319
+ return;
320
+ }
321
+
322
+ const size_t step128 = 16 / sizeof(T);
323
+ size_t roiw128 = size.width >= (step128 - 1) ? size.width - step128 + 1 : 0;
324
+ const size_t step64 = 8 / sizeof(T);
325
+ size_t roiw64 = size.width >= (step64 - 1) ? size.width - step64 + 1 : 0;
326
+
327
+ for (size_t i = 0; i < size.height; ++i)
328
+ {
329
+ const T * src1 = internal::getRowPtr(src1Base, src1Stride, i);
330
+ T * dst = internal::getRowPtr(dstBase, dstStride, i);
331
+ size_t j = 0;
332
+
333
+ if (cpolicy == CONVERT_POLICY_SATURATE)
334
+ {
335
+ for (; j < roiw128; j += step128)
336
+ {
337
+ internal::prefetch(src1 + j);
338
+
339
+ vec128 v_src1 = internal::vld1q(src1 + j);
340
+
341
+ vec128 v_mask = vtstq(v_src1,v_src1);
342
+ internal::vst1q(dst + j, internal::vandq(v_mask, recipSaturateQ(v_src1, scale)));
343
+ }
344
+ for (; j < roiw64; j += step64)
345
+ {
346
+ vec64 v_src1 = internal::vld1(src1 + j);
347
+
348
+ vec64 v_mask = vtst(v_src1,v_src1);
349
+ internal::vst1(dst + j, internal::vand(v_mask, recipSaturate(v_src1, scale)));
350
+ }
351
+ for (; j < size.width; j++)
352
+ {
353
+ dst[j] = src1[j] ? internal::saturate_cast<T>(scale / src1[j]) : 0;
354
+ }
355
+ }
356
+ else // CONVERT_POLICY_WRAP
357
+ {
358
+ for (; j < roiw128; j += step128)
359
+ {
360
+ internal::prefetch(src1 + j);
361
+
362
+ vec128 v_src1 = internal::vld1q(src1 + j);
363
+
364
+ vec128 v_mask = vtstq(v_src1,v_src1);
365
+ internal::vst1q(dst + j, internal::vandq(v_mask, recipWrapQ(v_src1, scale)));
366
+ }
367
+ for (; j < roiw64; j += step64)
368
+ {
369
+ vec64 v_src1 = internal::vld1(src1 + j);
370
+
371
+ vec64 v_mask = vtst(v_src1,v_src1);
372
+ internal::vst1(dst + j, internal::vand(v_mask, recipWrap(v_src1, scale)));
373
+ }
374
+ for (; j < size.width; j++)
375
+ {
376
+ dst[j] = src1[j] ? (T)((s32)trunc(scale / src1[j])) : 0;
377
+ }
378
+ }
379
+ }
380
+ #else
381
+ (void)size;
382
+ (void)src1Base;
383
+ (void)src1Stride;
384
+ (void)dstBase;
385
+ (void)dstStride;
386
+ (void)cpolicy;
387
+ (void)scale;
388
+ #endif
389
+ }
390
+
391
+ }
392
+
393
+ void div(const Size2D &size,
394
+ const u8 * src0Base, ptrdiff_t src0Stride,
395
+ const u8 * src1Base, ptrdiff_t src1Stride,
396
+ u8 * dstBase, ptrdiff_t dstStride,
397
+ f32 scale,
398
+ CONVERT_POLICY cpolicy)
399
+ {
400
+ div<u8>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
401
+ }
402
+
403
+ void div(const Size2D &size,
404
+ const s8 * src0Base, ptrdiff_t src0Stride,
405
+ const s8 * src1Base, ptrdiff_t src1Stride,
406
+ s8 * dstBase, ptrdiff_t dstStride,
407
+ f32 scale,
408
+ CONVERT_POLICY cpolicy)
409
+ {
410
+ div<s8>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
411
+ }
412
+
413
+ void div(const Size2D &size,
414
+ const u16 * src0Base, ptrdiff_t src0Stride,
415
+ const u16 * src1Base, ptrdiff_t src1Stride,
416
+ u16 * dstBase, ptrdiff_t dstStride,
417
+ f32 scale,
418
+ CONVERT_POLICY cpolicy)
419
+ {
420
+ div<u16>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
421
+ }
422
+
423
+ void div(const Size2D &size,
424
+ const s16 * src0Base, ptrdiff_t src0Stride,
425
+ const s16 * src1Base, ptrdiff_t src1Stride,
426
+ s16 * dstBase, ptrdiff_t dstStride,
427
+ f32 scale,
428
+ CONVERT_POLICY cpolicy)
429
+ {
430
+ div<s16>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
431
+ }
432
+
433
+ void div(const Size2D &size,
434
+ const s32 * src0Base, ptrdiff_t src0Stride,
435
+ const s32 * src1Base, ptrdiff_t src1Stride,
436
+ s32 * dstBase, ptrdiff_t dstStride,
437
+ f32 scale,
438
+ CONVERT_POLICY cpolicy)
439
+ {
440
+ div<s32>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
441
+ }
442
+
443
+ void div(const Size2D &size,
444
+ const f32 * src0Base, ptrdiff_t src0Stride,
445
+ const f32 * src1Base, ptrdiff_t src1Stride,
446
+ f32 * dstBase, ptrdiff_t dstStride,
447
+ f32 scale)
448
+ {
449
+ internal::assertSupportedConfiguration();
450
+ #ifdef CAROTENE_NEON
451
+ if (scale == 0.0f)
452
+ {
453
+ for (size_t y = 0; y < size.height; ++y)
454
+ {
455
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, y);
456
+ std::memset(dst, 0, sizeof(f32) * size.width);
457
+ }
458
+ return;
459
+ }
460
+
461
+ size_t roiw128 = size.width >= 3 ? size.width - 3 : 0;
462
+ size_t roiw64 = size.width >= 1 ? size.width - 1 : 0;
463
+
464
+ if (std::fabs(scale - 1.0f) < FLT_EPSILON)
465
+ {
466
+ for (size_t i = 0; i < size.height; ++i)
467
+ {
468
+ const f32 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
469
+ const f32 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
470
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
471
+ size_t j = 0;
472
+
473
+ for (; j < roiw128; j += 4)
474
+ {
475
+ internal::prefetch(src0 + j);
476
+ internal::prefetch(src1 + j);
477
+
478
+ float32x4_t v_src0 = vld1q_f32(src0 + j);
479
+ float32x4_t v_src1 = vld1q_f32(src1 + j);
480
+
481
+ vst1q_f32(dst + j, vmulq_f32(v_src0, internal::vrecpq_f32(v_src1)));
482
+ }
483
+
484
+ for (; j < roiw64; j += 2)
485
+ {
486
+ float32x2_t v_src0 = vld1_f32(src0 + j);
487
+ float32x2_t v_src1 = vld1_f32(src1 + j);
488
+
489
+ vst1_f32(dst + j, vmul_f32(v_src0, internal::vrecp_f32(v_src1)));
490
+ }
491
+
492
+ for (; j < size.width; j++)
493
+ {
494
+ dst[j] = src0[j] / src1[j];
495
+ }
496
+ }
497
+ }
498
+ else
499
+ {
500
+ for (size_t i = 0; i < size.height; ++i)
501
+ {
502
+ const f32 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
503
+ const f32 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
504
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
505
+ size_t j = 0;
506
+
507
+ for (; j < roiw128; j += 4)
508
+ {
509
+ internal::prefetch(src0 + j);
510
+ internal::prefetch(src1 + j);
511
+
512
+ float32x4_t v_src0 = vld1q_f32(src0 + j);
513
+ float32x4_t v_src1 = vld1q_f32(src1 + j);
514
+
515
+ vst1q_f32(dst + j, vmulq_f32(vmulq_n_f32(v_src0, scale),
516
+ internal::vrecpq_f32(v_src1)));
517
+ }
518
+
519
+ for (; j < roiw64; j += 2)
520
+ {
521
+ float32x2_t v_src0 = vld1_f32(src0 + j);
522
+ float32x2_t v_src1 = vld1_f32(src1 + j);
523
+
524
+ vst1_f32(dst + j, vmul_f32(vmul_n_f32(v_src0, scale),
525
+ internal::vrecp_f32(v_src1)));
526
+ }
527
+
528
+ for (; j < size.width; j++)
529
+ {
530
+ dst[j] = src0[j] * scale / src1[j];
531
+ }
532
+ }
533
+ }
534
+ #else
535
+ (void)size;
536
+ (void)src0Base;
537
+ (void)src0Stride;
538
+ (void)src1Base;
539
+ (void)src1Stride;
540
+ (void)dstBase;
541
+ (void)dstStride;
542
+ (void)scale;
543
+ #endif
544
+ }
545
+
546
+ void reciprocal(const Size2D &size,
547
+ const u8 * srcBase, ptrdiff_t srcStride,
548
+ u8 * dstBase, ptrdiff_t dstStride,
549
+ f32 scale,
550
+ CONVERT_POLICY cpolicy)
551
+ {
552
+ recip<u8>(size, srcBase, srcStride, dstBase, dstStride, scale, cpolicy);
553
+ }
554
+
555
+ void reciprocal(const Size2D &size,
556
+ const s8 * srcBase, ptrdiff_t srcStride,
557
+ s8 * dstBase, ptrdiff_t dstStride,
558
+ f32 scale,
559
+ CONVERT_POLICY cpolicy)
560
+ {
561
+ recip<s8>(size, srcBase, srcStride, dstBase, dstStride, scale, cpolicy);
562
+ }
563
+
564
+ void reciprocal(const Size2D &size,
565
+ const u16 * srcBase, ptrdiff_t srcStride,
566
+ u16 * dstBase, ptrdiff_t dstStride,
567
+ f32 scale,
568
+ CONVERT_POLICY cpolicy)
569
+ {
570
+ recip<u16>(size, srcBase, srcStride, dstBase, dstStride, scale, cpolicy);
571
+ }
572
+
573
+ void reciprocal(const Size2D &size,
574
+ const s16 * srcBase, ptrdiff_t srcStride,
575
+ s16 * dstBase, ptrdiff_t dstStride,
576
+ f32 scale,
577
+ CONVERT_POLICY cpolicy)
578
+ {
579
+ recip<s16>(size, srcBase, srcStride, dstBase, dstStride, scale, cpolicy);
580
+ }
581
+
582
+ void reciprocal(const Size2D &size,
583
+ const s32 * srcBase, ptrdiff_t srcStride,
584
+ s32 * dstBase, ptrdiff_t dstStride,
585
+ f32 scale,
586
+ CONVERT_POLICY cpolicy)
587
+ {
588
+ recip<s32>(size, srcBase, srcStride, dstBase, dstStride, scale, cpolicy);
589
+ }
590
+
591
+ void reciprocal(const Size2D &size,
592
+ const f32 * srcBase, ptrdiff_t srcStride,
593
+ f32 * dstBase, ptrdiff_t dstStride,
594
+ f32 scale)
595
+ {
596
+ internal::assertSupportedConfiguration();
597
+ #ifdef CAROTENE_NEON
598
+ if (scale == 0.0f)
599
+ {
600
+ for (size_t y = 0; y < size.height; ++y)
601
+ {
602
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, y);
603
+ std::memset(dst, 0, sizeof(f32) * size.width);
604
+ }
605
+ return;
606
+ }
607
+
608
+ size_t roiw128 = size.width >= 3 ? size.width - 3 : 0;
609
+ size_t roiw64 = size.width >= 1 ? size.width - 1 : 0;
610
+
611
+ if (std::fabs(scale - 1.0f) < FLT_EPSILON)
612
+ {
613
+ for (size_t i = 0; i < size.height; ++i)
614
+ {
615
+ const f32 * src1 = internal::getRowPtr(srcBase, srcStride, i);
616
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
617
+ size_t j = 0;
618
+
619
+ for (; j < roiw128; j += 4)
620
+ {
621
+ internal::prefetch(src1 + j);
622
+
623
+ float32x4_t v_src1 = vld1q_f32(src1 + j);
624
+
625
+ vst1q_f32(dst + j, internal::vrecpq_f32(v_src1));
626
+ }
627
+
628
+ for (; j < roiw64; j += 2)
629
+ {
630
+ float32x2_t v_src1 = vld1_f32(src1 + j);
631
+
632
+ vst1_f32(dst + j, internal::vrecp_f32(v_src1));
633
+ }
634
+
635
+ for (; j < size.width; j++)
636
+ {
637
+ dst[j] = 1.0f / src1[j];
638
+ }
639
+ }
640
+ }
641
+ else
642
+ {
643
+ for (size_t i = 0; i < size.height; ++i)
644
+ {
645
+ const f32 * src1 = internal::getRowPtr(srcBase, srcStride, i);
646
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
647
+ size_t j = 0;
648
+
649
+ for (; j < roiw128; j += 4)
650
+ {
651
+ internal::prefetch(src1 + j);
652
+
653
+ float32x4_t v_src1 = vld1q_f32(src1 + j);
654
+
655
+ vst1q_f32(dst + j, vmulq_n_f32(internal::vrecpq_f32(v_src1), scale));
656
+ }
657
+
658
+ for (; j < roiw64; j += 2)
659
+ {
660
+ float32x2_t v_src1 = vld1_f32(src1 + j);
661
+
662
+ vst1_f32(dst + j, vmul_n_f32(internal::vrecp_f32(v_src1), scale));
663
+ }
664
+
665
+ for (; j < size.width; j++)
666
+ {
667
+ dst[j] = scale / src1[j];
668
+ }
669
+ }
670
+ }
671
+ #else
672
+ (void)size;
673
+ (void)srcBase;
674
+ (void)srcStride;
675
+ (void)dstBase;
676
+ (void)dstStride;
677
+ (void)scale;
678
+ #endif
679
+ }
680
+
681
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/dot_product.cpp ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ namespace CAROTENE_NS {
43
+
44
+ f64 dotProduct(const Size2D &_size,
45
+ const u8 * src0Base, ptrdiff_t src0Stride,
46
+ const u8 * src1Base, ptrdiff_t src1Stride)
47
+ {
48
+ internal::assertSupportedConfiguration();
49
+ #ifdef CAROTENE_NEON
50
+ Size2D size(_size);
51
+ if (src0Stride == src1Stride &&
52
+ src0Stride == (ptrdiff_t)(size.width))
53
+ {
54
+ size.width *= size.height;
55
+ size.height = 1;
56
+ }
57
+
58
+ // It is possible to accumulate up to 66051 uchar multiplication results in uint32 without overflow
59
+ // We process 16 elements and accumulate two new elements per step. So we could handle 66051/2*16 elements
60
+ #define DOT_UINT_BLOCKSIZE 66050*8
61
+ f64 result = 0.0;
62
+ for (size_t row = 0; row < size.height; ++row)
63
+ {
64
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, row);
65
+ const u8 * src1 = internal::getRowPtr(src1Base, src1Stride, row);
66
+
67
+ size_t i = 0;
68
+ uint64x2_t ws = vmovq_n_u64(0);
69
+
70
+ while(i + 16 <= size.width)
71
+ {
72
+ size_t lim = std::min(i + DOT_UINT_BLOCKSIZE, size.width) - 16;
73
+
74
+ uint32x4_t s1 = vmovq_n_u32(0);
75
+ uint32x4_t s2 = vmovq_n_u32(0);
76
+
77
+ for (; i <= lim; i += 16)
78
+ {
79
+ internal::prefetch(src0 + i);
80
+ internal::prefetch(src1 + i);
81
+
82
+ uint8x16_t vs1 = vld1q_u8(src0 + i);
83
+ uint8x16_t vs2 = vld1q_u8(src1 + i);
84
+
85
+ uint16x8_t vdot1 = vmull_u8(vget_low_u8(vs1), vget_low_u8(vs2));
86
+ uint16x8_t vdot2 = vmull_u8(vget_high_u8(vs1), vget_high_u8(vs2));
87
+
88
+ s1 = vpadalq_u16(s1, vdot1);
89
+ s2 = vpadalq_u16(s2, vdot2);
90
+ }
91
+
92
+ ws = vpadalq_u32(ws, s1);
93
+ ws = vpadalq_u32(ws, s2);
94
+ }
95
+
96
+ if(i + 8 <= size.width)
97
+ {
98
+ uint8x8_t vs1 = vld1_u8(src0 + i);
99
+ uint8x8_t vs2 = vld1_u8(src1 + i);
100
+
101
+ ws = vpadalq_u32(ws, vpaddlq_u16(vmull_u8(vs1, vs2)));
102
+ i += 8;
103
+ }
104
+
105
+ result += (double)vget_lane_u64(vadd_u64(vget_low_u64(ws), vget_high_u64(ws)), 0);
106
+
107
+ for (; i < size.width; ++i)
108
+ result += s32(src0[i]) * s32(src1[i]);
109
+ }
110
+ return result;
111
+ #else
112
+ (void)_size;
113
+ (void)src0Base;
114
+ (void)src0Stride;
115
+ (void)src1Base;
116
+ (void)src1Stride;
117
+
118
+ return 0;
119
+ #endif
120
+ }
121
+
122
+ f64 dotProduct(const Size2D &_size,
123
+ const s8 * src0Base, ptrdiff_t src0Stride,
124
+ const s8 * src1Base, ptrdiff_t src1Stride)
125
+ {
126
+ internal::assertSupportedConfiguration();
127
+ #ifdef CAROTENE_NEON
128
+ Size2D size(_size);
129
+ if (src0Stride == src1Stride &&
130
+ src0Stride == (ptrdiff_t)(size.width))
131
+ {
132
+ size.width *= size.height;
133
+ size.height = 1;
134
+ }
135
+
136
+ // It is possible to accumulate up to 131071 schar multiplication results in sint32 without overflow
137
+ // We process 16 elements and accumulate two new elements per step. So we could handle 131071/2*16 elements
138
+ #define DOT_INT_BLOCKSIZE 131070*8
139
+ f64 result = 0.0;
140
+ for (size_t row = 0; row < size.height; ++row)
141
+ {
142
+ const s8 * src0 = internal::getRowPtr(src0Base, src0Stride, row);
143
+ const s8 * src1 = internal::getRowPtr(src1Base, src1Stride, row);
144
+
145
+ size_t i = 0;
146
+ int64x2_t ws = vmovq_n_s64(0);
147
+
148
+ while(i + 16 <= size.width)
149
+ {
150
+ size_t lim = std::min(i + DOT_UINT_BLOCKSIZE, size.width) - 16;
151
+
152
+ int32x4_t s1 = vmovq_n_s32(0);
153
+ int32x4_t s2 = vmovq_n_s32(0);
154
+
155
+ for (; i <= lim; i += 16)
156
+ {
157
+ internal::prefetch(src0 + i);
158
+ internal::prefetch(src1 + i);
159
+
160
+ int8x16_t vs1 = vld1q_s8(src0 + i);
161
+ int8x16_t vs2 = vld1q_s8(src1 + i);
162
+
163
+ int16x8_t vdot1 = vmull_s8(vget_low_s8(vs1), vget_low_s8(vs2));
164
+ int16x8_t vdot2 = vmull_s8(vget_high_s8(vs1), vget_high_s8(vs2));
165
+
166
+ s1 = vpadalq_s16(s1, vdot1);
167
+ s2 = vpadalq_s16(s2, vdot2);
168
+ }
169
+
170
+ ws = vpadalq_s32(ws, s1);
171
+ ws = vpadalq_s32(ws, s2);
172
+ }
173
+
174
+ if(i + 8 <= size.width)
175
+ {
176
+ int8x8_t vs1 = vld1_s8(src0 + i);
177
+ int8x8_t vs2 = vld1_s8(src1 + i);
178
+
179
+ ws = vpadalq_s32(ws, vpaddlq_s16(vmull_s8(vs1, vs2)));
180
+ i += 8;
181
+ }
182
+
183
+ result += (double)vget_lane_s64(vadd_s64(vget_low_s64(ws), vget_high_s64(ws)), 0);
184
+
185
+ for (; i < size.width; ++i)
186
+ result += s32(src0[i]) * s32(src1[i]);
187
+ }
188
+ return result;
189
+ #else
190
+ (void)_size;
191
+ (void)src0Base;
192
+ (void)src0Stride;
193
+ (void)src1Base;
194
+ (void)src1Stride;
195
+
196
+ return 0;
197
+ #endif
198
+ }
199
+
200
+ f64 dotProduct(const Size2D &_size,
201
+ const f32 * src0Base, ptrdiff_t src0Stride,
202
+ const f32 * src1Base, ptrdiff_t src1Stride)
203
+ {
204
+ internal::assertSupportedConfiguration();
205
+ #ifdef CAROTENE_NEON
206
+ Size2D size(_size);
207
+ if (src0Stride == src1Stride &&
208
+ src0Stride == (ptrdiff_t)(size.width * sizeof(f32)))
209
+ {
210
+ size.width *= size.height;
211
+ size.height = 1;
212
+ }
213
+
214
+ #define DOT_FLOAT_BLOCKSIZE (1 << 13)
215
+ f64 result = 0.0;
216
+ for (size_t row = 0; row < size.height; ++row)
217
+ {
218
+ const f32 * src0 = internal::getRowPtr(src0Base, src0Stride, row);
219
+ const f32 * src1 = internal::getRowPtr(src1Base, src1Stride, row);
220
+
221
+ size_t i = 0;
222
+ while(i + 4 <= size.width)
223
+ {
224
+ size_t lim = std::min(i + DOT_FLOAT_BLOCKSIZE, size.width) - 4;
225
+ float32x4_t v_sum = vdupq_n_f32(0.0f);
226
+
227
+ for( ; i <= lim; i += 4 )
228
+ {
229
+ internal::prefetch(src0 + i);
230
+ internal::prefetch(src1 + i);
231
+ v_sum = vmlaq_f32(v_sum, vld1q_f32(src0 + i), vld1q_f32(src1 + i));
232
+ }
233
+
234
+ float32x2_t vres = vpadd_f32(vget_low_f32(v_sum),vget_high_f32(v_sum));
235
+ result += vget_lane_f32(vres, 0) + vget_lane_f32(vres, 1);
236
+ }
237
+
238
+ if(i + 2 <= size.width)
239
+ {
240
+ float32x2_t vres = vmul_f32(vld1_f32(src0 + i), vld1_f32(src1 + i));
241
+ result += vget_lane_f32(vres, 0) + vget_lane_f32(vres, 1);
242
+ i += 2;
243
+ }
244
+
245
+ for (; i < size.width; ++i)
246
+ result += src0[i] * src1[i];
247
+ }
248
+ return result;
249
+ #else
250
+ (void)_size;
251
+ (void)src0Base;
252
+ (void)src0Stride;
253
+ (void)src1Base;
254
+ (void)src1Stride;
255
+
256
+ return 0;
257
+ #endif
258
+ }
259
+
260
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/dummy.cpp ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ // This file is needed for compilation on some platforms e.g. with XCode generator
2
+ // Related issue: https://gitlab.kitware.com/cmake/cmake/-/issues/17457
3rdparty/carotene/src/fast.cpp ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+
41
+ /* This is FAST corner detector, contributed to OpenCV by the author, Edward Rosten.
42
+ Below is the original copyright and the references */
43
+
44
+ /*
45
+ Copyright (c) 2006, 2008 Edward Rosten
46
+ All rights reserved.
47
+
48
+ Redistribution and use in source and binary forms, with or without
49
+ modification, are permitted provided that the following conditions
50
+ are met:
51
+
52
+ *Redistributions of source code must retain the above copyright
53
+ notice, this list of conditions and the following disclaimer.
54
+
55
+ *Redistributions in binary form must reproduce the above copyright
56
+ notice, this list of conditions and the following disclaimer in the
57
+ documentation and/or other materials provided with the distribution.
58
+
59
+ *Neither the name of the University of Cambridge nor the names of
60
+ its contributors may be used to endorse or promote products derived
61
+ from this software without specific prior written permission.
62
+
63
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
67
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
68
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
69
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
70
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
71
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
72
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
73
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74
+ */
75
+
76
+ /*
77
+ The references are:
78
+ * Machine learning for high-speed corner detection,
79
+ E. Rosten and T. Drummond, ECCV 2006
80
+ * Faster and better: A machine learning approach to corner detection
81
+ E. Rosten, R. Porter and T. Drummond, PAMI, 2009
82
+ */
83
+
84
+ #include "common.hpp"
85
+
86
+ #include <vector>
87
+ #include <cstring>
88
+
89
+ namespace CAROTENE_NS {
90
+
91
+ #ifdef CAROTENE_NEON
92
+ namespace
93
+ {
94
+
95
+ void makeOffsets(ptrdiff_t pixel[], ptrdiff_t row_stride)
96
+ {
97
+ pixel[0] = 0 + row_stride * 3;
98
+ pixel[1] = 1 + row_stride * 3;
99
+ pixel[2] = 2 + row_stride * 2;
100
+ pixel[3] = 3 + row_stride * 1;
101
+ pixel[4] = 3 + row_stride * 0;
102
+ pixel[5] = 3 + row_stride * -1;
103
+ pixel[6] = 2 + row_stride * -2;
104
+ pixel[7] = 1 + row_stride * -3;
105
+ pixel[8] = 0 + row_stride * -3;
106
+ pixel[9] = -1 + row_stride * -3;
107
+ pixel[10] = -2 + row_stride * -2;
108
+ pixel[11] = -3 + row_stride * -1;
109
+ pixel[12] = -3 + row_stride * 0;
110
+ pixel[13] = -3 + row_stride * 1;
111
+ pixel[14] = -2 + row_stride * 2;
112
+ pixel[15] = -1 + row_stride * 3;
113
+ }
114
+
115
+ u8 cornerScore(const u8* ptr, const ptrdiff_t pixel[])
116
+ {
117
+ const s32 K = 8, N = 16 + K + 1;
118
+ s32 k, v = ptr[0];
119
+ s16 d[(N + 7) & ~7];
120
+ for( k = 0; k < N; k++ )
121
+ d[k] = (s16)(v - ptr[pixel[k]]);
122
+
123
+ int16x8_t q0 = vdupq_n_s16((s16)(-1000));
124
+ int16x8_t q1 = vdupq_n_s16((s16)(1000));
125
+
126
+ int16x8_t d0_7 = vld1q_s16(d + 0);
127
+ int16x8_t d8_15 = vld1q_s16(d + 8);
128
+ int16x8_t d16_23 = vld1q_s16(d + 16);
129
+ int16x8_t d24 = vld1q_s16(d + 24);
130
+
131
+ //k == 0
132
+ int16x8_t v0k0 = vextq_s16(d0_7, d8_15, 1);
133
+ int16x8_t v1k0 = vextq_s16(d0_7, d8_15, 2);
134
+ int16x8_t ak0 = vminq_s16(v0k0, v1k0);
135
+ int16x8_t bk0 = vmaxq_s16(v0k0, v1k0);
136
+
137
+ v0k0 = vextq_s16(d0_7, d8_15, 3);
138
+ ak0 = vminq_s16(ak0, v0k0);
139
+ bk0 = vmaxq_s16(bk0, v0k0);
140
+
141
+ v1k0 = vextq_s16(d0_7, d8_15, 4);
142
+ ak0 = vminq_s16(ak0, v1k0);
143
+ bk0 = vmaxq_s16(bk0, v1k0);
144
+
145
+ v0k0 = vextq_s16(d0_7, d8_15, 5);
146
+ ak0 = vminq_s16(ak0, v0k0);
147
+ bk0 = vmaxq_s16(bk0, v0k0);
148
+
149
+ v1k0 = vextq_s16(d0_7, d8_15, 6);
150
+ ak0 = vminq_s16(ak0, v1k0);
151
+ bk0 = vmaxq_s16(bk0, v1k0);
152
+
153
+ v0k0 = vextq_s16(d0_7, d8_15, 7);
154
+ ak0 = vminq_s16(ak0, v0k0);
155
+ bk0 = vmaxq_s16(bk0, v0k0);
156
+
157
+ ak0 = vminq_s16(ak0, d8_15);
158
+ bk0 = vmaxq_s16(bk0, d8_15);
159
+
160
+ q0 = vmaxq_s16(q0, vminq_s16(ak0, d0_7));
161
+ q1 = vminq_s16(q1, vmaxq_s16(bk0, d0_7));
162
+
163
+ v1k0 = vextq_s16(d8_15, d16_23, 1);
164
+ q0 = vmaxq_s16(q0, vminq_s16(ak0, v1k0));
165
+ q1 = vminq_s16(q1, vmaxq_s16(bk0, v1k0));
166
+
167
+ //k == 8
168
+ int16x8_t v0k8 = v1k0;
169
+ int16x8_t v1k8 = vextq_s16(d8_15, d16_23, 2);
170
+ int16x8_t ak8 = vminq_s16(v0k8, v1k8);
171
+ int16x8_t bk8 = vmaxq_s16(v0k8, v1k8);
172
+
173
+ v0k8 = vextq_s16(d8_15, d16_23, 3);
174
+ ak8 = vminq_s16(ak8, v0k8);
175
+ bk8 = vmaxq_s16(bk8, v0k8);
176
+
177
+ v1k8 = vextq_s16(d8_15, d16_23, 4);
178
+ ak8 = vminq_s16(ak8, v1k8);
179
+ bk8 = vmaxq_s16(bk8, v1k8);
180
+
181
+ v0k8 = vextq_s16(d8_15, d16_23, 5);
182
+ ak8 = vminq_s16(ak8, v0k8);
183
+ bk8 = vmaxq_s16(bk8, v0k8);
184
+
185
+ v1k8 = vextq_s16(d8_15, d16_23, 6);
186
+ ak8 = vminq_s16(ak8, v1k8);
187
+ bk8 = vmaxq_s16(bk8, v1k8);
188
+
189
+ v0k8 = vextq_s16(d8_15, d16_23, 7);
190
+ ak8 = vminq_s16(ak8, v0k8);
191
+ bk8 = vmaxq_s16(bk8, v0k8);
192
+
193
+ ak8 = vminq_s16(ak8, d16_23);
194
+ bk8 = vmaxq_s16(bk8, d16_23);
195
+
196
+ q0 = vmaxq_s16(q0, vminq_s16(ak8, d8_15));
197
+ q1 = vminq_s16(q1, vmaxq_s16(bk8, d8_15));
198
+
199
+ v1k8 = vextq_s16(d16_23, d24, 1);
200
+ q0 = vmaxq_s16(q0, vminq_s16(ak8, v1k8));
201
+ q1 = vminq_s16(q1, vmaxq_s16(bk8, v1k8));
202
+
203
+ //fin
204
+ int16x8_t q = vmaxq_s16(q0, vsubq_s16(vmovq_n_s16(0), q1));
205
+ int16x4_t q2 = vmax_s16(vget_low_s16(q), vget_high_s16(q));
206
+ int32x4_t q2w = vmovl_s16(q2);
207
+ int32x2_t q4 = vmax_s32(vget_low_s32(q2w), vget_high_s32(q2w));
208
+ int32x2_t q8 = vmax_s32(q4, vreinterpret_s32_s64(vshr_n_s64(vreinterpret_s64_s32(q4), 32)));
209
+
210
+ return (u8)(vget_lane_s32(q8, 0) - 1);
211
+ }
212
+
213
+ } //namespace
214
+ #endif
215
+
216
+ void FAST(const Size2D &size,
217
+ u8 *srcBase, ptrdiff_t srcStride,
218
+ KeypointStore *keypoints,
219
+ u8 threshold, bool nonmax_suppression)
220
+ {
221
+ internal::assertSupportedConfiguration();
222
+ #ifdef CAROTENE_NEON
223
+ //keypoints.clear();
224
+
225
+ const s32 K = 8, N = 16 + K + 1;
226
+ ptrdiff_t i, j, k, pixel[N];
227
+ makeOffsets(pixel, srcStride);
228
+ for(k = 16; k < N; k++)
229
+ pixel[k] = pixel[k - 16];
230
+
231
+ uint8x16_t delta = vdupq_n_u8(128);
232
+ uint8x16_t t = vdupq_n_u8(threshold);
233
+ uint8x16_t K16 = vdupq_n_u8((u8)K);
234
+
235
+ u8 threshold_tab[512];
236
+ for( i = -255; i <= 255; i++ )
237
+ threshold_tab[i+255] = (u8)(i < -threshold ? 1 : i > threshold ? 2 : 0);
238
+
239
+ std::vector<u8> _buf((size.width+16)*3*(sizeof(ptrdiff_t) + sizeof(u8)) + 128);
240
+ u8* buf[3];
241
+ buf[0] = &_buf[0]; buf[1] = buf[0] + size.width; buf[2] = buf[1] + size.width;
242
+ ptrdiff_t* cpbuf[3];
243
+ cpbuf[0] = (ptrdiff_t*)internal::alignPtr(buf[2] + size.width, sizeof(ptrdiff_t)) + 1;
244
+ cpbuf[1] = cpbuf[0] + size.width + 1;
245
+ cpbuf[2] = cpbuf[1] + size.width + 1;
246
+ memset(buf[0], 0, size.width*3);
247
+
248
+ for(i = 3; i < (ptrdiff_t)size.height-2; i++)
249
+ {
250
+ const u8* ptr = internal::getRowPtr(srcBase, srcStride, i) + 3;
251
+ u8* curr = buf[(i - 3)%3];
252
+ ptrdiff_t* cornerpos = cpbuf[(i - 3)%3];
253
+ memset(curr, 0, size.width);
254
+ ptrdiff_t ncorners = 0;
255
+
256
+ if( i < (ptrdiff_t)size.height - 3 )
257
+ {
258
+ j = 3;
259
+
260
+ for(; j < (ptrdiff_t)size.width - 16 - 3; j += 16, ptr += 16)
261
+ {
262
+ internal::prefetch(ptr);
263
+ internal::prefetch(ptr + pixel[0]);
264
+ internal::prefetch(ptr + pixel[2]);
265
+
266
+ uint8x16_t v0 = vld1q_u8(ptr);
267
+ int8x16_t v1 = vreinterpretq_s8_u8(veorq_u8(vqsubq_u8(v0, t), delta));
268
+ int8x16_t v2 = vreinterpretq_s8_u8(veorq_u8(vqaddq_u8(v0, t), delta));
269
+
270
+ int8x16_t x0 = vreinterpretq_s8_u8(vsubq_u8(vld1q_u8(ptr + pixel[0]), delta));
271
+ int8x16_t x1 = vreinterpretq_s8_u8(vsubq_u8(vld1q_u8(ptr + pixel[4]), delta));
272
+ int8x16_t x2 = vreinterpretq_s8_u8(vsubq_u8(vld1q_u8(ptr + pixel[8]), delta));
273
+ int8x16_t x3 = vreinterpretq_s8_u8(vsubq_u8(vld1q_u8(ptr + pixel[12]), delta));
274
+
275
+ uint8x16_t m0 = vandq_u8(vcgtq_s8(x0, v2), vcgtq_s8(x1, v2));
276
+ uint8x16_t m1 = vandq_u8(vcgtq_s8(v1, x0), vcgtq_s8(v1, x1));
277
+ m0 = vorrq_u8(m0, vandq_u8(vcgtq_s8(x1, v2), vcgtq_s8(x2, v2)));
278
+ m1 = vorrq_u8(m1, vandq_u8(vcgtq_s8(v1, x1), vcgtq_s8(v1, x2)));
279
+ m0 = vorrq_u8(m0, vandq_u8(vcgtq_s8(x2, v2), vcgtq_s8(x3, v2)));
280
+ m1 = vorrq_u8(m1, vandq_u8(vcgtq_s8(v1, x2), vcgtq_s8(v1, x3)));
281
+ m0 = vorrq_u8(m0, vandq_u8(vcgtq_s8(x3, v2), vcgtq_s8(x0, v2)));
282
+ m1 = vorrq_u8(m1, vandq_u8(vcgtq_s8(v1, x3), vcgtq_s8(v1, x0)));
283
+ m0 = vorrq_u8(m0, m1);
284
+
285
+ u64 mask[2];
286
+ vst1q_u64(mask, vreinterpretq_u64_u8(m0));
287
+
288
+ if( mask[0] == 0 )
289
+ {
290
+ if (mask[1] != 0)
291
+ {
292
+ j -= 8;
293
+ ptr -= 8;
294
+ }
295
+ continue;
296
+ }
297
+
298
+ uint8x16_t c0 = vmovq_n_u8(0);
299
+ uint8x16_t c1 = vmovq_n_u8(0);
300
+ uint8x16_t max0 = vmovq_n_u8(0);
301
+ uint8x16_t max1 = vmovq_n_u8(0);
302
+ for( k = 0; k < N; k++ )
303
+ {
304
+ int8x16_t x = vreinterpretq_s8_u8(veorq_u8(vld1q_u8(ptr + pixel[k]), delta));
305
+ m0 = vcgtq_s8(x, v2);
306
+ m1 = vcgtq_s8(v1, x);
307
+
308
+ c0 = vandq_u8(vsubq_u8(c0, m0), m0);
309
+ c1 = vandq_u8(vsubq_u8(c1, m1), m1);
310
+
311
+ max0 = vmaxq_u8(max0, c0);
312
+ max1 = vmaxq_u8(max1, c1);
313
+ }
314
+
315
+ max0 = vmaxq_u8(max0, max1);
316
+ u8 m[16];
317
+ vst1q_u8(m, vcgtq_u8(max0, K16));
318
+
319
+ for( k = 0; k < 16; ++k )
320
+ if(m[k])
321
+ {
322
+ cornerpos[ncorners++] = j+k;
323
+ if(nonmax_suppression)
324
+ curr[j+k] = cornerScore(ptr+k, pixel);
325
+ }
326
+ }
327
+
328
+ for( ; j < (s32)size.width - 3; j++, ptr++ )
329
+ {
330
+ s32 v = ptr[0];
331
+ const u8* tab = &threshold_tab[0] - v + 255;
332
+ s32 d = tab[ptr[pixel[0]]] | tab[ptr[pixel[8]]];
333
+
334
+ if( d == 0 )
335
+ continue;
336
+
337
+ d &= tab[ptr[pixel[2]]] | tab[ptr[pixel[10]]];
338
+ d &= tab[ptr[pixel[4]]] | tab[ptr[pixel[12]]];
339
+ d &= tab[ptr[pixel[6]]] | tab[ptr[pixel[14]]];
340
+
341
+ if( d == 0 )
342
+ continue;
343
+
344
+ d &= tab[ptr[pixel[1]]] | tab[ptr[pixel[9]]];
345
+ d &= tab[ptr[pixel[3]]] | tab[ptr[pixel[11]]];
346
+ d &= tab[ptr[pixel[5]]] | tab[ptr[pixel[13]]];
347
+ d &= tab[ptr[pixel[7]]] | tab[ptr[pixel[15]]];
348
+
349
+ if( d & 1 )
350
+ {
351
+ s32 vt = v - threshold, count = 0;
352
+
353
+ for( k = 0; k < N; k++ )
354
+ {
355
+ s32 x = ptr[pixel[k]];
356
+ if(x < vt)
357
+ {
358
+ if( ++count > K )
359
+ {
360
+ cornerpos[ncorners++] = j;
361
+ if(nonmax_suppression)
362
+ curr[j] = cornerScore(ptr, pixel);
363
+ break;
364
+ }
365
+ }
366
+ else
367
+ count = 0;
368
+ }
369
+ }
370
+
371
+ if( d & 2 )
372
+ {
373
+ s32 vt = v + threshold, count = 0;
374
+
375
+ for( k = 0; k < N; k++ )
376
+ {
377
+ s32 x = ptr[pixel[k]];
378
+ if(x > vt)
379
+ {
380
+ if( ++count > K )
381
+ {
382
+ cornerpos[ncorners++] = j;
383
+ if(nonmax_suppression)
384
+ curr[j] = cornerScore(ptr, pixel);
385
+ break;
386
+ }
387
+ }
388
+ else
389
+ count = 0;
390
+ }
391
+ }
392
+ }
393
+ }
394
+
395
+ cornerpos[-1] = ncorners;
396
+
397
+ if( i == 3 )
398
+ continue;
399
+
400
+ const u8* prev = buf[(i - 4 + 3)%3];
401
+ const u8* pprev = buf[(i - 5 + 3)%3];
402
+ cornerpos = cpbuf[(i - 4 + 3)%3];
403
+ ncorners = cornerpos[-1];
404
+
405
+ for( k = 0; k < ncorners; k++ )
406
+ {
407
+ j = cornerpos[k];
408
+ s32 score = prev[j];
409
+ if( !nonmax_suppression ||
410
+ (score > prev[j+1] && score > prev[j-1] &&
411
+ score > pprev[j-1] && score > pprev[j] && score > pprev[j+1] &&
412
+ score > curr[j-1] && score > curr[j] && score > curr[j+1]) )
413
+ {
414
+ keypoints->push((f32)j, (f32)(i-1), 7.f, -1, (f32)score);
415
+ }
416
+ }
417
+ }
418
+ #else
419
+ (void)size;
420
+ (void)srcBase;
421
+ (void)srcStride;
422
+ (void)keypoints;
423
+ (void)threshold;
424
+ (void)nonmax_suppression;
425
+ #endif
426
+ }
427
+
428
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/fill_minmaxloc.cpp ADDED
@@ -0,0 +1,442 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ namespace CAROTENE_NS {
43
+
44
+ #ifdef CAROTENE_NEON
45
+
46
+ namespace {
47
+
48
+ template <typename T>
49
+ void process(const T * src, size_t j0, size_t j1, size_t i,
50
+ T minVal, size_t * minLocPtr, s32 & minLocCount, s32 minLocCapacity,
51
+ T maxVal, size_t * maxLocPtr, s32 & maxLocCount, s32 maxLocCapacity)
52
+ {
53
+ for (size_t j = j0; j < j1; ++j)
54
+ {
55
+ T val = src[j];
56
+
57
+ if (val == maxVal)
58
+ {
59
+ if (maxLocCount < maxLocCapacity)
60
+ {
61
+ maxLocPtr[maxLocCount] = j;
62
+ maxLocPtr[maxLocCount + 1] = i;
63
+ }
64
+ maxLocCount += 2;
65
+ }
66
+
67
+ if (val == minVal)
68
+ {
69
+ if (minLocCount < minLocCapacity)
70
+ {
71
+ minLocPtr[minLocCount] = j;
72
+ minLocPtr[minLocCount + 1] = i;
73
+ }
74
+ minLocCount += 2;
75
+ }
76
+ }
77
+ }
78
+
79
+ } // namespace
80
+
81
+ #endif
82
+
83
+ void fillMinMaxLocs(const Size2D & size,
84
+ const u8 * srcBase, ptrdiff_t srcStride,
85
+ u8 minVal, size_t * minLocPtr, s32 & minLocCount, s32 minLocCapacity,
86
+ u8 maxVal, size_t * maxLocPtr, s32 & maxLocCount, s32 maxLocCapacity)
87
+ {
88
+ internal::assertSupportedConfiguration();
89
+ #ifdef CAROTENE_NEON
90
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
91
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
92
+
93
+ uint8x16_t v_maxval16 = vdupq_n_u8(maxVal), v_minval16 = vdupq_n_u8(minVal);
94
+ uint8x8_t v_maxval8 = vdup_n_u8(maxVal), v_minval8 = vdup_n_u8(minVal);
95
+
96
+ u64 mask[2] = { 0ul };
97
+
98
+ minLocCapacity <<= 1;
99
+ maxLocCapacity <<= 1;
100
+
101
+ for (size_t i = 0; i < size.height; ++i)
102
+ {
103
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, i);
104
+ size_t j = 0;
105
+
106
+ for ( ; j < roiw16; j += 16)
107
+ {
108
+ internal::prefetch(src + j);
109
+ uint8x16_t v_src = vld1q_u8(src + j);
110
+
111
+ uint8x16_t v_maxmask = vceqq_u8(v_src, v_maxval16);
112
+ uint8x16_t v_minmask = vceqq_u8(v_src, v_minval16);
113
+ uint8x16_t v_mask = vorrq_u8(v_maxmask, v_minmask);
114
+
115
+ vst1q_u8((u8 *)&mask[0], v_mask);
116
+
117
+ if (mask[0])
118
+ process(src, j, j + 8, i,
119
+ minVal, minLocPtr, minLocCount, minLocCapacity,
120
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
121
+ if (mask[1])
122
+ process(src, j + 8, j + 16, i,
123
+ minVal, minLocPtr, minLocCount, minLocCapacity,
124
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
125
+ }
126
+ for ( ; j < roiw8; j += 8)
127
+ {
128
+ uint8x8_t v_src = vld1_u8(src + j);
129
+
130
+ uint8x8_t v_maxmask = vceq_u8(v_src, v_maxval8);
131
+ uint8x8_t v_minmask = vceq_u8(v_src, v_minval8);
132
+ uint8x8_t v_mask = vorr_u8(v_maxmask, v_minmask);
133
+
134
+ vst1_u8((u8 *)&mask[0], v_mask);
135
+
136
+ if (mask[0])
137
+ process(src, j, j + 8, i,
138
+ minVal, minLocPtr, minLocCount, minLocCapacity,
139
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
140
+ }
141
+
142
+ process(src, j, size.width, i,
143
+ minVal, minLocPtr, minLocCount, minLocCapacity,
144
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
145
+ }
146
+
147
+ minLocCount >>= 1;
148
+ maxLocCount >>= 1;
149
+ #else
150
+ (void)size;
151
+ (void)srcBase;
152
+ (void)srcStride;
153
+ (void)minVal;
154
+ (void)minLocPtr;
155
+ (void)minLocCount;
156
+ (void)minLocCapacity;
157
+ (void)maxVal;
158
+ (void)maxLocPtr;
159
+ (void)maxLocCount;
160
+ (void)maxLocCapacity;
161
+ #endif
162
+ }
163
+
164
+ void fillMinMaxLocs(const Size2D & size,
165
+ const u16 * srcBase, ptrdiff_t srcStride,
166
+ u16 minVal, size_t * minLocPtr, s32 & minLocCount, s32 minLocCapacity,
167
+ u16 maxVal, size_t * maxLocPtr, s32 & maxLocCount, s32 maxLocCapacity)
168
+ {
169
+ internal::assertSupportedConfiguration();
170
+ #ifdef CAROTENE_NEON
171
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
172
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
173
+
174
+ uint16x8_t v_maxval8 = vdupq_n_u16(maxVal),
175
+ v_minval8 = vdupq_n_u16(minVal);
176
+ u64 mask[2] = { 0ul };
177
+
178
+ minLocCapacity <<= 1;
179
+ maxLocCapacity <<= 1;
180
+
181
+ for (size_t i = 0; i < size.height; ++i)
182
+ {
183
+ const u16 * src = internal::getRowPtr(srcBase, srcStride, i);
184
+ size_t j = 0;
185
+
186
+ for ( ; j < roiw16; j += 16)
187
+ {
188
+ internal::prefetch(src + j);
189
+ uint16x8_t v_src0 = vld1q_u16(src + j), v_src1 = vld1q_u16(src + j + 8);
190
+
191
+ uint16x8_t v_mask0 = vorrq_u16(vceqq_u16(v_src0, v_maxval8), vceqq_u16(v_src0, v_minval8));
192
+ uint16x8_t v_mask1 = vorrq_u16(vceqq_u16(v_src1, v_maxval8), vceqq_u16(v_src1, v_minval8));
193
+
194
+ vst1q_u8((u8 *)&mask[0], vcombine_u8(vmovn_u16(v_mask0), vmovn_u16(v_mask1)));
195
+
196
+ if (mask[0])
197
+ process(src, j, j + 8, i,
198
+ minVal, minLocPtr, minLocCount, minLocCapacity,
199
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
200
+ if (mask[1])
201
+ process(src, j + 8, j + 16, i,
202
+ minVal, minLocPtr, minLocCount, minLocCapacity,
203
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
204
+ }
205
+ for ( ; j < roiw8; j += 8)
206
+ {
207
+ internal::prefetch(src + j);
208
+ uint16x8_t v_src = vld1q_u16(src + j);
209
+
210
+ uint16x8_t v_maxmask = vceqq_u16(v_src, v_maxval8);
211
+ uint16x8_t v_minmask = vceqq_u16(v_src, v_minval8);
212
+ uint16x8_t v_mask = vorrq_u16(v_maxmask, v_minmask);
213
+
214
+ vst1_u8((u8 *)&mask[0], vmovn_u16(v_mask));
215
+
216
+ if (mask[0])
217
+ process(src, j, j + 8, i,
218
+ minVal, minLocPtr, minLocCount, minLocCapacity,
219
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
220
+ }
221
+
222
+ process(src, j, size.width, i,
223
+ minVal, minLocPtr, minLocCount, minLocCapacity,
224
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
225
+ }
226
+
227
+ minLocCount >>= 1;
228
+ maxLocCount >>= 1;
229
+ #else
230
+ (void)size;
231
+ (void)srcBase;
232
+ (void)srcStride;
233
+ (void)minVal;
234
+ (void)minLocPtr;
235
+ (void)minLocCount;
236
+ (void)minLocCapacity;
237
+ (void)maxVal;
238
+ (void)maxLocPtr;
239
+ (void)maxLocCount;
240
+ (void)maxLocCapacity;
241
+ #endif
242
+ }
243
+
244
+ void fillMinMaxLocs(const Size2D & size,
245
+ const s16 * srcBase, ptrdiff_t srcStride,
246
+ s16 minVal, size_t * minLocPtr, s32 & minLocCount, s32 minLocCapacity,
247
+ s16 maxVal, size_t * maxLocPtr, s32 & maxLocCount, s32 maxLocCapacity)
248
+ {
249
+ internal::assertSupportedConfiguration();
250
+ #ifdef CAROTENE_NEON
251
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
252
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
253
+
254
+ int16x8_t v_maxval8 = vdupq_n_s16(maxVal),
255
+ v_minval8 = vdupq_n_s16(minVal);
256
+ u64 mask[2] = { 0ul };
257
+
258
+ minLocCapacity <<= 1;
259
+ maxLocCapacity <<= 1;
260
+
261
+ for (size_t i = 0; i < size.height; ++i)
262
+ {
263
+ const s16 * src = internal::getRowPtr(srcBase, srcStride, i);
264
+ size_t j = 0;
265
+
266
+ for ( ; j < roiw16; j += 16)
267
+ {
268
+ internal::prefetch(src + j);
269
+ int16x8_t v_src0 = vld1q_s16(src + j), v_src1 = vld1q_s16(src + j + 8);
270
+
271
+ uint16x8_t v_mask0 = vorrq_u16(vceqq_s16(v_src0, v_maxval8), vceqq_s16(v_src0, v_minval8));
272
+ uint16x8_t v_mask1 = vorrq_u16(vceqq_s16(v_src1, v_maxval8), vceqq_s16(v_src1, v_minval8));
273
+
274
+ vst1q_u8((u8 *)&mask[0], vcombine_u8(vmovn_u16(v_mask0), vmovn_u16(v_mask1)));
275
+
276
+ if (mask[0])
277
+ process(src, j, j + 8, i,
278
+ minVal, minLocPtr, minLocCount, minLocCapacity,
279
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
280
+ if (mask[1])
281
+ process(src, j + 8, j + 16, i,
282
+ minVal, minLocPtr, minLocCount, minLocCapacity,
283
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
284
+ }
285
+ for ( ; j < roiw8; j += 8)
286
+ {
287
+ internal::prefetch(src + j);
288
+ int16x8_t v_src = vld1q_s16(src + j);
289
+
290
+ uint16x8_t v_maxmask = vceqq_s16(v_src, v_maxval8);
291
+ uint16x8_t v_minmask = vceqq_s16(v_src, v_minval8);
292
+ uint16x8_t v_mask = vorrq_u16(v_maxmask, v_minmask);
293
+
294
+ vst1_u8((u8 *)&mask[0], vmovn_u16(v_mask));
295
+
296
+ if (mask[0])
297
+ process(src, j, j + 8, i,
298
+ minVal, minLocPtr, minLocCount, minLocCapacity,
299
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
300
+ }
301
+
302
+ process(src, j, size.width, i,
303
+ minVal, minLocPtr, minLocCount, minLocCapacity,
304
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
305
+ }
306
+
307
+ minLocCount >>= 1;
308
+ maxLocCount >>= 1;
309
+ #else
310
+ (void)size;
311
+ (void)srcBase;
312
+ (void)srcStride;
313
+ (void)minVal;
314
+ (void)minLocPtr;
315
+ (void)minLocCount;
316
+ (void)minLocCapacity;
317
+ (void)maxVal;
318
+ (void)maxLocPtr;
319
+ (void)maxLocCount;
320
+ (void)maxLocCapacity;
321
+ #endif
322
+ }
323
+
324
+ void fillMinMaxLocs(const Size2D & size,
325
+ const s32 * srcBase, ptrdiff_t srcStride,
326
+ s32 minVal, size_t * minLocPtr, s32 & minLocCount, s32 minLocCapacity,
327
+ s32 maxVal, size_t * maxLocPtr, s32 & maxLocCount, s32 maxLocCapacity)
328
+ {
329
+ internal::assertSupportedConfiguration();
330
+ #ifdef CAROTENE_NEON
331
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
332
+
333
+ int32x4_t v_maxval4 = vdupq_n_s32(maxVal),
334
+ v_minval4 = vdupq_n_s32(minVal);
335
+ u64 mask = 0ul;
336
+
337
+ minLocCapacity <<= 1;
338
+ maxLocCapacity <<= 1;
339
+
340
+ for (size_t i = 0; i < size.height; ++i)
341
+ {
342
+ const s32 * src = internal::getRowPtr(srcBase, srcStride, i);
343
+ size_t j = 0;
344
+
345
+ for ( ; j < roiw8; j += 8)
346
+ {
347
+ internal::prefetch(src + j);
348
+ int32x4_t v_src0 = vld1q_s32(src + j), v_src1 = vld1q_s32(src + j + 4);
349
+
350
+ uint32x4_t v_mask0 = vorrq_u32(vceqq_s32(v_src0, v_maxval4), vceqq_s32(v_src0, v_minval4));
351
+ uint32x4_t v_mask1 = vorrq_u32(vceqq_s32(v_src1, v_maxval4), vceqq_s32(v_src1, v_minval4));
352
+
353
+ vst1_u8((u8 *)&mask, vmovn_u16(vcombine_u16(vmovn_u32(v_mask0), vmovn_u32(v_mask1))));
354
+
355
+ if (mask)
356
+ process(src, j, j + 8, i,
357
+ minVal, minLocPtr, minLocCount, minLocCapacity,
358
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
359
+ }
360
+
361
+ process(src, j, size.width, i,
362
+ minVal, minLocPtr, minLocCount, minLocCapacity,
363
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
364
+ }
365
+
366
+ minLocCount >>= 1;
367
+ maxLocCount >>= 1;
368
+ #else
369
+ (void)size;
370
+ (void)srcBase;
371
+ (void)srcStride;
372
+ (void)minVal;
373
+ (void)minLocPtr;
374
+ (void)minLocCount;
375
+ (void)minLocCapacity;
376
+ (void)maxVal;
377
+ (void)maxLocPtr;
378
+ (void)maxLocCount;
379
+ (void)maxLocCapacity;
380
+ #endif
381
+ }
382
+
383
+ void fillMinMaxLocs(const Size2D & size,
384
+ const u32 * srcBase, ptrdiff_t srcStride,
385
+ u32 minVal, size_t * minLocPtr, s32 & minLocCount, s32 minLocCapacity,
386
+ u32 maxVal, size_t * maxLocPtr, s32 & maxLocCount, s32 maxLocCapacity)
387
+ {
388
+ internal::assertSupportedConfiguration();
389
+ #ifdef CAROTENE_NEON
390
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
391
+
392
+ uint32x4_t v_maxval4 = vdupq_n_u32(maxVal),
393
+ v_minval4 = vdupq_n_u32(minVal);
394
+ u64 mask = 0ul;
395
+
396
+ minLocCapacity <<= 1;
397
+ maxLocCapacity <<= 1;
398
+
399
+ for (size_t i = 0; i < size.height; ++i)
400
+ {
401
+ const u32 * src = internal::getRowPtr(srcBase, srcStride, i);
402
+ size_t j = 0;
403
+
404
+ for ( ; j < roiw8; j += 8)
405
+ {
406
+ internal::prefetch(src + j);
407
+ uint32x4_t v_src0 = vld1q_u32(src + j), v_src1 = vld1q_u32(src + j + 4);
408
+
409
+ uint32x4_t v_mask0 = vorrq_u32(vceqq_u32(v_src0, v_maxval4), vceqq_u32(v_src0, v_minval4));
410
+ uint32x4_t v_mask1 = vorrq_u32(vceqq_u32(v_src1, v_maxval4), vceqq_u32(v_src1, v_minval4));
411
+
412
+ vst1_u8((u8 *)&mask, vmovn_u16(vcombine_u16(vmovn_u32(v_mask0), vmovn_u32(v_mask1))));
413
+
414
+ if (mask)
415
+ process(src, j, j + 8, i,
416
+ minVal, minLocPtr, minLocCount, minLocCapacity,
417
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
418
+ }
419
+
420
+ process(src, j, size.width, i,
421
+ minVal, minLocPtr, minLocCount, minLocCapacity,
422
+ maxVal, maxLocPtr, maxLocCount, maxLocCapacity);
423
+ }
424
+
425
+ minLocCount >>= 1;
426
+ maxLocCount >>= 1;
427
+ #else
428
+ (void)size;
429
+ (void)srcBase;
430
+ (void)srcStride;
431
+ (void)minVal;
432
+ (void)minLocPtr;
433
+ (void)minLocCount;
434
+ (void)minLocCapacity;
435
+ (void)maxVal;
436
+ (void)maxLocPtr;
437
+ (void)maxLocCount;
438
+ (void)maxLocCapacity;
439
+ #endif
440
+ }
441
+
442
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/flip.cpp ADDED
@@ -0,0 +1,222 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ #include <cstring>
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ bool isFlipSupported(FLIP_MODE flipMode, u32 elemSize)
48
+ {
49
+ bool supportedElemSize = (elemSize == 1) || (elemSize == 2) || (elemSize == 3) || (elemSize == 4);
50
+ return isSupportedConfiguration() &&
51
+ ((supportedElemSize && ((flipMode == FLIP_BOTH_MODE) || (flipMode == FLIP_HORIZONTAL_MODE))) ||
52
+ (flipMode == FLIP_VERTICAL_MODE));
53
+ }
54
+
55
+ #ifdef CAROTENE_NEON
56
+
57
+ namespace {
58
+
59
+ template <typename T>
60
+ void flip(const Size2D & size,
61
+ const void * srcBase, ptrdiff_t srcStride,
62
+ void * dstBase, ptrdiff_t dstStride,
63
+ FLIP_MODE flipMode)
64
+ {
65
+ using namespace internal;
66
+
67
+ typedef typename VecTraits<T>::vec128 vec128;
68
+ typedef typename VecTraits<T>::vec64 vec64;
69
+
70
+ u32 step_base = 16 / sizeof(T), step_tail = 8 / sizeof(T);
71
+ size_t roiw_base = size.width >= (step_base - 1) ? size.width - step_base + 1 : 0;
72
+ size_t roiw_tail = size.width >= (step_tail - 1) ? size.width - step_tail + 1 : 0;
73
+
74
+ for (size_t i = 0; i < size.height; ++i)
75
+ {
76
+ const T * src = getRowPtr((const T *)srcBase, srcStride, i);
77
+ T * dst = getRowPtr((T *)dstBase, dstStride, (flipMode & FLIP_VERTICAL_MODE) != 0 ? size.height - i - 1 : i);
78
+ size_t js = 0, jd = size.width;
79
+
80
+ for (; js < roiw_base; js += step_base, jd -= step_base)
81
+ {
82
+ prefetch(src + js);
83
+
84
+ vec128 v_src = vld1q(src + js);
85
+ vec128 v_dst = vrev64q(v_src);
86
+ v_dst = vcombine(vget_high(v_dst), vget_low(v_dst));
87
+ vst1q(dst + jd - step_base, v_dst);
88
+ }
89
+ for (; js < roiw_tail; js += step_tail, jd -= step_tail)
90
+ {
91
+ vec64 v_src = vld1(src + js);
92
+ vst1(dst + jd - step_tail, vrev64(v_src));
93
+ }
94
+
95
+ for (--jd; js < size.width; ++js, --jd)
96
+ dst[jd] = src[js];
97
+ }
98
+ }
99
+
100
+ template <typename T>
101
+ void flip3(const Size2D & size,
102
+ const void * srcBase, ptrdiff_t srcStride,
103
+ void * dstBase, ptrdiff_t dstStride,
104
+ FLIP_MODE flipMode)
105
+ {
106
+ using namespace internal;
107
+
108
+ #ifndef __ANDROID__
109
+ typedef typename VecTraits<T, 3>::vec128 vec128;
110
+ #endif
111
+ typedef typename VecTraits<T, 3>::vec64 vec64;
112
+
113
+ #ifndef __ANDROID__
114
+ u32 step_base = 16 / sizeof(T), step_base3 = step_base * 3;
115
+ size_t roiw_base = size.width >= (step_base - 1) ? size.width - step_base + 1 : 0;
116
+ #endif
117
+ u32 step_tail = 8 / sizeof(T), step_tail3 = step_tail * 3;
118
+ size_t roiw_tail = size.width >= (step_tail - 1) ? size.width - step_tail + 1 : 0;
119
+
120
+ for (size_t i = 0; i < size.height; ++i)
121
+ {
122
+ const T * src = getRowPtr((const T *)srcBase, srcStride, i);
123
+ T * dst = getRowPtr((T *)dstBase, dstStride, (flipMode & FLIP_VERTICAL_MODE) != 0 ? size.height - i - 1 : i);
124
+ size_t j = 0, js = 0, jd = size.width * 3;
125
+
126
+ #ifndef __ANDROID__
127
+ for (; j < roiw_base; j += step_base, js += step_base3, jd -= step_base3)
128
+ {
129
+ prefetch(src + js);
130
+
131
+ vec128 v_src = vld3q(src + js), v_dst;
132
+ v_src.val[0] = vrev64q(v_src.val[0]);
133
+ v_src.val[1] = vrev64q(v_src.val[1]);
134
+ v_src.val[2] = vrev64q(v_src.val[2]);
135
+
136
+ v_dst.val[0] = vcombine(vget_high(v_src.val[0]), vget_low(v_src.val[0]));
137
+ v_dst.val[1] = vcombine(vget_high(v_src.val[1]), vget_low(v_src.val[1]));
138
+ v_dst.val[2] = vcombine(vget_high(v_src.val[2]), vget_low(v_src.val[2]));
139
+
140
+ vst3q(dst + jd - step_base3, v_dst);
141
+ }
142
+ #endif // __ANDROID__
143
+
144
+ for (; j < roiw_tail; j += step_tail, js += step_tail3, jd -= step_tail3)
145
+ {
146
+ vec64 v_src = vld3(src + js), v_dst;
147
+ v_dst.val[0] = vrev64(v_src.val[0]);
148
+ v_dst.val[1] = vrev64(v_src.val[1]);
149
+ v_dst.val[2] = vrev64(v_src.val[2]);
150
+
151
+ vst3(dst + jd - step_tail3, v_dst);
152
+ }
153
+
154
+ for (jd -= 3; j < size.width; ++j, js += 3, jd -= 3)
155
+ {
156
+ dst[jd] = src[js];
157
+ dst[jd + 1] = src[js + 1];
158
+ dst[jd + 2] = src[js + 2];
159
+ }
160
+ }
161
+ }
162
+
163
+ typedef void (* flipFunc)(const Size2D &size,
164
+ const void * srcBase, ptrdiff_t srcStride,
165
+ void * dstBase, ptrdiff_t dstStride,
166
+ FLIP_MODE flipMode);
167
+
168
+ } // namespace
169
+
170
+ #endif
171
+
172
+ void flip(const Size2D &size,
173
+ const u8 * srcBase, ptrdiff_t srcStride,
174
+ u8 * dstBase, ptrdiff_t dstStride,
175
+ FLIP_MODE flipMode, u32 elemSize)
176
+ {
177
+ internal::assertSupportedConfiguration(isFlipSupported(flipMode, elemSize));
178
+ #ifdef CAROTENE_NEON
179
+
180
+ if (flipMode == FLIP_VERTICAL_MODE)
181
+ {
182
+ for (size_t y = 0; y < size.height; ++y)
183
+ {
184
+ const u8 * src_row = internal::getRowPtr(srcBase, srcStride, y);
185
+ u8 * dst_row = internal::getRowPtr(dstBase, dstStride, size.height - y - 1);
186
+
187
+ std::memcpy(dst_row, src_row, elemSize * size.width);
188
+ }
189
+ return;
190
+ }
191
+
192
+ flipFunc func = NULL;
193
+
194
+ if (elemSize == (u32)sizeof(u8))
195
+ func = &flip<u8>;
196
+ if (elemSize == (u32)sizeof(u16))
197
+ func = &flip<u16>;
198
+ if (elemSize == (u32)sizeof(u32))
199
+ func = &flip<u32>;
200
+ if (elemSize == (u32)sizeof(u8) * 3)
201
+ func = &flip3<u8>;
202
+
203
+ if (func == NULL)
204
+ return;
205
+
206
+ func(size,
207
+ srcBase, srcStride,
208
+ dstBase, dstStride,
209
+ flipMode);
210
+
211
+ #else
212
+ (void)size;
213
+ (void)srcBase;
214
+ (void)srcStride;
215
+ (void)dstBase;
216
+ (void)dstStride;
217
+ (void)flipMode;
218
+ (void)elemSize;
219
+ #endif
220
+ }
221
+
222
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/gaussian_blur.cpp ADDED
@@ -0,0 +1,1059 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "saturate_cast.hpp"
42
+ #include "separable_filter.hpp"
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ bool isGaussianBlur3x3Supported(const Size2D &size, BORDER_MODE border)
47
+ {
48
+ return isSupportedConfiguration() && size.width >= 8 &&
49
+ (border == BORDER_MODE_CONSTANT ||
50
+ border == BORDER_MODE_REPLICATE);
51
+ }
52
+
53
+ void gaussianBlur3x3(const Size2D &size,
54
+ const u8 * srcBase, ptrdiff_t srcStride,
55
+ u8 * dstBase, ptrdiff_t dstStride,
56
+ BORDER_MODE border, u8 borderValue)
57
+ {
58
+ internal::assertSupportedConfiguration(isGaussianBlur3x3Supported(size, border));
59
+ #ifdef CAROTENE_NEON
60
+ const uint16x8_t v_border_x4 = vdupq_n_u16(borderValue << 2);
61
+ const uint16x8_t v_zero = vdupq_n_u16(0);
62
+ const uint8x8_t v_border = vdup_n_u8(borderValue);
63
+
64
+ uint16x8_t tprev = v_zero, tcurr = v_zero, tnext = v_zero;
65
+ uint16x8_t t0 = v_zero, t1 = v_zero, t2 = v_zero;
66
+
67
+ ptrdiff_t width = (ptrdiff_t)size.width, height = (ptrdiff_t)size.height;
68
+
69
+ for (ptrdiff_t y = 0; y < height; ++y)
70
+ {
71
+ const u8 * srow0 = y == 0 && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::max<ptrdiff_t>(y - 1, 0));
72
+ const u8 * srow1 = internal::getRowPtr(srcBase, srcStride, y);
73
+ const u8 * srow2 = y + 1 == height && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::min(y + 1, height - 1));
74
+ u8 * drow = internal::getRowPtr(dstBase, dstStride, y);
75
+
76
+ s16 prevx = 0, currx = 0, nextx = 0;
77
+ ptrdiff_t x = 0;
78
+ const ptrdiff_t bwidth = y + 2 < height ? width : (width - 8);
79
+
80
+ // perform vertical convolution
81
+ for ( ; x <= bwidth; x += 8)
82
+ {
83
+ internal::prefetch(srow0 + x);
84
+ internal::prefetch(srow1 + x);
85
+ internal::prefetch(srow2 + x);
86
+
87
+ uint8x8_t x0 = !srow0 ? v_border : vld1_u8(srow0 + x);
88
+ uint8x8_t x1 = vld1_u8(srow1 + x);
89
+ uint8x8_t x2 = !srow2 ? v_border : vld1_u8(srow2 + x);
90
+
91
+ // calculate values for plain CPU part below if needed
92
+ if (x + 8 >= bwidth)
93
+ {
94
+ ptrdiff_t x3 = x == width ? width - 1 : x;
95
+ ptrdiff_t x4 = border == BORDER_MODE_CONSTANT ? x3 - 1 : std::max<ptrdiff_t>(x3 - 1, 0);
96
+
97
+ if (border == BORDER_MODE_CONSTANT && x4 < 0)
98
+ prevx = borderValue;
99
+ else
100
+ prevx = (srow2 ? srow2[x4] : borderValue) + (srow1[x4] << 1) + (srow0 ? srow0[x4] : borderValue);
101
+
102
+ currx = (srow2 ? srow2[x3] : borderValue) + (srow1[x3] << 1) + (srow0 ? srow0[x3] : borderValue);
103
+ }
104
+
105
+ // make shift
106
+ if (x)
107
+ {
108
+ tprev = tcurr;
109
+ tcurr = tnext;
110
+ }
111
+
112
+ // and calculate next value
113
+ tnext = vaddq_u16(vaddl_u8(x0, x2), vshll_n_u8(x1, 1));
114
+
115
+ // make extrapolation for the first elements
116
+ if (!x)
117
+ {
118
+ // make border
119
+ if (border == BORDER_MODE_CONSTANT)
120
+ tcurr = v_border_x4;
121
+ else if (border == BORDER_MODE_REPLICATE)
122
+ tcurr = vdupq_n_u16(vgetq_lane_u16(tnext, 0));
123
+
124
+ continue;
125
+ }
126
+
127
+ // combine 3 "shifted" vectors
128
+ t0 = vextq_u16(tprev, tcurr, 7);
129
+ t1 = tcurr;
130
+ t2 = vextq_u16(tcurr, tnext, 1);
131
+
132
+ // and add them
133
+ t0 = vqaddq_u16(vshlq_n_u16(t1, 1), vqaddq_u16(t0, t2));
134
+ vst1_u8(drow + x - 8, vshrn_n_u16(t0, 4));
135
+ }
136
+
137
+ x -= 8;
138
+ if (x == width)
139
+ --x;
140
+
141
+ for ( ; x < width; ++x)
142
+ {
143
+ // make extrapolation for the last elements
144
+ if (x + 1 >= width)
145
+ {
146
+ if (border == BORDER_MODE_CONSTANT)
147
+ nextx = borderValue << 2;
148
+ else if (border == BORDER_MODE_REPLICATE)
149
+ nextx = srow2[x] + (srow1[x] << 1) + srow0[x];
150
+ }
151
+ else
152
+ nextx = (srow2 ? srow2[x + 1] : borderValue) +
153
+ (srow1[x + 1] << 1) +
154
+ (srow0 ? srow0[x + 1] : borderValue);
155
+
156
+ f32 val = (prevx + (currx << 1) + nextx) >> 4;
157
+ drow[x] = internal::saturate_cast<u8>((s32)val);
158
+
159
+ // make shift
160
+ prevx = currx;
161
+ currx = nextx;
162
+ }
163
+ }
164
+ #else
165
+ (void)srcBase;
166
+ (void)srcStride;
167
+ (void)dstBase;
168
+ (void)dstStride;
169
+ (void)borderValue;
170
+ #endif
171
+ }
172
+
173
+ bool isGaussianBlur3x3MarginSupported(const Size2D &size, BORDER_MODE border, Margin borderMargin)
174
+ {
175
+ return isSeparableFilter3x3Supported(size, border, 0, 0, borderMargin);
176
+ }
177
+
178
+ void gaussianBlur3x3Margin(const Size2D &size,
179
+ const u8 * srcBase, ptrdiff_t srcStride,
180
+ u8 * dstBase, ptrdiff_t dstStride,
181
+ BORDER_MODE border, u8 borderValue, Margin borderMargin)
182
+ {
183
+ internal::assertSupportedConfiguration(isGaussianBlur3x3MarginSupported(size, border, borderMargin));
184
+ #ifdef CAROTENE_NEON
185
+ internal::sepFilter3x3<internal::RowFilter3x3S16_121, internal::ColFilter3x3U8_121>::process(
186
+ size, srcBase, srcStride, dstBase, dstStride,
187
+ 0, 0, border, borderValue, borderMargin);
188
+ #else
189
+ (void)srcBase;
190
+ (void)srcStride;
191
+ (void)dstBase;
192
+ (void)dstStride;
193
+ (void)borderValue;
194
+ #endif
195
+ }
196
+
197
+ bool isGaussianBlur5x5Supported(const Size2D &size, s32 cn, BORDER_MODE border)
198
+ {
199
+ return isSupportedConfiguration() &&
200
+ cn > 0 && cn <= 4 &&
201
+ size.width >= 8 && size.height >= 2 &&
202
+ (border == BORDER_MODE_CONSTANT ||
203
+ border == BORDER_MODE_REFLECT101 ||
204
+ border == BORDER_MODE_REFLECT ||
205
+ border == BORDER_MODE_REPLICATE ||
206
+ border == BORDER_MODE_WRAP);
207
+ }
208
+
209
+ void gaussianBlur5x5(const Size2D &size, s32 cn,
210
+ const u8 * srcBase, ptrdiff_t srcStride,
211
+ u8 * dstBase, ptrdiff_t dstStride,
212
+ BORDER_MODE borderType, u8 borderValue, Margin borderMargin)
213
+ {
214
+ internal::assertSupportedConfiguration(isGaussianBlur5x5Supported(size, cn, borderType));
215
+ #ifdef CAROTENE_NEON
216
+ size_t colsn = size.width * cn;
217
+
218
+ std::vector<u8> _tmp;
219
+ u8 *tmp = 0;
220
+ if (borderType == BORDER_MODE_CONSTANT)
221
+ {
222
+ _tmp.assign(colsn + 4*cn, borderValue);
223
+ tmp = &_tmp[cn << 1];
224
+ }
225
+
226
+ ptrdiff_t idx_l1 = internal::borderInterpolate(-1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
227
+ ptrdiff_t idx_l2 = internal::borderInterpolate(-2, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
228
+ ptrdiff_t idx_r1 = internal::borderInterpolate(size.width + 0, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
229
+ ptrdiff_t idx_r2 = internal::borderInterpolate(size.width + 1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
230
+
231
+ //1-line buffer
232
+ std::vector<u16> _buf(cn * (size.width + 4) + 32 / sizeof(u16));
233
+ u16* lane = internal::alignPtr(&_buf[cn << 1], 32);
234
+
235
+ if (borderType == BORDER_MODE_CONSTANT)
236
+ for (s32 k = 0; k < cn; ++k)
237
+ {
238
+ lane[-cn+k] = borderValue;
239
+ lane[-cn-cn+k] = borderValue;
240
+ lane[colsn+k] = borderValue;
241
+ lane[colsn+cn+k] = borderValue;
242
+ }
243
+
244
+ uint8x8_t vc6u8 = vmov_n_u8(6);
245
+ uint16x8_t vc6u16 = vmovq_n_u16(6);
246
+ uint16x8_t vc4u16 = vmovq_n_u16(4);
247
+
248
+ for (size_t i = 0; i < size.height; ++i)
249
+ {
250
+ u8* dst = internal::getRowPtr(dstBase, dstStride, i);
251
+ //vertical convolution
252
+ ptrdiff_t idx_rm2 = internal::borderInterpolate(i - 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
253
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
254
+ ptrdiff_t idx_rp1 = internal::borderInterpolate(i + 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
255
+ ptrdiff_t idx_rp2 = internal::borderInterpolate(i + 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
256
+
257
+ const u8* ln0 = idx_rm2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm2) : tmp;
258
+ const u8* ln1 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
259
+ const u8* ln2 = internal::getRowPtr(srcBase, srcStride, i);
260
+ const u8* ln3 = idx_rp1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp1) : tmp;
261
+ const u8* ln4 = idx_rp2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp2) : tmp;
262
+
263
+ size_t x = 0;
264
+ for (; x <= colsn - 8; x += 8)
265
+ {
266
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, x % 5 - 2));
267
+ uint8x8_t v0 = vld1_u8(ln0+x);
268
+ uint8x8_t v1 = vld1_u8(ln1+x);
269
+ uint8x8_t v2 = vld1_u8(ln2+x);
270
+ uint8x8_t v3 = vld1_u8(ln3+x);
271
+ uint8x8_t v4 = vld1_u8(ln4+x);
272
+
273
+ uint16x8_t v = vaddl_u8(v0, v4);
274
+ uint16x8_t v13 = vaddl_u8(v1, v3);
275
+
276
+ v = vmlal_u8(v, v2, vc6u8);
277
+ v = vmlaq_u16(v, v13, vc4u16);
278
+
279
+ vst1q_u16(lane + x, v);
280
+ }
281
+ for (; x < colsn; ++x)
282
+ lane[x] = ln0[x] + ln4[x] + u16(4) * (ln1[x] + ln3[x]) + u16(6) * ln2[x];
283
+
284
+ //left&right borders
285
+ if (borderType != BORDER_MODE_CONSTANT)
286
+ for (s32 k = 0; k < cn; ++k)
287
+ {
288
+ lane[-cn+k] = lane[idx_l1 + k];
289
+ lane[-cn-cn+k] = lane[idx_l2 + k];
290
+
291
+ lane[colsn+k] = lane[idx_r1 + k];
292
+ lane[colsn+cn+k] = lane[idx_r2 + k];
293
+ }
294
+
295
+ //horizontal convolution
296
+ x = 0;
297
+ switch(cn)
298
+ {
299
+ case 1:
300
+ for (; x <= colsn - 8; x += 8)
301
+ {
302
+ internal::prefetch(lane + x);
303
+
304
+ uint16x8_t lane0 = vld1q_u16(lane + x - 2);
305
+ uint16x8_t lane4 = vld1q_u16(lane + x + 2);
306
+ uint16x8_t lane1 = vld1q_u16(lane + x - 1);
307
+ uint16x8_t lane3 = vld1q_u16(lane + x + 1);
308
+ uint16x8_t lane2 = vld1q_u16(lane + x + 0);
309
+
310
+ uint16x8_t ln04 = vaddq_u16(lane0, lane4);
311
+ uint16x8_t ln13 = vaddq_u16(lane1, lane3);
312
+
313
+ uint16x8_t ln042 = vmlaq_u16(ln04, lane2, vc6u16);
314
+ uint16x8_t lsw = vmlaq_u16(ln042, ln13, vc4u16);
315
+
316
+ uint8x8_t ls = vrshrn_n_u16(lsw, 8);
317
+
318
+ vst1_u8(dst + x, ls);
319
+ }
320
+ break;
321
+ case 2:
322
+ for (; x <= colsn - 8*2; x += 8*2)
323
+ {
324
+ internal::prefetch(lane + x);
325
+
326
+ u16* lidx0 = lane + x - 2*2;
327
+ u16* lidx1 = lane + x - 1*2;
328
+ u16* lidx3 = lane + x + 1*2;
329
+ u16* lidx4 = lane + x + 2*2;
330
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
331
+ __asm__ __volatile__ (
332
+ "vld2.16 {d0, d2}, [%[in0]]! \n\t"
333
+ "vld2.16 {d1, d3}, [%[in0]] \n\t"
334
+ "vld2.16 {d8, d10}, [%[in4]]! \n\t"
335
+ "vld2.16 {d9, d11}, [%[in4]] \n\t"
336
+ "vadd.i16 q0, q4 \n\t"
337
+ "vadd.i16 q1, q5 \n\t"
338
+ "vld2.16 {d16, d18}, [%[in1]]! \n\t"
339
+ "vld2.16 {d17, d19}, [%[in1]] \n\t"
340
+ "vld2.16 {d8, d10}, [%[in3]]! \n\t"
341
+ "vld2.16 {d9, d11}, [%[in3]] \n\t"
342
+ "vadd.i16 q4, q8 \n\t"
343
+ "vadd.i16 q5, q9 \n\t"
344
+ "vld2.16 {d16, d18}, [%[in2]] \n\t"
345
+ "vld2.16 {d17, d19}, [%[in22]] \n\t"
346
+ "vmla.i16 q0, q4, %q[c4] \n\t"
347
+ "vmla.i16 q1, q5, %q[c4] \n\t"
348
+ "vmla.i16 q0, q8, %q[c6] \n\t"
349
+ "vmla.i16 q1, q9, %q[c6] \n\t"
350
+ "vrshrn.u16 d8, q0, #8 \n\t"
351
+ "vrshrn.u16 d9, q1, #8 \n\t"
352
+ "vst2.8 {d8-d9}, [%[out]] \n\t"
353
+ : [in0] "=r" (lidx0),
354
+ [in1] "=r" (lidx1),
355
+ [in3] "=r" (lidx3),
356
+ [in4] "=r" (lidx4)
357
+ : [out] "r" (dst + x),
358
+ "0" (lidx0),
359
+ "1" (lidx1),
360
+ "2" (lidx3),
361
+ "3" (lidx4),
362
+ [in2] "r" (lane + x),
363
+ [in22] "r" (lane + x + 4*2),
364
+ [c4] "w" (vc4u16), [c6] "w" (vc6u16)
365
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23"
366
+ );
367
+ #else
368
+ uint16x8x2_t vLane0 = vld2q_u16(lidx0);
369
+ uint16x8x2_t vLane1 = vld2q_u16(lidx1);
370
+ uint16x8x2_t vLane2 = vld2q_u16(lane + x);
371
+ uint16x8x2_t vLane3 = vld2q_u16(lidx3);
372
+ uint16x8x2_t vLane4 = vld2q_u16(lidx4);
373
+
374
+ uint16x8_t vSum_0_4 = vaddq_u16(vLane0.val[0], vLane4.val[0]);
375
+ uint16x8_t vSum_1_5 = vaddq_u16(vLane0.val[1], vLane4.val[1]);
376
+
377
+ uint16x8_t vSum_4_8 = vaddq_u16(vLane1.val[0], vLane3.val[0]);
378
+ uint16x8_t vSum_5_9 = vaddq_u16(vLane1.val[1], vLane3.val[1]);
379
+
380
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vSum_4_8, vc4u16);
381
+ vSum_1_5 = vmlaq_u16(vSum_1_5, vSum_5_9, vc4u16);
382
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vLane2.val[0], vc6u16);
383
+ vSum_1_5 = vmlaq_u16(vSum_1_5, vLane2.val[1], vc6u16);
384
+
385
+ uint8x8x2_t vRes;
386
+ vRes.val[0] = vrshrn_n_u16(vSum_0_4, 8);
387
+ vRes.val[1] = vrshrn_n_u16(vSum_1_5, 8);
388
+ vst2_u8(dst + x, vRes);
389
+ #endif
390
+ }
391
+ break;
392
+ case 3:
393
+ for (; x <= colsn - 8*3; x += 8*3)
394
+ {
395
+ internal::prefetch(lane + x);
396
+
397
+ u16* lidx0 = lane + x - 2*3;
398
+ u16* lidx1 = lane + x - 1*3;
399
+ u16* lidx3 = lane + x + 1*3;
400
+ u16* lidx4 = lane + x + 2*3;
401
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
402
+ __asm__ __volatile__ (
403
+ "vld3.16 {d0, d2, d4}, [%[in0]]! \n\t"
404
+ "vld3.16 {d1, d3, d5}, [%[in0]] \n\t"
405
+ "vld3.16 {d8, d10, d12}, [%[in4]]! \n\t"
406
+ "vld3.16 {d9, d11, d13}, [%[in4]] \n\t"
407
+ "vadd.i16 q0, q4 \n\t"
408
+ "vadd.i16 q1, q5 \n\t"
409
+ "vadd.i16 q2, q6 \n\t"
410
+ "vld3.16 {d16, d18, d20}, [%[in1]]! \n\t"
411
+ "vld3.16 {d17, d19, d21}, [%[in1]] \n\t"
412
+ "vld3.16 {d8, d10, d12}, [%[in3]]! \n\t"
413
+ "vld3.16 {d9, d11, d13}, [%[in3]] \n\t"
414
+ "vadd.i16 q4, q8 \n\t"
415
+ "vadd.i16 q5, q9 \n\t"
416
+ "vadd.i16 q6, q10 \n\t"
417
+ "vld3.16 {d16, d18, d20}, [%[in2]] \n\t"
418
+ "vld3.16 {d17, d19, d21}, [%[in22]] \n\t"
419
+ "vmla.i16 q0, q4, %q[c4] \n\t"
420
+ "vmla.i16 q1, q5, %q[c4] \n\t"
421
+ "vmla.i16 q2, q6, %q[c4] \n\t"
422
+ "vmla.i16 q0, q8, %q[c6] \n\t"
423
+ "vmla.i16 q1, q9, %q[c6] \n\t"
424
+ "vmla.i16 q2, q10, %q[c6] \n\t"
425
+ "vrshrn.u16 d8, q0, #8 \n\t"
426
+ "vrshrn.u16 d9, q1, #8 \n\t"
427
+ "vrshrn.u16 d10, q2, #8 \n\t"
428
+ "vst3.8 {d8-d10}, [%[out]] \n\t"
429
+ : [in0] "=r" (lidx0),
430
+ [in1] "=r" (lidx1),
431
+ [in3] "=r" (lidx3),
432
+ [in4] "=r" (lidx4)
433
+ : [out] "r" (dst + x),
434
+ "0" (lidx0),
435
+ "1" (lidx1),
436
+ "2" (lidx3),
437
+ "3" (lidx4),
438
+ [in2] "r" (lane + x),
439
+ [in22] "r" (lane + x + 4*3),
440
+ [c4] "w" (vc4u16), [c6] "w" (vc6u16)
441
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23"
442
+ );
443
+ #else
444
+ uint16x8x3_t vLane0 = vld3q_u16(lidx0);
445
+ uint16x8x3_t vLane1 = vld3q_u16(lidx1);
446
+ uint16x8x3_t vLane2 = vld3q_u16(lane + x);
447
+ uint16x8x3_t vLane3 = vld3q_u16(lidx3);
448
+ uint16x8x3_t vLane4 = vld3q_u16(lidx4);
449
+
450
+ uint16x8_t vSum_0_4 = vaddq_u16(vLane0.val[0], vLane4.val[0]);
451
+ uint16x8_t vSum_1_5 = vaddq_u16(vLane0.val[1], vLane4.val[1]);
452
+ uint16x8_t vSum_2_6 = vaddq_u16(vLane0.val[2], vLane4.val[2]);
453
+
454
+ uint16x8_t vSum_3_1 = vaddq_u16(vLane3.val[0], vLane1.val[0]);
455
+ uint16x8_t vSum_4_2 = vaddq_u16(vLane3.val[1], vLane1.val[1]);
456
+ uint16x8_t vSum_5_6 = vaddq_u16(vLane3.val[2], vLane1.val[2]);
457
+
458
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vSum_3_1, vc4u16);
459
+ vSum_1_5 = vmlaq_u16(vSum_1_5, vSum_4_2, vc4u16);
460
+ vSum_2_6 = vmlaq_u16(vSum_2_6, vSum_5_6, vc4u16);
461
+
462
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vLane2.val[0], vc6u16);
463
+ vSum_1_5 = vmlaq_u16(vSum_1_5, vLane2.val[1], vc6u16);
464
+ vSum_2_6 = vmlaq_u16(vSum_2_6, vLane2.val[2], vc6u16);
465
+
466
+ uint8x8x3_t vRes;
467
+ vRes.val[0] = vrshrn_n_u16(vSum_0_4, 8);
468
+ vRes.val[1] = vrshrn_n_u16(vSum_1_5, 8);
469
+ vRes.val[2] = vrshrn_n_u16(vSum_2_6, 8);
470
+
471
+ vst3_u8(dst + x, vRes);
472
+ #endif
473
+ }
474
+ break;
475
+ case 4:
476
+ for (; x <= colsn - 8*4; x += 8*4)
477
+ {
478
+ internal::prefetch(lane + x);
479
+ internal::prefetch(lane + x + 16);
480
+
481
+ u16* lidx0 = lane + x - 2*4;
482
+ u16* lidx1 = lane + x - 1*4;
483
+ u16* lidx3 = lane + x + 1*4;
484
+ u16* lidx4 = lane + x + 2*4;
485
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
486
+ __asm__ __volatile__ (
487
+ "vld4.16 {d0, d2, d4, d6}, [%[in0]]! \n\t"
488
+ "vld4.16 {d1, d3, d5, d7}, [%[in0]] \n\t"
489
+ "vld4.16 {d8, d10, d12, d14}, [%[in4]]! \n\t"
490
+ "vld4.16 {d9, d11, d13, d15}, [%[in4]] \n\t"
491
+ "vadd.i16 q0, q4 \n\t"
492
+ "vadd.i16 q1, q5 \n\t"
493
+ "vadd.i16 q2, q6 \n\t"
494
+ "vadd.i16 q3, q7 \n\t"
495
+ "vld4.16 {d16, d18, d20, d22}, [%[in1]]! \n\t"
496
+ "vld4.16 {d17, d19, d21, d23}, [%[in1]] \n\t"
497
+ "vld4.16 {d8, d10, d12, d14}, [%[in3]]! \n\t"
498
+ "vld4.16 {d9, d11, d13, d15}, [%[in3]] \n\t"
499
+ "vadd.i16 q4, q8 \n\t"
500
+ "vadd.i16 q5, q9 \n\t"
501
+ "vadd.i16 q6, q10 \n\t"
502
+ "vadd.i16 q7, q11 \n\t"
503
+ "vld4.16 {d16, d18, d20, d22}, [%[in2],:256] \n\t"
504
+ "vld4.16 {d17, d19, d21, d23}, [%[in22],:256] \n\t"
505
+ "vmla.i16 q0, q4, %q[c4] \n\t"
506
+ "vmla.i16 q1, q5, %q[c4] \n\t"
507
+ "vmla.i16 q2, q6, %q[c4] \n\t"
508
+ "vmla.i16 q3, q7, %q[c4] \n\t"
509
+ "vmla.i16 q0, q8, %q[c6] \n\t"
510
+ "vmla.i16 q1, q9, %q[c6] \n\t"
511
+ "vmla.i16 q2, q10, %q[c6] \n\t"
512
+ "vmla.i16 q3, q11, %q[c6] \n\t"
513
+ "vrshrn.u16 d8, q0, #8 \n\t"
514
+ "vrshrn.u16 d9, q1, #8 \n\t"
515
+ "vrshrn.u16 d10, q2, #8 \n\t"
516
+ "vrshrn.u16 d11, q3, #8 \n\t"
517
+ "vst4.8 {d8-d11}, [%[out]] \n\t"
518
+ : [in0] "=r" (lidx0),
519
+ [in1] "=r" (lidx1),
520
+ [in3] "=r" (lidx3),
521
+ [in4] "=r" (lidx4)
522
+ : [out] "r" (dst + x),
523
+ "0" (lidx0),
524
+ "1" (lidx1),
525
+ "2" (lidx3),
526
+ "3" (lidx4),
527
+ [in2] "r" (lane + x),
528
+ [in22] "r" (lane + x + 4*4),
529
+ [c4] "w" (vc4u16), [c6] "w" (vc6u16)
530
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23"
531
+ );
532
+ #else
533
+ uint16x8x4_t vLane0 = vld4q_u16(lidx0);
534
+ uint16x8x4_t vLane2 = vld4q_u16(lidx4);
535
+ uint16x8x4_t vLane4 = vld4q_u16(lidx1);
536
+ uint16x8x4_t vLane6 = vld4q_u16(lidx3);
537
+ uint16x8x4_t vLane8 = vld4q_u16(lane + x);
538
+
539
+ uint16x8_t vSum_0_4 = vaddq_u16(vLane0.val[0], vLane2.val[0]);
540
+ uint16x8_t vSum_1_5 = vaddq_u16(vLane0.val[1], vLane2.val[1]);
541
+ uint16x8_t vSum_2_6 = vaddq_u16(vLane0.val[2], vLane2.val[2]);
542
+ uint16x8_t vSum_3_7 = vaddq_u16(vLane0.val[3], vLane2.val[3]);
543
+
544
+ uint16x8_t vSum_4_8 = vaddq_u16(vLane4.val[0], vLane6.val[0]);
545
+ uint16x8_t vSum_5_9 = vaddq_u16(vLane4.val[1], vLane6.val[1]);
546
+ uint16x8_t vSum_6_10 = vaddq_u16(vLane4.val[2], vLane6.val[2]);
547
+ uint16x8_t vSum_7_11 = vaddq_u16(vLane4.val[3], vLane6.val[3]);
548
+
549
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vSum_4_8, vc4u16);
550
+ vSum_1_5 = vmlaq_u16(vSum_1_5, vSum_5_9, vc4u16);
551
+ vSum_2_6 = vmlaq_u16(vSum_2_6, vSum_6_10, vc4u16);
552
+ vSum_3_7 = vmlaq_u16(vSum_3_7, vSum_7_11, vc4u16);
553
+
554
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vLane8.val[0], vc6u16);
555
+ vSum_1_5 = vmlaq_u16(vSum_1_5, vLane8.val[1], vc6u16);
556
+ vSum_2_6 = vmlaq_u16(vSum_2_6, vLane8.val[2], vc6u16);
557
+ vSum_3_7 = vmlaq_u16(vSum_3_7, vLane8.val[3], vc6u16);
558
+
559
+ uint8x8x4_t vRes;
560
+ vRes.val[0] = vrshrn_n_u16(vSum_0_4, 8);
561
+ vRes.val[1] = vrshrn_n_u16(vSum_1_5, 8);
562
+ vRes.val[2] = vrshrn_n_u16(vSum_2_6, 8);
563
+ vRes.val[3] = vrshrn_n_u16(vSum_3_7, 8);
564
+
565
+ vst4_u8(dst + x, vRes);
566
+ #endif
567
+ }
568
+ break;
569
+ }
570
+ for (s32 h = 0; h < cn; ++h)
571
+ {
572
+ u16* ln = lane + h;
573
+ u8* dt = dst + h;
574
+ for (size_t k = x; k < colsn; k += cn)
575
+ {
576
+ dt[k] = (u8)((ln[k-2*cn] + ln[k+2*cn]
577
+ + u16(4) * (ln[k-cn] + ln[k+cn])
578
+ + u16(6) * ln[k] + (1 << 7)) >> 8);
579
+ }
580
+ }
581
+ }
582
+ #else
583
+ (void)srcBase;
584
+ (void)srcStride;
585
+ (void)dstBase;
586
+ (void)dstStride;
587
+ (void)borderValue;
588
+ (void)borderMargin;
589
+ #endif
590
+ }
591
+
592
+ void gaussianBlur5x5(const Size2D &size, s32 cn,
593
+ const u16 * srcBase, ptrdiff_t srcStride,
594
+ u16 * dstBase, ptrdiff_t dstStride,
595
+ BORDER_MODE borderType, u16 borderValue, Margin borderMargin)
596
+ {
597
+ internal::assertSupportedConfiguration(isGaussianBlur5x5Supported(size, cn, borderType));
598
+ #ifdef CAROTENE_NEON
599
+ size_t colsn = size.width * cn;
600
+
601
+ std::vector<u16> _tmp;
602
+ u16 *tmp = 0;
603
+ if (borderType == BORDER_MODE_CONSTANT)
604
+ {
605
+ _tmp.assign(colsn + 4*cn, borderValue);
606
+ tmp = &_tmp[cn << 1];
607
+ }
608
+
609
+ ptrdiff_t idx_l1 = internal::borderInterpolate(-1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
610
+ ptrdiff_t idx_l2 = internal::borderInterpolate(-2, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
611
+ ptrdiff_t idx_r1 = internal::borderInterpolate(size.width + 0, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
612
+ ptrdiff_t idx_r2 = internal::borderInterpolate(size.width + 1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
613
+
614
+ //1-line buffer
615
+ std::vector<u32> _buf(cn * (size.width + 4) + 32 / sizeof(u32));
616
+ u32* lane = internal::alignPtr(&_buf[cn << 1], 32);
617
+
618
+ if (borderType == BORDER_MODE_CONSTANT)
619
+ for (s32 k = 0; k < cn; ++k)
620
+ {
621
+ lane[-cn+k] = borderValue;
622
+ lane[-cn-cn+k] = borderValue;
623
+ lane[colsn+k] = borderValue;
624
+ lane[colsn+cn+k] = borderValue;
625
+ }
626
+
627
+ uint16x4_t vc6u16 = vmov_n_u16(6);
628
+ uint32x4_t vc6u32 = vmovq_n_u32(6);
629
+ uint32x4_t vc4u32 = vmovq_n_u32(4);
630
+
631
+ for (size_t i = 0; i < size.height; ++i)
632
+ {
633
+ u16* dst = internal::getRowPtr(dstBase, dstStride, i);
634
+ //vertical convolution
635
+ ptrdiff_t idx_rm2 = internal::borderInterpolate(i - 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
636
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
637
+ ptrdiff_t idx_rp1 = internal::borderInterpolate(i + 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
638
+ ptrdiff_t idx_rp2 = internal::borderInterpolate(i + 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
639
+
640
+ const u16* ln0 = idx_rm2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm2) : tmp;
641
+ const u16* ln1 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
642
+ const u16* ln2 = internal::getRowPtr(srcBase, srcStride, i);
643
+ const u16* ln3 = idx_rp1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp1) : tmp;
644
+ const u16* ln4 = idx_rp2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp2) : tmp;
645
+
646
+ size_t x = 0;
647
+ for (; x <= colsn - 4; x += 4)
648
+ {
649
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, x % 5 - 2));
650
+ uint16x4_t v0 = vld1_u16(ln0+x);
651
+ uint16x4_t v1 = vld1_u16(ln1+x);
652
+ uint16x4_t v2 = vld1_u16(ln2+x);
653
+ uint16x4_t v3 = vld1_u16(ln3+x);
654
+ uint16x4_t v4 = vld1_u16(ln4+x);
655
+
656
+ uint32x4_t v = vaddl_u16(v0, v4);
657
+ uint32x4_t v13 = vaddl_u16(v1, v3);
658
+
659
+ v = vmlal_u16(v, v2, vc6u16);
660
+ v = vmlaq_u32(v, v13, vc4u32);
661
+
662
+ vst1q_u32(lane + x, v);
663
+ }
664
+ for (; x < colsn; ++x)
665
+ lane[x] = ln0[x] + ln4[x] + 4*(ln1[x] + ln3[x]) + 6*ln2[x];
666
+
667
+ //left&right borders
668
+ if (borderType != BORDER_MODE_CONSTANT)
669
+ for (s32 k = 0; k < cn; ++k)
670
+ {
671
+ lane[-cn+k] = lane[idx_l1 + k];
672
+ lane[-cn-cn+k] = lane[idx_l2 + k];
673
+
674
+ lane[colsn+k] = lane[idx_r1 + k];
675
+ lane[colsn+cn+k] = lane[idx_r2 + k];
676
+ }
677
+
678
+ //horizontal convolution
679
+ x = 0;
680
+ for (; x <= colsn - 4; x += 4)
681
+ {
682
+ internal::prefetch(lane + x);
683
+
684
+ uint32x4_t lane0 = vld1q_u32(lane + x - 2);
685
+ uint32x4_t lane4 = vld1q_u32(lane + x + 2);
686
+ uint32x4_t lane1 = vld1q_u32(lane + x - 1);
687
+ uint32x4_t lane3 = vld1q_u32(lane + x + 1);
688
+ uint32x4_t lane2 = vld1q_u32(lane + x + 0);
689
+
690
+ uint32x4_t ln04 = vaddq_u32(lane0, lane4);
691
+ uint32x4_t ln13 = vaddq_u32(lane1, lane3);
692
+
693
+ uint32x4_t ln042 = vmlaq_u32(ln04, lane2, vc6u32);
694
+ uint32x4_t lsw = vmlaq_u32(ln042, ln13, vc4u32);
695
+
696
+ uint16x4_t ls = vrshrn_n_u32(lsw, 8);
697
+
698
+ vst1_u16(dst + x, ls);
699
+ }
700
+ for (s32 h = 0; h < cn; ++h)
701
+ {
702
+ u32* ln = lane + h;
703
+ u16* dt = dst + h;
704
+ for (size_t k = x; k < colsn; k += cn)
705
+ {
706
+ dt[k] = (u16)((ln[k-2*cn] + ln[k+2*cn] + 4*(ln[k-cn] + ln[k+cn]) + 6*ln[k] + (1<<7))>>8);
707
+ }
708
+ }
709
+ }
710
+ #else
711
+ (void)srcBase;
712
+ (void)srcStride;
713
+ (void)dstBase;
714
+ (void)dstStride;
715
+ (void)borderValue;
716
+ (void)borderMargin;
717
+ #endif
718
+ }
719
+
720
+ void gaussianBlur5x5(const Size2D &size, s32 cn,
721
+ const s16 * srcBase, ptrdiff_t srcStride,
722
+ s16 * dstBase, ptrdiff_t dstStride,
723
+ BORDER_MODE borderType, s16 borderValue, Margin borderMargin)
724
+ {
725
+ internal::assertSupportedConfiguration(isGaussianBlur5x5Supported(size, cn, borderType));
726
+ #ifdef CAROTENE_NEON
727
+ size_t colsn = size.width * cn;
728
+
729
+ std::vector<s16> _tmp;
730
+ s16 *tmp = 0;
731
+ if (borderType == BORDER_MODE_CONSTANT)
732
+ {
733
+ _tmp.assign(colsn + 4*cn, borderValue);
734
+ tmp = &_tmp[cn << 1];
735
+ }
736
+
737
+ ptrdiff_t idx_l1 = internal::borderInterpolate(-1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
738
+ ptrdiff_t idx_l2 = internal::borderInterpolate(-2, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
739
+ ptrdiff_t idx_r1 = internal::borderInterpolate(size.width + 0, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
740
+ ptrdiff_t idx_r2 = internal::borderInterpolate(size.width + 1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
741
+
742
+ //1-line buffer
743
+ std::vector<s32> _buf(cn * (size.width + 4) + 32 / sizeof(s32));
744
+ s32* lane = internal::alignPtr(&_buf[cn << 1], 32);
745
+
746
+ if (borderType == BORDER_MODE_CONSTANT)
747
+ for (s32 k = 0; k < cn; ++k)
748
+ {
749
+ lane[-cn+k] = borderValue;
750
+ lane[-cn-cn+k] = borderValue;
751
+ lane[colsn+k] = borderValue;
752
+ lane[colsn+cn+k] = borderValue;
753
+ }
754
+
755
+ int16x4_t vc6s16 = vmov_n_s16(6);
756
+ int32x4_t vc6s32 = vmovq_n_s32(6);
757
+ int32x4_t vc4s32 = vmovq_n_s32(4);
758
+
759
+ for (size_t i = 0; i < size.height; ++i)
760
+ {
761
+ s16* dst = internal::getRowPtr(dstBase, dstStride, i);
762
+ //vertical convolution
763
+ ptrdiff_t idx_rm2 = internal::borderInterpolate(i - 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
764
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
765
+ ptrdiff_t idx_rp1 = internal::borderInterpolate(i + 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
766
+ ptrdiff_t idx_rp2 = internal::borderInterpolate(i + 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
767
+
768
+ const s16* ln0 = idx_rm2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm2) : tmp;
769
+ const s16* ln1 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
770
+ const s16* ln2 = internal::getRowPtr(srcBase, srcStride, i);
771
+ const s16* ln3 = idx_rp1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp1) : tmp;
772
+ const s16* ln4 = idx_rp2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp2) : tmp;
773
+
774
+ size_t x = 0;
775
+ for (; x <= colsn - 4; x += 4)
776
+ {
777
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, x % 5 - 2));
778
+ int16x4_t v0 = vld1_s16(ln0+x);
779
+ int16x4_t v1 = vld1_s16(ln1+x);
780
+ int16x4_t v2 = vld1_s16(ln2+x);
781
+ int16x4_t v3 = vld1_s16(ln3+x);
782
+ int16x4_t v4 = vld1_s16(ln4+x);
783
+
784
+ int32x4_t v = vaddl_s16(v0, v4);
785
+ int32x4_t v13 = vaddl_s16(v1, v3);
786
+
787
+ v = vmlal_s16(v, v2, vc6s16);
788
+ v = vmlaq_s32(v, v13, vc4s32);
789
+
790
+ vst1q_s32(lane + x, v);
791
+ }
792
+ for (; x < colsn; ++x)
793
+ lane[x] = ln0[x] + ln4[x] + 4*(ln1[x] + ln3[x]) + 6*ln2[x];
794
+
795
+ //left&right borders
796
+ if (borderType != BORDER_MODE_CONSTANT)
797
+ for (s32 k = 0; k < cn; ++k)
798
+ {
799
+ lane[-cn+k] = lane[idx_l1 + k];
800
+ lane[-cn-cn+k] = lane[idx_l2 + k];
801
+
802
+ lane[colsn+k] = lane[idx_r1 + k];
803
+ lane[colsn+cn+k] = lane[idx_r2 + k];
804
+ }
805
+
806
+ //horizontal convolution
807
+ x = 0;
808
+ switch(cn)
809
+ {
810
+ case 1:
811
+ case 2:
812
+ case 3:
813
+ for (; x <= colsn - 4; x += 4)
814
+ {
815
+ internal::prefetch(lane + x);
816
+
817
+ int32x4_t lane0 = vld1q_s32(lane + x - 2);
818
+ int32x4_t lane4 = vld1q_s32(lane + x + 2);
819
+ int32x4_t lane1 = vld1q_s32(lane + x - 1);
820
+ int32x4_t lane3 = vld1q_s32(lane + x + 1);
821
+ int32x4_t lane2 = vld1q_s32(lane + x + 0);
822
+
823
+ int32x4_t ln04 = vaddq_s32(lane0, lane4);
824
+ int32x4_t ln13 = vaddq_s32(lane1, lane3);
825
+
826
+ int32x4_t ln042 = vmlaq_s32(ln04, lane2, vc6s32);
827
+ int32x4_t lsw = vmlaq_s32(ln042, ln13, vc4s32);
828
+
829
+ int16x4_t ls = vrshrn_n_s32(lsw, 8);
830
+
831
+ vst1_s16(dst + x, ls);
832
+ }
833
+ break;
834
+ case 4:
835
+ /* for (; x <= colsn - 4*4; x += 4*4)
836
+ {
837
+ internal::prefetch(lane + x);
838
+ internal::prefetch(lane + x + 16);
839
+
840
+ ptrdiff_t* lidx0 = lane + x - 2*4;
841
+ ptrdiff_t* lidx1 = lane + x - 1*4;
842
+ ptrdiff_t* lidx3 = lane + x + 1*4;
843
+ ptrdiff_t* lidx4 = lane + x + 2*4;
844
+
845
+ __asm__ __volatile__ (
846
+ "vld4.32 {d0, d2, d4, d6}, [%[in0]]! \n\t"
847
+ "vld4.32 {d1, d3, d5, d7}, [%[in0]] \n\t"
848
+ "vld4.32 {d8, d10, d12, d14}, [%[in4]]! \n\t"
849
+ "vld4.32 {d9, d11, d13, d15}, [%[in4]] \n\t"
850
+ "vadd.i32 q0, q4 \n\t"
851
+ "vadd.i32 q1, q5 \n\t"
852
+ "vadd.i32 q2, q6 \n\t"
853
+ "vadd.i32 q3, q7 \n\t"
854
+ "vld4.32 {d16, d18, d20, d22}, [%[in1]]! \n\t"
855
+ "vld4.32 {d17, d19, d21, d23}, [%[in1]] \n\t"
856
+ "vld4.32 {d8, d10, d12, d14}, [%[in3]]! \n\t"
857
+ "vld4.32 {d9, d11, d13, d15}, [%[in3]] \n\t"
858
+ "vadd.i32 q4, q8 \n\t"
859
+ "vadd.i32 q5, q9 \n\t"
860
+ "vadd.i32 q6, q10 \n\t"
861
+ "vadd.i32 q7, q11 \n\t"
862
+ "vld4.32 {d16, d18, d20, d22}, [%[in2],:256] \n\t"
863
+ "vld4.32 {d17, d19, d21, d23}, [%[in22],:256] \n\t"
864
+ "vmla.i32 q0, q4, %q[c4] \n\t"
865
+ "vmla.i32 q1, q5, %q[c4] \n\t"
866
+ "vmla.i32 q2, q6, %q[c4] \n\t"
867
+ "vmla.i32 q3, q7, %q[c4] \n\t"
868
+ "vmla.i32 q0, q8, %q[c6] \n\t"
869
+ "vmla.i32 q1, q9, %q[c6] \n\t"
870
+ "vmla.i32 q2, q10, %q[c6] \n\t"
871
+ "vmla.i32 q3, q11, %q[c6] \n\t"
872
+ "vrshrn.i32 d8, q0, #8 \n\t"
873
+ "vrshrn.i32 d9, q1, #8 \n\t"
874
+ "vrshrn.i32 d10, q2, #8 \n\t"
875
+ "vrshrn.i32 d11, q3, #8 \n\t"
876
+ "vst4.16 {d8-d11}, [%[out]] \n\t"
877
+ : [in0] "=r" (lidx0),
878
+ [in1] "=r" (lidx1),
879
+ [in3] "=r" (lidx3),
880
+ [in4] "=r" (lidx4)
881
+ : [out] "r" (dst + x),
882
+ "0" (lidx0),
883
+ "1" (lidx1),
884
+ "2" (lidx3),
885
+ "3" (lidx4),
886
+ [in2] "r" (lane + x),
887
+ [in22] "r" (lane + x + 4*2),
888
+ [c4] "w" (vc4s32), [c6] "w" (vc6s32)
889
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23"
890
+ );
891
+ */
892
+ for (; x <= colsn - 4; x += 4)
893
+ {
894
+ internal::prefetch(lane + x);
895
+
896
+ int32x4_t lane0 = vld1q_s32(lane + x - 2);
897
+ int32x4_t lane4 = vld1q_s32(lane + x + 2);
898
+ int32x4_t lane1 = vld1q_s32(lane + x - 1);
899
+ int32x4_t lane3 = vld1q_s32(lane + x + 1);
900
+ int32x4_t lane2 = vld1q_s32(lane + x + 0);
901
+
902
+ int32x4_t ln04 = vaddq_s32(lane0, lane4);
903
+ int32x4_t ln13 = vaddq_s32(lane1, lane3);
904
+
905
+ int32x4_t ln042 = vmlaq_s32(ln04, lane2, vc6s32);
906
+ int32x4_t lsw = vmlaq_s32(ln042, ln13, vc4s32);
907
+
908
+ int16x4_t ls = vrshrn_n_s32(lsw, 8);
909
+
910
+ vst1_s16(dst + x, ls);
911
+ }
912
+ break;
913
+ }
914
+ for (s32 h = 0; h < cn; ++h)
915
+ {
916
+ s32* ln = lane + h;
917
+ s16* dt = dst + h;
918
+ for (size_t k = x; k < colsn; k += cn)
919
+ {
920
+ dt[k] = (s16)((ln[k-2*cn] + ln[k+2*cn] + 4*(ln[k-cn] + ln[k+cn]) + 6*ln[k] + (1<<7))>>8);
921
+ }
922
+ }
923
+ }
924
+ #else
925
+ (void)srcBase;
926
+ (void)srcStride;
927
+ (void)dstBase;
928
+ (void)dstStride;
929
+ (void)borderValue;
930
+ (void)borderMargin;
931
+ #endif
932
+ }
933
+
934
+ void gaussianBlur5x5(const Size2D &size, s32 cn,
935
+ const s32 * srcBase, ptrdiff_t srcStride,
936
+ s32 * dstBase, ptrdiff_t dstStride,
937
+ BORDER_MODE borderType, s32 borderValue, Margin borderMargin)
938
+ {
939
+ internal::assertSupportedConfiguration(isGaussianBlur5x5Supported(size, cn, borderType));
940
+ #ifdef CAROTENE_NEON
941
+ size_t colsn = size.width * cn;
942
+
943
+ std::vector<s32> _tmp;
944
+ s32 *tmp = 0;
945
+ if (borderType == BORDER_MODE_CONSTANT)
946
+ {
947
+ _tmp.assign(colsn + 4*cn, borderValue);
948
+ tmp = &_tmp[cn << 1];
949
+ }
950
+
951
+ ptrdiff_t idx_l1 = internal::borderInterpolate(-1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
952
+ ptrdiff_t idx_l2 = internal::borderInterpolate(-2, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
953
+ ptrdiff_t idx_r1 = internal::borderInterpolate(size.width + 0, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
954
+ ptrdiff_t idx_r2 = internal::borderInterpolate(size.width + 1, size.width, borderType, borderMargin.left, borderMargin.right) * cn;
955
+
956
+ //1-line buffer
957
+ std::vector<s32> _buf(cn * (size.width + 4) + 32 / sizeof(s32));
958
+ s32* lane = internal::alignPtr(&_buf[cn << 1], 32);
959
+
960
+ if (borderType == BORDER_MODE_CONSTANT)
961
+ for (s32 k = 0; k < cn; ++k)
962
+ {
963
+ lane[-cn+k] = borderValue;
964
+ lane[-cn-cn+k] = borderValue;
965
+ lane[colsn+k] = borderValue;
966
+ lane[colsn+cn+k] = borderValue;
967
+ }
968
+
969
+ int32x4_t vc6s32 = vmovq_n_s32(6);
970
+ int32x4_t vc4s32 = vmovq_n_s32(4);
971
+
972
+ for (size_t i = 0; i < size.height; ++i)
973
+ {
974
+ s32* dst = internal::getRowPtr(dstBase, dstStride, i);
975
+ //vertical convolution
976
+ ptrdiff_t idx_rm2 = internal::borderInterpolate(i - 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
977
+ ptrdiff_t idx_rm1 = internal::borderInterpolate(i - 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
978
+ ptrdiff_t idx_rp1 = internal::borderInterpolate(i + 1, size.height, borderType, borderMargin.top, borderMargin.bottom);
979
+ ptrdiff_t idx_rp2 = internal::borderInterpolate(i + 2, size.height, borderType, borderMargin.top, borderMargin.bottom);
980
+
981
+ const s32* ln0 = idx_rm2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm2) : tmp;
982
+ const s32* ln1 = idx_rm1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rm1) : tmp;
983
+ const s32* ln2 = internal::getRowPtr(srcBase, srcStride, i);
984
+ const s32* ln3 = idx_rp1 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp1) : tmp;
985
+ const s32* ln4 = idx_rp2 >= -(ptrdiff_t)borderMargin.top ? internal::getRowPtr(srcBase, srcStride, idx_rp2) : tmp;
986
+
987
+ size_t x = 0;
988
+ for (; x <= colsn - 4; x += 4)
989
+ {
990
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, x % 5 - 2));
991
+ int32x4_t v0 = vld1q_s32(ln0+x);
992
+ int32x4_t v1 = vld1q_s32(ln1+x);
993
+ int32x4_t v2 = vld1q_s32(ln2+x);
994
+ int32x4_t v3 = vld1q_s32(ln3+x);
995
+ int32x4_t v4 = vld1q_s32(ln4+x);
996
+
997
+ int32x4_t v = vaddq_s32(v0, v4);
998
+ int32x4_t v13 = vaddq_s32(v1, v3);
999
+
1000
+ v = vmlaq_s32(v, v2, vc6s32);
1001
+ v = vmlaq_s32(v, v13, vc4s32);
1002
+
1003
+ vst1q_s32(lane + x, v);
1004
+ }
1005
+ for (; x < colsn; ++x)
1006
+ lane[x] = ln0[x] + ln4[x] + 4*(ln1[x] + ln3[x]) + 6*ln2[x];
1007
+
1008
+ //left&right borders
1009
+ if (borderType != BORDER_MODE_CONSTANT)
1010
+ for (s32 k = 0; k < cn; ++k)
1011
+ {
1012
+ lane[-cn+k] = lane[idx_l1 + k];
1013
+ lane[-cn-cn+k] = lane[idx_l2 + k];
1014
+
1015
+ lane[colsn+k] = lane[idx_r1 + k];
1016
+ lane[colsn+cn+k] = lane[idx_r2 + k];
1017
+ }
1018
+
1019
+ //horizontal convolution
1020
+ x = 0;
1021
+ for (; x <= colsn - 4; x += 4)
1022
+ {
1023
+ internal::prefetch(lane + x);
1024
+
1025
+ int32x4_t lane0 = vld1q_s32(lane + x - 2);
1026
+ int32x4_t lane4 = vld1q_s32(lane + x + 2);
1027
+ int32x4_t lane1 = vld1q_s32(lane + x - 1);
1028
+ int32x4_t lane3 = vld1q_s32(lane + x + 1);
1029
+ int32x4_t lane2 = vld1q_s32(lane + x + 0);
1030
+
1031
+ int32x4_t ln04 = vaddq_s32(lane0, lane4);
1032
+ int32x4_t ln13 = vaddq_s32(lane1, lane3);
1033
+
1034
+ int32x4_t ln042 = vmlaq_s32(ln04, lane2, vc6s32);
1035
+ int32x4_t lsw = vmlaq_s32(ln042, ln13, vc4s32);
1036
+
1037
+ vst1q_s32(dst + x, lsw);
1038
+ }
1039
+ for (s32 h = 0; h < cn; ++h)
1040
+ {
1041
+ s32* ln = lane + h;
1042
+ s32* dt = dst + h;
1043
+ for (size_t k = x; k < colsn; k += cn)
1044
+ {
1045
+ dt[k] = ln[k-2*cn] + ln[k+2*cn] + 4*(ln[k-cn] + ln[k+cn]) + 6*ln[k];
1046
+ }
1047
+ }
1048
+ }
1049
+ #else
1050
+ (void)srcBase;
1051
+ (void)srcStride;
1052
+ (void)dstBase;
1053
+ (void)dstStride;
1054
+ (void)borderValue;
1055
+ (void)borderMargin;
1056
+ #endif
1057
+ }
1058
+
1059
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/in_range.cpp ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include "vtransform.hpp"
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ #ifdef CAROTENE_NEON
47
+
48
+ namespace {
49
+
50
+ inline void vnst(u8* dst, uint8x16_t v1, uint8x16_t v2) { vst1q_u8(dst, v1); vst1q_u8(dst+16, v2); }
51
+ inline void vnst(u8* dst, uint16x8_t v1, uint16x8_t v2) { vst1q_u8(dst, vcombine_u8(vmovn_u16(v1), vmovn_u16(v2))); }
52
+ inline void vnst(u8* dst, uint32x4_t v1, uint32x4_t v2) { vst1_u8(dst, vmovn_u16(vcombine_u16(vmovn_u32(v1), vmovn_u32(v2)))); }
53
+
54
+ template <typename T, int elsize> struct vtail
55
+ {
56
+ static inline void inRange(const T *, const T *, const T *,
57
+ u8 *, size_t &, size_t)
58
+ {
59
+ //do nothing since there couldn't be enough data
60
+ }
61
+ };
62
+ template <typename T> struct vtail<T, 2>
63
+ {
64
+ static inline void inRange(const T * src, const T * rng1, const T * rng2,
65
+ u8 * dst, size_t &x, size_t width)
66
+ {
67
+ typedef typename internal::VecTraits<T>::vec128 vec128;
68
+ typedef typename internal::VecTraits<T>::unsign::vec128 uvec128;
69
+ //There no more than 15 elements in the tail, so we could handle 8 element vector only once
70
+ if( x + 8 < width)
71
+ {
72
+ vec128 vs = internal::vld1q( src + x);
73
+ vec128 vr1 = internal::vld1q(rng1 + x);
74
+ vec128 vr2 = internal::vld1q(rng2 + x);
75
+ uvec128 vd = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));
76
+ internal::vst1(dst + x, internal::vmovn(vd));
77
+ x+=8;
78
+ }
79
+ }
80
+ };
81
+ template <typename T> struct vtail<T, 1>
82
+ {
83
+ static inline void inRange(const T * src, const T * rng1, const T * rng2,
84
+ u8 * dst, size_t &x, size_t width)
85
+ {
86
+ typedef typename internal::VecTraits<T>::vec128 vec128;
87
+ typedef typename internal::VecTraits<T>::unsign::vec128 uvec128;
88
+ typedef typename internal::VecTraits<T>::vec64 vec64;
89
+ typedef typename internal::VecTraits<T>::unsign::vec64 uvec64;
90
+ //There no more than 31 elements in the tail, so we could handle once 16+8 or 16 or 8 elements
91
+ if( x + 16 < width)
92
+ {
93
+ vec128 vs = internal::vld1q( src + x);
94
+ vec128 vr1 = internal::vld1q(rng1 + x);
95
+ vec128 vr2 = internal::vld1q(rng2 + x);
96
+ uvec128 vd = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));
97
+ internal::vst1q(dst + x, vd);
98
+ x+=16;
99
+ }
100
+ if( x + 8 < width)
101
+ {
102
+ vec64 vs = internal::vld1( src + x);
103
+ vec64 vr1 = internal::vld1(rng1 + x);
104
+ vec64 vr2 = internal::vld1(rng2 + x);
105
+ uvec64 vd = internal::vand(internal::vcge(vs, vr1), internal::vcge(vr2, vs));
106
+ internal::vst1(dst + x, vd);
107
+ x+=8;
108
+ }
109
+ }
110
+ };
111
+
112
+ template <typename T>
113
+ inline void inRangeCheck(const Size2D &_size,
114
+ const T * srcBase, ptrdiff_t srcStride,
115
+ const T * rng1Base, ptrdiff_t rng1Stride,
116
+ const T * rng2Base, ptrdiff_t rng2Stride,
117
+ u8 * dstBase, ptrdiff_t dstStride)
118
+ {
119
+ typedef typename internal::VecTraits<T>::vec128 vec128;
120
+ typedef typename internal::VecTraits<T>::unsign::vec128 uvec128;
121
+
122
+ Size2D size(_size);
123
+ if (srcStride == dstStride &&
124
+ srcStride == rng1Stride &&
125
+ srcStride == rng2Stride &&
126
+ srcStride == (ptrdiff_t)(size.width))
127
+ {
128
+ size.width *= size.height;
129
+ size.height = 1;
130
+ }
131
+ const size_t width = size.width & ~( 32/sizeof(T) - 1 );
132
+
133
+ for(size_t j = 0; j < size.height; ++j)
134
+ {
135
+ const T * src = internal::getRowPtr( srcBase, srcStride, j);
136
+ const T * rng1 = internal::getRowPtr(rng1Base, rng1Stride, j);
137
+ const T * rng2 = internal::getRowPtr(rng2Base, rng2Stride, j);
138
+ u8 * dst = internal::getRowPtr( dstBase, dstStride, j);
139
+ size_t i = 0;
140
+ for( ; i < width; i += 32/sizeof(T) )
141
+ {
142
+ internal::prefetch(src + i);
143
+ internal::prefetch(rng1 + i);
144
+ internal::prefetch(rng2 + i);
145
+
146
+ vec128 vs = internal::vld1q( src + i);
147
+ vec128 vr1 = internal::vld1q(rng1 + i);
148
+ vec128 vr2 = internal::vld1q(rng2 + i);
149
+ uvec128 vd1 = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));
150
+ vs = internal::vld1q( src + i + 16/sizeof(T));
151
+ vr1 = internal::vld1q(rng1 + i + 16/sizeof(T));
152
+ vr2 = internal::vld1q(rng2 + i + 16/sizeof(T));
153
+ uvec128 vd2 = internal::vandq(internal::vcgeq(vs, vr1), internal::vcgeq(vr2, vs));
154
+ vnst(dst + i, vd1, vd2);
155
+ }
156
+ vtail<T, sizeof(T)>::inRange(src, rng1, rng2, dst, i, size.width);
157
+ for( ; i < size.width; i++ )
158
+ dst[i] = (u8)(-(rng1[i] <= src[i] && src[i] <= rng2[i]));
159
+ }
160
+ }
161
+
162
+ }
163
+
164
+ #define INRANGEFUNC(T) \
165
+ void inRange(const Size2D &_size, \
166
+ const T * srcBase, ptrdiff_t srcStride, \
167
+ const T * rng1Base, ptrdiff_t rng1Stride, \
168
+ const T * rng2Base, ptrdiff_t rng2Stride, \
169
+ u8 * dstBase, ptrdiff_t dstStride) \
170
+ { \
171
+ internal::assertSupportedConfiguration(); \
172
+ inRangeCheck(_size, srcBase, srcStride, \
173
+ rng1Base, rng1Stride, rng2Base, rng2Stride, \
174
+ dstBase, dstStride); \
175
+ }
176
+ #else
177
+ #define INRANGEFUNC(T) \
178
+ void inRange(const Size2D &, \
179
+ const T *, ptrdiff_t, \
180
+ const T *, ptrdiff_t, \
181
+ const T *, ptrdiff_t, \
182
+ u8 *, ptrdiff_t) \
183
+ { \
184
+ internal::assertSupportedConfiguration(); \
185
+ }
186
+ #endif
187
+
188
+ INRANGEFUNC(u8)
189
+ INRANGEFUNC(s8)
190
+ INRANGEFUNC(u16)
191
+ INRANGEFUNC(s16)
192
+ INRANGEFUNC(s32)
193
+ INRANGEFUNC(f32)
194
+
195
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/integral.cpp ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ namespace CAROTENE_NS {
43
+
44
+ void integral(const Size2D &size,
45
+ const u8 * srcBase, ptrdiff_t srcStride,
46
+ u32 * sumBase, ptrdiff_t sumStride)
47
+ {
48
+ internal::assertSupportedConfiguration();
49
+ #ifdef CAROTENE_NEON
50
+ uint32x4_t v_zero = vmovq_n_u32(0u);
51
+
52
+ // the first iteration
53
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, 0);
54
+ u32 * sum = internal::getRowPtr(sumBase, sumStride, 0);
55
+
56
+ uint32x4_t prev = v_zero;
57
+ size_t j = 0u;
58
+
59
+ for ( ; j + 7 < size.width; j += 8)
60
+ {
61
+ internal::prefetch(sum + j);
62
+ internal::prefetch(src + j);
63
+
64
+ uint8x8_t el8shr0 = vld1_u8(src + j);
65
+ uint8x8_t el8shr1 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 8));
66
+ uint8x8_t el8shr2 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 16));
67
+ uint8x8_t el8shr3 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 24));
68
+
69
+ uint16x8_t el8shr12 = vaddl_u8(el8shr1, el8shr2);
70
+ uint16x8_t el8shr03 = vaddl_u8(el8shr0, el8shr3);
71
+
72
+ uint16x8_t el8 = vaddq_u16(el8shr12, el8shr03);
73
+ uint16x4_t el4h = vadd_u16(vget_low_u16(el8), vget_high_u16(el8));
74
+
75
+ uint32x4_t vsuml = vaddw_u16(prev, vget_low_u16(el8));
76
+ uint32x4_t vsumh = vaddw_u16(prev, el4h);
77
+
78
+ vst1q_u32(sum + j, vsuml);
79
+ vst1q_u32(sum + j + 4, vsumh);
80
+
81
+ prev = vaddw_u16(prev, vdup_lane_u16(el4h, 3));
82
+ }
83
+
84
+ for (u32 v = vgetq_lane_u32(prev, 3); j < size.width; ++j)
85
+ sum[j] = (v += src[j]);
86
+
87
+ // the others
88
+ for (size_t i = 1; i < size.height ; ++i)
89
+ {
90
+ src = internal::getRowPtr(srcBase, srcStride, i);
91
+ u32 * prevSum = internal::getRowPtr(sumBase, sumStride, i - 1);
92
+ sum = internal::getRowPtr(sumBase, sumStride, i);
93
+
94
+ prev = v_zero;
95
+ j = 0u;
96
+
97
+ for ( ; j + 7 < size.width; j += 8)
98
+ {
99
+ internal::prefetch(sum + j);
100
+ internal::prefetch(src + j);
101
+
102
+ uint32x4_t vsuml = vld1q_u32(prevSum + j);
103
+ uint32x4_t vsumh = vld1q_u32(prevSum + j + 4);
104
+
105
+ uint8x8_t el8shr0 = vld1_u8(src + j);
106
+ uint8x8_t el8shr1 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 8));
107
+ uint8x8_t el8shr2 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 16));
108
+ uint8x8_t el8shr3 = vreinterpret_u8_u64(vshl_n_u64(vreinterpret_u64_u8(el8shr0), 24));
109
+
110
+ vsuml = vaddq_u32(vsuml, prev);
111
+ vsumh = vaddq_u32(vsumh, prev);
112
+
113
+ uint16x8_t el8shr12 = vaddl_u8(el8shr1, el8shr2);
114
+ uint16x8_t el8shr03 = vaddl_u8(el8shr0, el8shr3);
115
+
116
+ uint16x8_t el8 = vaddq_u16(el8shr12, el8shr03);
117
+ uint16x4_t el4h = vadd_u16(vget_low_u16(el8), vget_high_u16(el8));
118
+
119
+ vsuml = vaddw_u16(vsuml, vget_low_u16(el8));
120
+ vsumh = vaddw_u16(vsumh, el4h);
121
+
122
+ vst1q_u32(sum + j, vsuml);
123
+ vst1q_u32(sum + j + 4, vsumh);
124
+
125
+ prev = vaddw_u16(prev, vdup_lane_u16(el4h, 3));
126
+ }
127
+
128
+ for (u32 v = vgetq_lane_u32(prev, 3); j < size.width; ++j)
129
+ sum[j] = (v += src[j]) + prevSum[j];
130
+ }
131
+ #else
132
+ (void)size;
133
+ (void)srcBase;
134
+ (void)srcStride;
135
+ (void)sumBase;
136
+ (void)sumStride;
137
+ #endif
138
+ }
139
+
140
+ void sqrIntegral(const Size2D &size,
141
+ const u8 * srcBase, ptrdiff_t srcStride,
142
+ f64 * sqsumBase, ptrdiff_t sqsumStride)
143
+ {
144
+ internal::assertSupportedConfiguration();
145
+ #ifdef CAROTENE_NEON
146
+ uint16x8_t v_zero8 = vmovq_n_u16(0u);
147
+
148
+ // the first iteration
149
+ const u8 * src = internal::getRowPtr(srcBase, srcStride, 0);
150
+ f64 * sqsum = internal::getRowPtr(sqsumBase, sqsumStride, 0);
151
+
152
+ double prev = 0.;
153
+ size_t j = 0u;
154
+
155
+ for ( ; j + 7 < size.width; j += 8)
156
+ {
157
+ internal::prefetch(sqsum + j);
158
+ internal::prefetch(src + j);
159
+
160
+ uint8x8_t vsrc = vld1_u8(src + j);
161
+
162
+ uint16x8_t el8shr0 = vmull_u8(vsrc, vsrc);
163
+ uint16x8_t el8shr1 = vextq_u16(v_zero8, el8shr0, 7);
164
+
165
+ uint32x4_t el8shr01l = vaddl_u16(vget_low_u16(el8shr0), vget_low_u16(el8shr1));
166
+ uint32x4_t el8shr01h = vaddl_u16(vget_high_u16(el8shr0), vget_high_u16(el8shr1));
167
+
168
+ uint32x4_t el4h = vaddq_u32(el8shr01l, el8shr01h);
169
+
170
+ uint32x2_t el2l = vadd_u32(vget_low_u32(el8shr01l), vget_high_u32(el8shr01l));
171
+ uint32x2_t el2hl = vadd_u32(vget_low_u32(el4h), vget_high_u32(el8shr01l));
172
+ uint32x2_t el2hh = vadd_u32(vget_low_u32(el4h), vget_high_u32(el4h));
173
+
174
+ u32 buf[8];
175
+ vst1_u32(buf, vget_low_u32(el8shr01l));
176
+ vst1_u32(buf+2, el2l);
177
+ vst1_u32(buf+4, el2hl);
178
+ vst1_u32(buf+6, el2hh);
179
+ for(u32 k=0; k < 8; k++)
180
+ sqsum[j+k] = prev + buf[k];
181
+ prev += buf[7];
182
+ }
183
+
184
+ for (; j < size.width; ++j)
185
+ sqsum[j] = (prev += src[j]*src[j]);
186
+
187
+ // the others
188
+ for (size_t i = 1; i < size.height ; ++i)
189
+ {
190
+ src = internal::getRowPtr(srcBase, srcStride, i);
191
+ f64 * prevSqSum = internal::getRowPtr(sqsumBase, sqsumStride, i - 1);
192
+ sqsum = internal::getRowPtr(sqsumBase, sqsumStride, i);
193
+
194
+ prev = 0.;
195
+ j = 0u;
196
+
197
+ for ( ; j + 7 < size.width; j += 8)
198
+ {
199
+ internal::prefetch(sqsum + j);
200
+ internal::prefetch(src + j);
201
+
202
+ uint8x8_t vsrc = vld1_u8(src + j);
203
+
204
+ uint16x8_t el8shr0 = vmull_u8(vsrc, vsrc);
205
+ uint16x8_t el8shr1 = vextq_u16(v_zero8, el8shr0, 7);
206
+
207
+ uint32x4_t el8shr01l = vaddl_u16(vget_low_u16(el8shr0), vget_low_u16(el8shr1));
208
+ uint32x4_t el8shr01h = vaddl_u16(vget_high_u16(el8shr0), vget_high_u16(el8shr1));
209
+
210
+ uint32x4_t el4h = vaddq_u32(el8shr01l, el8shr01h);
211
+
212
+ uint32x2_t el2l = vadd_u32(vget_low_u32(el8shr01l), vget_high_u32(el8shr01l));
213
+ uint32x2_t el2hl = vadd_u32(vget_low_u32(el4h), vget_high_u32(el8shr01l));
214
+ uint32x2_t el2hh = vadd_u32(vget_low_u32(el4h), vget_high_u32(el4h));
215
+
216
+ u32 buf[8];
217
+ vst1_u32(buf, vget_low_u32(el8shr01l));
218
+ vst1_u32(buf+2, el2l);
219
+ vst1_u32(buf+4, el2hl);
220
+ vst1_u32(buf+6, el2hh);
221
+ for(u32 k=0; k < 8; k++)
222
+ sqsum[j+k] = prev + prevSqSum[j+k] + buf[k];
223
+ prev += buf[7];
224
+ }
225
+
226
+ for (; j < size.width; ++j)
227
+ sqsum[j] = (prev += src[j]*src[j]) + prevSqSum[j];
228
+ }
229
+ #else
230
+ (void)size;
231
+ (void)srcBase;
232
+ (void)srcStride;
233
+ (void)sqsumBase;
234
+ (void)sqsumStride;
235
+ #endif
236
+ }
237
+
238
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/intrinsics.hpp ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #ifndef CAROTENE_INTRINSICS_HPP
41
+ #define CAROTENE_INTRINSICS_HPP
42
+
43
+ #include <carotene/definitions.hpp>
44
+
45
+ #include <arm_neon.h>
46
+
47
+ namespace CAROTENE_NS { namespace internal {
48
+
49
+ /////////////// Custom NEON intrinsics ///////////////////
50
+
51
+ // calculate reciprocal value
52
+
53
+ inline float32x4_t vrecpq_f32(float32x4_t val)
54
+ {
55
+ float32x4_t reciprocal = vrecpeq_f32(val);
56
+ reciprocal = vmulq_f32(vrecpsq_f32(val, reciprocal), reciprocal);
57
+ reciprocal = vmulq_f32(vrecpsq_f32(val, reciprocal), reciprocal);
58
+ return reciprocal;
59
+ }
60
+
61
+ inline float32x2_t vrecp_f32(float32x2_t val)
62
+ {
63
+ float32x2_t reciprocal = vrecpe_f32(val);
64
+ reciprocal = vmul_f32(vrecps_f32(val, reciprocal), reciprocal);
65
+ reciprocal = vmul_f32(vrecps_f32(val, reciprocal), reciprocal);
66
+ return reciprocal;
67
+ }
68
+
69
+ // caclulate sqrt value
70
+
71
+ inline float32x4_t vrsqrtq_f32(float32x4_t val)
72
+ {
73
+ float32x4_t e = vrsqrteq_f32(val);
74
+ e = vmulq_f32(vrsqrtsq_f32(vmulq_f32(e, e), val), e);
75
+ e = vmulq_f32(vrsqrtsq_f32(vmulq_f32(e, e), val), e);
76
+ return e;
77
+ }
78
+
79
+ inline float32x2_t vrsqrt_f32(float32x2_t val)
80
+ {
81
+ float32x2_t e = vrsqrte_f32(val);
82
+ e = vmul_f32(vrsqrts_f32(vmul_f32(e, e), val), e);
83
+ e = vmul_f32(vrsqrts_f32(vmul_f32(e, e), val), e);
84
+ return e;
85
+ }
86
+
87
+ inline float32x4_t vsqrtq_f32(float32x4_t val)
88
+ {
89
+ return vrecpq_f32(vrsqrtq_f32(val));
90
+ }
91
+
92
+ inline float32x2_t vsqrt_f32(float32x2_t val)
93
+ {
94
+ return vrecp_f32(vrsqrt_f32(val));
95
+ }
96
+
97
+ // table lookup with the table in a 128-bit register
98
+
99
+ inline uint8x8_t vqtbl1_u8 (uint8x16_t a, uint8x8_t b)
100
+ {
101
+ #ifdef __aarch64__
102
+ // AArch64 supports this natively
103
+ return ::vqtbl1_u8(a, b);
104
+ #else
105
+ union { uint8x16_t v; uint8x8x2_t w; } u = { a };
106
+ return vtbl2_u8(u.w, b);
107
+ #endif
108
+ }
109
+
110
+ } }
111
+
112
+ #endif
3rdparty/carotene/src/laplacian.cpp ADDED
@@ -0,0 +1,713 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "saturate_cast.hpp"
42
+
43
+ #include <vector>
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ bool isLaplacian3x3Supported(const Size2D &size, BORDER_MODE border)
48
+ {
49
+ return isSupportedConfiguration() && size.width >= 8 &&
50
+ (border == BORDER_MODE_CONSTANT ||
51
+ border == BORDER_MODE_REPLICATE);
52
+ }
53
+
54
+ void Laplacian3x3(const Size2D &size,
55
+ const u8 * srcBase, ptrdiff_t srcStride,
56
+ u8 * dstBase, ptrdiff_t dstStride,
57
+ BORDER_MODE border, u8 borderValue)
58
+ {
59
+ internal::assertSupportedConfiguration(isLaplacian3x3Supported(size, border));
60
+ #ifdef CAROTENE_NEON
61
+ const uint16x8_t v_border_x3 = vdupq_n_u16(borderValue * 3);
62
+ const uint16x8_t v_zero = vdupq_n_u16(0);
63
+ const uint8x8_t v_border = vdup_n_u8(borderValue);
64
+
65
+ uint8x8_t vsub;
66
+ uint16x8_t tprev = v_zero, tcurr = v_zero, tnext = v_zero;
67
+ uint16x8_t t0 = v_zero, t1 = v_zero, t2 = v_zero;
68
+
69
+ ptrdiff_t width = (ptrdiff_t)size.width, height = (ptrdiff_t)size.height;
70
+
71
+ for (ptrdiff_t y = 0; y < height; ++y)
72
+ {
73
+ const u8 * srow0 = y == 0 && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::max<ptrdiff_t>(y - 1, 0));
74
+ const u8 * srow1 = internal::getRowPtr(srcBase, srcStride, y);
75
+ const u8 * srow2 = y + 1 == height && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::min(y + 1, height - 1));
76
+ u8 * drow = internal::getRowPtr(dstBase, dstStride, y);
77
+
78
+ s16 prevx = 0, currx = 0, nextx = 0;
79
+ ptrdiff_t x = 0;
80
+ const ptrdiff_t bwidth = y + 2 < height ? width : (width - 8);
81
+
82
+ // perform vertical convolution
83
+ for ( ; x <= bwidth; x += 8)
84
+ {
85
+ internal::prefetch(srow0 + x);
86
+ internal::prefetch(srow1 + x);
87
+ internal::prefetch(srow2 + x);
88
+
89
+ uint8x8_t x0 = !srow0 ? v_border : vld1_u8(srow0 + x);
90
+ uint8x8_t x1 = vld1_u8(srow1 + x);
91
+ uint8x8_t x2 = !srow2 ? v_border : vld1_u8(srow2 + x);
92
+
93
+ // calculate values for plain CPU part below if needed
94
+ if (x + 8 >= bwidth)
95
+ {
96
+ ptrdiff_t x3 = x == width ? width - 1 : x;
97
+ ptrdiff_t x4 = border == BORDER_MODE_CONSTANT ? x3 - 1 : std::max<ptrdiff_t>(x3 - 1, 0);
98
+
99
+ if (border == BORDER_MODE_CONSTANT && x4 < 0)
100
+ prevx = borderValue;
101
+ else
102
+ prevx = (srow2 ? srow2[x4] : borderValue) + srow1[x4] + (srow0 ? srow0[x4] : borderValue);
103
+
104
+ currx = (srow2 ? srow2[x3] : borderValue) + srow1[x3] + (srow0 ? srow0[x3] : borderValue);
105
+ }
106
+
107
+ // make shift
108
+ if (x)
109
+ {
110
+ tprev = tcurr;
111
+ tcurr = tnext;
112
+ }
113
+
114
+ // and calculate next value
115
+ tnext = vaddw_u8(vaddl_u8(x0, x1), x2);
116
+
117
+ // make extrapolation for the first elements
118
+ if (!x)
119
+ {
120
+ // make border
121
+ if (border == BORDER_MODE_CONSTANT)
122
+ tcurr = v_border_x3;
123
+ else if (border == BORDER_MODE_REPLICATE)
124
+ tcurr = vdupq_n_u16(vgetq_lane_u16(tnext, 0));
125
+
126
+ vsub = x1;
127
+
128
+ continue;
129
+ }
130
+
131
+ // combine 3 "shifted" vectors
132
+ t0 = vextq_u16(tprev, tcurr, 7);
133
+ t1 = tcurr;
134
+ t2 = vextq_u16(tcurr, tnext, 1);
135
+
136
+ // and add them
137
+ t0 = vqaddq_u16(t0, vqaddq_u16(t1, t2));
138
+
139
+ int16x8_t tt0 = vsubq_s16(vreinterpretq_s16_u16(t0),
140
+ vreinterpretq_s16_u16(vaddw_u8(vshll_n_u8(vsub, 3), vsub)));
141
+ uint8x8_t it0 = vqmovun_s16(tt0);
142
+ vst1_u8(drow + x - 8, it0);
143
+
144
+ vsub = x1;
145
+ }
146
+
147
+ x -= 8;
148
+ if (x == width)
149
+ --x;
150
+
151
+ for ( ; x < width; ++x)
152
+ {
153
+ // make extrapolation for the last elements
154
+ if (x + 1 >= width)
155
+ {
156
+ if (border == BORDER_MODE_CONSTANT)
157
+ nextx = borderValue * 3;
158
+ else if (border == BORDER_MODE_REPLICATE)
159
+ nextx = srow2[x] + srow1[x] + srow0[x];
160
+ }
161
+ else
162
+ {
163
+ nextx = (srow2 ? srow2[x + 1] : borderValue) +
164
+ srow1[x + 1] +
165
+ (srow0 ? srow0[x + 1] : borderValue);
166
+ }
167
+
168
+ s32 val = (prevx + currx + nextx) - 9 * srow1[x];
169
+ drow[x] = internal::saturate_cast<u8>((s32)val);
170
+
171
+ // make shift
172
+ prevx = currx;
173
+ currx = nextx;
174
+ }
175
+ }
176
+ #else
177
+ (void)size;
178
+ (void)srcBase;
179
+ (void)srcStride;
180
+ (void)dstBase;
181
+ (void)dstStride;
182
+ (void)border;
183
+ (void)borderValue;
184
+ #endif
185
+ }
186
+
187
+ bool isLaplacianOpenCVSupported(const Size2D &size, BORDER_MODE border)
188
+ {
189
+ return isSupportedConfiguration() &&
190
+ size.width >= 8 && size.height >= 1 &&
191
+ (border == BORDER_MODE_CONSTANT ||
192
+ border == BORDER_MODE_REFLECT ||
193
+ border == BORDER_MODE_REFLECT101 ||
194
+ border == BORDER_MODE_REPLICATE);
195
+ }
196
+
197
+ void Laplacian1OpenCV(const Size2D &size,
198
+ const u8 * srcBase, ptrdiff_t srcStride,
199
+ s16 * dstBase, ptrdiff_t dstStride,
200
+ BORDER_MODE border, u8 borderValue)
201
+ {
202
+ internal::assertSupportedConfiguration(isLaplacianOpenCVSupported(size, border));
203
+ #ifdef CAROTENE_NEON
204
+ ptrdiff_t rows = size.height, cols = size.width;
205
+
206
+ std::vector<u8> _tmp;
207
+ u8 *tmp = 0;
208
+ if (border == BORDER_MODE_CONSTANT)
209
+ {
210
+ _tmp.assign(cols + 4,borderValue);
211
+ tmp = &_tmp[2];
212
+ }
213
+
214
+ for( ptrdiff_t y = 0; y < rows; y++ )
215
+ {
216
+ const u8* v0 = 0;
217
+ const u8* v1 = internal::getRowPtr(srcBase, srcStride, y);
218
+ const u8* v2 = 0;
219
+ // make border
220
+ if (border == BORDER_MODE_REFLECT101) {
221
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : y+1);
222
+ v2 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 1 ? rows-2 : 0);
223
+ } else if (border == BORDER_MODE_CONSTANT) {
224
+ v0 = y > 0 ? internal::getRowPtr(srcBase, srcStride, y-1) : tmp;
225
+ v2 = y < rows-1 ? internal::getRowPtr(srcBase, srcStride, y+1) : tmp;
226
+ } else {
227
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
228
+ v2 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 0 ? rows-1 : 0);
229
+ }
230
+ s16* drow = internal::getRowPtr(dstBase, dstStride, y);
231
+
232
+ int16x8_t tcurr = vmovq_n_s16(0x0);
233
+ int16x8_t tnext = vmovq_n_s16(0x0);
234
+ int16x8_t t0, t2;
235
+ uint8x8_t xx0 = vmov_n_u8(0x0);
236
+ uint8x8_t xx1 = vmov_n_u8(0x0);
237
+ uint8x8_t xx2 = vmov_n_u8(0x0);
238
+ ptrdiff_t x = 0;
239
+ const ptrdiff_t bcols = y + 2 < rows ? cols : (cols - 8);
240
+ for( ; x <= bcols; x += 8 )
241
+ {
242
+ internal::prefetch(v0 + x);
243
+ internal::prefetch(v1 + x);
244
+ internal::prefetch(v2 + x);
245
+
246
+ uint8x8_t x0 = vld1_u8(v0 + x);
247
+ uint8x8_t x1 = vld1_u8(v1 + x);
248
+ uint8x8_t x2 = vld1_u8(v2 + x);
249
+
250
+ if(x) {
251
+ xx0 = xx1;
252
+ xx1 = xx2;
253
+ } else {
254
+ xx1 = x1;
255
+ // make border
256
+ if (border == BORDER_MODE_REPLICATE || border == BORDER_MODE_REFLECT)
257
+ {
258
+ xx1 = vset_lane_u8(vget_lane_u8(x1, 0),x1, 7);
259
+ }
260
+ else if (border == BORDER_MODE_CONSTANT)
261
+ {
262
+ xx1 = vset_lane_u8(borderValue, x1, 7);
263
+ }
264
+ else if (border == BORDER_MODE_REFLECT101)
265
+ {
266
+ xx1 = vset_lane_u8(vget_lane_u8(x1, 1),x1, 7);
267
+ }
268
+ }
269
+ xx2 = x1;
270
+
271
+ if(x) {
272
+ tcurr = tnext;
273
+ }
274
+ tnext = vsubq_s16(vreinterpretq_s16_u16(vaddl_u8(x0, x2)),
275
+ vreinterpretq_s16_u16(vshll_n_u8(x1, 2)));
276
+
277
+ if(!x) {
278
+ tcurr = tnext;
279
+ continue;
280
+ }
281
+ t0 = vreinterpretq_s16_u16(vmovl_u8(vext_u8(xx0, xx1, 7)));
282
+ t2 = vreinterpretq_s16_u16(vmovl_u8(vext_u8(xx1, xx2, 1)));
283
+ t0 = vaddq_s16(vqaddq_s16(t0, t2), tcurr);
284
+
285
+ vst1q_s16(drow + x - 8, t0);
286
+ }
287
+
288
+ x -= 8;
289
+ if(x == cols){
290
+ x--;
291
+ }
292
+
293
+ for( ; x < cols; x++ )
294
+ {
295
+ s16 nextx;
296
+ s16 prevx;
297
+ // make border
298
+ if (border == BORDER_MODE_REPLICATE || border == BORDER_MODE_REFLECT)
299
+ {
300
+ prevx = x == 0 ? v1[0] : v1[x-1];
301
+ nextx = x == cols-1 ? v1[x] : v1[x+1];
302
+ }
303
+ else if (border == BORDER_MODE_REFLECT101)
304
+ {
305
+ prevx = x == 0 ? v1[1] : v1[x-1];
306
+ nextx = x == cols-1 ? v1[x-1] : v1[x+1];
307
+ }
308
+ else //if (border == BORDER_MODE_CONSTANT)
309
+ {
310
+ prevx = x == 0 ? borderValue : v1[x-1];
311
+ nextx = x == cols-1 ? borderValue : v1[x+1];
312
+ }
313
+ *(drow+x) = prevx + nextx - 4*v1[x] + v0[x] + v2[x];
314
+ }
315
+ }
316
+ #else
317
+ (void)size;
318
+ (void)srcBase;
319
+ (void)srcStride;
320
+ (void)dstBase;
321
+ (void)dstStride;
322
+ (void)border;
323
+ (void)borderValue;
324
+ #endif
325
+ }
326
+
327
+ void Laplacian3OpenCV(const Size2D &size,
328
+ const u8 * srcBase, ptrdiff_t srcStride,
329
+ s16 * dstBase, ptrdiff_t dstStride,
330
+ BORDER_MODE border, u8 borderValue)
331
+ {
332
+ internal::assertSupportedConfiguration(isLaplacianOpenCVSupported(size, border));
333
+ #ifdef CAROTENE_NEON
334
+ ptrdiff_t rows = size.height, cols = size.width;
335
+
336
+ std::vector<u8> _tmp;
337
+ u8 *tmp = 0;
338
+ if (border == BORDER_MODE_CONSTANT)
339
+ {
340
+ _tmp.assign(cols + 4,borderValue);
341
+ tmp = &_tmp[2];
342
+ }
343
+
344
+ for( ptrdiff_t y = 0; y < rows; y++ )
345
+ {
346
+ const u8* v0 = 0;
347
+ const u8* v1 = internal::getRowPtr(srcBase, srcStride, y);
348
+ const u8* v2 = 0;
349
+ // make border
350
+ if (border == BORDER_MODE_REFLECT101) {
351
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : y+1);
352
+ v2 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 1 ? rows-2 : 0);
353
+ } else if (border == BORDER_MODE_CONSTANT) {
354
+ v0 = y > 0 ? internal::getRowPtr(srcBase, srcStride, y-1) : tmp;
355
+ v2 = y < rows-1 ? internal::getRowPtr(srcBase, srcStride, y+1) : tmp;
356
+ } else {
357
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
358
+ v2 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 0 ? rows-1 : 0);
359
+ }
360
+ s16* drow = internal::getRowPtr(dstBase, dstStride, y);
361
+
362
+ int16x8_t tprev = vmovq_n_s16(0x0);
363
+ int16x8_t tcurr = vmovq_n_s16(0x0);
364
+ int16x8_t tnext = vmovq_n_s16(0x0);
365
+ int16x8_t tc = vmovq_n_s16(0x0);
366
+ int16x8_t t0, t2, tcnext;
367
+ ptrdiff_t x = 0;
368
+ const ptrdiff_t bcols = y + 2 < rows ? cols : (cols - 8);
369
+ for( ; x <= bcols; x += 8 )
370
+ {
371
+ internal::prefetch(v0 + x);
372
+ internal::prefetch(v1 + x);
373
+ internal::prefetch(v2 + x);
374
+
375
+ uint8x8_t x0 = vld1_u8(v0 + x);
376
+ uint8x8_t x1 = vld1_u8(v1 + x);
377
+ uint8x8_t x2 = vld1_u8(v2 + x);
378
+ tcnext = vreinterpretq_s16_u16(vshll_n_u8(x1, 2));
379
+
380
+ if(x) {
381
+ tprev = tcurr;
382
+ tcurr = tnext;
383
+ }
384
+ tnext = vreinterpretq_s16_u16(vaddl_u8(x0, x2));
385
+
386
+ if(!x) {
387
+ tcurr = tnext;
388
+ tc = tcnext;
389
+
390
+ // make border
391
+ if (border == BORDER_MODE_REPLICATE || border == BORDER_MODE_REFLECT)
392
+ {
393
+ tcurr = vsetq_lane_s16(vgetq_lane_s16(tcurr, 0),tcurr, 7);
394
+ }
395
+ else if (border == BORDER_MODE_CONSTANT)
396
+ {
397
+ tcurr = vsetq_lane_s16(borderValue, tcurr, 7);
398
+ }
399
+ else if (border == BORDER_MODE_REFLECT101)
400
+ {
401
+ tcurr = vsetq_lane_s16(vgetq_lane_s16(tcurr, 1),tcurr, 7);
402
+ }
403
+ continue;
404
+ }
405
+
406
+ t0 = vextq_s16(tprev, tcurr, 7);
407
+ t2 = vextq_s16(tcurr, tnext, 1);
408
+
409
+ t0 = vsubq_s16(vqaddq_s16(t0, t2), tc);
410
+ tc = tcnext;
411
+
412
+ t0 = vshlq_n_s16(t0, 1);
413
+ vst1q_s16(drow + x - 8, t0);
414
+ }
415
+ x -= 8;
416
+ if(x == cols){
417
+ x--;
418
+ }
419
+
420
+ for( ; x < cols; x++ )
421
+ {
422
+ s16 nextx, nextx2;
423
+ s16 prevx, prevx2;
424
+ // make border
425
+ if (border == BORDER_MODE_REPLICATE || border == BORDER_MODE_REFLECT)
426
+ {
427
+ prevx = x == 0 ? v0[0] : v0[x-1];
428
+ prevx2 = x == 0 ? v2[0] : v2[x-1];
429
+ nextx = x == cols-1 ? v0[x] : v0[x+1];
430
+ nextx2 = x == cols-1 ? v2[x] : v2[x+1];
431
+ }
432
+ else if (border == BORDER_MODE_REFLECT101)
433
+ {
434
+ prevx = x == 0 ? v0[1] : v0[x-1];
435
+ prevx2 = x == 0 ? v2[1] : v2[x-1];
436
+ nextx = x == cols-1 ? v0[x-1] : v0[x+1];
437
+ nextx2 = x == cols-1 ? v2[x-1] : v2[x+1];
438
+ }
439
+ else //if (border == BORDER_MODE_CONSTANT)
440
+ {
441
+ prevx = x == 0 ? borderValue : v0[x-1];
442
+ prevx2 = x == 0 ? borderValue : v2[x-1];
443
+ nextx = x == cols-1 ? borderValue : v0[x+1];
444
+ nextx2 = x == cols-1 ? borderValue : v2[x+1];
445
+ }
446
+ s16 res = prevx + nextx - 4*v1[x] + prevx2 + nextx2;
447
+ *(drow+x) = 2*res;
448
+ }
449
+ }
450
+ #else
451
+ (void)size;
452
+ (void)srcBase;
453
+ (void)srcStride;
454
+ (void)dstBase;
455
+ (void)dstStride;
456
+ (void)border;
457
+ (void)borderValue;
458
+ #endif
459
+ }
460
+
461
+ void Laplacian5OpenCV(const Size2D &size,
462
+ const u8 * srcBase, ptrdiff_t srcStride,
463
+ s16 * dstBase, ptrdiff_t dstStride,
464
+ BORDER_MODE border, u8 borderValue)
465
+ {
466
+ internal::assertSupportedConfiguration(isLaplacianOpenCVSupported(size, border));
467
+ #ifdef CAROTENE_NEON
468
+ ptrdiff_t rows = size.height, cols = size.width;
469
+
470
+ std::vector<u8> _tmp;
471
+ u8 *tmp = 0;
472
+ if (border == BORDER_MODE_CONSTANT)
473
+ {
474
+ _tmp.assign(cols + 4,borderValue);
475
+ tmp = &_tmp[2];
476
+ }
477
+
478
+ for( ptrdiff_t y = 0; y < rows; y++ )
479
+ {
480
+ const u8* v0 = 0;
481
+ const u8* v1 = 0;
482
+ const u8* v2 = internal::getRowPtr(srcBase, srcStride, y);
483
+ const u8* v3 = 0;
484
+ const u8* v4 = 0;
485
+ // make border
486
+ if (border == BORDER_MODE_REPLICATE) {
487
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 1 ? y-2 : 0);
488
+ v1 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
489
+ v3 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 0 ? rows-1 : 0);
490
+ v4 = internal::getRowPtr(srcBase, srcStride, y < rows-2 ? y+2 : rows > 0 ? rows-1 : 0);
491
+ } else if (border == BORDER_MODE_REFLECT) {
492
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 1 ? y-2 : rows > 1 ? 1-y : 0);
493
+ v1 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : 0);
494
+ v3 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 0 ? rows-1 : 0);
495
+ v4 = internal::getRowPtr(srcBase, srcStride, y < rows-2 ? y+2 : rows > 1 ? 2*rows-(y+3) : 0);
496
+ } else if (border == BORDER_MODE_REFLECT101) {
497
+ v0 = internal::getRowPtr(srcBase, srcStride, y > 1 ? y-2 : rows > 2-y ? 2-y : 0); ///check
498
+ v1 = internal::getRowPtr(srcBase, srcStride, y > 0 ? y-1 : rows > 1 ? 1 : 0);
499
+ v3 = internal::getRowPtr(srcBase, srcStride, y < rows-1 ? y+1 : rows > 1 ? rows-2 : 0);
500
+ v4 = internal::getRowPtr(srcBase, srcStride, y < rows-2 ? y+2 : rows > 2 ? 2*rows-(y+4) : 0);///bad if rows=2 y=1 rows - 4 + (2,1)
501
+ } else if (border == BORDER_MODE_CONSTANT) {
502
+ v0 = y > 1 ? internal::getRowPtr(srcBase, srcStride, y-2) : tmp;
503
+ v1 = y > 0 ? internal::getRowPtr(srcBase, srcStride, y-1) : tmp;
504
+ v3 = y < rows-1 ? internal::getRowPtr(srcBase, srcStride, y+1) : tmp;
505
+ v4 = y < rows-2 ? internal::getRowPtr(srcBase, srcStride, y+2) : tmp;
506
+ }
507
+ s16* drow = internal::getRowPtr(dstBase, dstStride, y);
508
+
509
+ int16x8_t tnext, tc, t0;
510
+ int16x8_t tnext2, tnext3;
511
+ int16x8_t tnext1Old, tnext2Old, tnext3Old;
512
+ int16x8_t tnext4OldOldOld, tnext5OldOldOld;
513
+
514
+ int16x8_t tcurr1 = vmovq_n_s16(0x0);
515
+ int16x8_t tnext1 = vmovq_n_s16(0x0);
516
+ int16x8_t tprev1 = vmovq_n_s16(0x0);
517
+ int16x8_t tpprev1 = vmovq_n_s16(0x0);
518
+ int16x8_t tppprev1 = vmovq_n_s16(0x0);
519
+
520
+ int16x8_t tnext4Old = vmovq_n_s16(0x0);
521
+ int16x8_t tnext5Old = vmovq_n_s16(0x0);
522
+ int16x8_t tnext1OldOld = vmovq_n_s16(0x0);
523
+ int16x8_t tnext2OldOld = vmovq_n_s16(0x0);
524
+ int16x8_t tnext3OldOld = vmovq_n_s16(0x0);
525
+ int16x8_t tnext4OldOld = vmovq_n_s16(0x0);
526
+ int16x8_t tnext5OldOld = vmovq_n_s16(0x0);
527
+
528
+ // do vertical convolution
529
+ ptrdiff_t x = 0;
530
+ const ptrdiff_t bcols = y + 3 < rows ? cols : (cols - 8);
531
+ for( ; x <= bcols; x += 8 )
532
+ {
533
+ internal::prefetch(v0 + x);
534
+ internal::prefetch(v1 + x);
535
+ internal::prefetch(v2 + x);
536
+ internal::prefetch(v3 + x);
537
+ internal::prefetch(v4 + x);
538
+
539
+ uint8x8_t x0 = vld1_u8(v0 + x);
540
+ uint8x8_t x1 = vld1_u8(v1 + x);
541
+ uint8x8_t x2 = vld1_u8(v2 + x);
542
+ uint8x8_t x3 = vld1_u8(v3 + x);
543
+ uint8x8_t x4 = vld1_u8(v4 + x);
544
+ if(x) {
545
+ tcurr1 = tnext1;
546
+ }
547
+
548
+ tnext4OldOldOld = tnext4Old;
549
+ tnext5OldOldOld = tnext5Old;
550
+ tnext1Old = tnext1OldOld;
551
+ tnext2Old = tnext2OldOld;
552
+ tnext3Old = tnext3OldOld;
553
+ tnext4Old = tnext4OldOld;
554
+ tnext5Old = tnext5OldOld;
555
+
556
+ tnext3 = vreinterpretq_s16_u16(vaddq_u16(vaddl_u8(x3, x2),vaddl_u8(x2, x1)));
557
+ tnext3 = vshlq_n_s16(tnext3, 1);
558
+
559
+ tc = vreinterpretq_s16_u16(vsubl_u8(x4, x2));
560
+ tnext = vreinterpretq_s16_u16(vsubl_u8(x2, x0));
561
+ tnext2 = vsubq_s16(tc, tnext);
562
+
563
+ tnext1 = vaddq_s16(tnext3, tnext2);
564
+ // tnext1 = x0 + 2*x1 + 2*x2 + 2*x3 + x4
565
+
566
+ tnext2 = vshlq_n_s16(tnext2, 1);
567
+ // tnext2 = 2*x4 - 4*x2 + 2*x0
568
+
569
+ tnext3 = vsubq_s16(tnext2, vshlq_n_s16(tnext3, 1));
570
+ // tnext3 = 2*x0 - 4*x1 - 12*x2 - 4*x3 + 2*x4
571
+
572
+ tnext1OldOld = tnext1;
573
+ tnext2OldOld = tnext2;
574
+ tnext3OldOld = tnext3;
575
+ tnext4OldOld = tnext2;
576
+ tnext5OldOld = tnext1;
577
+
578
+ if(x) {
579
+ tnext1 = vextq_s16(tnext1Old, tnext1, 2);
580
+ tcurr1 = vextq_s16(tnext2Old, tnext2, 1);
581
+ tprev1 = tnext3Old;
582
+
583
+ if(x!=8) {
584
+ tpprev1 = vextq_s16(tnext4OldOldOld, tnext4Old, 7);
585
+ tppprev1 = vextq_s16(tnext5OldOldOld, tnext5Old, 6);
586
+ }
587
+ }
588
+
589
+ if(!x) {
590
+ // make border
591
+ if (border == BORDER_MODE_REPLICATE) {
592
+ tpprev1 = vextq_s16(tnext2, tnext2, 7);
593
+ tpprev1 = vsetq_lane_s16(vgetq_lane_s16(tpprev1, 1),tpprev1, 0);
594
+
595
+ tprev1 = vextq_s16(tnext1, tnext1, 6);
596
+ tprev1 = vsetq_lane_s16(vgetq_lane_s16(tprev1, 2),tprev1, 0);
597
+ tprev1 = vsetq_lane_s16(vgetq_lane_s16(tprev1, 2),tprev1, 1);
598
+ } else if (border == BORDER_MODE_REFLECT) {
599
+ tpprev1 = vextq_s16(tnext2, tnext2, 7);
600
+ tpprev1 = vsetq_lane_s16(vgetq_lane_s16(tpprev1, 1),tpprev1, 0);
601
+
602
+ tprev1 = vextq_s16(tnext1, tnext1, 6);
603
+ tprev1 = vsetq_lane_s16(vgetq_lane_s16(tprev1, 3),tprev1, 0);
604
+ tprev1 = vsetq_lane_s16(vgetq_lane_s16(tprev1, 2),tprev1, 1);
605
+ } else if (border == BORDER_MODE_REFLECT101) {
606
+ tpprev1 = vextq_s16(tnext2, tnext2, 7);
607
+ tpprev1 = vsetq_lane_s16(vgetq_lane_s16(tpprev1, 2),tpprev1, 0);
608
+
609
+ tprev1 = vextq_s16(tnext1, tnext1, 6);
610
+ tprev1 = vsetq_lane_s16(vgetq_lane_s16(tprev1, 3),tprev1, 1);
611
+ tprev1 = vsetq_lane_s16(vgetq_lane_s16(tprev1, 4),tprev1, 0);
612
+ } else if (border == BORDER_MODE_CONSTANT) {
613
+ tpprev1 = vextq_s16(tnext2, tnext2, 7);
614
+ tpprev1 = vsetq_lane_s16(borderValue, tpprev1, 0);
615
+
616
+ tprev1 = vextq_s16(tnext1, tnext1, 6);
617
+ tprev1 = vsetq_lane_s16(borderValue, tprev1, 0);
618
+ tprev1 = vsetq_lane_s16(borderValue, tprev1, 1);
619
+ }
620
+ tppprev1 = tprev1;
621
+ continue;
622
+ }
623
+
624
+ t0 = vaddq_s16(vaddq_s16(vqaddq_s16(tcurr1, tprev1), vqaddq_s16(tpprev1, tppprev1)), tnext1);
625
+ t0 = vaddq_s16(t0, t0);
626
+ vst1q_s16(drow + x - 8, t0);
627
+ }
628
+ x -= 8;
629
+ if(x >= cols - 1)
630
+ x = cols-2;
631
+
632
+ s16 pprevx = 0;
633
+ s16 prevx = 0;
634
+ s16 nextx = 0;
635
+ s16 nnextx = 0;
636
+
637
+ for( ; x < cols; x++ )
638
+ {
639
+ if (x == 0) {
640
+ // make border
641
+ if (border == BORDER_MODE_REPLICATE) {
642
+ pprevx = v0[0] + 2*v1[0] + 2*v2[0] + 2*v3[0] + v4[0];
643
+ prevx = 2*v0[0] - 4*v2[0] + 2*v4[0];
644
+ } else if (border == BORDER_MODE_REFLECT) {
645
+ pprevx = v0[1] + 2*v1[1] + 2*v2[1] + 2*v3[1] + v4[1];
646
+ prevx = 2*v0[0] - 4*v2[0] + 2*v4[0];
647
+ } else if (border == BORDER_MODE_REFLECT101) {
648
+ pprevx = v0[2] + 2*v1[2] + 2*v2[2] + 2*v3[2] + v4[2];
649
+ prevx = 2*v0[1] - 4*v2[1] + 2*v4[1];
650
+ } else if (border == BORDER_MODE_CONSTANT) {
651
+ pprevx = 8 * borderValue;
652
+ prevx = 0;
653
+ }
654
+ } else if (x == 1) {
655
+ // make border
656
+ if (border == BORDER_MODE_REPLICATE || border == BORDER_MODE_REFLECT) {
657
+ pprevx = v0[0] + 2*v1[0] + 2*v2[0] + 2*v3[0] + v4[0];
658
+ } else if (border == BORDER_MODE_REFLECT101) {
659
+ pprevx = v0[1] + 2*v1[1] + 2*v2[1] + 2*v3[1] + v4[1];
660
+ } else if (border == BORDER_MODE_CONSTANT) {
661
+ pprevx = 8 * borderValue;
662
+ }
663
+ prevx = 2*v0[0] - 4*v2[0] + 2*v4[0];
664
+ } else {
665
+ pprevx = v0[x-2] + 2*v1[x-2] + 2*v2[x-2] + 2*v3[x-2] + v4[x-2];
666
+ prevx = 2*v0[x-1] - 4*v2[x-1] + 2*v4[x-1];
667
+ }
668
+ s16 currx = 2*v0[x] - 4*v1[x] - 12*v2[x] - 4*v3[x] + 2*v4[x];
669
+ if (x == cols-1) {
670
+ // make border
671
+ if (border == BORDER_MODE_REPLICATE) {
672
+ nextx = 2*v0[x] - 4*v2[x] + 2*v4[x];
673
+ nnextx = v0[x] + 2*v1[x] + 2*v2[x] + 2*v3[x] + v4[x];
674
+ } else if (border == BORDER_MODE_REFLECT) {
675
+ nextx = 2*v0[x] - 4*v2[x] + 2*v4[x];
676
+ nnextx = v0[x-1] + 2*v1[x-1] + 2*v2[x-1] + 2*v3[x-1] + v4[x-1];
677
+ } else if (border == BORDER_MODE_REFLECT101) {
678
+ nextx = 2*v0[x-1] - 4*v2[x-1] + 2*v4[x-1];
679
+ nnextx = v0[x-2] + 2*v1[x-2] + 2*v2[x-2] + 2*v3[x-2] + v4[x-2];
680
+ } else if (border == BORDER_MODE_CONSTANT) {
681
+ nextx = 0;
682
+ nnextx = 8 * borderValue;
683
+ }
684
+ } else if (x == cols-2) {
685
+ // make border
686
+ if (border == BORDER_MODE_REPLICATE || border == BORDER_MODE_REFLECT) {
687
+ nnextx = v0[x+1] + 2*v1[x+1] + 2*v2[x+1] + 2*v3[x+1] + v4[x+1];
688
+ } else if (border == BORDER_MODE_REFLECT101) {
689
+ nnextx = v0[x] + 2*v1[x] + 2*v2[x] + 2*v3[x] + v4[x];
690
+ } else if (border == BORDER_MODE_CONSTANT) {
691
+ nnextx = 8 * borderValue;
692
+ }
693
+ nextx = 2*v0[x+1] - 4*v2[x+1] + 2*v4[x+1];
694
+ } else {
695
+ nextx = 2*v0[x+1] - 4*v2[x+1] + 2*v4[x+1];
696
+ nnextx = v0[x+2] + 2*v1[x+2] + 2*v2[x+2] + 2*v3[x+2] + v4[x+2];
697
+ }
698
+ s16 res = pprevx + prevx + currx + nextx + nnextx;
699
+ *(drow+x) = 2*res;
700
+ }
701
+ }
702
+ #else
703
+ (void)size;
704
+ (void)srcBase;
705
+ (void)srcStride;
706
+ (void)dstBase;
707
+ (void)dstStride;
708
+ (void)border;
709
+ (void)borderValue;
710
+ #endif
711
+ }
712
+
713
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/magnitude.cpp ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ #include <cmath>
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ #ifdef CAROTENE_NEON
48
+
49
+ namespace {
50
+
51
+ struct Magnitude
52
+ {
53
+ typedef s16 type;
54
+
55
+ void operator() (const int16x8_t & v_src0, const int16x8_t & v_src1,
56
+ int16x8_t & v_dst) const
57
+ {
58
+ int16x4_t v_src0_p = vget_low_s16(v_src0), v_src1_p = vget_low_s16(v_src1);
59
+ float32x4_t v_sqr0 = vaddq_f32(vcvtq_f32_s32(vmull_s16(v_src0_p, v_src0_p)),
60
+ vcvtq_f32_s32(vmull_s16(v_src1_p, v_src1_p)));
61
+ v_src0_p = vget_high_s16(v_src0);
62
+ v_src1_p = vget_high_s16(v_src1);
63
+ float32x4_t v_sqr1 = vaddq_f32(vcvtq_f32_s32(vmull_s16(v_src0_p, v_src0_p)),
64
+ vcvtq_f32_s32(vmull_s16(v_src1_p, v_src1_p)));
65
+
66
+ int32x4_t v_sqrt0 = vcvtq_s32_f32(internal::vsqrtq_f32(v_sqr0));
67
+ int32x4_t v_sqrt1 = vcvtq_s32_f32(internal::vsqrtq_f32(v_sqr1));
68
+
69
+ v_dst = vcombine_s16(vqmovn_s32(v_sqrt0), vqmovn_s32(v_sqrt1));
70
+ }
71
+
72
+ void operator() (const int16x4_t & v_src0, const int16x4_t & v_src1,
73
+ int16x4_t & v_dst) const
74
+ {
75
+ float32x4_t v_tmp = vaddq_f32(vcvtq_f32_s32(vmull_s16(v_src0, v_src0)),
76
+ vcvtq_f32_s32(vmull_s16(v_src1, v_src1)));
77
+ int32x4_t v_sqrt = vcvtq_s32_f32(internal::vsqrtq_f32(v_tmp));
78
+ v_dst = vqmovn_s32(v_sqrt);
79
+ }
80
+
81
+ void operator() (const short * src0, const short * src1, short * dst) const
82
+ {
83
+ f32 src0val = (f32)src0[0], src1val = (f32)src1[0];
84
+ dst[0] = internal::saturate_cast<s16>((s32)sqrtf(src0val * src0val + src1val * src1val));
85
+ }
86
+ };
87
+
88
+ struct MagnitudeF32
89
+ {
90
+ typedef f32 type;
91
+
92
+ void operator() (const float32x4_t & v_src0, const float32x4_t & v_src1,
93
+ float32x4_t & v_dst) const
94
+ {
95
+ v_dst = internal::vsqrtq_f32(vaddq_f32(vmulq_f32(v_src0, v_src0), vmulq_f32(v_src1, v_src1)));
96
+ }
97
+
98
+ void operator() (const float32x2_t & v_src0, const float32x2_t & v_src1,
99
+ float32x2_t & v_dst) const
100
+ {
101
+ v_dst = internal::vsqrt_f32(vadd_f32(vmul_f32(v_src0, v_src0), vmul_f32(v_src1, v_src1)));
102
+ }
103
+
104
+ void operator() (const f32 * src0, const f32 * src1, f32 * dst) const
105
+ {
106
+ dst[0] = sqrtf(src0[0] * src0[0] + src1[0] * src1[0]);
107
+ }
108
+ };
109
+
110
+ } // namespace
111
+
112
+ #endif
113
+
114
+ void magnitude(const Size2D &size,
115
+ const s16 * src0Base, ptrdiff_t src0Stride,
116
+ const s16 * src1Base, ptrdiff_t src1Stride,
117
+ s16 * dstBase, ptrdiff_t dstStride)
118
+ {
119
+ internal::assertSupportedConfiguration();
120
+ #ifdef CAROTENE_NEON
121
+ internal::vtransform(size,
122
+ src0Base, src0Stride,
123
+ src1Base, src1Stride,
124
+ dstBase, dstStride,
125
+ Magnitude());
126
+ #else
127
+ (void)size;
128
+ (void)src0Base;
129
+ (void)src0Stride;
130
+ (void)src1Base;
131
+ (void)src1Stride;
132
+ (void)dstBase;
133
+ (void)dstStride;
134
+ #endif
135
+ }
136
+
137
+ void magnitude(const Size2D &size,
138
+ const f32 * src0Base, ptrdiff_t src0Stride,
139
+ const f32 * src1Base, ptrdiff_t src1Stride,
140
+ f32 * dstBase, ptrdiff_t dstStride)
141
+ {
142
+ internal::assertSupportedConfiguration();
143
+ #ifdef CAROTENE_NEON
144
+ internal::vtransform(size,
145
+ src0Base, src0Stride,
146
+ src1Base, src1Stride,
147
+ dstBase, dstStride,
148
+ MagnitudeF32());
149
+ #else
150
+ (void)size;
151
+ (void)src0Base;
152
+ (void)src0Stride;
153
+ (void)src1Base;
154
+ (void)src1Stride;
155
+ (void)dstBase;
156
+ (void)dstStride;
157
+ #endif
158
+ }
159
+
160
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/meanstddev.cpp ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include <cmath>
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ void meanStdDev(const Size2D &size,
47
+ const u8 * srcBase, ptrdiff_t srcStride,
48
+ f32 * pMean, f32 * pStdDev)
49
+ {
50
+ internal::assertSupportedConfiguration();
51
+ #ifdef CAROTENE_NEON
52
+ f64 fsum = 0.0f, fsqsum = 0.0f;
53
+ sqsum(size, srcBase, srcStride, &fsum, &fsqsum, 1);
54
+
55
+ // calc mean and stddev
56
+ f64 itotal = 1.0 / size.total();
57
+ f64 mean = fsum * itotal;
58
+ f64 stddev = sqrt(std::max(fsqsum * itotal - mean * mean, 0.0));
59
+
60
+ if (pMean)
61
+ *pMean = mean;
62
+ if (pStdDev)
63
+ *pStdDev = stddev;
64
+ #else
65
+ (void)size;
66
+ (void)srcBase;
67
+ (void)srcStride;
68
+ (void)pMean;
69
+ (void)pStdDev;
70
+ #endif
71
+ }
72
+
73
+ void meanStdDev(const Size2D &size,
74
+ const u16 * srcBase, ptrdiff_t srcStride,
75
+ f32 * pMean, f32 * pStdDev)
76
+ {
77
+ internal::assertSupportedConfiguration();
78
+ #ifdef CAROTENE_NEON
79
+ size_t blockSize0 = 1 << 10, roiw4 = size.width & ~3;
80
+ f64 fsum = 0.0f, fsqsum = 0.0f;
81
+
82
+ f32 arsum[8];
83
+ uint32x4_t v_zero = vdupq_n_u32(0u), v_sum;
84
+ float32x4_t v_zero_f = vdupq_n_f32(0.0f), v_sqsum;
85
+
86
+ for (size_t i = 0; i < size.height; ++i)
87
+ {
88
+ const u16 * src = internal::getRowPtr(srcBase, srcStride, i);
89
+ size_t j = 0u;
90
+
91
+ while (j < roiw4)
92
+ {
93
+ size_t blockSize = std::min(roiw4 - j, blockSize0) + j;
94
+ v_sum = v_zero;
95
+ v_sqsum = v_zero_f;
96
+
97
+ for ( ; j + 16 < blockSize ; j += 16)
98
+ {
99
+ internal::prefetch(src + j);
100
+ uint16x8_t v_src0 = vld1q_u16(src + j), v_src1 = vld1q_u16(src + j + 8);
101
+
102
+ // 0
103
+ uint32x4_t v_srclo = vmovl_u16(vget_low_u16(v_src0));
104
+ uint32x4_t v_srchi = vmovl_u16(vget_high_u16(v_src0));
105
+ v_sum = vaddq_u32(v_sum, vaddq_u32(v_srclo, v_srchi));
106
+ float32x4_t v_srclo_f = vcvtq_f32_u32(v_srclo);
107
+ float32x4_t v_srchi_f = vcvtq_f32_u32(v_srchi);
108
+ v_sqsum = vmlaq_f32(v_sqsum, v_srclo_f, v_srclo_f);
109
+ v_sqsum = vmlaq_f32(v_sqsum, v_srchi_f, v_srchi_f);
110
+
111
+ // 1
112
+ v_srclo = vmovl_u16(vget_low_u16(v_src1));
113
+ v_srchi = vmovl_u16(vget_high_u16(v_src1));
114
+ v_sum = vaddq_u32(v_sum, vaddq_u32(v_srclo, v_srchi));
115
+ v_srclo_f = vcvtq_f32_u32(v_srclo);
116
+ v_srchi_f = vcvtq_f32_u32(v_srchi);
117
+ v_sqsum = vmlaq_f32(v_sqsum, v_srclo_f, v_srclo_f);
118
+ v_sqsum = vmlaq_f32(v_sqsum, v_srchi_f, v_srchi_f);
119
+ }
120
+
121
+ for ( ; j < blockSize; j += 4)
122
+ {
123
+ uint32x4_t v_src = vmovl_u16(vld1_u16(src + j));
124
+ float32x4_t v_src_f = vcvtq_f32_u32(v_src);
125
+ v_sum = vaddq_u32(v_sum, v_src);
126
+ v_sqsum = vmlaq_f32(v_sqsum, v_src_f, v_src_f);
127
+ }
128
+
129
+ vst1q_f32(arsum, vcvtq_f32_u32(v_sum));
130
+ vst1q_f32(arsum + 4, v_sqsum);
131
+
132
+ fsum += (f64)arsum[0] + arsum[1] + arsum[2] + arsum[3];
133
+ fsqsum += (f64)arsum[4] + arsum[5] + arsum[6] + arsum[7];
134
+ }
135
+
136
+ // collect a few last elements in the current row
137
+ for ( ; j < size.width; ++j)
138
+ {
139
+ f32 srcval = src[j];
140
+ fsum += srcval;
141
+ fsqsum += srcval * srcval;
142
+ }
143
+ }
144
+
145
+ // calc mean and stddev
146
+ f64 itotal = 1.0 / size.total();
147
+ f64 mean = fsum * itotal;
148
+ f64 stddev = sqrt(std::max(fsqsum * itotal - mean * mean, 0.0));
149
+
150
+ if (pMean)
151
+ *pMean = mean;
152
+ if (pStdDev)
153
+ *pStdDev = stddev;
154
+ #else
155
+ (void)size;
156
+ (void)srcBase;
157
+ (void)srcStride;
158
+ (void)pMean;
159
+ (void)pStdDev;
160
+ #endif
161
+ }
162
+
163
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/median_filter.cpp ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ /*
43
+ * The code here is based on the code in
44
+ * <http://ndevilla.free.fr/median/median/src/optmed.c>, which is in public domain.
45
+ * See also <http://ndevilla.free.fr/median/median/index.html>.
46
+ */
47
+
48
+ namespace CAROTENE_NS {
49
+
50
+ #ifdef CAROTENE_NEON
51
+ namespace {
52
+
53
+ uint8x16_t getLeftReplicate(uint8x16_t r, u32 cn)
54
+ {
55
+ u8 buf[16+8];
56
+ vst1q_u8(buf+cn, r);
57
+ for (u32 i = 0; i < cn; ++i) buf[i] = buf[cn+i];
58
+ return vld1q_u8(buf);
59
+ }
60
+
61
+ uint8x8_t getRightReplicate(uint8x8_t r, u32 cn)
62
+ {
63
+ u8 buf[8+8];
64
+ vst1_u8(buf, r);
65
+ for (u32 i = 0; i < cn; ++i) buf[8+i] = buf[8-cn+i];
66
+ return vld1_u8(buf+cn);
67
+ }
68
+
69
+ } // namespace
70
+
71
+ //o------^-------^-----------------------------o 0
72
+ // | |
73
+ //o--^---v---^---|-------^---------------------o 1
74
+ // | | | |
75
+ //o--v-------v---|-------|-^-------^-------^---o 2
76
+ // | | | | |
77
+ //o------^-------v-----^-|-|-------|-------|---o 3
78
+ // | | | | | |
79
+ //o--^---v---^-----^---|-v-|---^---v---^---v---o 4
80
+ // | | | | | | |
81
+ //o--v-------v---^-|---|---v---|-------|-------o 5
82
+ // | | | | |
83
+ //o------^-------|-|---v-------|-------v-------o 6
84
+ // | | | |
85
+ //o--^---v---^---|-v-----------v---------------o 7
86
+ // | | |
87
+ //o--v-------v---v-----------------------------o 8
88
+
89
+ #define ELT(num, level) v ## num ## _lv ## level
90
+ #define PIX_SORT(a, alvl, b, blvl, newlvl) \
91
+ PIX_MIN(a, alvl, b, blvl, newlvl); \
92
+ PIX_MAX(a, alvl, b, blvl, newlvl);
93
+
94
+ #define SORT9 \
95
+ PIX_SORT(1, 00, 2, 00, 01); \
96
+ PIX_SORT(4, 00, 5, 00, 02); \
97
+ PIX_SORT(7, 00, 8, 00, 03); \
98
+ PIX_SORT(0, 00, 1, 01, 04); \
99
+ PIX_SORT(3, 00, 4, 02, 05); \
100
+ PIX_SORT(6, 00, 7, 03, 06); \
101
+ PIX_SORT(1, 04, 2, 01, 07); \
102
+ PIX_SORT(4, 05, 5, 02, 08); \
103
+ PIX_SORT(7, 06, 8, 03, 09); \
104
+ PIX_MAX (0, 04, 3, 05, 10); \
105
+ PIX_MIN (5, 08, 8, 09, 11); \
106
+ PIX_SORT(4, 08, 7, 09, 12); \
107
+ PIX_MAX (3, 10, 6, 06, 13); \
108
+ PIX_MAX (1, 07, 4, 12, 14); \
109
+ PIX_MIN (2, 07, 5, 11, 15); \
110
+ PIX_MIN (4, 14, 7, 12, 16); \
111
+ PIX_SORT(4, 16, 2, 15, 17); \
112
+ PIX_MAX (6, 13, 4, 17, 18); \
113
+ PIX_MIN (4, 18, 2, 17, 19);
114
+
115
+ #endif
116
+
117
+ bool isMedianFilter3x3Supported(const Size2D &size, u32 numChannels)
118
+ {
119
+ return isSupportedConfiguration() && size.width >= 16 + numChannels && numChannels <= 8;
120
+ }
121
+
122
+ void medianFilter3x3(const Size2D &size, u32 numChannels,
123
+ const u8 *srcBase, ptrdiff_t srcStride,
124
+ const Margin &srcMargin,
125
+ u8 *dstBase, ptrdiff_t dstStride)
126
+ {
127
+ internal::assertSupportedConfiguration(isMedianFilter3x3Supported(size, numChannels));
128
+ #ifdef CAROTENE_NEON
129
+ u32 cn = numChannels;
130
+ size_t colsn = size.width * cn;
131
+
132
+ for (size_t i = 0; i < size.height; ++i) {
133
+ const u8* psrc1 = internal::getRowPtr(srcBase, srcStride, i);
134
+ const u8* psrc0 = i == 0 && srcMargin.top == 0 ? psrc1 : psrc1 - srcStride;
135
+ const u8* psrc2 = i + 1 == size.height && srcMargin.bottom == 0 ? psrc1 : psrc1 + srcStride;
136
+ u8* pdst = internal::getRowPtr(dstBase, dstStride, i);
137
+ size_t j = 0;
138
+
139
+ {
140
+ uint8x16_t v3_lv00 = vld1q_u8(psrc0);
141
+ uint8x16_t v4_lv00 = vld1q_u8(psrc1);
142
+ uint8x16_t v5_lv00 = vld1q_u8(psrc2);
143
+ uint8x16_t v6_lv00 = vld1q_u8(psrc0 + cn);
144
+ uint8x16_t v7_lv00 = vld1q_u8(psrc1 + cn);
145
+ uint8x16_t v8_lv00 = vld1q_u8(psrc2 + cn);
146
+ uint8x16_t v0_lv00 = srcMargin.left > 0 ? vld1q_u8(psrc0 - cn) : getLeftReplicate(v3_lv00, cn);
147
+ uint8x16_t v1_lv00 = srcMargin.left > 0 ? vld1q_u8(psrc1 - cn) : getLeftReplicate(v4_lv00, cn);
148
+ uint8x16_t v2_lv00 = srcMargin.left > 0 ? vld1q_u8(psrc2 - cn) : getLeftReplicate(v5_lv00, cn);
149
+
150
+ goto medianBlur3x3_mainBody;
151
+
152
+ for (; j < colsn - 16; j += 16) {
153
+ internal::prefetch(psrc0 + j);
154
+ internal::prefetch(psrc1 + j);
155
+ internal::prefetch(psrc2 + j);
156
+
157
+ v0_lv00 = vld1q_u8(psrc0 + j - cn);
158
+ v1_lv00 = vld1q_u8(psrc1 + j - cn);
159
+ v2_lv00 = vld1q_u8(psrc2 + j - cn);
160
+ v3_lv00 = vld1q_u8(psrc0 + j);
161
+ v4_lv00 = vld1q_u8(psrc1 + j);
162
+ v5_lv00 = vld1q_u8(psrc2 + j);
163
+ v6_lv00 = vld1q_u8(psrc0 + j + cn);
164
+ v7_lv00 = vld1q_u8(psrc1 + j + cn);
165
+ v8_lv00 = vld1q_u8(psrc2 + j + cn);
166
+
167
+ medianBlur3x3_mainBody:
168
+
169
+ #define PIX_MIN(a, alvl, b, blvl, newlvl) uint8x16_t ELT(a, newlvl) = vminq_u8(ELT(a, alvl), ELT(b, blvl))
170
+ #define PIX_MAX(a, alvl, b, blvl, newlvl) uint8x16_t ELT(b, newlvl) = vmaxq_u8(ELT(a, alvl), ELT(b, blvl))
171
+ SORT9;
172
+ #undef PIX_MAX
173
+ #undef PIX_MIN
174
+
175
+ vst1q_u8(pdst + j, v4_lv19);
176
+ }
177
+ }
178
+
179
+ {
180
+ size_t k = colsn - 8;
181
+ uint8x8_t v0_lv00 = vld1_u8(psrc0 + k - cn);
182
+ uint8x8_t v1_lv00 = vld1_u8(psrc1 + k - cn);
183
+ uint8x8_t v2_lv00 = vld1_u8(psrc2 + k - cn);
184
+ uint8x8_t v3_lv00 = vld1_u8(psrc0 + k);
185
+ uint8x8_t v4_lv00 = vld1_u8(psrc1 + k);
186
+ uint8x8_t v5_lv00 = vld1_u8(psrc2 + k);
187
+ uint8x8_t v6_lv00 = srcMargin.right > 0 ? vld1_u8(psrc0 + k + cn) : getRightReplicate(v3_lv00, cn);
188
+ uint8x8_t v7_lv00 = srcMargin.right > 0 ? vld1_u8(psrc1 + k + cn) : getRightReplicate(v4_lv00, cn);
189
+ uint8x8_t v8_lv00 = srcMargin.right > 0 ? vld1_u8(psrc2 + k + cn) : getRightReplicate(v5_lv00, cn);
190
+
191
+ goto medianBlur3x3_tailBody;
192
+
193
+ for (; k >= j - 8; k -= 8) {
194
+ v0_lv00 = vld1_u8(psrc0 + k - cn);
195
+ v1_lv00 = vld1_u8(psrc1 + k - cn);
196
+ v2_lv00 = vld1_u8(psrc2 + k - cn);
197
+ v3_lv00 = vld1_u8(psrc0 + k);
198
+ v4_lv00 = vld1_u8(psrc1 + k);
199
+ v5_lv00 = vld1_u8(psrc2 + k);
200
+ v6_lv00 = vld1_u8(psrc0 + k + cn);
201
+ v7_lv00 = vld1_u8(psrc1 + k + cn);
202
+ v8_lv00 = vld1_u8(psrc2 + k + cn);
203
+
204
+ medianBlur3x3_tailBody:
205
+
206
+ #define PIX_MIN(a, alvl, b, blvl, newlvl) uint8x8_t ELT(a, newlvl) = vmin_u8(ELT(a, alvl), ELT(b, blvl))
207
+ #define PIX_MAX(a, alvl, b, blvl, newlvl) uint8x8_t ELT(b, newlvl) = vmax_u8(ELT(a, alvl), ELT(b, blvl))
208
+ SORT9;
209
+ #undef PIX_MAX
210
+ #undef PIX_MIN
211
+
212
+ vst1_u8(pdst + k, v4_lv19);
213
+ }
214
+ }
215
+ }
216
+ #else
217
+ (void)size;
218
+ (void)numChannels;
219
+ (void)srcBase;
220
+ (void)srcStride;
221
+ (void)srcMargin;
222
+ (void)dstBase;
223
+ (void)dstStride;
224
+ #endif
225
+ }
226
+
227
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/min_max.cpp ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include <algorithm>
41
+
42
+ #include "common.hpp"
43
+ #include "vtransform.hpp"
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ #ifdef CAROTENE_NEON
48
+
49
+ namespace {
50
+
51
+ template <typename T>
52
+ struct Min
53
+ {
54
+ typedef T type;
55
+
56
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0,
57
+ const typename internal::VecTraits<T>::vec128 & v_src1,
58
+ typename internal::VecTraits<T>::vec128 & v_dst) const
59
+ {
60
+ v_dst = internal::vminq(v_src0, v_src1);
61
+ }
62
+
63
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0,
64
+ const typename internal::VecTraits<T>::vec64 & v_src1,
65
+ typename internal::VecTraits<T>::vec64 & v_dst) const
66
+ {
67
+ v_dst = internal::vmin(v_src0, v_src1);
68
+ }
69
+
70
+ void operator() (const T * src0, const T * src1, T * dst) const
71
+ {
72
+ dst[0] = std::min(src0[0], src1[0]);
73
+ }
74
+ };
75
+
76
+ template <typename T>
77
+ struct Max
78
+ {
79
+ typedef T type;
80
+
81
+ void operator() (const typename internal::VecTraits<T>::vec128 & v_src0,
82
+ const typename internal::VecTraits<T>::vec128 & v_src1,
83
+ typename internal::VecTraits<T>::vec128 & v_dst) const
84
+ {
85
+ v_dst = internal::vmaxq(v_src0, v_src1);
86
+ }
87
+
88
+ void operator() (const typename internal::VecTraits<T>::vec64 & v_src0,
89
+ const typename internal::VecTraits<T>::vec64 & v_src1,
90
+ typename internal::VecTraits<T>::vec64 & v_dst) const
91
+ {
92
+ v_dst = internal::vmax(v_src0, v_src1);
93
+ }
94
+
95
+ void operator() (const T * src0, const T * src1, T * dst) const
96
+ {
97
+ dst[0] = std::max(src0[0], src1[0]);
98
+ }
99
+ };
100
+
101
+ } // namespace
102
+
103
+ #define IMPL_OP(fun, op, type) \
104
+ void fun(const Size2D &size, \
105
+ const type * src0Base, ptrdiff_t src0Stride, \
106
+ const type * src1Base, ptrdiff_t src1Stride, \
107
+ type * dstBase, ptrdiff_t dstStride) \
108
+ { \
109
+ internal::assertSupportedConfiguration(); \
110
+ internal::vtransform(size, \
111
+ src0Base, src0Stride, \
112
+ src1Base, src1Stride, \
113
+ dstBase, dstStride, op<type>()); \
114
+ }
115
+
116
+ #else
117
+
118
+ #define IMPL_OP(fun, op, type) \
119
+ void fun(const Size2D &, \
120
+ const type *, ptrdiff_t, \
121
+ const type *, ptrdiff_t, \
122
+ type *, ptrdiff_t) \
123
+ { \
124
+ internal::assertSupportedConfiguration(); \
125
+ }
126
+
127
+ #endif
128
+
129
+ #define IMPL_MINMAX(type) IMPL_OP(min, Min, type) IMPL_OP(max, Max, type)
130
+
131
+ IMPL_MINMAX(u8)
132
+ IMPL_MINMAX(s8)
133
+ IMPL_MINMAX(u16)
134
+ IMPL_MINMAX(s16)
135
+ IMPL_MINMAX(u32)
136
+ IMPL_MINMAX(s32)
137
+ IMPL_MINMAX(f32)
138
+
139
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/minmaxloc.cpp ADDED
@@ -0,0 +1,1340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ #include <limits>
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ #ifdef CAROTENE_NEON
48
+
49
+ namespace {
50
+
51
+ template <typename T>
52
+ void minMaxVals(const Size2D &size,
53
+ const T * srcBase, ptrdiff_t srcStride,
54
+ T * pMinVal, T * pMaxVal)
55
+ {
56
+ using namespace internal;
57
+
58
+ typedef typename VecTraits<T>::vec128 vec128;
59
+ typedef typename VecTraits<T>::vec64 vec64;
60
+
61
+ u32 step_base = 32 / sizeof(T), step_tail = 8 / sizeof(T);
62
+ size_t roiw_base = size.width >= (step_base - 1) ? size.width - step_base + 1 : 0;
63
+ size_t roiw_tail = size.width >= (step_tail - 1) ? size.width - step_tail + 1 : 0;
64
+
65
+ T maxVal = std::numeric_limits<T>::min();
66
+ T minVal = std::numeric_limits<T>::max();
67
+ vec128 v_min_base = vdupq_n(minVal), v_max_base = vdupq_n(maxVal);
68
+ vec64 v_min_tail = vdup_n(minVal), v_max_tail = vdup_n(maxVal);
69
+
70
+ for (size_t i = 0; i < size.height; ++i)
71
+ {
72
+ const T * src = getRowPtr(srcBase, srcStride, i);
73
+ size_t j = 0;
74
+
75
+ for (; j < roiw_base; j += step_base)
76
+ {
77
+ prefetch(src + j);
78
+ vec128 v_src0 = vld1q(src + j), v_src1 = vld1q(src + j + 16 / sizeof(T));
79
+ v_min_base = vminq(v_min_base, v_src0);
80
+ v_max_base = vmaxq(v_max_base, v_src0);
81
+ v_min_base = vminq(v_min_base, v_src1);
82
+ v_max_base = vmaxq(v_max_base, v_src1);
83
+ }
84
+ for (; j < roiw_tail; j += step_tail)
85
+ {
86
+ vec64 v_src0 = vld1(src + j);
87
+ v_min_tail = vmin(v_min_tail, v_src0);
88
+ v_max_tail = vmax(v_max_tail, v_src0);
89
+ }
90
+
91
+ for (; j < size.width; j++)
92
+ {
93
+ T srcval = src[j];
94
+ minVal = std::min(srcval, minVal);
95
+ maxVal = std::max(srcval, maxVal);
96
+ }
97
+ }
98
+
99
+ // collect min & max values
100
+ T ar[16 / sizeof(T)];
101
+ vst1q(ar, vcombine(vmin(v_min_tail, vmin(vget_low(v_min_base), vget_high(v_min_base))),
102
+ vmax(v_max_tail, vmax(vget_low(v_max_base), vget_high(v_max_base)))));
103
+
104
+ for (size_t x = 0; x < 8u / sizeof(T); ++x)
105
+ {
106
+ minVal = std::min(minVal, ar[x]);
107
+ maxVal = std::max(maxVal, ar[x + 8 / sizeof(T)]);
108
+ }
109
+
110
+ if (pMaxVal)
111
+ *pMaxVal = maxVal;
112
+ if (pMinVal)
113
+ *pMinVal = minVal;
114
+ }
115
+
116
+ } // namespace
117
+
118
+ #endif
119
+
120
+ void minMaxVals(const Size2D &size,
121
+ const u8 * srcBase, ptrdiff_t srcStride,
122
+ u8 * pMinVal, u8 * pMaxVal)
123
+ {
124
+ internal::assertSupportedConfiguration();
125
+ #ifdef CAROTENE_NEON
126
+ minMaxVals<u8>(size,
127
+ srcBase, srcStride,
128
+ pMinVal, pMaxVal);
129
+ #else
130
+ (void)size;
131
+ (void)srcBase;
132
+ (void)srcStride;
133
+ (void)pMinVal;
134
+ (void)pMaxVal;
135
+ #endif
136
+ }
137
+
138
+ void minMaxVals(const Size2D &size,
139
+ const s16 * srcBase, ptrdiff_t srcStride,
140
+ s16 * pMinVal, s16 * pMaxVal)
141
+ {
142
+ internal::assertSupportedConfiguration();
143
+ #ifdef CAROTENE_NEON
144
+ minMaxVals<s16>(size,
145
+ srcBase, srcStride,
146
+ pMinVal, pMaxVal);
147
+ #else
148
+ (void)size;
149
+ (void)srcBase;
150
+ (void)srcStride;
151
+ (void)pMinVal;
152
+ (void)pMaxVal;
153
+ #endif
154
+ }
155
+
156
+ void minMaxVals(const Size2D &size,
157
+ const u16 * srcBase, ptrdiff_t srcStride,
158
+ u16 * pMinVal, u16 * pMaxVal)
159
+ {
160
+ internal::assertSupportedConfiguration();
161
+ #ifdef CAROTENE_NEON
162
+ minMaxVals<u16>(size,
163
+ srcBase, srcStride,
164
+ pMinVal, pMaxVal);
165
+ #else
166
+ (void)size;
167
+ (void)srcBase;
168
+ (void)srcStride;
169
+ (void)pMinVal;
170
+ (void)pMaxVal;
171
+ #endif
172
+ }
173
+
174
+ void minMaxVals(const Size2D &size,
175
+ const s32 * srcBase, ptrdiff_t srcStride,
176
+ s32 * pMinVal, s32 * pMaxVal)
177
+ {
178
+ internal::assertSupportedConfiguration();
179
+ #ifdef CAROTENE_NEON
180
+ minMaxVals<s32>(size,
181
+ srcBase, srcStride,
182
+ pMinVal, pMaxVal);
183
+ #else
184
+ (void)size;
185
+ (void)srcBase;
186
+ (void)srcStride;
187
+ (void)pMinVal;
188
+ (void)pMaxVal;
189
+ #endif
190
+ }
191
+
192
+ void minMaxVals(const Size2D &size,
193
+ const u32 * srcBase, ptrdiff_t srcStride,
194
+ u32 * pMinVal, u32 * pMaxVal)
195
+ {
196
+ internal::assertSupportedConfiguration();
197
+ #ifdef CAROTENE_NEON
198
+ minMaxVals<u32>(size,
199
+ srcBase, srcStride,
200
+ pMinVal, pMaxVal);
201
+ #else
202
+ (void)size;
203
+ (void)srcBase;
204
+ (void)srcStride;
205
+ (void)pMinVal;
206
+ (void)pMaxVal;
207
+ #endif
208
+ }
209
+
210
+ void minMaxLoc(const Size2D &size,
211
+ const f32 * srcBase, ptrdiff_t srcStride,
212
+ f32 &minVal, size_t &minCol, size_t &minRow,
213
+ f32 &maxVal, size_t &maxCol, size_t &maxRow)
214
+ {
215
+ internal::assertSupportedConfiguration();
216
+ #ifdef CAROTENE_NEON
217
+ minVal = srcBase[0];
218
+ minCol = 0;
219
+ minRow = 0;
220
+ maxVal = srcBase[0];
221
+ maxCol = 0;
222
+ maxRow = 0;
223
+ for(size_t l = 0, i = 0; l < size.height; ++l, i = 0)
224
+ {
225
+ const f32 * src = internal::getRowPtr( srcBase, srcStride, l);
226
+ if (size.width >= 16)
227
+ {
228
+ u32 tmp0123[4] = { 0, 1, 2, 3 };
229
+ uint32x4_t c4 = vdupq_n_u32(4);
230
+
231
+ #if SIZE_MAX > UINT32_MAX
232
+ size_t boundAll = size.width - (4 - 1);
233
+ for(size_t b = 0; i < boundAll; b = i)
234
+ {
235
+ size_t bound = std::min<size_t>(boundAll, b + 0xffffFFFC);
236
+ #else
237
+ {
238
+ size_t bound = size.width - (4 - 1);
239
+ #endif
240
+ uint32x4_t lineIdxOffset = vld1q_u32(tmp0123);
241
+ float32x4_t n_min = vdupq_n_f32(minVal);
242
+ uint32x4_t n_minIdx = vdupq_n_u32(0xffffFFFC);
243
+ float32x4_t n_max = vdupq_n_f32(maxVal);
244
+ uint32x4_t n_maxIdx = vdupq_n_u32(0xffffFFFC);
245
+
246
+ for(; i < bound; i+=4)
247
+ {
248
+ internal::prefetch(src + i);
249
+ float32x4_t line = vld1q_f32(src + i);
250
+
251
+ uint32x4_t minmask = vcltq_f32(line, n_min);
252
+ uint32x4_t maxmask = vcgtq_f32(line, n_max);
253
+
254
+ n_min = vbslq_f32(minmask, line, n_min);
255
+ n_minIdx = vbslq_u32(minmask, lineIdxOffset, n_minIdx);
256
+ n_max = vbslq_f32(maxmask, line, n_max);
257
+ n_maxIdx = vbslq_u32(maxmask, lineIdxOffset, n_maxIdx);
258
+
259
+ // idx[] +=4
260
+ lineIdxOffset = vaddq_u32(lineIdxOffset, c4);
261
+ }
262
+
263
+ f32 fmin[4], fmax[4];
264
+ u32 fminIdx[4], fmaxIdx[4];
265
+
266
+ vst1q_f32(fmin, n_min);
267
+ vst1q_f32(fmax, n_max);
268
+
269
+ vst1q_u32(fminIdx, n_minIdx);
270
+ vst1q_u32(fmaxIdx, n_maxIdx);
271
+
272
+ size_t minIdx = fminIdx[0];
273
+ size_t maxIdx = fmaxIdx[0];
274
+ minVal = fmin[0];
275
+ maxVal = fmax[0];
276
+
277
+ for (s32 j = 1; j < 4; ++j)
278
+ {
279
+ f32 minval = fmin[j];
280
+ f32 maxval = fmax[j];
281
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
282
+ {
283
+ minIdx = fminIdx[j];
284
+ minVal = minval;
285
+ }
286
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
287
+ {
288
+ maxIdx = fmaxIdx[j];
289
+ maxVal = maxval;
290
+ }
291
+ }
292
+ if(minIdx < 0xffffFFFC)
293
+ {
294
+ #if SIZE_MAX > UINT32_MAX
295
+ minCol = b + minIdx;
296
+ #else
297
+ minCol = minIdx;
298
+ #endif
299
+ minRow = l;
300
+ }
301
+ if(maxIdx < 0xffffFFFC)
302
+ {
303
+ #if SIZE_MAX > UINT32_MAX
304
+ maxCol = b + maxIdx;
305
+ #else
306
+ maxCol = maxIdx;
307
+ #endif
308
+ maxRow = l;
309
+ }
310
+ }
311
+ }
312
+ for(; i < size.width; ++i )
313
+ {
314
+ float val = src[i];
315
+ if( val < minVal )
316
+ {
317
+ minVal = val;
318
+ minCol = i;
319
+ minRow = l;
320
+ }
321
+ else if( val > maxVal )
322
+ {
323
+ maxVal = val;
324
+ maxCol = i;
325
+ maxRow = l;
326
+ }
327
+ }
328
+ }
329
+ #else
330
+ (void)size;
331
+ (void)srcBase;
332
+ (void)srcStride;
333
+ (void)minVal;
334
+ (void)minCol;
335
+ (void)minRow;
336
+ (void)maxVal;
337
+ (void)maxCol;
338
+ (void)maxRow;
339
+ #endif
340
+ }
341
+
342
+ void minMaxLoc(const Size2D &size,
343
+ const f32 * srcBase, ptrdiff_t srcStride,
344
+ const u8 * maskBase, ptrdiff_t maskStride,
345
+ f32 &minVal, size_t &minCol, size_t &minRow,
346
+ f32 &maxVal, size_t &maxCol, size_t &maxRow)
347
+ {
348
+ internal::assertSupportedConfiguration();
349
+ #ifdef CAROTENE_NEON
350
+ minVal = std::numeric_limits<f32>::max();
351
+ minCol = size.width;
352
+ minRow = size.height;
353
+ maxVal = -std::numeric_limits<f32>::max();
354
+ maxCol = size.width;
355
+ maxRow = size.height;
356
+ for(size_t l = 0, i = 0; l < size.height; ++l, i = 0)
357
+ {
358
+ const f32 * src = internal::getRowPtr( srcBase, srcStride, l);
359
+ const u8 * mask = internal::getRowPtr( maskBase, maskStride, l);
360
+ if (size.width >= 16)
361
+ {
362
+ u32 tmp0123[4] = { 0, 1, 2, 3 };
363
+ uint32x4_t uOne = vdupq_n_u32(1);
364
+ uint32x4_t c4 = vdupq_n_u32(4);
365
+
366
+ #if SIZE_MAX > UINT32_MAX
367
+ size_t boundAll = size.width - (4 - 1);
368
+ for(size_t b = 0; i < boundAll; b = i)
369
+ {
370
+ size_t bound = std::min<size_t>(boundAll, b + 0xffffFFFC);
371
+ #else
372
+ {
373
+ size_t bound = size.width - (4 - 1);
374
+ #endif
375
+ uint32x4_t lineIdxOffset = vld1q_u32(tmp0123);
376
+ float32x4_t n_min = vdupq_n_f32(minVal);
377
+ uint32x4_t n_minIdx = vdupq_n_u32(0xffffFFFC);
378
+ float32x4_t n_max = vdupq_n_f32(maxVal);
379
+ uint32x4_t n_maxIdx = vdupq_n_u32(0xffffFFFC);
380
+
381
+ for(; i < bound; i+=4)
382
+ {
383
+ internal::prefetch(src + i);
384
+ internal::prefetch(mask + i);
385
+ float32x4_t line = vld1q_f32(src + i);
386
+ uint8x8_t maskLine = vld1_u8(mask + i);
387
+
388
+ uint32x4_t maskLine4 = vmovl_u16(vget_low_u16(vmovl_u8(maskLine)));
389
+ maskLine4 = vcgeq_u32(maskLine4, uOne);
390
+
391
+ uint32x4_t minmask = vcltq_f32(line, n_min);
392
+ uint32x4_t maxmask = vcgtq_f32(line, n_max);
393
+
394
+ minmask = vandq_u32(minmask, maskLine4);
395
+ maxmask = vandq_u32(maxmask, maskLine4);
396
+
397
+ n_min = vbslq_f32(minmask, line, n_min);
398
+ n_minIdx = vbslq_u32(minmask, lineIdxOffset, n_minIdx);
399
+ n_max = vbslq_f32(maxmask, line, n_max);
400
+ n_maxIdx = vbslq_u32(maxmask, lineIdxOffset, n_maxIdx);
401
+
402
+ // idx[] +=4
403
+ lineIdxOffset = vaddq_u32(lineIdxOffset, c4);
404
+ }
405
+
406
+ f32 fmin[4], fmax[4];
407
+ u32 fminIdx[4], fmaxIdx[4];
408
+
409
+ vst1q_f32(fmin, n_min);
410
+ vst1q_f32(fmax, n_max);
411
+
412
+ vst1q_u32(fminIdx, n_minIdx);
413
+ vst1q_u32(fmaxIdx, n_maxIdx);
414
+
415
+ size_t minIdx = fminIdx[0];
416
+ size_t maxIdx = fmaxIdx[0];
417
+ minVal = fmin[0];
418
+ maxVal = fmax[0];
419
+
420
+ for (s32 j = 1; j < 4; ++j)
421
+ {
422
+ f32 minval = fmin[j];
423
+ f32 maxval = fmax[j];
424
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
425
+ {
426
+ minIdx = fminIdx[j];
427
+ minVal = minval;
428
+ }
429
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
430
+ {
431
+ maxIdx = fmaxIdx[j];
432
+ maxVal = maxval;
433
+ }
434
+ }
435
+ if(minIdx < 0xffffFFFC)
436
+ {
437
+ #if SIZE_MAX > UINT32_MAX
438
+ minCol = b + minIdx;
439
+ #else
440
+ minCol = minIdx;
441
+ #endif
442
+ minRow = l;
443
+ }
444
+ if(maxIdx < 0xffffFFFC)
445
+ {
446
+ #if SIZE_MAX > UINT32_MAX
447
+ maxCol = b + maxIdx;
448
+ #else
449
+ maxCol = maxIdx;
450
+ #endif
451
+ maxRow = l;
452
+ }
453
+ }
454
+ }
455
+ for(; i < size.width; i++ )
456
+ {
457
+ if (!mask[i])
458
+ continue;
459
+ f32 val = src[i];
460
+ if( val < minVal )
461
+ {
462
+ minVal = val;
463
+ minCol = i;
464
+ minRow = l;
465
+ }
466
+ if( val > maxVal )
467
+ {
468
+ maxVal = val;
469
+ maxCol = i;
470
+ maxRow = l;
471
+ }
472
+ }
473
+ }
474
+ #else
475
+ (void)size;
476
+ (void)srcBase;
477
+ (void)srcStride;
478
+ (void)maskBase;
479
+ (void)maskStride;
480
+ (void)minVal;
481
+ (void)minCol;
482
+ (void)minRow;
483
+ (void)maxVal;
484
+ (void)maxCol;
485
+ (void)maxRow;
486
+ #endif
487
+ }
488
+
489
+ void minMaxLoc(const Size2D &size,
490
+ const s32 * srcBase, ptrdiff_t srcStride,
491
+ s32 &minVal, size_t &minCol, size_t &minRow,
492
+ s32 &maxVal, size_t &maxCol, size_t &maxRow)
493
+ {
494
+ internal::assertSupportedConfiguration();
495
+ #ifdef CAROTENE_NEON
496
+ minVal = srcBase[0];
497
+ minCol = 0;
498
+ minRow = 0;
499
+ maxVal = srcBase[0];
500
+ maxCol = 0;
501
+ maxRow = 0;
502
+ for(size_t l = 0, i = 0; l < size.height; ++l, i = 0)
503
+ {
504
+ const s32 * src = internal::getRowPtr( srcBase, srcStride, l);
505
+ if (size.width >= 16)
506
+ {
507
+ u32 tmp0123[4] = { 0, 1, 2, 3 };
508
+ uint32x4_t c4 = vdupq_n_u32(4);
509
+
510
+ #if SIZE_MAX > UINT32_MAX
511
+ size_t boundAll = size.width - (4 - 1);
512
+ for(size_t b = 0; i < boundAll; b = i)
513
+ {
514
+ size_t bound = std::min<size_t>(boundAll, b + 0xffffFFFC);
515
+ #else
516
+ {
517
+ size_t bound = size.width - (4 - 1);
518
+ #endif
519
+ uint32x4_t lineIdxOffset = vld1q_u32(tmp0123);
520
+ int32x4_t n_min = vdupq_n_s32(minVal);
521
+ uint32x4_t n_minIdx = vdupq_n_u32(0xffffFFFC);
522
+ int32x4_t n_max = vdupq_n_s32(maxVal);
523
+ uint32x4_t n_maxIdx = vdupq_n_u32(0xffffFFFC);
524
+
525
+ for(; i < bound; i+=4 )
526
+ {
527
+ internal::prefetch(src + i);
528
+ int32x4_t line = vld1q_s32(src + i);
529
+
530
+ uint32x4_t minmask = vcltq_s32(line, n_min);
531
+ uint32x4_t maxmask = vcgtq_s32(line, n_max);
532
+
533
+ n_min = vbslq_s32(minmask, line, n_min);
534
+ n_minIdx = vbslq_u32(minmask, lineIdxOffset, n_minIdx);
535
+ n_max = vbslq_s32(maxmask, line, n_max);
536
+ n_maxIdx = vbslq_u32(maxmask, lineIdxOffset, n_maxIdx);
537
+
538
+ // idx[] +=4
539
+ lineIdxOffset = vaddq_u32(lineIdxOffset, c4);
540
+ }
541
+
542
+ s32 fmin[4], fmax[4];
543
+ u32 fminIdx[4], fmaxIdx[4];
544
+
545
+ vst1q_s32(fmin, n_min);
546
+ vst1q_s32(fmax, n_max);
547
+
548
+ vst1q_u32(fminIdx, n_minIdx);
549
+ vst1q_u32(fmaxIdx, n_maxIdx);
550
+
551
+ size_t minIdx = fminIdx[0];
552
+ size_t maxIdx = fmaxIdx[0];
553
+ minVal = fmin[0];
554
+ maxVal = fmax[0];
555
+
556
+ for (s32 j = 1; j < 4; ++j)
557
+ {
558
+ s32 minval = fmin[j];
559
+ s32 maxval = fmax[j];
560
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
561
+ {
562
+ minIdx = fminIdx[j];
563
+ minVal = minval;
564
+ }
565
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
566
+ {
567
+ maxIdx = fmaxIdx[j];
568
+ maxVal = maxval;
569
+ }
570
+ }
571
+ if(minIdx < 0xffffFFFC)
572
+ {
573
+ #if SIZE_MAX > UINT32_MAX
574
+ minCol = b + minIdx;
575
+ #else
576
+ minCol = minIdx;
577
+ #endif
578
+ minRow = l;
579
+ }
580
+ if(maxIdx < 0xffffFFFC)
581
+ {
582
+ #if SIZE_MAX > UINT32_MAX
583
+ maxCol = b + maxIdx;
584
+ #else
585
+ maxCol = maxIdx;
586
+ #endif
587
+ maxRow = l;
588
+ }
589
+ }
590
+ }
591
+ for(; i < size.width; ++i )
592
+ {
593
+ s32 val = src[i];
594
+ if( val < minVal )
595
+ {
596
+ minVal = val;
597
+ minCol = i;
598
+ minRow = l;
599
+ }
600
+ else if( val > maxVal )
601
+ {
602
+ maxVal = val;
603
+ maxCol = i;
604
+ maxRow = l;
605
+ }
606
+ }
607
+ }
608
+ #else
609
+ (void)size;
610
+ (void)srcBase;
611
+ (void)srcStride;
612
+ (void)minVal;
613
+ (void)minCol;
614
+ (void)minRow;
615
+ (void)maxVal;
616
+ (void)maxCol;
617
+ (void)maxRow;
618
+ #endif
619
+ }
620
+
621
+ void minMaxLoc(const Size2D &size,
622
+ const s16 * srcBase, ptrdiff_t srcStride,
623
+ s16 &minVal, size_t &minCol, size_t &minRow,
624
+ s16 &maxVal, size_t &maxCol, size_t &maxRow)
625
+ {
626
+ internal::assertSupportedConfiguration();
627
+ #ifdef CAROTENE_NEON
628
+ minVal = srcBase[0];
629
+ minCol = 0;
630
+ minRow = 0;
631
+ maxVal = srcBase[0];
632
+ maxCol = 0;
633
+ maxRow = 0;
634
+ for(size_t l = 0, i = 0; l < size.height; ++l, i = 0)
635
+ {
636
+ const s16 * src = internal::getRowPtr( srcBase, srcStride, l);
637
+ if (size.width >= 32)
638
+ {
639
+ u32 tmp0123[4] = { 0, 1, 2, 3 };
640
+ uint32x4_t c8 = vdupq_n_u32(8);
641
+
642
+ #if SIZE_MAX > UINT32_MAX
643
+ size_t boundAll = size.width - (8 - 1);
644
+ for(size_t b = 0; i < boundAll; b = i)
645
+ {
646
+ size_t bound = std::min<size_t>(boundAll, b + 0xffffFFF8);
647
+ #else
648
+ {
649
+ size_t bound = size.width - (8 - 1);
650
+ #endif
651
+ uint32x4_t lineIdxOffset = vld1q_u32(tmp0123);
652
+ int16x8_t n_min = vdupq_n_s16(minVal);
653
+ uint32x4_t n_minIdxl = vdupq_n_u32(0xffffFFF8);
654
+ uint32x4_t n_minIdxh = vdupq_n_u32(0xffffFFF8);
655
+ int16x8_t n_max = vdupq_n_s16(maxVal);
656
+ uint32x4_t n_maxIdxl = vdupq_n_u32(0xffffFFF8);
657
+ uint32x4_t n_maxIdxh = vdupq_n_u32(0xffffFFF8);
658
+
659
+ for(; i < bound; i+=8 )
660
+ {
661
+ internal::prefetch(src + i);
662
+ int16x8_t line = vld1q_s16(src + i);
663
+
664
+ uint16x8_t minmask = vcltq_s16(line, n_min);
665
+ uint16x8_t maxmask = vcgtq_s16(line, n_max);
666
+
667
+ n_min = vbslq_s16(minmask, line, n_min);
668
+ uint16x4_t minml = vget_low_u16(minmask);
669
+ uint16x4_t minmh = vget_high_u16(minmask);
670
+ uint32x4_t minml2 = vmovl_u16(minml);
671
+ uint32x4_t minmh2 = vmovl_u16(minmh);
672
+ minml2 = vqshlq_n_u32(minml2, 31);
673
+ minmh2 = vqshlq_n_u32(minmh2, 31);
674
+ n_minIdxl = vbslq_u32(minml2, lineIdxOffset, n_minIdxl);
675
+ n_minIdxh = vbslq_u32(minmh2, lineIdxOffset, n_minIdxh);
676
+
677
+ n_max = vbslq_s16(maxmask, line, n_max);
678
+ uint16x4_t maxml = vget_low_u16(maxmask);
679
+ uint16x4_t maxmh = vget_high_u16(maxmask);
680
+ uint32x4_t maxml2 = vmovl_u16(maxml);
681
+ uint32x4_t maxmh2 = vmovl_u16(maxmh);
682
+ maxml2 = vqshlq_n_u32(maxml2, 31);
683
+ maxmh2 = vqshlq_n_u32(maxmh2, 31);
684
+ n_maxIdxl = vbslq_u32(maxml2, lineIdxOffset, n_maxIdxl);
685
+ n_maxIdxh = vbslq_u32(maxmh2, lineIdxOffset, n_maxIdxh);
686
+
687
+ // idx[] +=8
688
+ lineIdxOffset = vaddq_u32(lineIdxOffset, c8);
689
+ }
690
+
691
+ // fix high part of indexes
692
+ uint32x4_t c4 = vdupq_n_u32((int32_t) 4);
693
+ n_minIdxh = vaddq_u32(n_minIdxh, c4);
694
+ n_maxIdxh = vaddq_u32(n_maxIdxh, c4);
695
+
696
+ s16 fmin[8], fmax[8];
697
+ u32 fminIdx[8], fmaxIdx[8];
698
+
699
+ vst1q_s16(fmin, n_min);
700
+ vst1q_s16(fmax, n_max);
701
+ vst1q_u32(fminIdx+0, n_minIdxl);
702
+ vst1q_u32(fmaxIdx+0, n_maxIdxl);
703
+ vst1q_u32(fminIdx+4, n_minIdxh);
704
+ vst1q_u32(fmaxIdx+4, n_maxIdxh);
705
+
706
+ size_t minIdx = fminIdx[0];
707
+ size_t maxIdx = fmaxIdx[0];
708
+ minVal = fmin[0];
709
+ maxVal = fmax[0];
710
+
711
+ for (s32 j = 1; j < 8; ++j)
712
+ {
713
+ s16 minval = fmin[j];
714
+ s16 maxval = fmax[j];
715
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
716
+ {
717
+ minIdx = fminIdx[j];
718
+ minVal = minval;
719
+ }
720
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
721
+ {
722
+ maxIdx = fmaxIdx[j];
723
+ maxVal = maxval;
724
+ }
725
+ }
726
+ if(minIdx < 0xffffFFF8)
727
+ {
728
+ #if SIZE_MAX > UINT32_MAX
729
+ minCol = b + minIdx;
730
+ #else
731
+ minCol = minIdx;
732
+ #endif
733
+ minRow = l;
734
+ }
735
+ if(maxIdx < 0xffffFFF8)
736
+ {
737
+ #if SIZE_MAX > UINT32_MAX
738
+ maxCol = b + maxIdx;
739
+ #else
740
+ maxCol = maxIdx;
741
+ #endif
742
+ maxRow = l;
743
+ }
744
+ }
745
+ }
746
+ for(; i < size.width; ++i )
747
+ {
748
+ short val = src[i];
749
+ if( val < minVal )
750
+ {
751
+ minVal = val;
752
+ minCol = i;
753
+ minRow = l;
754
+ }
755
+ else if( val > maxVal )
756
+ {
757
+ maxVal = val;
758
+ maxCol = i;
759
+ maxRow = l;
760
+ }
761
+ }
762
+ }
763
+ #else
764
+ (void)size;
765
+ (void)srcBase;
766
+ (void)srcStride;
767
+ (void)minVal;
768
+ (void)minCol;
769
+ (void)minRow;
770
+ (void)maxVal;
771
+ (void)maxCol;
772
+ (void)maxRow;
773
+ #endif
774
+ }
775
+
776
+ void minMaxLoc(const Size2D &size,
777
+ const u16 * srcBase, ptrdiff_t srcStride,
778
+ u16 &minVal, size_t &minCol, size_t &minRow,
779
+ u16 &maxVal, size_t &maxCol, size_t &maxRow)
780
+ {
781
+ internal::assertSupportedConfiguration();
782
+ #ifdef CAROTENE_NEON
783
+ minVal = srcBase[0];
784
+ minCol = 0;
785
+ minRow = 0;
786
+ maxVal = srcBase[0];
787
+ maxCol = 0;
788
+ maxRow = 0;
789
+ for(size_t l = 0, i = 0; l < size.height; ++l, i = 0)
790
+ {
791
+ const u16 * src = internal::getRowPtr( srcBase, srcStride, l);
792
+ if (size.width >= 32)
793
+ {
794
+ u32 tmp0123[4] = { 0, 1, 2, 3 };
795
+ uint32x4_t c8 = vdupq_n_u32(8);
796
+
797
+ #if SIZE_MAX > UINT32_MAX
798
+ size_t boundAll = size.width - (8 - 1);
799
+ for(size_t b = 0; i < boundAll; b = i)
800
+ {
801
+ size_t bound = std::min<size_t>(boundAll, b + 0xffffFFF8);
802
+ #else
803
+ {
804
+ size_t bound = size.width - (8 - 1);
805
+ #endif
806
+ uint32x4_t lineIdxOffset = vld1q_u32(tmp0123);
807
+ uint16x8_t n_min = vdupq_n_u16(minVal);
808
+ uint32x4_t n_minIdxl = vdupq_n_u32(0xffffFFF8);
809
+ uint32x4_t n_minIdxh = vdupq_n_u32(0xffffFFF8);
810
+ uint16x8_t n_max = vdupq_n_u16(maxVal);
811
+ uint32x4_t n_maxIdxl = vdupq_n_u32(0xffffFFF8);
812
+ uint32x4_t n_maxIdxh = vdupq_n_u32(0xffffFFF8);
813
+
814
+ for(; i < bound; i+=8 )
815
+ {
816
+ internal::prefetch(src + i);
817
+ uint16x8_t line = vld1q_u16(src + i);
818
+
819
+ uint16x8_t minmask = vcltq_u16(line, n_min);
820
+ uint16x8_t maxmask = vcgtq_u16(line, n_max);
821
+
822
+ n_min = vbslq_u16(minmask, line, n_min);
823
+ uint16x4_t minml = vget_low_u16(minmask);
824
+ uint16x4_t minmh = vget_high_u16(minmask);
825
+ uint32x4_t minml2 = vmovl_u16(minml);
826
+ uint32x4_t minmh2 = vmovl_u16(minmh);
827
+ minml2 = vqshlq_n_u32(minml2, 31);
828
+ minmh2 = vqshlq_n_u32(minmh2, 31);
829
+ n_minIdxl = vbslq_u32(minml2, lineIdxOffset, n_minIdxl);
830
+ n_minIdxh = vbslq_u32(minmh2, lineIdxOffset, n_minIdxh);
831
+
832
+ n_max = vbslq_u16(maxmask, line, n_max);
833
+ uint16x4_t maxml = vget_low_u16(maxmask);
834
+ uint16x4_t maxmh = vget_high_u16(maxmask);
835
+ uint32x4_t maxml2 = vmovl_u16(maxml);
836
+ uint32x4_t maxmh2 = vmovl_u16(maxmh);
837
+ maxml2 = vqshlq_n_u32(maxml2, 31);
838
+ maxmh2 = vqshlq_n_u32(maxmh2, 31);
839
+ n_maxIdxl = vbslq_u32(maxml2, lineIdxOffset, n_maxIdxl);
840
+ n_maxIdxh = vbslq_u32(maxmh2, lineIdxOffset, n_maxIdxh);
841
+
842
+ // idx[] +=8
843
+ lineIdxOffset = vaddq_u32(lineIdxOffset, c8);
844
+ }
845
+
846
+ // fix high part of indexes
847
+ uint32x4_t c4 = vdupq_n_u32(4);
848
+ n_minIdxh = vaddq_u32(n_minIdxh, c4);
849
+ n_maxIdxh = vaddq_u32(n_maxIdxh, c4);
850
+
851
+ u16 fmin[8], fmax[8];
852
+ u32 fminIdx[8], fmaxIdx[8];
853
+
854
+ vst1q_u16(fmin, n_min);
855
+ vst1q_u16(fmax, n_max);
856
+ vst1q_u32(fminIdx+0, n_minIdxl);
857
+ vst1q_u32(fmaxIdx+0, n_maxIdxl);
858
+ vst1q_u32(fminIdx+4, n_minIdxh);
859
+ vst1q_u32(fmaxIdx+4, n_maxIdxh);
860
+
861
+ size_t minIdx = fminIdx[0];
862
+ size_t maxIdx = fmaxIdx[0];
863
+ minVal = fmin[0];
864
+ maxVal = fmax[0];
865
+
866
+ for (s32 j = 1; j < 8; ++j)
867
+ {
868
+ u16 minval = fmin[j];
869
+ u16 maxval = fmax[j];
870
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
871
+ {
872
+ minIdx = fminIdx[j];
873
+ minVal = minval;
874
+ }
875
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
876
+ {
877
+ maxIdx = fmaxIdx[j];
878
+ maxVal = maxval;
879
+ }
880
+ }
881
+ if(minIdx < 0xffffFFF8)
882
+ {
883
+ #if SIZE_MAX > UINT32_MAX
884
+ minCol = b + minIdx;
885
+ #else
886
+ minCol = minIdx;
887
+ #endif
888
+ minRow = l;
889
+ }
890
+ if(maxIdx < 0xffffFFF8)
891
+ {
892
+ #if SIZE_MAX > UINT32_MAX
893
+ maxCol = b + maxIdx;
894
+ #else
895
+ maxCol = maxIdx;
896
+ #endif
897
+ maxRow = l;
898
+ }
899
+ }
900
+ }
901
+ for(; i < size.width; ++i )
902
+ {
903
+ u16 val = src[i];
904
+ if( val < minVal )
905
+ {
906
+ minVal = val;
907
+ minCol = i;
908
+ minRow = l;
909
+ }
910
+ else if( val > maxVal )
911
+ {
912
+ maxVal = val;
913
+ maxCol = i;
914
+ maxRow = l;
915
+ }
916
+ }
917
+ }
918
+ #else
919
+ (void)size;
920
+ (void)srcBase;
921
+ (void)srcStride;
922
+ (void)minVal;
923
+ (void)minCol;
924
+ (void)minRow;
925
+ (void)maxVal;
926
+ (void)maxCol;
927
+ (void)maxRow;
928
+ #endif
929
+ }
930
+
931
+ #ifdef CAROTENE_NEON
932
+ namespace {
933
+
934
+ void minMaxLocBlock(const u8 * src, u32 len,
935
+ u8 &minVal, u16 &minIdx,
936
+ u8 &maxVal, u16 &maxIdx)
937
+ {
938
+ u16 tmp0123[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
939
+
940
+ uint8x16_t n_min = vdupq_n_u8(src[0]);
941
+ uint16x8_t n_minIdxl = vdupq_n_u16(0);
942
+ uint16x8_t n_minIdxh = vdupq_n_u16(0);
943
+ uint8x16_t n_max = vdupq_n_u8(src[0]);
944
+ uint16x8_t n_maxIdxl = vdupq_n_u16(0);
945
+ uint16x8_t n_maxIdxh = vdupq_n_u16(0);
946
+ uint16x8_t c16 = vdupq_n_u16(16);
947
+ uint16x8_t lineIdxOffset = vld1q_u16(tmp0123);
948
+
949
+ s32 i = 0;
950
+ s32 bound = len - (16 - 1);
951
+ for(; i < bound; i+=16 )
952
+ {
953
+ internal::prefetch(src + i);
954
+ uint8x16_t line = vld1q_u8(src + i);
955
+
956
+ uint8x16_t minmask = vcltq_u8(line, n_min);
957
+ uint8x16_t maxmask = vcgtq_u8(line, n_max);
958
+
959
+ n_min = vbslq_u8(minmask, line, n_min);
960
+ uint8x8_t minml = vget_low_u8(minmask);
961
+ uint8x8_t minmh = vget_high_u8(minmask);
962
+ uint16x8_t minml2 = vmovl_u8(minml);
963
+ uint16x8_t minmh2 = vmovl_u8(minmh);
964
+ minml2 = vqshlq_n_u16(minml2, 15);
965
+ minmh2 = vqshlq_n_u16(minmh2, 15);
966
+ n_minIdxl = vbslq_u16(minml2, lineIdxOffset, n_minIdxl);
967
+ n_minIdxh = vbslq_u16(minmh2, lineIdxOffset, n_minIdxh);
968
+
969
+ n_max = vbslq_u8(maxmask, line, n_max);
970
+ uint8x8_t maxml = vget_low_u8(maxmask);
971
+ uint8x8_t maxmh = vget_high_u8(maxmask);
972
+ uint16x8_t maxml2 = vmovl_u8(maxml);
973
+ uint16x8_t maxmh2 = vmovl_u8(maxmh);
974
+ maxml2 = vqshlq_n_u16(maxml2, 15);
975
+ maxmh2 = vqshlq_n_u16(maxmh2, 15);
976
+ n_maxIdxl = vbslq_u16(maxml2, lineIdxOffset, n_maxIdxl);
977
+ n_maxIdxh = vbslq_u16(maxmh2, lineIdxOffset, n_maxIdxh);
978
+
979
+ // idx[] +=16
980
+ lineIdxOffset = vaddq_u16(lineIdxOffset, c16);
981
+ }
982
+
983
+ // fix high part of indexes
984
+ uint16x8_t c8 = vdupq_n_u16(8);
985
+ n_minIdxh = vaddq_u16(n_minIdxh, c8);
986
+ n_maxIdxh = vaddq_u16(n_maxIdxh, c8);
987
+
988
+ u8 fmin[16], fmax[16];
989
+ u16 fminIdx[16], fmaxIdx[16];
990
+ /*{
991
+ uint8x8_t min_low = vget_low_u8(n_min);
992
+ uint8x8_t min_high = vget_high_u8(n_min);
993
+ uint8x8_t max_low = vget_low_u8(n_max);
994
+ uint8x8_t max_high = vget_high_u8(n_max);
995
+
996
+ uint8x8_t minmask = vclt_u8(min_low, min_high);
997
+ uint8x8_t maxmask = vcgt_u8(max_low, max_high);
998
+
999
+ uint8x8_t min2 = vbsl_u8(minmask, min_low, min_high);
1000
+ uint8x8_t max2 = vbsl_u8(maxmask, max_low, max_high);
1001
+
1002
+ uint16x8_t minidxmask = vmovl_u8(minmask);
1003
+ uint16x8_t maxidxmask = vmovl_u8(maxmask);
1004
+ minidxmask = vqshlq_n_u16(minidxmask, 15);
1005
+ maxidxmask = vqshlq_n_u16(maxidxmask, 15);
1006
+
1007
+ uint16x8_t n_minIdx = vbslq_u16(minidxmask, n_minIdxl, n_minIdxh);
1008
+ uint16x8_t n_maxIdx = vbslq_u16(maxidxmask, n_maxIdxl, n_maxIdxh);
1009
+
1010
+ vst1_u8((uint8_t*)fmin, min2);
1011
+ vst1_u8((uint8_t*)fmax, max2);
1012
+
1013
+ vst1q_u16((uint16_t*)(fminIdx), n_minIdx);
1014
+ vst1q_u16((uint16_t*)(fmaxIdx), n_maxIdx);
1015
+ }*/
1016
+
1017
+ vst1q_u8(fmin, n_min);
1018
+ vst1q_u8(fmax, n_max);
1019
+ vst1q_u16(fminIdx+0, n_minIdxl);
1020
+ vst1q_u16(fmaxIdx+0, n_maxIdxl);
1021
+ vst1q_u16(fminIdx+8, n_minIdxh);
1022
+ vst1q_u16(fmaxIdx+8, n_maxIdxh);
1023
+
1024
+ minIdx = fminIdx[0];
1025
+ maxIdx = fmaxIdx[0];
1026
+ minVal = fmin[0];
1027
+ maxVal = fmax[0];
1028
+
1029
+ for (s32 j = 1; j < 16; ++j)
1030
+ {
1031
+ u8 minval = fmin[j];
1032
+ u8 maxval = fmax[j];
1033
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
1034
+ {
1035
+ minIdx = fminIdx[j];
1036
+ minVal = minval;
1037
+ }
1038
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
1039
+ {
1040
+ maxIdx = fmaxIdx[j];
1041
+ maxVal = maxval;
1042
+ }
1043
+ }
1044
+
1045
+ for(; i < (s32)len; ++i )
1046
+ {
1047
+ u8 val = src[i];
1048
+ if( val < minVal )
1049
+ {
1050
+ minVal = val;
1051
+ minIdx = (u16)i;
1052
+ }
1053
+ else if( val > maxVal )
1054
+ {
1055
+ maxVal = val;
1056
+ maxIdx = (u16)i;
1057
+ }
1058
+ }
1059
+ }
1060
+
1061
+ void minMaxLocBlock(const s8 * src, u32 len,
1062
+ s8 &minVal, u16 &minIdx,
1063
+ s8 &maxVal, u16 &maxIdx)
1064
+ {
1065
+ u16 tmp0123[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
1066
+
1067
+ int8x16_t n_min = vdupq_n_s8(src[0]);
1068
+ uint16x8_t n_minIdxl = vdupq_n_u16(0);
1069
+ uint16x8_t n_minIdxh = vdupq_n_u16(0);
1070
+ int8x16_t n_max = vdupq_n_s8(src[0]);
1071
+ uint16x8_t n_maxIdxl = vdupq_n_u16(0);
1072
+ uint16x8_t n_maxIdxh = vdupq_n_u16(0);
1073
+ uint16x8_t c16 = vdupq_n_u16(16);
1074
+ uint16x8_t lineIdxOffset = vld1q_u16(tmp0123);
1075
+
1076
+ s32 i = 0;
1077
+ s32 bound = len - (16 - 1);
1078
+ for(; i < bound; i+=16 )
1079
+ {
1080
+ internal::prefetch(src + i);
1081
+ int8x16_t line = vld1q_s8(src + i);
1082
+
1083
+ uint8x16_t minmask = vcltq_s8(line, n_min);
1084
+ uint8x16_t maxmask = vcgtq_s8(line, n_max);
1085
+
1086
+ n_min = vbslq_s8(minmask, line, n_min);
1087
+ uint8x8_t minml = vget_low_u8(minmask);
1088
+ uint8x8_t minmh = vget_high_u8(minmask);
1089
+ uint16x8_t minml2 = vmovl_u8(minml);
1090
+ uint16x8_t minmh2 = vmovl_u8(minmh);
1091
+ minml2 = vqshlq_n_u16(minml2, 15);
1092
+ minmh2 = vqshlq_n_u16(minmh2, 15);
1093
+ n_minIdxl = vbslq_u16(minml2, lineIdxOffset, n_minIdxl);
1094
+ n_minIdxh = vbslq_u16(minmh2, lineIdxOffset, n_minIdxh);
1095
+
1096
+ n_max = vbslq_s8(maxmask, line, n_max);
1097
+ uint8x8_t maxml = vget_low_u8(maxmask);
1098
+ uint8x8_t maxmh = vget_high_u8(maxmask);
1099
+ uint16x8_t maxml2 = vmovl_u8(maxml);
1100
+ uint16x8_t maxmh2 = vmovl_u8(maxmh);
1101
+ maxml2 = vqshlq_n_u16(maxml2, 15);
1102
+ maxmh2 = vqshlq_n_u16(maxmh2, 15);
1103
+ n_maxIdxl = vbslq_u16(maxml2, lineIdxOffset, n_maxIdxl);
1104
+ n_maxIdxh = vbslq_u16(maxmh2, lineIdxOffset, n_maxIdxh);
1105
+
1106
+ // idx[] +=16
1107
+ lineIdxOffset = vaddq_u16(lineIdxOffset, c16);
1108
+ }
1109
+
1110
+ // fix high part of indexes
1111
+ uint16x8_t c8 = vdupq_n_u16(8);
1112
+ n_minIdxh = vaddq_u16(n_minIdxh, c8);
1113
+ n_maxIdxh = vaddq_u16(n_maxIdxh, c8);
1114
+
1115
+ s8 fmin[16], fmax[16];
1116
+ u16 fminIdx[16], fmaxIdx[16];
1117
+
1118
+ vst1q_s8(fmin, n_min);
1119
+ vst1q_s8(fmax, n_max);
1120
+ vst1q_u16(fminIdx+0, n_minIdxl);
1121
+ vst1q_u16(fmaxIdx+0, n_maxIdxl);
1122
+ vst1q_u16(fminIdx+8, n_minIdxh);
1123
+ vst1q_u16(fmaxIdx+8, n_maxIdxh);
1124
+
1125
+ minIdx = fminIdx[0];
1126
+ maxIdx = fmaxIdx[0];
1127
+ minVal = fmin[0];
1128
+ maxVal = fmax[0];
1129
+
1130
+ for (s32 j = 1; j < 16; ++j)
1131
+ {
1132
+ s8 minval = fmin[j];
1133
+ s8 maxval = fmax[j];
1134
+ if (minval < minVal || (minval == minVal && fminIdx[j] < minIdx))
1135
+ {
1136
+ minIdx = fminIdx[j];
1137
+ minVal = minval;
1138
+ }
1139
+ if (maxval > maxVal || (maxval == maxVal && fmaxIdx[j] < maxIdx))
1140
+ {
1141
+ maxIdx = fmaxIdx[j];
1142
+ maxVal = maxval;
1143
+ }
1144
+ }
1145
+
1146
+ for(; i < (s32)len; ++i )
1147
+ {
1148
+ s8 val = src[i];
1149
+ if( val < minVal )
1150
+ {
1151
+ minVal = val;
1152
+ minIdx = (u16)i;
1153
+ }
1154
+ else if( val > maxVal )
1155
+ {
1156
+ maxVal = val;
1157
+ maxIdx = (u16)i;
1158
+ }
1159
+ }
1160
+ }
1161
+
1162
+ } // namespace
1163
+ #endif // CAROTENE_NEON
1164
+
1165
+ #define USHORT_BLOCK_MAX_SIZE (1 << 16)
1166
+
1167
+ void minMaxLoc(const Size2D &size,
1168
+ const u8 * srcBase, ptrdiff_t srcStride,
1169
+ u8 &minVal, size_t &minCol, size_t &minRow,
1170
+ u8 &maxVal, size_t &maxCol, size_t &maxRow)
1171
+ {
1172
+ internal::assertSupportedConfiguration();
1173
+ #ifdef CAROTENE_NEON
1174
+ minVal = srcBase[0];
1175
+ minCol = 0;
1176
+ minRow = 0;
1177
+ maxVal = srcBase[0];
1178
+ maxCol = 0;
1179
+ maxRow = 0;
1180
+ for(size_t l = 0; l < size.height; ++l)
1181
+ {
1182
+ const u8 * src = internal::getRowPtr( srcBase, srcStride, l);
1183
+ if (size.width > 128)
1184
+ {
1185
+ for(size_t blockStart = 0; blockStart < size.width; blockStart += USHORT_BLOCK_MAX_SIZE)
1186
+ {
1187
+ u8 locMinVal, locMaxVal;
1188
+ u16 locMinIdx, locMaxIdx;
1189
+ size_t tail = size.width - blockStart;
1190
+ minMaxLocBlock(src + blockStart, tail < USHORT_BLOCK_MAX_SIZE ? tail : USHORT_BLOCK_MAX_SIZE,
1191
+ locMinVal, locMinIdx, locMaxVal, locMaxIdx);
1192
+
1193
+ if (locMinVal == 0 && locMaxVal == 255)
1194
+ {
1195
+ minCol = blockStart + locMinIdx;
1196
+ maxCol = blockStart + locMaxIdx;
1197
+ minRow = l;
1198
+ maxRow = l;
1199
+ minVal = 0;
1200
+ maxVal = 255;
1201
+ return;
1202
+ }
1203
+ else
1204
+ {
1205
+ if (locMinVal < minVal)
1206
+ {
1207
+ minCol = blockStart + locMinIdx;
1208
+ minRow = l;
1209
+ minVal = locMinVal;
1210
+ }
1211
+ if (locMaxVal > maxVal)
1212
+ {
1213
+ maxCol = blockStart + locMaxIdx;
1214
+ maxRow = l;
1215
+ maxVal = locMaxVal;
1216
+ }
1217
+ }
1218
+ }
1219
+ }
1220
+ else
1221
+ {
1222
+ for(size_t i = 0; i < size.width; ++i )
1223
+ {
1224
+ u8 val = src[i];
1225
+ if( val < minVal )
1226
+ {
1227
+ minVal = val;
1228
+ minCol = i;
1229
+ minRow = l;
1230
+ }
1231
+ else if( val > maxVal )
1232
+ {
1233
+ maxVal = val;
1234
+ maxCol = i;
1235
+ maxRow = l;
1236
+ }
1237
+ }
1238
+ }
1239
+
1240
+ }
1241
+ #else
1242
+ (void)size;
1243
+ (void)srcBase;
1244
+ (void)srcStride;
1245
+ (void)minVal;
1246
+ (void)minCol;
1247
+ (void)minRow;
1248
+ (void)maxVal;
1249
+ (void)maxCol;
1250
+ (void)maxRow;
1251
+ #endif
1252
+ }
1253
+
1254
+ void minMaxLoc(const Size2D &size,
1255
+ const s8 * srcBase, ptrdiff_t srcStride,
1256
+ s8 &minVal, size_t &minCol, size_t &minRow,
1257
+ s8 &maxVal, size_t &maxCol, size_t &maxRow)
1258
+ {
1259
+ internal::assertSupportedConfiguration();
1260
+ #ifdef CAROTENE_NEON
1261
+ minVal = srcBase[0];
1262
+ minCol = 0;
1263
+ minRow = 0;
1264
+ maxVal = srcBase[0];
1265
+ maxCol = 0;
1266
+ maxRow = 0;
1267
+ for(size_t l = 0; l < size.height; ++l)
1268
+ {
1269
+ const s8 * src = internal::getRowPtr( srcBase, srcStride, l);
1270
+ if (size.width > 128)
1271
+ {
1272
+ for(size_t blockStart = 0; blockStart < size.width; blockStart += USHORT_BLOCK_MAX_SIZE)
1273
+ {
1274
+ s8 locMinVal, locMaxVal;
1275
+ u16 locMinIdx, locMaxIdx;
1276
+ size_t tail = size.width - blockStart;
1277
+ minMaxLocBlock(src + blockStart, tail < USHORT_BLOCK_MAX_SIZE ? tail : USHORT_BLOCK_MAX_SIZE,
1278
+ locMinVal, locMinIdx, locMaxVal, locMaxIdx);
1279
+
1280
+ if (locMinVal == -128 && locMaxVal == 127)
1281
+ {
1282
+ minCol = blockStart + locMinIdx;
1283
+ maxCol = blockStart + locMaxIdx;
1284
+ minRow = l;
1285
+ maxRow = l;
1286
+ minVal = -128;
1287
+ maxVal = 127;
1288
+ return;
1289
+ }
1290
+ else
1291
+ {
1292
+ if (locMinVal < minVal)
1293
+ {
1294
+ minCol = blockStart + locMinIdx;
1295
+ minRow = l;
1296
+ minVal = locMinVal;
1297
+ }
1298
+ if (locMaxVal > maxVal)
1299
+ {
1300
+ maxCol = blockStart + locMaxIdx;
1301
+ maxRow = l;
1302
+ maxVal = locMaxVal;
1303
+ }
1304
+ }
1305
+ }
1306
+ }
1307
+ else
1308
+ {
1309
+ for(size_t i = 0; i < size.width; ++i )
1310
+ {
1311
+ s8 val = src[i];
1312
+ if( val < minVal )
1313
+ {
1314
+ minVal = val;
1315
+ minRow = l;
1316
+ minCol = i;
1317
+ }
1318
+ else if( val > maxVal )
1319
+ {
1320
+ maxVal = val;
1321
+ maxRow = l;
1322
+ maxCol = i;
1323
+ }
1324
+ }
1325
+ }
1326
+ }
1327
+ #else
1328
+ (void)size;
1329
+ (void)srcBase;
1330
+ (void)srcStride;
1331
+ (void)minVal;
1332
+ (void)minCol;
1333
+ (void)minRow;
1334
+ (void)maxVal;
1335
+ (void)maxCol;
1336
+ (void)maxRow;
1337
+ #endif
1338
+ }
1339
+
1340
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/morph.cpp ADDED
@@ -0,0 +1,728 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include <algorithm>
43
+ #include <limits>
44
+ #include <vector>
45
+ #include <cstring>
46
+
47
+ namespace CAROTENE_NS {
48
+
49
+ bool isMorph3x3Supported(const Size2D &size, BORDER_MODE border)
50
+ {
51
+ return isSupportedConfiguration() && size.width >= 16 &&
52
+ (border == BORDER_MODE_CONSTANT ||
53
+ border == BORDER_MODE_REPLICATE);
54
+ }
55
+
56
+ #ifdef CAROTENE_NEON
57
+
58
+ namespace {
59
+
60
+ struct ErodeVecOp
61
+ {
62
+ ErodeVecOp():borderValue(0){}
63
+
64
+ ErodeVecOp(BORDER_MODE border, u8 borderValue_) :
65
+ borderValue(borderValue_)
66
+ {
67
+ if (border == BORDER_MODE_REPLICATE)
68
+ borderValue = std::numeric_limits<u8>::max();
69
+ }
70
+
71
+ inline uint8x16_t operator()(uint8x16_t a, uint8x16_t b) const
72
+ {
73
+ return vminq_u8(a, b);
74
+ }
75
+
76
+ inline uint8x8_t operator()(uint8x8_t a, uint8x8_t b) const
77
+ {
78
+ return vmin_u8(a, b);
79
+ }
80
+
81
+ inline u8 operator()(u8 a, u8 b) const
82
+ {
83
+ return std::min(a, b);
84
+ }
85
+
86
+ u8 borderValue;
87
+ };
88
+
89
+ struct DilateVecOp
90
+ {
91
+ DilateVecOp():borderValue(0){}
92
+
93
+ DilateVecOp(BORDER_MODE border, u8 borderValue_) :
94
+ borderValue(borderValue_)
95
+ {
96
+ if (border == BORDER_MODE_REPLICATE)
97
+ borderValue = std::numeric_limits<u8>::min();
98
+ }
99
+
100
+ inline uint8x16_t operator()(uint8x16_t a, uint8x16_t b) const
101
+ {
102
+ return vmaxq_u8(a, b);
103
+ }
104
+
105
+ inline uint8x8_t operator()(uint8x8_t a, uint8x8_t b) const
106
+ {
107
+ return vmax_u8(a, b);
108
+ }
109
+
110
+ inline u8 operator()(u8 a, u8 b) const
111
+ {
112
+ return std::max(a, b);
113
+ }
114
+
115
+ u8 borderValue;
116
+ };
117
+
118
+ template <typename VecOp>
119
+ void morph3x3(const Size2D &size,
120
+ const u8 * srcBase, ptrdiff_t srcStride,
121
+ u8 * dstBase, ptrdiff_t dstStride,
122
+ BORDER_MODE border, const VecOp & vop)
123
+ {
124
+ u8 borderValue = vop.borderValue;
125
+ ptrdiff_t width = (ptrdiff_t)size.width, height = (ptrdiff_t)size.height;
126
+
127
+ const uint8x16_t v_zero = vdupq_n_u8(0);
128
+ const uint8x16_t v_border = vdupq_n_u8(borderValue);
129
+
130
+ uint8x16_t tprev = v_zero, tcurr = v_zero, tnext = v_zero;
131
+ uint8x16_t t0 = v_zero, t1 = v_zero, t2 = v_zero;
132
+
133
+ for (ptrdiff_t y = 0; y < height; ++y)
134
+ {
135
+ const u8 * srow0 = y == 0 && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::max<ptrdiff_t>(y - 1, 0));
136
+ const u8 * srow1 = internal::getRowPtr(srcBase, srcStride, y);
137
+ const u8 * srow2 = y + 1 == height && border == BORDER_MODE_CONSTANT ? NULL : internal::getRowPtr(srcBase, srcStride, std::min(y + 1, height - 1));
138
+ u8 * drow = internal::getRowPtr(dstBase, dstStride, y);
139
+
140
+ u8 prevx = 0, currx = 0, nextx = 0;
141
+ ptrdiff_t x = 0;
142
+ const ptrdiff_t bwidth = y + 2 < height ? width : (width - 16);
143
+
144
+ // perform vertical convolution
145
+ for ( ; x <= bwidth; x += 16)
146
+ {
147
+ internal::prefetch(srow0 + x);
148
+ internal::prefetch(srow1 + x);
149
+ internal::prefetch(srow2 + x);
150
+
151
+ uint8x16_t x0 = !srow0 ? v_border : vld1q_u8(srow0 + x);
152
+ uint8x16_t x1 = vld1q_u8(srow1 + x);
153
+ uint8x16_t x2 = !srow2 ? v_border : vld1q_u8(srow2 + x);
154
+
155
+ // calculate values for plain CPU part below if needed
156
+ if (x + 16 >= bwidth)
157
+ {
158
+ ptrdiff_t x3 = x == width ? width - 1 : x;
159
+ ptrdiff_t x4 = border == BORDER_MODE_CONSTANT ? x3 - 1 : std::max<ptrdiff_t>(x3 - 1, 0);
160
+
161
+ if (border == BORDER_MODE_CONSTANT && x4 < 0)
162
+ prevx = borderValue;
163
+ else
164
+ prevx = vop(srow1[x4],
165
+ vop(srow2 ? srow2[x4] : borderValue,
166
+ srow0 ? srow0[x4] : borderValue));
167
+
168
+ currx = vop(srow2 ? srow2[x3] : borderValue, vop(srow1[x3], srow0 ? srow0[x3] : borderValue));
169
+ }
170
+
171
+ // make shift
172
+ if (x)
173
+ {
174
+ tprev = tcurr;
175
+ tcurr = tnext;
176
+ }
177
+
178
+ // and calculate next value
179
+ tnext = vop(vop(x0, x1), x2);
180
+
181
+ // make extrapolation for the first elements
182
+ if (!x)
183
+ {
184
+ // make border
185
+ if (border == BORDER_MODE_CONSTANT)
186
+ tcurr = v_border;
187
+ else if (border == BORDER_MODE_REPLICATE)
188
+ tcurr = vdupq_n_u8(vgetq_lane_u8(tnext, 0));
189
+
190
+ continue;
191
+ }
192
+
193
+ // combine 3 "shifted" vectors
194
+ t0 = vextq_u8(tprev, tcurr, 15);
195
+ t1 = tcurr;
196
+ t2 = vextq_u8(tcurr, tnext, 1);
197
+
198
+ // and add them
199
+ t0 = vop(t0, vop(t1, t2));
200
+
201
+ vst1q_u8(drow + x - 16, t0);
202
+ }
203
+
204
+ x -= 16;
205
+ if (x == width)
206
+ --x;
207
+
208
+ for ( ; x < width; ++x)
209
+ {
210
+ // make extrapolation for the last elements
211
+ if (x + 1 >= width)
212
+ {
213
+ if (border == BORDER_MODE_CONSTANT)
214
+ nextx = borderValue;
215
+ else if (border == BORDER_MODE_REPLICATE)
216
+ nextx = vop(srow2[x], vop(srow1[x], srow0[x]));
217
+ }
218
+ else
219
+ nextx = vop(vop(srow2 ? srow2[x + 1] : borderValue,
220
+ srow0 ? srow0[x + 1] : borderValue),
221
+ srow1[x + 1]);
222
+
223
+ drow[x] = vop(prevx, vop(currx, nextx));
224
+
225
+ // make shift
226
+ prevx = currx;
227
+ currx = nextx;
228
+ }
229
+ }
230
+ }
231
+
232
+ } // namespace
233
+
234
+ #endif
235
+
236
+ void erode3x3(const Size2D &size,
237
+ const u8 * srcBase, ptrdiff_t srcStride,
238
+ u8 * dstBase, ptrdiff_t dstStride,
239
+ BORDER_MODE border, u8 borderValue)
240
+ {
241
+ internal::assertSupportedConfiguration(isMorph3x3Supported(size, border));
242
+ #ifdef CAROTENE_NEON
243
+ morph3x3(size,
244
+ srcBase, srcStride,
245
+ dstBase, dstStride,
246
+ border, ErodeVecOp(border, borderValue));
247
+ #else
248
+ (void)size;
249
+ (void)srcBase;
250
+ (void)srcStride;
251
+ (void)dstBase;
252
+ (void)dstStride;
253
+ (void)border;
254
+ (void)borderValue;
255
+ #endif
256
+ }
257
+
258
+ void dilate3x3(const Size2D &size,
259
+ const u8 * srcBase, ptrdiff_t srcStride,
260
+ u8 * dstBase, ptrdiff_t dstStride,
261
+ BORDER_MODE border, u8 borderValue)
262
+ {
263
+ internal::assertSupportedConfiguration(isMorph3x3Supported(size, border));
264
+ #ifdef CAROTENE_NEON
265
+ morph3x3(size,
266
+ srcBase, srcStride,
267
+ dstBase, dstStride,
268
+ border, DilateVecOp(border, borderValue));
269
+ #else
270
+ (void)size;
271
+ (void)srcBase;
272
+ (void)srcStride;
273
+ (void)dstBase;
274
+ (void)dstStride;
275
+ (void)border;
276
+ (void)borderValue;
277
+ #endif
278
+ }
279
+
280
+ #ifdef CAROTENE_NEON
281
+ namespace {
282
+
283
+ template<class VecUpdate>
284
+ void MorphRow(const u8* src, u8* dst, size_t width, s32 cn, size_t ksize)
285
+ {
286
+ size_t i, j, k;
287
+ size_t width16 = (width & -16) * cn;
288
+ size_t width8 = (width & -8) * cn;
289
+ width *= cn;
290
+
291
+ if (ksize == 1)
292
+ {
293
+ for (i = 0; i < width; i++)
294
+ dst[i] = src[i];
295
+ return;
296
+ }
297
+
298
+ ksize = ksize*cn;
299
+ VecUpdate updateOp;
300
+ switch(cn)
301
+ {
302
+ case 1:
303
+ for (i = 0; i < width16; i += 16)
304
+ {
305
+ const u8* sptr = src + i;
306
+ uint8x16_t s = vld1q_u8(sptr);
307
+ internal::prefetch(sptr);
308
+
309
+ for( k = 1; k < ksize; ++k)
310
+ s = updateOp(s, vld1q_u8(sptr + k));
311
+
312
+ vst1q_u8(dst + i, s);
313
+ }
314
+
315
+ for (; i < width8; i += 8)
316
+ {
317
+ const u8* sptr = src + i;
318
+ uint8x8_t s = vld1_u8(sptr);
319
+ internal::prefetch(sptr);
320
+
321
+ for( k = 1; k < ksize; ++k)
322
+ s = updateOp(s, vld1_u8(sptr + k));
323
+
324
+ vst1_u8(dst + i, s);
325
+ }
326
+ break;
327
+ default:
328
+ for (i = 0; i < width16; i += 16)
329
+ {
330
+ uint8x16_t s = vld1q_u8(src + i);
331
+ internal::prefetch(src + i);
332
+
333
+ for (k = cn; k < ksize; k += cn)
334
+ s = updateOp(s, vld1q_u8(src + i + k));
335
+
336
+ vst1q_u8(dst + i, s);
337
+ }
338
+
339
+ for (; i < width8; i += 8)
340
+ {
341
+ uint8x8_t s = vld1_u8(src + i);
342
+ internal::prefetch(src + i);
343
+
344
+ for (k = cn; k < ksize; k += cn)
345
+ s = updateOp(s, vld1_u8(src + i + k));
346
+
347
+ vst1_u8(dst + i, s);
348
+ }
349
+ break;
350
+ }
351
+
352
+ ptrdiff_t i0 = i;
353
+ for( k = 0; k < (size_t)cn; k++, src++, dst++ )
354
+ {
355
+ for( i = i0; i <= width - cn*2; i += cn*2 )
356
+ {
357
+ const u8* s = src + i;
358
+ u8 m = s[cn];
359
+ for( j = cn*2; j < ksize; j += cn )
360
+ m = updateOp(m, s[j]);
361
+ dst[i] = updateOp(m, s[0]);
362
+ dst[i+cn] = updateOp(m, s[j]);
363
+ }
364
+
365
+ for( ; i < width; i += cn )
366
+ {
367
+ const u8* s = src + i;
368
+ u8 m = s[0];
369
+ for( j = cn; j < ksize; j += cn )
370
+ m = updateOp(m, s[j]);
371
+ dst[i] = m;
372
+ }
373
+ }
374
+ }
375
+
376
+ template<class VecUpdate>
377
+ void MorphColumn(const u8** src, u8* dst, ptrdiff_t dststep, size_t count, size_t width, size_t ksize)
378
+ {
379
+ size_t i, k;
380
+ size_t width32 = width & -32;
381
+ VecUpdate updateOp;
382
+
383
+ uint8x16_t x0,x1,s0,s1;
384
+ if (ksize == 3)
385
+ {
386
+ for (; count > 1; count -= 2, dst += dststep * 2, src += 2)
387
+ {
388
+ for (i = 0; i < width32; i += 32)
389
+ {
390
+ const u8* sptr = src[1] + i;
391
+ s0 = vld1q_u8(sptr);
392
+ s1 = vld1q_u8(sptr + 16);
393
+ internal::prefetch(sptr);
394
+
395
+ sptr = src[2] + i;
396
+ x0 = vld1q_u8(sptr);
397
+ x1 = vld1q_u8(sptr + 16);
398
+ internal::prefetch(sptr);
399
+
400
+ s0 = updateOp(s0, x0);
401
+ s1 = updateOp(s1, x1);
402
+
403
+ sptr = src[0] + i;
404
+ x0 = vld1q_u8(sptr);
405
+ x1 = vld1q_u8(sptr + 16);
406
+ internal::prefetch(sptr);
407
+
408
+ vst1q_u8(dst+i, updateOp(s0, x0));
409
+ vst1q_u8(dst+i+16, updateOp(s1, x1));
410
+
411
+ sptr = src[3] + i;
412
+ x0 = vld1q_u8(sptr);
413
+ x1 = vld1q_u8(sptr + 16);
414
+ internal::prefetch(sptr);
415
+ vst1q_u8(dst + dststep + i, updateOp(s0, x0));
416
+ vst1q_u8(dst + dststep + i + 16, updateOp(s1, x1));
417
+
418
+ }
419
+ for(; i < width; i++ )
420
+ {
421
+ u8 s = src[1][i];
422
+
423
+ for( k = 2; k < ksize; k++ )
424
+ s = updateOp(s, src[k][i]);
425
+
426
+ dst[i] = updateOp(s, src[0][i]);
427
+ dst[i+dststep] = updateOp(s, src[k][i]);
428
+ }
429
+ }
430
+ }
431
+ else if (ksize > 1)
432
+ for (; count > 1; count -= 2, dst += dststep*2, src += 2)
433
+ {
434
+ for (i = 0; i < width32; i += 32)
435
+ {
436
+ const u8* sptr = src[1] + i;
437
+ s0 = vld1q_u8(sptr);
438
+ s1 = vld1q_u8(sptr + 16);
439
+ internal::prefetch(sptr);
440
+ for (k = 2; k < ksize; k++)
441
+ {
442
+ sptr = src[k] + i;
443
+ x0 = vld1q_u8(sptr);
444
+ x1 = vld1q_u8(sptr + 16);
445
+ internal::prefetch(sptr);
446
+
447
+ s0 = updateOp(s0, x0);
448
+ s1 = updateOp(s1, x1);
449
+ }
450
+
451
+ sptr = src[0] + i;
452
+ x0 = vld1q_u8(sptr);
453
+ x1 = vld1q_u8(sptr + 16);
454
+ internal::prefetch(sptr);
455
+
456
+ vst1q_u8(dst+i, updateOp(s0, x0));
457
+ vst1q_u8(dst+i+16, updateOp(s1, x1));
458
+
459
+ sptr = src[k] + i;
460
+ x0 = vld1q_u8(sptr);
461
+ x1 = vld1q_u8(sptr + 16);
462
+ internal::prefetch(sptr);
463
+ vst1q_u8(dst + dststep + i, updateOp(s0, x0));
464
+ vst1q_u8(dst + dststep + i + 16, updateOp(s1, x1));
465
+ }
466
+ for(; i < width; i++ )
467
+ {
468
+ u8 s = src[1][i];
469
+
470
+ for( k = 2; k < ksize; k++ )
471
+ s = updateOp(s, src[k][i]);
472
+
473
+ dst[i] = updateOp(s, src[0][i]);
474
+ dst[i+dststep] = updateOp(s, src[k][i]);
475
+ }
476
+ }
477
+
478
+ for (; count > 0; count--, dst += dststep, src++)
479
+ {
480
+ for (i = 0; i < width32; i += 32)
481
+ {
482
+ const u8* sptr = src[0] + i;
483
+ s0 = vld1q_u8(sptr);
484
+ s1 = vld1q_u8(sptr + 16);
485
+ internal::prefetch(sptr);
486
+
487
+ for (k = 1; k < ksize; k++)
488
+ {
489
+ sptr = src[k] + i;
490
+ x0 = vld1q_u8(sptr);
491
+ x1 = vld1q_u8(sptr + 16);
492
+ internal::prefetch(sptr);
493
+ s0 = updateOp(s0, x0);
494
+ s1 = updateOp(s1, x1);
495
+ }
496
+
497
+ vst1q_u8(dst + i, s0);
498
+ vst1q_u8(dst + i + 16, s1);
499
+ }
500
+ for(; i < width; i++ )
501
+ {
502
+ u8 s = src[0][i];
503
+ for( k = 1; k < ksize; k++ )
504
+ s = updateOp(s, src[k][i]);
505
+ dst[i] = s;
506
+ }
507
+ }
508
+ }
509
+
510
+ template <class Op>
511
+ inline void morphology(const Size2D &ssize, u32 cn,
512
+ const u8 * srcBase, ptrdiff_t srcStride,
513
+ u8 * dstBase, ptrdiff_t dstStride,
514
+ const Size2D &ksize,
515
+ size_t anchorX, size_t anchorY,
516
+ BORDER_MODE rowBorderType, BORDER_MODE columnBorderType,
517
+ const u8 * borderValues, Margin borderMargin)
518
+ {
519
+ //Temporary buffers common for all iterations
520
+ std::vector<u8> _srcRow(cn*(ssize.width + ksize.width - 1));
521
+ u8* srcRow = &_srcRow[0];
522
+
523
+ size_t bufRows = std::max<size_t>(ksize.height + 3, std::max<size_t>(anchorY, ksize.height-anchorY-1)*2+1);
524
+ std::vector<u8*> _rows(bufRows);
525
+ u8** rows = &_rows[0];
526
+
527
+ // adjust swidthcn so that the used part of buffers stays compact in memory
528
+ ptrdiff_t swidthcn = cn*((ssize.width + 15) & -16);// cn * (aligned ssize.width size)
529
+ std::vector<u8> _ringBuf(swidthcn*bufRows+16);
530
+ u8 * ringBuf = internal::alignPtr(&_ringBuf[0], 16);
531
+
532
+ size_t borderLength = std::max<size_t>(ksize.width - 1, 1) * cn;
533
+ std::vector<ptrdiff_t> _borderTab(borderLength);
534
+ ptrdiff_t * borderTab = &_borderTab[0];
535
+
536
+ std::vector<u8> _constBorderValue;
537
+ std::vector<u8> _constBorderRow;
538
+ u8 * constBorderValue = NULL;
539
+ u8 * constBorderRow = NULL;
540
+ if( rowBorderType == BORDER_MODE_CONSTANT || columnBorderType == BORDER_MODE_CONSTANT )
541
+ {
542
+ _constBorderValue.resize(borderLength);
543
+ constBorderValue = &_constBorderValue[0];
544
+ size_t i;
545
+ for(i = 0; i < cn; i++)
546
+ constBorderValue[i] = borderValues[i];
547
+ for(; i < borderLength; i++)
548
+ constBorderValue[i] = constBorderValue[i-cn];
549
+
550
+ if( columnBorderType == BORDER_MODE_CONSTANT )
551
+ {
552
+ _constBorderRow.resize(cn*(ssize.width + ksize.width - 1 + 16));
553
+ constBorderRow = internal::alignPtr(&_constBorderRow[0], 16);
554
+ size_t N = (ssize.width + ksize.width - 1)*cn;
555
+ for( i = 0; i < N; i += borderLength )
556
+ {
557
+ size_t n = std::min( borderLength, N - i );
558
+ for(size_t j = 0; j < n; j++)
559
+ srcRow[i+j] = constBorderValue[j];
560
+ }
561
+ MorphRow<Op>(srcRow, constBorderRow, ssize.width, cn, ksize.width);
562
+ }
563
+ }
564
+
565
+ Size2D wholeSize(ssize.width + borderMargin.left + borderMargin.right,
566
+ ssize.height + borderMargin.top + borderMargin.bottom);
567
+
568
+ ptrdiff_t dx1 = std::max<ptrdiff_t>(anchorX - (ptrdiff_t)borderMargin.left, 0);
569
+ ptrdiff_t dx2 = std::max<ptrdiff_t>((ptrdiff_t)ksize.width - anchorX - 1 - (ptrdiff_t)borderMargin.right, 0);
570
+ // recompute border tables
571
+ if( dx1 > 0 || dx2 > 0 )
572
+ {
573
+ if( rowBorderType == BORDER_MODE_CONSTANT )
574
+ {
575
+ memcpy( srcRow, &constBorderValue[0], dx1*cn );
576
+ memcpy( srcRow + (ssize.width + ksize.width - 1 - dx2)*cn, &constBorderValue[0], dx2*cn );
577
+ }
578
+ else
579
+ {
580
+ ptrdiff_t xofs1 = std::min<ptrdiff_t>(borderMargin.left, anchorX) - borderMargin.left;
581
+
582
+ ptrdiff_t wholeWidth = wholeSize.width;
583
+
584
+ ptrdiff_t i, j;
585
+ for( i = 0; i < dx1; i++ )
586
+ {
587
+ ptrdiff_t p0 = (internal::borderInterpolate(i-dx1, wholeWidth, rowBorderType) + xofs1)*cn;
588
+ for( j = 0; j < (ptrdiff_t)cn; j++ )
589
+ borderTab[i*cn + j] = p0 + j;
590
+ }
591
+
592
+ for( i = 0; i < dx2; i++ )
593
+ {
594
+ ptrdiff_t p0 = (internal::borderInterpolate(wholeWidth + i, wholeWidth, rowBorderType) + xofs1)*cn;
595
+ for( j = 0; j < (ptrdiff_t)cn; j++ )
596
+ borderTab[(i + dx1)*cn + j] = p0 + j;
597
+ }
598
+ }
599
+ }
600
+
601
+ ptrdiff_t startY, startY0, endY, rowCount;
602
+ startY = startY0 = std::max<ptrdiff_t>(borderMargin.top - anchorY, 0);
603
+ endY = std::min<ptrdiff_t>(borderMargin.top + ssize.height + ksize.height - anchorY - 1, wholeSize.height);
604
+
605
+ const u8* src = srcBase + (startY - borderMargin.top)*srcStride;
606
+ u8* dst = dstBase;
607
+
608
+ ptrdiff_t width = ssize.width, kwidth = ksize.width;
609
+ ptrdiff_t kheight = ksize.height, ay = anchorY;
610
+ ptrdiff_t width1 = ssize.width + kwidth - 1;
611
+ ptrdiff_t xofs1 = std::min<ptrdiff_t>(borderMargin.left, anchorX);
612
+ bool makeBorder = (dx1 > 0 || dx2 > 0) && rowBorderType != BORDER_MODE_CONSTANT;
613
+ ptrdiff_t dy = 0, i = 0;
614
+
615
+ src -= xofs1*cn;
616
+ ptrdiff_t count = endY - startY;
617
+
618
+ rowCount = 0;
619
+ for(;; dst += dstStride*i, dy += i)
620
+ {
621
+ ptrdiff_t dcount = bufRows - ay - startY - rowCount + borderMargin.top;
622
+ dcount = dcount > 0 ? dcount : bufRows - kheight + 1;
623
+ dcount = std::min(dcount, count);
624
+ count -= dcount;
625
+ for( ; dcount-- > 0; src += srcStride )
626
+ {
627
+ ptrdiff_t bi = (startY - startY0 + rowCount) % bufRows;
628
+ u8* brow = ringBuf + bi*swidthcn;
629
+
630
+ if( (size_t)(++rowCount) > bufRows )
631
+ {
632
+ --rowCount;
633
+ ++startY;
634
+ }
635
+
636
+ memcpy( srcRow + dx1*cn, src, (width1 - dx2 - dx1)*cn );
637
+
638
+ if( makeBorder )
639
+ {
640
+ for( i = 0; i < (ptrdiff_t)(dx1*cn); i++ )
641
+ srcRow[i] = src[borderTab[i]];
642
+ for( i = 0; i < (ptrdiff_t)(dx2*cn); i++ )
643
+ srcRow[i + (width1 - dx2)*cn] = src[borderTab[i+dx1*cn]];
644
+ }
645
+
646
+ MorphRow<Op>(srcRow, brow, width, cn, ksize.width);
647
+ }
648
+
649
+ ptrdiff_t max_i = std::min<ptrdiff_t>(bufRows, ssize.height - dy + (kheight - 1));
650
+ for( i = 0; i < max_i; i++ )
651
+ {
652
+ ptrdiff_t srcY = internal::borderInterpolate(dy + i + borderMargin.top - ay,
653
+ wholeSize.height, columnBorderType);
654
+ if( srcY < 0 ) // can happen only with constant border type
655
+ rows[i] = constBorderRow;
656
+ else
657
+ {
658
+ if( srcY >= startY + rowCount )
659
+ break;
660
+ ptrdiff_t bi = (srcY - startY0) % bufRows;
661
+ rows[i] = ringBuf + bi*swidthcn;
662
+ }
663
+ }
664
+ if( i < kheight )
665
+ break;
666
+ i -= kheight - 1;
667
+ MorphColumn<Op>((const u8**)rows, dst, dstStride, i, ssize.width*cn, ksize.height);
668
+ }
669
+ }
670
+
671
+ } // namespace
672
+ #endif // CAROTENE_NEON
673
+
674
+ void erode(const Size2D &ssize, u32 cn,
675
+ const u8 * srcBase, ptrdiff_t srcStride,
676
+ u8 * dstBase, ptrdiff_t dstStride,
677
+ const Size2D &ksize,
678
+ size_t anchorX, size_t anchorY,
679
+ BORDER_MODE rowBorderType, BORDER_MODE columnBorderType,
680
+ const u8 * borderValues, Margin borderMargin)
681
+ {
682
+ internal::assertSupportedConfiguration(ssize.width > 0 && ssize.height > 0 &&
683
+ anchorX < ksize.width && anchorY < ksize.height);
684
+ #ifdef CAROTENE_NEON
685
+ morphology<ErodeVecOp>(ssize, cn, srcBase, srcStride, dstBase, dstStride,
686
+ ksize, anchorX, anchorY, rowBorderType, columnBorderType,
687
+ borderValues, borderMargin);
688
+ #else
689
+ (void)cn;
690
+ (void)srcBase;
691
+ (void)srcStride;
692
+ (void)dstBase;
693
+ (void)dstStride;
694
+ (void)rowBorderType;
695
+ (void)columnBorderType;
696
+ (void)borderValues;
697
+ (void)borderMargin;
698
+ #endif
699
+ }
700
+
701
+ void dilate(const Size2D &ssize, u32 cn,
702
+ const u8 * srcBase, ptrdiff_t srcStride,
703
+ u8 * dstBase, ptrdiff_t dstStride,
704
+ const Size2D &ksize,
705
+ size_t anchorX, size_t anchorY,
706
+ BORDER_MODE rowBorderType, BORDER_MODE columnBorderType,
707
+ const u8 * borderValues, Margin borderMargin)
708
+ {
709
+ internal::assertSupportedConfiguration(ssize.width > 0 && ssize.height > 0 &&
710
+ anchorX < ksize.width && anchorY < ksize.height);
711
+ #ifdef CAROTENE_NEON
712
+ morphology<DilateVecOp>(ssize, cn, srcBase, srcStride, dstBase, dstStride,
713
+ ksize, anchorX, anchorY, rowBorderType, columnBorderType,
714
+ borderValues, borderMargin);
715
+ #else
716
+ (void)cn;
717
+ (void)srcBase;
718
+ (void)srcStride;
719
+ (void)dstBase;
720
+ (void)dstStride;
721
+ (void)rowBorderType;
722
+ (void)columnBorderType;
723
+ (void)borderValues;
724
+ (void)borderMargin;
725
+ #endif
726
+ }
727
+
728
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/mul.cpp ADDED
@@ -0,0 +1,1572 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2014-2016, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "vtransform.hpp"
42
+
43
+ #include <cstring>
44
+ #include <cfloat>
45
+ #include <cmath>
46
+ #include <limits>
47
+
48
+ namespace CAROTENE_NS {
49
+
50
+ #ifdef CAROTENE_NEON
51
+
52
+ namespace {
53
+
54
+ bool isIntegerScale(f32 scale)
55
+ {
56
+ return std::fabs(scale - static_cast<s32>(scale)) < FLT_EPSILON;
57
+ }
58
+
59
+ template <s32 shift>
60
+ void mulu8(const Size2D &size,
61
+ const u8 * src0Base, ptrdiff_t src0Stride,
62
+ const u8 * src1Base, ptrdiff_t src1Stride,
63
+ u8 * dstBase, ptrdiff_t dstStride,
64
+ CONVERT_POLICY cpolicy)
65
+ {
66
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
67
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
68
+
69
+ for (size_t i = 0; i < size.height; ++i)
70
+ {
71
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
72
+ const u8 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
73
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
74
+ size_t j = 0;
75
+
76
+ if (cpolicy == CONVERT_POLICY_SATURATE)
77
+ {
78
+ for (; j < roiw16; j += 16)
79
+ {
80
+ internal::prefetch(src0 + j);
81
+ internal::prefetch(src1 + j);
82
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
83
+
84
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
85
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
86
+
87
+ v_dst0 = vshrq_n_u16(v_dst0, shift);
88
+ v_dst1 = vshrq_n_u16(v_dst1, shift);
89
+
90
+ vst1q_u8(dst + j, vcombine_u8(vqmovn_u16(v_dst0), vqmovn_u16(v_dst1)));
91
+ }
92
+ for (; j < roiw8; j += 8)
93
+ {
94
+ uint16x8_t v_dst = vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j));
95
+ vst1_u8(dst + j, vqmovn_u16(vshrq_n_u16(v_dst, shift)));
96
+ }
97
+
98
+ for (; j < size.width; j++)
99
+ {
100
+ u16 val = (u16)src0[j] * (u16)src1[j];
101
+ dst[j] = internal::saturate_cast<u8>(val >> shift);
102
+ }
103
+ }
104
+ else // CONVERT_POLICY_WRAP
105
+ {
106
+ for (; j < roiw16; j += 16)
107
+ {
108
+ internal::prefetch(src0 + j);
109
+ internal::prefetch(src1 + j);
110
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
111
+
112
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
113
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
114
+
115
+ v_dst0 = vshrq_n_u16(v_dst0, shift);
116
+ v_dst1 = vshrq_n_u16(v_dst1, shift);
117
+
118
+ vst1q_u8(dst + j, vcombine_u8(vmovn_u16(v_dst0), vmovn_u16(v_dst1)));
119
+ }
120
+ for (; j < roiw8; j += 8)
121
+ {
122
+ uint16x8_t v_dst = vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j));
123
+ vst1_u8(dst + j, vmovn_u16(vshrq_n_u16(v_dst, shift)));
124
+ }
125
+
126
+ for (; j < size.width; j++)
127
+ {
128
+ u16 val = (u16)src0[j] * (u16)src1[j];
129
+ dst[j] = (u8)(val >> shift);
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ template <s32 shift>
136
+ void muls16(const Size2D &size,
137
+ const u8 * src0Base, ptrdiff_t src0Stride,
138
+ const u8 * src1Base, ptrdiff_t src1Stride,
139
+ s16 * dstBase, ptrdiff_t dstStride,
140
+ CONVERT_POLICY cpolicy)
141
+ {
142
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
143
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
144
+
145
+ uint16x8_t v_32767 = vdupq_n_u16(0x7FFF);
146
+
147
+ for (size_t i = 0; i < size.height; ++i)
148
+ {
149
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
150
+ const u8 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
151
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
152
+ size_t j = 0;
153
+
154
+ if (cpolicy == CONVERT_POLICY_SATURATE)
155
+ {
156
+ for (; j < roiw16; j += 16)
157
+ {
158
+ internal::prefetch(src0 + j);
159
+ internal::prefetch(src1 + j);
160
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
161
+
162
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
163
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
164
+
165
+ v_dst0 = vshrq_n_u16(v_dst0, shift);
166
+ v_dst1 = vshrq_n_u16(v_dst1, shift);
167
+
168
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(vminq_u16(v_32767, v_dst0)));
169
+ vst1q_s16(dst + j + 8, vreinterpretq_s16_u16(vminq_u16(v_32767, v_dst1)));
170
+ }
171
+ for (; j < roiw8; j += 8)
172
+ {
173
+ uint16x8_t v_dst = vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j));
174
+ v_dst = vshrq_n_u16(v_dst, shift);
175
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(vminq_u16(v_32767, v_dst)));
176
+ }
177
+
178
+ for (; j < size.width; j++)
179
+ {
180
+ u16 val = (u16)src0[j] * (u16)src1[j];
181
+ dst[j] = internal::saturate_cast<s16>(val >> shift);
182
+ }
183
+ }
184
+ else // CONVERT_POLICY_WRAP
185
+ {
186
+ for (; j < roiw16; j += 16)
187
+ {
188
+ internal::prefetch(src0 + j);
189
+ internal::prefetch(src1 + j);
190
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
191
+
192
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
193
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
194
+
195
+ v_dst0 = vshrq_n_u16(v_dst0, shift);
196
+ v_dst1 = vshrq_n_u16(v_dst1, shift);
197
+
198
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(v_dst0));
199
+ vst1q_s16(dst + j + 8, vreinterpretq_s16_u16(v_dst1));
200
+ }
201
+ for (; j < roiw8; j += 8)
202
+ {
203
+ uint16x8_t v_dst = vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j));
204
+ v_dst = vshrq_n_u16(v_dst, shift);
205
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(v_dst));
206
+ }
207
+
208
+ for (; j < size.width; j++)
209
+ {
210
+ u16 val = (u16)src0[j] * (u16)src1[j];
211
+ dst[j] = (s16)(val >> shift);
212
+ }
213
+ }
214
+ }
215
+ }
216
+
217
+ typedef void (* mulFuncu8)(const Size2D &size,
218
+ const u8 * src0Base, ptrdiff_t src0Stride,
219
+ const u8 * src1Base, ptrdiff_t src1Stride,
220
+ u8 * dstBase, ptrdiff_t dstStride,
221
+ CONVERT_POLICY cpolicy);
222
+
223
+ typedef void (* mulFuncs16)(const Size2D &size,
224
+ const u8 * src0Base, ptrdiff_t src0Stride,
225
+ const u8 * src1Base, ptrdiff_t src1Stride,
226
+ s16 * dstBase, ptrdiff_t dstStride,
227
+ CONVERT_POLICY cpolicy);
228
+
229
+ } // namespace
230
+
231
+ #endif
232
+
233
+ void mul(const Size2D &size,
234
+ const u8 * src0Base, ptrdiff_t src0Stride,
235
+ const u8 * src1Base, ptrdiff_t src1Stride,
236
+ u8 * dstBase, ptrdiff_t dstStride,
237
+ f32 scale,
238
+ CONVERT_POLICY cpolicy)
239
+ {
240
+ internal::assertSupportedConfiguration();
241
+
242
+ #ifdef CAROTENE_NEON
243
+ if ((scale * UCHAR_MAX * UCHAR_MAX) < 1.0f)
244
+ {
245
+ for (size_t y = 0; y < size.height; ++y)
246
+ {
247
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, y);
248
+ std::memset(dst, 0, sizeof(u8) * size.width);
249
+ }
250
+ return;
251
+ }
252
+
253
+ s32 iscale = static_cast<s32>(scale), exp = 0;
254
+ f32 significand = frexp(scale, &exp);
255
+ bool is_integer_scale = isIntegerScale(scale),
256
+ is_power_of_2 = (significand == 0.5f) && (exp <= 0);
257
+ exp = -exp + 1;
258
+
259
+ if (is_power_of_2)
260
+ {
261
+ static const mulFuncu8 funcs[16] =
262
+ {
263
+ NULL,
264
+ mulu8<1>,
265
+ mulu8<2>,
266
+ mulu8<3>,
267
+ mulu8<4>,
268
+ mulu8<5>,
269
+ mulu8<6>,
270
+ mulu8<7>,
271
+ mulu8<8>,
272
+ mulu8<9>,
273
+ mulu8<10>,
274
+ mulu8<11>,
275
+ mulu8<12>,
276
+ mulu8<13>,
277
+ mulu8<14>,
278
+ mulu8<15>
279
+ };
280
+
281
+ mulFuncu8 func = funcs[exp];
282
+
283
+ func(size,
284
+ src0Base, src0Stride,
285
+ src1Base, src1Stride,
286
+ dstBase, dstStride,
287
+ cpolicy);
288
+
289
+ return;
290
+ }
291
+
292
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
293
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
294
+
295
+ for (size_t i = 0; i < size.height; ++i)
296
+ {
297
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
298
+ const u8 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
299
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
300
+ size_t j = 0;
301
+
302
+ if (cpolicy == CONVERT_POLICY_SATURATE)
303
+ {
304
+ if (is_integer_scale && iscale == 1)
305
+ {
306
+ for (; j < roiw16; j += 16)
307
+ {
308
+ internal::prefetch(src0 + j);
309
+ internal::prefetch(src1 + j);
310
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
311
+
312
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
313
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
314
+
315
+ vst1q_u8(dst + j, vcombine_u8(vqmovn_u16(v_dst0), vqmovn_u16(v_dst1)));
316
+ }
317
+ for (; j < roiw8; j += 8)
318
+ {
319
+ vst1_u8(dst + j, vqmovn_u16(vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j))));
320
+ }
321
+
322
+ for (; j < size.width; j++)
323
+ {
324
+ u16 val = (u16)src0[j] * (u16)src1[j];
325
+ dst[j] = internal::saturate_cast<u8>(val);
326
+ }
327
+ }
328
+ else // generic case using floats
329
+ {
330
+ for (; j < roiw16; j += 16)
331
+ {
332
+ internal::prefetch(src0 + j);
333
+ internal::prefetch(src1 + j);
334
+
335
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
336
+ uint8x16_t v_src1 = vld1q_u8(src1 + j);
337
+
338
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
339
+ uint16x8_t v_src1_p = vmovl_u8(vget_low_u8(v_src1));
340
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
341
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
342
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
343
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
344
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
345
+ v_src1_p = vmovl_u8(vget_high_u8(v_src1));
346
+ float32x4_t v_dst2f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
347
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
348
+ float32x4_t v_dst3f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
349
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
350
+ uint16x8_t v_dst0u = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(v_dst0f)),
351
+ vqmovn_u32(vcvtq_u32_f32(v_dst1f)));
352
+ uint16x8_t v_dst1u = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(v_dst2f)),
353
+ vqmovn_u32(vcvtq_u32_f32(v_dst3f)));
354
+ vst1q_u8(dst + j, vcombine_u8(vqmovn_u16(v_dst0u), vqmovn_u16(v_dst1u)));
355
+ }
356
+ for (; j < roiw8; j += 8)
357
+ {
358
+ uint16x8_t v_src0 = vmovl_u8(vld1_u8(src0 + j));
359
+ uint16x8_t v_src1 = vmovl_u8(vld1_u8(src1 + j));
360
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))),
361
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1)))), scale);
362
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))),
363
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1)))), scale);
364
+ uint16x8_t v_dstu = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(v_dst0f)),
365
+ vqmovn_u32(vcvtq_u32_f32(v_dst1f)));
366
+ vst1_u8(dst + j, vqmovn_u16(v_dstu));
367
+ }
368
+
369
+ for (; j < size.width; j++)
370
+ {
371
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
372
+ dst[j] = internal::saturate_cast<u8>((s32)trunc(fval));
373
+ }
374
+ }
375
+ }
376
+ else // CONVERT_POLICY_WRAP
377
+ {
378
+ if (is_integer_scale && iscale == 1)
379
+ {
380
+ for (; j < roiw16; j += 16)
381
+ {
382
+ internal::prefetch(src0 + j);
383
+ internal::prefetch(src1 + j);
384
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
385
+
386
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
387
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
388
+
389
+ vst1q_u8(dst + j, vcombine_u8(vmovn_u16(v_dst0), vmovn_u16(v_dst1)));
390
+ }
391
+ for (; j < roiw8; j += 8)
392
+ {
393
+ vst1_u8(dst + j, vmovn_u16(vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j))));
394
+ }
395
+
396
+ for (; j < size.width; j++)
397
+ {
398
+ u16 val = (u16)src0[j] * (u16)src1[j];
399
+ dst[j] = (u8)(val);
400
+ }
401
+ }
402
+ else // generic case using floats
403
+ {
404
+ for (; j < roiw16; j += 16)
405
+ {
406
+ internal::prefetch(src0 + j);
407
+ internal::prefetch(src1 + j);
408
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
409
+ uint8x16_t v_src1 = vld1q_u8(src1 + j);
410
+
411
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
412
+ uint16x8_t v_src1_p = vmovl_u8(vget_low_u8(v_src1));
413
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
414
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
415
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
416
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
417
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
418
+ v_src1_p = vmovl_u8(vget_high_u8(v_src1));
419
+ float32x4_t v_dst2f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
420
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
421
+ float32x4_t v_dst3f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
422
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
423
+ uint16x8_t v_dst0u = vcombine_u16(vmovn_u32(vcvtq_u32_f32(v_dst0f)),
424
+ vmovn_u32(vcvtq_u32_f32(v_dst1f)));
425
+ uint16x8_t v_dst1u = vcombine_u16(vmovn_u32(vcvtq_u32_f32(v_dst2f)),
426
+ vmovn_u32(vcvtq_u32_f32(v_dst3f)));
427
+ vst1q_u8(dst + j, vcombine_u8(vmovn_u16(v_dst0u), vmovn_u16(v_dst1u)));
428
+ }
429
+ for (; j < roiw8; j += 8)
430
+ {
431
+ uint16x8_t v_src0 = vmovl_u8(vld1_u8(src0 + j));
432
+ uint16x8_t v_src1 = vmovl_u8(vld1_u8(src1 + j));
433
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))),
434
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1)))), scale);
435
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))),
436
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1)))), scale);
437
+ uint16x8_t v_dstu = vcombine_u16(vmovn_u32(vcvtq_u32_f32(v_dst0f)),
438
+ vmovn_u32(vcvtq_u32_f32(v_dst1f)));
439
+ vst1_u8(dst + j, vmovn_u16(v_dstu));
440
+ }
441
+
442
+ for (; j < size.width; j++)
443
+ {
444
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
445
+ dst[j] = (u8)(s32)trunc(fval);
446
+ }
447
+ }
448
+ }
449
+ }
450
+ #else
451
+ (void)size;
452
+ (void)src0Base;
453
+ (void)src0Stride;
454
+ (void)src1Base;
455
+ (void)src1Stride;
456
+ (void)dstBase;
457
+ (void)dstStride;
458
+ (void)cpolicy;
459
+ (void)scale;
460
+ #endif
461
+ }
462
+
463
+ void mul(const Size2D &size,
464
+ const u8 * src0Base, ptrdiff_t src0Stride,
465
+ const u8 * src1Base, ptrdiff_t src1Stride,
466
+ s16 * dstBase, ptrdiff_t dstStride,
467
+ f32 scale,
468
+ CONVERT_POLICY cpolicy)
469
+ {
470
+ internal::assertSupportedConfiguration();
471
+ #ifdef CAROTENE_NEON
472
+ if (((scale * UCHAR_MAX * UCHAR_MAX) < 1.0f) && (scale >= 0))
473
+ {
474
+ for (size_t y = 0; y < size.height; ++y)
475
+ {
476
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, y);
477
+ std::memset(dst, 0, sizeof(s16) * size.width);
478
+ }
479
+ return;
480
+ }
481
+
482
+ s32 iscale = static_cast<s32>(scale), exp = 0;
483
+ f32 significand = frexp(scale, &exp);
484
+ bool is_integer_scale = isIntegerScale(scale),
485
+ is_power_of_2 = (significand == 0.5f) && (exp <= 0);
486
+ exp = -exp + 1;
487
+
488
+ if (is_power_of_2)
489
+ {
490
+ static const mulFuncs16 funcs[16] =
491
+ {
492
+ NULL,
493
+ muls16<1>,
494
+ muls16<2>,
495
+ muls16<3>,
496
+ muls16<4>,
497
+ muls16<5>,
498
+ muls16<6>,
499
+ muls16<7>,
500
+ muls16<8>,
501
+ muls16<9>,
502
+ muls16<10>,
503
+ muls16<11>,
504
+ muls16<12>,
505
+ muls16<13>,
506
+ muls16<14>,
507
+ muls16<15>
508
+ };
509
+
510
+ mulFuncs16 func = funcs[exp];
511
+
512
+ func(size,
513
+ src0Base, src0Stride,
514
+ src1Base, src1Stride,
515
+ dstBase, dstStride,
516
+ cpolicy);
517
+
518
+ return;
519
+ }
520
+
521
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
522
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
523
+
524
+ uint16x8_t v_32767 = vdupq_n_u16(0x7FFF);
525
+
526
+ for (size_t i = 0; i < size.height; ++i)
527
+ {
528
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
529
+ const u8 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
530
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
531
+ size_t j = 0;
532
+
533
+ if (cpolicy == CONVERT_POLICY_SATURATE)
534
+ {
535
+ if (is_integer_scale && iscale == 1)
536
+ {
537
+ for (; j < roiw16; j += 16)
538
+ {
539
+ internal::prefetch(src0 + j);
540
+ internal::prefetch(src1 + j);
541
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
542
+
543
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
544
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
545
+
546
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(vminq_u16(v_32767, v_dst0)));
547
+ vst1q_s16(dst + j +8, vreinterpretq_s16_u16(vminq_u16(v_32767, v_dst1)));
548
+ }
549
+ for (; j < roiw8; j += 8)
550
+ {
551
+ uint16x8_t v_dst = vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j));
552
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(vminq_u16(v_32767, v_dst)));
553
+ }
554
+
555
+ for (; j < size.width; j++)
556
+ {
557
+ u16 val = (u16)src0[j] * (u16)src1[j];
558
+ dst[j] = internal::saturate_cast<s16>(val);
559
+ }
560
+ }
561
+ else // generic case using floats
562
+ {
563
+ for (; j < roiw16; j += 16)
564
+ {
565
+ internal::prefetch(src0 + j);
566
+ internal::prefetch(src1 + j);
567
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
568
+ uint8x16_t v_src1 = vld1q_u8(src1 + j);
569
+
570
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
571
+ uint16x8_t v_src1_p = vmovl_u8(vget_low_u8(v_src1));
572
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
573
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
574
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
575
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
576
+ vst1q_s16(dst + j, vcombine_s16(vqmovn_s32(vcvtq_s32_f32(v_dst0f)),
577
+ vqmovn_s32(vcvtq_s32_f32(v_dst1f))));
578
+
579
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
580
+ v_src1_p = vmovl_u8(vget_high_u8(v_src1));
581
+ v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
582
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
583
+ v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
584
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
585
+ vst1q_s16(dst + j + 8, vcombine_s16(vqmovn_s32(vcvtq_s32_f32(v_dst0f)),
586
+ vqmovn_s32(vcvtq_s32_f32(v_dst1f))));
587
+ }
588
+ for (; j < roiw8; j += 8)
589
+ {
590
+ uint16x8_t v_src0 = vmovl_u8(vld1_u8(src0 + j));
591
+ uint16x8_t v_src1 = vmovl_u8(vld1_u8(src1 + j));
592
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))),
593
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1)))), scale);
594
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))),
595
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1)))), scale);
596
+ vst1q_s16(dst + j, vcombine_s16(vqmovn_s32(vcvtq_s32_f32(v_dst0f)),
597
+ vqmovn_s32(vcvtq_s32_f32(v_dst1f))));
598
+ }
599
+
600
+ for (; j < size.width; j++)
601
+ {
602
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
603
+ dst[j] = internal::saturate_cast<s16>((s32)trunc(fval));
604
+ }
605
+ }
606
+ }
607
+ else // CONVERT_POLICY_WRAP
608
+ {
609
+ if (is_integer_scale && iscale == 1)
610
+ {
611
+ for (; j < roiw16; j += 16)
612
+ {
613
+ internal::prefetch(src0 + j);
614
+ internal::prefetch(src1 + j);
615
+ uint8x16_t v_src0 = vld1q_u8(src0 + j), v_src1 = vld1q_u8(src1 + j);
616
+
617
+ uint16x8_t v_dst0 = vmull_u8(vget_low_u8(v_src0), vget_low_u8(v_src1));
618
+ uint16x8_t v_dst1 = vmull_u8(vget_high_u8(v_src0), vget_high_u8(v_src1));
619
+
620
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(v_dst0));
621
+ vst1q_s16(dst + j + 8, vreinterpretq_s16_u16(v_dst1));
622
+ }
623
+ for (; j < roiw8; j += 8)
624
+ {
625
+ uint16x8_t v_dst = vmull_u8(vld1_u8(src0 + j), vld1_u8(src1 + j));
626
+ vst1q_s16(dst + j, vreinterpretq_s16_u16(v_dst));
627
+ }
628
+
629
+ for (; j < size.width; j++)
630
+ {
631
+ u16 val = (u16)src0[j] * (u16)src1[j];
632
+ dst[j] = (s16)(val);
633
+ }
634
+ }
635
+ else // generic case using floats
636
+ {
637
+ for (; j < roiw16; j += 16)
638
+ {
639
+ internal::prefetch(src0 + j);
640
+ internal::prefetch(src1 + j);
641
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
642
+ uint8x16_t v_src1 = vld1q_u8(src1 + j);
643
+
644
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
645
+ uint16x8_t v_src1_p = vmovl_u8(vget_low_u8(v_src1));
646
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
647
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
648
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
649
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
650
+ vst1q_s16(dst + j, vcombine_s16(vmovn_s32(vcvtq_s32_f32(v_dst0f)),
651
+ vmovn_s32(vcvtq_s32_f32(v_dst1f))));
652
+
653
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
654
+ v_src1_p = vmovl_u8(vget_high_u8(v_src1));
655
+ v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
656
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1_p)))), scale);
657
+ v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
658
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1_p)))), scale);
659
+ vst1q_s16(dst + j + 8, vcombine_s16(vmovn_s32(vcvtq_s32_f32(v_dst0f)),
660
+ vmovn_s32(vcvtq_s32_f32(v_dst1f))));
661
+ }
662
+ for (; j < roiw8; j += 8)
663
+ {
664
+ uint16x8_t v_src0 = vmovl_u8(vld1_u8(src0 + j));
665
+ uint16x8_t v_src1 = vmovl_u8(vld1_u8(src1 + j));
666
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))),
667
+ vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src1)))), scale);
668
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))),
669
+ vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src1)))), scale);
670
+ vst1q_s16(dst + j, vcombine_s16(vmovn_s32(vcvtq_s32_f32(v_dst0f)),
671
+ vmovn_s32(vcvtq_s32_f32(v_dst1f))));
672
+ }
673
+
674
+ for (; j < size.width; j++)
675
+ {
676
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
677
+ dst[j] = (s16)(s32)trunc(fval);
678
+ }
679
+ }
680
+ }
681
+ }
682
+ #else
683
+ (void)size;
684
+ (void)src0Base;
685
+ (void)src0Stride;
686
+ (void)src1Base;
687
+ (void)src1Stride;
688
+ (void)dstBase;
689
+ (void)dstStride;
690
+ (void)cpolicy;
691
+ (void)scale;
692
+ #endif
693
+ }
694
+
695
+ void mul(const Size2D &size,
696
+ const u8 * src0Base, ptrdiff_t src0Stride,
697
+ const s16 * src1Base, ptrdiff_t src1Stride,
698
+ s16 * dstBase, ptrdiff_t dstStride,
699
+ f32 scale,
700
+ CONVERT_POLICY cpolicy)
701
+ {
702
+ internal::assertSupportedConfiguration();
703
+
704
+ #ifdef CAROTENE_NEON
705
+ if (scale == 0.0f)
706
+ {
707
+ for (size_t y = 0; y < size.height; ++y)
708
+ {
709
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, y);
710
+ std::memset(dst, 0, sizeof(s16) * size.width);
711
+ }
712
+ return;
713
+ }
714
+
715
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
716
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
717
+
718
+ bool is_integer_scale = isIntegerScale(scale);
719
+ s32 iscale = static_cast<s32>(scale);
720
+
721
+ for (size_t i = 0; i < size.height; ++i)
722
+ {
723
+ const u8 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
724
+ const s16 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
725
+ s16 * dst = internal::getRowPtr(dstBase, dstStride, i);
726
+ size_t j = 0;
727
+
728
+ if (cpolicy == CONVERT_POLICY_SATURATE)
729
+ {
730
+ if (is_integer_scale && iscale == 1)
731
+ {
732
+ for (; j < roiw16; j += 16)
733
+ {
734
+ internal::prefetch(src0 + j);
735
+ internal::prefetch(src1 + j);
736
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
737
+
738
+ int16x8_t v_src0_p = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src0)));
739
+ int16x8_t v_src1_p = vld1q_s16(src1 + j);
740
+ int16x8_t v_dst = vcombine_s16(vqmovn_s32(vmull_s16(vget_low_s16(v_src0_p), vget_low_s16(v_src1_p))),
741
+ vqmovn_s32(vmull_s16(vget_high_s16(v_src0_p), vget_high_s16(v_src1_p))));
742
+ vst1q_s16(dst + j, v_dst);
743
+
744
+ v_src0_p = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src0)));
745
+ v_src1_p = vld1q_s16(src1 + j + 8);
746
+ v_dst = vcombine_s16(vqmovn_s32(vmull_s16(vget_low_s16(v_src0_p), vget_low_s16(v_src1_p))),
747
+ vqmovn_s32(vmull_s16(vget_high_s16(v_src0_p), vget_high_s16(v_src1_p))));
748
+ vst1q_s16(dst + j + 8, v_dst);
749
+ }
750
+ for (; j < roiw8; j += 8)
751
+ {
752
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vld1q_u8(src0 + j))));
753
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
754
+ int16x8_t v_dst = vcombine_s16(vqmovn_s32(vmull_s16(vget_low_s16(v_src0), vget_low_s16(v_src1))),
755
+ vqmovn_s32(vmull_s16(vget_high_s16(v_src0), vget_high_s16(v_src1))));
756
+ vst1q_s16(dst + j, v_dst);
757
+ }
758
+
759
+ for (; j < size.width; j++)
760
+ {
761
+ s32 val = (s32)src0[j] * (s32)src1[j];
762
+ dst[j] = internal::saturate_cast<s16>(val);
763
+ }
764
+ }
765
+ else // generic case using floats
766
+ {
767
+ for (; j < roiw16; j += 16)
768
+ {
769
+ internal::prefetch(src0 + j);
770
+ internal::prefetch(src1 + j);
771
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
772
+
773
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
774
+ int16x8_t v_src1_p = vld1q_s16(src1 + j);
775
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
776
+ vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1_p)))), scale);
777
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
778
+ vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1_p)))), scale);
779
+ vst1q_s16(dst + j, vcombine_s16(vqmovn_s32(vcvtq_s32_f32(v_dst0f)),
780
+ vqmovn_s32(vcvtq_s32_f32(v_dst1f))));
781
+
782
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
783
+ v_src1_p = vld1q_s16(src1 + j + 8);
784
+ v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
785
+ vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1_p)))), scale);
786
+ v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
787
+ vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1_p)))), scale);
788
+ vst1q_s16(dst + j + 8, vcombine_s16(vqmovn_s32(vcvtq_s32_f32(v_dst0f)),
789
+ vqmovn_s32(vcvtq_s32_f32(v_dst1f))));
790
+ }
791
+ for (; j < roiw8; j += 8)
792
+ {
793
+ uint16x8_t v_src0 = vmovl_u8(vld1_u8(src0 + j));
794
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
795
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))),
796
+ vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1)))), scale);
797
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))),
798
+ vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1)))), scale);
799
+ vst1q_s16(dst + j, vcombine_s16(vqmovn_s32(vcvtq_s32_f32(v_dst0f)),
800
+ vqmovn_s32(vcvtq_s32_f32(v_dst1f))));
801
+ }
802
+
803
+ for (; j < size.width; j++)
804
+ {
805
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
806
+ dst[j] = internal::saturate_cast<s16>((s32)trunc(fval));
807
+ }
808
+ }
809
+ }
810
+ else // CONVERT_POLICY_WRAP
811
+ {
812
+ if (is_integer_scale && iscale == 1)
813
+ {
814
+ for (; j < roiw16; j += 16)
815
+ {
816
+ internal::prefetch(src0 + j);
817
+ internal::prefetch(src1 + j);
818
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
819
+
820
+ int16x8_t v_src0_p = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(v_src0)));
821
+ int16x8_t v_src1_p = vld1q_s16(src1 + j);
822
+ int16x8_t v_dst = vcombine_s16(vmovn_s32(vmull_s16(vget_low_s16(v_src0_p), vget_low_s16(v_src1_p))),
823
+ vmovn_s32(vmull_s16(vget_high_s16(v_src0_p), vget_high_s16(v_src1_p))));
824
+ vst1q_s16(dst + j, v_dst);
825
+
826
+ v_src0_p = vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(v_src0)));
827
+ v_src1_p = vld1q_s16(src1 + j + 8);
828
+ v_dst = vcombine_s16(vmovn_s32(vmull_s16(vget_low_s16(v_src0_p), vget_low_s16(v_src1_p))),
829
+ vmovn_s32(vmull_s16(vget_high_s16(v_src0_p), vget_high_s16(v_src1_p))));
830
+ vst1q_s16(dst + j + 8, v_dst);
831
+ }
832
+ for (; j < roiw8; j += 8)
833
+ {
834
+ int16x8_t v_src0 = vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(vld1q_u8(src0 + j))));
835
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
836
+ int16x8_t v_dst = vcombine_s16(vmovn_s32(vmull_s16(vget_low_s16(v_src0), vget_low_s16(v_src1))),
837
+ vmovn_s32(vmull_s16(vget_high_s16(v_src0), vget_high_s16(v_src1))));
838
+ vst1q_s16(dst + j, v_dst);
839
+ }
840
+
841
+ for (; j < size.width; j++)
842
+ {
843
+ s32 val = (s32)src0[j] * (s32)src1[j];
844
+ dst[j] = (s16)(val);
845
+ }
846
+ }
847
+ else // generic case using floats
848
+ {
849
+ for (; j < roiw16; j += 16)
850
+ {
851
+ internal::prefetch(src0 + j);
852
+ internal::prefetch(src1 + j);
853
+ uint8x16_t v_src0 = vld1q_u8(src0 + j);
854
+
855
+ uint16x8_t v_src0_p = vmovl_u8(vget_low_u8(v_src0));
856
+ int16x8_t v_src1_p = vld1q_s16(src1 + j);
857
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
858
+ vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1_p)))), scale);
859
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
860
+ vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1_p)))), scale);
861
+ vst1q_s16(dst + j, vcombine_s16(vmovn_s32(vcvtq_s32_f32(v_dst0f)),
862
+ vmovn_s32(vcvtq_s32_f32(v_dst1f))));
863
+
864
+ v_src0_p = vmovl_u8(vget_high_u8(v_src0));
865
+ v_src1_p = vld1q_s16(src1 + j + 8);
866
+ v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0_p))),
867
+ vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1_p)))), scale);
868
+ v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0_p))),
869
+ vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1_p)))), scale);
870
+ vst1q_s16(dst + j + 8, vcombine_s16(vmovn_s32(vcvtq_s32_f32(v_dst0f)),
871
+ vmovn_s32(vcvtq_s32_f32(v_dst1f))));
872
+ }
873
+ for (; j < roiw8; j += 8)
874
+ {
875
+ uint16x8_t v_src0 = vmovl_u8(vld1_u8(src0 + j));
876
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
877
+ float32x4_t v_dst0f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(v_src0))),
878
+ vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1)))), scale);
879
+ float32x4_t v_dst1f = vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(v_src0))),
880
+ vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1)))), scale);
881
+ vst1q_s16(dst + j, vcombine_s16(vmovn_s32(vcvtq_s32_f32(v_dst0f)),
882
+ vmovn_s32(vcvtq_s32_f32(v_dst1f))));
883
+ }
884
+
885
+ for (; j < size.width; j++)
886
+ {
887
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
888
+ dst[j] = (s16)(s32)trunc(fval);
889
+ }
890
+ }
891
+ }
892
+ }
893
+ #else
894
+ (void)size;
895
+ (void)src0Base;
896
+ (void)src0Stride;
897
+ (void)src1Base;
898
+ (void)src1Stride;
899
+ (void)dstBase;
900
+ (void)dstStride;
901
+ (void)cpolicy;
902
+ (void)scale;
903
+ #endif
904
+ }
905
+
906
+ namespace {
907
+
908
+ #ifdef CAROTENE_NEON
909
+
910
+ template <typename T>
911
+ inline T mulSaturateQ(const T &v1, const T &v2, const float scale)
912
+ {
913
+ return internal::vcombine(internal::vqmovn(mulSaturateQ(internal::vmovl(internal::vget_low(v1)),
914
+ internal::vmovl(internal::vget_low(v2)), scale)),
915
+ internal::vqmovn(mulSaturateQ(internal::vmovl(internal::vget_high(v1)),
916
+ internal::vmovl(internal::vget_high(v2)), scale))
917
+ );
918
+ }
919
+ template <>
920
+ inline int32x4_t mulSaturateQ<int32x4_t>(const int32x4_t &v1, const int32x4_t &v2, const float scale)
921
+ { return vcvtq_s32_f32(vmulq_n_f32(vmulq_f32(vcvtq_f32_s32(v1), vcvtq_f32_s32(v2)), scale)); }
922
+ template <>
923
+ inline uint32x4_t mulSaturateQ<uint32x4_t>(const uint32x4_t &v1, const uint32x4_t &v2, const float scale)
924
+ { return vcvtq_u32_f32(vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(v1), vcvtq_f32_u32(v2)), scale)); }
925
+
926
+ template <typename T>
927
+ inline T mulSaturate(const T &v1, const T &v2, const float scale)
928
+ {
929
+ return internal::vqmovn(mulSaturateQ(internal::vmovl(v1), internal::vmovl(v2), scale));
930
+ }
931
+ template <>
932
+ inline int32x2_t mulSaturate<int32x2_t>(const int32x2_t &v1, const int32x2_t &v2, const float scale)
933
+ { return vcvt_s32_f32(vmul_n_f32(vmul_f32(vcvt_f32_s32(v1), vcvt_f32_s32(v2)), scale)); }
934
+ template <>
935
+ inline uint32x2_t mulSaturate<uint32x2_t>(const uint32x2_t &v1, const uint32x2_t &v2, const float scale)
936
+ { return vcvt_u32_f32(vmul_n_f32(vmul_f32(vcvt_f32_u32(v1), vcvt_f32_u32(v2)), scale)); }
937
+
938
+
939
+ template <typename T>
940
+ inline T mulWrapQ(const T &v1, const T &v2, const float scale)
941
+ {
942
+ return internal::vcombine(internal::vmovn(mulWrapQ(internal::vmovl(internal::vget_low(v1)),
943
+ internal::vmovl(internal::vget_low(v2)), scale)),
944
+ internal::vmovn(mulWrapQ(internal::vmovl(internal::vget_high(v1)),
945
+ internal::vmovl(internal::vget_high(v2)), scale))
946
+ );
947
+ }
948
+ template <>
949
+ inline int32x4_t mulWrapQ<int32x4_t>(const int32x4_t &v1, const int32x4_t &v2, const float scale)
950
+ { return vcvtq_s32_f32(vmulq_n_f32(vmulq_f32(vcvtq_f32_s32(v1), vcvtq_f32_s32(v2)), scale)); }
951
+ template <>
952
+ inline uint32x4_t mulWrapQ<uint32x4_t>(const uint32x4_t &v1, const uint32x4_t &v2, const float scale)
953
+ { return vcvtq_u32_f32(vmulq_n_f32(vmulq_f32(vcvtq_f32_u32(v1), vcvtq_f32_u32(v2)), scale)); }
954
+
955
+ template <typename T>
956
+ inline T mulWrap(const T &v1, const T &v2, const float scale)
957
+ {
958
+ return internal::vmovn(mulWrapQ(internal::vmovl(v1), internal::vmovl(v2), scale));
959
+ }
960
+ template <>
961
+ inline int32x2_t mulWrap<int32x2_t>(const int32x2_t &v1, const int32x2_t &v2, const float scale)
962
+ { return vcvt_s32_f32(vmul_n_f32(vmul_f32(vcvt_f32_s32(v1), vcvt_f32_s32(v2)), scale)); }
963
+ template <>
964
+ inline uint32x2_t mulWrap<uint32x2_t>(const uint32x2_t &v1, const uint32x2_t &v2, const float scale)
965
+ { return vcvt_u32_f32(vmul_n_f32(vmul_f32(vcvt_f32_u32(v1), vcvt_f32_u32(v2)), scale)); }
966
+
967
+
968
+ template <int n> inline uint8x16_t vshrq_n(const uint8x16_t & v0) { return vshrq_n_u8 (v0, n); }
969
+ template <int n> inline int8x16_t vshrq_n(const int8x16_t & v0) { return vshrq_n_s8 (v0, n); }
970
+ template <int n> inline uint16x8_t vshrq_n(const uint16x8_t & v0) { return vshrq_n_u16(v0, n); }
971
+ template <int n> inline int16x8_t vshrq_n(const int16x8_t & v0) { return vshrq_n_s16(v0, n); }
972
+ template <int n> inline uint32x4_t vshrq_n(const uint32x4_t & v0) { return vshrq_n_u32(v0, n); }
973
+ template <int n> inline int32x4_t vshrq_n(const int32x4_t & v0) { return vshrq_n_s32(v0, n); }
974
+ template <int n> inline uint64x2_t vshrq_n(const uint64x2_t & v0) { return vshrq_n_u64(v0, n); }
975
+ template <int n> inline int64x2_t vshrq_n(const int64x2_t & v0) { return vshrq_n_s64(v0, n); }
976
+
977
+ template <int n> inline uint8x8_t vshr_n(const uint8x8_t & v0) { return vshr_n_u8 (v0, n); }
978
+ template <int n> inline int8x8_t vshr_n(const int8x8_t & v0) { return vshr_n_s8 (v0, n); }
979
+ template <int n> inline uint16x4_t vshr_n(const uint16x4_t & v0) { return vshr_n_u16(v0, n); }
980
+ template <int n> inline int16x4_t vshr_n(const int16x4_t & v0) { return vshr_n_s16(v0, n); }
981
+ template <int n> inline uint32x2_t vshr_n(const uint32x2_t & v0) { return vshr_n_u32(v0, n); }
982
+ template <int n> inline int32x2_t vshr_n(const int32x2_t & v0) { return vshr_n_s32(v0, n); }
983
+ template <int n> inline uint64x1_t vshr_n(const uint64x1_t & v0) { return vshr_n_u64(v0, n); }
984
+ template <int n> inline int64x1_t vshr_n(const int64x1_t & v0) { return vshr_n_s64(v0, n); }
985
+
986
+ template <int n> inline uint8x16_t vrshrq_n(const uint8x16_t & v0) { return vrshrq_n_u8 (v0, n); }
987
+ template <int n> inline int8x16_t vrshrq_n(const int8x16_t & v0) { return vrshrq_n_s8 (v0, n); }
988
+ template <int n> inline uint16x8_t vrshrq_n(const uint16x8_t & v0) { return vrshrq_n_u16(v0, n); }
989
+ template <int n> inline int16x8_t vrshrq_n(const int16x8_t & v0) { return vrshrq_n_s16(v0, n); }
990
+ template <int n> inline uint32x4_t vrshrq_n(const uint32x4_t & v0) { return vrshrq_n_u32(v0, n); }
991
+ template <int n> inline int32x4_t vrshrq_n(const int32x4_t & v0) { return vrshrq_n_s32(v0, n); }
992
+ template <int n> inline uint64x2_t vrshrq_n(const uint64x2_t & v0) { return vrshrq_n_u64(v0, n); }
993
+ template <int n> inline int64x2_t vrshrq_n(const int64x2_t & v0) { return vrshrq_n_s64(v0, n); }
994
+
995
+ template <int n> inline uint8x8_t vrshr_n(const uint8x8_t & v0) { return vrshr_n_u8 (v0, n); }
996
+ template <int n> inline int8x8_t vrshr_n(const int8x8_t & v0) { return vrshr_n_s8 (v0, n); }
997
+ template <int n> inline uint16x4_t vrshr_n(const uint16x4_t & v0) { return vrshr_n_u16(v0, n); }
998
+ template <int n> inline int16x4_t vrshr_n(const int16x4_t & v0) { return vrshr_n_s16(v0, n); }
999
+ template <int n> inline uint32x2_t vrshr_n(const uint32x2_t & v0) { return vrshr_n_u32(v0, n); }
1000
+ template <int n> inline int32x2_t vrshr_n(const int32x2_t & v0) { return vrshr_n_s32(v0, n); }
1001
+ template <int n> inline uint64x1_t vrshr_n(const uint64x1_t & v0) { return vrshr_n_u64(v0, n); }
1002
+ template <int n> inline int64x1_t vrshr_n(const int64x1_t & v0) { return vrshr_n_s64(v0, n); }
1003
+
1004
+ template <typename T, typename WT, s32 shift>
1005
+ void mulShift(const Size2D &size,
1006
+ const T * src0Base, ptrdiff_t src0Stride,
1007
+ const T * src1Base, ptrdiff_t src1Stride,
1008
+ T * dstBase, ptrdiff_t dstStride,
1009
+ CONVERT_POLICY cpolicy)
1010
+ {
1011
+ typedef typename internal::VecTraits<T>::vec128 vec128;
1012
+ typedef typename internal::VecTraits<WT>::vec128 wvec128;
1013
+ typedef typename internal::VecTraits<T>::vec64 vec64;
1014
+ const size_t step128 = 16 / sizeof(T);
1015
+ size_t roiw128 = size.width >= (step128 - 1) ? size.width - step128 + 1 : 0;
1016
+ const size_t step64 = 8 / sizeof(T);
1017
+ size_t roiw64 = size.width >= (step64 - 1) ? size.width - step64 + 1 : 0;
1018
+
1019
+ wvec128 v_mask = internal::vdupq_n((WT)(1<<shift));
1020
+
1021
+ for (size_t i = 0; i < size.height; ++i)
1022
+ {
1023
+ const T * src0 = internal::getRowPtr(src0Base, src0Stride, i);
1024
+ const T * src1 = internal::getRowPtr(src1Base, src1Stride, i);
1025
+ T * dst = internal::getRowPtr(dstBase, dstStride, i);
1026
+ size_t j = 0;
1027
+
1028
+ if (cpolicy == CONVERT_POLICY_SATURATE)
1029
+ {
1030
+ for (; j < roiw128; j += step128)
1031
+ {
1032
+ internal::prefetch(src0 + j);
1033
+ internal::prefetch(src1 + j);
1034
+ vec128 v_src0 = internal::vld1q(src0 + j), v_src1 = internal::vld1q(src1 + j);
1035
+ wvec128 v_mul0 = internal::vmull( internal::vget_low(v_src0), internal::vget_low(v_src1));
1036
+ wvec128 v_mul1 = internal::vmull(internal::vget_high(v_src0), internal::vget_high(v_src1));
1037
+
1038
+ vec64 v_res0 = internal::vqmovn(vrshrq_n<shift>(internal::vqsubq(v_mul0, vshrq_n<shift>(internal::vbicq(v_mask, v_mul0)) )));
1039
+ vec64 v_res1 = internal::vqmovn(vrshrq_n<shift>(internal::vqsubq(v_mul1, vshrq_n<shift>(internal::vbicq(v_mask, v_mul1)) )));
1040
+
1041
+ internal::vst1q(dst + j, internal::vcombine(v_res0, v_res1));
1042
+ }
1043
+ for (; j < roiw64; j += step64)
1044
+ {
1045
+ wvec128 v_mul = internal::vmull(internal::vld1(src0 + j), internal::vld1(src1 + j));
1046
+ vec64 v_res = internal::vqmovn(vrshrq_n<shift>(internal::vqsubq(v_mul, vshrq_n<shift>(internal::vbicq(v_mask, v_mul)) )));
1047
+ internal::vst1(dst + j, v_res);
1048
+ }
1049
+
1050
+ for (; j < size.width; j++)
1051
+ {
1052
+ WT val = (WT)src0[j] * (WT)src1[j];
1053
+ dst[j] = internal::saturate_cast<T>((val - (((1<<shift) & ~val) >> shift) + (1<<(shift-1))) >> shift);
1054
+ }
1055
+ }
1056
+ else // CONVERT_POLICY_WRAP
1057
+ {
1058
+ for (; j < roiw128; j += step128)
1059
+ {
1060
+ internal::prefetch(src0 + j);
1061
+ internal::prefetch(src1 + j);
1062
+ vec128 v_src0 = internal::vld1q(src0 + j), v_src1 = internal::vld1q(src1 + j);
1063
+ wvec128 v_mul0 = internal::vmull( internal::vget_low(v_src0), internal::vget_low(v_src1));
1064
+ wvec128 v_mul1 = internal::vmull(internal::vget_high(v_src0), internal::vget_high(v_src1));
1065
+
1066
+ vec64 v_res0 = internal::vmovn(vrshrq_n<shift>(internal::vqsubq(v_mul0, vshrq_n<shift>(internal::vbicq(v_mask, v_mul0)) )));
1067
+ vec64 v_res1 = internal::vmovn(vrshrq_n<shift>(internal::vqsubq(v_mul1, vshrq_n<shift>(internal::vbicq(v_mask, v_mul1)) )));
1068
+
1069
+ internal::vst1q(dst + j, internal::vcombine(v_res0, v_res1));
1070
+ }
1071
+ for (; j < roiw64; j += step64)
1072
+ {
1073
+ wvec128 v_mul = internal::vmull(internal::vld1(src0 + j), internal::vld1(src1 + j));
1074
+ vec64 v_res = internal::vmovn(vrshrq_n<shift>(internal::vqsubq(v_mul, vshrq_n<shift>(internal::vbicq(v_mask, v_mul)) )));
1075
+ internal::vst1(dst + j, v_res);
1076
+ }
1077
+
1078
+ for (; j < size.width; j++)
1079
+ {
1080
+ WT val = (WT)src0[j] * (WT)src1[j];
1081
+ dst[j] = (T)((val - (((1<<shift) & ~val) >> shift) + (1<<(shift-1))) >> shift);
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+ #endif
1087
+
1088
+ template <typename T, typename WT>
1089
+ void mul(const Size2D &size,
1090
+ const T * src0Base, ptrdiff_t src0Stride,
1091
+ const T * src1Base, ptrdiff_t src1Stride,
1092
+ T * dstBase, ptrdiff_t dstStride,
1093
+ f32 scale,
1094
+ CONVERT_POLICY cpolicy)
1095
+ {
1096
+ internal::assertSupportedConfiguration();
1097
+
1098
+ #ifdef CAROTENE_NEON
1099
+ typedef typename internal::VecTraits<T>::vec128 vec128;
1100
+
1101
+ typedef void (* mulFunc)(const Size2D &size,
1102
+ const T * src0Base, ptrdiff_t src0Stride,
1103
+ const T * src1Base, ptrdiff_t src1Stride,
1104
+ T * dstBase, ptrdiff_t dstStride,
1105
+ CONVERT_POLICY cpolicy);
1106
+
1107
+ if (scale == 0.0f ||
1108
+ (std::numeric_limits<T>::is_integer &&
1109
+ (scale * std::numeric_limits<T>::max() * std::numeric_limits<T>::max()) < 1.0f &&
1110
+ (scale * std::numeric_limits<T>::max() * std::numeric_limits<T>::max()) > -1.0f))
1111
+ {
1112
+ for (size_t y = 0; y < size.height; ++y)
1113
+ {
1114
+ T * dst = internal::getRowPtr(dstBase, dstStride, y);
1115
+ std::memset(dst, 0, sizeof(T) * size.width);
1116
+ }
1117
+ return;
1118
+ }
1119
+
1120
+ s32 iscale = static_cast<s32>(scale), exp = 0;
1121
+ f32 significand = frexp(scale, &exp);
1122
+ bool is_integer_scale = isIntegerScale(scale),
1123
+ is_power_of_2 = (significand == 0.5f) && (exp <= 0);
1124
+ exp = -exp + 1;
1125
+
1126
+ if (is_power_of_2)
1127
+ {
1128
+ static const mulFunc funcs[16] =
1129
+ {
1130
+ NULL,
1131
+ mulShift<T,WT,1>,
1132
+ mulShift<T,WT,2>,
1133
+ mulShift<T,WT,3>,
1134
+ mulShift<T,WT,4>,
1135
+ mulShift<T,WT,5>,
1136
+ mulShift<T,WT,6>,
1137
+ mulShift<T,WT,7>,
1138
+ mulShift<T,WT,8>,
1139
+ mulShift<T,WT,9>,
1140
+ mulShift<T,WT,10>,
1141
+ mulShift<T,WT,11>,
1142
+ mulShift<T,WT,12>,
1143
+ mulShift<T,WT,13>,
1144
+ mulShift<T,WT,14>,
1145
+ mulShift<T,WT,15>
1146
+ };
1147
+
1148
+ mulFunc func = funcs[exp];
1149
+
1150
+ func(size,
1151
+ src0Base, src0Stride,
1152
+ src1Base, src1Stride,
1153
+ dstBase, dstStride,
1154
+ cpolicy);
1155
+
1156
+ return;
1157
+ }
1158
+
1159
+ const size_t step128 = 16 / sizeof(T);
1160
+ size_t roiw128 = size.width >= (step128 - 1) ? size.width - step128 + 1 : 0;
1161
+ const size_t step64 = 8 / sizeof(T);
1162
+ size_t roiw64 = size.width >= (step64 - 1) ? size.width - step64 + 1 : 0;
1163
+
1164
+ for (size_t i = 0; i < size.height; ++i)
1165
+ {
1166
+ const T * src0 = internal::getRowPtr(src0Base, src0Stride, i);
1167
+ const T * src1 = internal::getRowPtr(src1Base, src1Stride, i);
1168
+ T * dst = internal::getRowPtr(dstBase, dstStride, i);
1169
+ size_t j = 0;
1170
+
1171
+ if (cpolicy == CONVERT_POLICY_SATURATE)
1172
+ {
1173
+ if (is_integer_scale && iscale == 1)
1174
+ {
1175
+ for (; j < roiw128; j += step128)
1176
+ {
1177
+ internal::prefetch(src0 + j);
1178
+ internal::prefetch(src1 + j);
1179
+ vec128 v_src0 = internal::vld1q(src0 + j), v_src1 = internal::vld1q(src1 + j);
1180
+ internal::vst1q(dst + j, internal::vcombine(
1181
+ internal::vqmovn(internal::vmull(internal::vget_low(v_src0),
1182
+ internal::vget_low(v_src1))),
1183
+ internal::vqmovn(internal::vmull(internal::vget_high(v_src0),
1184
+ internal::vget_high(v_src1)))
1185
+ )
1186
+ );
1187
+ }
1188
+ for (; j < roiw64; j += step64)
1189
+ {
1190
+ internal::vst1(dst + j, internal::vqmovn(internal::vmull(internal::vld1(src0 + j),
1191
+ internal::vld1(src1 + j))));
1192
+ }
1193
+
1194
+ for (; j < size.width; j++)
1195
+ {
1196
+ WT val = (WT)src0[j] * (WT)src1[j];
1197
+ dst[j] = internal::saturate_cast<T>(val);
1198
+ }
1199
+ }
1200
+ else // generic case using floats
1201
+ {
1202
+ for (; j < roiw128; j += step128)
1203
+ {
1204
+ internal::prefetch(src0 + j);
1205
+ internal::prefetch(src1 + j);
1206
+ internal::vst1q(dst + j, mulSaturateQ(internal::vld1q(src0 + j),
1207
+ internal::vld1q(src1 + j), scale));
1208
+ }
1209
+ for (; j < roiw64; j += step64)
1210
+ {
1211
+ internal::vst1(dst + j, mulSaturate(internal::vld1(src0 + j),
1212
+ internal::vld1(src1 + j), scale));
1213
+ }
1214
+
1215
+ for (; j < size.width; j++)
1216
+ {
1217
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
1218
+ dst[j] = internal::saturate_cast<T>(fval);
1219
+ }
1220
+ }
1221
+ }
1222
+ else // CONVERT_POLICY_WRAP
1223
+ {
1224
+ if (is_integer_scale && iscale == 1)
1225
+ {
1226
+ for (; j < roiw128; j += step128)
1227
+ {
1228
+ internal::prefetch(src0 + j);
1229
+ internal::prefetch(src1 + j);
1230
+ vec128 v_src0 = internal::vld1q(src0 + j), v_src1 = internal::vld1q(src1 + j);
1231
+ internal::vst1q(dst + j, internal::vcombine(
1232
+ internal::vmovn(internal::vmull(internal::vget_low(v_src0),
1233
+ internal::vget_low(v_src1))),
1234
+ internal::vmovn(internal::vmull(internal::vget_high(v_src0),
1235
+ internal::vget_high(v_src1)))
1236
+ )
1237
+ );
1238
+ }
1239
+ for (; j < roiw64; j += step64)
1240
+ {
1241
+ internal::vst1(dst + j, internal::vmovn(internal::vmull(internal::vld1(src0 + j),
1242
+ internal::vld1(src1 + j))));
1243
+ }
1244
+
1245
+ for (; j < size.width; j++)
1246
+ {
1247
+ WT val = (WT)src0[j] * (WT)src1[j];
1248
+ dst[j] = (T)(val);
1249
+ }
1250
+ }
1251
+ else // generic case using floats
1252
+ {
1253
+ for (; j < roiw128; j += step128)
1254
+ {
1255
+ internal::prefetch(src0 + j);
1256
+ internal::prefetch(src1 + j);
1257
+ internal::vst1q(dst + j, mulWrapQ(internal::vld1q(src0 + j),
1258
+ internal::vld1q(src1 + j), scale));
1259
+ }
1260
+ for (; j < roiw64; j += step64)
1261
+ {
1262
+ internal::vst1(dst + j, mulWrap(internal::vld1(src0 + j),
1263
+ internal::vld1(src1 + j), scale));
1264
+ }
1265
+
1266
+ for (; j < size.width; j++)
1267
+ {
1268
+ f32 fval = (f32)src0[j] * (f32)src1[j] * scale;
1269
+ dst[j] = (T)((s32)trunc(fval));
1270
+ }
1271
+ }
1272
+ }
1273
+ }
1274
+ #else
1275
+ (void)size;
1276
+ (void)src0Base;
1277
+ (void)src0Stride;
1278
+ (void)src1Base;
1279
+ (void)src1Stride;
1280
+ (void)dstBase;
1281
+ (void)dstStride;
1282
+ (void)cpolicy;
1283
+ (void)scale;
1284
+ #endif
1285
+ }
1286
+
1287
+ }
1288
+
1289
+ void mul(const Size2D &size,
1290
+ const s8 * src0Base, ptrdiff_t src0Stride,
1291
+ const s8 * src1Base, ptrdiff_t src1Stride,
1292
+ s8 * dstBase, ptrdiff_t dstStride,
1293
+ f32 scale,
1294
+ CONVERT_POLICY cpolicy)
1295
+ {
1296
+ mul<s8,s16>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
1297
+ }
1298
+
1299
+ void mul(const Size2D &size,
1300
+ const u16 * src0Base, ptrdiff_t src0Stride,
1301
+ const u16 * src1Base, ptrdiff_t src1Stride,
1302
+ u16 * dstBase, ptrdiff_t dstStride,
1303
+ f32 scale,
1304
+ CONVERT_POLICY cpolicy)
1305
+ {
1306
+ mul<u16,u32>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
1307
+ }
1308
+
1309
+ void mul(const Size2D &size,
1310
+ const s16 * src0Base, ptrdiff_t src0Stride,
1311
+ const s16 * src1Base, ptrdiff_t src1Stride,
1312
+ s16 * dstBase, ptrdiff_t dstStride,
1313
+ f32 scale,
1314
+ CONVERT_POLICY cpolicy)
1315
+ {
1316
+ mul<s16,s32>(size, src0Base, src0Stride, src1Base, src1Stride, dstBase, dstStride, scale, cpolicy);
1317
+ }
1318
+
1319
+ void mul(const Size2D &size,
1320
+ const s32 * src0Base, ptrdiff_t src0Stride,
1321
+ const s32 * src1Base, ptrdiff_t src1Stride,
1322
+ s32 * dstBase, ptrdiff_t dstStride,
1323
+ f64 scale,
1324
+ CONVERT_POLICY cpolicy)
1325
+ {
1326
+ internal::assertSupportedConfiguration();
1327
+ #ifdef CAROTENE_NEON
1328
+ typedef void (* mulFunc)(const Size2D &size,
1329
+ const s32 * src0Base, ptrdiff_t src0Stride,
1330
+ const s32 * src1Base, ptrdiff_t src1Stride,
1331
+ s32 * dstBase, ptrdiff_t dstStride,
1332
+ CONVERT_POLICY cpolicy);
1333
+
1334
+ if (!std::isnormal(scale) ||
1335
+ ((scale * std::numeric_limits<s32>::max() * std::numeric_limits<s32>::max()) < 1.0f &&
1336
+ (scale * std::numeric_limits<s32>::max() * std::numeric_limits<s32>::max()) > -1.0f))
1337
+ {
1338
+ for (size_t y = 0; y < size.height; ++y)
1339
+ {
1340
+ s32 * dst = internal::getRowPtr(dstBase, dstStride, y);
1341
+ std::memset(dst, 0, sizeof(s32) * size.width);
1342
+ }
1343
+ return;
1344
+ }
1345
+
1346
+ s32 iscale = static_cast<s32>(scale), exp = 0;
1347
+ f64 significand = frexp(scale, &exp);
1348
+ bool is_integer_scale = isIntegerScale(scale),
1349
+ is_power_of_2 = (significand == 0.5) && (exp <= 0);
1350
+ exp = -exp + 1;
1351
+
1352
+ if (is_power_of_2)
1353
+ {
1354
+ static const mulFunc funcs[16] =
1355
+ {
1356
+ NULL,
1357
+ mulShift<s32,s64,1>,
1358
+ mulShift<s32,s64,2>,
1359
+ mulShift<s32,s64,3>,
1360
+ mulShift<s32,s64,4>,
1361
+ mulShift<s32,s64,5>,
1362
+ mulShift<s32,s64,6>,
1363
+ mulShift<s32,s64,7>,
1364
+ mulShift<s32,s64,8>,
1365
+ mulShift<s32,s64,9>,
1366
+ mulShift<s32,s64,10>,
1367
+ mulShift<s32,s64,11>,
1368
+ mulShift<s32,s64,12>,
1369
+ mulShift<s32,s64,13>,
1370
+ mulShift<s32,s64,14>,
1371
+ mulShift<s32,s64,15>
1372
+ };
1373
+
1374
+ mulFunc func = funcs[exp];
1375
+
1376
+ func(size,
1377
+ src0Base, src0Stride,
1378
+ src1Base, src1Stride,
1379
+ dstBase, dstStride,
1380
+ cpolicy);
1381
+
1382
+ return;
1383
+ }
1384
+
1385
+ size_t roiw128 = size.width >= 3 ? size.width - 3 : 0;
1386
+ size_t roiw64 = size.width >= 1 ? size.width - 1 : 0;
1387
+
1388
+ for (size_t i = 0; i < size.height; ++i)
1389
+ {
1390
+ const s32 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
1391
+ const s32 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
1392
+ s32 * dst = internal::getRowPtr(dstBase, dstStride, i);
1393
+ size_t j = 0;
1394
+
1395
+ if (cpolicy == CONVERT_POLICY_SATURATE)
1396
+ {
1397
+ if (is_integer_scale && iscale == 1)
1398
+ {
1399
+ for (; j < roiw128; j += 4)
1400
+ {
1401
+ internal::prefetch(src0 + j);
1402
+ internal::prefetch(src1 + j);
1403
+ int32x4_t v_src0 = internal::vld1q(src0 + j), v_src1 = internal::vld1q(src1 + j);
1404
+ internal::vst1q(dst + j, internal::vcombine(
1405
+ internal::vqmovn(internal::vmull(internal::vget_low(v_src0),
1406
+ internal::vget_low(v_src1))),
1407
+ internal::vqmovn(internal::vmull(internal::vget_high(v_src0),
1408
+ internal::vget_high(v_src1)))
1409
+ )
1410
+ );
1411
+ }
1412
+ for (; j < roiw64; j += 2)
1413
+ {
1414
+ internal::vst1(dst + j, internal::vqmovn(internal::vmull(internal::vld1(src0 + j),
1415
+ internal::vld1(src1 + j))));
1416
+ }
1417
+
1418
+ for (; j < size.width; j++)
1419
+ {
1420
+ s64 val = (s64)src0[j] * (s64)src1[j];
1421
+ dst[j] = internal::saturate_cast<s32>(val);
1422
+ }
1423
+ }
1424
+ else // generic case using floats
1425
+ {
1426
+ for (; j < size.width; j++)
1427
+ {
1428
+ f64 fval = src0[j] * src1[j] * scale;
1429
+ dst[j] = internal::saturate_cast<s32>(fval);
1430
+ }
1431
+ }
1432
+ }
1433
+ else // CONVERT_POLICY_WRAP
1434
+ {
1435
+ if (is_integer_scale && iscale == 1)
1436
+ {
1437
+ for (; j < roiw128; j += 4)
1438
+ {
1439
+ internal::prefetch(src0 + j);
1440
+ internal::prefetch(src1 + j);
1441
+ int32x4_t v_src0 = internal::vld1q(src0 + j), v_src1 = internal::vld1q(src1 + j);
1442
+ internal::vst1q(dst + j, internal::vcombine(
1443
+ internal::vmovn(internal::vmull(internal::vget_low(v_src0),
1444
+ internal::vget_low(v_src1))),
1445
+ internal::vmovn(internal::vmull(internal::vget_high(v_src0),
1446
+ internal::vget_high(v_src1)))
1447
+ )
1448
+ );
1449
+ }
1450
+ for (; j < roiw64; j += 2)
1451
+ {
1452
+ internal::vst1(dst + j, internal::vmovn(internal::vmull(internal::vld1(src0 + j),
1453
+ internal::vld1(src1 + j))));
1454
+ }
1455
+
1456
+ for (; j < size.width; j++)
1457
+ {
1458
+ s64 val = (s64)src0[j] * (s64)src1[j];
1459
+ dst[j] = (s32)(val);
1460
+ }
1461
+ }
1462
+ else // generic case using floats
1463
+ {
1464
+ for (; j < size.width; j++)
1465
+ {
1466
+ f64 fval = src0[j] * src1[j] * scale;
1467
+ dst[j] = (s32)trunc(fval);
1468
+ }
1469
+ }
1470
+ }
1471
+ }
1472
+ #else
1473
+ (void)size;
1474
+ (void)src0Base;
1475
+ (void)src0Stride;
1476
+ (void)src1Base;
1477
+ (void)src1Stride;
1478
+ (void)dstBase;
1479
+ (void)dstStride;
1480
+ (void)cpolicy;
1481
+ (void)scale;
1482
+ #endif
1483
+ }
1484
+
1485
+ void mul(const Size2D &size,
1486
+ const f32 * src0Base, ptrdiff_t src0Stride,
1487
+ const f32 * src1Base, ptrdiff_t src1Stride,
1488
+ f32 * dstBase, ptrdiff_t dstStride,
1489
+ f32 scale)
1490
+ {
1491
+ internal::assertSupportedConfiguration();
1492
+ #ifdef CAROTENE_NEON
1493
+ if (scale == 0.0f)
1494
+ {
1495
+ for (size_t y = 0; y < size.height; ++y)
1496
+ {
1497
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, y);
1498
+ std::memset(dst, 0, sizeof(f32) * size.width);
1499
+ }
1500
+ return;
1501
+ }
1502
+
1503
+ size_t roiw128 = size.width >= 3 ? size.width - 3 : 0;
1504
+ size_t roiw64 = size.width >= 1 ? size.width - 1 : 0;
1505
+
1506
+ if (std::fabs(scale - 1.0f) < FLT_EPSILON)
1507
+ {
1508
+ for (size_t i = 0; i < size.height; ++i)
1509
+ {
1510
+ const f32 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
1511
+ const f32 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
1512
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
1513
+ size_t j = 0;
1514
+
1515
+ for (; j < roiw128; j += 4)
1516
+ {
1517
+ internal::prefetch(src0 + j);
1518
+ internal::prefetch(src1 + j);
1519
+ vst1q_f32(dst + j, vmulq_f32(vld1q_f32(src0 + j), vld1q_f32(src1 + j)));
1520
+ }
1521
+
1522
+ for (; j < roiw64; j += 2)
1523
+ {
1524
+ vst1_f32(dst + j, vmul_f32(vld1_f32(src0 + j), vld1_f32(src1 + j)));
1525
+ }
1526
+
1527
+ for (; j < size.width; j++)
1528
+ {
1529
+ dst[j] = src0[j] * src1[j];
1530
+ }
1531
+ }
1532
+ }
1533
+ else
1534
+ {
1535
+ for (size_t i = 0; i < size.height; ++i)
1536
+ {
1537
+ const f32 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
1538
+ const f32 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
1539
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
1540
+ size_t j = 0;
1541
+
1542
+ for (; j < roiw128; j += 4)
1543
+ {
1544
+ internal::prefetch(src0 + j);
1545
+ internal::prefetch(src1 + j);
1546
+ vst1q_f32(dst + j, vmulq_n_f32(vmulq_f32(vld1q_f32(src0 + j), vld1q_f32(src1 + j)), scale));
1547
+ }
1548
+
1549
+ for (; j < roiw64; j += 2)
1550
+ {
1551
+ vst1_f32(dst + j, vmul_n_f32(vmul_f32(vld1_f32(src0 + j), vld1_f32(src1 + j)), scale));
1552
+ }
1553
+
1554
+ for (; j < size.width; j++)
1555
+ {
1556
+ dst[j] = src0[j] * src1[j] * scale;
1557
+ }
1558
+ }
1559
+ }
1560
+ #else
1561
+ (void)size;
1562
+ (void)src0Base;
1563
+ (void)src0Stride;
1564
+ (void)src1Base;
1565
+ (void)src1Stride;
1566
+ (void)dstBase;
1567
+ (void)dstStride;
1568
+ (void)scale;
1569
+ #endif
1570
+ }
1571
+
1572
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/norm.cpp ADDED
@@ -0,0 +1,1310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ namespace CAROTENE_NS {
43
+
44
+ //magic number; must be multiple of 4
45
+ #define NORM32F_BLOCK_SIZE 2048
46
+
47
+ s32 normInf(const Size2D &_size,
48
+ const u8 * srcBase, ptrdiff_t srcStride)
49
+ {
50
+ internal::assertSupportedConfiguration();
51
+ #ifdef CAROTENE_NEON
52
+ Size2D size(_size);
53
+ if (srcStride == (ptrdiff_t)(size.width))
54
+ {
55
+ size.width *= size.height;
56
+ size.height = 1;
57
+ }
58
+ s32 result = 0;
59
+ for(size_t k = 0; k < size.height; ++k)
60
+ {
61
+ const u8* src = internal::getRowPtr( srcBase, srcStride, k);
62
+ size_t i = 0;
63
+ if (size.width >= 16)
64
+ {
65
+ uint8x16_t s = vld1q_u8(src);
66
+ for (i = 16; i <= size.width - 16; i += 16)
67
+ {
68
+ internal::prefetch(src + i);
69
+ uint8x16_t s1 = vld1q_u8(src + i);
70
+ s = vmaxq_u8(s1, s);
71
+ }
72
+ u8 s2[8];
73
+ uint8x8_t s3 = vmax_u8(vget_low_u8(s), vget_high_u8(s));
74
+ vst1_u8(s2, s3);
75
+ for (u32 j = 0; j < 8; j++)
76
+ result = std::max((s32)(s2[j]), result);
77
+ }
78
+ for ( ; i < size.width; i++)
79
+ result = std::max((s32)(src[i]), result);
80
+ }
81
+ return result;
82
+ #else
83
+ (void)_size;
84
+ (void)srcBase;
85
+ (void)srcStride;
86
+
87
+ return 0;
88
+ #endif
89
+ }
90
+
91
+ s32 normInf(const Size2D &_size,
92
+ const s8 * srcBase, ptrdiff_t srcStride)
93
+ {
94
+ internal::assertSupportedConfiguration();
95
+ #ifdef CAROTENE_NEON
96
+ Size2D size(_size);
97
+ if (srcStride == (ptrdiff_t)(size.width))
98
+ {
99
+ size.width *= size.height;
100
+ size.height = 1;
101
+ }
102
+ s32 result = 0;
103
+ for(size_t k = 0; k < size.height; ++k)
104
+ {
105
+ const s8* src = internal::getRowPtr( srcBase, srcStride, k);
106
+ size_t i = 0;
107
+ if (size.width >= 16)
108
+ {
109
+ uint8x16_t s = vreinterpretq_u8_s8(vabsq_s8(vld1q_s8(src)));
110
+ for (i = 16; i <= size.width - 16; i += 16)
111
+ {
112
+ internal::prefetch(src + i);
113
+ uint8x16_t s1 = vreinterpretq_u8_s8(vabsq_s8(vld1q_s8(src + i)));
114
+ s = vmaxq_u8(s1, s);
115
+ }
116
+ u8 s2[8];
117
+ uint8x8_t s3 = vmax_u8(vget_low_u8(s), vget_high_u8(s));
118
+ vst1_u8(s2, s3);
119
+ for (u32 j = 0; j < 8; j++)
120
+ result = std::max((s32)(s2[j]), result);
121
+ }
122
+ for ( ; i < size.width; i++)
123
+ result = std::max((s32)(std::abs(src[i])), result);
124
+ }
125
+ return result;
126
+ #else
127
+ (void)_size;
128
+ (void)srcBase;
129
+ (void)srcStride;
130
+
131
+ return 0;
132
+ #endif
133
+ }
134
+
135
+ s32 normInf(const Size2D &_size,
136
+ const u16 * srcBase, ptrdiff_t srcStride)
137
+ {
138
+ internal::assertSupportedConfiguration();
139
+ #ifdef CAROTENE_NEON
140
+ Size2D size(_size);
141
+ if (srcStride == (ptrdiff_t)(size.width))
142
+ {
143
+ size.width *= size.height;
144
+ size.height = 1;
145
+ }
146
+ s32 result = 0;
147
+ for(size_t k = 0; k < size.height; ++k)
148
+ {
149
+ const u16* src = internal::getRowPtr( srcBase, srcStride, k);
150
+ size_t i = 0;
151
+ if (size.width >= 8)
152
+ {
153
+ uint16x8_t s = vld1q_u16(src);
154
+ for (i = 8; i <= size.width - 8; i += 8)
155
+ {
156
+ internal::prefetch(src + i);
157
+ uint16x8_t s1 = vld1q_u16(src + i);
158
+ s = vmaxq_u16(s1, s);
159
+ }
160
+ u16 s2[4];
161
+ uint16x4_t s3 = vmax_u16(vget_low_u16(s), vget_high_u16(s));
162
+ vst1_u16(s2, s3);
163
+ for (u32 j = 0; j < 4; j++)
164
+ result = std::max((s32)(s2[j]), result);
165
+ }
166
+ for ( ; i < size.width; i++)
167
+ result = std::max((s32)(src[i]), result);
168
+ }
169
+ return result;
170
+ #else
171
+ (void)_size;
172
+ (void)srcBase;
173
+ (void)srcStride;
174
+
175
+ return 0;
176
+ #endif
177
+ }
178
+
179
+ s32 normInf(const Size2D &_size,
180
+ const s16 * srcBase, ptrdiff_t srcStride)
181
+ {
182
+ internal::assertSupportedConfiguration();
183
+ #ifdef CAROTENE_NEON
184
+ Size2D size(_size);
185
+ if (srcStride == (ptrdiff_t)(size.width))
186
+ {
187
+ size.width *= size.height;
188
+ size.height = 1;
189
+ }
190
+ s32 result = 0;
191
+ for(size_t k = 0; k < size.height; ++k)
192
+ {
193
+ const s16* src = internal::getRowPtr( srcBase, srcStride, k);
194
+ size_t i = 0;
195
+ if (size.width >= 8)
196
+ {
197
+ uint16x8_t s = vreinterpretq_u16_s16(vabsq_s16(vld1q_s16(src)));
198
+ for (i = 8; i <= size.width - 8; i += 8)
199
+ {
200
+ internal::prefetch(src + i);
201
+ uint16x8_t s1 = vreinterpretq_u16_s16(vabsq_s16(vld1q_s16(src + i)));
202
+ s = vmaxq_u16(s1, s);
203
+ }
204
+ u16 s2[4];
205
+ uint16x4_t s3 = vmax_u16(vget_low_u16(s), vget_high_u16(s));
206
+ vst1_u16(s2, s3);
207
+ for (u32 j = 0; j < 4; j++)
208
+ result = std::max((s32)(s2[j]), result);
209
+ }
210
+ for ( ; i < size.width; i++)
211
+ result = std::max(std::abs((s32)(src[i])), result);
212
+ }
213
+ return result;
214
+ #else
215
+ (void)_size;
216
+ (void)srcBase;
217
+ (void)srcStride;
218
+
219
+ return 0;
220
+ #endif
221
+ }
222
+
223
+ s32 normInf(const Size2D &_size,
224
+ const s32 * srcBase, ptrdiff_t srcStride)
225
+ {
226
+ internal::assertSupportedConfiguration();
227
+ #ifdef CAROTENE_NEON
228
+ Size2D size(_size);
229
+ if (srcStride == (ptrdiff_t)(size.width))
230
+ {
231
+ size.width *= size.height;
232
+ size.height = 1;
233
+ }
234
+ s32 result = 0;
235
+ for(size_t k = 0; k < size.height; ++k)
236
+ {
237
+ const s32* src = internal::getRowPtr( srcBase, srcStride, k);
238
+ size_t i = 0;
239
+ if (size.width >= 4)
240
+ {
241
+ uint32x4_t s = vreinterpretq_u32_s32(vabsq_s32(vld1q_s32(src)));
242
+ for (i = 4; i <= size.width - 4; i += 4)
243
+ {
244
+ internal::prefetch(src + i);
245
+ uint32x4_t s1 = vreinterpretq_u32_s32(vabsq_s32(vld1q_s32(src + i)));
246
+ s = vmaxq_u32(s1, s);
247
+ }
248
+ u32 s2[2];
249
+ uint32x2_t s3 = vmax_u32(vget_low_u32(s), vget_high_u32(s));
250
+ vst1_u32(s2, s3);
251
+ for (u32 j = 0; j < 2; j++)
252
+ result = std::max((s32)(s2[j]), result);
253
+ }
254
+ for ( ; i < size.width; i++)
255
+ result = std::max((s32)(std::abs(src[i])), result);
256
+ }
257
+ return result;
258
+ #else
259
+ (void)_size;
260
+ (void)srcBase;
261
+ (void)srcStride;
262
+
263
+ return 0;
264
+ #endif
265
+ }
266
+
267
+ f32 normInf(const Size2D &_size,
268
+ const f32 * srcBase, ptrdiff_t srcStride)
269
+ {
270
+ internal::assertSupportedConfiguration();
271
+ #ifdef CAROTENE_NEON
272
+ Size2D size(_size);
273
+ if (srcStride == (ptrdiff_t)(size.width))
274
+ {
275
+ size.width *= size.height;
276
+ size.height = 1;
277
+ }
278
+ f32 result = 0;
279
+ for(size_t k = 0; k < size.height; ++k)
280
+ {
281
+ const f32* src = internal::getRowPtr( srcBase, srcStride, k);
282
+ size_t i = 0;
283
+ if (size.width >= 4)
284
+ {
285
+ float32x4_t s = vabsq_f32(vld1q_f32(src));
286
+ for (i = 4; i <= size.width - 4; i += 4 )
287
+ {
288
+ internal::prefetch(src + i);
289
+ float32x4_t s1 = vld1q_f32(src + i);
290
+ float32x4_t sa = vabsq_f32(s1);
291
+ s = vmaxq_f32(sa, s);
292
+ }
293
+ f32 s2[2];
294
+ float32x2_t s3 = vmax_f32(vget_low_f32(s), vget_high_f32(s));
295
+ vst1_f32(s2, s3);
296
+ for (u32 j = 0; j < 2; j++)
297
+ result = std::max(s2[j], result);
298
+ }
299
+ for (; i < size.width; i++)
300
+ result = std::max(std::abs(src[i]), result);
301
+ }
302
+ return result;
303
+ #else
304
+ (void)_size;
305
+ (void)srcBase;
306
+ (void)srcStride;
307
+
308
+ return 0.;
309
+ #endif
310
+ }
311
+
312
+ s32 normL1(const Size2D &_size,
313
+ const u8 * srcBase, ptrdiff_t srcStride)
314
+ {
315
+ internal::assertSupportedConfiguration();
316
+ #ifdef CAROTENE_NEON
317
+ Size2D size(_size);
318
+ if (srcStride == (ptrdiff_t)(size.width))
319
+ {
320
+ size.width *= size.height;
321
+ size.height = 1;
322
+ }
323
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
324
+ s32 result = 0;
325
+ for(size_t k = 0; k < size.height; ++k)
326
+ {
327
+ const u8* src = internal::getRowPtr( srcBase, srcStride, k);
328
+ size_t i = 0;
329
+ uint32x4_t vs = vmovq_n_u32(0);
330
+ for (; i < roiw8;)
331
+ {
332
+ size_t limit = std::min(size.width, i + 256) - 8;
333
+ uint8x8_t s0 = vld1_u8(src + i);
334
+ uint16x8_t s = vmovl_u8(s0);
335
+
336
+ for (i += 8; i <= limit; i += 8)
337
+ {
338
+ internal::prefetch(src + i);
339
+ uint8x8_t s1 = vld1_u8(src + i);
340
+ s = vaddw_u8(s, s1);
341
+ }
342
+
343
+ uint16x4_t s4 = vadd_u16(vget_low_u16(s), vget_high_u16(s));
344
+ vs = vaddw_u16(vs, s4);
345
+ }
346
+
347
+ u32 s2[2];
348
+ uint32x2_t vs2 = vadd_u32(vget_low_u32(vs), vget_high_u32(vs));
349
+ vst1_u32(s2, vs2);
350
+
351
+ result += (s32)(s2[0] + s2[1]);
352
+
353
+ for ( ; i < size.width; i++)
354
+ result += (s32)(src[i]);
355
+ }
356
+ return result;
357
+ #else
358
+ (void)_size;
359
+ (void)srcBase;
360
+ (void)srcStride;
361
+
362
+ return 0;
363
+ #endif
364
+ }
365
+
366
+ s32 normL1(const Size2D &_size,
367
+ const s8 * srcBase, ptrdiff_t srcStride)
368
+ {
369
+ internal::assertSupportedConfiguration();
370
+ #ifdef CAROTENE_NEON
371
+ Size2D size(_size);
372
+ if (srcStride == (ptrdiff_t)(size.width))
373
+ {
374
+ size.width *= size.height;
375
+ size.height = 1;
376
+ }
377
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
378
+ s32 result = 0;
379
+ for(size_t k = 0; k < size.height; ++k)
380
+ {
381
+ const s8* src = internal::getRowPtr( srcBase, srcStride, k);
382
+ size_t i = 0;
383
+ uint32x4_t vs = vmovq_n_u32(0);
384
+
385
+ for (; i < roiw8;)
386
+ {
387
+ size_t limit = std::min(size.width, i + 256) - 8;
388
+ uint8x8_t s0 = vreinterpret_u8_s8(vabs_s8(vld1_s8(src + i)));
389
+ uint16x8_t s = vmovl_u8(s0);
390
+
391
+ for (i += 8; i <= limit; i += 8)
392
+ {
393
+ internal::prefetch(src + i);
394
+ uint8x8_t s1 = vreinterpret_u8_s8(vabs_s8(vld1_s8(src + i)));
395
+ s = vaddw_u8(s, s1);
396
+ }
397
+
398
+ uint16x4_t s4 = vadd_u16(vget_low_u16(s), vget_high_u16(s));
399
+ vs = vaddw_u16(vs, s4);
400
+ }
401
+
402
+ u32 s2[2];
403
+ uint32x2_t vs2 = vadd_u32(vget_low_u32(vs), vget_high_u32(vs));
404
+ vst1_u32(s2, vs2);
405
+
406
+ result += (s32)(s2[0] + s2[1]);
407
+
408
+ for ( ; i < size.width; i++)
409
+ result += (s32)(std::abs(src[i]));
410
+ }
411
+ return result;
412
+ #else
413
+ (void)_size;
414
+ (void)srcBase;
415
+ (void)srcStride;
416
+
417
+ return 0;
418
+ #endif
419
+ }
420
+
421
+ s32 normL1(const Size2D &_size,
422
+ const u16 * srcBase, ptrdiff_t srcStride)
423
+ {
424
+ internal::assertSupportedConfiguration();
425
+ #ifdef CAROTENE_NEON
426
+ Size2D size(_size);
427
+ if (srcStride == (ptrdiff_t)(size.width))
428
+ {
429
+ size.width *= size.height;
430
+ size.height = 1;
431
+ }
432
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
433
+ s32 result = 0;
434
+ for(size_t k = 0; k < size.height; ++k)
435
+ {
436
+ const u16* src = internal::getRowPtr( srcBase, srcStride, k);
437
+ size_t i = 0;
438
+ uint32x4_t vs = vmovq_n_u32(0);
439
+ for (; i < roiw4; i += 4)
440
+ {
441
+ internal::prefetch(src + i);
442
+ uint16x4_t s = vld1_u16(src + i);
443
+ vs = vaddw_u16(vs, s);
444
+ }
445
+ u32 s2[4];
446
+ vst1q_u32(s2, vs);
447
+ for (u32 j = 0; j < 4; j++)
448
+ result += s2[j];
449
+ for ( ; i < size.width; i++)
450
+ result += (s32)(src[i]);
451
+ }
452
+ return result;
453
+ #else
454
+ (void)_size;
455
+ (void)srcBase;
456
+ (void)srcStride;
457
+
458
+ return 0;
459
+ #endif
460
+ }
461
+
462
+ s32 normL1(const Size2D &_size,
463
+ const s16 * srcBase, ptrdiff_t srcStride)
464
+ {
465
+ internal::assertSupportedConfiguration();
466
+ #ifdef CAROTENE_NEON
467
+ Size2D size(_size);
468
+ if (srcStride == (ptrdiff_t)(size.width))
469
+ {
470
+ size.width *= size.height;
471
+ size.height = 1;
472
+ }
473
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
474
+ s32 result = 0;
475
+ for(size_t k = 0; k < size.height; ++k)
476
+ {
477
+ const s16* src = internal::getRowPtr( srcBase, srcStride, k);
478
+ size_t i = 0;
479
+ uint32x4_t vs = vmovq_n_u32(0);
480
+ for (; i < roiw4; i += 4)
481
+ {
482
+ internal::prefetch(src + i);
483
+ uint16x4_t s = vreinterpret_u16_s16(vabs_s16(vld1_s16(src + i)));
484
+ vs = vaddw_u16(vs, s);
485
+ }
486
+ u32 s2[4];
487
+ vst1q_u32(s2, vs);
488
+ for (u32 j = 0; j < 4; j++)
489
+ result += s2[j];
490
+ for ( ; i < size.width; i++)
491
+ result += (s32)(std::abs(src[i]));
492
+ }
493
+ return result;
494
+ #else
495
+ (void)_size;
496
+ (void)srcBase;
497
+ (void)srcStride;
498
+
499
+ return 0;
500
+ #endif
501
+ }
502
+
503
+ f64 normL1(const Size2D &_size,
504
+ const s32 * srcBase, ptrdiff_t srcStride)
505
+ {
506
+ internal::assertSupportedConfiguration();
507
+ #ifdef CAROTENE_NEON
508
+ Size2D size(_size);
509
+ if (srcStride == (ptrdiff_t)(size.width))
510
+ {
511
+ size.width *= size.height;
512
+ size.height = 1;
513
+ }
514
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
515
+ f64 result = 0;
516
+ for(size_t k = 0; k < size.height; ++k)
517
+ {
518
+ const s32* src = internal::getRowPtr( srcBase, srcStride, k);
519
+ size_t i = 0;
520
+ for (; i < roiw4;)
521
+ {
522
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
523
+ float32x4_t s = vcvtq_f32_s32(vabsq_s32(vld1q_s32(src + i)));
524
+ for (i += 4; i <= limit; i += 4 )
525
+ {
526
+ internal::prefetch(src + i);
527
+ float32x4_t s1 = vcvtq_f32_s32(vabsq_s32(vld1q_s32(src + i)));
528
+ s = vaddq_f32(s, s1);
529
+ }
530
+
531
+ f32 s2[4];
532
+ vst1q_f32(s2, s);
533
+
534
+ for (u32 j = 0; j < 4; j++)
535
+ result += (f64)(s2[j]);
536
+ }
537
+ for ( ; i < size.width; i++)
538
+ result += (f64)(std::abs(src[i]));
539
+ }
540
+ return result;
541
+ #else
542
+ (void)_size;
543
+ (void)srcBase;
544
+ (void)srcStride;
545
+
546
+ return 0.;
547
+ #endif
548
+ }
549
+
550
+ f64 normL1(const Size2D &_size,
551
+ const f32 * srcBase, ptrdiff_t srcStride)
552
+ {
553
+ internal::assertSupportedConfiguration();
554
+ #ifdef CAROTENE_NEON
555
+ Size2D size(_size);
556
+ if (srcStride == (ptrdiff_t)(size.width))
557
+ {
558
+ size.width *= size.height;
559
+ size.height = 1;
560
+ }
561
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
562
+ f64 result = 0;
563
+ for(size_t k = 0; k < size.height; ++k)
564
+ {
565
+ const f32* src = internal::getRowPtr( srcBase, srcStride, k);
566
+ size_t i = 0;
567
+
568
+ for (; i < roiw4;)
569
+ {
570
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
571
+ float32x4_t s = vabsq_f32(vld1q_f32(src + i));
572
+ for (i += 4; i <= limit; i += 4)
573
+ {
574
+ internal::prefetch(src + i);
575
+ float32x4_t s1 = vld1q_f32(src + i);
576
+ float32x4_t sa = vabsq_f32(s1);
577
+ s = vaddq_f32(sa, s);
578
+ }
579
+
580
+ f32 s2[4];
581
+ vst1q_f32(s2, s);
582
+
583
+ for (u32 j = 0; j < 4; j++)
584
+ result += (f64)(s2[j]);
585
+ }
586
+ for (; i < size.width; i++)
587
+ result += std::abs((f64)(src[i]));
588
+ }
589
+ return result;
590
+ #else
591
+ (void)_size;
592
+ (void)srcBase;
593
+ (void)srcStride;
594
+
595
+ return 0.;
596
+ #endif
597
+ }
598
+
599
+ s32 normL2(const Size2D &_size,
600
+ const u8 * srcBase, ptrdiff_t srcStride)
601
+ {
602
+ internal::assertSupportedConfiguration();
603
+ #ifdef CAROTENE_NEON
604
+ Size2D size(_size);
605
+ if (srcStride == (ptrdiff_t)(size.width))
606
+ {
607
+ size.width *= size.height;
608
+ size.height = 1;
609
+ }
610
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
611
+ s32 result = 0;
612
+ for(size_t k = 0; k < size.height; ++k)
613
+ {
614
+ const u8* src = internal::getRowPtr( srcBase, srcStride, k);
615
+ size_t i = 0;
616
+
617
+ uint32x4_t sl = vmovq_n_u32(0);
618
+ uint32x4_t sh = vmovq_n_u32(0);
619
+
620
+ for (; i < roiw8; i += 8)
621
+ {
622
+ internal::prefetch(src + i);
623
+ uint8x8_t s1 = vld1_u8(src + i);
624
+ uint16x8_t sq = vmull_u8(s1, s1);
625
+
626
+ sl = vaddw_u16(sl, vget_low_u16(sq));
627
+ sh = vaddw_u16(sh, vget_high_u16(sq));
628
+ }
629
+
630
+ uint32x4_t s = vaddq_u32(sl, sh);
631
+ uint32x2_t ss = vadd_u32(vget_low_u32(s), vget_high_u32(s));
632
+
633
+ u32 s2[2];
634
+ vst1_u32(s2, ss);
635
+
636
+ result += (s32)(s2[0] + s2[1]);
637
+
638
+ for (; i < size.width; i++)
639
+ result += (s32)(src[i]) * (s32)(src[i]);
640
+ }
641
+ return result;
642
+ #else
643
+ (void)_size;
644
+ (void)srcBase;
645
+ (void)srcStride;
646
+
647
+ return 0;
648
+ #endif
649
+ }
650
+
651
+ s32 normL2(const Size2D &_size,
652
+ const s8 * srcBase, ptrdiff_t srcStride)
653
+ {
654
+ internal::assertSupportedConfiguration();
655
+ #ifdef CAROTENE_NEON
656
+ Size2D size(_size);
657
+ if (srcStride == (ptrdiff_t)(size.width))
658
+ {
659
+ size.width *= size.height;
660
+ size.height = 1;
661
+ }
662
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
663
+ s32 result = 0;
664
+ for(size_t k = 0; k < size.height; ++k)
665
+ {
666
+ const s8* src = internal::getRowPtr( srcBase, srcStride, k);
667
+ size_t i = 0;
668
+
669
+ int32x4_t sl = vmovq_n_s32(0);
670
+ int32x4_t sh = vmovq_n_s32(0);
671
+
672
+ for (; i < roiw8; i += 8)
673
+ {
674
+ internal::prefetch(src + i);
675
+ int8x8_t s1 = vld1_s8(src + i);
676
+ int16x8_t sq = vmull_s8(s1, s1);
677
+
678
+ sl = vaddw_s16(sl, vget_low_s16(sq));
679
+ sh = vaddw_s16(sh, vget_high_s16(sq));
680
+ }
681
+
682
+ int32x4_t s = vaddq_s32(sl, sh);
683
+ int32x2_t ss = vadd_s32(vget_low_s32(s), vget_high_s32(s));
684
+
685
+ s32 s2[2];
686
+ vst1_s32(s2, ss);
687
+
688
+ result += s2[0] + s2[1];
689
+
690
+ for (; i < size.width; i++)
691
+ result += (s32)(src[i]) * (s32)(src[i]);
692
+ }
693
+ return result;
694
+ #else
695
+ (void)_size;
696
+ (void)srcBase;
697
+ (void)srcStride;
698
+
699
+ return 0;
700
+ #endif
701
+ }
702
+
703
+ f64 normL2(const Size2D &_size,
704
+ const u16 * srcBase, ptrdiff_t srcStride)
705
+ {
706
+ internal::assertSupportedConfiguration();
707
+ #ifdef CAROTENE_NEON
708
+ Size2D size(_size);
709
+ if (srcStride == (ptrdiff_t)(size.width))
710
+ {
711
+ size.width *= size.height;
712
+ size.height = 1;
713
+ }
714
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
715
+ f64 result = 0;
716
+ for(size_t k = 0; k < size.height; ++k)
717
+ {
718
+ const u16* src = internal::getRowPtr( srcBase, srcStride, k);
719
+ size_t i = 0;
720
+ for (; i < roiw4;)
721
+ {
722
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
723
+ uint16x4_t s0 = vld1_u16(src+i);
724
+ float32x4_t s = vcvtq_f32_u32(vmull_u16(s0,s0));
725
+ for (i += 4; i <= limit; i += 4 )
726
+ {
727
+ internal::prefetch(src + i);
728
+ uint16x4_t s1 = vld1_u16(src+i);
729
+ float32x4_t sq = vcvtq_f32_u32(vmull_u16(s1, s1));
730
+ s = vaddq_f32(s, sq);
731
+ }
732
+ f32 s2[4];
733
+ vst1q_f32(s2, s);
734
+ for (u32 j = 0; j < 4; j++)
735
+ result += (f64)(s2[j]);
736
+ }
737
+
738
+ for ( ; i < size.width; i++)
739
+ result += (f64)(src[i]) * (f64)(src[i]);
740
+ }
741
+ return result;
742
+ #else
743
+ (void)_size;
744
+ (void)srcBase;
745
+ (void)srcStride;
746
+
747
+ return 0.;
748
+ #endif
749
+ }
750
+
751
+ f64 normL2(const Size2D &_size,
752
+ const s16 * srcBase, ptrdiff_t srcStride)
753
+ {
754
+ internal::assertSupportedConfiguration();
755
+ #ifdef CAROTENE_NEON
756
+ Size2D size(_size);
757
+ if (srcStride == (ptrdiff_t)(size.width))
758
+ {
759
+ size.width *= size.height;
760
+ size.height = 1;
761
+ }
762
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
763
+ f64 result = 0;
764
+ for(size_t k = 0; k < size.height; ++k)
765
+ {
766
+ const s16* src = internal::getRowPtr( srcBase, srcStride, k);
767
+ size_t i = 0;
768
+ for (; i < roiw4;)
769
+ {
770
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
771
+ int16x4_t s0 = vld1_s16(src+i);
772
+ float32x4_t s = vcvtq_f32_s32(vmull_s16(s0,s0));
773
+ for (i += 4; i <= limit; i += 4 )
774
+ {
775
+ internal::prefetch(src + i);
776
+ int16x4_t s1 = vld1_s16(src+i);
777
+ float32x4_t sq = vcvtq_f32_s32(vmull_s16(s1, s1));
778
+ s = vaddq_f32(s, sq);
779
+ }
780
+ f32 s2[4];
781
+ vst1q_f32(s2, s);
782
+ for (u32 j = 0; j < 4; j++)
783
+ result += (f64)(s2[j]);
784
+ }
785
+
786
+ for ( ; i < size.width; i++)
787
+ result += (f64)(src[i]) * (f64)(src[i]);
788
+ }
789
+ return result;
790
+ #else
791
+ (void)_size;
792
+ (void)srcBase;
793
+ (void)srcStride;
794
+
795
+ return 0.;
796
+ #endif
797
+ }
798
+
799
+ f64 normL2(const Size2D &_size,
800
+ const s32 * srcBase, ptrdiff_t srcStride)
801
+ {
802
+ internal::assertSupportedConfiguration();
803
+ #ifdef CAROTENE_NEON
804
+ Size2D size(_size);
805
+ if (srcStride == (ptrdiff_t)(size.width))
806
+ {
807
+ size.width *= size.height;
808
+ size.height = 1;
809
+ }
810
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
811
+ f64 result = 0;
812
+ for(size_t k = 0; k < size.height; ++k)
813
+ {
814
+ const s32* src = internal::getRowPtr( srcBase, srcStride, k);
815
+ size_t i = 0;
816
+ for (; i < roiw4;)
817
+ {
818
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
819
+ float32x4_t s = vcvtq_f32_s32(vld1q_s32(src + i));
820
+ s = vmulq_f32(s, s);
821
+ for (i += 4; i <= limit; i += 4 )
822
+ {
823
+ internal::prefetch(src + i);
824
+ float32x4_t s1 = vcvtq_f32_s32(vld1q_s32(src + i));
825
+ s = vmlaq_f32(s, s1, s1);
826
+ }
827
+
828
+ f32 s2[4];
829
+ vst1q_f32(s2, s);
830
+
831
+ for (u32 j = 0; j < 4; j++)
832
+ result += (f64)(s2[j]);
833
+ }
834
+ for ( ; i < size.width; i++)
835
+ result += (f64)(src[i]) * (f64)(src[i]);
836
+ }
837
+ return result;
838
+ #else
839
+ (void)_size;
840
+ (void)srcBase;
841
+ (void)srcStride;
842
+
843
+ return 0.;
844
+ #endif
845
+ }
846
+
847
+ f64 normL2(const Size2D &_size,
848
+ const f32 * srcBase, ptrdiff_t srcStride)
849
+ {
850
+ internal::assertSupportedConfiguration();
851
+ #ifdef CAROTENE_NEON
852
+ Size2D size(_size);
853
+ if (srcStride == (ptrdiff_t)(size.width))
854
+ {
855
+ size.width *= size.height;
856
+ size.height = 1;
857
+ }
858
+ size_t roiw4 = size.width >= 3 ? size.width - 3 : 0;
859
+ f64 result = 0;
860
+ for(size_t k = 0; k < size.height; ++k)
861
+ {
862
+ const f32* src = internal::getRowPtr( srcBase, srcStride, k);
863
+ size_t i = 0;
864
+ for (; i < roiw4;)
865
+ {
866
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
867
+ float32x4_t s = vld1q_f32(src + i);
868
+ s = vmulq_f32(s, s);
869
+ for (i += 4; i <= limit; i += 4 )
870
+ {
871
+ internal::prefetch(src + i);
872
+ float32x4_t s1 = vld1q_f32(src + i);
873
+ s = vmlaq_f32(s, s1, s1);
874
+ }
875
+
876
+ f32 s2[4];
877
+ vst1q_f32(s2, s);
878
+
879
+ for (u32 j = 0; j < 4; j++)
880
+ result += (f64)(s2[j]);
881
+ }
882
+ for ( ; i < size.width; i++)
883
+ result += (f64)(src[i]) * (f64)(src[i]);
884
+ }
885
+ return result;
886
+ #else
887
+ (void)_size;
888
+ (void)srcBase;
889
+ (void)srcStride;
890
+
891
+ return 0.;
892
+ #endif
893
+ }
894
+
895
+ s32 diffNormInf(const Size2D &_size,
896
+ const u8 * src0Base, ptrdiff_t src0Stride,
897
+ const u8 * src1Base, ptrdiff_t src1Stride)
898
+ {
899
+ internal::assertSupportedConfiguration();
900
+ #ifdef CAROTENE_NEON
901
+ Size2D size(_size);
902
+ if (src0Stride == src1Stride &&
903
+ src0Stride == (ptrdiff_t)(size.width))
904
+ {
905
+ size.width *= size.height;
906
+ size.height = 1;
907
+ }
908
+ s32 result = 0;
909
+ for(size_t k = 0; k < size.height; ++k)
910
+ {
911
+ const u8* src1 = internal::getRowPtr( src0Base, src0Stride, k);
912
+ const u8* src2 = internal::getRowPtr( src1Base, src1Stride, k);
913
+ size_t i = 0;
914
+
915
+ if (size.width >= 16)
916
+ {
917
+ uint8x16_t vs3 = vdupq_n_u8(0);
918
+ for (; i < size.width - 16; i += 16)
919
+ {
920
+ internal::prefetch(src1 + i);
921
+ internal::prefetch(src2 + i);
922
+
923
+ uint8x16_t vs1 = vld1q_u8(src1 + i);
924
+ uint8x16_t vs2 = vld1q_u8(src2 + i);
925
+
926
+ vs3 = vmaxq_u8(vs3, vabdq_u8(vs1, vs2));
927
+ }
928
+
929
+ u8 s2[8];
930
+ vst1_u8(s2, vpmax_u8(vget_low_u8(vs3), vget_high_u8(vs3)));
931
+
932
+ for (u32 j = 0; j < 8; j++)
933
+ result = std::max((s32)(s2[j]), result);
934
+ }
935
+
936
+ for (; i < size.width; i++)
937
+ {
938
+ result = std::max(std::abs((s32)(src1[i]) - (s32)(src2[i])), result);
939
+ }
940
+ }
941
+ return result;
942
+ #else
943
+ (void)_size;
944
+ (void)src0Base;
945
+ (void)src0Stride;
946
+ (void)src1Base;
947
+ (void)src1Stride;
948
+
949
+ return 0;
950
+ #endif
951
+ }
952
+
953
+ f32 diffNormInf(const Size2D &_size,
954
+ const f32 * src0Base, ptrdiff_t src0Stride,
955
+ const f32 * src1Base, ptrdiff_t src1Stride)
956
+ {
957
+ internal::assertSupportedConfiguration();
958
+ #ifdef CAROTENE_NEON
959
+ Size2D size(_size);
960
+ if (src0Stride == src1Stride &&
961
+ src0Stride == (ptrdiff_t)(size.width))
962
+ {
963
+ size.width *= size.height;
964
+ size.height = 1;
965
+ }
966
+ f32 result = 0;
967
+ for(size_t k = 0; k < size.height; ++k)
968
+ {
969
+ const f32* src1 = internal::getRowPtr( src0Base, src0Stride, k);
970
+ const f32* src2 = internal::getRowPtr( src1Base, src1Stride, k);
971
+ size_t i = 0;
972
+
973
+ if (size.width >= 4)
974
+ {
975
+ float32x4_t s = vabdq_f32(vld1q_f32(src1), vld1q_f32(src2));
976
+
977
+ for (i += 4; i <= size.width - 4; i += 4 )
978
+ {
979
+ internal::prefetch(src1 + i);
980
+ internal::prefetch(src2 + i);
981
+
982
+ float32x4_t vs1 = vld1q_f32(src1 + i);
983
+ float32x4_t vs2 = vld1q_f32(src2 + i);
984
+
985
+ float32x4_t vd = vabdq_f32(vs2, vs1);
986
+ s = vmaxq_f32(s, vd);
987
+ }
988
+
989
+ f32 s2[4];
990
+ vst1q_f32(s2, s);
991
+
992
+ for (u32 j = 0; j < 4; j++)
993
+ if (s2[j] > result)
994
+ result = s2[j];
995
+ }
996
+
997
+ for (; i < size.width; i++)
998
+ {
999
+ f32 v = std::abs(src1[i] - src2[i]);
1000
+ if (v > result)
1001
+ result = v;
1002
+ }
1003
+ }
1004
+ return result;
1005
+ #else
1006
+ (void)_size;
1007
+ (void)src0Base;
1008
+ (void)src0Stride;
1009
+ (void)src1Base;
1010
+ (void)src1Stride;
1011
+
1012
+ return 0.;
1013
+ #endif
1014
+ }
1015
+
1016
+ s32 diffNormL1(const Size2D &_size,
1017
+ const u8 * src0Base, ptrdiff_t src0Stride,
1018
+ const u8 * src1Base, ptrdiff_t src1Stride)
1019
+ {
1020
+ internal::assertSupportedConfiguration();
1021
+ #ifdef CAROTENE_NEON
1022
+ Size2D size(_size);
1023
+ if (src0Stride == src1Stride &&
1024
+ src0Stride == (ptrdiff_t)(size.width))
1025
+ {
1026
+ size.width *= size.height;
1027
+ size.height = 1;
1028
+ }
1029
+ s32 result = 0;
1030
+ for(size_t k = 0; k < size.height; ++k)
1031
+ {
1032
+ const u8* src1 = internal::getRowPtr( src0Base, src0Stride, k);
1033
+ const u8* src2 = internal::getRowPtr( src1Base, src1Stride, k);
1034
+ size_t i = 0;
1035
+
1036
+ if (size.width >= 16)
1037
+ {
1038
+ for(; i <= size.width - 16;)
1039
+ {
1040
+ size_t limit = std::min(size.width, i + 2*256) - 16;
1041
+ uint16x8_t si1 = vmovq_n_u16(0);
1042
+ uint16x8_t si2 = vmovq_n_u16(0);
1043
+
1044
+ for (; i <= limit; i += 16)
1045
+ {
1046
+ internal::prefetch(src1 + i);
1047
+ internal::prefetch(src2 + i);
1048
+
1049
+ uint8x16_t vs1 = vld1q_u8(src1 + i);
1050
+ uint8x16_t vs2 = vld1q_u8(src2 + i);
1051
+
1052
+ si1 = vabal_u8(si1, vget_low_u8(vs1), vget_low_u8(vs2));
1053
+ si2 = vabal_u8(si2, vget_high_u8(vs1), vget_high_u8(vs2));
1054
+ }
1055
+
1056
+ u32 s2[4];
1057
+ vst1q_u32(s2, vaddq_u32(vpaddlq_u16(si1), vpaddlq_u16(si2)));
1058
+
1059
+ for (u32 j = 0; j < 4; j++)
1060
+ {
1061
+ if ((s32)(0x7fFFffFFu - s2[j]) <= result)
1062
+ {
1063
+ return 0x7fFFffFF; //result already saturated
1064
+ }
1065
+ result = (s32)((u32)(result) + s2[j]);
1066
+ }
1067
+ }
1068
+
1069
+ }
1070
+
1071
+ for (; i < size.width; i++)
1072
+ {
1073
+ u32 v = std::abs((s32)(src1[i]) - (s32)(src2[i]));
1074
+
1075
+ if ((s32)(0x7fFFffFFu - v) <= result)
1076
+ {
1077
+ return 0x7fFFffFF; //result already saturated
1078
+ }
1079
+ result = (s32)((u32)(result) + v);
1080
+ }
1081
+ }
1082
+ return result;
1083
+ #else
1084
+ (void)_size;
1085
+ (void)src0Base;
1086
+ (void)src0Stride;
1087
+ (void)src1Base;
1088
+ (void)src1Stride;
1089
+
1090
+ return 0;
1091
+ #endif
1092
+ }
1093
+
1094
+ f64 diffNormL1(const Size2D &_size,
1095
+ const f32 * src0Base, ptrdiff_t src0Stride,
1096
+ const f32 * src1Base, ptrdiff_t src1Stride)
1097
+ {
1098
+ internal::assertSupportedConfiguration();
1099
+ #ifdef CAROTENE_NEON
1100
+ Size2D size(_size);
1101
+ if (src0Stride == src1Stride &&
1102
+ src0Stride == (ptrdiff_t)(size.width))
1103
+ {
1104
+ size.width *= size.height;
1105
+ size.height = 1;
1106
+ }
1107
+ f64 result = 0;
1108
+ for(size_t k = 0; k < size.height; ++k)
1109
+ {
1110
+ const f32* src1 = internal::getRowPtr( src0Base, src0Stride, k);
1111
+ const f32* src2 = internal::getRowPtr( src1Base, src1Stride, k);
1112
+ size_t i = 0;
1113
+
1114
+ if (size.width >= 4)
1115
+ {
1116
+ for(; i <= size.width - 4;)
1117
+ {
1118
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
1119
+ float32x4_t s = vmovq_n_f32(0.0f);
1120
+
1121
+ for (; i <= limit; i += 4 )
1122
+ {
1123
+ internal::prefetch(src1 + i);
1124
+ internal::prefetch(src2 + i);
1125
+
1126
+ float32x4_t vs1 = vld1q_f32(src1 + i);
1127
+ float32x4_t vs2 = vld1q_f32(src2 + i);
1128
+
1129
+ float32x4_t vd = vabdq_f32(vs2, vs1);
1130
+ s = vaddq_f32(s, vd);
1131
+ }
1132
+
1133
+ f32 s2[4];
1134
+ vst1q_f32(s2, s);
1135
+
1136
+ for (u32 j = 0; j < 4; j++)
1137
+ result += (f64)(s2[j]);
1138
+ }
1139
+ }
1140
+
1141
+ for (; i < size.width; i++)
1142
+ {
1143
+ f32 v = std::abs(src1[i] - src2[i]);
1144
+ result += (f64)(v);
1145
+ }
1146
+ }
1147
+ return result;
1148
+ #else
1149
+ (void)_size;
1150
+ (void)src0Base;
1151
+ (void)src0Stride;
1152
+ (void)src1Base;
1153
+ (void)src1Stride;
1154
+
1155
+ return 0.;
1156
+ #endif
1157
+ }
1158
+
1159
+ s32 diffNormL2(const Size2D &_size,
1160
+ const u8 * src0Base, ptrdiff_t src0Stride,
1161
+ const u8 * src1Base, ptrdiff_t src1Stride)
1162
+ {
1163
+ internal::assertSupportedConfiguration();
1164
+ #ifdef CAROTENE_NEON
1165
+ Size2D size(_size);
1166
+ if (src0Stride == src1Stride &&
1167
+ src0Stride == (ptrdiff_t)(size.width))
1168
+ {
1169
+ size.width *= size.height;
1170
+ size.height = 1;
1171
+ }
1172
+ s32 result = 0;
1173
+ for(size_t k = 0; k < size.height; ++k)
1174
+ {
1175
+ const u8* src1 = internal::getRowPtr( src0Base, src0Stride, k);
1176
+ const u8* src2 = internal::getRowPtr( src1Base, src1Stride, k);
1177
+ size_t i = 0;
1178
+
1179
+ #define NORML28U_BLOCK_SIZE (33024*2) //bigger block size can result in integer overflow
1180
+ if (size.width >= 16)
1181
+ {
1182
+ for(; i <= size.width - 16;)
1183
+ {
1184
+ size_t limit = std::min(size.width, i + NORML28U_BLOCK_SIZE) - 16;
1185
+ uint32x4_t si1 = vmovq_n_u32(0);
1186
+ uint32x4_t si2 = vmovq_n_u32(0);
1187
+
1188
+ for (; i <= limit; i += 16)
1189
+ {
1190
+ internal::prefetch(src1 + i);
1191
+ internal::prefetch(src2 + i);
1192
+
1193
+ uint8x16_t vs1 = vld1q_u8(src1 + i);
1194
+ uint8x16_t vs2 = vld1q_u8(src2 + i);
1195
+
1196
+ uint16x8_t vdlo = vabdl_u8(vget_low_u8(vs1), vget_low_u8(vs2));
1197
+ uint16x8_t vdhi = vabdl_u8(vget_high_u8(vs1), vget_high_u8(vs2));
1198
+
1199
+ si1 = vmlal_u16(si1, vget_low_u16(vdlo), vget_low_u16(vdlo));
1200
+ si2 = vmlal_u16(si2, vget_high_u16(vdlo), vget_high_u16(vdlo));
1201
+
1202
+ si1 = vmlal_u16(si1, vget_low_u16(vdhi), vget_low_u16(vdhi));
1203
+ si2 = vmlal_u16(si2, vget_high_u16(vdhi), vget_high_u16(vdhi));
1204
+ }
1205
+
1206
+ u32 s2[4];
1207
+ vst1q_u32(s2, vqaddq_u32(si1, si2));
1208
+
1209
+ for (u32 j = 0; j < 4; j++)
1210
+ {
1211
+ if ((s32)(0x7fFFffFFu - s2[j]) <= result)
1212
+ {
1213
+ return 0x7fFFffFF; //result already saturated
1214
+ }
1215
+ result += (s32)s2[j];
1216
+ }
1217
+ }
1218
+
1219
+ }
1220
+
1221
+ for (; i < size.width; i++)
1222
+ {
1223
+ s32 v = (s32)(src1[i]) - (s32)(src2[i]);
1224
+ v *= v;
1225
+
1226
+ if ((s32)(0x7fFFffFFu - (u32)(v)) <= result)
1227
+ {
1228
+ return 0x7fFFffFF; //result already saturated
1229
+ }
1230
+ result += v;
1231
+ }
1232
+ }
1233
+ return result;
1234
+ #else
1235
+ (void)_size;
1236
+ (void)src0Base;
1237
+ (void)src0Stride;
1238
+ (void)src1Base;
1239
+ (void)src1Stride;
1240
+
1241
+ return 0;
1242
+ #endif
1243
+ }
1244
+
1245
+ f64 diffNormL2(const Size2D &_size,
1246
+ const f32 * src0Base, ptrdiff_t src0Stride,
1247
+ const f32 * src1Base, ptrdiff_t src1Stride)
1248
+ {
1249
+ internal::assertSupportedConfiguration();
1250
+ #ifdef CAROTENE_NEON
1251
+ Size2D size(_size);
1252
+ if (src0Stride == src1Stride &&
1253
+ src0Stride == (ptrdiff_t)(size.width))
1254
+ {
1255
+ size.width *= size.height;
1256
+ size.height = 1;
1257
+ }
1258
+ f64 result = 0;
1259
+ for(size_t k = 0; k < size.height; ++k)
1260
+ {
1261
+ const f32* src1 = internal::getRowPtr( src0Base, src0Stride, k);
1262
+ const f32* src2 = internal::getRowPtr( src1Base, src1Stride, k);
1263
+ size_t i = 0;
1264
+
1265
+ if (size.width >= 4)
1266
+ {
1267
+ for(; i <= size.width - 4;)
1268
+ {
1269
+ size_t limit = std::min(size.width, i + NORM32F_BLOCK_SIZE) - 4;
1270
+ float32x4_t s = vmovq_n_f32(0.0f);
1271
+
1272
+ for (; i <= limit; i += 4 )
1273
+ {
1274
+ internal::prefetch(src1 + i);
1275
+ internal::prefetch(src2 + i);
1276
+
1277
+ float32x4_t vs1 = vld1q_f32(src1 + i);
1278
+ float32x4_t vs2 = vld1q_f32(src2 + i);
1279
+
1280
+ float32x4_t vd = vsubq_f32(vs2,vs1);
1281
+ s = vmlaq_f32(s, vd, vd);
1282
+ }
1283
+
1284
+ f32 s2[4];
1285
+ vst1q_f32(s2, s);
1286
+
1287
+ for (u32 j = 0; j < 4; j++)
1288
+ result += (f64)(s2[j]);
1289
+ }
1290
+ }
1291
+
1292
+ for (; i < size.width; i++)
1293
+ {
1294
+ f32 v = src1[i] - src2[i];
1295
+ result += v * v;
1296
+ }
1297
+ }
1298
+ return result;
1299
+ #else
1300
+ (void)_size;
1301
+ (void)src0Base;
1302
+ (void)src0Stride;
1303
+ (void)src1Base;
1304
+ (void)src1Stride;
1305
+
1306
+ return 0.;
1307
+ #endif
1308
+ }
1309
+
1310
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/opticalflow.cpp ADDED
@@ -0,0 +1,539 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+ #include "saturate_cast.hpp"
42
+ #include <vector>
43
+ #include <float.h> // For FLT_EPSILON
44
+
45
+ namespace CAROTENE_NS {
46
+
47
+ #define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
48
+
49
+ /*
50
+ * Pyramidal Lucas-Kanade Optical Flow level processing
51
+ */
52
+ void pyrLKOptFlowLevel(const Size2D &size, s32 cn,
53
+ const u8 *prevData, ptrdiff_t prevStride,
54
+ const s16 *prevDerivData, ptrdiff_t prevDerivStride,
55
+ const u8 *nextData, ptrdiff_t nextStride,
56
+ u32 ptCount,
57
+ const f32 *prevPts, f32 *nextPts,
58
+ u8 *status, f32 *err,
59
+ const Size2D &winSize,
60
+ u32 terminationCount, f64 terminationEpsilon,
61
+ u32 level, u32 maxLevel, bool useInitialFlow, bool getMinEigenVals,
62
+ f32 minEigThreshold)
63
+ {
64
+ internal::assertSupportedConfiguration();
65
+ #ifdef CAROTENE_NEON
66
+ f32 halfWinX = (winSize.width-1)*0.5f, halfWinY = (winSize.height-1)*0.5f;
67
+ s32 cn2 = cn*2;
68
+
69
+ std::vector<s16> _buf(winSize.total()*(cn + cn2));
70
+ s16* IWinBuf = &_buf[0];
71
+ s32 IWinBufStride = winSize.width*cn;
72
+ s16* derivIWinBuf = &_buf[winSize.total()*cn];
73
+ s32 derivIWinBufStride = winSize.width*cn2;
74
+
75
+ for( u32 ptidx = 0; ptidx < ptCount; ptidx++ )
76
+ {
77
+ f32 levscale = (1./(1 << level));
78
+ u32 ptref = ptidx << 1;
79
+ f32 prevPtX = prevPts[ptref+0]*levscale;
80
+ f32 prevPtY = prevPts[ptref+1]*levscale;
81
+ f32 nextPtX;
82
+ f32 nextPtY;
83
+ if( level == maxLevel )
84
+ {
85
+ if( useInitialFlow )
86
+ {
87
+ nextPtX = nextPts[ptref+0]*levscale;
88
+ nextPtY = nextPts[ptref+1]*levscale;
89
+ }
90
+ else
91
+ {
92
+ nextPtX = prevPtX;
93
+ nextPtY = prevPtY;
94
+ }
95
+ }
96
+ else
97
+ {
98
+ nextPtX = nextPts[ptref+0]*2.f;
99
+ nextPtY = nextPts[ptref+1]*2.f;
100
+ }
101
+ nextPts[ptref+0] = nextPtX;
102
+ nextPts[ptref+1] = nextPtY;
103
+
104
+ s32 iprevPtX, iprevPtY;
105
+ s32 inextPtX, inextPtY;
106
+ prevPtX -= halfWinX;
107
+ prevPtY -= halfWinY;
108
+ iprevPtX = floor(prevPtX);
109
+ iprevPtY = floor(prevPtY);
110
+
111
+ if( iprevPtX < -(s32)winSize.width || iprevPtX >= (s32)size.width ||
112
+ iprevPtY < -(s32)winSize.height || iprevPtY >= (s32)size.height )
113
+ {
114
+ if( level == 0 )
115
+ {
116
+ if( status )
117
+ status[ptidx] = false;
118
+ if( err )
119
+ err[ptidx] = 0;
120
+ }
121
+ continue;
122
+ }
123
+
124
+ f32 a = prevPtX - iprevPtX;
125
+ f32 b = prevPtY - iprevPtY;
126
+ const s32 W_BITS = 14, W_BITS1 = 14;
127
+ const f32 FLT_SCALE = 1.f/(1 << 20);
128
+ s32 iw00 = round((1.f - a)*(1.f - b)*(1 << W_BITS));
129
+ s32 iw01 = round(a*(1.f - b)*(1 << W_BITS));
130
+ s32 iw10 = round((1.f - a)*b*(1 << W_BITS));
131
+ s32 iw11 = (1 << W_BITS) - iw00 - iw01 - iw10;
132
+
133
+ s32 dstep = prevDerivStride/sizeof(s16);
134
+ f32 A11 = 0, A12 = 0, A22 = 0;
135
+
136
+ int16x4_t viw00 = vmov_n_s16((s16)iw00);
137
+ int16x4_t viw01 = vmov_n_s16((s16)iw01);
138
+ int16x4_t viw10 = vmov_n_s16((s16)iw10);
139
+ int16x4_t viw11 = vmov_n_s16((s16)iw11);
140
+
141
+ float32x4_t vA11 = vmovq_n_f32(0);
142
+ float32x4_t vA12 = vmovq_n_f32(0);
143
+ float32x4_t vA22 = vmovq_n_f32(0);
144
+
145
+ s32 wwcn = winSize.width*cn;
146
+
147
+ // extract the patch from the first image, compute covariation matrix of derivatives
148
+ s32 x = 0;
149
+ for(s32 y = 0; y < (s32)winSize.height; y++ )
150
+ {
151
+ const u8* src = prevData + prevStride*(y + iprevPtY) + iprevPtX*cn;
152
+ const s16* dsrc = prevDerivData + dstep*(y + iprevPtY) + iprevPtX*cn2;
153
+
154
+ s16* Iptr = IWinBuf + y*IWinBufStride;
155
+ s16* dIptr = derivIWinBuf + y*derivIWinBufStride;
156
+
157
+ internal::prefetch(src + x + prevStride * 2, 0);
158
+ for(x = 0; x <= wwcn - 8; x += 8)
159
+ {
160
+ uint8x8_t vsrc00 = vld1_u8(src + x);
161
+ uint8x8_t vsrc10 = vld1_u8(src + x + prevStride);
162
+ uint8x8_t vsrc01 = vld1_u8(src + x + cn);
163
+ uint8x8_t vsrc11 = vld1_u8(src + x + prevStride + cn);
164
+
165
+ int16x8_t vs00 = vreinterpretq_s16_u16(vmovl_u8(vsrc00));
166
+ int16x8_t vs10 = vreinterpretq_s16_u16(vmovl_u8(vsrc10));
167
+ int16x8_t vs01 = vreinterpretq_s16_u16(vmovl_u8(vsrc01));
168
+ int16x8_t vs11 = vreinterpretq_s16_u16(vmovl_u8(vsrc11));
169
+
170
+ int32x4_t vsuml = vmull_s16(vget_low_s16(vs00), viw00);
171
+ int32x4_t vsumh = vmull_s16(vget_high_s16(vs10), viw10);
172
+
173
+ vsuml = vmlal_s16(vsuml, vget_low_s16(vs01), viw01);
174
+ vsumh = vmlal_s16(vsumh, vget_high_s16(vs11), viw11);
175
+
176
+ vsuml = vmlal_s16(vsuml, vget_low_s16(vs10), viw10);
177
+ vsumh = vmlal_s16(vsumh, vget_high_s16(vs00), viw00);
178
+
179
+ vsuml = vmlal_s16(vsuml, vget_low_s16(vs11), viw11);
180
+ vsumh = vmlal_s16(vsumh, vget_high_s16(vs01), viw01);
181
+
182
+ int16x4_t vsumnl = vrshrn_n_s32(vsuml, W_BITS1-5);
183
+ int16x4_t vsumnh = vrshrn_n_s32(vsumh, W_BITS1-5);
184
+
185
+ vst1q_s16(Iptr + x, vcombine_s16(vsumnl, vsumnh));
186
+ }
187
+ for(; x <= wwcn - 4; x += 4)
188
+ {
189
+ uint8x8_t vsrc00 = vld1_u8(src + x);
190
+ uint8x8_t vsrc10 = vld1_u8(src + x + prevStride);
191
+ uint8x8_t vsrc01 = vld1_u8(src + x + cn);
192
+ uint8x8_t vsrc11 = vld1_u8(src + x + prevStride + cn);
193
+
194
+ int16x4_t vs00 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(vsrc00)));
195
+ int16x4_t vs10 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(vsrc10)));
196
+ int16x4_t vs01 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(vsrc01)));
197
+ int16x4_t vs11 = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(vsrc11)));
198
+
199
+ int32x4_t vsuml1 = vmull_s16(vs00, viw00);
200
+ int32x4_t vsuml2 = vmull_s16(vs01, viw01);
201
+ vsuml1 = vmlal_s16(vsuml1, vs10, viw10);
202
+ vsuml2 = vmlal_s16(vsuml2, vs11, viw11);
203
+ int32x4_t vsuml = vaddq_s32(vsuml1, vsuml2);
204
+
205
+ int16x4_t vsumnl = vrshrn_n_s32(vsuml, W_BITS1-5);
206
+
207
+ vst1_s16(Iptr + x, vsumnl);
208
+ }
209
+
210
+ internal::prefetch(dsrc + dstep * 2, 0);
211
+ for(x = 0; x <= wwcn - 4; x += 4, dsrc += 4*2, dIptr += 4*2 )
212
+ {
213
+ #if 0
214
+ __asm__ (
215
+ "vld2.16 {d0-d1}, [%[dsrc00]] \n\t"
216
+ "vld2.16 {d2-d3}, [%[dsrc10]] \n\t"
217
+ "vld2.16 {d4-d5}, [%[dsrc01]] \n\t"
218
+ "vld2.16 {d6-d7}, [%[dsrc11]] \n\t"
219
+ "vmull.s16 q4, d3, %P[viw10] \n\t"
220
+ "vmull.s16 q5, d0, %P[viw00] \n\t"
221
+ "vmlal.s16 q4, d7, %P[viw11] \n\t"
222
+ "vmlal.s16 q5, d4, %P[viw01] \n\t"
223
+ "vmlal.s16 q4, d1, %P[viw00] \n\t"
224
+ "vmlal.s16 q5, d2, %P[viw10] \n\t"
225
+ "vmlal.s16 q4, d5, %P[viw01] \n\t"
226
+ "vmlal.s16 q5, d6, %P[viw11] \n\t"
227
+ "vrshrn.s32 d13, q4, %[W_BITS1] \n\t"
228
+ "vrshrn.s32 d12, q5, %[W_BITS1] \n\t"
229
+ "vmull.s16 q3, d13, d13 \n\t"
230
+ "vmull.s16 q4, d12, d12 \n\t"
231
+ "vmull.s16 q5, d13, d12 \n\t"
232
+ "vcvt.f32.s32 q3, q3 \n\t"
233
+ "vcvt.f32.s32 q4, q4 \n\t"
234
+ "vcvt.f32.s32 q5, q5 \n\t"
235
+ "vadd.f32 %q[vA22], q3 \n\t"
236
+ "vadd.f32 %q[vA11], q4 \n\t"
237
+ "vadd.f32 %q[vA12], q5 \n\t"
238
+ "vst2.16 {d12-d13}, [%[out]] \n\t"
239
+ : [vA22] "=w" (vA22),
240
+ [vA11] "=w" (vA11),
241
+ [vA12] "=w" (vA12)
242
+ : "0" (vA22),
243
+ "1" (vA11),
244
+ "2" (vA12),
245
+ [out] "r" (dIptr),
246
+ [dsrc00] "r" (dsrc),
247
+ [dsrc10] "r" (dsrc + dstep),
248
+ [dsrc01] "r" (dsrc + cn2),
249
+ [dsrc11] "r" (dsrc + dstep + cn2),
250
+ [viw00] "w" (viw00),
251
+ [viw10] "w" (viw10),
252
+ [viw01] "w" (viw01),
253
+ [viw11] "w" (viw11),
254
+ [W_BITS1] "I" (W_BITS1)
255
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13"
256
+ );
257
+ #else
258
+ int16x4x2_t vdsrc00 = vld2_s16(dsrc);
259
+ int16x4x2_t vdsrc10 = vld2_s16(dsrc + dstep);
260
+ int16x4x2_t vdsrc01 = vld2_s16(dsrc + cn2);
261
+ int16x4x2_t vdsrc11 = vld2_s16(dsrc + dstep + cn2);
262
+
263
+ int32x4_t vsumy = vmull_s16(vdsrc10.val[1], viw10);
264
+ int32x4_t vsumx = vmull_s16(vdsrc00.val[0], viw00);
265
+
266
+ vsumy = vmlal_s16(vsumy, vdsrc11.val[1], viw11);
267
+ vsumx = vmlal_s16(vsumx, vdsrc01.val[0], viw01);
268
+
269
+ vsumy = vmlal_s16(vsumy, vdsrc00.val[1], viw00);
270
+ vsumx = vmlal_s16(vsumx, vdsrc10.val[0], viw10);
271
+
272
+ vsumy = vmlal_s16(vsumy, vdsrc01.val[1], viw01);
273
+ vsumx = vmlal_s16(vsumx, vdsrc11.val[0], viw11);
274
+
275
+ int16x4_t vsumny = vrshrn_n_s32(vsumy, W_BITS1);
276
+ int16x4_t vsumnx = vrshrn_n_s32(vsumx, W_BITS1);
277
+
278
+ int32x4_t va22i = vmull_s16(vsumny, vsumny);
279
+ int32x4_t va11i = vmull_s16(vsumnx, vsumnx);
280
+ int32x4_t va12i = vmull_s16(vsumnx, vsumny);
281
+
282
+ float32x4_t va22f = vcvtq_f32_s32(va22i);
283
+ float32x4_t va11f = vcvtq_f32_s32(va11i);
284
+ float32x4_t va12f = vcvtq_f32_s32(va12i);
285
+
286
+ vA22 = vaddq_f32(vA22, va22f);
287
+ vA11 = vaddq_f32(vA11, va11f);
288
+ vA12 = vaddq_f32(vA12, va12f);
289
+
290
+ int16x4x2_t vsum;
291
+ vsum.val[0] = vsumnx;
292
+ vsum.val[1] = vsumny;
293
+ vst2_s16(dIptr, vsum);
294
+ #endif
295
+ }
296
+
297
+ for( ; x < wwcn; x++, dsrc += 2, dIptr += 2 )
298
+ {
299
+ s32 ival = CV_DESCALE(src[x]*iw00 + src[x+cn]*iw01 +
300
+ src[x+prevStride]*iw10 + src[x+prevStride+cn]*iw11, W_BITS1-5);
301
+ s32 ixval = CV_DESCALE(dsrc[0]*iw00 + dsrc[cn2]*iw01 +
302
+ dsrc[dstep]*iw10 + dsrc[dstep+cn2]*iw11, W_BITS1);
303
+ s32 iyval = CV_DESCALE(dsrc[1]*iw00 + dsrc[cn2+1]*iw01 + dsrc[dstep+1]*iw10 +
304
+ dsrc[dstep+cn2+1]*iw11, W_BITS1);
305
+ Iptr[x] = (s16)ival;
306
+ dIptr[0] = (s16)ixval;
307
+ dIptr[1] = (s16)iyval;
308
+
309
+ A11 += (f32)(ixval*ixval);
310
+ A12 += (f32)(ixval*iyval);
311
+ A22 += (f32)(iyval*iyval);
312
+ }
313
+ }
314
+
315
+ f32 A11buf[2], A12buf[2], A22buf[2];
316
+ vst1_f32(A11buf, vadd_f32(vget_low_f32(vA11), vget_high_f32(vA11)));
317
+ vst1_f32(A12buf, vadd_f32(vget_low_f32(vA12), vget_high_f32(vA12)));
318
+ vst1_f32(A22buf, vadd_f32(vget_low_f32(vA22), vget_high_f32(vA22)));
319
+ A11 += A11buf[0] + A11buf[1];
320
+ A12 += A12buf[0] + A12buf[1];
321
+ A22 += A22buf[0] + A22buf[1];
322
+
323
+ A11 *= FLT_SCALE;
324
+ A12 *= FLT_SCALE;
325
+ A22 *= FLT_SCALE;
326
+
327
+ f32 D = A11*A22 - A12*A12;
328
+ f32 minEig = (A22 + A11 - std::sqrt((A11-A22)*(A11-A22) +
329
+ 4.f*A12*A12))/(2*winSize.width*winSize.height);
330
+
331
+ if( err && getMinEigenVals )
332
+ err[ptidx] = (f32)minEig;
333
+
334
+ if( minEig < minEigThreshold || D < FLT_EPSILON )
335
+ {
336
+ if( level == 0 && status )
337
+ status[ptidx] = false;
338
+ continue;
339
+ }
340
+
341
+ D = 1.f/D;
342
+
343
+ nextPtX -= halfWinX;
344
+ nextPtY -= halfWinY;
345
+ f32 prevDeltaX = 0;
346
+ f32 prevDeltaY = 0;
347
+
348
+ for(u32 j = 0; j < terminationCount; j++ )
349
+ {
350
+ inextPtX = floor(nextPtX);
351
+ inextPtY = floor(nextPtY);
352
+
353
+ if( inextPtX < -(s32)winSize.width || inextPtX >= (s32)size.width ||
354
+ inextPtY < -(s32)winSize.height || inextPtY >= (s32)size.height )
355
+ {
356
+ if( level == 0 && status )
357
+ status[ptidx] = false;
358
+ break;
359
+ }
360
+
361
+ a = nextPtX - inextPtX;
362
+ b = nextPtY - inextPtY;
363
+ iw00 = round((1.f - a)*(1.f - b)*(1 << W_BITS));
364
+ iw01 = round(a*(1.f - b)*(1 << W_BITS));
365
+ iw10 = round((1.f - a)*b*(1 << W_BITS));
366
+ iw11 = (1 << W_BITS) - iw00 - iw01 - iw10;
367
+ f32 b1 = 0, b2 = 0;
368
+
369
+ viw00 = vmov_n_s16((s16)iw00);
370
+ viw01 = vmov_n_s16((s16)iw01);
371
+ viw10 = vmov_n_s16((s16)iw10);
372
+ viw11 = vmov_n_s16((s16)iw11);
373
+
374
+ float32x4_t vb1 = vmovq_n_f32(0);
375
+ float32x4_t vb2 = vmovq_n_f32(0);
376
+
377
+ for(s32 y = 0; y < (s32)winSize.height; y++ )
378
+ {
379
+ const u8* Jptr = nextData + nextStride*(y + inextPtY) + inextPtX*cn;
380
+ const s16* Iptr = IWinBuf + y*IWinBufStride;
381
+ const s16* dIptr = derivIWinBuf + y*derivIWinBufStride;
382
+
383
+ x = 0;
384
+
385
+ internal::prefetch(Jptr, nextStride * 2);
386
+ internal::prefetch(Iptr, IWinBufStride/2);
387
+ internal::prefetch(dIptr, derivIWinBufStride/2);
388
+
389
+ for( ; x <= wwcn - 8; x += 8, dIptr += 8*2 )
390
+ {
391
+ uint8x8_t vj00 = vld1_u8(Jptr + x);
392
+ uint8x8_t vj10 = vld1_u8(Jptr + x + nextStride);
393
+ uint8x8_t vj01 = vld1_u8(Jptr + x + cn);
394
+ uint8x8_t vj11 = vld1_u8(Jptr + x + nextStride + cn);
395
+ int16x8_t vI = vld1q_s16(Iptr + x);
396
+ int16x8x2_t vDerivI = vld2q_s16(dIptr);
397
+
398
+ int16x8_t vs00 = vreinterpretq_s16_u16(vmovl_u8(vj00));
399
+ int16x8_t vs10 = vreinterpretq_s16_u16(vmovl_u8(vj10));
400
+ int16x8_t vs01 = vreinterpretq_s16_u16(vmovl_u8(vj01));
401
+ int16x8_t vs11 = vreinterpretq_s16_u16(vmovl_u8(vj11));
402
+
403
+ int32x4_t vsuml = vmull_s16(vget_low_s16(vs00), viw00);
404
+ int32x4_t vsumh = vmull_s16(vget_high_s16(vs10), viw10);
405
+
406
+ vsuml = vmlal_s16(vsuml, vget_low_s16(vs01), viw01);
407
+ vsumh = vmlal_s16(vsumh, vget_high_s16(vs11), viw11);
408
+
409
+ vsuml = vmlal_s16(vsuml, vget_low_s16(vs10), viw10);
410
+ vsumh = vmlal_s16(vsumh, vget_high_s16(vs00), viw00);
411
+
412
+ vsuml = vmlal_s16(vsuml, vget_low_s16(vs11), viw11);
413
+ vsumh = vmlal_s16(vsumh, vget_high_s16(vs01), viw01);
414
+
415
+ int16x4_t vsumnl = vrshrn_n_s32(vsuml, W_BITS1-5);
416
+ int16x4_t vsumnh = vrshrn_n_s32(vsumh, W_BITS1-5);
417
+
418
+ int16x8_t diff = vqsubq_s16(vcombine_s16(vsumnl, vsumnh), vI);
419
+
420
+ int32x4_t vb1l = vmull_s16(vget_low_s16(diff), vget_low_s16(vDerivI.val[0]));
421
+ int32x4_t vb2h = vmull_s16(vget_high_s16(diff), vget_high_s16(vDerivI.val[1]));
422
+ int32x4_t vb1i = vmlal_s16(vb1l, vget_high_s16(diff), vget_high_s16(vDerivI.val[0]));
423
+ int32x4_t vb2i = vmlal_s16(vb2h, vget_low_s16(diff), vget_low_s16(vDerivI.val[1]));
424
+
425
+ float32x4_t vb1f = vcvtq_f32_s32(vb1i);
426
+ float32x4_t vb2f = vcvtq_f32_s32(vb2i);
427
+
428
+ vb1 = vaddq_f32(vb1, vb1f);
429
+ vb2 = vaddq_f32(vb2, vb2f);
430
+ }
431
+
432
+ for( ; x < wwcn; x++, dIptr += 2 )
433
+ {
434
+ s32 diff = CV_DESCALE(Jptr[x]*iw00 + Jptr[x+cn]*iw01 +
435
+ Jptr[x+nextStride]*iw10 + Jptr[x+nextStride+cn]*iw11,
436
+ W_BITS1-5) - Iptr[x];
437
+ b1 += (f32)(diff*dIptr[0]);
438
+ b2 += (f32)(diff*dIptr[1]);
439
+ }
440
+ }
441
+
442
+ f32 bbuf[2];
443
+ float32x2_t vb = vpadd_f32(vadd_f32(vget_low_f32(vb1), vget_high_f32(vb1)), vadd_f32(vget_low_f32(vb2), vget_high_f32(vb2)));
444
+ vst1_f32(bbuf, vb);
445
+ b1 += bbuf[0];
446
+ b2 += bbuf[1];
447
+
448
+ b1 *= FLT_SCALE;
449
+ b2 *= FLT_SCALE;
450
+
451
+ f32 deltaX = (f32)((A12*b2 - A22*b1) * D);
452
+ f32 deltaY = (f32)((A12*b1 - A11*b2) * D);
453
+
454
+ nextPtX += deltaX;
455
+ nextPtY += deltaY;
456
+ nextPts[ptref+0] = nextPtX + halfWinX;
457
+ nextPts[ptref+1] = nextPtY + halfWinY;
458
+
459
+ if( ((double)deltaX*deltaX + (double)deltaY*deltaY) <= terminationEpsilon )
460
+ break;
461
+
462
+ if( j > 0 && std::abs(deltaX + prevDeltaX) < 0.01 &&
463
+ std::abs(deltaY + prevDeltaY) < 0.01 )
464
+ {
465
+ nextPts[ptref+0] -= deltaX*0.5f;
466
+ nextPts[ptref+1] -= deltaY*0.5f;
467
+ break;
468
+ }
469
+ prevDeltaX = deltaX;
470
+ prevDeltaY = deltaY;
471
+ }
472
+
473
+ if( status && status[ptidx] && err && level == 0 && !getMinEigenVals )
474
+ {
475
+ f32 nextPointX = nextPts[ptref+0] - halfWinX;
476
+ f32 nextPointY = nextPts[ptref+1] - halfWinY;
477
+
478
+ s32 inextPointX = floor(nextPointX);
479
+ s32 inextPointY = floor(nextPointY);
480
+
481
+ if( inextPointX < -(s32)winSize.width || inextPointX >= (s32)size.width ||
482
+ inextPointY < -(s32)winSize.height || inextPointY >= (s32)size.height )
483
+ {
484
+ if( status )
485
+ status[ptidx] = false;
486
+ continue;
487
+ }
488
+
489
+ f32 aa = nextPointX - inextPointX;
490
+ f32 bb = nextPointY - inextPointY;
491
+ iw00 = round((1.f - aa)*(1.f - bb)*(1 << W_BITS));
492
+ iw01 = round(aa*(1.f - bb)*(1 << W_BITS));
493
+ iw10 = round((1.f - aa)*bb*(1 << W_BITS));
494
+ iw11 = (1 << W_BITS) - iw00 - iw01 - iw10;
495
+ f32 errval = 0.f;
496
+
497
+ for(s32 y = 0; y < (s32)winSize.height; y++ )
498
+ {
499
+ const u8* Jptr = nextData + nextStride*(y + inextPointY) + inextPointX*cn;
500
+ const s16* Iptr = IWinBuf + y*IWinBufStride;
501
+
502
+ for( x = 0; x < wwcn; x++ )
503
+ {
504
+ s32 diff = CV_DESCALE(Jptr[x]*iw00 + Jptr[x+cn]*iw01 +
505
+ Jptr[x+nextStride]*iw10 + Jptr[x+nextStride+cn]*iw11,
506
+ W_BITS1-5) - Iptr[x];
507
+ errval += std::abs((f32)diff);
508
+ }
509
+ }
510
+ err[ptidx] = errval / (32*wwcn*winSize.height);
511
+ }
512
+ }
513
+ #else
514
+ (void)size;
515
+ (void)cn;
516
+ (void)prevData;
517
+ (void)prevStride;
518
+ (void)prevDerivData;
519
+ (void)prevDerivStride;
520
+ (void)nextData;
521
+ (void)nextStride;
522
+ (void)prevPts;
523
+ (void)nextPts;
524
+ (void)status;
525
+ (void)err;
526
+ (void)winSize;
527
+ (void)terminationCount;
528
+ (void)terminationEpsilon;
529
+ (void)level;
530
+ (void)maxLevel;
531
+ (void)useInitialFlow;
532
+ (void)getMinEigenVals;
533
+ (void)minEigThreshold;
534
+ (void)ptCount;
535
+ #endif
536
+ }
537
+
538
+ }//CAROTENE_NS
539
+
3rdparty/carotene/src/phase.cpp ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include <cfloat>
41
+ #include <cmath>
42
+
43
+ #include "common.hpp"
44
+ #include "vround_helper.hpp"
45
+
46
+ namespace CAROTENE_NS {
47
+
48
+ #ifdef CAROTENE_NEON
49
+
50
+ namespace {
51
+
52
+ #define FASTATAN2CONST(scale) \
53
+ f32 P1((f32)( 0.9997878412794807 * (180.0 / M_PI) * scale)), \
54
+ P3((f32)(-0.3258083974640975 * (180.0 / M_PI) * scale)), \
55
+ P5((f32)( 0.1555786518463281 * (180.0 / M_PI) * scale)), \
56
+ P7((f32)(-0.04432655554792128 * (180.0 / M_PI) * scale)), \
57
+ A_90((f32)(90.f * scale)), \
58
+ A_180((f32)(180.f * scale)), \
59
+ A_360((f32)(360.f * scale)); \
60
+ float32x4_t eps(vdupq_n_f32((float)DBL_EPSILON)), \
61
+ _90(vdupq_n_f32(A_90)), \
62
+ _180(vdupq_n_f32(A_180)), \
63
+ _360(vdupq_n_f32(A_360)), \
64
+ z(vdupq_n_f32(0.0f)), \
65
+ p1(vdupq_n_f32(P1)), \
66
+ p3(vdupq_n_f32(P3)), \
67
+ p5(vdupq_n_f32(P5)), \
68
+ p7(vdupq_n_f32(P7));
69
+
70
+ #define FASTATAN2SCALAR(y, x, a) \
71
+ { \
72
+ f32 ax = std::abs(x), ay = std::abs(y); \
73
+ f32 c, c2; \
74
+ if (ax >= ay) \
75
+ { \
76
+ c = ay / (ax + (float)DBL_EPSILON); \
77
+ c2 = c * c; \
78
+ a = (((P7 * c2 + P5) * c2 + P3) * c2 + P1) * c; \
79
+ } \
80
+ else \
81
+ { \
82
+ c = ax / (ay + (float)DBL_EPSILON); \
83
+ c2 = c * c; \
84
+ a = A_90 - (((P7 * c2 + P5) * c2 + P3) * c2 + P1) * c; \
85
+ } \
86
+ if (x < 0) \
87
+ a = A_180 - a; \
88
+ if (y < 0) \
89
+ a = A_360 - a; \
90
+ }
91
+
92
+ #define FASTATAN2VECTOR(v_y, v_x, a) \
93
+ { \
94
+ float32x4_t ax = vabsq_f32(v_x), ay = vabsq_f32(v_y); \
95
+ float32x4_t tmin = vminq_f32(ax, ay), tmax = vmaxq_f32(ax, ay); \
96
+ float32x4_t c = vmulq_f32(tmin, internal::vrecpq_f32(vaddq_f32(tmax, eps))); \
97
+ float32x4_t c2 = vmulq_f32(c, c); \
98
+ a = vmulq_f32(c2, p7); \
99
+ \
100
+ a = vmulq_f32(vaddq_f32(a, p5), c2); \
101
+ a = vmulq_f32(vaddq_f32(a, p3), c2); \
102
+ a = vmulq_f32(vaddq_f32(a, p1), c); \
103
+ \
104
+ a = vbslq_f32(vcgeq_f32(ax, ay), a, vsubq_f32(_90, a)); \
105
+ a = vbslq_f32(vcltq_f32(v_x, z), vsubq_f32(_180, a), a); \
106
+ a = vbslq_f32(vcltq_f32(v_y, z), vsubq_f32(_360, a), a); \
107
+ \
108
+ }
109
+
110
+ } // namespace
111
+
112
+ #endif
113
+
114
+ void phase(const Size2D &size,
115
+ const s16 * src0Base, ptrdiff_t src0Stride,
116
+ const s16 * src1Base, ptrdiff_t src1Stride,
117
+ u8 * dstBase, ptrdiff_t dstStride)
118
+ {
119
+ internal::assertSupportedConfiguration();
120
+ #ifdef CAROTENE_NEON
121
+ FASTATAN2CONST(256.0f / 360.0f)
122
+ size_t roiw16 = size.width >= 15 ? size.width - 15 : 0;
123
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
124
+
125
+ for (size_t i = 0; i < size.height; ++i)
126
+ {
127
+ const s16 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
128
+ const s16 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
129
+ u8 * dst = internal::getRowPtr(dstBase, dstStride, i);
130
+ size_t j = 0;
131
+
132
+ for (; j < roiw16; j += 16)
133
+ {
134
+ internal::prefetch(src0 + j);
135
+ internal::prefetch(src1 + j);
136
+
137
+ int16x8_t v_src00 = vld1q_s16(src0 + j), v_src01 = vld1q_s16(src0 + j + 8);
138
+ int16x8_t v_src10 = vld1q_s16(src1 + j), v_src11 = vld1q_s16(src1 + j + 8);
139
+
140
+ // 0
141
+ float32x4_t v_src0_p = vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src00)));
142
+ float32x4_t v_src1_p = vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src10)));
143
+ float32x4_t v_dst32f0;
144
+ FASTATAN2VECTOR(v_src1_p, v_src0_p, v_dst32f0)
145
+
146
+ v_src0_p = vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src00)));
147
+ v_src1_p = vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src10)));
148
+ float32x4_t v_dst32f1;
149
+ FASTATAN2VECTOR(v_src1_p, v_src0_p, v_dst32f1)
150
+
151
+ uint16x8_t v_dst16s0 = vcombine_u16(vmovn_u32(internal::vroundq_u32_f32(v_dst32f0)),
152
+ vmovn_u32(internal::vroundq_u32_f32(v_dst32f1)));
153
+
154
+ // 1
155
+ v_src0_p = vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src01)));
156
+ v_src1_p = vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src11)));
157
+ FASTATAN2VECTOR(v_src1_p, v_src0_p, v_dst32f0)
158
+
159
+ v_src0_p = vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src01)));
160
+ v_src1_p = vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src11)));
161
+ FASTATAN2VECTOR(v_src1_p, v_src0_p, v_dst32f1)
162
+
163
+ uint16x8_t v_dst16s1 = vcombine_u16(vmovn_u32(internal::vroundq_u32_f32(v_dst32f0)),
164
+ vmovn_u32(internal::vroundq_u32_f32(v_dst32f1)));
165
+
166
+ vst1q_u8(dst + j, vcombine_u8(vmovn_u16(v_dst16s0),
167
+ vmovn_u16(v_dst16s1)));
168
+ }
169
+ for (; j < roiw8; j += 8)
170
+ {
171
+ int16x8_t v_src0 = vld1q_s16(src0 + j);
172
+ int16x8_t v_src1 = vld1q_s16(src1 + j);
173
+
174
+ float32x4_t v_src0_p = vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src0)));
175
+ float32x4_t v_src1_p = vcvtq_f32_s32(vmovl_s16(vget_low_s16(v_src1)));
176
+ float32x4_t v_dst32f0;
177
+ FASTATAN2VECTOR(v_src1_p, v_src0_p, v_dst32f0)
178
+
179
+ v_src0_p = vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src0)));
180
+ v_src1_p = vcvtq_f32_s32(vmovl_s16(vget_high_s16(v_src1)));
181
+ float32x4_t v_dst32f1;
182
+ FASTATAN2VECTOR(v_src1_p, v_src0_p, v_dst32f1)
183
+
184
+ uint16x8_t v_dst = vcombine_u16(vmovn_u32(internal::vroundq_u32_f32(v_dst32f0)),
185
+ vmovn_u32(internal::vroundq_u32_f32(v_dst32f1)));
186
+
187
+ vst1_u8(dst + j, vmovn_u16(v_dst));
188
+ }
189
+
190
+ for (; j < size.width; j++)
191
+ {
192
+ f32 x = src0[j], y = src1[j];
193
+ f32 a;
194
+ FASTATAN2SCALAR(y, x, a)
195
+ dst[j] = (u8)(s32)floor(a + 0.5f);
196
+ }
197
+ }
198
+ #else
199
+ (void)size;
200
+ (void)src0Base;
201
+ (void)src0Stride;
202
+ (void)src1Base;
203
+ (void)src1Stride;
204
+ (void)dstBase;
205
+ (void)dstStride;
206
+ #endif
207
+ }
208
+
209
+ void phase(const Size2D &size,
210
+ const f32 * src0Base, ptrdiff_t src0Stride,
211
+ const f32 * src1Base, ptrdiff_t src1Stride,
212
+ f32 * dstBase, ptrdiff_t dstStride,
213
+ f32 scale)
214
+ {
215
+ internal::assertSupportedConfiguration();
216
+ #ifdef CAROTENE_NEON
217
+ FASTATAN2CONST(scale)
218
+ size_t roiw8 = size.width >= 7 ? size.width - 7 : 0;
219
+
220
+ for (size_t i = 0; i < size.height; ++i)
221
+ {
222
+ const f32 * src0 = internal::getRowPtr(src0Base, src0Stride, i);
223
+ const f32 * src1 = internal::getRowPtr(src1Base, src1Stride, i);
224
+ f32 * dst = internal::getRowPtr(dstBase, dstStride, i);
225
+ size_t j = 0;
226
+
227
+ for (; j < roiw8; j += 8)
228
+ {
229
+ internal::prefetch(src0 + j);
230
+ internal::prefetch(src1 + j);
231
+
232
+ float32x4_t v_src00 = vld1q_f32(src0 + j), v_src01 = vld1q_f32(src0 + j + 4);
233
+ float32x4_t v_src10 = vld1q_f32(src1 + j), v_src11 = vld1q_f32(src1 + j + 4);
234
+
235
+ float32x4_t v_dst32f;
236
+ // 0
237
+ FASTATAN2VECTOR(v_src10, v_src00, v_dst32f)
238
+ vst1q_f32(dst + j, v_dst32f);
239
+ // 1
240
+ FASTATAN2VECTOR(v_src11, v_src01, v_dst32f)
241
+ vst1q_f32(dst + j + 4, v_dst32f);
242
+ }
243
+ if(j + 4 <= size.width)
244
+ {
245
+ float32x4_t v_src0 = vld1q_f32(src0 + j);
246
+ float32x4_t v_src1 = vld1q_f32(src1 + j);
247
+
248
+ float32x4_t v_dst32f;
249
+ FASTATAN2VECTOR(v_src1, v_src0, v_dst32f)
250
+ vst1q_f32(dst + j, v_dst32f);
251
+ j += 4;
252
+ }
253
+
254
+ for (; j < size.width; j++)
255
+ {
256
+ f32 a;
257
+ FASTATAN2SCALAR(src1[j], src0[j], a)
258
+ dst[j] = a;
259
+ }
260
+ }
261
+ #else
262
+ (void)size;
263
+ (void)src0Base;
264
+ (void)src0Stride;
265
+ (void)src1Base;
266
+ (void)src1Stride;
267
+ (void)dstBase;
268
+ (void)dstStride;
269
+ (void)scale;
270
+ #endif
271
+ }
272
+
273
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/pyramid.cpp ADDED
@@ -0,0 +1,1414 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include <vector>
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ bool isGaussianPyramidDownRTZSupported(const Size2D &srcSize, const Size2D &dstSize, BORDER_MODE border_mode)
47
+ {
48
+ if (!isSupportedConfiguration())
49
+ return false;
50
+ // Need at least 8 pixels for vectorization.
51
+ // Need to make sure dst width is half the src width.
52
+ // Don't care about dst height.
53
+ if ( dstSize.width < 8 || std::abs((ptrdiff_t)dstSize.width*2 - (ptrdiff_t)srcSize.width) > 2 )
54
+ return false;
55
+
56
+ // Current implementation only supports Reflect101 (ie: UNDEFINED mode)
57
+ if (border_mode != BORDER_MODE_UNDEFINED)
58
+ return false;
59
+
60
+ return true;
61
+ }
62
+
63
+ bool isGaussianPyramidDownU8Supported(const Size2D &srcSize, const Size2D &dstSize, u8 cn)
64
+ {
65
+ if (!isSupportedConfiguration())
66
+ return false;
67
+ if ( (dstSize.width * cn) < 8 ||
68
+ (cn != 1 && cn !=3 && cn!=4) ||
69
+ std::abs((ptrdiff_t)dstSize.width*2 - (ptrdiff_t)srcSize.width) > 2 ||
70
+ std::abs((ptrdiff_t)dstSize.height*2 - (ptrdiff_t)srcSize.height) > 2 )
71
+ return false;
72
+
73
+ return true;
74
+ }
75
+
76
+ bool isGaussianPyramidDownS16Supported(const Size2D &srcSize, const Size2D &dstSize, u8 cn)
77
+ {
78
+ if (!isSupportedConfiguration())
79
+ return false;
80
+ if ( (dstSize.width * cn) < 4 ||
81
+ (cn != 1 && cn !=3 && cn!=4) ||
82
+ std::abs((ptrdiff_t)dstSize.width*2 - (ptrdiff_t)srcSize.width) > 2 ||
83
+ std::abs((ptrdiff_t)dstSize.height*2 - (ptrdiff_t)srcSize.height) > 2 )
84
+ return false;
85
+
86
+ return true;
87
+ }
88
+
89
+ bool isGaussianPyramidDownF32Supported(const Size2D &srcSize, const Size2D &dstSize, u8 cn)
90
+ {
91
+ if (!isSupportedConfiguration())
92
+ return false;
93
+ if ( (dstSize.width * cn) < 4 ||
94
+ (cn != 1 && cn !=3 && cn!=4) ||
95
+ std::abs((ptrdiff_t)dstSize.width*2 - (ptrdiff_t)srcSize.width) > 2 ||
96
+ std::abs((ptrdiff_t)dstSize.height*2 - (ptrdiff_t)srcSize.height) > 2 )
97
+ return false;
98
+
99
+ return true;
100
+ }
101
+
102
+ bool isGaussianPyramidUpU8Supported(const Size2D &srcSize, const Size2D &dstSize, u8 cn)
103
+ {
104
+ if (!isSupportedConfiguration())
105
+ return false;
106
+ if ( (srcSize.width * cn) < 8 ||
107
+ (cn != 1 && cn !=3 && cn!=4) ||
108
+ std::abs((ptrdiff_t)dstSize.width - (ptrdiff_t)srcSize.width*2) != (ptrdiff_t)dstSize.width % 2 ||
109
+ std::abs((ptrdiff_t)dstSize.height - (ptrdiff_t)srcSize.height*2) != (ptrdiff_t)dstSize.height % 2 )
110
+ return false;
111
+
112
+ return true;
113
+ }
114
+
115
+ bool isGaussianPyramidUpS16Supported(const Size2D &srcSize, const Size2D &dstSize, u8 cn)
116
+ {
117
+ if (!isSupportedConfiguration())
118
+ return false;
119
+ if ( (srcSize.width * cn) < 12 ||
120
+ (cn != 1 && cn !=3 && cn!=4) ||
121
+ std::abs((ptrdiff_t)dstSize.width - (ptrdiff_t)srcSize.width*2) != (ptrdiff_t)dstSize.width % 2 ||
122
+ std::abs((ptrdiff_t)dstSize.height - (ptrdiff_t)srcSize.height*2) != (ptrdiff_t)dstSize.height % 2 )
123
+ return false;
124
+
125
+ return true;
126
+ }
127
+
128
+ #ifdef CAROTENE_NEON
129
+
130
+ namespace {
131
+
132
+ ptrdiff_t borderInterpolate101(ptrdiff_t p, ptrdiff_t len)
133
+ {
134
+ if (len == 1)
135
+ return 0;
136
+ else
137
+ {
138
+ while ((unsigned)p >= (unsigned)len)
139
+ {
140
+ if (p < 0)
141
+ p = -p;
142
+ else
143
+ p = (len - 1)*2 - p;
144
+ }
145
+ }
146
+ return p;
147
+ }
148
+
149
+ } // namespace
150
+
151
+ #endif
152
+
153
+ void gaussianPyramidDownRTZ(const Size2D &srcSize,
154
+ const u8 *srcBase, ptrdiff_t srcStride,
155
+ const Size2D &dstSize,
156
+ u8 *dstBase, ptrdiff_t dstStride,
157
+ BORDER_MODE border, u8 borderValue)
158
+ {
159
+ internal::assertSupportedConfiguration(isGaussianPyramidDownRTZSupported(srcSize, dstSize, border));
160
+ #ifdef CAROTENE_NEON
161
+ // Single-core NEON code
162
+ const size_t dwidth = dstSize.width;
163
+ const size_t dheight = dstSize.height;
164
+ const size_t swidth = srcSize.width;
165
+ const size_t sheight = srcSize.height;
166
+
167
+ ptrdiff_t idx_l1 = borderInterpolate101(-1, swidth);
168
+ ptrdiff_t idx_l2 = borderInterpolate101(-2, swidth);
169
+ ptrdiff_t idx_r1 = borderInterpolate101(swidth + 0, swidth);
170
+ ptrdiff_t idx_r2 = borderInterpolate101(swidth + 1, swidth);
171
+
172
+ //1-line buffer
173
+ std::vector<u16> _buf((swidth + 4) + 32/sizeof(u16));
174
+ u16* lane = internal::alignPtr(&_buf[2], 32);
175
+
176
+ uint8x8_t vc6u8 = vmov_n_u8(6);
177
+ uint16x8_t vc6u16 = vmovq_n_u16(6);
178
+ uint16x8_t vc4u16 = vmovq_n_u16(4);
179
+
180
+ u8* dst = dstBase;
181
+
182
+ for (size_t i = 0; i < dheight; ++i, dst += dstStride)
183
+ {
184
+ //vertical convolution
185
+ const u8* ln0 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-2, sheight));
186
+ const u8* ln1 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-1, sheight));
187
+ const u8* ln2 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+0, sheight));
188
+ const u8* ln3 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+1, sheight));
189
+ const u8* ln4 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+2, sheight));
190
+
191
+ size_t x = 0;
192
+ for (; x <= swidth - 8; x += 8)
193
+ {
194
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, x % 5 - 2));
195
+ uint8x8_t v0 = vld1_u8(ln0+x);
196
+ uint8x8_t v1 = vld1_u8(ln1+x);
197
+ uint8x8_t v2 = vld1_u8(ln2+x);
198
+ uint8x8_t v3 = vld1_u8(ln3+x);
199
+ uint8x8_t v4 = vld1_u8(ln4+x);
200
+
201
+ uint16x8_t v = vaddl_u8(v0, v4);
202
+ uint16x8_t v13 = vaddl_u8(v1, v3);
203
+
204
+ v = vmlal_u8(v, v2, vc6u8);
205
+ v = vmlaq_u16(v, v13, vc4u16);
206
+
207
+ vst1q_u16(lane + x, v);
208
+ }
209
+ for (; x < swidth; ++x)
210
+ {
211
+ lane[x] = ln0[x] + ln4[x] + 4u * (ln1[x] + ln3[x]) + 6u * ln2[x];
212
+ }
213
+
214
+ //left&right borders
215
+ lane[-1] = lane[idx_l1];
216
+ lane[-2] = lane[idx_l2];
217
+
218
+ lane[swidth] = lane[idx_r1];
219
+ lane[swidth+1] = lane[idx_r2];
220
+
221
+ //horizontal convolution
222
+ x = 0;
223
+ size_t vw = (swidth/2) - 7; // Using 7 instead of 8 allows swidth of 14 or 15.
224
+ for (; x < vw; x += 8)
225
+ {
226
+ internal::prefetch(lane + 2 * x);
227
+ uint16x8x2_t vLane0 = vld2q_u16(lane + 2*x-2); // L0[0] = x0 x2 x4 x6 x8 x10 x12 x14 L0[1] = x1 x3 x5 x7 x9 x11 x13 x15
228
+ uint16x8x2_t vLane1 = vld2q_u16(lane + 2*x-1); // L1[0] = x1 x3 x5 x7 x9 x11 x13 x15 L1[1] = x2 x4 x6 x8 x10 x12 x14 x16
229
+ uint16x8x2_t vLane2 = vld2q_u16(lane + 2*x+0); // L2[0] = x2 x4 x6 x8 x10 x12 x14 x16 L2[1] = x3 x5 x7 x9 x11 x13 x15 x17
230
+ uint16x8x2_t vLane3 = vld2q_u16(lane + 2*x+1); // L3[0] = x3 x5 x7 x9 x11 x13 x15 x17 L3[1] = x4 x6 x8 x10 x12 x14 x16 x18
231
+ uint16x8x2_t vLane4 = vld2q_u16(lane + 2*x+2); // L4[0] = x4 x6 x8 x10 x12 x14 x16 x18 L4[1] = x5 x7 x9 x11 x13 x15 x17 x19
232
+ uint16x8_t vSum_0_4 = vaddq_u16(vLane0.val[0], vLane4.val[0]);
233
+ uint16x8_t vSum_1_3 = vaddq_u16(vLane1.val[0], vLane3.val[0]);
234
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vLane2.val[0], vc6u16);
235
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vSum_1_3, vc4u16);
236
+ uint8x8_t vRes = vshrn_n_u16(vSum_0_4, 8);
237
+
238
+ vst1_u8(dst + x, vRes);
239
+ }
240
+
241
+ for (; x < dwidth; x++)
242
+ {
243
+ dst[x] = u8((lane[2*x-2] + lane[2*x+2] + 4u * (lane[2*x-1] + lane[2*x+1]) + 6u * lane[2*x]) >> 8);
244
+ }
245
+ }
246
+ #else
247
+ // Remove 'unused parameter' warnings.
248
+ (void)srcSize;
249
+ (void)srcBase;
250
+ (void)srcStride;
251
+ (void)dstSize;
252
+ (void)dstBase;
253
+ (void)dstStride;
254
+ (void)border;
255
+ #endif
256
+ (void)borderValue;
257
+ }
258
+
259
+ void gaussianPyramidDown(const Size2D &srcSize,
260
+ const u8 *srcBase, ptrdiff_t srcStride,
261
+ const Size2D &dstSize,
262
+ u8 *dstBase, ptrdiff_t dstStride, u8 cn)
263
+ {
264
+ internal::assertSupportedConfiguration(isGaussianPyramidDownU8Supported(srcSize, dstSize, cn));
265
+ #ifdef CAROTENE_NEON
266
+ size_t dcolcn = dstSize.width*cn;
267
+ size_t scolcn = srcSize.width*cn;
268
+ size_t roiw8 = dcolcn - 7;
269
+
270
+ size_t idx_l1 = borderInterpolate101(-1, srcSize.width) * cn;
271
+ size_t idx_l2 = borderInterpolate101(-2, srcSize.width) * cn;
272
+ size_t idx_r1 = borderInterpolate101(srcSize.width + 0, srcSize.width) * cn;
273
+ size_t idx_r2 = borderInterpolate101(srcSize.width + 1, srcSize.width) * cn;
274
+
275
+ //1-line buffer
276
+ std::vector<u16> _buf(cn*(srcSize.width + 4) + 32/sizeof(u16));
277
+ u16* lane = internal::alignPtr(&_buf[2*cn], 32);
278
+
279
+ uint8x8_t vc6u8 = vmov_n_u8(6);
280
+ uint16x8_t vc6u16 = vmovq_n_u16(6);
281
+ uint16x8_t vc4u16 = vmovq_n_u16(4);
282
+
283
+ for (size_t i = 0; i < dstSize.height; ++i)
284
+ {
285
+ u8* dst = internal::getRowPtr(dstBase, dstStride, i);
286
+ //vertical convolution
287
+ const u8* ln0 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-2, srcSize.height));
288
+ const u8* ln1 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-1, srcSize.height));
289
+ const u8* ln2 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+0, srcSize.height));
290
+ const u8* ln3 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+1, srcSize.height));
291
+ const u8* ln4 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+2, srcSize.height));
292
+
293
+ size_t x = 0;
294
+ for (; x <= scolcn - 8; x += 8)
295
+ {
296
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, (ptrdiff_t)x % 5 - 2));
297
+ uint8x8_t v0 = vld1_u8(ln0+x);
298
+ uint8x8_t v1 = vld1_u8(ln1+x);
299
+ uint8x8_t v2 = vld1_u8(ln2+x);
300
+ uint8x8_t v3 = vld1_u8(ln3+x);
301
+ uint8x8_t v4 = vld1_u8(ln4+x);
302
+
303
+ uint16x8_t v = vaddl_u8(v0, v4);
304
+ uint16x8_t v13 = vaddl_u8(v1, v3);
305
+
306
+ v = vmlal_u8(v, v2, vc6u8);
307
+ v = vmlaq_u16(v, v13, vc4u16);
308
+
309
+ vst1q_u16(lane + x, v);
310
+ }
311
+ for (; x < scolcn; ++x)
312
+ {
313
+ lane[x] = ln0[x] + ln4[x] + 4u * (ln1[x] + ln3[x]) + 6u * ln2[x];
314
+ }
315
+
316
+ //left&right borders
317
+ for (u32 k = 0; k < cn; ++k)
318
+ {
319
+ lane[(s32)(-cn+k)] = lane[idx_l1 + k];
320
+ lane[(s32)(-cn-cn+k)] = lane[idx_l2 + k];
321
+
322
+ lane[scolcn+k] = lane[idx_r1 + k];
323
+ lane[scolcn+cn+k] = lane[idx_r2 + k];
324
+ }
325
+
326
+ //horizontal convolution
327
+ x = 0;
328
+ switch(cn)
329
+ {
330
+ case 1:
331
+ for (; x < roiw8; x += 8)
332
+ {
333
+ internal::prefetch(lane + 2 * x);
334
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
335
+ __asm__ (
336
+ "vld2.16 {d0-d3}, [%[in0]] \n\t"
337
+ "vld2.16 {d4-d7}, [%[in4]] \n\t"
338
+ "vld2.16 {d12-d15}, [%[in1]] \n\t"
339
+ "vld2.16 {d16-d19}, [%[in3]] \n\t"
340
+ "vld2.16 {d8-d11}, [%[in2],:256] \n\t"
341
+ "vadd.i16 q0, q2 /*q0 = v0 + v4*/ \n\t"
342
+ "vadd.i16 q6, q8 /*q6 = v1 + v3*/ \n\t"
343
+ "vmla.i16 q0, q4, %q[c6] /*q0 += v2 * 6*/ \n\t"
344
+ "vmla.i16 q0, q6, %q[c4] /*q1 += (v1+v3) * 4*/ \n\t"
345
+ "vrshrn.u16 d8, q0, #8 \n\t"
346
+ "vst1.8 {d8}, [%[out]] \n\t"
347
+ : /*no output*/
348
+ : [out] "r" (dst + x),
349
+ [in0] "r" (lane + 2*x-2),
350
+ [in1] "r" (lane + 2*x-1),
351
+ [in2] "r" (lane + 2*x+0),
352
+ [in3] "r" (lane + 2*x+1),
353
+ [in4] "r" (lane + 2*x+2),
354
+ [c4] "w" (vc4u16), [c6] "w" (vc6u16)
355
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19"
356
+ );
357
+ #else
358
+ uint16x8x2_t vLane0 = vld2q_u16(lane + 2*x-2);
359
+ uint16x8x2_t vLane1 = vld2q_u16(lane + 2*x-1);
360
+ uint16x8x2_t vLane2 = vld2q_u16(lane + 2*x+0);
361
+ uint16x8x2_t vLane3 = vld2q_u16(lane + 2*x+1);
362
+ uint16x8x2_t vLane4 = vld2q_u16(lane + 2*x+2);
363
+
364
+ uint16x8_t vSum_0_4 = vaddq_u16(vLane0.val[0], vLane4.val[0]);
365
+ uint16x8_t vSum_1_3 = vaddq_u16(vLane1.val[0], vLane3.val[0]);
366
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vLane2.val[0], vc6u16);
367
+ vSum_0_4 = vmlaq_u16(vSum_0_4, vSum_1_3, vc4u16);
368
+ uint8x8_t vRes = vrshrn_n_u16(vSum_0_4, 8);
369
+
370
+ vst1_u8(dst + x, vRes);
371
+ #endif
372
+ }
373
+ break;
374
+ case 3:
375
+ {
376
+ uint16x4_t vx1 = vld1_u16(lane - 2*3);
377
+ uint16x4_t vx2 = vld1_u16(lane - 1*3);
378
+ uint16x4_t vx3 = vld1_u16(lane + 0*3);
379
+ uint16x8_t v0 = vcombine_u16(vx1, vx3);
380
+
381
+ uint8x8_t map = vreinterpret_u8_u64(vmov_n_u64(0xFFFF060504020100ULL));
382
+ for (; x < roiw8; x += 6)
383
+ {
384
+ internal::prefetch(lane + 2 * x + 12);
385
+
386
+ uint16x4_t vx_ = vld1_u16(lane + 2*x-1*3 + 6);
387
+ uint16x4_t vx4 = vld1_u16(lane + 2*x+0*3 + 6);
388
+ uint16x4_t vx5 = vld1_u16(lane + 2*x+1*3 + 6);
389
+ uint16x4_t vx6 = vld1_u16(lane + 2*x+2*3 + 6);
390
+
391
+ uint16x8_t v1 = vcombine_u16(vx2, vx_);
392
+ uint16x8_t v2 = vcombine_u16(vget_high_u16(v0), vx4);
393
+ uint16x8_t v3 = vcombine_u16(vx_, vx5);
394
+ uint16x8_t v4 = vcombine_u16(vx4, vx6);
395
+ vx2 = vx5;
396
+
397
+ uint16x8_t v = vaddq_u16(v0, v4);
398
+ uint16x8_t v13 = vaddq_u16(v1, v3);
399
+
400
+ v = vmlaq_u16(v, v2, vc6u16);
401
+ v = vmlaq_u16(v, v13, vc4u16);
402
+
403
+ uint8x8_t v8 = vrshrn_n_u16(v, 8);
404
+
405
+ v0 = v4;
406
+
407
+ vst1_u8(dst + x, vtbl1_u8(v8, map));
408
+ }
409
+ }
410
+ break;
411
+ case 4:
412
+ {
413
+ uint16x4_t vx1 = vld1_u16(lane - 2*4);
414
+ uint16x4_t vx2 = vld1_u16(lane - 1*4);
415
+ uint16x4_t vx3 = vld1_u16(lane + 0*4);
416
+ uint16x8_t v0 = vcombine_u16(vx1, vx3);
417
+
418
+ for (; x < roiw8; x += 8)
419
+ {
420
+ internal::prefetch(lane + 2 * x + 16);
421
+
422
+ uint16x4_t vx_ = vld1_u16(lane + 2 * x - 1*4 + 8);
423
+ uint16x4_t vx4 = vld1_u16(lane + 2 * x + 0*4 + 8);
424
+ uint16x4_t vx5 = vld1_u16(lane + 2 * x + 1*4 + 8);
425
+ uint16x4_t vx6 = vld1_u16(lane + 2 * x + 2*4 + 8);
426
+
427
+ uint16x8_t v1 = vcombine_u16(vx2, vx_);
428
+ uint16x8_t v2 = vcombine_u16(vget_high_u16(v0), vx4);
429
+ uint16x8_t v3 = vcombine_u16(vx_, vx5);
430
+ uint16x8_t v4 = vcombine_u16(vx4, vx6);
431
+ vx2 = vx5;
432
+
433
+ uint16x8_t v = vaddq_u16(v0, v4);
434
+ uint16x8_t v13 = vaddq_u16(v1, v3);
435
+
436
+ v = vmlaq_u16(v, v2, vc6u16);
437
+ v = vmlaq_u16(v, v13, vc4u16);
438
+
439
+ uint8x8_t v8 = vrshrn_n_u16(v, 8);
440
+
441
+ v0 = v4;
442
+
443
+ vst1_u8(dst + x, v8);
444
+ }
445
+ }
446
+ break;
447
+ }
448
+
449
+ for (u32 h = 0; h < cn; ++h)
450
+ {
451
+ u16* ln = lane + h;
452
+ u8* dt = dst + h;
453
+ for (size_t k = x; k < dcolcn; k += cn)
454
+ dt[k] = u8((ln[2*k-2*cn] + ln[2*k+2*cn] + 4u * (ln[2*k-cn] + ln[2*k+cn]) + 6u * ln[2*k] + (1 << 7)) >> 8);
455
+ }
456
+ }
457
+ #else
458
+ // Remove 'unused parameter' warnings.
459
+ (void)srcBase;
460
+ (void)srcStride;
461
+ (void)dstBase;
462
+ (void)dstStride;
463
+ #endif
464
+ }
465
+
466
+ void gaussianPyramidDown(const Size2D &srcSize,
467
+ const s16 *srcBase, ptrdiff_t srcStride,
468
+ const Size2D &dstSize,
469
+ s16 *dstBase, ptrdiff_t dstStride, u8 cn)
470
+ {
471
+ internal::assertSupportedConfiguration(isGaussianPyramidDownS16Supported(srcSize, dstSize, cn));
472
+ #ifdef CAROTENE_NEON
473
+ size_t dcolcn = dstSize.width*cn;
474
+ size_t scolcn = srcSize.width*cn;
475
+ size_t roiw4 = dcolcn - 3;
476
+
477
+ size_t idx_l1 = borderInterpolate101(-1, srcSize.width) * cn;
478
+ size_t idx_l2 = borderInterpolate101(-2, srcSize.width) * cn;
479
+ size_t idx_r1 = borderInterpolate101(srcSize.width + 0, srcSize.width) * cn;
480
+ size_t idx_r2 = borderInterpolate101(srcSize.width + 1, srcSize.width) * cn;
481
+
482
+ //1-line buffer
483
+ std::vector<s32> _buf(cn*(srcSize.width + 4) + 32/sizeof(s32));
484
+ s32* lane = internal::alignPtr(&_buf[2*cn], 32);
485
+
486
+ int16x4_t vc6s16 = vmov_n_s16(6);
487
+ int32x4_t vc6s32 = vmovq_n_s32(6);
488
+ int32x4_t vc4s32 = vmovq_n_s32(4);
489
+
490
+ for (size_t i = 0; i < dstSize.height; ++i)
491
+ {
492
+ s16* dst = internal::getRowPtr(dstBase, dstStride, i);
493
+ //vertical convolution
494
+ const s16* ln0 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-2, srcSize.height));
495
+ const s16* ln1 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-1, srcSize.height));
496
+ const s16* ln2 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+0, srcSize.height));
497
+ const s16* ln3 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+1, srcSize.height));
498
+ const s16* ln4 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+2, srcSize.height));
499
+
500
+ size_t x = 0;
501
+ for (; x <= scolcn - 4; x += 4)
502
+ {
503
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, (ptrdiff_t)x % 5 - 2));
504
+ int16x4_t v0 = vld1_s16(ln0 + x);
505
+ int16x4_t v1 = vld1_s16(ln1 + x);
506
+ int16x4_t v2 = vld1_s16(ln2 + x);
507
+ int16x4_t v3 = vld1_s16(ln3 + x);
508
+ int16x4_t v4 = vld1_s16(ln4 + x);
509
+
510
+ int32x4_t v = vaddl_s16(v0, v4);
511
+ int32x4_t v13 = vaddl_s16(v1, v3);
512
+
513
+ v = vmlal_s16(v, v2, vc6s16);
514
+ v = vmlaq_s32(v, v13, vc4s32);
515
+
516
+ vst1q_s32(lane + x, v);
517
+ }
518
+ for (; x < scolcn; ++x)
519
+ {
520
+ lane[x] = ln0[x] + ln4[x] + 4 * (ln1[x] + ln3[x]) + 6 * ln2[x];
521
+ }
522
+
523
+ //left&right borders
524
+ for (u32 k = 0; k < cn; ++k)
525
+ {
526
+ lane[(s32)(-cn+k)] = lane[idx_l1 + k];
527
+ lane[(s32)(-cn-cn+k)] = lane[idx_l2 + k];
528
+
529
+ lane[scolcn+k] = lane[idx_r1 + k];
530
+ lane[scolcn+cn+k] = lane[idx_r2 + k];
531
+ }
532
+
533
+ //horizontal convolution
534
+ x = 0;
535
+ switch(cn)
536
+ {
537
+ case 1:
538
+ for (; x < roiw4; x += 4)
539
+ {
540
+ internal::prefetch(lane + 2 * x);
541
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
542
+ __asm__ (
543
+ "vld2.32 {d0-d3}, [%[in0]] \n\t"
544
+ "vld2.32 {d4-d7}, [%[in4]] \n\t"
545
+ "vld2.32 {d12-d15}, [%[in1]] \n\t"
546
+ "vld2.32 {d16-d19}, [%[in3]] \n\t"
547
+ "vld2.32 {d8-d11}, [%[in2],:256] \n\t"
548
+ "vadd.i32 q0, q2 \n\t"
549
+ "vadd.i32 q6, q8 \n\t"
550
+ "vmla.i32 q0, q4, %q[c6] \n\t"
551
+ "vmla.i32 q0, q6, %q[c4] \n\t"
552
+ "vrshrn.s32 d8, q0, #8 \n\t"
553
+ "vst1.16 {d8}, [%[out]] \n\t"
554
+ : /*no output*/
555
+ : [out] "r" (dst + x),
556
+ [in0] "r" (lane + 2*x-2),
557
+ [in1] "r" (lane + 2*x-1),
558
+ [in2] "r" (lane + 2*x+0),
559
+ [in3] "r" (lane + 2*x+1),
560
+ [in4] "r" (lane + 2*x+2),
561
+ [c4] "w" (vc4s32), [c6] "w" (vc6s32)
562
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19"
563
+ );
564
+ #else
565
+ int32x4x2_t vLane0 = vld2q_s32(lane + 2*x-2);
566
+ int32x4x2_t vLane1 = vld2q_s32(lane + 2*x-1);
567
+ int32x4x2_t vLane2 = vld2q_s32(lane + 2*x+0);
568
+ int32x4x2_t vLane3 = vld2q_s32(lane + 2*x+1);
569
+ int32x4x2_t vLane4 = vld2q_s32(lane + 2*x+2);
570
+
571
+ int32x4_t vSum_0_4 = vaddq_s32(vLane0.val[0], vLane4.val[0]);
572
+ int32x4_t vSum_1_3 = vaddq_s32(vLane1.val[0], vLane3.val[0]);
573
+ vSum_0_4 = vmlaq_s32(vSum_0_4, vLane2.val[0], vc6s32);
574
+ vSum_0_4 = vmlaq_s32(vSum_0_4, vSum_1_3, vc4s32);
575
+ int16x4_t vRes = vrshrn_n_s32(vSum_0_4, 8);
576
+
577
+ vst1_s16(dst + x, vRes);
578
+ #endif
579
+ }
580
+ break;
581
+ case 3:
582
+ {
583
+ int32x4_t v0 = vld1q_s32(lane - 2*3);
584
+ int32x4_t v1 = vld1q_s32(lane - 1*3);
585
+ int32x4_t v2 = vld1q_s32(lane + 0*3);
586
+ for (; x < roiw4; x += 3)
587
+ {
588
+ internal::prefetch(lane + 2 * x);
589
+
590
+ int32x4_t v3 = vld1q_s32(lane + 2 * x + 1*3);
591
+ int32x4_t v4 = vld1q_s32(lane + 2 * x + 2*3);
592
+
593
+ int32x4_t v = vaddq_s32(v0, v4);
594
+ int32x4_t v13 = vaddq_s32(v1, v3);
595
+
596
+ v = vmlaq_s32(v, v2, vc6s32);
597
+ v = vmlaq_s32(v, v13, vc4s32);
598
+
599
+ int16x4_t vv = vrshrn_n_s32(v, 8);
600
+
601
+ v0 = v2;
602
+ v1 = v3;
603
+ v2 = v4;
604
+
605
+ vst1_s16(dst + x, vv);
606
+ }
607
+ }
608
+ break;
609
+ case 4:
610
+ {
611
+ int32x4_t v0 = vld1q_s32(lane - 2*4);
612
+ int32x4_t v1 = vld1q_s32(lane - 1*4);
613
+ int32x4_t v2 = vld1q_s32(lane + 0*4);
614
+ for (; x < roiw4; x += 4)
615
+ {
616
+ internal::prefetch(lane + 2 * x + 8);
617
+ int32x4_t v3 = vld1q_s32(lane + 2 * x + 1*4);
618
+ int32x4_t v4 = vld1q_s32(lane + 2 * x + 2*4);
619
+
620
+ int32x4_t v = vaddq_s32(v0, v4);
621
+ int32x4_t v13 = vaddq_s32(v1, v3);
622
+
623
+ v = vmlaq_s32(v, v2, vc6s32);
624
+ v = vmlaq_s32(v, v13, vc4s32);
625
+
626
+ int16x4_t vv = vrshrn_n_s32(v, 8);
627
+
628
+ v0 = v2;
629
+ v1 = v3;
630
+ v2 = v4;
631
+
632
+ vst1_s16(dst + x, vv);
633
+ }
634
+ }
635
+ break;
636
+ }
637
+
638
+ for (u32 h = 0; h < cn; ++h)
639
+ {
640
+ s32* ln = lane + h;
641
+ s16* dt = dst + h;
642
+ for (size_t k = x; k < dcolcn; k += cn)
643
+ dt[k] = s16((ln[2*k-2*cn] + ln[2*k+2*cn] + 4 * (ln[2*k-cn] + ln[2*k+cn]) + 6 * ln[2*k] + (1 << 7)) >> 8);
644
+ }
645
+ }
646
+ #else
647
+ // Remove 'unused parameter' warnings.
648
+ (void)srcBase;
649
+ (void)srcStride;
650
+ (void)dstBase;
651
+ (void)dstStride;
652
+ #endif
653
+ }
654
+
655
+ void gaussianPyramidDown(const Size2D &srcSize,
656
+ const f32 *srcBase, ptrdiff_t srcStride,
657
+ const Size2D &dstSize,
658
+ f32 *dstBase, ptrdiff_t dstStride, u8 cn)
659
+ {
660
+ internal::assertSupportedConfiguration(isGaussianPyramidDownF32Supported(srcSize, dstSize, cn));
661
+ #ifdef CAROTENE_NEON
662
+ size_t dcolcn = dstSize.width*cn;
663
+ size_t scolcn = srcSize.width*cn;
664
+ size_t roiw4 = dcolcn - 3;
665
+
666
+ size_t idx_l1 = borderInterpolate101(-1, srcSize.width) * cn;
667
+ size_t idx_l2 = borderInterpolate101(-2, srcSize.width) * cn;
668
+ size_t idx_r1 = borderInterpolate101(srcSize.width + 0, srcSize.width) * cn;
669
+ size_t idx_r2 = borderInterpolate101(srcSize.width + 1, srcSize.width) * cn;
670
+
671
+ //1-line buffer
672
+ std::vector<f32> _buf(cn*(srcSize.width + 4) + 32/sizeof(f32));
673
+ f32* lane = internal::alignPtr(&_buf[2*cn], 32);
674
+
675
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
676
+ register float32x4_t vc6d4f32 asm ("q11") = vmovq_n_f32(1.5f); // 6/4
677
+ register float32x4_t vc1d4f32 asm ("q12") = vmovq_n_f32(0.25f); // 1/4
678
+
679
+ register float32x4_t vc1d64f32 asm ("q13") = vmovq_n_f32(0.015625f); //1/4/16
680
+ register float32x4_t vc4d64f32 asm ("q14") = vmovq_n_f32(0.0625f); //4/4/16
681
+ register float32x4_t vc6d64f32 asm ("q15") = vmovq_n_f32(0.09375f); //6/4/16
682
+ #else
683
+ float32x4_t vc6d4f32 = vmovq_n_f32(1.5f); // 6/4
684
+ float32x4_t vc1d4f32 = vmovq_n_f32(0.25f); // 1/4
685
+
686
+ float32x4_t vc1d64f32 = vmovq_n_f32(0.015625f); //1/4/16
687
+ float32x4_t vc4d64f32 = vmovq_n_f32(0.0625f); //4/4/16
688
+ float32x4_t vc6d64f32 = vmovq_n_f32(0.09375f); //6/4/16
689
+ #endif
690
+
691
+ for (size_t i = 0; i < dstSize.height; ++i)
692
+ {
693
+ f32* dst = internal::getRowPtr(dstBase, dstStride, i);
694
+ //vertical convolution
695
+ const f32* ln0 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-2, srcSize.height));
696
+ const f32* ln1 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2-1, srcSize.height));
697
+ const f32* ln2 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+0, srcSize.height));
698
+ const f32* ln3 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+1, srcSize.height));
699
+ const f32* ln4 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i*2+2, srcSize.height));
700
+
701
+ size_t x = 0;
702
+ for (; x <= scolcn - 4; x += 4)
703
+ {
704
+ internal::prefetch(internal::getRowPtr(ln2 + x, srcStride, (ptrdiff_t)x % 5 - 2));
705
+ float32x4_t v0 = vld1q_f32((const float32_t*)ln0 + x);
706
+ float32x4_t v1 = vld1q_f32((const float32_t*)ln1 + x);
707
+ float32x4_t v2 = vld1q_f32((const float32_t*)ln2 + x);
708
+ float32x4_t v3 = vld1q_f32((const float32_t*)ln3 + x);
709
+ float32x4_t v4 = vld1q_f32((const float32_t*)ln4 + x);
710
+
711
+ float32x4_t v = vaddq_f32(v1, v3);
712
+ float32x4_t v04 = vaddq_f32(v0, v4);
713
+
714
+ v = vmlaq_f32(v, v2, vc6d4f32);
715
+ v = vmlaq_f32(v, v04, vc1d4f32);
716
+
717
+ vst1q_f32(lane + x, v);
718
+ }
719
+ for (; x < scolcn; ++x)
720
+ {
721
+ lane[x] = 0.25f*(ln0[x] + ln4[x]) + (ln1[x] + ln3[x]) + 1.5f * ln2[x];
722
+ }
723
+
724
+ //left&right borders
725
+ for (u32 k = 0; k < cn; ++k)
726
+ {
727
+ lane[(s32)(-cn+k)] = lane[idx_l1 + k];
728
+ lane[(s32)(-cn-cn+k)] = lane[idx_l2 + k];
729
+
730
+ lane[scolcn+k] = lane[idx_r1 + k];
731
+ lane[scolcn+cn+k] = lane[idx_r2 + k];
732
+ }
733
+
734
+ //horizontal convolution
735
+ x = 0;
736
+ switch(cn)
737
+ {
738
+ case 1:
739
+ for (; x < roiw4; x += 4)
740
+ {
741
+ internal::prefetch(lane + 2 * x);
742
+ #if !defined(__aarch64__) && defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7 && !defined(__clang__)
743
+ __asm__ __volatile__ (
744
+ "vld2.32 {d0-d3}, [%[in0]] \n\t"
745
+ "vld2.32 {d8-d11}, [%[in4]] \n\t"
746
+ "vld2.32 {d14-d17}, [%[in2],:256] \n\t"
747
+ "vld2.32 {d10-d13}, [%[in1]] \n\t"
748
+ "vld2.32 {d16-d19}, [%[in3]] \n\t"
749
+ "vmul.f32 q7, %q[c6d64] \n\t"
750
+ "vadd.f32 q0, q4 @v04 \n\t"
751
+ "vadd.f32 q5, q8 @v13 \n\t"
752
+ "vmla.f32 q7, q0, %q[c1d64] \n\t"
753
+ "vmla.f32 q7, q5, %q[c4d64] \n\t"
754
+ "vst1.32 {d14-d15}, [%[out]] \n\t"
755
+ :
756
+ : [out] "r" (dst + x),
757
+ [in0] "r" (lane + 2*x-2),
758
+ [in1] "r" (lane + 2*x-1),
759
+ [in2] "r" (lane + 2*x+0),
760
+ [in3] "r" (lane + 2*x+1),
761
+ [in4] "r" (lane + 2*x+2),
762
+ [c4d64] "w" (vc4d64f32), [c6d64] "w" (vc6d64f32), [c1d64] "w" (vc1d64f32)
763
+ : "d0","d1","d2","d3","d4",/*"d5","d6","d7",*/"d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19" //ugly compiler "bug" - can't touch d5-d7
764
+ );
765
+ #else
766
+ float32x4x2_t vLane0 = vld2q_f32(lane + 2*x-2);
767
+ float32x4x2_t vLane1 = vld2q_f32(lane + 2*x-1);
768
+ float32x4x2_t vLane2 = vld2q_f32(lane + 2*x+0);
769
+ float32x4x2_t vLane3 = vld2q_f32(lane + 2*x+1);
770
+ float32x4x2_t vLane4 = vld2q_f32(lane + 2*x+2);
771
+
772
+ float32x4_t vSum_0_4 = vaddq_f32(vLane0.val[0], vLane4.val[0]);
773
+ float32x4_t vSum_1_3 = vaddq_f32(vLane1.val[0], vLane3.val[0]);
774
+ float32x4_t vRes = vmulq_f32(vLane2.val[0], vc6d64f32);
775
+ vRes = vmlaq_f32(vRes, vSum_0_4, vc1d64f32);
776
+ vRes = vmlaq_f32(vRes, vSum_1_3, vc4d64f32);
777
+
778
+ vst1q_f32(dst + x, vRes);
779
+ #endif
780
+ }
781
+ break;
782
+ case 3:
783
+ {
784
+ float32x4_t v0 = vld1q_f32((const float32_t*)lane - 2*3);
785
+ float32x4_t v1 = vld1q_f32((const float32_t*)lane - 1*3);
786
+ float32x4_t v2 = vld1q_f32((const float32_t*)lane + 0*3);
787
+
788
+ for (; x < roiw4; x += 3)
789
+ {
790
+ internal::prefetch(lane + 2 * x);
791
+
792
+ float32x4_t v3 = vld1q_f32((const float32_t*)lane + 2 * x + 1*3);
793
+ float32x4_t v4 = vld1q_f32((const float32_t*)lane + 2 * x + 2*3);
794
+
795
+ float32x4_t v04 = vaddq_f32(v0, v4);
796
+ float32x4_t v13 = vaddq_f32(v1, v3);
797
+
798
+ float32x4_t v = vmulq_f32(v2, vc6d64f32);
799
+ v = vmlaq_f32(v, v04, vc1d64f32);
800
+ v = vmlaq_f32(v, v13, vc4d64f32);
801
+
802
+ v0 = v2;
803
+ v1 = v3;
804
+ v2 = v4;
805
+
806
+ vst1q_f32(dst + x, v);
807
+ }
808
+ }
809
+ break;
810
+ case 4:
811
+ {
812
+ float32x4_t v0 = vld1q_f32((const float32_t*)lane - 2*4);
813
+ float32x4_t v1 = vld1q_f32((const float32_t*)lane - 1*4);
814
+ float32x4_t v2 = vld1q_f32((const float32_t*)lane + 0*4);
815
+
816
+ for (; x < roiw4; x += 4)
817
+ {
818
+ internal::prefetch(lane + 2 * x + 8);
819
+
820
+ float32x4_t v3 = vld1q_f32((const float32_t*)lane + 2 * x + 1*4);
821
+ float32x4_t v4 = vld1q_f32((const float32_t*)lane + 2 * x + 2*4);
822
+
823
+ float32x4_t v04 = vaddq_f32(v0, v4);
824
+ float32x4_t v13 = vaddq_f32(v1, v3);
825
+
826
+ float32x4_t v = vmulq_f32(v2, vc6d64f32);
827
+ v = vmlaq_f32(v, v04, vc1d64f32);
828
+ v = vmlaq_f32(v, v13, vc4d64f32);
829
+
830
+ v0 = v2;
831
+ v1 = v3;
832
+ v2 = v4;
833
+
834
+ vst1q_f32(dst + x, v);
835
+ }
836
+ }
837
+ break;
838
+ }
839
+
840
+ for (u32 h = 0; h < cn; ++h)
841
+ {
842
+ f32* ln = lane + h;
843
+ f32* dt = dst + h;
844
+ for (size_t k = x; k < dcolcn; k += cn)
845
+ dt[k] = 0.015625f * (ln[2*k-2*cn] + ln[2*k+2*cn]) + 0.0625f * (ln[2*k-cn] + ln[2*k+cn]) + 0.09375f * ln[2*k];
846
+ }
847
+ }
848
+ #else
849
+ // Remove 'unused parameter' warnings.
850
+ (void)srcBase;
851
+ (void)srcStride;
852
+ (void)dstBase;
853
+ (void)dstStride;
854
+ #endif
855
+ }
856
+
857
+ void gaussianPyramidUp(const Size2D &srcSize,
858
+ const u8 *srcBase, ptrdiff_t srcStride,
859
+ const Size2D &dstSize,
860
+ u8 *dstBase, ptrdiff_t dstStride, u8 cn)
861
+ {
862
+ internal::assertSupportedConfiguration(isGaussianPyramidUpU8Supported(srcSize, dstSize, cn));
863
+ #ifdef CAROTENE_NEON
864
+ size_t dcolshn = (dstSize.width/2) * cn;
865
+ size_t dcolshw = ((dstSize.width+1)/2) * cn;
866
+ size_t scolsn = srcSize.width*cn;
867
+
868
+ size_t idx_l = (borderInterpolate101(-2, 2 * srcSize.width)/2) * cn;
869
+ size_t idx_r1 = (borderInterpolate101(2 * srcSize.width + 0, 2 * srcSize.width)/2) * cn;
870
+ size_t idx_r2 = (borderInterpolate101(2 * srcSize.width + 2, 2 * srcSize.width + 2)/2) * cn;
871
+
872
+ //2-lines buffer
873
+ std::vector<u16> _buf(2*(cn*(srcSize.width + 3) + 32/sizeof(u16)));
874
+ u16* lane0 = internal::alignPtr(&_buf[cn], 32);
875
+ u16* lane1 = internal::alignPtr(lane0 + (3 + srcSize.width)*cn, 32);
876
+
877
+ uint8x8_t vc6u8 = vmov_n_u8(6);
878
+ uint16x8_t vc6u16 = vmovq_n_u16(6);
879
+
880
+ for (size_t i = 0; i < (dstSize.height + 1)/2; ++i)
881
+ {
882
+ u8* dst = internal::getRowPtr(dstBase, dstStride, 2*i);
883
+ //vertical convolution
884
+ const u8* ln0 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i * 2 - 2, srcSize.height * 2)/2);
885
+ const u8* ln1 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i * 2 + 0, srcSize.height * 2)/2);
886
+ const u8* ln2 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i * 2 + 2, srcSize.height * 2)/2);
887
+
888
+ size_t x = 0;
889
+ for (; x <= scolsn - 8; x += 8)
890
+ {
891
+ internal::prefetch(internal::getRowPtr(ln1 + x, srcStride, (ptrdiff_t)x % 3 - 1));
892
+ uint8x8_t v0 = vld1_u8(ln0+x);
893
+ uint8x8_t v2 = vld1_u8(ln2+x);
894
+ uint8x8_t v1 = vld1_u8(ln1+x);
895
+
896
+ uint16x8_t vl0 = vaddl_u8(v0, v2);
897
+ uint16x8_t vl1 = vaddl_u8(v1, v2);
898
+
899
+ vl0 = vmlal_u8(vl0, v1, vc6u8);
900
+ vl1 = vshlq_n_u16(vl1, 2);
901
+
902
+ vst1q_u16(lane0 + x, vl0);
903
+ vst1q_u16(lane1 + x, vl1);
904
+ }
905
+ for (; x < scolsn; ++x)
906
+ {
907
+ lane0[x] = ln0[x] + ln2[x] + 6u * ln1[x];
908
+ lane1[x] = 4u * (ln1[x] + ln2[x]);
909
+ }
910
+
911
+ //left&right borders
912
+ for (u32 k = 0; k < cn; ++k)
913
+ {
914
+ lane0[(s32)(-cn+k)] = lane0[idx_l + k];
915
+ lane1[(s32)(-cn+k)] = lane1[idx_l + k];
916
+
917
+ lane0[scolsn+k] = lane0[idx_r1 + k];
918
+ lane0[scolsn+cn+k] = lane0[idx_r2 + k];
919
+ lane1[scolsn+k] = lane1[idx_r1 + k];
920
+ lane1[scolsn+cn+k] = lane1[idx_r2 + k];
921
+ }
922
+
923
+ //horizontal convolution
924
+ const u16* lane = lane0;
925
+ pyrUp8uHorizontalConvolution:
926
+ x = 0;
927
+ size_t lim;
928
+ switch(cn)
929
+ {
930
+ case 1:
931
+ lim = dcolshn > 7 ? dcolshn - 7 : 0;
932
+ for (; x < lim; x += 8)
933
+ {
934
+ internal::prefetch(lane + x);
935
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
936
+ __asm__ (
937
+ "vld1.16 {d0-d1}, [%[in0]] /*q0 = v0*/ \n\t"
938
+ "vld1.16 {d2-d3}, [%[in2]] /*q1 = v2*/ \n\t"
939
+ "vld1.16 {d4-d5}, [%[in1],:128] /*q2 = v1*/ \n\t"
940
+ "vadd.i16 q0, q1 /*q0 = v0 + v2*/ \n\t"
941
+ "vadd.i16 q3, q1, q2 /*q3 = v1 + v2*/ \n\t"
942
+ "vmla.i16 q0, q2, %q[c6] /*q0 += v1*6*/ \n\t"
943
+ "vrshrn.u16 d9, q3, #4 \n\t"
944
+ "vrshrn.u16 d8, q0, #6 \n\t"
945
+ "vst2.8 {d8-d9}, [%[out]] \n\t"
946
+ : /*no output*/
947
+ : [out] "r" (dst + x*2),
948
+ [in0] "r" (lane + x - 1),
949
+ [in1] "r" (lane + x + 0),
950
+ [in2] "r" (lane + x + 1),
951
+ [c6] "w" (vc6u16)
952
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9"
953
+ );
954
+ #else
955
+ uint16x8_t vLane0 = vld1q_u16(lane + x - 1);
956
+ uint16x8_t vLane1 = vld1q_u16(lane + x + 0);
957
+ uint16x8_t vLane2 = vld1q_u16(lane + x + 1);
958
+
959
+ vLane0 = vaddq_u16(vLane0, vLane2);
960
+ vLane2 = vaddq_u16(vLane2, vLane1);
961
+ vLane0 = vmlaq_u16(vLane0, vLane1, vc6u16);
962
+ uint8x8x2_t vRes;
963
+ vRes.val[0] = vrshrn_n_u16(vLane0, 6);
964
+ vRes.val[1] = vrshrn_n_u16(vLane2, 4);
965
+
966
+ vst2_u8(dst + x*2, vRes);
967
+ #endif
968
+ }
969
+ break;
970
+ case 3:
971
+ {
972
+ lim = dcolshn > 23 ? dcolshn - 23 : 0;
973
+ for (; x < lim; x += 24)
974
+ {
975
+ internal::prefetch(lane + x);
976
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
977
+ __asm__ (
978
+ "vmov.u16 q9, #6 \n\t"
979
+ "vld3.16 {d0, d2, d4}, [%[in0]] /*v0*/ \n\t"
980
+ "vld3.16 {d1, d3, d5}, [%[in02]] \n\t"
981
+ "vld3.16 {d6, d8, d10}, [%[in2]] /*v2*/ \n\t"
982
+ "vld3.16 {d7, d9, d11}, [%[in22]] \n\t"
983
+ "vld3.16 {d12, d14, d16}, [%[in1]] /*v1*/ \n\t"
984
+ "vld3.16 {d13, d15, d17}, [%[in12]] \n\t"
985
+ "vadd.i16 q0, q3 /*v0 + v2*/ \n\t"
986
+ "vadd.i16 q1, q4 /*v0 + v2*/ \n\t"
987
+ "vadd.i16 q2, q5 /*v0 + v2*/ \n\t"
988
+ "vadd.i16 q3, q6 /*v1 + v2*/ \n\t"
989
+ "vadd.i16 q4, q7 /*v1 + v2*/ \n\t"
990
+ "vadd.i16 q5, q8 /*v1 + v2*/ \n\t"
991
+ "vmla.i16 q0, q6, q9 /*v0 + v2 + v1*6 */ \n\t"
992
+ "vmla.i16 q1, q7, q9 /*v0 + v2 + v1*6 */ \n\t"
993
+ "vmla.i16 q2, q8, q9 /*v0 + v2 + v1*6 */ \n\t"
994
+ "vrshrn.u16 d19, q3, #4 \n\t"
995
+ "vrshrn.u16 d21, q4, #4 \n\t"
996
+ "vrshrn.u16 d23, q5, #4 \n\t"
997
+ "vrshrn.u16 d18, q0, #6 \n\t"
998
+ "vrshrn.u16 d20, q1, #6 \n\t"
999
+ "vrshrn.u16 d22, q2, #6 \n\t"
1000
+ "vzip.8 d18, d19 \n\t"
1001
+ "vzip.8 d20, d21 \n\t"
1002
+ "vzip.8 d22, d23 \n\t"
1003
+ "vst3.8 {d18, d20, d22}, [%[out1]] \n\t"
1004
+ "vst3.8 {d19, d21, d23}, [%[out2]] \n\t"
1005
+ : /*no output*/
1006
+ : [out1] "r" (dst + 2 * x),
1007
+ [out2] "r" (dst + 2 * x + 24),
1008
+ [in0] "r" (lane + x - 3),
1009
+ [in02] "r" (lane + x + 9),
1010
+ [in1] "r" (lane + x),
1011
+ [in12] "r" (lane + x + 12),
1012
+ [in2] "r" (lane + x + 3),
1013
+ [in22] "r" (lane + x + 15)
1014
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23"
1015
+ );
1016
+ #else
1017
+ uint16x8_t vc6 = vmovq_n_u16(6);
1018
+ uint16x8x3_t vLane0 = vld3q_u16(lane + x - 3);
1019
+ uint16x8x3_t vLane1 = vld3q_u16(lane + x + 0);
1020
+ uint16x8x3_t vLane2 = vld3q_u16(lane + x + 3);
1021
+
1022
+ uint16x8_t vSum_0_3 = vaddq_u16(vLane0.val[0], vLane2.val[0]);
1023
+ uint16x8_t vSum_1_4 = vaddq_u16(vLane0.val[1], vLane2.val[1]);
1024
+ uint16x8_t vSum_2_5 = vaddq_u16(vLane0.val[2], vLane2.val[2]);
1025
+ uint16x8_t vSum_3_6 = vaddq_u16(vLane2.val[0], vLane1.val[0]);
1026
+ uint16x8_t vSum_4_7 = vaddq_u16(vLane2.val[1], vLane1.val[1]);
1027
+ uint16x8_t vSum_5_8 = vaddq_u16(vLane2.val[2], vLane1.val[2]);
1028
+
1029
+ vSum_0_3 = vmlaq_u16(vSum_0_3, vLane1.val[0], vc6);
1030
+ vSum_1_4 = vmlaq_u16(vSum_1_4, vLane1.val[1], vc6);
1031
+ vSum_2_5 = vmlaq_u16(vSum_2_5, vLane1.val[2], vc6);
1032
+
1033
+ uint8x8x2_t vSumShr3;
1034
+ vSumShr3.val[0] = vrshrn_n_u16(vSum_3_6, 4);
1035
+ vSumShr3.val[1] = vrshrn_n_u16(vSum_0_3, 6);;
1036
+ uint8x8x2_t vSumShr4;
1037
+ vSumShr4.val[0] = vrshrn_n_u16(vSum_4_7, 4);
1038
+ vSumShr4.val[1] = vrshrn_n_u16(vSum_1_4, 6);
1039
+ uint8x8x2_t vSumShr5;
1040
+ vSumShr5.val[0] = vrshrn_n_u16(vSum_5_8, 4);
1041
+ vSumShr5.val[1] = vrshrn_n_u16(vSum_2_5, 6);
1042
+
1043
+ vSumShr3 = vzip_u8(vSumShr3.val[1], vSumShr3.val[0]);
1044
+ vSumShr4 = vzip_u8(vSumShr4.val[1], vSumShr4.val[0]);
1045
+ vSumShr5 = vzip_u8(vSumShr5.val[1], vSumShr5.val[0]);
1046
+
1047
+ uint8x8x3_t vRes1;
1048
+ vRes1.val[0] = vSumShr3.val[0];
1049
+ vRes1.val[1] = vSumShr4.val[0];
1050
+ vRes1.val[2] = vSumShr5.val[0];
1051
+ vst3_u8(dst + 2 * x, vRes1);
1052
+
1053
+ uint8x8x3_t vRes2;
1054
+ vRes2.val[0] = vSumShr3.val[1];
1055
+ vRes2.val[1] = vSumShr4.val[1];
1056
+ vRes2.val[2] = vSumShr5.val[1];
1057
+ vst3_u8(dst + 2 * x + 24, vRes2);
1058
+ #endif
1059
+ }
1060
+ }
1061
+ break;
1062
+ case 4:
1063
+ lim = dcolshn > 7 ? dcolshn - 7 : 0;
1064
+ for (; x < lim; x += 8)
1065
+ {
1066
+ internal::prefetch(lane + x);
1067
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
1068
+ __asm__ (
1069
+ "vld1.16 {d0-d1}, [%[in0]] /*q0 = v0*/ \n\t"
1070
+ "vld1.16 {d2-d3}, [%[in2]] /*q1 = v2*/ \n\t"
1071
+ "vld1.16 {d4-d5}, [%[in1],:128] /*q2 = v1*/ \n\t"
1072
+ "vadd.i16 q0, q1 /*q0 = v0 + v2*/ \n\t"
1073
+ "vadd.i16 q3, q1, q2 /*q3 = v1 + v2*/ \n\t"
1074
+ "vmla.i16 q0, q2, %q[c6] /*q0 += v1*6*/ \n\t"
1075
+ "vrshrn.u16 d9, q3, #4 \n\t"
1076
+ "vrshrn.u16 d8, q0, #6 \n\t"
1077
+ "vst2.32 {d8-d9}, [%[out]] \n\t"
1078
+ : /*no output*/
1079
+ : [out] "r" (dst + x*2),
1080
+ [in0] "r" (lane + x-4),
1081
+ [in1] "r" (lane + x),
1082
+ [in2] "r" (lane + x+4),
1083
+ [c6] "w" (vc6u16)
1084
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9"
1085
+ );
1086
+ #else
1087
+ uint16x8_t vLane0 = vld1q_u16(lane + x-4);
1088
+ uint16x8_t vLane1 = vld1q_u16(lane + x+0);
1089
+ uint16x8_t vLane2 = vld1q_u16(lane + x+4);
1090
+
1091
+ vLane0 = vaddq_u16(vLane0, vLane2);
1092
+ vLane2 = vaddq_u16(vLane2, vLane1);
1093
+ vLane0 = vmlaq_u16(vLane0, vLane1, vc6u16);
1094
+ uint32x2x2_t vRes;
1095
+ vRes.val[1] = vreinterpret_u32_u8(vrshrn_n_u16(vLane2, 4));
1096
+ vRes.val[0] = vreinterpret_u32_u8(vrshrn_n_u16(vLane0, 6));
1097
+
1098
+ vst2_u32((uint32_t*)(dst + x*2), vRes);
1099
+ #endif
1100
+ }
1101
+ break;
1102
+ };
1103
+
1104
+ for (u32 h = 0; h < cn; ++h)
1105
+ {
1106
+ const u16* ln = lane + h;
1107
+ u8* dt = dst + h;
1108
+ size_t k = x;
1109
+ for (; k < dcolshn; k += cn)
1110
+ {
1111
+ dt[2*k+0] = u8((ln[(ptrdiff_t)(k-cn)] + ln[k+cn] + 6u * ln[k] + (1 << 5)) >> 6);
1112
+ dt[2*k+cn] = u8(((ln[k] + ln[k+cn]) * 4u + (1 << 5)) >> 6);
1113
+ }
1114
+ for (; k < dcolshw; k += cn)
1115
+ dt[2*k] = u8((ln[(ptrdiff_t)(k-cn)] + ln[k+cn] + 6u * ln[k] + (1 << 5)) >> 6);
1116
+ }
1117
+ dst = internal::getRowPtr(dstBase, dstStride, 2*i+1);
1118
+
1119
+ //second row
1120
+ if (lane == lane0 && 2*i+1 < dstSize.height)
1121
+ {
1122
+ lane = lane1;
1123
+ goto pyrUp8uHorizontalConvolution;
1124
+ }
1125
+ }
1126
+ #else
1127
+ // Remove 'unused parameter' warnings.
1128
+ (void)srcBase;
1129
+ (void)srcStride;
1130
+ (void)dstBase;
1131
+ (void)dstStride;
1132
+ #endif
1133
+ }
1134
+
1135
+ void gaussianPyramidUp(const Size2D &srcSize,
1136
+ const s16 *srcBase, ptrdiff_t srcStride,
1137
+ const Size2D &dstSize,
1138
+ s16 *dstBase, ptrdiff_t dstStride, u8 cn)
1139
+ {
1140
+ internal::assertSupportedConfiguration(isGaussianPyramidUpS16Supported(srcSize, dstSize, cn));
1141
+ #ifdef CAROTENE_NEON
1142
+ size_t dcolshn = (dstSize.width/2) * cn;
1143
+ size_t dcolshw = ((dstSize.width+1)/2) * cn;
1144
+ size_t scolsn = srcSize.width*cn;
1145
+
1146
+ size_t idx_l = (borderInterpolate101(-2, 2 * srcSize.width)/2) * cn;
1147
+ size_t idx_r1 = (borderInterpolate101(2 * srcSize.width + 0, 2 * srcSize.width)/2) * cn;
1148
+ size_t idx_r2 = (borderInterpolate101(2 * srcSize.width + 2, 2 * srcSize.width + 2)/2) * cn;
1149
+
1150
+ //2-lines buffer
1151
+ std::vector<s32> _buf(2*(cn*(srcSize.width + 3) + 32/sizeof(s32)));
1152
+ s32* lane0 = internal::alignPtr(&_buf[cn], 32);
1153
+ s32* lane1 = internal::alignPtr(lane0 + (3 + srcSize.width)*cn, 32);
1154
+
1155
+ int16x4_t vc6s16 = vmov_n_s16(6);
1156
+ int32x4_t vc6s32 = vmovq_n_s32(6);
1157
+
1158
+ for (size_t i = 0; i < (dstSize.height + 1)/2; ++i)
1159
+ {
1160
+ s16* dst = internal::getRowPtr(dstBase, dstStride, 2*i);
1161
+ //vertical convolution
1162
+ const s16* ln0 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i * 2 - 2, srcSize.height * 2)/2);
1163
+ const s16* ln1 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i * 2 + 0, srcSize.height * 2)/2);
1164
+ const s16* ln2 = internal::getRowPtr(srcBase, srcStride, borderInterpolate101(i * 2 + 2, srcSize.height * 2)/2);
1165
+
1166
+ size_t x = 0;
1167
+ for (; x <= scolsn - 4; x += 4)
1168
+ {
1169
+ internal::prefetch(internal::getRowPtr(ln1 + x, srcStride, (ptrdiff_t)x % 3 - 1));
1170
+ int16x4_t v0 = vld1_s16(ln0 + x);
1171
+ int16x4_t v2 = vld1_s16(ln2 + x);
1172
+ int16x4_t v1 = vld1_s16(ln1 + x);
1173
+
1174
+ int32x4_t vl0 = vaddl_s16(v0, v2);
1175
+ int32x4_t vl1 = vaddl_s16(v1, v2);
1176
+
1177
+ vl0 = vmlal_s16(vl0, v1, vc6s16);
1178
+ vl1 = vshlq_n_s32(vl1, 2);
1179
+
1180
+ vst1q_s32(lane0 + x, vl0);
1181
+ vst1q_s32(lane1 + x, vl1);
1182
+ }
1183
+ for (; x < scolsn; ++x)
1184
+ {
1185
+ lane0[x] = ln0[x] + ln2[x] + 6 * ln1[x];
1186
+ lane1[x] = 4 * (ln1[x] + ln2[x]);
1187
+ }
1188
+
1189
+ //left&right borders
1190
+ for (u32 k = 0; k < cn; ++k)
1191
+ {
1192
+ lane0[(s32)(-cn+k)] = lane0[idx_l + k];
1193
+ lane1[(s32)(-cn+k)] = lane1[idx_l + k];
1194
+
1195
+ lane0[scolsn+k] = lane0[idx_r1 + k];
1196
+ lane0[scolsn+cn+k] = lane0[idx_r2 + k];
1197
+ lane1[scolsn+k] = lane1[idx_r1 + k];
1198
+ lane1[scolsn+cn+k] = lane1[idx_r2 + k];
1199
+ }
1200
+
1201
+ //horizontal convolution
1202
+ const s32* lane = lane0;
1203
+ pyrUp16sHorizontalConvolution:
1204
+ x = 0;
1205
+ size_t lim;
1206
+ switch(cn)
1207
+ {
1208
+ case 1:
1209
+ lim = dcolshn > 3 ? dcolshn - 3 : 0;
1210
+ for (; x < lim; x += 4)
1211
+ {
1212
+ internal::prefetch(lane + x);
1213
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
1214
+ __asm__ (
1215
+ "vld1.32 {d0-d1}, [%[in0]] /*q0 = v0*/ \n\t"
1216
+ "vld1.32 {d2-d3}, [%[in2]] /*q1 = v2*/ \n\t"
1217
+ "vld1.32 {d4-d5}, [%[in1],:128] /*q2 = v1*/ \n\t"
1218
+ "vadd.i32 q0, q0, q1 /*q0 = v0 + v2*/ \n\t"
1219
+ "vadd.i32 q3, q1, q2 /*q3 = v1 + v2*/ \n\t"
1220
+ "vmla.i32 q0, q2, %q[c6] /*q0 += v1*6*/ \n\t"
1221
+ "vrshrn.s32 d9, q3, #4 \n\t"
1222
+ "vrshrn.s32 d8, q0, #6 \n\t"
1223
+ "vst2.16 {d8-d9}, [%[out]] \n\t"
1224
+ : /*no output*/
1225
+ : [out] "r" (dst + x * 2),
1226
+ [in0] "r" (lane + x - 1),
1227
+ [in1] "r" (lane + x),
1228
+ [in2] "r" (lane + x + 1),
1229
+ [c6] "w" (vc6s32)
1230
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9"
1231
+ );
1232
+ #else
1233
+ int32x4_t vLane0 = vld1q_s32(lane + x - 1);
1234
+ int32x4_t vLane1 = vld1q_s32(lane + x);
1235
+ int32x4_t vLane2 = vld1q_s32(lane + x + 1);
1236
+
1237
+ vLane0 = vaddq_s32(vLane0, vLane2);
1238
+ vLane2 = vaddq_s32(vLane2, vLane1);
1239
+ vLane0 = vmlaq_s32(vLane0, vLane1, vc6s32);
1240
+ int16x4x2_t vRes;
1241
+ vRes.val[0] = vrshrn_n_s32(vLane0, 6);
1242
+ vRes.val[1] = vrshrn_n_s32(vLane2, 4);
1243
+
1244
+ vst2_s16(dst + x * 2, vRes);
1245
+ #endif
1246
+ }
1247
+ break;
1248
+ case 3:
1249
+ {
1250
+ lim = dcolshn > 11 ? dcolshn - 11 : 0;
1251
+ for (; x < lim; x += 12)
1252
+ {
1253
+ internal::prefetch(lane + x + 3);
1254
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
1255
+ __asm__ (
1256
+ "vmov.s32 q9, #6 \n\t"
1257
+ "vld3.32 {d0, d2, d4}, [%[in0]] /*v0*/ \n\t"
1258
+ "vld3.32 {d1, d3, d5}, [%[in2]] \n\t"
1259
+ "vld3.32 {d6, d8, d10}, [%[in2]] /*v2*/ \n\t"
1260
+ "vld3.32 {d7, d9, d11}, [%[in22]] \n\t"
1261
+ "vld3.32 {d12, d14, d16}, [%[in1]] /*v1*/ \n\t"
1262
+ "vld3.32 {d13, d15, d17}, [%[in12]] \n\t"
1263
+ "vadd.i32 q0, q3 /*v0 + v2*/ \n\t"
1264
+ "vadd.i32 q1, q4 /*v0 + v2*/ \n\t"
1265
+ "vadd.i32 q2, q5 /*v0 + v2*/ \n\t"
1266
+ "vadd.i32 q3, q6 /*v1 + v2*/ \n\t"
1267
+ "vadd.i32 q4, q7 /*v1 + v2*/ \n\t"
1268
+ "vadd.i32 q5, q8 /*v1 + v2*/ \n\t"
1269
+ "vmla.i32 q0, q6, q9 /*v0 + v2 + v1*6 */ \n\t"
1270
+ "vmla.i32 q1, q7, q9 /*v0 + v2 + v1*6 */ \n\t"
1271
+ "vmla.i32 q2, q8, q9 /*v0 + v2 + v1*6 */ \n\t"
1272
+ "vrshrn.s32 d19, q3, #4 \n\t"
1273
+ "vrshrn.s32 d21, q4, #4 \n\t"
1274
+ "vrshrn.s32 d23, q5, #4 \n\t"
1275
+ "vrshrn.s32 d18, q0, #6 \n\t"
1276
+ "vrshrn.s32 d20, q1, #6 \n\t"
1277
+ "vrshrn.s32 d22, q2, #6 \n\t"
1278
+ "vzip.16 d18, d19 \n\t"
1279
+ "vzip.16 d20, d21 \n\t"
1280
+ "vzip.16 d22, d23 \n\t"
1281
+ "vst3.16 {d18, d20, d22}, [%[out1]] \n\t"
1282
+ "vst3.16 {d19, d21, d23}, [%[out2]] \n\t"
1283
+ : /*no output*/
1284
+ : [out1] "r" (dst + 2*x),
1285
+ [out2] "r" (dst + 2*x + 12),
1286
+ [in0] "r" (lane + x - 3),
1287
+ [in1] "r" (lane + x),
1288
+ [in12] "r" (lane + x + 6),
1289
+ [in2] "r" (lane + x + 3),
1290
+ [in22] "r" (lane + x + 9)
1291
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12","d13","d14","d15","d16","d17","d18","d19","d20","d21","d22","d23"
1292
+ );
1293
+ #else
1294
+ int32x4_t vc6 = vmovq_n_s32(6);
1295
+ int32x4x3_t vLane0 = vld3q_s32(lane + x - 3);
1296
+ int32x4x3_t vLane1 = vld3q_s32(lane + x);
1297
+ int32x4x3_t vLane2 = vld3q_s32(lane + x + 3);
1298
+
1299
+ int32x4_t vSum_0_3 = vaddq_s32(vLane0.val[0], vLane2.val[0]);
1300
+ int32x4_t vSum_1_4 = vaddq_s32(vLane0.val[1], vLane2.val[1]);
1301
+ int32x4_t vSum_2_5 = vaddq_s32(vLane0.val[2], vLane2.val[2]);
1302
+ int32x4_t vSum_3_6 = vaddq_s32(vLane2.val[0], vLane1.val[0]);
1303
+ int32x4_t vSum_4_7 = vaddq_s32(vLane2.val[1], vLane1.val[1]);
1304
+ int32x4_t vSum_5_8 = vaddq_s32(vLane2.val[2], vLane1.val[2]);
1305
+
1306
+ vSum_0_3 = vmlaq_s32(vSum_0_3, vLane1.val[0], vc6);
1307
+ vSum_1_4 = vmlaq_s32(vSum_1_4, vLane1.val[1], vc6);
1308
+ vSum_2_5 = vmlaq_s32(vSum_2_5, vLane1.val[2], vc6);
1309
+
1310
+ int16x4x2_t vSumShr1;
1311
+ vSumShr1.val[1] = vrshrn_n_s32(vSum_3_6, 4);
1312
+ vSumShr1.val[0] = vrshrn_n_s32(vSum_0_3, 6);
1313
+
1314
+ int16x4x2_t vSumShr2;
1315
+ vSumShr2.val[1] = vrshrn_n_s32(vSum_4_7, 4);
1316
+ vSumShr2.val[0] = vrshrn_n_s32(vSum_1_4, 6);
1317
+
1318
+ int16x4x2_t vSumShr3;
1319
+ vSumShr3.val[1] = vrshrn_n_s32(vSum_5_8, 4);
1320
+ vSumShr3.val[0] = vrshrn_n_s32(vSum_2_5, 6);
1321
+
1322
+ vSumShr1 = vzip_s16(vSumShr1.val[0], vSumShr1.val[1]);
1323
+ vSumShr2 = vzip_s16(vSumShr2.val[0], vSumShr2.val[1]);
1324
+ vSumShr3 = vzip_s16(vSumShr3.val[0], vSumShr3.val[1]);
1325
+
1326
+ int16x4x3_t vRes1;
1327
+ vRes1.val[0] = vSumShr1.val[0];
1328
+ vRes1.val[1] = vSumShr2.val[0];
1329
+ vRes1.val[2] = vSumShr3.val[0];
1330
+ vst3_s16((int16_t*)(dst + 2 * x), vRes1);
1331
+
1332
+ int16x4x3_t vRes2;
1333
+ vRes2.val[0] = vSumShr1.val[1];
1334
+ vRes2.val[1] = vSumShr2.val[1];
1335
+ vRes2.val[2] = vSumShr3.val[1];
1336
+ vst3_s16(dst + 2 * x + 12, vRes2);
1337
+ #endif
1338
+ }
1339
+ }
1340
+ break;
1341
+ case 4:
1342
+ lim = dcolshn > 3 ? dcolshn - 3 : 0;
1343
+ for (; x < lim; x += 4)
1344
+ {
1345
+ internal::prefetch(lane + x);
1346
+ #if !defined(__aarch64__) && defined(__GNUC__) && defined(__arm__)
1347
+ __asm__ (
1348
+ "vld1.32 {d0-d1}, [%[in0]] /*q0 = v0*/ \n\t"
1349
+ "vld1.32 {d2-d3}, [%[in2]] /*q1 = v2*/ \n\t"
1350
+ "vld1.32 {d4-d5}, [%[in1],:128] /*q2 = v1*/ \n\t"
1351
+ "vadd.i32 q0, q1 /*q0 = v0 + v2*/ \n\t"
1352
+ "vadd.i32 q3, q1, q2 /*q3 = v1 + v2*/ \n\t"
1353
+ "vmla.i32 q0, q2, %q[c6] /*q0 += v1*6*/ \n\t"
1354
+ "vrshrn.s32 d9, q3, #4 \n\t"
1355
+ "vrshrn.s32 d8, q0, #6 \n\t"
1356
+ "vst1.16 {d8-d9}, [%[out]] \n\t"
1357
+ : /*no output*/
1358
+ : [out] "r" (dst + x * 2),
1359
+ [in0] "r" (lane + x - 4),
1360
+ [in1] "r" (lane + x),
1361
+ [in2] "r" (lane + x + 4),
1362
+ [c6] "w" (vc6s32)
1363
+ : "d0","d1","d2","d3","d4","d5","d6","d7","d8","d9"
1364
+ );
1365
+ #else
1366
+ int32x4_t vLane0 = vld1q_s32(lane + x - 4);
1367
+ int32x4_t vLane1 = vld1q_s32(lane + x);
1368
+ int32x4_t vLane2 = vld1q_s32(lane + x + 4);
1369
+
1370
+ vLane0 = vaddq_s32(vLane0, vLane2);
1371
+ vLane2 = vaddq_s32(vLane2, vLane1);
1372
+ vLane0 = vmlaq_s32(vLane0, vLane1, vc6s32);
1373
+ int16x4x2_t vRes;
1374
+ vRes.val[0] = vrshrn_n_s32(vLane0, 6);
1375
+ vRes.val[1] = vrshrn_n_s32(vLane2, 4);
1376
+
1377
+ vst1q_s16(dst + x * 2, vcombine_s16(vRes.val[0], vRes.val[1]));
1378
+ #endif
1379
+ }
1380
+ break;
1381
+ };
1382
+
1383
+ for (u32 h = 0; h < cn; ++h)
1384
+ {
1385
+ const s32* ln = lane + h;
1386
+ s16* dt = dst + h;
1387
+ size_t k = x;
1388
+ for (; k < dcolshn; k += cn)
1389
+ {
1390
+ dt[2*k+0] = s16((ln[(ptrdiff_t)(k-cn)] + ln[k+cn] + 6 * ln[k] + (1 << 5)) >> 6);
1391
+ dt[2*k+cn] = s16(((ln[k] + ln[k+cn]) * 4 + (1 << 5)) >> 6);
1392
+ }
1393
+ for (; k < dcolshw; k += cn)
1394
+ dt[2*k] = s16((ln[(ptrdiff_t)(k-cn)] + ln[k+cn] + 6 * ln[k] + (1 << 5)) >> 6);
1395
+ }
1396
+ dst = internal::getRowPtr(dstBase, dstStride, 2*i+1);
1397
+
1398
+ //second row
1399
+ if (lane == lane0 && 2*i+1 < dstSize.height)
1400
+ {
1401
+ lane = lane1;
1402
+ goto pyrUp16sHorizontalConvolution;
1403
+ }
1404
+ }
1405
+ #else
1406
+ // Remove 'unused parameter' warnings.
1407
+ (void)srcBase;
1408
+ (void)srcStride;
1409
+ (void)dstBase;
1410
+ (void)dstStride;
1411
+ #endif
1412
+ }
1413
+
1414
+ } // namespace CAROTENE_NS
3rdparty/carotene/src/reduce.cpp ADDED
@@ -0,0 +1,460 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * By downloading, copying, installing or using the software you agree to this license.
3
+ * If you do not agree to this license, do not download, install,
4
+ * copy or use the software.
5
+ *
6
+ *
7
+ * License Agreement
8
+ * For Open Source Computer Vision Library
9
+ * (3-clause BSD License)
10
+ *
11
+ * Copyright (C) 2012-2015, NVIDIA Corporation, all rights reserved.
12
+ * Third party copyrights are property of their respective owners.
13
+ *
14
+ * Redistribution and use in source and binary forms, with or without modification,
15
+ * are permitted provided that the following conditions are met:
16
+ *
17
+ * * Redistributions of source code must retain the above copyright notice,
18
+ * this list of conditions and the following disclaimer.
19
+ *
20
+ * * Redistributions in binary form must reproduce the above copyright notice,
21
+ * this list of conditions and the following disclaimer in the documentation
22
+ * and/or other materials provided with the distribution.
23
+ *
24
+ * * Neither the names of the copyright holders nor the names of the contributors
25
+ * may be used to endorse or promote products derived from this software
26
+ * without specific prior written permission.
27
+ *
28
+ * This software is provided by the copyright holders and contributors "as is" and
29
+ * any express or implied warranties, including, but not limited to, the implied
30
+ * warranties of merchantability and fitness for a particular purpose are disclaimed.
31
+ * In no event shall copyright holders or contributors be liable for any direct,
32
+ * indirect, incidental, special, exemplary, or consequential damages
33
+ * (including, but not limited to, procurement of substitute goods or services;
34
+ * loss of use, data, or profits; or business interruption) however caused
35
+ * and on any theory of liability, whether in contract, strict liability,
36
+ * or tort (including negligence or otherwise) arising in any way out of
37
+ * the use of this software, even if advised of the possibility of such damage.
38
+ */
39
+
40
+ #include "common.hpp"
41
+
42
+ #include <cstring>
43
+
44
+ namespace CAROTENE_NS {
45
+
46
+ void reduceColSum(const Size2D &size,
47
+ const u8 * srcBase, ptrdiff_t srcStride,
48
+ s32 * dstBase)
49
+ {
50
+ internal::assertSupportedConfiguration();
51
+ #ifdef CAROTENE_NEON
52
+ memset(dstBase, 0, size.width*sizeof(s32));
53
+ size_t i = 0;
54
+ for (; i + 16 <= size.width; i += 16)
55
+ {
56
+ const u8* src_address = srcBase + i;
57
+
58
+ int32x4_t sll = vmovq_n_s32(0);
59
+ int32x4_t slh = vmovq_n_s32(0);
60
+ int32x4_t shl = vmovq_n_s32(0);
61
+ int32x4_t shh = vmovq_n_s32(0);
62
+
63
+ for (size_t h = 0; h < size.height; h += 256)
64
+ {
65
+ size_t lim = std::min(h + 256, size.height);
66
+
67
+ uint16x8_t sl = vmovq_n_u16(0);
68
+ uint16x8_t sh = vmovq_n_u16(0);
69
+
70
+ for (size_t k = h; k < lim; ++k, src_address += srcStride)
71
+ {
72
+ internal::prefetch(src_address + srcStride, 0);
73
+
74
+ uint8x16_t v = vld1q_u8(src_address);
75
+
76
+ sl = vaddw_u8(sl, vget_low_u8(v));
77
+ sh = vaddw_u8(sh, vget_high_u8(v));
78
+ }
79
+
80
+ int32x4_t vsll = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(sl)));
81
+ int32x4_t vslh = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(sl)));
82
+ int32x4_t vshl = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(sh)));
83
+ int32x4_t vshh = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(sh)));
84
+
85
+ sll = vqaddq_s32(sll, vsll);
86
+ slh = vqaddq_s32(slh, vslh);
87
+ shl = vqaddq_s32(shl, vshl);
88
+ shh = vqaddq_s32(shh, vshh);
89
+ }
90
+
91
+ vst1q_s32(dstBase + i + 0, sll);
92
+ vst1q_s32(dstBase + i + 4, slh);
93
+ vst1q_s32(dstBase + i + 8, shl);
94
+ vst1q_s32(dstBase + i + 12, shh);
95
+ }
96
+
97
+ for(size_t h = 0; h < size.height; ++h)
98
+ {
99
+ for(size_t j = i ; j < size.width; j++ )
100
+ {
101
+ if (((u32)(dstBase[j] += srcBase[j + srcStride * h])) > 0x7fFFffFFu)
102
+ dstBase[j] = 0x7fFFffFF;
103
+ }
104
+ }
105
+ #else
106
+ (void)size;
107
+ (void)srcBase;
108
+ (void)srcStride;
109
+ (void)dstBase;
110
+ #endif
111
+ }
112
+
113
+ void reduceColMax(const Size2D &size,
114
+ const u8 * srcBase, ptrdiff_t srcStride,
115
+ u8 * dstBase)
116
+ {
117
+ internal::assertSupportedConfiguration();
118
+ #ifdef CAROTENE_NEON
119
+ memcpy(dstBase, srcBase, size.width);
120
+ size_t i = 0;
121
+ for (; i + 16*4 <= size.width; i += 16*4)
122
+ {
123
+ const u8* src_address = srcBase + i;
124
+
125
+ uint8x16_t s1 = vld1q_u8(src_address + 0);
126
+ uint8x16_t s2 = vld1q_u8(src_address + 16);
127
+ uint8x16_t s3 = vld1q_u8(src_address + 32);
128
+ uint8x16_t s4 = vld1q_u8(src_address + 48);
129
+
130
+ src_address += srcStride;
131
+
132
+ for(size_t h = 1; h < size.height; ++h, src_address += srcStride)
133
+ {
134
+ internal::prefetch(src_address + srcStride, 0);
135
+ internal::prefetch(src_address + srcStride, 32);
136
+
137
+ uint8x16_t v1 = vld1q_u8(src_address + 0);
138
+ uint8x16_t v2 = vld1q_u8(src_address + 16);
139
+ uint8x16_t v3 = vld1q_u8(src_address + 32);
140
+ uint8x16_t v4 = vld1q_u8(src_address + 48);
141
+
142
+ s1 = vmaxq_u8(s1, v1);
143
+ s2 = vmaxq_u8(s2, v2);
144
+ s3 = vmaxq_u8(s3, v3);
145
+ s4 = vmaxq_u8(s4, v4);
146
+ }
147
+
148
+ vst1q_u8(dstBase + i + 0, s1);
149
+ vst1q_u8(dstBase + i + 16, s2);
150
+ vst1q_u8(dstBase + i + 32, s3);
151
+ vst1q_u8(dstBase + i + 48, s4);
152
+ }
153
+
154
+ for (; i + 16 <= size.width; i += 16)
155
+ {
156
+ const u8* src_address = srcBase + i;
157
+ uint8x16_t s1 = vld1q_u8(src_address);
158
+ src_address += srcStride;
159
+ for(size_t h = 1; h < size.height; ++h, src_address += srcStride)
160
+ {
161
+ internal::prefetch(src_address + srcStride, 0);
162
+
163
+ uint8x16_t v1 = vld1q_u8(src_address);
164
+ s1 = vmaxq_u8(s1, v1);
165
+ }
166
+ vst1q_u8(dstBase + i, s1);
167
+ }
168
+
169
+ if (i < size.width)
170
+ for(size_t h = 1; h < size.height; ++h)
171
+ for(size_t j = i ; j < size.width; j++ )
172
+ dstBase[j] = std::max(dstBase[j], srcBase[j + srcStride * h]);
173
+ #else
174
+ (void)size;
175
+ (void)srcBase;
176
+ (void)srcStride;
177
+ (void)dstBase;
178
+ #endif
179
+ }
180
+
181
+ void reduceColMin(const Size2D &size,
182
+ const u8 * srcBase, ptrdiff_t srcStride,
183
+ u8 * dstBase)
184
+ {
185
+ internal::assertSupportedConfiguration();
186
+ #ifdef CAROTENE_NEON
187
+ memcpy(dstBase, srcBase, size.width);
188
+ size_t i = 0;
189
+ for (; i + 16*4 <= size.width; i += 16*4)
190
+ {
191
+ const u8* src_address = srcBase + i;
192
+
193
+ uint8x16_t s1 = vld1q_u8(src_address + 0);
194
+ uint8x16_t s2 = vld1q_u8(src_address + 16);
195
+ uint8x16_t s3 = vld1q_u8(src_address + 32);
196
+ uint8x16_t s4 = vld1q_u8(src_address + 48);
197
+
198
+ src_address += srcStride;
199
+
200
+ for(size_t h = 1; h < size.height; ++h, src_address += srcStride)
201
+ {
202
+ internal::prefetch(src_address + srcStride, 0);
203
+ internal::prefetch(src_address + srcStride, 32);
204
+
205
+ uint8x16_t v1 = vld1q_u8(src_address + 0);
206
+ uint8x16_t v2 = vld1q_u8(src_address + 16);
207
+ uint8x16_t v3 = vld1q_u8(src_address + 32);
208
+ uint8x16_t v4 = vld1q_u8(src_address + 48);
209
+
210
+ s1 = vminq_u8(s1, v1);
211
+ s2 = vminq_u8(s2, v2);
212
+ s3 = vminq_u8(s3, v3);
213
+ s4 = vminq_u8(s4, v4);
214
+ }
215
+
216
+ vst1q_u8(dstBase + i + 0, s1);
217
+ vst1q_u8(dstBase + i + 16, s2);
218
+ vst1q_u8(dstBase + i + 32, s3);
219
+ vst1q_u8(dstBase + i + 48, s4);
220
+ }
221
+
222
+ for (; i + 16 <= size.width; i += 16)
223
+ {
224
+ const u8* src_address = srcBase + i;
225
+ uint8x16_t s1 = vld1q_u8(src_address);
226
+ src_address += srcStride;
227
+ for(size_t h = 1; h < size.height; ++h, src_address += srcStride)
228
+ {
229
+ internal::prefetch(src_address + srcStride, 0);
230
+
231
+ uint8x16_t v1 = vld1q_u8(src_address);
232
+ s1 = vminq_u8(s1, v1);
233
+ }
234
+ vst1q_u8(dstBase + i, s1);
235
+ }
236
+
237
+ if (i < size.width)
238
+ for(size_t h = 1; h < size.height; ++h)
239
+ for(size_t j = i ; j < size.width; j++ )
240
+ dstBase[j] = std::min(dstBase[j], srcBase[j + srcStride * h]);
241
+ #else
242
+ (void)size;
243
+ (void)srcBase;
244
+ (void)srcStride;
245
+ (void)dstBase;
246
+ #endif
247
+ }
248
+
249
+ void reduceColSum(const Size2D &size,
250
+ const f32 * srcBase, ptrdiff_t srcStride,
251
+ f32 * dstBase)
252
+ {
253
+ internal::assertSupportedConfiguration();
254
+ #ifdef CAROTENE_NEON
255
+ memcpy(dstBase, srcBase, size.width*sizeof(f32));
256
+ size_t srcstep = srcStride/sizeof(f32);
257
+ size_t i = 0;
258
+ for (; i + 16 <= size.width; i += 16)
259
+ {
260
+ const f32* src_address = srcBase + i;
261
+
262
+ float32x4_t s1 = vld1q_f32(src_address + 0);
263
+ float32x4_t s2 = vld1q_f32(src_address + 4);
264
+ float32x4_t s3 = vld1q_f32(src_address + 8);
265
+ float32x4_t s4 = vld1q_f32(src_address + 12);
266
+
267
+ src_address += srcstep;
268
+
269
+ for(size_t h = 1; h < size.height; ++h, src_address += srcstep)
270
+ {
271
+ internal::prefetch(src_address + srcstep, 0);
272
+ internal::prefetch(src_address + srcstep, 32);
273
+
274
+ float32x4_t v1 = vld1q_f32(src_address + 0);
275
+ float32x4_t v2 = vld1q_f32(src_address + 4);
276
+ float32x4_t v3 = vld1q_f32(src_address + 8);
277
+ float32x4_t v4 = vld1q_f32(src_address + 12);
278
+
279
+ s1 = vaddq_f32(s1, v1);
280
+ s2 = vaddq_f32(s2, v2);
281
+ s3 = vaddq_f32(s3, v3);
282
+ s4 = vaddq_f32(s4, v4);
283
+ }
284
+
285
+ vst1q_f32(dstBase + i + 0, s1);
286
+ vst1q_f32(dstBase + i + 4, s2);
287
+ vst1q_f32(dstBase + i + 8, s3);
288
+ vst1q_f32(dstBase + i + 12, s4);
289
+ }
290
+
291
+ for (; i + 4 <= size.width; i += 4)
292
+ {
293
+ const f32* src_address = srcBase + i;
294
+ float32x4_t s1 = vld1q_f32(src_address);
295
+ src_address += srcstep;
296
+ for(size_t h = 1; h < size.height; ++h, src_address += srcstep)
297
+ {
298
+ internal::prefetch(src_address + srcstep, 0);
299
+
300
+ float32x4_t v1 = vld1q_f32(src_address);
301
+ s1 = vaddq_f32(s1, v1);
302
+ }
303
+ vst1q_f32(dstBase + i, s1);
304
+ }
305
+
306
+ if (i < size.width)
307
+ for(size_t h = 1; h < size.height; ++h)
308
+ {
309
+ for(size_t j = i ; j < size.width; j++ )
310
+ {
311
+ dstBase[j] += srcBase[j + srcstep * h];
312
+ }
313
+ }
314
+ #else
315
+ (void)size;
316
+ (void)srcBase;
317
+ (void)srcStride;
318
+ (void)dstBase;
319
+ #endif
320
+ }
321
+
322
+ void reduceColMax(const Size2D &size,
323
+ const f32 * srcBase, ptrdiff_t srcStride,
324
+ f32 * dstBase)
325
+ {
326
+ internal::assertSupportedConfiguration();
327
+ #ifdef CAROTENE_NEON
328
+ memcpy(dstBase, srcBase, size.width*sizeof(f32));
329
+ size_t srcstep = srcStride/sizeof(f32);
330
+ size_t i = 0;
331
+ for (; i + 16 <= size.width; i += 16)
332
+ {
333
+ const f32* src_address = srcBase + i;
334
+
335
+ float32x4_t s1 = vld1q_f32(src_address + 0);
336
+ float32x4_t s2 = vld1q_f32(src_address + 4);
337
+ float32x4_t s3 = vld1q_f32(src_address + 8);
338
+ float32x4_t s4 = vld1q_f32(src_address + 12);
339
+
340
+ src_address += srcstep;
341
+
342
+ for(size_t h = 1; h < size.height; ++h, src_address += srcstep)
343
+ {
344
+ internal::prefetch(src_address + srcstep, 0);
345
+ internal::prefetch(src_address + srcstep, 32);
346
+
347
+ float32x4_t v1 = vld1q_f32(src_address + 0);
348
+ float32x4_t v2 = vld1q_f32(src_address + 4);
349
+ float32x4_t v3 = vld1q_f32(src_address + 8);
350
+ float32x4_t v4 = vld1q_f32(src_address + 12);
351
+
352
+ s1 = vmaxq_f32(s1, v1);
353
+ s2 = vmaxq_f32(s2, v2);
354
+ s3 = vmaxq_f32(s3, v3);
355
+ s4 = vmaxq_f32(s4, v4);
356
+ }
357
+
358
+ vst1q_f32(dstBase + i + 0, s1);
359
+ vst1q_f32(dstBase + i + 4, s2);
360
+ vst1q_f32(dstBase + i + 8, s3);
361
+ vst1q_f32(dstBase + i + 12, s4);
362
+ }
363
+
364
+ for (; i + 4 <= size.width; i += 4)
365
+ {
366
+ const f32* src_address = srcBase + i;
367
+ float32x4_t s1 = vld1q_f32(src_address);
368
+ src_address += srcstep;
369
+ for(size_t h = 1; h < size.height; ++h, src_address += srcstep)
370
+ {
371
+ internal::prefetch(src_address + srcstep, 0);
372
+
373
+ float32x4_t v1 = vld1q_f32(src_address);
374
+ s1 = vmaxq_f32(s1, v1);
375
+ }
376
+ vst1q_f32(dstBase + i, s1);
377
+ }
378
+
379
+ if (i < size.width)
380
+ for(size_t h = 1; h < size.height; ++h)
381
+ for(size_t j = i ; j < size.width; j++ )
382
+ dstBase[j] = std::max(dstBase[j], srcBase[j + srcstep * h]);
383
+ #else
384
+ (void)size;
385
+ (void)srcBase;
386
+ (void)srcStride;
387
+ (void)dstBase;
388
+ #endif
389
+ }
390
+
391
+ void reduceColMin(const Size2D &size,
392
+ const f32 * srcBase, ptrdiff_t srcStride,
393
+ f32 * dstBase)
394
+ {
395
+ internal::assertSupportedConfiguration();
396
+ #ifdef CAROTENE_NEON
397
+ memcpy(dstBase, srcBase, size.width*sizeof(f32));
398
+ size_t srcstep = srcStride/sizeof(f32);
399
+ size_t i = 0;
400
+ for (; i + 16 <= size.width; i += 16)
401
+ {
402
+ const f32* src_address = srcBase + i;
403
+
404
+ float32x4_t s1 = vld1q_f32(src_address + 0);
405
+ float32x4_t s2 = vld1q_f32(src_address + 4);
406
+ float32x4_t s3 = vld1q_f32(src_address + 8);
407
+ float32x4_t s4 = vld1q_f32(src_address + 12);
408
+
409
+ src_address += srcstep;
410
+
411
+ for(size_t h = 1; h < size.height; ++h, src_address += srcstep)
412
+ {
413
+ internal::prefetch(src_address + srcstep, 0);
414
+ internal::prefetch(src_address + srcstep, 32);
415
+
416
+ float32x4_t v1 = vld1q_f32(src_address + 0);
417
+ float32x4_t v2 = vld1q_f32(src_address + 4);
418
+ float32x4_t v3 = vld1q_f32(src_address + 8);
419
+ float32x4_t v4 = vld1q_f32(src_address + 12);
420
+
421
+ s1 = vminq_f32(s1, v1);
422
+ s2 = vminq_f32(s2, v2);
423
+ s3 = vminq_f32(s3, v3);
424
+ s4 = vminq_f32(s4, v4);
425
+ }
426
+
427
+ vst1q_f32(dstBase + i + 0, s1);
428
+ vst1q_f32(dstBase + i + 4, s2);
429
+ vst1q_f32(dstBase + i + 8, s3);
430
+ vst1q_f32(dstBase + i + 12, s4);
431
+ }
432
+
433
+ for (; i + 4 <= size.width; i += 4)
434
+ {
435
+ const f32* src_address = srcBase + i;
436
+ float32x4_t s1 = vld1q_f32(src_address);
437
+ src_address += srcstep;
438
+ for(size_t h = 1; h < size.height; ++h, src_address += srcstep)
439
+ {
440
+ internal::prefetch(src_address + srcstep, 0);
441
+
442
+ float32x4_t v1 = vld1q_f32(src_address);
443
+ s1 = vminq_f32(s1, v1);
444
+ }
445
+ vst1q_f32(dstBase + i, s1);
446
+ }
447
+
448
+ if (i < size.width)
449
+ for(size_t h = 1; h < size.height; ++h)
450
+ for(size_t j = i ; j < size.width; j++ )
451
+ dstBase[j] = std::min(dstBase[j], srcBase[j + srcstep * h]);
452
+ #else
453
+ (void)size;
454
+ (void)srcBase;
455
+ (void)srcStride;
456
+ (void)dstBase;
457
+ #endif
458
+ }
459
+
460
+ } // namespace CAROTENE_NS