Skip to content

Commit

Permalink
port loop unrolling to C++14
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbachmann committed Dec 25, 2024
1 parent c95fe98 commit fcff9f2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
30 changes: 22 additions & 8 deletions extras/rapidfuzz_amalgamated.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// RapidFuzz v1.0.2
// Generated: 2024-12-25 01:37:33.201987
// Generated: 2024-12-25 01:42:39.581315
// ----------------------------------------------------------
// This file is an amalgamation of multiple different files.
// You probably shouldn't edit it directly.
Expand Down Expand Up @@ -1563,16 +1563,30 @@ static inline unsigned int countr_zero(uint8_t x)
return countr_zero(static_cast<uint32_t>(x));
}

template <class T, T... inds, class F>
constexpr void unroll_impl(std::integer_sequence<T, inds...>, F&& f)
{
(f(std::integral_constant<T, inds>{}), ...);
}
template <typename T, T N, T Pos = 0, bool IsEmpty = (N == 0)>
struct UnrollImpl;

template <typename T, T N, T Pos>
struct UnrollImpl<T, N, Pos, false> {
template <typename F>
static void call(F&& f)
{
f(Pos);
UnrollImpl<T, N - 1, Pos + 1>::call(std::forward<F>(f));
}
};

template <typename T, T N, T Pos>
struct UnrollImpl<T, N, Pos, true> {
template <typename F>
static void call(F&&)
{}
};

template <class T, T count, class F>
template <typename T, int N, class F>
constexpr void unroll(F&& f)
{
unroll_impl(std::make_integer_sequence<T, count>{}, std::forward<F>(f));
UnrollImpl<T, N>::call(f);
}

} // namespace detail
Expand Down
28 changes: 21 additions & 7 deletions rapidfuzz/details/intrinsics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,30 @@ static inline unsigned int countr_zero(uint8_t x)
return countr_zero(static_cast<uint32_t>(x));
}

template <class T, T... inds, class F>
constexpr void unroll_impl(std::integer_sequence<T, inds...>, F&& f)
{
(f(std::integral_constant<T, inds>{}), ...);
}
template <typename T, T N, T Pos = 0, bool IsEmpty = (N == 0)>
struct UnrollImpl;

template <typename T, T N, T Pos>
struct UnrollImpl<T, N, Pos, false> {
template <typename F>
static void call(F&& f)
{
f(Pos);
UnrollImpl<T, N - 1, Pos + 1>::call(std::forward<F>(f));
}
};

template <typename T, T N, T Pos>
struct UnrollImpl<T, N, Pos, true> {
template <typename F>
static void call(F&&)
{}
};

template <class T, T count, class F>
template <typename T, int N, class F>
constexpr void unroll(F&& f)
{
unroll_impl(std::make_integer_sequence<T, count>{}, std::forward<F>(f));
UnrollImpl<T, N>::call(f);
}

} // namespace detail
Expand Down

0 comments on commit fcff9f2

Please sign in to comment.