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)
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>
122 namespace AlignedNumberImpl {
125 template<
class T, std::
size_t align>
127 :
public AlignedBase<align, AlignedNumber<T, align> >
134 template<
class U, std::size_t uAlign,
135 class = std::enable_if_t<(align >= uAlign) &&
136 std::is_convertible<U, T>::value> >
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>&
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,
160 return str << u.value_;
172 template<class U = T, class = std::void_t<decltype(++std::declval<U&>())> >
175 template<
class U = T,
class =
std::void_t<
decltype(--std::declval<U&>())> >
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")
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")
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")
344 DUNE_UNARY_FUNC(abs);
345 DUNE_UNARY_FUNC(acos);
346 DUNE_UNARY_FUNC(acosh);
347 DUNE_UNARY_FUNC(asin);
348 DUNE_UNARY_FUNC(asinh);
349 DUNE_UNARY_FUNC(atan);
351 DUNE_UNARY_FUNC(atanh);
352 DUNE_UNARY_FUNC(cbrt);
353 DUNE_UNARY_FUNC(ceil);
355 DUNE_UNARY_FUNC(cos);
356 DUNE_UNARY_FUNC(cosh);
357 DUNE_UNARY_FUNC(erf);
358 DUNE_UNARY_FUNC(erfc);
359 DUNE_UNARY_FUNC(exp);
360 DUNE_UNARY_FUNC(exp2);
361 DUNE_UNARY_FUNC(expm1);
362 DUNE_UNARY_FUNC(fabs);
364 DUNE_UNARY_FUNC(floor);
371 DUNE_UNARY_FUNC(ilogb);
373 DUNE_UNARY_FUNC(lgamma);
374 DUNE_UNARY_FUNC(llrint);
375 DUNE_UNARY_FUNC(llround);
376 DUNE_UNARY_FUNC(log);
377 DUNE_UNARY_FUNC(log10);
378 DUNE_UNARY_FUNC(log1p);
379 DUNE_UNARY_FUNC(log2);
380 DUNE_UNARY_FUNC(logb);
381 DUNE_UNARY_FUNC(lrint);
382 DUNE_UNARY_FUNC(lround);
384 DUNE_UNARY_FUNC(nearbyint);
390 DUNE_UNARY_FUNC(rint);
391 DUNE_UNARY_FUNC(
round);
394 DUNE_UNARY_FUNC(sin);
395 DUNE_UNARY_FUNC(sinh);
396 DUNE_UNARY_FUNC(sqrt);
397 DUNE_UNARY_FUNC(tan);
398 DUNE_UNARY_FUNC(tanh);
399 DUNE_UNARY_FUNC(tgamma);
400 DUNE_UNARY_FUNC(
trunc);
402 DUNE_UNARY_FUNC(isfinite);
403 DUNE_UNARY_FUNC(isinf);
404 DUNE_UNARY_FUNC(isnan);
405 DUNE_UNARY_FUNC(isnormal);
406 DUNE_UNARY_FUNC(signbit);
421 DUNE_UNARY_FUNC(real);
423#undef DUNE_UNARY_FUNC
437 template<
class T, std::
size_t align>
442 return aligned<align>(
max(T(a), T(b)));
445 template<
class T, std::
size_t align>
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>
494 T max_value(
const AlignedNumber<T, align>& val)
499 template<
class T, std::
size_t align>
500 T min_value(
const AlignedNumber<T, align>& val)
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>
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>
552 return mask ? ifTrue : ifFalse;
555 template<std::
size_t align>
Basic definitions for SIMD Implementations.
aligned wrappers for arithmetic types
Definition: debugalign.hh:128
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: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
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
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
Mask< V > mask(ADLTag< 0, std::is_same< V, Mask< V > >::value >, const V &v)
implements Simd::mask()
Definition: defaults.hh:153
bool anyTrue(ADLTag< 0 >, const Mask &mask)=delete
implements Simd::anyTrue()
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:36
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:97
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:114
ViolatedAlignmentHandler & violatedAlignmentHandler()
access the handler called by violatedAlignment()
Definition: debugalign.cc:30
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
CRTP base mixin class to check alignment.
Definition: debugalign.hh:66
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