Skip to content

Commit

Permalink
Port alpaka phi functions to DataFormats
Browse files Browse the repository at this point in the history
  • Loading branch information
VourMa committed Dec 31, 2024
1 parent 1ff4d57 commit dbf0fbf
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions DataFormats/Math/interface/alpaka/deltaPhi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef DataFormats_Math_alpaka_deltaPhi_h
#define DataFormats_Math_alpaka_deltaPhi_h

#include "HeterogeneousCore/AlpakaInterface/interface/config.h"

namespace cms::alpakatools {

// reduce to [-pi,pi]
template <typename TAcc, typename T>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T reduceRange(TAcc const& acc, T x) {
constexpr T o2pi = 1. / (2. * M_PI);
if (alpaka::math::abs(acc, x) <= T(M_PI))
return x;
T n = alpaka::math::round(acc, x * o2pi);
return x - n * T(2. * M_PI);
}

template <typename TAcc, typename T>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T phi(TAcc const& acc, T x, T y) {
return reduceRange(acc, M_PI + alpaka::math::atan2(acc, -y, -x));
}

template <typename TAcc, typename T>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T deltaPhi(TAcc const& acc, T x1, T y1, T x2, T y2) {
return reduceRange(acc, alpaka::math::atan2(acc, -y2, -x2) - alpaka::math::atan2(acc, -y1, -x1));
}

template <typename TAcc, typename T>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr T deltaPhi(T phi1, T phi2) {
return reduceRange(phi1 - phi2);
}

} // namespace cms::alpakatools

#endif

0 comments on commit dbf0fbf

Please sign in to comment.