5 #ifndef DUNE_DEBUGALIGN_HH 6 #define DUNE_DEBUGALIGN_HH 18 #include <type_traits> 31 std::function<void(const char*, std::size_t, const void*)>;
56 inline bool isAligned(
const void *p, std::size_t align)
58 void* aligned_p = (
void*)p;
59 std::size_t space = align*2;
60 return p == std::align(align, align, aligned_p, space);
64 template<std::
size_t align,
class Impl>
75 static void*
operator new(std::size_t count,
void* ptr) {
78 return ::operator
new(count*
sizeof(Impl), ptr);
89 static void*
operator new[](std::size_t count,
void* ptr) {
92 return ::operator
new[](count*
sizeof(Impl), ptr);
99 namespace AlignedNumberImpl {
101 template<
class T, std::
size_t align = debugAlignment>
106 using AlignedNumberImpl::AlignedNumber;
108 template<
class T, std::
size_t align>
109 struct IsNumber<AlignedNumberImpl::AlignedNumber<T,align>>
110 :
public std::true_type {};
113 template<std::
size_t align = debugAlignment,
class T>
114 AlignedNumber<T, align>
aligned(T value) {
return { std::move(value) }; }
122 namespace AlignedNumberImpl {
125 template<
class T, std::
size_t align>
127 :
public AlignedBase<align, AlignedNumber<T, align> >
132 AlignedNumber() =
default;
133 AlignedNumber(T value) : value_(
std::move(value)) {}
134 template<
class U, std::size_t uAlign,
135 class = std::enable_if_t<(align >= uAlign) &&
136 std::is_convertible<U, T>::value> >
137 AlignedNumber(
const AlignedNumber<U, uAlign> &o) : value_(U(o)) {}
141 class = std::enable_if_t<std::is_convertible<T, U>::value> >
142 explicit operator U()
const {
return value_; }
144 const T &value()
const {
return value_; }
145 T &value() {
return value_; }
148 template<
class charT,
class Traits>
149 friend std::basic_istream<charT, Traits>&
150 operator>>(std::basic_istream<charT, Traits>& str, AlignedNumber &u)
152 return str >> u.value_;
155 template<
class charT,
class Traits>
156 friend std::basic_ostream<charT, Traits>&
157 operator<<(std::basic_ostream<charT, Traits>& str,
158 const AlignedNumber &u)
160 return str << u.value_;
172 template<
class U = T,
class = std::
void_t<decltype(++std::declval<U&>())> >
173 AlignedNumber &operator++() { ++value_;
return *
this; }
175 template<
class U = T,
class =
std::void_t<decltype(--std::declval<U&>())> >
176 AlignedNumber &operator--() { --value_;
return *
this; }
178 template<
class U = T,
class = std::
void_t<decltype(std::declval<U&>()++)> >
179 decltype(
auto) operator++(
int) {
return aligned<align>(value_++); }
181 template<
class U = T,
class = std::
void_t<decltype(std::declval<U&>()--)> >
182 decltype(
auto) operator--(
int) {
return aligned<align>(value_--); }
185 template<
class U = T,
186 class = std::void_t<decltype(+std::declval<const U&>())> >
187 decltype(
auto) operator+()
const {
return aligned<align>(+value_); }
189 template<
class U = T,
190 class =
std::void_t<decltype(-std::declval<const U&>())> >
191 decltype(
auto) operator-()
const {
return aligned<align>(-value_); }
198 # pragma GCC diagnostic push 199 # pragma GCC diagnostic ignored "-Wbool-operation" 202 # pragma clang diagnostic push 203 # pragma clang diagnostic ignored "-Wbool-operation" 205 template<
class U = T,
206 class = std::void_t<decltype(~std::declval<const U&>())> >
207 decltype(
auto) operator~()
const {
return aligned<align>(~value_); }
209 # pragma GCC diagnostic pop 212 # pragma clang diagnostic pop 215 template<
class U = T,
216 class = std::void_t<decltype(!std::declval<const U&>())> >
217 decltype(
auto) operator!()
const {
return aligned<align>(!value_); }
220 #define DUNE_ASSIGN_OP(OP) \ 221 template<class U, std::size_t uAlign, \ 222 class = std::enable_if_t< \ 223 ( uAlign <= align && \ 224 sizeof(std::declval<T&>() OP std::declval<U>()) ) \ 226 AlignedNumber &operator OP(const AlignedNumber<U, uAlign> &u) \ 233 class = std::void_t<decltype(std::declval<T&>() OP \ 234 std::declval<U>())> > \ 235 AlignedNumber &operator OP(const U &u) \ 241 static_assert(true, "Require semicolon to unconfuse editors") 257 #undef DUNE_ASSIGN_OP 261 #define DUNE_BINARY_OP(OP) \ 262 template<class T, std::size_t tAlign, class U, std::size_t uAlign, \ 263 class = std::void_t<decltype(std::declval<T>() \ 264 OP std::declval<U>())> > \ 266 operator OP(const AlignedNumber<T, tAlign> &t, \ 267 const AlignedNumber<U, uAlign> &u) \ 270 return aligned<(tAlign > uAlign ? tAlign : uAlign)>(T(t) OP U(u)); \ 273 template<class T, class U, std::size_t uAlign, \ 274 class = std::void_t<decltype(std::declval<T>() \ 275 OP std::declval<U>())> > \ 277 operator OP(const T &t, const AlignedNumber<U, uAlign> &u) \ 279 return aligned<uAlign>(t OP U(u)); \ 282 template<class T, std::size_t tAlign, class U, \ 283 class = std::void_t<decltype(std::declval<T>() \ 284 OP std::declval<U>())> > \ 286 operator OP(const AlignedNumber<T, tAlign> &t, const U &u) \ 288 return aligned<tAlign>(T(t) OP u); \ 291 static_assert(true, "Require semicolon to unconfuse editors") 317 #undef DUNE_BINARY_OP 323 #define DUNE_UNARY_FUNC(name) \ 324 template<class T, std::size_t align> \ 325 decltype(auto) name(const AlignedNumber<T, align> &u) \ 328 return aligned<align>(name(T(u))); \ 330 static_assert(true, "Require semicolon to unconfuse editors") 423 #undef DUNE_UNARY_FUNC 437 template<
class T, std::
size_t align>
438 auto max(
const AlignedNumber<T, align> &a,
439 const AlignedNumber<T, align> &b)
442 return aligned<align>(max(T(a), T(b)));
445 template<
class T, std::
size_t align>
446 auto max(
const T &a,
const AlignedNumber<T, align> &b)
449 return aligned<align>(
max(a, T(b)));
452 template<
class T, std::
size_t align>
453 auto max(
const AlignedNumber<T, align> &a,
const T &b)
456 return aligned<align>(
max(T(a), b));
459 template<
class T, std::
size_t align>
460 auto min(
const AlignedNumber<T, align> &a,
461 const AlignedNumber<T, align> &b)
464 return aligned<align>(
min(T(a), T(b)));
467 template<
class T, std::
size_t align>
468 auto min(
const T &a,
const AlignedNumber<T, align> &b)
471 return aligned<align>(
min(a, T(b)));
474 template<
class T, std::
size_t align>
475 auto min(
const AlignedNumber<T, align> &a,
const T &b)
478 return aligned<align>(
min(T(a), b));
484 template<
class T, std::
size_t align>
485 AlignedNumber<T, align>
486 cond(
const AlignedNumber<bool, align> &b,
487 const AlignedNumber<T, align> &v1,
const AlignedNumber<T, align> &v2)
493 template<
class T, std::
size_t align>
499 template<
class T, std::
size_t align>
505 template<std::
size_t align>
506 bool any_true(
const AlignedNumber<bool, align>& val)
511 template<std::
size_t align>
512 bool all_true(
const AlignedNumber<bool, align>& val)
519 namespace Overloads {
521 template<
class T, std::
size_t align>
524 template<
class U,
class T, std::
size_t align>
526 using type = AlignedNumber<U, align>;
529 template<
class T, std::
size_t align>
532 template<
class T, std::
size_t align>
539 template<
class T, std::
size_t align>
546 template<
class T, std::
size_t align>
547 const AlignedNumber<T, align> &
549 const AlignedNumber<T, align> &ifTrue,
550 const AlignedNumber<T, align> &ifFalse)
552 return mask ? ifTrue : ifFalse;
555 template<std::
size_t align>
567 #endif // DUNE_DEBUGALIGN_HH AlignedNumber< U, align > type
Definition: debugalign.hh:526
T max_value(const AlignedNumber< T, align > &val)
Definition: debugalign.hh:494
void violatedAlignment(const char *className, std::size_t expectedAlignment, const void *address)
called when an alignment violation is detected
Definition: debugalign.cc:36
ViolatedAlignmentHandler & violatedAlignmentHandler()
access the handler called by violatedAlignment()
Definition: debugalign.cc:30
const T1 cond(bool b, const T1 &v1, const T2 &v2)
conditional evaluate
Definition: conditional.hh:28
std::istream & operator>>(std::istream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:489
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:407
bool all_true(const AlignedNumber< bool, align > &val)
Definition: debugalign.hh:512
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
bool isAligned(const void *p, std::size_t align)
check whether an address conforms to the given alignment
Definition: debugalign.hh:56
bool any_true(const AlignedNumber< bool, align > &val)
Definition: debugalign.hh:506
should have a member type type
Definition: base.hh:196
Dune namespace
Definition: alignedallocator.hh:12
A free function to provide the demangled class name of a given object or type as a string...
static constexpr auto debugAlignment
an alignment large enough to trigger alignment errors
Definition: debugalign.hh:97
#define DUNE_UNARY_FUNC(name)
#define DUNE_ASSIGN_OP(OP)
Definition: debugalign.hh:220
T min_value(const AlignedNumber< T, align > &val)
Definition: debugalign.hh:500
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:511
Tag used to force late-binding lookup in Dune::Simd::Overloads.
Definition: base.hh:182
T lane(std::size_t l, const T &v)
access a lane of a simd vector (scalar version)
Definition: simd.hh:366
Default implementations for SIMD ImplementationsThis file provides default overloads for SIMD impleme...
constexpr auto sqrt(T t) requires(std
Definition: cmath.hh:39
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:311
CRTP base mixin class to check alignment.
Definition: debugalign.hh:65
should be derived from a Dune::index_constant
Definition: base.hh:212
Basic definitions for SIMD ImplementationsThis file provides basic definitions and template declarati...
bool anyTrue(ADLTag< 5 >, const AlignedNumber< bool, align > &mask)
Definition: debugalign.hh:556
std::function< void(const char *, std::size_t, const void *)> ViolatedAlignmentHandler
type of the handler called by violatedAlignment()
Definition: debugalign.hh:31
Mask< V > mask(ADLTag< 0, std::is_same< V, Mask< V > >::value >, const V &v)
implements Simd::mask()
Definition: defaults.hh:153
AlignedNumber< T, align > aligned(T value)
align a value to a certain alignment
Definition: debugalign.hh:114
Traits for type conversions and type information.
constexpr T abs(T t)
Definition: cmath.hh:27
should have a member type type
Definition: base.hh:204
T type
Definition: debugalign.hh:522
std::string className()
Provide the demangled class name of a type T as a string.
Definition: classname.hh:47
#define DUNE_BINARY_OP(OP)
Definition: debugalign.hh:261