VecCore 0.8.1
C++ Library for Portable SIMD Vectorization
Loading...
Searching...
No Matches
ScalarWrapper.h
Go to the documentation of this file.
1#ifndef VECCORE_BACKEND_SCALAR_WRAPPER_H
2#define VECCORE_BACKEND_SCALAR_WRAPPER_H
3
4#include <type_traits>
5
6namespace vecCore {
7
8class WrappedBool;
9template <typename>
10class MaskedScalar;
11template <typename>
12class WrappedScalar;
13
14template <>
16 using ScalarType = bool;
18 static constexpr size_t Size = 1;
19};
20
21template <typename T>
23 using ScalarType = T;
26 static constexpr size_t Size = 1;
27};
28
29namespace backend {
30
31template <typename T = Real_s>
33public:
37
42
47};
48
50
51} // namespace backend
52
54public:
55 static constexpr size_t Size = 1;
56
58 WrappedBool() { /* uninitialized */}
59
61 WrappedBool(bool val) : fBool(val) {}
62
64 bool isFull() const { return fBool; }
65
67 bool isEmpty() const { return !fBool; }
68
70 static constexpr size_t size() { return 1; }
71
73 operator bool &() noexcept { return fBool; }
74
76 operator bool const &() const noexcept { return fBool; }
77
79 bool &operator[](int index)
80 {
81 assert(index == 0);
82 (void)index;
83 return fBool;
84 }
85
87 bool operator[](int index) const
88 {
89 assert(index == 0);
90 (void)index;
91 return fBool;
92 }
93
95 void store(bool *dest) const { *dest = fBool; }
96
97private:
98 bool fBool{};
99};
100
101template <class T>
103public:
105
107 MaskedScalar() = delete;
108
110 MaskedScalar(T &ref, Mask mask = true) : fRef(ref), fMask(mask) {}
111
112#define MASK_ASSIGN_OPERATOR(OP) \
113 VECCORE_ATT_HOST_DEVICE \
114 T &operator OP(const T &ref) \
115 { \
116 if (fMask) fRef OP ref; \
117 return fRef; \
118 }
119
131
132#undef MASK_ASSIGN_OPERATOR
133
134private:
135 T &fRef;
136 Mask fMask;
137};
138
139template <class T>
141public:
142 using Type = T;
144
145 static constexpr size_t Size = 1;
146
148 WrappedScalar() { /* uninitialized */}
149
151 WrappedScalar(const T &val) : fVal(val) {}
152
154 WrappedScalar(const T *const val_ptr) : fVal(*val_ptr) {}
155
157 WrappedScalar(const WrappedScalar *const s) : fVal(s->val_ptr) {}
158
159 /* allow type conversion from other scalar types at initialization */
160 template <typename Type, class = typename std::enable_if<std::is_integral<Type>::value>::type>
162 WrappedScalar(const Type &val) : fVal(static_cast<T>(val))
163 {
164 }
165
167 static constexpr size_t size() { return 1; }
168
170 operator T &() noexcept { return fVal; }
171
173 operator T const &() const noexcept { return fVal; }
174
177
179 T &operator[](int index)
180 {
181 assert(index == 0);
182 (void)index;
183 return fVal;
184 }
185
187 T const operator[](int index) const
188 {
189 assert(index == 0);
190 (void)index;
191 return fVal;
192 }
193
195 void load(T const *const src) { fVal = *src; }
196
198 void store(T &dest) const { dest = fVal; }
199
201 void store(T *dest) const { *dest = fVal; }
202
203#define SCALAR_WRAPPER_OPERATOR(OP) \
204 VECCORE_FORCE_INLINE \
205 VECCORE_ATT_HOST_DEVICE \
206 WrappedScalar operator OP(const WrappedScalar &x) const { return WrappedScalar(fVal OP x.fVal); } \
207 \
208 VECCORE_FORCE_INLINE \
209 VECCORE_ATT_HOST_DEVICE \
210 WrappedScalar operator OP(const T &x) const { return WrappedScalar(fVal OP x); }
211
217
218#undef SCALAR_WRAPPER_OPERATOR
219
220private:
221 T fVal;
222};
223
224template <>
228{
229 return !mask;
230}
231
232template <>
236{
237 return mask;
238}
239
240template <typename T>
244 static void Assign(WrappedScalar<T> &dst, WrappedBool const &mask, WrappedScalar<T> const &src)
245 {
246 if (mask) dst = src;
247 }
248
251 static void Blend(WrappedScalar<T> &dst, WrappedBool const &mask, WrappedScalar<T> const &src1,
252 WrappedScalar<T> const &src2)
253 {
254 dst = mask ? src1 : src2;
255 }
256};
257
258} // namespace vecCore
259
260#endif
#define VECCORE_ATT_HOST_DEVICE
Definition: CUDA.h:10
#define VECCORE_FORCE_INLINE
Definition: Common.h:32
#define MASK_ASSIGN_OPERATOR(OP)
#define SCALAR_WRAPPER_OPERATOR(OP)
VECCORE_ATT_HOST_DEVICE MaskedScalar()=delete
VECCORE_ATT_HOST_DEVICE MaskedScalar(T &ref, Mask mask=true)
VECCORE_ATT_HOST_DEVICE bool isFull() const
Definition: ScalarWrapper.h:64
static VECCORE_ATT_HOST_DEVICE constexpr size_t size()
Definition: ScalarWrapper.h:70
VECCORE_ATT_HOST_DEVICE WrappedBool()
Definition: ScalarWrapper.h:58
VECCORE_ATT_HOST_DEVICE bool isEmpty() const
Definition: ScalarWrapper.h:67
VECCORE_ATT_HOST_DEVICE WrappedBool(bool val)
Definition: ScalarWrapper.h:61
VECCORE_ATT_HOST_DEVICE bool operator[](int index) const
Definition: ScalarWrapper.h:87
VECCORE_ATT_HOST_DEVICE void store(bool *dest) const
Definition: ScalarWrapper.h:95
VECCORE_ATT_HOST_DEVICE bool & operator[](int index)
Definition: ScalarWrapper.h:79
static constexpr size_t Size
Definition: ScalarWrapper.h:55
VECCORE_ATT_HOST_DEVICE WrappedScalar(const Type &val)
static VECCORE_ATT_HOST_DEVICE constexpr size_t size()
VECCORE_ATT_HOST_DEVICE WrappedScalar(const T &val)
VECCORE_ATT_HOST_DEVICE T & operator[](int index)
VECCORE_ATT_HOST_DEVICE WrappedScalar()
VECCORE_ATT_HOST_DEVICE void load(T const *const src)
VECCORE_ATT_HOST_DEVICE void store(T *dest) const
static constexpr size_t Size
VECCORE_ATT_HOST_DEVICE MaskedScalar< T > operator()(Mask m)
VECCORE_ATT_HOST_DEVICE WrappedScalar(const T *const val_ptr)
VECCORE_ATT_HOST_DEVICE T const operator[](int index) const
VECCORE_ATT_HOST_DEVICE void store(T &dest) const
VECCORE_ATT_HOST_DEVICE WrappedScalar(const WrappedScalar *const s)
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE bool MaskEmpty< WrappedBool >(const WrappedBool &mask)
VECCORE_FORCE_INLINE VECCORE_ATT_HOST_DEVICE bool MaskFull< WrappedBool >(const WrappedBool &mask)
VECCORE_FORCE_INLINE static VECCORE_ATT_HOST_DEVICE void Assign(WrappedScalar< T > &dst, WrappedBool const &mask, WrappedScalar< T > const &src)
VECCORE_FORCE_INLINE static VECCORE_ATT_HOST_DEVICE void Blend(WrappedScalar< T > &dst, WrappedBool const &mask, WrappedScalar< T > const &src1, WrappedScalar< T > const &src2)
static constexpr size_t Size
Definition: Scalar.h:14