forked from alpaka-group/llama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ProxyRefOpMixin CRTP mixing for proxy references
* supplies compound assignment and increment/decrement operators * mixin ProxyRefOpMixin into proxy references for ChangeTypeReference, BitPackedFloatRef, BitPackedIntRef and Bytesplit::Reference * add unit tests for ProxyRefOpMixin and proxy references of mappings Fixes alpaka-group#429
- Loading branch information
1 parent
727f363
commit ec4fe12
Showing
7 changed files
with
473 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#pragma once | ||
|
||
#include "Concepts.hpp" | ||
#include "macros.hpp" | ||
|
||
namespace llama | ||
{ | ||
/// CRTP mixin for proxy reference types to support all compound assignment and increment/decrement operators. | ||
template<typename Derived, typename ValueType> | ||
struct ProxyRefOpMixin | ||
{ | ||
private: | ||
LLAMA_FN_HOST_ACC_INLINE constexpr auto derived() -> Derived& | ||
{ | ||
return static_cast<Derived&>(*this); | ||
} | ||
|
||
// in principle, load() could be const, but we use it only from non-const functions | ||
LLAMA_FN_HOST_ACC_INLINE constexpr auto load() -> ValueType | ||
{ | ||
return static_cast<ValueType>(derived()); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr void store(ValueType t) | ||
{ | ||
derived() = std::move(t); | ||
} | ||
|
||
public: | ||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator+=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs += rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator-=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs -= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator*=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs *= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator/=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs /= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator%=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs %= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator<<=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs <<= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator>>=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs >>= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator&=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs &= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator|=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs |= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator^=(const ValueType& rhs) -> Derived& | ||
{ | ||
ValueType lhs = load(); | ||
lhs ^= rhs; | ||
store(lhs); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator++() -> Derived& | ||
{ | ||
ValueType v = load(); | ||
++v; | ||
store(v); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator++(int) -> ValueType | ||
{ | ||
ValueType v = load(); | ||
ValueType old = v++; | ||
store(v); | ||
return old; | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator--() -> Derived& | ||
{ | ||
ValueType v = load(); | ||
--v; | ||
store(v); | ||
return derived(); | ||
} | ||
|
||
LLAMA_FN_HOST_ACC_INLINE constexpr auto operator--(int) -> ValueType | ||
{ | ||
ValueType v = load(); | ||
ValueType old = v--; | ||
store(v); | ||
return old; | ||
} | ||
}; | ||
} // namespace llama |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.