Am 17.09.2022 um 21:03 schrieb Chris M. Thomasson:
> Iirc Boost preprocessor is based on the Chaos preprocessor codebase,
> right? Anyway, check out this loop unrolling in C for a HMAC lib:
>
https://github.com/ogay/hmac/blob/master/sha2.c
> Macros indeed. Search the code for UNROLL_LOOPS :^)
template<size_t N, typename Fn>
requires (N >= 1) && requires( Fn fn ) { { fn.template operator
()<(size_t)N - 1>() } -> std::same_as<void>; }
inline
void unroll( Fn fn )
{
auto unroll_n = [&]<size_t ... Indices>( std::index_sequence<Indices ...> )
{
(fn.template operator ()<Indices>(), ...);
};
unroll_n( std::make_index_sequence<N>() );
}
template<size_t N, bool Unroll, typename Fn>
requires (N >= 1) && requires( Fn fn ) { { fn() } -> std::same_as<void>; }
inline
void unroll( Fn fn )
{
if constexpr( Unroll )
{
auto unroll_n = [&]<size_t ... Indices>( std::index_sequence<Indices
..> )
{
return ((Indices, fn()), ...);
};
unroll_n( std::make_index_sequence<N>() );
}
else
for( size_t i = 0; i != N; fn(), ++i );
}
template<size_t N, bool Unroll, typename Fn>
requires (N >= 1) && requires( Fn fn, size_t i ) { { fn( i ) } ->
std::convertible_to<bool>; }
inline
bool unroll( Fn fn )
{
if constexpr( Unroll )
{
auto unroll_n = [&]<size_t ... Indices>( std::index_sequence<Indices
..> ) -> bool
{
return (fn( Indices ) && ...);
};
return unroll_n( std::make_index_sequence<N>() );
}
else
{
for( size_t i = 0; i != N; )
if( !fn( i++ ) )
return false;
return true;
}
}
template<size_t N, typename Fn>
requires (N >= 1) && requires( Fn fn ) { { fn.template operator
()<(size_t)N - 1>() } -> std::convertible_to<bool>; }
inline
bool unroll( Fn fn )
{
auto unroll_n = [&]<size_t ... Indices>( std::index_sequence<Indices
..> ) -> bool
{
return (fn.template operator ()<Indices>() && ...);
};
return unroll_n( std::make_index_sequence<N>() );
}
template<size_t N, bool Unroll, typename Fn>
requires (N >= 1) && requires( Fn fn ) { { fn() } ->
std::convertible_to<bool>; }
inline
bool unroll( Fn fn )
{
if constexpr( Unroll )
{
auto unroll_n = [&]<size_t ... Indices>( std::index_sequence<Indices
..> ) -> bool
{
return ((Indices, fn()) && ...);
};
return unroll_n( std::make_index_sequence<N>() );
}
else
{
for( size_t i = 0; i != N; ++i )
if( !fn() )
return false;
return true;
}
}
template<std::size_t N, typename RandomIt, typename UnaryFunction>
requires std::random_access_iterator<RandomIt>
&& requires( UnaryFunction fn, std::iter_value_t<RandomIt> elem ) { {
fn( elem ) }; }
inline
RandomIt unroll_for_each( RandomIt begin, RandomIt end, UnaryFunction fn )
{
RandomIt &it = begin;
if constexpr( N > 1 )
for( ; it + N <= end; it += N )
unroll<N>( [&]<std::size_t I>() { fn( it[I] ); } );
for( ; it < end; ++it )
fn( *begin );
return it;
}