File: Unroll.h

package info (click to toggle)
nvidia-cuda-toolkit 12.4.1-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 18,505,836 kB
  • sloc: ansic: 203,477; cpp: 64,769; python: 34,699; javascript: 22,006; xml: 13,410; makefile: 3,085; sh: 2,343; perl: 352
file content (36 lines) | stat: -rw-r--r-- 826 bytes parent folder | download | duplicates (6)
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
#pragma once

namespace Unroll {

// Helper to force loop unrolling.  Replace this:
//
//    for (int i = 0; i < N; ++i)
//    {
//        WriteTo(&output[i]);
//    }
//
// with this:
//
//    For<0,N>::Do([&](int i)
//    {
//        WriteTo(&output[i]);
//    });
//
template <int From, int To> struct For
{
    // Take func by const& instead && because we need to support both
    // lvalues (like named functions) and rvalues (like lambdas), but
    // it makes no sense to move/forward rvalues since func is called
    // multiple times.
    template <typename Func> static void Do(Func const& func, int i = From)
    {
        func(i);
        For<From + 1, To>::Do(func, i + 1);
    }
};
template <int To> struct For<To, To>
{
    template <typename Func> static void Do(Func const&, int) {}
};

} // namespace Unroll