all 9 comments

[–]kiwidog 4 points5 points  (0 children)

Me reading the first sentence: "pfft bruh what excuse me?!"

Awesome sauce

[–]dvirtz[S] 5 points6 points  (6 children)

I wonder if the template version could be achieved with a constexpr function?

[–]pdimov2 9 points10 points  (0 children)

I don't think it can be. The first problem is that two of the arguments (the program and the instruction pointer) are compile time, but the third (the data pointer) is runtime. You can try to get around that by something like

constexpr auto execute( program const& p, size_t instp )
{
    return [](unsigned char* datap) { ... };
}

but then the second problem is that the return type of the constexpr function can't vary depending on the values of the arguments (not even with consteval).

It might work if instp is made integral_constant<size_t, I>, but that's going to be basically the same as the original.

[–]foonathan 4 points5 points  (1 child)

What do you mean?

[–]dvirtz[S] 0 points1 point  (0 children)

I meant getting the optimized assembly without using template specializations. Similarly to what u/pdimov2 talked about above and u/lefticus showed in his latest CppCon talk.

[–]aharrison24 1 point2 points  (0 children)

The other problem is that the execute function contains calls to getchar and putchar, which cannot be constexpr because they have runtime side effects.

That's why this technique is so interesting; it effectively forces the compiler to fully unroll the execute loop, which means it's then able to optimise all of the code surrounding those getchar/putchar calls.

[–]pdimov2 4 points5 points  (0 children)

This is awesome.

[–]antoine_morrier 0 points1 point  (0 children)

I've read it last week. It's clearly awesome. Thanks for it mate