Unroll.h 667 B

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