Skip to content

Commit d4ea880

Browse files
authored
Merge pull request #139 from aous72/better_arm_support
Better ARM Support
2 parents 104c8a7 + 50021a4 commit d4ea880

File tree

17 files changed

+597
-254
lines changed

17 files changed

+597
-254
lines changed

.github/workflows/ccp-workflow.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,31 @@ jobs:
1515
name: ${{ matrix.system }} Build
1616
runs-on: ${{ matrix.runner }}
1717
steps:
18-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1919
- name: cmake
20-
run: cmake ..
20+
run: cmake -DOJPH_BUILD_STREAM_EXPAND=ON ..
2121
working-directory: build
2222
- name: build
2323
run: make
2424
working-directory: build
2525

26+
build_windows:
27+
strategy:
28+
matrix:
29+
include: [
30+
{ system: Windows, runner: windows-latest },
31+
]
32+
name: ${{ matrix.system }} Build
33+
runs-on: ${{ matrix.runner }}
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: cmake
37+
run: cmake -G "Visual Studio 17 2022" -A x64 -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_STREAM_EXPAND=ON ..
38+
working-directory: build
39+
- name: build
40+
run: cmake --build . --config Release
41+
working-directory: build
42+
2643
test:
2744
strategy:
2845
matrix:
@@ -33,7 +50,7 @@ jobs:
3350
name: ${{ matrix.system }} Test
3451
runs-on: ${{ matrix.runner }}
3552
steps:
36-
- uses: actions/checkout@v3
53+
- uses: actions/checkout@v4
3754
- name: cmake
3855
run: cmake -DOJPH_BUILD_TESTS=yes ..
3956
working-directory: build
@@ -53,9 +70,9 @@ jobs:
5370
name: ${{ matrix.system }} Test
5471
runs-on: ${{ matrix.runner }}
5572
steps:
56-
- uses: actions/checkout@v3
73+
- uses: actions/checkout@v4
5774
- name: cmake
58-
run: cmake -G "Visual Studio 17 2022" -A x64 -DOJPH_ENABLE_TIFF_SUPPORT=off -DOJPH_BUILD_TESTS=yes ..
75+
run: cmake -G "Visual Studio 17 2022" -A x64 -DOJPH_ENABLE_TIFF_SUPPORT=OFF -DOJPH_BUILD_TESTS=ON ..
5976
working-directory: build
6077
- name: build
6178
run: cmake --build . --config Release

CMakeLists.txt

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.11.0)
22

33
## Library name/version
4-
include(ojph_libname.cmake)
4+
include(ojph_version.cmake)
55

66
## project
77
project (openjph VERSION ${OPENJPH_VERSION} DESCRIPTION "Open source implementation of JPH" LANGUAGES CXX)
@@ -11,21 +11,63 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1111
# Building OpenJPH
1212
################################################################################################
1313

14+
## Target architecture
15+
# We use the target architecture to help with arranging files in "source_group" commands.
16+
# The code does not use the results provided by target_arch.cmake, and relies, instead,
17+
# on its own logic, which matches that in target_arch.cmake, to identify the architecture
18+
include(target_arch.cmake)
19+
target_architecture(OJPH_TARGET_ARCH)
20+
message(STATUS "CPU Architecture is ${OJPH_TARGET_ARCH}")
21+
1422
## options
15-
option(OJPH_DISABLE_INTEL_SIMD "Disables the use of SIMD instructions and associated files" OFF)
16-
option(OJPH_ENABLE_INTEL_AVX512 "enables the use of AVX512 SIMD instructions and associated files" ON)
1723
option(BUILD_SHARED_LIBS "Shared Libraries" ON)
1824
option(OJPH_ENABLE_TIFF_SUPPORT "Enables input and output support for TIFF files" ON)
1925
option(OJPH_BUILD_TESTS "Enables building test code" OFF)
2026
option(OJPH_BUILD_EXECUTABLES "Enables building command line executables" ON)
2127
option(OJPH_BUILD_STREAM_EXPAND "Enables building ojph_stream_expand executable" OFF)
2228

29+
option(OJPH_DISABLE_SIMD "Disables the use of SIMD instructions -- agnostic to architectures" OFF)
30+
option(OJPH_DISABLE_SSE "Disables the use of SSE SIMD instructions and associated files" OFF)
31+
option(OJPH_DISABLE_SSE2 "Disables the use of SSE2 SIMD instructions and associated files" OFF)
32+
option(OJPH_DISABLE_SSSE3 "Disables the use of SSSE3 SIMD instructions and associated files" OFF)
33+
option(OJPH_DISABLE_SSE4 "Disables the use of SSE4 SIMD instructions and associated files" OFF)
34+
option(OJPH_DISABLE_AVX "Disables the use of AVX SIMD instructions and associated files" OFF)
35+
option(OJPH_DISABLE_AVX2 "Disables the use of AVX2 SIMD instructions and associated files" OFF)
36+
option(OJPH_DISABLE_AVX512 "Disables the use of AVX512 SIMD instructions and associated files" OFF)
37+
option(OJPH_DISABLE_NEON "Disables the use of NEON SIMD instructions and associated files" OFF)
38+
39+
## options that are being deprecated
40+
if (DEFINED OJPH_DISABLE_INTEL_SIMD)
41+
message(STATUS "OJPH_DISABLE_INTEL_SIMD is being deprecated. Instead, use \"OJPH_DISABLE_SIMD\", "
42+
"which is architecture agnostic. If you do not specify any, the default is "
43+
"OJPH_DISABLE_SIMD=OFF.")
44+
set(OJPH_DISABLE_SIMD ${OJPH_DISABLE_INTEL_SIMD})
45+
message(STATUS "OJPH_DISABLE_SIMD is set to ${OJPH_DISABLE_SIMD}")
46+
unset(OJPH_DISABLE_INTEL_SIMD)
47+
endif()
48+
if (DEFINED OJPH_ENABLE_INTEL_AVX512)
49+
message(STATUS "OJPH_ENABLE_INTEL_AVX512 is being deprecated, use \"OJPH_DISABLE_AVX512\" instead."
50+
"If you do not specify any, the default is OJPH_DISABLE_AVX512=OFF.")
51+
if (OJPH_ENABLE_INTEL_AVX512)
52+
set(OJPH_DISABLE_AVX512 OFF)
53+
else()
54+
set(OJPH_DISABLE_AVX512 ON)
55+
endif()
56+
message(STATUS "OJPH_DISABLE_AVX512 is set to ${OJPH_DISABLE_AVX512}")
57+
unset(OJPH_ENABLE_INTEL_AVX512)
58+
endif()
59+
2360
## Setting some of the options if EMSCRIPTEN is the compiler
61+
# WebAssembly SIMD is treated differently. The SIMD flags above have no effect on the
62+
# use of WASM SIMD. This is because, for WASM, both non-SIMD and SIMD are required,
63+
# and therefore two sets of binaries are generated. For CPUs, one binary can carry both
64+
# non-SIMD and SIMD, and the program, at run-time, can decide which path to follow,
65+
# depending on what CPU instructions are available.
2466
if(EMSCRIPTEN)
25-
set(OJPH_DISABLE_INTEL_SIMD ON)
2667
set(BUILD_SHARED_LIBS OFF)
2768
set(OJPH_ENABLE_TIFF_SUPPORT OFF)
2869
set(OJPH_BUILD_STREAM_EXPAND OFF)
70+
set(OJPH_DISABLE_SIMD ON)
2971
endif()
3072

3173
# This is related to how the timestamp is set for URL downloaded files.
@@ -36,15 +78,16 @@ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0")
3678
endif()
3779
endif()
3880

81+
## Added by Michael Smith
3982
set(CMAKE_CXX_FLAGS_ASAN
4083
"-fsanitize=address -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer -g -O1"
4184
CACHE STRING "Flags used by the C++ compiler during AddressSanitizer builds."
4285
FORCE)
4386

44-
## build type
87+
## Build type
4588
if (NOT CMAKE_BUILD_TYPE)
4689
set(CMAKE_BUILD_TYPE "Release")
47-
message( STATUS "To use AddressSanitizer, use \"cmake .. -DCMAKE_BUILD_TYPE=asan\"" )
90+
message(STATUS "To use AddressSanitizer, use \"cmake .. -DCMAKE_BUILD_TYPE=asan\"" )
4891
endif()
4992
message(STATUS "Building ${CMAKE_BUILD_TYPE}")
5093

@@ -64,11 +107,34 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
64107
)
65108
endif()
66109

67-
## The option OJPH_DISABLE_INTEL_SIMD and OJPH_ENABLE_INTEL_AVX512
68-
if (OJPH_DISABLE_INTEL_SIMD)
69-
add_compile_definitions(OJPH_DISABLE_INTEL_SIMD)
70-
elseif (OJPH_ENABLE_INTEL_AVX512)
71-
add_compile_definitions(OJPH_ENABLE_INTEL_AVX512)
110+
## Enhanced instruction options
111+
if (OJPH_DISABLE_SIMD)
112+
add_compile_definitions(OJPH_DISABLE_SIMD)
113+
else()
114+
if(OJPH_DISABLE_SSE)
115+
add_compile_definitions(OJPH_DISABLE_SSE)
116+
endif()
117+
if(OJPH_DISABLE_SSE2)
118+
add_compile_definitions(OJPH_DISABLE_SSE2)
119+
endif()
120+
if(OJPH_DISABLE_SSSE3)
121+
add_compile_definitions(OJPH_DISABLE_SSSE3)
122+
endif()
123+
if(OJPH_DISABLE_SSE4)
124+
add_compile_definitions(OJPH_DISABLE_SSE4)
125+
endif()
126+
if(OJPH_DISABLE_AVX)
127+
add_compile_definitions(OJPH_DISABLE_AVX)
128+
endif()
129+
if(OJPH_DISABLE_AVX2)
130+
add_compile_definitions(OJPH_DISABLE_AVX2)
131+
endif()
132+
if(OJPH_DISABLE_AVX512)
133+
add_compile_definitions(OJPH_DISABLE_AVX512)
134+
endif()
135+
if(OJPH_DISABLE_NEON)
136+
add_compile_definitions(OJPH_DISABLE_NEON)
137+
endif()
72138
endif()
73139

74140
## Build library and applications

docs/compiling.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ The scripts, in the ```.devcontainer``` folder, will build a docker image that c
109109

110110
# Compiling for ARM and other platforms #
111111

112-
To compile for platforms where x86_64 SIMD instructions are not supported, such as on ARM, we need to disable SIMD instructions; this can be achieved using
112+
Compilation should simply work now. The simple test code I have passes when run on MacOS ARM on GitHub.
113113

114-
cd build
115-
cmake -DCMAKE_BUILD_TYPE=Release \ -DOJPH_DISABLE_INTEL_SIMD=ON ../
116-
make
114+
# Disabling SIMD instructions #
117115

118-
You may need to install libtiff. As I do not have an ARM board, I tested this using QEMU for aarch64 architecture, targeting a Cortex-A57 CPU. The code worked without issues, but because the ARM platform is emulated, the whole process was slow.
116+
The code now employs the architecture-agnostic option `OJPH_DISABLE_SIMD`, which should include SIMD instructions wherever they are supported. This can be achieved with `-DOJPH_DISABLE_SIMD=ON` option during CMake configuration. Individual instruction sets can be disabled; see the options in the main CMakeLists.txt file.

ojph_libname.cmake renamed to ojph_version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
################################################################################################
2-
# Generating library name
2+
# Generating ojph library version number
33
################################################################################################
44

55
############################################################

src/apps/ojph_compress/CMakeLists.txt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,32 @@ if(EMSCRIPTEN)
2929
target_link_libraries(ojph_compress_simd PRIVATE openjphsimd)
3030
install(TARGETS ojph_compress_simd DESTINATION bin)
3131
else()
32-
if(NOT OJPH_DISABLE_INTEL_SIMD)
33-
add_executable(ojph_compress ${SOURCES} ${OJPH_IMG_IO_SSE4} ${OJPH_IMG_IO_AVX2})
34-
35-
if (MSVC)
36-
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS "/arch:AVX2")
37-
else()
38-
set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1)
39-
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS -mavx2)
32+
if (NOT OJPH_DISABLE_SIMD)
33+
if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_X86_64") OR ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_I386"))
34+
if (NOT OJPH_DISABLE_SSE4)
35+
list(APPEND SOURCES ${OJPH_IMG_IO_SSE4})
36+
source_group("others" FILES ${OJPH_IMG_IO_SSE4})
37+
endif()
38+
if (NOT OJPH_DISABLE_AVX2)
39+
list(APPEND SOURCES ${OJPH_IMG_IO_AVX2})
40+
source_group("others" FILES ${OJPH_IMG_IO_AVX2})
41+
endif()
42+
43+
# Set compilation flags
44+
if (MSVC)
45+
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS "/arch:AVX2")
46+
else()
47+
set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1)
48+
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS -mavx2)
49+
endif()
50+
elseif ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_ARM")
51+
4052
endif()
41-
else()
42-
add_executable(ojph_compress ${SOURCES})
53+
4354
endif()
4455

56+
add_executable(ojph_compress ${SOURCES})
57+
4558
if( USE_TIFF )
4659
target_link_libraries(ojph_compress PUBLIC openjph ${TIFF_LIBRARIES})
4760
else()

src/apps/ojph_expand/CMakeLists.txt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,32 @@ if(EMSCRIPTEN)
2929
target_link_libraries(ojph_expand_simd PRIVATE openjphsimd)
3030
install(TARGETS ojph_expand_simd DESTINATION bin)
3131
else()
32-
if(NOT OJPH_DISABLE_INTEL_SIMD)
33-
add_executable(ojph_expand ${SOURCES} ${OJPH_IMG_IO_SSE4} ${OJPH_IMG_IO_AVX2})
34-
35-
if (MSVC)
36-
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS "/arch:AVX2")
37-
else()
38-
set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1)
39-
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS -mavx2)
32+
if (NOT OJPH_DISABLE_SIMD)
33+
if (("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_X86_64") OR ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_I386"))
34+
if (NOT OJPH_DISABLE_SSE4)
35+
list(APPEND SOURCES ${OJPH_IMG_IO_SSE4})
36+
source_group("others" FILES ${OJPH_IMG_IO_SSE4})
37+
endif()
38+
if (NOT OJPH_DISABLE_AVX2)
39+
list(APPEND SOURCES ${OJPH_IMG_IO_AVX2})
40+
source_group("others" FILES ${OJPH_IMG_IO_AVX2})
41+
endif()
42+
43+
# Set compilation flags
44+
if (MSVC)
45+
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS "/arch:AVX2")
46+
else()
47+
set_source_files_properties(${OJPH_IMG_IO_SSE4} PROPERTIES COMPILE_FLAGS -msse4.1)
48+
set_source_files_properties(${OJPH_IMG_IO_AVX2} PROPERTIES COMPILE_FLAGS -mavx2)
49+
endif()
50+
elseif ("${OJPH_TARGET_ARCH}" MATCHES "OJPH_ARCH_ARM")
51+
4052
endif()
41-
else()
42-
add_executable(ojph_expand ${SOURCES})
53+
4354
endif()
4455

56+
add_executable(ojph_expand ${SOURCES})
57+
4558
if( USE_TIFF )
4659
target_link_libraries(ojph_expand PUBLIC openjph ${TIFF_LIBRARIES})
4760
else()

src/apps/ojph_stream_expand/CMakeLists.txt

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33

44
if (OJPH_BUILD_STREAM_EXPAND)
55

6-
include_directories(../common)
7-
include_directories(../../core/common)
8-
set(CMAKE_CXX_STANDARD 14)
9-
10-
file(GLOB OJPH_STREAM_EXPAND "*.cpp" "*.h")
11-
file(GLOB OJPH_SOCKETS "../others/ojph_sockets.cpp")
12-
file(GLOB OJPH_SOCKETS_H "../common/ojph_sockets.h")
13-
file(GLOB OJPH_THREADS "../others/ojph_threads.cpp")
14-
file(GLOB OJPH_THREADS_H "../common/ojph_threads.h")
6+
include_directories(../common)
7+
include_directories(../../core/common)
8+
set(CMAKE_CXX_STANDARD 14)
9+
10+
file(GLOB OJPH_STREAM_EXPAND "*.cpp" "*.h")
11+
file(GLOB OJPH_SOCKETS "../others/ojph_sockets.cpp")
12+
file(GLOB OJPH_SOCKETS_H "../common/ojph_sockets.h")
13+
file(GLOB OJPH_THREADS "../others/ojph_threads.cpp")
14+
file(GLOB OJPH_THREADS_H "../common/ojph_threads.h")
1515

16-
list(APPEND SOURCES ${OJPH_STREAM_EXPAND} ${OJPH_SOCKETS} ${OJPH_SOCKETS_H} ${OJPH_THREADS} ${OJPH_THREADS_H})
16+
list(APPEND SOURCES ${OJPH_STREAM_EXPAND} ${OJPH_SOCKETS} ${OJPH_SOCKETS_H} ${OJPH_THREADS} ${OJPH_THREADS_H})
1717

18-
source_group("main" FILES ${OJPH_STREAM_EXPAND})
19-
source_group("others" FILES ${OJPH_SOCKETS} ${OJPH_THREADS})
20-
source_group("common" FILES ${OJPH_SOCKETS_H} ${OJPH_THREADS_H})
18+
source_group("main" FILES ${OJPH_STREAM_EXPAND})
19+
source_group("others" FILES ${OJPH_SOCKETS} ${OJPH_THREADS})
20+
source_group("common" FILES ${OJPH_SOCKETS_H} ${OJPH_THREADS_H})
2121

22-
add_executable(ojph_stream_expand ${SOURCES})
23-
if(MSVC)
24-
target_link_libraries(ojph_stream_expand PUBLIC openjph ws2_32)
25-
else()
26-
target_link_libraries(ojph_stream_expand PUBLIC openjph pthread)
27-
endif(MSVC)
22+
add_executable(ojph_stream_expand ${SOURCES})
23+
if(MSVC)
24+
target_link_libraries(ojph_stream_expand PUBLIC openjph ws2_32)
25+
else()
26+
target_link_libraries(ojph_stream_expand PUBLIC openjph pthread)
27+
endif(MSVC)
28+
29+
install(TARGETS ojph_stream_expand DESTINATION bin)
2830

29-
install(TARGETS ojph_stream_expand DESTINATION bin)
3031
endif(OJPH_BUILD_STREAM_EXPAND)

0 commit comments

Comments
 (0)