Skip to content

Commit

Permalink
simd wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
YGXXD committed Apr 18, 2024
1 parent 9cd1bee commit a88ea27
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: cmake ktm action
name: cmake ktm test

on:
push:
Expand Down Expand Up @@ -67,6 +67,7 @@ jobs:
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
echo "build-output-test-dir=${{ github.workspace }}/build/test" >> "$GITHUB_OUTPUT"
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand All @@ -76,19 +77,19 @@ jobs:
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DKTM_BUILD_TESTING=ON
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
working-directory: ${{ steps.strings.outputs.build-output-test-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}

- name: Install
# Install your program with the given configuration.
run: ${{ matrix.root }} cmake --install ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

run: ${{ matrix.root }} cmake --install ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
59 changes: 59 additions & 0 deletions .github/workflows/cmake_ktm_wasm_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: cmake ktm wasm test

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false

matrix:
os: [macos-latest, ubuntu-latest]
build_type: [Release]
include:
- os: macos-latest
- os: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v14
with:
version: "latest"
actions-cache-folder: 'emsdk-cache'

- name: Set reusable strings
id: strings
shell: bash
run: |
echo $EMSDK
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
echo "build-output-test-dir=${{ github.workspace }}/build/test" >> "$GITHUB_OUTPUT"
- name: Configure CMake
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DKTM_BUILD_TESTING=ON
-DKTM_BUILD_WASM=ON
-DCMAKE_TOOLCHAIN_FILE="$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
-S ${{ github.workspace }}
- name: Build
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-test-dir }}
run: ctest --build-config ${{ matrix.build_type }}

- name: Install
run: cmake --install ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
48 changes: 21 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
cmake_minimum_required(VERSION 3.20)

project(ktm)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/std:c++17)
project(ktm)

if(KTM_BUILD_WASM)
add_compile_options(-msimd128)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/source-charset:utf-8)
add_compile_options(/execution-charset:utf-8)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-std=gnu++17)
add_compile_options(-fvisibility=hidden)
add_compile_options(-march=native)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
add_compile_options(-std=gnu++17)
add_compile_options(-Wc++17-extensions)
add_compile_options(-fvisibility=hidden)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
add_compile_options(-march=native)
endif()

add_executable(geometry_test ${CMAKE_CURRENT_SOURCE_DIR}/test/geometry_test.cpp)
add_executable(matrix_test ${CMAKE_CURRENT_SOURCE_DIR}/test/matrix_test.cpp)
add_executable(quaternion_test ${CMAKE_CURRENT_SOURCE_DIR}/test/quaternion_test.cpp)
add_executable(vector_test ${CMAKE_CURRENT_SOURCE_DIR}/test/vector_test.cpp)

enable_testing()
if(KTM_BUILD_TESTING)
add_subdirectory(test)
endif()

add_test(geometry_test geometry_test)
add_test(matrix_test matrix_test)
add_test(quaternion_test quaternion_test)
add_test(vector_test vector_test)
add_executable(ktm ${CMAKE_CURRENT_SOURCE_DIR}/src/ktm.cpp)

install(DIRECTORY ktm DESTINATION include)

if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)

add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@
**特点**

- head-only,引入头文件即可使用
- 支持Simd指令集加速,SSE,SSE2,SSE3,SSE4.1,SSE4.2,Neon
- 支持Simd指令集加速,SSE,SSE2,SSE3,SSE4.1,SSE4.2,Neon,Wasm
- 代码结构清晰,类利用模板实现组件化

**构建和安装**

```shell
# unix
mkdir build && cd build
cmake ..
sudo make install

# windows
cmake -S . -B ./build
cmake --install ./build --config Release
```

**示例**

```c++
#include "ktm/ktm.h"
#include <ktm/ktm.h>

using namespace ktm;
using namespace std;
Expand Down
27 changes: 7 additions & 20 deletions ktm/detail/function/common_simd.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "common_fwd.h"
#include "../../simd/skv.h"

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

template<>
struct ktm::detail::common_implement::reduce_add<4, float>
Expand Down Expand Up @@ -278,9 +278,9 @@ struct ktm::detail::common_implement::fast_recip<N, float, std::enable_if_t<N ==
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2 | KTM_SIMD_WASM)

template<>
struct ktm::detail::common_implement::reduce_add<4, int>
Expand All @@ -304,9 +304,9 @@ struct ktm::detail::common_implement::abs<N, int, std::enable_if_t<N == 3 || N =
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2 | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

template<>
struct ktm::detail::common_implement::reduce_min<4, int>
Expand Down Expand Up @@ -364,23 +364,10 @@ struct ktm::detail::common_implement::clamp<N, int, std::enable_if_t<N == 3 || N
}
};

#endif // KTM_SIMD_X86 & KTM_SIMD_SSE4_1_FLAG
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON)

// template<size_t L>
// struct ktm::detail::common_implement::elem_move<L, 2, float>
// {
// static_assert(L == 1);
// using V = vec<2, float>;
// static KTM_INLINE V call(const V& x) noexcept
// {
// V ret;
// ret.st = _shuffo64_f32(x.st, x.st, 0, L);
// return ret;
// }
// };

template<>
struct ktm::detail::common_implement::abs<2, float>
{
Expand Down Expand Up @@ -664,6 +651,6 @@ struct ktm::detail::common_implement::clamp<2, int>
}
};

#endif
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON)

#endif
4 changes: 2 additions & 2 deletions ktm/detail/function/geometric_simd.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "geometric_fwd.h"
#include "../../simd/skv.h"

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

template<size_t N>
struct ktm::detail::geometric_implement::dot<N, float, std::enable_if_t<N == 3 || N == 4>>
Expand Down Expand Up @@ -252,7 +252,7 @@ struct ktm::detail::geometric_implement::fast_normalize<N, float, std::enable_if
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON)

Expand Down
8 changes: 4 additions & 4 deletions ktm/detail/function/matrix_simd.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "matrix_fwd.h"
#include "../../simd/skv.h"

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

template<>
struct ktm::detail::matrix_implement::transpose<2, 2, float>
Expand Down Expand Up @@ -345,9 +345,9 @@ struct ktm::detail::matrix_implement::inverse<4, float>
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

template<>
struct ktm::detail::matrix_implement::determinant<3, int>
Expand Down Expand Up @@ -405,6 +405,6 @@ struct ktm::detail::matrix_implement::determinant<4, int>
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

#endif
12 changes: 6 additions & 6 deletions ktm/detail/matrix/mat_calc_simd.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "mat_calc_fwd.h"
#include "../../simd/skv.h"

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

template<size_t Row, size_t Col>
struct ktm::detail::mat_opt_implement::mat_mul_vec<Row, Col, float, std::enable_if_t<Col == 3 || Col == 4>>
Expand Down Expand Up @@ -100,9 +100,9 @@ private:
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2 | KTM_SIMD_WASM)

template<size_t Row, size_t Col>
struct ktm::detail::mat_opt_implement::add<Row, Col, int, std::enable_if_t<Col == 3 || Col == 4>>
Expand Down Expand Up @@ -142,9 +142,9 @@ private:
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE2 | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

template<size_t Row, size_t Col>
struct ktm::detail::mat_opt_implement::mat_mul_vec<Row, Col, int, std::enable_if_t<Col == 3 || Col == 4>>
Expand Down Expand Up @@ -195,7 +195,7 @@ private:
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON)

Expand Down
4 changes: 2 additions & 2 deletions ktm/detail/quaternion/quat_calc_simd.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "quat_calc_fwd.h"
#include "../../simd/skv.h"

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

namespace ktm
{
Expand Down Expand Up @@ -81,6 +81,6 @@ struct ktm::detail::quat_calc_implement::act<float>
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

#endif
4 changes: 2 additions & 2 deletions ktm/detail/vector/vec_calc_simd.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "vec_calc_fwd.h"
#include "../../simd/skv.h"

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE)
#if KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE | KTM_SIMD_WASM)

template<size_t N>
struct ktm::detail::vec_calc_implement::add<N, float, std::enable_if_t<N == 3 || N == 4>>
Expand Down Expand Up @@ -354,7 +354,7 @@ struct ktm::detail::vec_calc_implement::mul_scalar_to_self<N, int, std::enable_i
}
};

#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1)
#endif // KTM_SIMD_ENABLE(KTM_SIMD_NEON | KTM_SIMD_SSE4_1 | KTM_SIMD_WASM)

#if KTM_SIMD_ENABLE(KTM_SIMD_NEON)

Expand Down
Loading

0 comments on commit a88ea27

Please sign in to comment.