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
10namespace c10 {
11
12template <int n>
13struct 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
21template <>
22struct 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