5#ifndef DUNE_DEBUGALIGN_HH
6#define DUNE_DEBUGALIGN_HH
22#include <dune/common/indices.hh>
31 std::function<void(
const char*, std::size_t,
const void*)>;
56 inline bool isAligned(
const void *p, std::size_t align)
60 return std::uintptr_t(p) % align == 0;
64 template<std::
size_t align,
class Impl>
67 void checkAlignment()
const
69 auto pimpl =
static_cast<const Impl*
>(
this);
86 namespace AlignedNumberImpl {
88 template<
class T, std::
size_t align = debugAlignment>
93 using AlignedNumberImpl::AlignedNumber;
95 template<
class T, std::
size_t align>
96 struct IsNumber<AlignedNumberImpl::AlignedNumber<T,align>>
97 :
public std::true_type {};
100 template<std::
size_t align = debugAlignment,
class T>
109 namespace AlignedNumberImpl {
112 template<
class T, std::
size_t align>
114 :
public AlignedBase<align, AlignedNumber<T, align> >
121 template<
class U, std::size_t uAlign,
122 class = std::enable_if_t<(align >= uAlign) &&
123 std::is_convertible<U, T>::value> >
128 class = std::enable_if_t<std::is_convertible<T, U>::value> >
129 explicit operator U()
const {
return value_; }
131 const T &value()
const {
return value_; }
132 T &value() {
return value_; }
135 template<
class charT,
class Traits>
136 friend std::basic_istream<charT, Traits>&
139 return str >> u.value_;
142 template<
class charT,
class Traits>
143 friend std::basic_ostream<charT, Traits>&
144 operator<<(std::basic_ostream<charT, Traits>& str,
147 return str << u.value_;
159 template<class U = T, class = std::void_t<decltype(++std::declval<U&>())> >
162 template<
class U = T,
class =
std::void_t<
decltype(--std::declval<U&>())> >
165 template<class U = T, class = std::void_t<decltype(std::declval<U&>()++)> >
166 decltype(
auto) operator++(
int) {
return aligned<align>(value_++); }
168 template<class U = T, class = std::void_t<decltype(std::declval<U&>()--)> >
169 decltype(
auto) operator--(
int) {
return aligned<align>(value_--); }
172 template<
class U = T,
173 class = std::void_t<decltype(+std::declval<const U&>())> >
174 decltype(
auto) operator+()
const {
return aligned<align>(+value_); }
176 template<
class U = T,
177 class =
std::void_t<
decltype(-std::declval<const U&>())> >
178 decltype(
auto) operator-()
const {
return aligned<align>(-value_); }
185# pragma GCC diagnostic push
186# pragma GCC diagnostic ignored "-Wbool-operation"
189# pragma clang diagnostic push
190# pragma clang diagnostic ignored "-Wbool-operation"
192 template<
class U = T,
193 class = std::void_t<decltype(~std::declval<const U&>())> >
194 decltype(
auto) operator~()
const {
return aligned<align>(~value_); }
196# pragma GCC diagnostic pop
199# pragma clang diagnostic pop
202 template<
class U = T,
203 class = std::void_t<decltype(!std::declval<const U&>())> >
204 decltype(
auto) operator!()
const {
return aligned<align>(!value_); }
207#define DUNE_ASSIGN_OP(OP) \
208 template<class U, std::size_t uAlign, \
209 class = std::enable_if_t< \
210 ( uAlign <= align && \
211 sizeof(std::declval<T&>() OP std::declval<U>()) ) \
213 AlignedNumber &operator OP(const AlignedNumber<U, uAlign> &u) \
220 class = std::void_t<decltype(std::declval<T&>() OP \
221 std::declval<U>())> > \
222 AlignedNumber &operator OP(const U &u) \
228 static_assert(true, "Require semicolon to unconfuse editors")
248#define DUNE_BINARY_OP(OP) \
249 template<class T, std::size_t tAlign, class U, std::size_t uAlign, \
250 class = std::void_t<decltype(std::declval<T>() \
251 OP std::declval<U>())> > \
253 operator OP(const AlignedNumber<T, tAlign> &t, \
254 const AlignedNumber<U, uAlign> &u) \
257 return aligned<(tAlign > uAlign ? tAlign : uAlign)>(T(t) OP U(u)); \
260 template<class T, class U, std::size_t uAlign, \
261 class = std::void_t<decltype(std::declval<T>() \
262 OP std::declval<U>())> > \
264 operator OP(const T &t, const AlignedNumber<U, uAlign> &u) \
266 return aligned<uAlign>(t OP U(u)); \
269 template<class T, std::size_t tAlign, class U, \
270 class = std::void_t<decltype(std::declval<T>() \
271 OP std::declval<U>())> > \
273 operator OP(const AlignedNumber<T, tAlign> &t, const U &u) \
275 return aligned<tAlign>(T(t) OP u); \
278 static_assert(true, "Require semicolon to unconfuse editors")
310#define DUNE_UNARY_FUNC(name) \
311 template<class T, std::size_t align> \
312 decltype(auto) name(const AlignedNumber<T, align> &u) \
315 return aligned<align>(name(T(u))); \
317 static_assert(true, "Require semicolon to unconfuse editors")
331 DUNE_UNARY_FUNC(abs);
332 DUNE_UNARY_FUNC(acos);
333 DUNE_UNARY_FUNC(acosh);
334 DUNE_UNARY_FUNC(asin);
335 DUNE_UNARY_FUNC(asinh);
336 DUNE_UNARY_FUNC(atan);
338 DUNE_UNARY_FUNC(atanh);
339 DUNE_UNARY_FUNC(cbrt);
340 DUNE_UNARY_FUNC(ceil);
342 DUNE_UNARY_FUNC(cos);
343 DUNE_UNARY_FUNC(cosh);
344 DUNE_UNARY_FUNC(erf);
345 DUNE_UNARY_FUNC(erfc);
346 DUNE_UNARY_FUNC(exp);
347 DUNE_UNARY_FUNC(exp2);
348 DUNE_UNARY_FUNC(expm1);
349 DUNE_UNARY_FUNC(fabs);
351 DUNE_UNARY_FUNC(floor);
358 DUNE_UNARY_FUNC(ilogb);
360 DUNE_UNARY_FUNC(lgamma);
361 DUNE_UNARY_FUNC(llrint);
362 DUNE_UNARY_FUNC(llround);
363 DUNE_UNARY_FUNC(log);
364 DUNE_UNARY_FUNC(log10);
365 DUNE_UNARY_FUNC(log1p);
366 DUNE_UNARY_FUNC(log2);
367 DUNE_UNARY_FUNC(logb);
368 DUNE_UNARY_FUNC(lrint);
369 DUNE_UNARY_FUNC(lround);
371 DUNE_UNARY_FUNC(nearbyint);
377 DUNE_UNARY_FUNC(rint);
378 DUNE_UNARY_FUNC(
round);
381 DUNE_UNARY_FUNC(sin);
382 DUNE_UNARY_FUNC(sinh);
383 DUNE_UNARY_FUNC(sqrt);
384 DUNE_UNARY_FUNC(tan);
385 DUNE_UNARY_FUNC(tanh);
386 DUNE_UNARY_FUNC(tgamma);
387 DUNE_UNARY_FUNC(
trunc);
389 DUNE_UNARY_FUNC(isfinite);
390 DUNE_UNARY_FUNC(isinf);
391 DUNE_UNARY_FUNC(isnan);
392 DUNE_UNARY_FUNC(isnormal);
393 DUNE_UNARY_FUNC(signbit);
408 DUNE_UNARY_FUNC(real);
410#undef DUNE_UNARY_FUNC
424 template<
class T, std::
size_t align>
429 return aligned<align>(
max(T(a), T(b)));
432 template<
class T, std::
size_t align>
436 return aligned<align>(
max(a, T(b)));
439 template<
class T, std::
size_t align>
440 auto max(
const AlignedNumber<T, align> &a,
const T &b)
443 return aligned<align>(
max(T(a), b));
446 template<
class T, std::
size_t align>
447 auto min(
const AlignedNumber<T, align> &a,
448 const AlignedNumber<T, align> &b)
451 return aligned<align>(
min(T(a), T(b)));
454 template<
class T, std::
size_t align>
455 auto min(
const T &a,
const AlignedNumber<T, align> &b)
458 return aligned<align>(
min(a, T(b)));
461 template<
class T, std::
size_t align>
462 auto min(
const AlignedNumber<T, align> &a,
const T &b)
465 return aligned<align>(
min(T(a), b));
471 template<
class T, std::
size_t align>
472 AlignedNumber<T, align>
473 cond(
const AlignedNumber<bool, align> &b,
474 const AlignedNumber<T, align> &v1,
const AlignedNumber<T, align> &v2)
480 template<
class T, std::
size_t align>
481 T max_value(
const AlignedNumber<T, align>& val)
486 template<
class T, std::
size_t align>
487 T min_value(
const AlignedNumber<T, align>& val)
492 template<std::
size_t align>
493 bool any_true(
const AlignedNumber<bool, align>& val)
498 template<std::
size_t align>
499 bool all_true(
const AlignedNumber<bool, align>& val)
506 namespace Overloads {
508 template<
class T, std::
size_t align>
511 template<
class U,
class T, std::
size_t align>
516 template<
class T, std::
size_t align>
519 template<
class T, std::
size_t align>
526 template<
class T, std::
size_t align>
533 template<
class T, std::
size_t align>
539 return mask ? ifTrue : ifFalse;
542 template<std::
size_t align>
Basic definitions for SIMD Implementations.
CRTP base mixin class to check alignment.
Definition: debugalign.hh:66
aligned wrappers for arithmetic types
Definition: debugalign.hh:115
A free function to provide the demangled class name of a given object or type as a string.
Default implementations for SIMD Implementations.
Traits for type conversions and type information.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:30
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
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:311
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:407
Mask< V > mask(ADLTag< 0, std::is_same< V, Mask< V > >::value >, const V &v)
implements Simd::mask()
Definition: defaults.hh:153
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition: defaults.hh:89
bool anyTrue(ADLTag< 0 >, const Mask &mask)=delete
implements Simd::anyTrue()
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:81
Dune namespace.
Definition: alignedallocator.hh:13
void violatedAlignment(const char *className, std::size_t expectedAlignment, const void *address)
called when an alignment violation is detected
Definition: debugalign.cc:39
std::string className()
Provide the demangled class name of a type T as a string.
Definition: classname.hh:47
static constexpr auto debugAlignment
an alignment large enough to trigger alignment errors
Definition: debugalign.hh:84
T lane(std::size_t l, const T &v)
access a lane of a simd vector (scalar version)
Definition: simd.hh:366
const T1 cond(bool b, const T1 &v1, const T2 &v2)
conditional evaluate
Definition: conditional.hh:28
AlignedNumber< T, align > aligned(T value)
align a value to a certain alignment
Definition: debugalign.hh:101
ViolatedAlignmentHandler & violatedAlignmentHandler()
access the handler called by violatedAlignment()
Definition: debugalign.cc:33
bool isAligned(const void *p, std::size_t align)
check whether an address conforms to the given alignment
Definition: debugalign.hh:56
std::function< void(const char *, std::size_t, const void *)> ViolatedAlignmentHandler
type of the handler called by violatedAlignment()
Definition: debugalign.hh:31
Tag used to force late-binding lookup in Dune::Simd::Overloads.
Definition: base.hh:182
should be derived from a Dune::index_constant
Definition: standard.hh:74
should have a member type type
Definition: standard.hh:67
should have a member type type
Definition: standard.hh:60