Unroll loop with template meta-programming

Voici un exemple de template meta-programming. L’archive contient également un exemple de configure.in pouvant servir de base pour d’autres projets basés sur les autotools.

Le but: dérouler une boucle à la compilation en utilisant des templates.

Contraintes: plusieurs paramètres de templates.

// --- TEMPLATE: BEGIN --- //
template<int idx, unsigned int length>
struct apply_kernel {
    inline static unsigned int run(const unsigned char *  const buff) {
        int i = 100 + g_kernels[idx].coords[length][1] * 3 + g_kernels[idx].coords[length][0];
        unsigned int value = buff[i];
        return value + apply_kernel<idx, length-1>::run(buff);
    };
};
 
template<int idx>
struct apply_kernel<idx, 0> {
    inline static unsigned int run(const unsigned char * const buff) {
        int i = 100 + g_kernels[idx].coords[0][1] * 3 + g_kernels[idx].coords[0][0];
        return buff[i];
    };
};
 
typedef unsigned int (apply_t)(const unsigned char * const buff);
 
apply_t *fcts[KERNEL_MAX] = {
                                apply_kernel<0, 2-1>::run,
                                apply_kernel<1, 2-1>::run,
                                apply_kernel<2, 4-1>::run,
                                apply_kernel<3, 1-1>::run,
                                apply_kernel<4, 8-1>::run
                            };
 
// --- TEMPLATE: END --- //
Print/export