#include <cstdint>enum bytecode : std::size_t{
ADD_1,
ADD_2,
ADD_3,
ADD_5,
ADD_8,
ADD_13,
ADD_21,
MUL_3,
HALT,
};double func(const bytecode* instructions){double value{0};conststaticvoid* jumptable[]={&&add_1,
&&add_2,
&&add_3,
&&add_5,
&&add_8,
&&add_13,
&&add_21,
&&mul_3,
&&halt
};constauto next =[&](){return jumptable[*instructions++];};goto* next();
add_1:
value +=1;goto* next();
add_2:
value +=2;goto* next();
add_3:
value +=3;goto* next();
add_5:
value +=5;goto* next();
add_8:
value +=8;goto* next();
add_13:
value +=13;goto* next();
add_21:
value +=21;goto* next();
mul_3:
value *=3;goto* next();
halt:return value;}// int func_loop(const bytecode* instructions)// {// for (auto value = 0;;)// {// switch (*instructions++)// {// case ADD_1: value += 1; break;// case ADD_2: value += 2; break;// case ADD_3: value += 3; break;// case ADD_5: value += 5; break;// case ADD_8: value += 8; break;// case ADD_13: value += 13; break;// case ADD_21: value += 21; break;// case MUL_3: value *= 3; break;// case HALT: return value;// }// }// }
cpp/goto_jumptable.txt · Last modified: by allspark