What I'm trying to do is take this code:
char naive_smooth_descr[] = "naive_smooth: Naive baseline implementation";
void naive_smooth(int dim, pixel *src, pixel *dst)
{
int i, j;
for (i = 0; i < dim; i++)
for (j = 0; j < dim; j++)
dst[RIDX(i, j, dim)] = avg(dim, i, j, src);
}
and replace the function call avg(dim, i, j, src);
with the actual code at the very bottom of the page. Then, take that code and replace all the function calls in that code with the the actual code, etc.
If you're asking why do all this, the reason is simple: when you get rid of function calls the program runs faster, and I'm trying to attain the fastest cycles per element when the above code runs by getting rid of all the function calls and replacing it with the actual code.
Now I'm really just having a lot of trouble doing this. Do I take the code with the brackets and then just copy and paste? Do I leave out the brackets? Do I include the beginning of the code, for example, static pixel avg(int dim, int i, int j, pixel *src)
and then the brackets and then the code to replace the function call?
I am going to paste all the code here:
/* A struct used to compute averaged pixel value */
typedef struct {
int red;
int green;
int blue;
int num;
} pixel_sum;
/* Compute min and max of two integers, respectively */
static int min(int a, int b) { return (a < b ? a : b); }
static int max(int a, int b) { return (a > b ? a : b); }
/*
* initialize_ pixel_ sum - Initializes all fields of sum to 0
*/
static void initialize_ pixel_ sum (pixel_sum *sum)
{
sum->red = sum->green = sum->blue = 0;
sum->num = 0;
return;
}
/*
* accumulate_sum - Accumulates field values of p in corresponding
* fields of sum
*/
static void accumulate_ sum (pixel_sum *sum, pixel p)
{
sum->red += (int) p.red;
sum->green += (int) p.green;
sum->blue += (int) p.blue;
sum->num++;
return;
}
/*
* assign_ sum_ to_ pixel - Computes averaged pixel value in current_pixel
*/
static void assign_ sum_ to_ pixel (pixel *current_ pixel, pixel_ sum sum)
{
current_pixel->red = (unsigned short) (sum.red/sum.num);
current_pixel->green = (unsigned short) (sum.green/sum.num);
current_pixel->blue = (unsigned short) (sum.blue/sum.num);
return;
}
/*
* avg - Returns averaged pixel value at (i,j)
*/
This is the code that I want to replace the function call avg(dim, i, j, src);
with:
static pixel avg (int dim, int i, int j, pixel *src)
{
int ii, jj;
pixel_sum sum;
pixel current_pixel;
initialize_pixel_sum(&sum);
for(ii = max(i-1, 0); ii <= min(i+1, dim-1); ii++)
for(jj = max(j-1, 0); jj <= min(j+1, dim-1); jj++)
accumulate_sum(&sum, src[RIDX(ii, jj, dim)]);
assign_sum_to_pixel(¤t_pixel, sum);
return current_pixel;
}