0

I have the following code for a filter:

// Shift array to the left
memmove(&Fifo[0], &Fifo[1], 12 * 4);

// Add new value
Fifo[12] = NewValue;

int Result = Factor[0] * Fifo[6];

Result += Factor[1] * (Fifo[5] + Fifo[7]);
Result += Factor[2] * (Fifo[4] + Fifo[8]);
Result += Factor[3] * (Fifo[3] + Fifo[9]);
Result += Factor[4] * (Fifo[2] + Fifo[10]);
Result += Factor[5] * (Fifo[1] + Fifo[11]);
Result += Factor[6] * (Fifo[0] + Fifo[12]);

Is there any way I could rewrite this so that I dont have to copy so much memory each call, to increase performance? I thought about circular buffers and linked lists, but it would require much extra code and complexity, that it seems the above is the best option.

Maestro
  • 9,046
  • 15
  • 83
  • 116
  • A circular buffer is boilerplate here. Give it a power of 2 elements, like 16, so wrapping the index when you increment it is trivial with a simple AND. – Hans Passant Sep 02 '13 at 15:52

1 Answers1

1

You could implement a circular buffer by simply keeping an extra variable w/ the index of, say, the start of the buffer, and make all references to Fifo relative to that (modulo 13 -- thanks, Ingo!); that way, shifting is just bumping that index.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    It's a very slow embedded ARM cpu, so it could be that the modulo operation makes the code perform worse than the 'memmove' call, but I guess that will be a matter of benchmarking both. – Maestro Sep 02 '13 at 15:49