Unroll.h 843 B

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. #include <c10/macros/Macros.h>
  3. #include <type_traits>
  4. // Utility to guarantee 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. // Example: c10::ForcedUnroll<4>{}(f); is equivalent to f(0); f(1); f(2); f(3);
  8. namespace c10 {
  9. template <int n>
  10. struct ForcedUnroll {
  11. template <typename Func, typename... Args>
  12. C10_ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
  13. ForcedUnroll<n - 1>{}(f, args...);
  14. f(std::integral_constant<int, n - 1>{}, args...);
  15. }
  16. };
  17. template <>
  18. struct ForcedUnroll<1> {
  19. template <typename Func, typename... Args>
  20. C10_ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
  21. f(std::integral_constant<int, 0>{}, args...);
  22. }
  23. };
  24. } // namespace c10