Skip to content

Commit

Permalink
Move Vc math overloads to algebra-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Nov 22, 2024
1 parent 38b6a67 commit c02babd
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 57 deletions.
79 changes: 33 additions & 46 deletions math/common/include/algebra/math/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,42 @@
#include <algorithm>
#include <cmath>

namespace algebra::math {
namespace algebra {

/// Namespace to pick up math functions from
#if defined(CL_SYCL_LANGUAGE_VERSION) || defined(SYCL_LANGUAGE_VERSION)
namespace math_ns = cl::sycl;
namespace math = cl::sycl;
#else
namespace math_ns = std;
namespace math {

using std::abs;
using std::acos;
using std::asin;
using std::atan;
using std::atan2;
using std::atanh;
using std::ceil;
using std::copysign;
using std::cos;
using std::cosh;
using std::exp;
using std::fabs;
using std::floor;
using std::fma;
using std::hypot;
using std::log;
using std::log10;
using std::max;
using std::min;
using std::pow;
using std::signbit;
using std::sin;
using std::sinh;
using std::sqrt;
using std::tan;
using std::tanh;

} // namespace math
#endif // SYCL

/// Absolute value of arg
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline auto fabs(scalar_t arg) {
return math_ns::fabs(arg);
}

/// Fused multiply add
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline auto fma(scalar_t x, scalar_t y, scalar_t z) {
return math_ns::fma(x, y, z);
}

/// Arc tangent of y/x
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline scalar_t atan2(scalar_t y, scalar_t x) {
return math_ns::atan2(y, x);
}

/// Square root of arg
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline scalar_t sqrt(scalar_t arg) {
return math_ns::sqrt(arg);
}

/// Inverse hyperbolic tangent of arg
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline scalar_t atanh(scalar_t arg) {
return math_ns::atanh(arg);
}

/// Minimum of two values
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline scalar_t min(scalar_t a, scalar_t b) {
return math_ns::min(a, b);
}

/// Maximum of two values
template <concepts::scalar scalar_t>
ALGEBRA_HOST_DEVICE inline scalar_t max(scalar_t a, scalar_t b) {
return math_ns::max(a, b);
}

} // namespace algebra::math
} // namespace algebra
2 changes: 1 addition & 1 deletion math/vc_aos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Set up the library.
algebra_add_library( algebra_vc_aos_math vc_aos_math
"include/algebra/math/vc_aos.hpp"
"include/algebra/math/impl/vc_aos_getter.hpp"
"include/algebra/math/impl/vc_aos_matrix.hpp"
"include/algebra/math/impl/vc_aos_transform3.hpp"
"include/algebra/math/impl/vc_aos_vector.hpp" )
target_link_libraries( algebra_vc_aos_math
Expand Down
2 changes: 2 additions & 0 deletions math/vc_soa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Set up the library.
algebra_add_library( algebra_vc_soa_math vc_soa_math
"include/algebra/math/vc_soa.hpp"
"include/algebra/math/impl/vc_soa_math.hpp"
"include/algebra/math/impl/vc_soa_matrix.hpp"
"include/algebra/math/impl/vc_soa_vector.hpp")
target_link_libraries( algebra_vc_soa_math
INTERFACE algebra::common algebra::common_math algebra::common_storage algebra::vc_soa_storage Vc::Vc )
Expand Down
116 changes: 116 additions & 0 deletions math/vc_soa/include/algebra/math/impl/vc_soa_math.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/** Detray library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Vc include(s).
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif // MSVC
#include <Vc/Vc>
#ifdef _MSC_VER
#pragma warning(pop)
#endif // MSVC

namespace algebra::math {

/// Vc overloads of common math functions
/// @{
template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) abs(T &&vec) {
return Vc::abs(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) fabs(T &&vec) {
return Vc::abs(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) sqrt(T &&vec) {
return Vc::sqrt(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) exp(T &&vec) {
return Vc::exp(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) log(T &&vec) {
return Vc::log(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) sin(T &&vec) {
return Vc::sin(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) asin(T &&vec) {
return Vc::asin(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) cos(T &&vec) {
return Vc::cos(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) tan(T &&vec) {
// It seems there is no dedicated @c Vc::tan function ?
return Vc::sin(std::forward<T>(vec)) / Vc::cos(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) atan(T &&vec) {
return Vc::atan(std::forward<T>(vec));
}

template <typename T, typename S>
requires Vc::Traits::is_simd_vector<T>::value &&
Vc::Traits::is_simd_vector<S>::value
inline decltype(auto) copysign(T &&mag, S &&sgn) {
return Vc::copysign(std::forward<T>(mag), std::forward<S>(sgn));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) min(T &&vec) {
return Vc::min(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) max(T &&vec) {
return Vc::max(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) signbit(T &&vec) {
return Vc::isnegative(std::forward<T>(vec));
}

template <typename T>
requires Vc::Traits::is_simd_vector<T>::value
inline decltype(auto) fma(T &&x, T &&y, T &&z) {
return Vc::fma(std::forward<T>(x), std::forward<T>(y), std::forward<T>(z));
}
/// @}

} // namespace algebra::math
25 changes: 15 additions & 10 deletions math/vc_soa/include/algebra/math/impl/vc_soa_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

// Project include(s).
#include "algebra/concepts.hpp"
#include "algebra/math/impl/vc_soa_math.hpp"
#include "algebra/qualifiers.hpp"
#include "algebra/storage/vector.hpp"

Expand All @@ -32,8 +33,9 @@ namespace algebra::vc_soa::math {
/// @param v the input vector
template <std::size_t N, concepts::value value_t,
template <typename, std::size_t> class array_t>
requires(N >= 2) ALGEBRA_HOST_DEVICE
inline auto phi(const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {
requires(N >= 2)
ALGEBRA_HOST_DEVICE inline auto phi(
const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {

return Vc::atan2(v[1], v[0]);
}
Expand All @@ -47,7 +49,8 @@ requires(N >= 2) ALGEBRA_HOST_DEVICE
/// @param v the input vector
template <std::size_t N, concepts::value value_t,
template <typename, std::size_t> class array_t>
requires(N >= 2) ALGEBRA_HOST_DEVICE inline auto perp(
requires(N >= 2)
ALGEBRA_HOST_DEVICE inline auto perp(
const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {

return Vc::sqrt(Vc::fma(v[0], v[0], v[1] * v[1]));
Expand All @@ -62,7 +65,8 @@ requires(N >= 2) ALGEBRA_HOST_DEVICE inline auto perp(
/// @param v the input vector
template <std::size_t N, concepts::value value_t,
template <typename, std::size_t> class array_t>
requires(N >= 3) ALGEBRA_HOST_DEVICE inline auto theta(
requires(N >= 3)
ALGEBRA_HOST_DEVICE inline auto theta(
const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {

return Vc::atan2(perp(v), v[2]);
Expand All @@ -80,10 +84,10 @@ requires(N >= 3) ALGEBRA_HOST_DEVICE inline auto theta(
/// @return a vector (expression) representing the cross product
template <std::size_t N, concepts::value value_t,
template <typename, std::size_t> class array_t>
requires(N == 3) ALGEBRA_HOST_DEVICE
inline storage::vector<N, Vc::Vector<value_t>, array_t> cross(
const storage::vector<N, Vc::Vector<value_t>, array_t> &a,
const storage::vector<N, Vc::Vector<value_t>, array_t> &b) {
requires(N == 3)
ALGEBRA_HOST_DEVICE inline storage::vector<N, Vc::Vector<value_t>, array_t>
cross(const storage::vector<N, Vc::Vector<value_t>, array_t> &a,
const storage::vector<N, Vc::Vector<value_t>, array_t> &b) {

return {Vc::fma(a[1], b[2], -b[1] * a[2]), Vc::fma(a[2], b[0], -b[2] * a[0]),
Vc::fma(a[0], b[1], -b[0] * a[1])};
Expand Down Expand Up @@ -160,8 +164,9 @@ normalize(const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {
/// @param v the input vector
template <std::size_t N, concepts::value value_t,
template <typename, std::size_t> class array_t>
requires(N >= 3) ALGEBRA_HOST_DEVICE
inline auto eta(const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {
requires(N >= 3)
ALGEBRA_HOST_DEVICE inline auto eta(
const storage::vector<N, Vc::Vector<value_t>, array_t> &v) {

// atanh does not exist in Vc
auto atanh_func = [](value_t e) { return std::atanh(e); };
Expand Down
1 change: 1 addition & 0 deletions math/vc_soa/include/algebra/math/vc_soa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
#pragma once

// Project include(s).
#include "algebra/math/impl/vc_soa_math.hpp"
#include "algebra/math/impl/vc_soa_matrix.hpp"
#include "algebra/math/impl/vc_soa_vector.hpp"

0 comments on commit c02babd

Please sign in to comment.