Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86403

Re: What is the good of new way of coding?

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++
Subject Re: What is the good of new way of coding?
Date 2022-09-18 16:21 +0200
Organization A noiseless patient Spider
Message-ID <tg79gd$ilvm$1@dont-email.me> (permalink)
References (5 earlier) <tg4r45$6q5f$1@dont-email.me> <tg4rnu$aeq$1@gioia.aioe.org> <tg4sug$6vcn$1@dont-email.me> <tg51mc$7cfq$1@dont-email.me> <tg55ll$7nub$1@dont-email.me>

Show all headers | View raw


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;
}

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is the good of new way of coding? wij <wyniijj2@gmail.com> - 2022-09-17 05:46 -0700
  Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 15:03 +0200
    Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 14:50 +0000
      Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 17:31 +0200
        Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 15:59 +0000
          Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 18:03 +0200
            Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 16:13 +0000
              Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 18:34 +0200
                Re: What is the good of new way of coding? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-09-17 19:55 +0200
                Re: What is the good of new way of coding? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-09-17 12:03 -0700
                Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-18 16:21 +0200
                Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-19 08:30 +0000
                Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-19 10:37 +0200
                Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-19 08:54 +0000
                Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-19 11:13 +0200
  Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 15:23 +0200
    Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 15:38 +0200
  Re: What is the good of new way of coding? Andreas Kempe <kempe@lysator.liu.se> - 2022-09-17 14:37 +0000
    Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 16:40 +0200
      Re: What is the good of new way of coding? Andreas Kempe <kempe@lysator.liu.se> - 2022-09-17 20:52 +0000
        Re: What is the good of new way of coding? Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-09-17 22:18 +0100
          Re: What is the good of new way of coding? Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-09-17 22:21 +0100
            Re: What is the good of new way of coding? Andreas Kempe <kempe@lysator.liu.se> - 2022-09-17 21:31 +0000
  Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 14:52 +0000
    Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 17:32 +0200

csiph-web