#include "testcache.h"

/* This is a very very basic test of cache behaviour */

/*
 * We'll use a big array, say NELEMENTS in size
 * The goal is to add up the values in that array, but we'll
 * use a double loop and a STRIDE. The outer loop increments
 * the start element, the inner loop strides through the array
 */

/*
 * Fill the array with values, but in a possibly cache-unfriendly
 * manner. The array should contain at least "size" elements.
 * The function returns a long so as to ensure that it has the
 * same type as the sumArray function below.
 */
long fillArray(long *array, int size, int stride)
{
    int start;
    int i = 0;

    for (start = 0; start < stride; start++)
    {
        for (i = start; i < size; i += stride)
        {
            array[i] = i;
        }
    }

    return (long) i;
}

/*
 * Now sum the values in the array, but in a possibly cache-unfriendly
 * manner. The array should contain at least "size" elements.
 * The value sum is returned to avoid the possibility that the compiler
 * deletes essentially all of the code in this routine, as it produces
 * no eventual output.
 */
long sumArray(long *array, int size, int stride)
{
    int start;
    int i;
    long sum = 0;

    for( start = 0; start < stride; start++ )
    {
        for( i = start; i < size; i += stride )
        {
            sum += array[i];
        }
    }

    return sum;
}
