alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
SmartMaskValueRef.hpp
Go to the documentation of this file.
1/* Copyright 2026 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
8
9#include <type_traits>
10
11namespace alpaka::internal
12{
13
14 /** Simd mask reference
15 *
16 * A SIMD mask is not required to store its values as bool, it can store the values as a representable value type
17 * where all bits are 1 for true and zero for false. To be able to assign values to a SIMD mask we can not return a
18 * reference to the stored value because we need to cast the value during the write. For the read we need to cast
19 * the value to bool.
20 */
21 template<typename T, typename T_ValueMask>
23 {
24 using value_type = T;
25 using ValueMaskType = T_ValueMask;
26
27 constexpr SmartMaskValueRef(ValueMaskType& ref) noexcept : valueRef(ref)
28 {
29 }
30
31 // Convert to bool
32 constexpr operator bool() const noexcept
33 {
34 if constexpr(std::is_same_v<ValueMaskType, bool>)
35 {
36 return valueRef;
37 }
38 else
39 {
40 return valueRef != ValueMaskType{0};
41 }
42 }
43
44 // Optional: convert to raw storage type
45 constexpr ValueMaskType value() const noexcept
46 {
47 return valueRef;
48 }
49
50 // Unary operators
51 constexpr bool operator!() const noexcept
52 {
53 return !static_cast<bool>(*this);
54 }
55
56 constexpr ValueMaskType operator~() const noexcept
57 {
58 if constexpr(std::is_same_v<ValueMaskType, bool>)
59 return !valueRef;
60 else
61 return ~valueRef;
62 }
63
64 // Binary operators returning values (not references)
65 constexpr ValueMaskType operator|(SmartMaskValueRef const& rhs) const noexcept
66 {
67 return valueRef | rhs.valueRef;
68 }
69
70 constexpr ValueMaskType operator&(SmartMaskValueRef const& rhs) const noexcept
71 {
72 return valueRef & rhs.valueRef;
73 }
74
75 constexpr ValueMaskType operator^(SmartMaskValueRef const& rhs) const noexcept
76 {
77 if constexpr(std::is_same_v<ValueMaskType, bool>)
78 return static_cast<bool>(*this) != static_cast<bool>(rhs);
79 else
80 return valueRef ^ rhs.valueRef;
81 }
82
83 // Comparison operators
84 constexpr bool operator==(SmartMaskValueRef const& rhs) const noexcept
85 {
86 return static_cast<bool>(*this) == static_cast<bool>(rhs);
87 }
88
89 constexpr bool operator!=(SmartMaskValueRef const& rhs) const noexcept
90 {
91 return !(*this == rhs);
92 }
93
94#define SIMD_MASK_REF_ASSIGN_OP(OP, BOOL_FALLBACK) \
95 constexpr SmartMaskValueRef& operator OP(bool b) noexcept \
96 { \
97 return (*this OP internal::valueMaskCast<ValueMaskType>(b)); \
98 } \
99 \
100 constexpr SmartMaskValueRef& operator OP(ValueMaskType v) noexcept \
101 { \
102 if constexpr(std::is_same_v<ValueMaskType, bool>) \
103 { \
104 valueRef = valueRef BOOL_FALLBACK static_cast<bool>(v); \
105 } \
106 else \
107 { \
108 valueRef OP v; \
109 } \
110 return *this; \
111 } \
112 static_assert(true)
113
118
119#undef SIMD_MASK_REF_ASSIGN_OP
120
121 private:
123 };
124
125} // namespace alpaka::internal
#define SIMD_MASK_REF_ASSIGN_OP(OP, BOOL_FALLBACK)
alpaka internal implementations.
Definition generic.hpp:19
constexpr ValueMaskType operator&(SmartMaskValueRef const &rhs) const noexcept
constexpr bool operator!=(SmartMaskValueRef const &rhs) const noexcept
constexpr bool operator==(SmartMaskValueRef const &rhs) const noexcept
constexpr ValueMaskType value() const noexcept
constexpr ValueMaskType operator|(SmartMaskValueRef const &rhs) const noexcept
constexpr ValueMaskType operator^(SmartMaskValueRef const &rhs) const noexcept
constexpr bool operator!() const noexcept
constexpr SmartMaskValueRef(ValueMaskType &ref) noexcept
constexpr ValueMaskType operator~() const noexcept