PocketMatrix forums

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

assembly bitmap scaler

Does anyone have an ARM assembly bitmap scaler routine or know of a source for one?

Ideally it would interface like this:
blitscale(
word *src, int srcw, int srch,
word *dst, int dstw, int dsth,
int sx, int sy, int sw, int sh, // source region
int dx, int dy, int dw, int dh) // destination region
If you find no other source, post exactly what you want in C with a compilable test harness and I'll do it for you.
wow thanks refractor, ill wait the weekend to see if anyone posts anything and if not ill get what you request :)
Sorry for the delay, I have created a testbed and C function scaler, the testbed uses PocketFrog, you can download it here

http://pocketfrog.droneship.com/download.html

use EVC 3.0
1) download and unzip pocketfrog
2) open the pocketfrog workspace
3) replace the example blit.cpp file with the one attached
3) compile the PocketFrog lib
4) compile Blit and run it

You could convert the whole blitStretch function and its sub function, but I think that might be a waste of time at it is doing some simple clipping and probably won't yeild must of an increase in preformance.

The workhorse function is blitStretchClippedFixed and the one which should be tuned in ASM if possible.

I have some benchmark results in the file. Let me know if you need me to explain anything else or need some code changes or comments. I think the blitStretchClippedFixed function is about as optimized as it can be in C.
Attachments:
blit.cpp (10 KB)
No device at this location; no idea what this'll do.

Code:

void blitStretchClippedFixed(
word *dst, int dw, int dh, // dest surface
word *src, int sw, int sh, // source surface
int x, int y, int w, int h, // position and size of scaling
int mx, int my,
dword fdx, dword fdy,
dword fsx, dword fcy)
{
word *cur_dstp = dst + x + y * dw;
word *cur_dstpend = cur_dstp + (my - y) * dw;
int scaledw = mx - x;

while(cur_dstp < cur_dstpend)
{
word *dstp = cur_dstp;
word *srcp = src + (fcy>>fs) * sw;
dword fcx = fsx;

int n = (scaledw + 15) / 16;

switch(scaledw % 16) {
case 0: do { *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 15: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 14: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 13: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 12: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 11: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 10: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 9: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 8: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 7: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 6: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 5: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 4: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 3: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 2: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
case 1: *dstp = *(srcp + (fcx>>fs)); dstp++; fcx += fdx;
} while (--n > 0);
}

cur_dstp += dw;
fcy += fdy;
}
}
Change lines 19 to 21 as follows and try your benchmark again:

Code:

int n = (scaledw + 15) >> 4;

switch(scaledw & 15) {


Dan East
Thanks Andy and Dan, it works. 8) I think the code is getting so fast now that speed improvements are hard to see on my Axim x50v, I am only going by framerate as well which isn't very accurate in this blit tutorial. Here are the results

benchmark results - fps when all the way zoomed in and pocketfrog logo is gone

Axim x50v

DEBUG mode
13 fps - blitStretchClipped section 1
36 fps - blitStretchClipped section 2
36/37 fps - blitStretchClippedFixed

RELEASE mode optimized for speed EVC 3.0
41 fps - blitStretchClippedFixed
41/42 fps - blitStretchClippedFixedAndy

so your change squeeks out an extra .5fps or so it seems :) the big difference will be on slow devices though, at 620mhz the difference isn't noticible, but at 150 or 200mhz it should be x4 at least, which might mean a difference of 2+ fps. I'll get my e330 out and test with it, its the slowest device is the world ;)
It's looking fairly bus-bound so I'm not expecting a huge gain in performance from an ARM version, but I'll do it tomorrow anyway. :mrgreen:

You should definitely sort out the blitStretch function though -- ideally you should do all of that in fixed point (the float divisions will cost a lot).
Right.

Attached is a blit.exe with blitStretchClippedFixed coded in ARM (the original function, not Andy's). I'll post the source for the ARM function in a few hours after I've tidied it up a bit.

I haven't had time to properly speed-test it yet (I just wrote it and made sure it ran). As I said before, it looks pretty bus-bound and I wouldn't be surprised if there was little to no gain (other than giving me something interesting to do during my lunch hour).

I could probably kill another cycle or two out of the setup, but the loops are looking pretty tight (tighter than the compiler's version at least). I could also modify the ARM based on Andy's suggestion and see if that helped at all.
Attachments:
Blit.zip (44 KB)
I get the same as results as Andy's function, but this is on a very fast device (x50v) on a slow CPU device I think this may make a big difference, I will test that specifically in the future.
I haven't had time to tidy/finish optimising it, but here's the function anyway.

How it works is left as an exercise to the reader.

Use it in C like this (note that I changed the prototype to get rid of unused parameters):

Code:
extern "C" {
void blitStretchClippedFixed(
word *dst, int dw, // dest surface + pitch
word *src, int sw, // source surface + pitch
int x, int y, // position and size of scaling
int mx, int my,
dword fdx, dword fdy,
dword fsx, dword fcy);
}
Thanks a lot refractor :D
joshbu [MSFT] · 23 Aug 2005, 21:30 UTC
Nice blitter. I don't think Duff's Device is ever going to die. :D

Possibilities for faster processing still:

1. For bigger images: make sure all scan lines are cache line aligned. (Try saying that 3 times fast...) and use the Xscale prefetch on them.

2. Only read and write 32 bits a time. It's actually probably worth the extra CPU clocks to cut your read/write frequencies in half. There might be a clever application of WMMX here as well to process multiple pixels in parallel.

3. Special cases for exact 2x/3x/4x ... scaling?

4. Speical cases for small source images: unroll the loop.
Hi joshbu, thanks for your suggestions.

I tried a 32 bit implementation and it provided no speed improvement at all, plus you had the mess of making sure everything was aligned and cleaning up if it wasn't, didn't seem to be worth it.

To ensure scan lines are cache aligned wouldn't that be specific for each type of processor or device? Or do they all have the same cache sizes? I'm not sure this would matter since my data is all in a tightly packed array, so it should already be only missing the cache the minimal amount.

Adding special cases for certain scaling would speed it up a lot, but is so rare that it would hit those specific sizes its not worth doing (of course this depends a lot on your app).
To ensure scan lines are cache aligned wouldn't that be specific for each type of processor or device? Or do they all have the same cache sizes?


ISTR it's not in the ARM specification, but practically everybody uses a 32-byte cache line in their implementations (StrongARM, XScale, Samsung's ARM9). I'm slightly surprised that 32-bit writes didn't gain you anything, however. Maybe I'll have another play at lunchtime :)
Yeah, I am surprised too.

When I was writing some ASM blitting code having 32 writes was quite beneficial even when it occasionally meant necessity of running realigning code.
joshbu [MSFT] · 25 Aug 2005, 15:57 UTC
mm40 wrote:To ensure scan lines are cache aligned wouldn't that be specific for each type of processor or device? Or do they all have the same cache sizes? I'm not sure this would matter since my data is all in a tightly packed array, so it should already be only missing the cache the minimal amount.


A little more on prefetching: on an XScale, it's possible to be prefetching a cache line while still executing code. That's pretty powerful. Normally, when you're executing a load instruction and you miss the cache, the CPU blocks until the line has been fetched (yes, the entire line: even for a 1 byte read.) The exact miss penalty varies with RAM and bus speed, but a good rule of thumb is orders of magnitude: 1 cycle to read from register, 10 from cache, 100 from RAM.

So, in practice what you're doing is prefetching the next chunk of data before you go to work on the current one.
I think the reason 32bit didn't gain me much is because it is bound by the memory bandwidth with such a fast CPU.

I'm still not convinced that prefetching would do anything, because it doesn't matter when you request the data to be ready, everything is still limited by the memory bus speed. Even in the worst case with 100 clocks wasted I think the CPU is still waiting for the bus most of the time, I would be interesting to try it though.

Is it useful to call a prefetch function from C if one was made in ASM? And how would I use it? In a simple blitter which copies memory from one location to another for example would you simply call prefetch(src+cachesize) when you were copying src?
awesome, how is the best way to use it?

also has anyone seen huge improvements by using the latest EVC compiler and SDK? I use the 2002 SDK for backward compatibilty with all ARM devices, but I was wondering if you used the 2005 SDK and compiled specifically for XScale (if this is possible) does this speed up your code?
if you believe that your code is purely memory bound i suggest you have a look at this thread:

http://www.pocketmatrix.com/forums/view ... hp?t=19742

it turned out that by prefetching (using pure c code) it was possible to speed up the algorithm by several 100%.

bye,
Daniel
Prefetching can easily double the speed of your blitter. Trust me.
So, with prefetching in this example - you would only prefetch the source, right?

If you are doing something like a blending of images, would you prefetch both the source and destination - or would that mess things up?
joshbu [MSFT] · 26 Aug 2005, 21:48 UTC
Prefetch the memory areas you are going to read.

Thus, in a blending or multisampling routine, yes, there would be some value to prefetching the destination. Unless the destination is the primary surface.

On XScale devices, we encourage driver writers to mark the primary uncached with combining. This gives us the maximum write speed possible without cluttering the cache. It also means that prefetching will have no effect.

General graphics tip: reading back from the primary is only going to get more expensive on PPCs/SPs. Don't do it.

We've also seen some good speed-ups making other buffers that are overwhealming written to (back buffers, or composition buffers) uncached with combining.
I tried adding prefetching as described in the thread linked above, I put the following just above my inner loop in blitStretchClippedFixed2

Code:

// preload the source line into the cache
// 32 byte cache lines / 2 bytes per pixel = 16 pixels per cache line
// note this will miss the first pixels if the width is not a multiple of 16
int i = scaledw - 16;
int dummy0 = 0;
while(i >= 0)
{
dummy0 += srcp[i];
i -= 16;
}


It had no effect, would using EVC 3.0 with the 2002 SDK compiling for WCE ARM be holding this back? Do I need to be compiling to WCE XScale if there is such a thing?
Is it possible that those lines are being optimized out; ie, does declaring dummy as volatile make a difference?
I think I'm not seeing any improvements because of some other barrier, I just compiled blit.exe without any drawing except for the frame counter and the words 'pocketfrog' in the corner and only get 47fps! :x obviously trying to inch towards 47fps from 41fps by optimzing one function isn't going to cut it. the barrier must be somewhere in pocketfrog, gapi, or the QVGA to VGA scaler in the x50v, time to move benching to my other axim I think...
after moving to my axim x5 for testing things are much clearer, turns out the x50v is a terrible device to bench on because the internal scaler to VGA eats a bunch of cpu and time (when going by framerate).

what i have found:

1) using dwords are faster for larger sized blits, but the difference in speed isn't worth the code changes.
2) unrolling loops helps a lot more than i thought, and is really easy to do with macros.
3) prefetching does speed up accesses to large memory, but dont use it on small bitmaps.
4) memcpy is very fast when used with the correct bitmaps (i also optimized my normal blitter)

with all changes both my stretch blit and my normal blit are about 2x faster :) thanks for all the help guys :D
the code in that other forum was not the latest version. here is my the latest version:
Code:

const unsigned int* srcI = (const unsigned int*)nSrc;
register int dummy0=0,dummy1=0,dummy2=0,dummy3=0;
unsigned int* dstCLineEnd = dstC + nWidth/4;

// preload 2*nWidth bytes into 2*nWidth/32 cache lines
// (32 bytes each by reading one DWORD from each cacheline)
// use dummies to force reading of data
// uses all 4 load buffers of the xscale processor
int i = nWidth/2 - 8; // same as: i = (2*nWidth/32)*8 - 32;
while(i>=0)
{
dummy0 = srcI[i+ 0];
dummy1 = srcI[i+ 8];
dummy2 = srcI[i+16];
dummy3 = srcI[i+24];
i -= 4*8;
}

it makes use of the fact that the XScale's cachelines are 32 bytes long and that reading 4 DWORD will fill the whole cacheline. furthermore the XScale has 4 read buffers which can read in parallel (therefore dummy0-dummy4). i just execute this code before processing a scanline. the actual algorithm is unchanged. the result is a speedup of around 300%-400% (haven't done these timing for quite some time).

believe me, even if you code is already "fully optimized" (as mine was). using clever precaching can give you a huge speedup, since fast algorithms are soon memory bound on XScale processors.

Daniel
drgoldie: thank you, i added prefetching and it speeds up my fastest code a small bit (10% - 20%) (i tested this yesterday on my X5) the faster the processor the less of an impact prefetching has, on my x50v (620mhz) prefetching did nothing. i tried both single and quad prefetching and quad was actually slower or the same in all my tests. also i found the prefecting overhead is actually slower once you access lines which are less than 256 bytes, which makes sense if that is the cache size...

fyi: one thing to watch when testing that i ran into is you must blow the cache between each test blit, otherwise things appear much faster than they actually are as everything (especially on small blits) gets cached in all sorts of places...

one thing i was wondering is why do you preload the data backwards? is this if the scanline is too long the data at the end is pushed out by the data at the front of the scanline?

my prefetching
Code:

// word pointers
int i = scaledw - 16;
while(i >= 0)
{
dummy0 += srcp[i];
i -= 16;
}
i don't remember any good reason for prefetching backwards. i think it was just for testing against 0.

but it made a big difference using four dummy variables over just one (four read buffers in XScale). i tested my speedups on an X30 device. if done properly the speedup (considering your code is really, really just memorybound, which was in my case) the speedup should be enourmous.

why do you add 16?
is srcp an unsigned short* in your case?
did you try unsigned int* ?

bye,
Daniel
joshbu [MSFT] · 30 Aug 2005, 17:28 UTC
Just as an FYI, this sort of code is not really prefetching because the CPU still needs to do a store into your "dummy" variables. Wether they are registers or on the stack, neither scenario is favorable.

Using "register" variables tends to make the compilers register allocation algorithm less efficent, and using stack variables could potentially incure a store, which adds an extra memory access.

Using the XSCALE "pld" instruction incurs neither penalty. It's a "fire and forget" tool. The memory controller is even capable of working out which cache line to fetch for unaligned pointers, and will do nothing if you prefetch NULL.
reading a memory value into a register is done in a single clock (not counding memory bound effects). considering that my whole "prefetching-loop" is purely memory bound, i'd say that this is exactly as good as using PLD. infact i tried the corresponding intrinsic function and it performed worse since (looking at the ASM output) the compiler was not able to optimize the code as good as it did for the simple c code loop without intrinsics.

from my point of view only a pure ASM version would perform optimal with PLD and i doubt that it would be faster in this case. infact Tala tried to write a better ASM version but his code performed worse than my c-code.

Daniel
from my point of view only a pure ASM version would perform optimal with PLD and i doubt that it would be faster in this case. infact Tala tried to write a better ASM version but his code performed worse than my c-code.


@joshbu [MSFT]
Indeed, if you read the thread drgoldie linked last page, i had the very same arguments in favour of using prefetch instead of loads. The main advantage (besides register usage) is the fact, that pld is not blocking the execution.
Sadly i was not able to top drgoldies implementation in terms of performance unsing assembly and pld.

Tala
joshbu [MSFT] · 01 Sep 2005, 22:32 UTC
Interesting. A little bizarre, but hey, things don't always work the way the "should". :)