File: loop.hh

package info (click to toggle)
dune-common 2.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: cpp: 52,256; python: 3,979; sh: 1,658; makefile: 17
file content (627 lines) | stat: -rw-r--r-- 25,438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_SIMD_LOOP_HH
#define DUNE_COMMON_SIMD_LOOP_HH

#include <array>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <cstdint>
#include <ostream>

#include <dune/common/math.hh>
#include <dune/common/simd/simd.hh>
#include <dune/common/typetraits.hh>

namespace Dune {


/*
 * silence warnings from GCC about using integer operands on a bool
 * (when instantiated for T=bool)
 */
#if __GNUC__ >= 7
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wbool-operation"
#  pragma GCC diagnostic ignored "-Wint-in-bool-context"
#  define GCC_WARNING_DISABLED
#endif

/*
 * silence warnings from Clang about using bitwise operands on
 * a bool (when instantiated for T=bool)
 */
#ifdef __clang__
#if __has_warning("-Wbitwise-instead-of-logical")
#  pragma clang diagnostic push
#  pragma clang diagnostic ignored "-Wbitwise-instead-of-logical"
#  define CLANG_WARNING_DISABLED
#endif
#endif

/*
 * Introduce a simd pragma if OpenMP is available in standard version >= 4
 */
#if _OPENMP >= 201307
  #define DUNE_PRAGMA_OMP_SIMD _Pragma("omp simd")
#else
  #define DUNE_PRAGMA_OMP_SIMD
#endif


  /**
    *  This class specifies a vector-like type deriving from std::array
    *  for memory management and basic accessibility.
    *  This type is capable of dealing with all (well-defined) operators
    *  and is usable with the SIMD-interface.
    *
    *  @tparam T Base type. Could also be vectorized type.
    *  @tparam S Size
    *  @tparam minimum alignment. It is inherited to rebound types.
    */

  template<class T, std::size_t S, std::size_t A = 0>
  class alignas(A==0?alignof(T):A) LoopSIMD : public std::array<T,S> {

  public:

    //default constructor
    LoopSIMD() {
      assert(reinterpret_cast<uintptr_t>(this) % std::min(alignof(LoopSIMD<T,S,A>),alignof(std::max_align_t)) == 0);
    }

    // broadcast constructor initializing the content with a given value
    LoopSIMD(Simd::Scalar<T> i) : LoopSIMD() {
      this->fill(i);
    }

    template<std::size_t OA>
      explicit LoopSIMD(const LoopSIMD<T,S,OA>& other)
      : std::array<T,S>(other)
    {
      assert(reinterpret_cast<uintptr_t>(this) % std::min(alignof(LoopSIMD<T,S,A>),alignof(std::max_align_t)) == 0);
    }

    /*
     *  Definition of basic operators
     */

    //Prefix operators
#define DUNE_SIMD_LOOP_PREFIX_OP(SYMBOL)         \
    auto operator SYMBOL() {                     \
      DUNE_PRAGMA_OMP_SIMD                       \
      for(std::size_t i=0; i<S; i++){            \
        SYMBOL(*this)[i];                        \
      }                                          \
      return *this;                              \
    }                                            \
    static_assert(true, "expecting ;")

    DUNE_SIMD_LOOP_PREFIX_OP(++);
    DUNE_SIMD_LOOP_PREFIX_OP(--);
#undef DUNE_SIMD_LOOP_PREFIX_OP

    //Unary operators
#define DUNE_SIMD_LOOP_UNARY_OP(SYMBOL)          \
    auto operator SYMBOL() const {               \
      LoopSIMD<T,S,A> out;                        \
      DUNE_PRAGMA_OMP_SIMD                       \
      for(std::size_t i=0; i<S; i++){            \
        out[i] = SYMBOL((*this)[i]);             \
      }                                          \
      return out;                                \
    }                                            \
    static_assert(true, "expecting ;")

    DUNE_SIMD_LOOP_UNARY_OP(+);
    DUNE_SIMD_LOOP_UNARY_OP(-);
    DUNE_SIMD_LOOP_UNARY_OP(~);

    auto operator!() const {
      Simd::Mask<LoopSIMD<T,S,A>> out;
      DUNE_PRAGMA_OMP_SIMD
      for(std::size_t i=0; i<S; i++){
        out[i] = !((*this)[i]);
      }
      return out;
    }
#undef DUNE_SIMD_LOOP_UNARY_OP

    //Postfix operators
#define DUNE_SIMD_LOOP_POSTFIX_OP(SYMBOL)        \
    auto operator SYMBOL(int){                   \
      LoopSIMD<T,S,A> out = *this;               \
      SYMBOL(*this);                             \
      return out;                                \
    }                                            \
    static_assert(true, "expecting ;")

   DUNE_SIMD_LOOP_POSTFIX_OP(++);
   DUNE_SIMD_LOOP_POSTFIX_OP(--);
#undef DUNE_SIMD_LOOP_POSTFIX_OP

    //Assignment operators
#define DUNE_SIMD_LOOP_ASSIGNMENT_OP(SYMBOL)              \
    auto operator SYMBOL(const Simd::Scalar<T> s) {               \
      DUNE_PRAGMA_OMP_SIMD                                \
      for(std::size_t i=0; i<S; i++){                     \
        (*this)[i] SYMBOL s;                              \
      }                                                   \
      return *this;                                       \
    }                                                     \
                                                          \
    auto operator SYMBOL(const LoopSIMD<T,S,A> &v) {      \
      DUNE_PRAGMA_OMP_SIMD                                \
      for(std::size_t i=0; i<S; i++){                     \
        (*this)[i] SYMBOL v[i];                           \
      }                                                   \
      return *this;                                       \
    }                                                     \
    static_assert(true, "expecting ;")

    DUNE_SIMD_LOOP_ASSIGNMENT_OP(+=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(-=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(*=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(/=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(%=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(<<=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(>>=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(&=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(|=);
    DUNE_SIMD_LOOP_ASSIGNMENT_OP(^=);
#undef DUNE_SIMD_LOOP_ASSIGNMENT_OP
  };

  //Arithmetic operators
#define DUNE_SIMD_LOOP_BINARY_OP(SYMBOL)                        \
  template<class T, std::size_t S, std::size_t A>                                \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v, const Simd::Scalar<T> s) { \
    LoopSIMD<T,S,A> out;                                                 \
    DUNE_PRAGMA_OMP_SIMD                                        \
    for(std::size_t i=0; i<S; i++){                             \
      out[i] = v[i] SYMBOL s;                                   \
    }                                                           \
    return out;                                                 \
  }                                                             \
  template<class T, std::size_t S, std::size_t A>                              \
  auto operator SYMBOL(const Simd::Scalar<T> s, const LoopSIMD<T,S,A> &v) { \
    LoopSIMD<T,S,A> out;                                                 \
    DUNE_PRAGMA_OMP_SIMD                                        \
    for(std::size_t i=0; i<S; i++){                             \
      out[i] = s SYMBOL v[i];                                   \
    }                                                           \
    return out;                                                 \
  }                                                             \
  template<class T, std::size_t S, std::size_t A>                              \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v,                         \
                       const LoopSIMD<T,S,A> &w) {                       \
    LoopSIMD<T,S,A> out;                                                 \
    DUNE_PRAGMA_OMP_SIMD                                        \
    for(std::size_t i=0; i<S; i++){                             \
      out[i] = v[i] SYMBOL w[i];                                \
    }                                                           \
    return out;                                                 \
  }                                                             \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_BINARY_OP(+);
  DUNE_SIMD_LOOP_BINARY_OP(-);
  DUNE_SIMD_LOOP_BINARY_OP(*);
  DUNE_SIMD_LOOP_BINARY_OP(/);
  DUNE_SIMD_LOOP_BINARY_OP(%);

  DUNE_SIMD_LOOP_BINARY_OP(&);
  DUNE_SIMD_LOOP_BINARY_OP(|);
  DUNE_SIMD_LOOP_BINARY_OP(^);

#undef DUNE_SIMD_LOOP_BINARY_OP

  //Bitshift operators
#define DUNE_SIMD_LOOP_BITSHIFT_OP(SYMBOL)                        \
  template<class T, std::size_t S, std::size_t A, class U>                       \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v, const U s) {            \
    LoopSIMD<T,S,A> out;                                                 \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = v[i] SYMBOL s;                                     \
    }                                                             \
    return out;                                                   \
  }                                                               \
  template<class T, std::size_t S, std::size_t A, class U, std::size_t AU>       \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v,                         \
                       const LoopSIMD<U,S,AU> &w) {                       \
    LoopSIMD<T,S,A> out;                                                 \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = v[i] SYMBOL w[i];                                  \
    }                                                             \
    return out;                                                   \
  }                                                               \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_BITSHIFT_OP(<<);
  DUNE_SIMD_LOOP_BITSHIFT_OP(>>);

#undef DUNE_SIMD_LOOP_BITSHIFT_OP

  //Comparison operators
#define DUNE_SIMD_LOOP_COMPARISON_OP(SYMBOL)                      \
  template<class T, std::size_t S, std::size_t A, class U>                       \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v, const U s) {            \
    Simd::Mask<LoopSIMD<T,S,A>> out;                                     \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = v[i] SYMBOL s;                                     \
    }                                                             \
    return out;                                                   \
  }                                                               \
  template<class T, std::size_t S, std::size_t A>                                \
  auto operator SYMBOL(const Simd::Scalar<T> s, const LoopSIMD<T,S,A> &v) { \
    Simd::Mask<LoopSIMD<T,S,A>> out;                                     \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = s SYMBOL v[i];                                     \
    }                                                             \
    return out;                                                   \
  }                                                               \
  template<class T, std::size_t S, std::size_t A>                                \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v,                         \
                       const LoopSIMD<T,S,A> &w) {                       \
    Simd::Mask<LoopSIMD<T,S,A>> out;                                     \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = v[i] SYMBOL w[i];                                  \
    }                                                             \
    return out;                                                   \
  }                                                               \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_COMPARISON_OP(<);
  DUNE_SIMD_LOOP_COMPARISON_OP(>);
  DUNE_SIMD_LOOP_COMPARISON_OP(<=);
  DUNE_SIMD_LOOP_COMPARISON_OP(>=);
  DUNE_SIMD_LOOP_COMPARISON_OP(==);
  DUNE_SIMD_LOOP_COMPARISON_OP(!=);
#undef DUNE_SIMD_LOOP_COMPARISON_OP

  //Boolean operators
#define DUNE_SIMD_LOOP_BOOLEAN_OP(SYMBOL)                         \
  template<class T, std::size_t S, std::size_t A>                                \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v, const Simd::Scalar<T> s) { \
    Simd::Mask<LoopSIMD<T,S,A>> out;                                     \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = v[i] SYMBOL s;                                     \
    }                                                             \
    return out;                                                   \
  }                                                               \
  template<class T, std::size_t S, std::size_t A>                                \
  auto operator SYMBOL(const Simd::Mask<T> s, const LoopSIMD<T,S,A> &v) { \
    Simd::Mask<LoopSIMD<T,S,A>> out;                                     \
    DUNE_PRAGMA_OMP_SIMD                                          \
    for(std::size_t i=0; i<S; i++){                               \
      out[i] = s SYMBOL v[i];                                     \
    }                                                             \
    return out;                                                   \
  }                                                               \
  template<class T, std::size_t S, std::size_t A>                                \
  auto operator SYMBOL(const LoopSIMD<T,S,A> &v,                         \
                       const LoopSIMD<T,S,A> &w) {                       \
    Simd::Mask<LoopSIMD<T,S,A>> out;                                     \
    DUNE_PRAGMA_OMP_SIMD                                          \
      for(std::size_t i=0; i<S; i++){                             \
        out[i] = v[i] SYMBOL w[i];                                \
      }                                                           \
    return out;                                                   \
  }                                                               \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_BOOLEAN_OP(&&);
  DUNE_SIMD_LOOP_BOOLEAN_OP(||);
#undef DUNE_SIMD_LOOP_BOOLEAN_OP

  //prints a given LoopSIMD
  template<class T, std::size_t S, std::size_t A>
  std::ostream& operator<< (std::ostream &os, const LoopSIMD<T,S,A> &v) {
    os << "[";
    for(std::size_t i=0; i<S-1; i++) {
      os << v[i] << ", ";
    }
    os << v[S-1] << "]";
    return os;
  }

  namespace Simd {
    namespace Overloads {
      /*
       *  Implementation/Overloads of the functions needed for
       *  SIMD-interface-compatibility
       */

      //Implementation of SIMD-interface-types
      template<class T, std::size_t S, std::size_t A>
      struct ScalarType<LoopSIMD<T,S,A>> {
        using type = Simd::Scalar<T>;
      };

      template<class U, class T, std::size_t S, std::size_t A>
      struct RebindType<U, LoopSIMD<T,S,A>> {
        using type =  LoopSIMD<Simd::Rebind<U, T>,S,A>;
      };

      //Implementation of SIMD-interface-functionality
      template<class T, std::size_t S, std::size_t A>
      struct LaneCount<LoopSIMD<T,S,A>> : index_constant<S*lanes<T>()> {};

      template<class T, std::size_t S, std::size_t A>
      auto lane(ADLTag<5>, std::size_t l, LoopSIMD<T,S,A> &&v)
        -> decltype(std::move(Simd::lane(l%lanes<T>(), v[l/lanes<T>()])))
      {
        return std::move(Simd::lane(l%lanes<T>(), v[l/lanes<T>()]));
      }

      template<class T, std::size_t S, std::size_t A>
      auto lane(ADLTag<5>, std::size_t l, const LoopSIMD<T,S,A> &v)
        -> decltype(Simd::lane(l%lanes<T>(), v[l/lanes<T>()]))
      {
        return Simd::lane(l%lanes<T>(), v[l/lanes<T>()]);
      }

      template<class T, std::size_t S, std::size_t A>
      auto lane(ADLTag<5>, std::size_t l, LoopSIMD<T,S,A> &v)
        -> decltype(Simd::lane(l%lanes<T>(), v[l/lanes<T>()]))
      {
        return Simd::lane(l%lanes<T>(), v[l/lanes<T>()]);
      }

      template<class T, std::size_t S, std::size_t AM, std::size_t AD>
      auto cond(ADLTag<5>, Simd::Mask<LoopSIMD<T,S,AM>> mask,
                LoopSIMD<T,S,AD> ifTrue, LoopSIMD<T,S,AD> ifFalse) {
        LoopSIMD<T,S,AD> out;
        for(std::size_t i=0; i<S; i++) {
          out[i] = Simd::cond(mask[i], ifTrue[i], ifFalse[i]);
        }
        return out;
      }

      template<class M, class T, std::size_t S, std::size_t A>
      auto cond(ADLTag<5, std::is_same<bool, Simd::Scalar<M> >::value
                && Simd::lanes<M>() == Simd::lanes<LoopSIMD<T,S,A> >()>,
                M mask, LoopSIMD<T,S,A> ifTrue, LoopSIMD<T,S,A> ifFalse)
      {
        LoopSIMD<T,S,A> out;
        for(auto l : range(Simd::lanes(mask)))
          Simd::lane(l, out) = Simd::lane(l, mask) ? Simd::lane(l, ifTrue) : Simd::lane(l, ifFalse);
        return out;
      }

      template<class M, std::size_t S, std::size_t A>
      bool anyTrue(ADLTag<5>, LoopSIMD<M,S,A> mask) {
        bool out = false;
        for(std::size_t i=0; i<S; i++) {
          out |= Simd::anyTrue(mask[i]);
        }
        return out;
      }

      template<class M, std::size_t S, std::size_t A>
      bool allTrue(ADLTag<5>, LoopSIMD<M,S,A> mask) {
        bool out = true;
        for(std::size_t i=0; i<S; i++) {
          out &= Simd::allTrue(mask[i]);
        }
        return out;
      }

      template<class M, std::size_t S, std::size_t A>
      bool anyFalse(ADLTag<5>, LoopSIMD<M,S,A> mask) {
        bool out = false;
        for(std::size_t i=0; i<S; i++) {
          out |= Simd::anyFalse(mask[i]);
        }
        return out;
      }

      template<class M, std::size_t S, std::size_t A>
      bool allFalse(ADLTag<5>, LoopSIMD<M,S,A> mask) {
        bool out = true;
        for(std::size_t i=0; i<S; i++) {
          out &= Simd::allFalse(mask[i]);
        }
        return out;
      }
    }  //namespace Overloads

  }  //namespace Simd


  /*
   *  Overloads the unary cmath-operations. Operations requiring
   *  or returning more than one argument are not supported.
   *  Due to inconsistency with the return values, cmath-operations
   *  on integral types are also not supported-
   */

#define DUNE_SIMD_LOOP_CMATH_UNARY_OP(expr)                          \
  template<class T, std::size_t S, std::size_t A, typename Sfinae =                 \
           typename std::enable_if_t<!std::is_integral<Simd::Scalar<T>>::value> > \
  auto expr(const LoopSIMD<T,S,A> &v) {                                  \
    using std::expr;                                                 \
    LoopSIMD<T,S,A> out;                                              \
    for(std::size_t i=0; i<S; i++) {                                 \
      out[i] = expr(v[i]);                                           \
    }                                                                \
    return out;                                                      \
  }                                                                  \
  static_assert(true, "expecting ;")

#define DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN(expr, returnType)  \
  template<class T, std::size_t S, std::size_t A, typename Sfinae =                 \
           typename std::enable_if_t<!std::is_integral<Simd::Scalar<T>>::value> > \
  auto expr(const LoopSIMD<T,S,A> &v) {                                  \
    using std::expr;                                                 \
    LoopSIMD<returnType,S> out;                                      \
    for(std::size_t i=0; i<S; i++) {                                 \
      out[i] = expr(v[i]);                                           \
    }                                                                \
    return out;                                                      \
  }                                                                  \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_CMATH_UNARY_OP(cos);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(sin);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(tan);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(acos);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(asin);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(atan);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(cosh);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(sinh);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(tanh);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(acosh);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(asinh);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(atanh);

  DUNE_SIMD_LOOP_CMATH_UNARY_OP(exp);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(log);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(log10);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(exp2);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(expm1);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN(ilogb, int);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(log1p);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(log2);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(logb);

  DUNE_SIMD_LOOP_CMATH_UNARY_OP(sqrt);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(cbrt);

  DUNE_SIMD_LOOP_CMATH_UNARY_OP(erf);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(erfc);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(tgamma);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(lgamma);

  DUNE_SIMD_LOOP_CMATH_UNARY_OP(ceil);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(floor);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(trunc);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(round);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN(lround, long);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN(llround, long long);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(rint);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN(lrint, long);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN(llrint, long long);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(nearbyint);

  DUNE_SIMD_LOOP_CMATH_UNARY_OP(fabs);
  DUNE_SIMD_LOOP_CMATH_UNARY_OP(abs);

#undef DUNE_SIMD_LOOP_CMATH_UNARY_OP
#undef DUNE_SIMD_LOOP_CMATH_UNARY_OP_WITH_RETURN


  /*  not implemented cmath-functions:
   *  atan2
   *  frexp, idexp
   *  modf
   *  scalbn, scalbln
   *  pow
   *  hypot
   *  remainder, remquo
   *  copysign
   *  nan
   *  nextafter, nexttoward
   *  fdim, fmax, fmin
   */

  /*
   * Overloads specific functions usually provided by the std library
   * More overloads will be provided should the need arise.
   */

#define DUNE_SIMD_LOOP_STD_UNARY_OP(expr)                  \
  template<class T, std::size_t S, std::size_t A>          \
  auto expr(const LoopSIMD<T,S,A> &v) {                     \
    using std::expr;                        \
    LoopSIMD<T,S,A> out;                     \
    for(std::size_t i=0; i<S; i++) {        \
      out[i] = expr(v[i]);                  \
    }                                       \
    return out;                             \
  }                                         \
                                                    \
  template<class T, std::size_t S, std::size_t A>                  \
  auto expr(const LoopSIMD<std::complex<T>,S,A> &v) {               \
    using std::expr;                                \
    LoopSIMD<T,S,A> out;                             \
    for(std::size_t i=0; i<S; i++) {                \
      out[i] = expr(v[i]);                          \
    }                                               \
    return out;                                     \
  }                                                 \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_STD_UNARY_OP(real);
  DUNE_SIMD_LOOP_STD_UNARY_OP(imag);

#undef DUNE_SIMD_LOOP_STD_UNARY_OP

#define DUNE_SIMD_LOOP_STD_BINARY_OP(expr)                    \
  template<class T, std::size_t S, std::size_t A>                           \
  auto expr(const LoopSIMD<T,S,A> &v, const LoopSIMD<T,S,A> &w) {        \
    using std::expr;                                          \
    LoopSIMD<T,S,A> out;                                       \
    for(std::size_t i=0; i<S; i++) {                          \
      out[i] = expr(v[i],w[i]);                               \
    }                                                         \
    return out;                                               \
  }                                                           \
  static_assert(true, "expecting ;")

  DUNE_SIMD_LOOP_STD_BINARY_OP(max);
  DUNE_SIMD_LOOP_STD_BINARY_OP(min);

#undef DUNE_SIMD_LOOP_STD_BINARY_OP

  namespace MathOverloads {
    template<class T, std::size_t S, std::size_t A>
    auto isNaN(const LoopSIMD<T,S,A> &v, PriorityTag<3>, ADLTag) {
      Simd::Mask<LoopSIMD<T,S,A>> out;
      for(auto l : range(S))
        out[l] = Dune::isNaN(v[l]);
      return out;
    }

    template<class T, std::size_t S, std::size_t A>
    auto isInf(const LoopSIMD<T,S,A> &v, PriorityTag<3>, ADLTag) {
      Simd::Mask<LoopSIMD<T,S,A>> out;
      for(auto l : range(S))
        out[l] = Dune::isInf(v[l]);
      return out;
    }

    template<class T, std::size_t S, std::size_t A>
    auto isFinite(const LoopSIMD<T,S,A> &v, PriorityTag<3>, ADLTag) {
      Simd::Mask<LoopSIMD<T,S,A>> out;
      for(auto l : range(S))
        out[l] = Dune::isFinite(v[l]);
      return out;
    }
  } //namespace MathOverloads

  template<class T, std::size_t S, std::size_t A>
  struct IsNumber<LoopSIMD<T,S,A>> :
          public std::integral_constant<bool, IsNumber<T>::value>{
  };

#ifdef CLANG_WARNING_DISABLED
#  pragma clang diagnostic pop
#  undef CLANG_WARNING_DISABLED
#endif

#ifdef GCC_WARNING_DISABLED
#  pragma GCC diagnostic pop
#  undef GCC_WARNING_DISABLED
#endif

} //namespace Dune

#endif