My question is similar to Can one unroll a loop when working with an integer template parameter? but I want to mix compile time and runtime. Specifically, I know at compile time a constant NBLOCK
and I want to write a switch on a variable start_block
which is only known at runtime where NBLOCK
is the number of entries in the switch. Here is what I got using macros:
#define CASE_UNROLL(i_loop) \
case i_loop : \
dst.blocks[i_loop+1] -= (load_unaligned_epi8(srcblock) != zero) & block1; \
srcblock += sizeof(*srcblock);
switch(start_block)
{
CASE_UNROLL(0);
#if NBLOCKS > 2
CASE_UNROLL(1);
#endif
#if NBLOCKS > 3
CASE_UNROLL(2);
#endif
#if NBLOCKS > 4
CASE_UNROLL(3);
#endif
...
...
#if NBLOCKS > 15
CASE_UNROLL(14);
#endif
#if NBLOCKS > 16
#error "Too many blocks"
#endif
}
I find it very ugly. Especially if I want to raise the bound from 16 to 32.
I would like to know if it is possible to write that using some template meta programming. The hard part is that for performance reasons it is crucial that the switch is compiled with a jump table than a sequence of nested conditional.
Note that the question is very similar to C++/C++11 - Switch statement for variadic templates? but as far as I understand the solution proposed here is to remove the switch by using a mixed compile/tun time initialized functions pointer array. I can't pay the prince a calling a function here.
I'm working with GCC if some nasty extensions is needed.