#include <stdio.h>
#include "ImC/imc_kernel.h"

#define INPUT_SIZE 20
#define KERNEL_SIZE 6
#define OUTPUT_SIZE (INPUT_SIZE - KERNEL_SIZE + 1)

static int input[INPUT_SIZE][INPUT_SIZE] = {
    [0 ... INPUT_SIZE - 1] = {[0 ... INPUT_SIZE - 1] = 2}};

static int kernel[KERNEL_SIZE][KERNEL_SIZE] = {
    [0 ... KERNEL_SIZE - 1] = {[0 ... KERNEL_SIZE - 1] = 2}};

void vConv2d()
{
    int IMC_REPEAT = 10;

    int output[OUTPUT_SIZE * OUTPUT_SIZE];

#pragma clang loop unroll(disable)
    for (int imc_repeat = 0; imc_repeat < IMC_REPEAT; imc_repeat++)
    {

// #pragma clang loop unroll(disable)
// for(int i=0; i<OUTPUT_SIZE; i++) {
//     #pragma clang loop unroll(disable)
//     for(int j=0; j<OUTPUT_SIZE; j++) {
#pragma clang loop unroll(disable)
        for (int _i = 0; _i < OUTPUT_SIZE * OUTPUT_SIZE; _i++)
        {
            int i = _i / OUTPUT_SIZE;
            int j = _i % OUTPUT_SIZE;
            int sum = 0;
            // #pragma clang loop unroll(disable)
            for (int ii = 0; ii < KERNEL_SIZE; ii++)
            {
                // #pragma clang loop unroll(disable)
                for (int jj = 0; jj < KERNEL_SIZE; jj++)
                {
                    int input_i = i + ii;
                    int input_j = j + jj;
                    sum += input[input_i][input_j] * kernel[ii][jj];
                }
            }
            // output[i * OUTPUT_SIZE + j] = sum;
            output[_i] = sum;
        }
        // }
    }

#pragma clang loop unroll(disable)
    for (int _i = 0; _i < OUTPUT_SIZE * OUTPUT_SIZE; _i++)
    {
        printf("%d ", output[_i]);
        if (_i % OUTPUT_SIZE == OUTPUT_SIZE - 1)
        {
            printf("\r\n");
        }
    }

    printf("\r\n");
}