xref: /aosp_15_r20/external/executorch/kernels/optimized/utils/unroll.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <executorch/runtime/platform/compiler.h>
12 
13 // Utility to guaruntee complete unrolling of a loop where the bounds are known
14 // at compile time. Various pragmas achieve similar effects, but are not as
15 // portable across compilers.
16 
17 // Example: ForcedUnroll<4>{}(f); is equivalent to f(0); f(1); f(2); f(3);
18 
19 namespace executorch {
20 namespace utils {
21 
22 template <int n>
23 struct ForcedUnroll {
24   template <typename Func>
operatorForcedUnroll25   ET_INLINE void operator()(const Func& f) const {
26     ForcedUnroll<n - 1>{}(f);
27     f(n - 1);
28   }
29 };
30 
31 template <>
32 struct ForcedUnroll<1> {
33   template <typename Func>
34   ET_INLINE void operator()(const Func& f) const {
35     f(0);
36   }
37 };
38 
39 } // namespace utils
40 } // namespace executorch
41