1 | #pragma once |
2 | #include <c10/macros/Macros.h> |
3 | |
4 | // Utility to guaruntee complete unrolling of a loop where the bounds are known |
5 | // at compile time. Various pragmas achieve similar effects, but are not as |
6 | // portable across compilers. |
7 | |
8 | // Example: c10::ForcedUnroll<4>{}(f); is equivalent to f(0); f(1); f(2); f(3); |
9 | |
10 | namespace c10 { |
11 | |
12 | template <int n> |
13 | struct ForcedUnroll { |
14 | template <typename Func> |
15 | C10_ALWAYS_INLINE void operator()(const Func& f) const { |
16 | ForcedUnroll<n - 1>{}(f); |
17 | f(n - 1); |
18 | } |
19 | }; |
20 | |
21 | template <> |
22 | struct ForcedUnroll<1> { |
23 | template <typename Func> |
24 | C10_ALWAYS_INLINE void operator()(const Func& f) const { |
25 | f(0); |
26 | } |
27 | }; |
28 | |
29 | } // namespace c10 |
30 | |