Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Functional programming at its best

26 views
Skip to first unread message

Bonita Montero

unread,
Sep 20, 2022, 10:18:26 AM9/20/22
to
I experimented with loop unrolling in C++20 and I found that sometimes
its the best to to interleaved steps while unrolling. F.e. you load
multiple values into the registers in the first step, then process
these values interleaved and then have some interleaved write back.
This is usually somewhat awkward to program because you have code
parts which are replicated, usually differing only in some array
indices of the data you process.

So I extended my unrolling helper, here it is:

template<size_t N, typename ... Fns>
requires (N >= 1) && (requires( Fns fn ) { { fn.template operator
()<(size_t)N - 1>() } -> std::same_as<void>; } && ...)
inline void unroll( Fns ... fns )
{
auto unroll_n = [&]<typename Fn, size_t ... Indices>( Fn fn,
std::index_sequence<Indices ...> )
{
(fn.template operator ()<Indices>(), ...);
};
(unroll_n( fns, std::make_index_sequence<N>() ), ...);
}

This whole interleaving then might look like this:

unroll<3>(
[]<size_t I>() { cout << "step 1, interleave " << I << endl; },
[]<size_t I>() { cout << "step 2, interleave " << I << endl; },
[]<size_t I>() { cout << "step 3, interleave " << I << endl; } );


Doesn't look good but the manual way is even worse.

Mut...@dastardlyhq.com

unread,
Sep 20, 2022, 11:41:57 AM9/20/22
to
On Tue, 20 Sep 2022 16:18:25 +0200
Bonita Montero <Bonita....@gmail.com> wrote:
>I experimented with loop unrolling in C++20 and I found that sometimes
>its the best to to interleaved steps while unrolling. F.e. you load
>multiple values into the registers in the first step, then process
>these values interleaved and then have some interleaved write back.
>This is usually somewhat awkward to program because you have code
>parts which are replicated, usually differing only in some array
>indices of the data you process.
>
>So I extended my unrolling helper, here it is:
>
>template<size_t N, typename ... Fns>
> requires (N >= 1) && (requires( Fns fn ) { { fn.template operator
>()<(size_t)N - 1>() } -> std::same_as<void>; } && ...)

Larry Wall must be starting to get envious of C++ syntax these days.


Bonita Montero

unread,
Sep 20, 2022, 12:37:09 PM9/20/22
to
That's all a matter of whether you've learned this or not.
If you learned it it's not hard to read.
And I don't know what you do in a C++ group, criticizing C++.


Mut...@dastardlyhq.com

unread,
Sep 21, 2022, 3:09:05 AM9/21/22
to
On Tue, 20 Sep 2022 18:37:14 +0200
Occasionally I learn something new. Unlike you however I'm not blind to C++'s
faults and as a paid C++ developer the mess that is being made of the language
annoys me.

Bonita Montero

unread,
Sep 21, 2022, 3:50:58 AM9/21/22
to
You _are_ blind with the faults since there's no fault in my code
but you just don't know what's going on.

Mut...@dastardlyhq.com

unread,
Sep 21, 2022, 4:48:04 AM9/21/22
to
On Wed, 21 Sep 2022 09:51:03 +0200
Oh dear.

0 new messages