PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Cache or unrolling?

In one of my routine I copy the data from one surface to another.

I used to have this simple line in the body of my loop:

Code:
*Destination = *Source;
However I recently replaced that code with:

Code:
// this is only pseudo code to explain
Destination1 = Dest;
Destination2 = Destination1+Pitch;
Destination3 = Destination2+Pitch;
Destination4 = Destination3+Pitch;

// same for source
// ...

// then do four copies at once:
*Destination1 = *Source1;
*Destination2 = *Source2;
*Destination3 = *Source3;
*Destination4 = *Source4;

I noticed an increase in speed after this change. Is it that the cache is better used, or the fact that I simply unrolled the loop?

Anything else I can do to improve this?

Thanks,

--Eric
Are each of those variables seperate? (ie you have Destination1-4?)
I would think *Destination++ = ... would work just as well.

As to your question, it could be a little of both if you can clarify my question above I could guess which. ;)

Most likely its the unrolling, when you have a loop, especially a small one the processor stream gets thrown out a lot, which wastes cpu cycles. When you have a larger block of straight sequencial code, there are very few external delays.
Yes I have four separate variables. I cannot use Destination++ because I need to add the X or Y pitch for the surface. I'm not copying pixel from the same X,Y on the source and destination.

Here is another fact I discovered. Since this routine only deals with fixed-sized surfaced, I tried to replace the whole inner loop the (for X = ..) and just leave the outer look (for Y = ...).

The copy function then become a lot *slower*. I guess it's probably because of the larger quantity of code and cache issues, but I'm not sure.

I was looking to optimize the copy routine further, but since I'm not copying the pixels on the destination, I don't know what to do next.

Thanks!
efortier: A little tip:

(1) Check if the abs value for xpitch and ypitch. If xpitch is the largest one, swap them (if you do so you need to swap width/height as well).
(2) If xpitch is negative, add WIDTH to xyOffset and set it to positive.

You can now use both ++Source and ++Destination, resulting in more than 2x performance improvement on devices such as the 3630 IPAQ...