|
| 1 | +// Copyright 2025 CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/// \file VcShim.h |
| 12 | +/// \brief Provides a basic fallback implementation for Vc |
| 13 | +/// |
| 14 | +/// \author Felix Weiglhofer |
| 15 | + |
| 16 | +#ifndef GPU_UTILS_VCSHIM_H |
| 17 | +#define GPU_UTILS_VCSHIM_H |
| 18 | + |
| 19 | +#ifndef GPUCA_NO_VC |
| 20 | + |
| 21 | +#include <Vc/Vc> |
| 22 | + |
| 23 | +#else |
| 24 | + |
| 25 | +#include <algorithm> |
| 26 | +#include <array> |
| 27 | +#include <bitset> |
| 28 | +#include <cstddef> |
| 29 | + |
| 30 | +namespace Vc |
| 31 | +{ |
| 32 | + |
| 33 | +constexpr struct VectorSpecialInitializerZero { |
| 34 | +} Zero; |
| 35 | +constexpr struct AlignedTag { |
| 36 | +} Aligned; |
| 37 | + |
| 38 | +template <typename T> |
| 39 | +typename T::vector_type& internal_data(T& v) |
| 40 | +{ |
| 41 | + return v.mData; |
| 42 | +} |
| 43 | + |
| 44 | +template <typename T> |
| 45 | +const typename T::vector_type& internal_data(const T& v) |
| 46 | +{ |
| 47 | + return v.mData; |
| 48 | +} |
| 49 | + |
| 50 | +namespace Common |
| 51 | +{ |
| 52 | + |
| 53 | +template <typename V, typename M> |
| 54 | +class WriteMaskVector |
| 55 | +{ |
| 56 | + private: |
| 57 | + const M& mMask; |
| 58 | + V& mVec; |
| 59 | + |
| 60 | + public: |
| 61 | + using value_type = typename V::value_type; |
| 62 | + |
| 63 | + WriteMaskVector(V& v, const M& m) : mMask(m), mVec(v) {} |
| 64 | + |
| 65 | + WriteMaskVector& operator++(int) |
| 66 | + { |
| 67 | + for (size_t i = 0; i < mVec.size(); i++) |
| 68 | + mVec[i] += value_type(mMask[i]); |
| 69 | + return *this; |
| 70 | + } |
| 71 | + |
| 72 | + WriteMaskVector& operator=(const value_type& v) |
| 73 | + { |
| 74 | + for (size_t i = 0; i < mVec.size(); i++) { |
| 75 | + if (mMask[i]) |
| 76 | + mVec[i] = v; |
| 77 | + } |
| 78 | + return *this; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +inline void prefetchMid(const void*) {} |
| 83 | +inline void prefetchFar(const void*) {} |
| 84 | +inline void prefetchForOneRead(const void*) {} |
| 85 | + |
| 86 | +} // namespace Common |
| 87 | + |
| 88 | +template <typename T, size_t N> |
| 89 | +class fixed_size_simd_mask |
| 90 | +{ |
| 91 | + private: |
| 92 | + std::bitset<N> mData; |
| 93 | + |
| 94 | + public: |
| 95 | + bool isNotEmpty() const { return mData.any(); } |
| 96 | + |
| 97 | + std::bitset<N>::reference operator[](size_t i) { return mData[i]; } |
| 98 | + bool operator[](size_t i) const { return mData[i]; } |
| 99 | + |
| 100 | + fixed_size_simd_mask operator!() const |
| 101 | + { |
| 102 | + auto o = *this; |
| 103 | + o.mData.flip(); |
| 104 | + return o; |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +template <typename T, size_t N> |
| 109 | +class fixed_size_simd |
| 110 | +{ |
| 111 | + private: |
| 112 | + std::array<T, N> mData; |
| 113 | + |
| 114 | + public: |
| 115 | + using vector_type = std::array<T, N>; |
| 116 | + using value_type = T; |
| 117 | + using mask_type = fixed_size_simd_mask<T, N>; |
| 118 | + |
| 119 | + static constexpr size_t size() { return N; } |
| 120 | + |
| 121 | + fixed_size_simd() = default; |
| 122 | + explicit fixed_size_simd(VectorSpecialInitializerZero) { mData = {}; } |
| 123 | + |
| 124 | + template <typename U> |
| 125 | + fixed_size_simd(const fixed_size_simd<U, N>& w) |
| 126 | + { |
| 127 | + std::copy_n(internal_data(w).begin(), N, mData.begin()); |
| 128 | + } |
| 129 | + |
| 130 | + fixed_size_simd(const T* d, AlignedTag) { std::copy_n(d, N, mData.begin()); } |
| 131 | + |
| 132 | + T& operator[](size_t i) { return mData[i]; } |
| 133 | + const T& operator[](size_t i) const { return mData[i]; } |
| 134 | + |
| 135 | + Common::WriteMaskVector<fixed_size_simd, mask_type> operator()(const mask_type& m) { return {*this, m}; } |
| 136 | + |
| 137 | + fixed_size_simd& operator=(const T& v) |
| 138 | + { |
| 139 | + for (auto& x : mData) |
| 140 | + x = v; |
| 141 | + return *this; |
| 142 | + } |
| 143 | + |
| 144 | + fixed_size_simd& operator+=(const T& v) |
| 145 | + { |
| 146 | + for (auto& x : mData) |
| 147 | + x += v; |
| 148 | + return *this; |
| 149 | + } |
| 150 | + |
| 151 | + fixed_size_simd& operator/=(const T& v) |
| 152 | + { |
| 153 | + for (auto& x : mData) |
| 154 | + x /= v; |
| 155 | + return *this; |
| 156 | + } |
| 157 | + |
| 158 | + fixed_size_simd operator/(const T& v) const |
| 159 | + { |
| 160 | + auto x = *this; |
| 161 | + return x /= v; |
| 162 | + } |
| 163 | + |
| 164 | + mask_type operator==(const T& v) const |
| 165 | + { |
| 166 | + mask_type m; |
| 167 | + for (size_t i = 0; i < N; i++) |
| 168 | + m[i] = mData[i] == v; |
| 169 | + return m; |
| 170 | + } |
| 171 | + |
| 172 | + mask_type operator!=(const T& v) const { return !(*this == v); } |
| 173 | + |
| 174 | + friend vector_type& internal_data<>(fixed_size_simd& x); |
| 175 | + friend const vector_type& internal_data<>(const fixed_size_simd& x); |
| 176 | +}; |
| 177 | + |
| 178 | +template <typename V> |
| 179 | +V max(const V& a, const V& b) |
| 180 | +{ |
| 181 | + V o; |
| 182 | + for (size_t i = 0; i < a.size(); i++) |
| 183 | + o[i] = std::max(a[i], b[i]); |
| 184 | + return o; |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace Vc |
| 188 | + |
| 189 | +#endif // ifndef GPUCA_NO_VC |
| 190 | + |
| 191 | +#endif |
0 commit comments