PocketMatrix forums

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

15bit to 16 bit

Ok, I am having a little trouble comming up with a fast routine to convert from 15 bit (with an alpha chanle 555 +1 format)to 16bit(565) before blitting. My idea was to shift everything down one bit, and then shift part of it up. The problem is, I am not sure how to go about this(quickly). I have several ideas that would take a long time to run, and I need something as fast as possible. Thanks for all help you have.
The fastest way on a PocketPC is probably to use a look-up table (LUT)... But this would eat up 128K, something you might not want.

An alternative is to use two LUTs, one for the upper byte and one for the lower one. Then you would do:

color565 = LUT_HI[ color555 >> 8 ] | LUT_LOW[ color555 & 0xFF ];

Each LUT is an array of 256 uint16_t values. This would only eat 2*512 bytes. Much better, but two memory accesses. This might actually be slower then using straight shifts:

color565 = ((color 555 << 1) & 0xFFC0) | (color 555 & 0x1F);
In favor of size, I think I will take the shift.
You should bench both methods #2 and #3... #2 might actually be faster then #3. The PocketPC architecture doesn't work like your usual PC architecture. I've found out that many of my assumptions (based on PC experience) were wrong.
Ok, I am having a little trouble comming up with a fast routine to convert from 15 bit (with an alpha chanle 555 +1 format)to 16bit(565) before blitting. My idea was to shift everything down one bit, and then shift part of it up. The problem is, I am not sure how to go about this(quickly). I have several ideas that would take a long time to run, and I need something as fast as possible. Thanks for all help you have


Hang on - aren't the bits in the same place for 555 and 565 anyway? On my iPAQ it's 444 colour, BUT the bits are placed in the upper bits of 565, so I don't need to worry about it I just write 565.

I would guess (/hope to god if anyone in pocket PC manufactor land isn't a sadistic freak) this would be the case with 555 as well - hence no shifting needed at all and (gasp!) compatibility across units!

Anyone know - I'm worried now!
I've never heard of any device with a 555 display out there. Our friend Rob here is trying to convert 555 to 565. My guess is that he is working on some emulator (PSX?) or game that provides him with a 555 image to display.

The iPAQ is a different beast... The framebuffer is arranged as a 565 buffer (as reported by GAPI). The fact that only 4 bits of each channel is effective is another matter. I see two possibilities: the extra bits aren't even connected to anything or there is some limitation with the display cells. It doesn't really matter... From a programmer's point of view, the iPAQ has a 565 display. (But then there is banding... oh well).
Actually, a 555 lookup table to 565 would only be 65k.

Dan East
I'd go for Kzinti's last piece of code (well, almost ;)).

Normally a 555 representation has the TOP bit as the alpha... you said you were shifting it down? Are you sure it's the bottom bit that's alpha?


Ok. I'm actually assuming that the transparency is in the TOP bit, here, not the bottom, i.e.:

ARRRRRGGGGGBBBBB
To:
RRRRRGGGGGGBBBBB

We'll do two pixels at once, in an unsigned long.

Giving us:
ARRRRRGGGGGBBBBB ARRRRRGGGGGBBBBB
To:
RRRRRGGGGG0BBBBB RRRRRGGGGG0BBBBB

Code:

unsigned long ul_blue_bits;
unsigned long ul_not_blue_mask;

//Create a mask for the blue:
ul_blue_bits=0x1f;// bottom 5 bits stay the same = 0x1f = 11111
ul_blue_bits+=ul_blue_bits<<16;// copy the bits into the high half-word
ul_not_blue_mask=~ul_blue_bits;// make a bit-mask

// repeat this step over the entire area

// NB: I moved the shift to always leave the bottom bit of green as zero, not whatever the top bit of blue was.
color565 = ((color555 & ul_not_blue_mask)<<1) | (color555 & ul_blue_bits);



IMO, a LUT for this, on a StrongARM is a bad plan. When it loads a single entry, you're pulling a 16-byte (IIRC) cache-line from memory, and I suspect your "hits" are going to be all over the place, so it's going to fill the cache with garbage and you're going to get almost zero usage out of it. Not good. On the other hand, the shifts and ANDs are very efficient on the ARM.

If you *really* need speed for this, let me know exactly what you want and I'll knock you up a function in ARM code (if you're doing it over a buffer, anyway).

Regarding the (earlier) iPAQs, they have a 4096 colour LCD. The bottom bits from the colours are thrown away.

Cheers,

Refractor
Ok, I am having a little trouble comming up with a fast routine to convert from 15 bit (with an alpha chanle 555 +1 format)to 16bit(565) before blitting.


Idea - why not make your alpha bit the low bit of green instead of the highest bit? That way no shifts are needed, you just go &0xffdf!
You generally don't get to choose which bit is the alpha bit.

If you could choose the alpha bit to be the lowest bit in green, you probably wouldn't even bother with the AND - the lowest bit of green isn't going to affect the image /that/ much (I only "corrected" it earlier because there was no reason not to).
You could use a LUT of ints, which would cost 131k. Looking up and converting from 32 bit to 16 bit would probably be more efficient than performing the math on the fly.

Dan East
Ummm... I really do doubt it, on a PPC processor. On a "real" PC with a much larger cache, it might work out alright.

You could do the mask and shifts in very few cycles (circa 4 or 5 per pair of pixels).

As I said earlier, the StrongARM will fill a cache-line for every load. So, in this case, you'll be doing:

LDR value,[base,pixel]

The moment that hits the processor, a cache-line is requested. Until that cache line is fully loaded into the cache, any subsequent loads will *stall*.

So:

LDR value,[base,pixel1]
(Big stall)
LDR value,[base,pixel2]

In this case, the (Big stall) is going to be larger than the 4 or 5 cycles for the shifting method.
On an area of memory that large, you've got to assume that a large majority of the lookups will miss the cache.

Of course, if you can fill the (Big stall) with useful operations that don't use the memory or loading registers (data processing) then you're ok... but for something to process a screenfull of 555 to 565 the shifting method will walk all over a LUT (on a PPC).

Cheers,

Refractor
Ok, I made a mistake in my original discription, but you guys caught it anyways. When I asked what format the psx used, they said 555 with an alpha bit as the lowest bit. Apparently, they were wrong, because I looked through the code, and it was laid out as you guys have said.

Right now, I am using the first method proposed. It is set up as a definition, and is converting each pixel right before it is blitted. Refractor, if you wanted to write me up some asm, that would be great. Here is what the hole blitting routine does.

Ok, the facts.

1. The psx buffer is 1024x1024 (technicly it is 1024x512x16, but anyways).
2. Psx games use different resolutions. Your function will be given the width and height that the game uses. The original function also gave the destination width and height, but you know that is going to be 320x240, so there is no need to worry about that.
3. The function will have to convert 15 bit to 16 bit, and then write the pixel to the video buffer. I will pass the address of the buffer to the function.
4. The function will have to do a rotated blit.

here is an example of the original scalling routine used in the psx emu (note, this did not rotate or blit the image, it simply scalled it).

Code:
void soft_scale (UINT16 *src, int sw, int sh,
UINT16 *dst, int dw, int dh) {
int x,y,i;
float xs,ys,xx,yy;


xs = (float)dw / sw;
ys = (float)dh / sh;



if ((xs==1) & (ys==1)) {
for (y=0; y<dh; y++) {
for (x=0; x<dw; x++) *(dst++) = *(src++);
src+=1024-sw;
}
return;
}

yy = 0;
for (y=0; y<sh; y++) {
xx = 0;
yy += ys;
if (yy >= 1) {
for (x=0; x<sw; x++) {
xx += xs;
if (xx >= 1) {
for (i=0; i<(int)xx; i++) {
int j;

for (j=0; j<(int)yy; j++)
*(dst+(dw*j)) = *src;
dst++;
}
src++;
xx -= (int)xx;
}
else src++;
}
for (i=1; i<(int)yy; i++) {
dst+=dw;
}
yy -= (int)yy;
src+=1024-sw;
}
else src+=1024;
}
}


As you can see, the game positions it's self in the top left corner.

Well, good luck, and thanks for the contribution.
I said I'd do you a 555->565 over a buffer, not a bitmap scaling/rotation routine. :P Typical user specification adjustment ;)

Question:

The emulator is running in landscape? (320 x 240 rather than 240 x 320). If so, why do you want a rotated blit?

Cheers,

Refractor
Unless I am missing something, the video buffer on the e200 is set up as 240x320, not 320x240. This is my first use of the gapi for the ppc (I used to use HPC GAPI which is extreamly different), so it is possible that there is a way to change the buffer to 320x240 that I just don't know of.

P.s. I will eventually get around to rewriting the code in asm, but currently I am working on other things right now.
Urk. The iPAQ 36xx screen's natural orientation is landscape. I.e. if you plot to screen memory in a contiguous run (with the device upright as normal) you start at the bottom left, draw a column up the screen, then the next column, from left to right.

If you're right about the e200 you'll want a non-rotated and a rotated blit version ... or you'll have to hack at the other side of things to produce a pre-rotated buffer to read from.

Good luck!

Cheers,

Refractor
I think I remember hearing about this from somewhere else. I will probably just make an ipaq version and a nonipaq version. If you notice, ziosoft ran into the same problem with their tiger woods golf, and that is why there is two versions. Try to switch to landscape on the e200, and the screen becomes all garbled.
refractor, I am going to take you up on that offer to make a fast asm routine to convert from 15 to 16 bit. The sooner you can get it, the better, thanks.
Ok. I'll try and sort something out if I get some free time this week.
refractor, here is what I have so far
Code:

ldrh r9, [r1] ;copy the 15 bit color value from the source
mov r12, r9, lsl #1
mov r14, #0xFF, 24
orr r14, r14, #0xC0
and r12, r12, r14
and r9, r9, #0x1f
orr r9, r12, r9
strh r9, [r6] ;copy that value to the screen


So, if you just wanted to optimize it (or at least tell me how, that would be great.
Put the "and r9, r9, #0x1f" between lines 4 and 5 and swap lines 2 & 3 to hopefully reduce pipeline stalls. It may or may not help.

I think for this sort of thing, the most optimization would probably come from optimizing the whole blit routine to work on more than one pixel at a time. I don't think there is really that much to gain from optimizing the assembly for the pixel conversion without finding a fundamentally more efficient algorithm (I don't think there is one). Just about all you can do is reduce pipeline stalls.

If you worked on two pixels at once, you could do the same thing for two pixels in only ~2 more instructions (you would have to do the masking differently; either by loading the mask from memory or maybe using the BIC instruction).

probably what I would do is something like this:
Code:

ldr r9, [r1] ;copy two 15 bit color values from the source
mov r12, r9, lsl #1
mov r14, #0x1f ; mask for pixel 1
bic r12, r12, #0x3f ; clear the contaminated bits from pixel one.
orr r14, #0x1f0000 ; plus mask for pixel 2
bic r12, r12, #0x3f0000 ; clear the bottom of pixel two
and r9, r9, r14 ; mask it off
orr r9, r12, r9 ; combine redgreen and blue
str r9, [r6], #4 ; write to screen and go to the next pixel

Should be only one cycle longer (shorter if pipeline stalls are prevented (I still don't completely understand them) and/or the removal of the halfword writes saves cycles (I think it does)), Factor in the cycle that is saved by the use of writeback on the store (Which you probably would have used had you used the code in a loop), and you've got a better *and* faster version.
Ok. Here we go then.

Problems I see with your code (at the ARM code level - I haven't "sanity checked" the code to see if it's doing the right thing). I always use a % symbol to denote binary - it's an old habit.

#1 the stall after the load - shifting instructions around would solve that... were it not for #2
#2 it's doing one pixel at a time, and performing set up for every pixel. Yucky.

Code:

ldrh r9, [r1] ;copy the 15 bit color value from the source
(stall waiting for r9 here)
mov r12, r9, lsl #1 <--- this could be removed and combined with the AND later = and r12, r14, r12, lsl #1
mov r14, #0xFF, 24
orr r14, r14, #0xC0
and r12, r12, r14
and r9, r9, #0x1f
orr r9, r12, r9
strh r9, [r6] ;copy that value to the screen


Let's see if we can better that a little, code-wise :) :

Code:

ldrh r9, [r1] ; load
mvn r14,#0x3f ; = one's complement of 0x3f = ~ %111111
and r12,r9,#0x1f ; = blue = pixel & %11111
and r9,r14,r9,lsl #1; = (pixel<<1 & ~0x3f)
orr r9,r9,r12 ; = blue | (pixel<<1 & ~0x3f) = reconstituted 565 pixel
strh r9,[r6] ; store


IIRC you can use writeback on the ldrh and strh to perform the increments on r1 and r6:
Code:

ldrh r9, [r1],#2 ; load
strh r9, [r6],#2 ; store


Let's do a two-pixel version, too. :D

Just for reference, the code I / kzinti posted earlier, in C:

Code:

//Create a mask for the blue:
ul_blue_bits=0x1f;// bottom 5 bits stay the same = 0x1f = 11111
ul_blue_bits+=ul_blue_bits<<16;// copy the bits into the high half-word
ul_not_blue_mask=~ul_blue_bits;// make a bit-mask

// repeat this step over the entire area

// NB: I moved the shift to always leave the bottom bit of green as zero, not whatever the top bit of blue was.
color565 = ((color555 & ul_not_blue_mask)<<1) | (color555 & ul_blue_bits);


I'd be inclined to do something like:

Code:

; R0 = Source address
; R1 = Destination address
; R2 = Count ( MOD 2 == 0)

STMFD r13!,{r4-r6,r14}

; Setup:
MOV r3,#0x1f ; Mask for blue - low pixel - 5 bits
; = %00000000 00000000 00000000 00011111
ADD r3,r3,r3,LSL #16 ; copy mask for top pixel
; = %00000000 00011111 00000000 00011111

MVN r4,#0x3f ; we want to obtain a mask with bottom *6* bits unset
; = %11111111 11111111 11111111 11000000
BIC r4,r4,#(0x3f<<16) ; clear the other 6 bits:
; = %11111111 11000000 11111111 11000000

; R3 = blue mask
; R4 = red + green + alpha mask

; Real work:
.remove_alpha_loop
LDR r6,[r0],#4 ; Load the pixels into R6 + increment load address
; "Unavoidable" stall
AND r5,r6,r3 ; r5 = blue bits of the pixel (color555 & ul_blue_bits)
AND r6,r4,r6,LSL #1 ; r6 = (ul_not_blue_mask & (pixel<<1))
ORR r5,r5,r6 ; r5 = (ul_not_blue_mask & (pixel<<1)) | (pixel & ul_blue_bits)
STR r5,[r1],#4
SUBS r2,r2,#2
BNE remove_alpha_loop

LDMFD r13!,{r4-r6,pc}


Ok. Points raised:

Firstly, this routine is "correct". You could write a faster, but slightly incorrect version that used an inversion of the blue mask to mask the rest against - the problem would be that the top bit of blue would appear in the bottom bit of green. If you wanted that, the setup would be 1 cycle shorter (really not worth it, IMHO):

Code:

MOV r3,#0x1f ; Mask for blue - low pixel - 5 bits = %11111
ADD r3,r3,r3,LSL #16 ; copy mask for top pixel
MVN r4,r3 ; 1's complement of mask= %11111111 11X00000 11111111 11X00000


The bits marked X would be "random"... but as it's only the lowest bit of green it wouldn't matter all that much. On the iPAQs it wouldn't even be visible as the bottom one or two bits are masked off anyway.

Secondly, the double-pixel routine is not fast for a single case. It performs setup and then runs reasonably quickly over sets. It's designed to be given a word-aligned address and an even number of pixels. Give it anything else and you'll get an unaligned address exception and it's goodbye program... and if the count MOD 2!=0 then you're not going to terminate your loop.

Thirdly, optimisation. As always, optimisation is going to be a little "painful". What I'd do is this:

see how many pixels you're going to be doing. If it's lots, branch to a routine that does things in bulk, and maybe works on cache-aligned data.

So, you'd be given an address. You'd check how many pixels were to be done. Then you'd do a pixel-pair at a time until the cache line (address MOD 32==0). Then if you have more than 16 pixels to do still, go to a routine that works on 16 pixels at a time, until the count <16... then do it a pixel-pair at a time again until you're done.

The "cache-aligned" routine would do something like:
(though I wouldn't really have it as a separate function because you're stacking registers again when you needn't)

Code:

; r0 = source
; r1 = destination
; r2 = count
; r3 = blue mask
; r4 = NOT blue mask (ish)
; r5-r12, r14 = fair game

stmfd r13!,{r4-r12,r14} ; may not have to stack r12 here for APCS

.aligned_loop
ldmia r0!,{r5-r12} ; load a cache-line full of data

; (stall here)
AND r14,r5,r3 ; r14 = blue bits of the pixel (color555 & ul_blue_bits)
AND r5,r4,r5,LSL #1 ; r5 = (ul_not_blue_mask & (pixel<<1))
ORR r5,r5,r14 ; r5 = (ul_not_blue_mask & (pixel<<1)) | (pixel & ul_blue_bits)

AND r14,r6,r3 ; r14 = blue bits of the pixel (color555 & ul_blue_bits)
AND r6,r4,r6,LSL #1 ; r5 = (ul_not_blue_mask & (pixel<<1))
ORR r6,r6,r14 ; r5 = (ul_not_blue_mask & (pixel<<1)) | (pixel & ul_blue_bits)

AND r14,r7,r3 ; r14 = blue bits of the pixel (color555 & ul_blue_bits)
AND r7,r4,r7,LSL #1 ; r5 = (ul_not_blue_mask & (pixel<<1))
ORR r7,r7,r14 ; r5 = (ul_not_blue_mask & (pixel<<1)) | (pixel & ul_blue_bits)

;...
;...
;etc (put this in a macro maybe)

; store the 16-pixels in one chunk
stmia r1!,{r5-r12} ; store a cache-line (write-buffer-full) of pixels

sub r2,r2,#16
cmp r2,#16
bgt aligned_loop

ldmfd r13!,{r4-r12,pc}



Again, it's designed to go fast over large sets.

Billcow:

The result latency is really the number of cycles it will take for the result of an instruction to be ready. The main "offenders" are loads (especially from a non-cached area) and multiplies. If the result from a multiply, say, takes 3 cycles, then you have to wait for the result to arrive, and the pipeline stalls. For example, let's pretend there's a 3-cycle penalty on the result of every MUL:

Code:

MUL R0,R1,R2
ADD R3,R1,R0 <- We're using the result of the MUL straight away.. we MUST wait 3 cycles for R0 to arrive = pipeline stall
AND R6,R5,R4 <- 1
AND R7,R8,R9 <- 2
AND R10,R11,R12 <- 3


vs

Code:

MUL R0,R1,R2
AND R6,R5,R4 <- 1
AND R7,R8,R9 <- 2
AND R10,R11,R12 <- 3
ADD R3,R1,R0 <- R0 now available - no stall here


the second code is 3 cycles faster because the pipeline doesn't stall.

I have a StrongARM machine with a very slow bus - on that you can get silly amounts of processing done in the latency following a load.



Quick disclaimer:

none of the above code has been tested or even compiled (and obviously I'm not infallible and may have screwed up somewhere).


Hope that helps, cheers,

Refractor
Those are some very interesting concepts that you have taught me. Now, here is the thing. Because of the way casio has their vram set up(it's 240x320, not 320x240), you have to perform a rotated blit for landscape mode, which means that every pixel is 480 bytes apart in a row (this is in landscape mode). So, is there a way to copy 2 pixels at a time with this setup?
Well, the screen is still usually a linear buffer... i.e. a contiguous chunk of memory.

So, it really shouldn't shouldn't matter the orientation of the screen. Just because GAPI tells you you have to do stupid things like move the pointer along 480 to access the "next" pixel horizontally doesn't mean that you have to play by its rules and actually do it.

Ask GAPI to give you the start address of the screen and the orientation, and then shove a full, contiguous row/column into it (depending on the orientation), and move along to the next row/column (on the iPAQ you don't even need to move along to the next row, but on some devices you might). All you've got to do is watch out for variations in the "pitch".

If you're plucking pixels from a rotated buffer it should be fairly simple to pick them out two at a time, transform them and plot them.

If you always try and load and store in the same "grain" as the memory/cache-lines you're going to produce fast code.

If you start loading and plotting 16-bit pixels one at a time, all over the memory it's going to be slow as hell. You need to try to do at least one side of things (loading or storing) contiguously in memory.

Cheers,

Refractor
Ok, here is my next question/comment. Since I also have to scale the buffer, it is not easy to grab a bunch of pixels at a time, since some I will be jumping over, and other's I won't. So, do you think it would be quicker if I scaled the buffer first, and then converted/blitted second in a second funtion, or is it quicker to just blit as I scale (which I am currently doing right now)?
You might not be grabbing pixels from contiguous blocks, but you should be able to store them contiguously. Even if you load eight individual pixels from your (rotated) source using LDRH then transform and store them using a STMIA you're going to gain speed, IMO. Essentially you want to implement a simple form of write-buffering.

You should at least try and combine the operations, otherwise you're just loading and processing the same data over again (and a screen-full of data is much larger than the cache so the cache isn't going to help).

Cheers,

Refractor
If you start loading and plotting 16-bit pixels one at a time, all over the memory it's going to be slow as hell. You need to try to do at least one side of things (loading or storing) contiguously in memory.


What's wrong with non-contiguous?
I don't see a problem... especially if you stash the pixel pitch in a register.
Reading non-contiguously screws with the cache. However, I have heard that the iPAQ's *video* memory isn't cached, so if you are reading (don't do it) or writing to the *video* memory, it won't matter. Like I said, though, reading from video memory is horrendously slow, so don't do it.

So if you are writing directly to video memory, it shouldn't matter if you are writing in order, so it's probably better to focus more on reading in order.

There was a discussion of this a while back on Phantom's Forum.
Non-contiguous is bad because on a cached area of memory, every read loads an entire 32-byte cache line (and stalls the data bus while it's doing it). The same kind of thing goes for writing - if you're writing contiguous blocks of memory to a buffered area then the write-buffer is used and you get less stalls on the data-bus.

As billcow says, though, the screen on most systems isn't actually cached, or buffered, so writing to the screen isn't affected as much as writing elsewhere... but IMO it's almost always worth doing it "properly".

Cheers,

Refractor
Hi refractor, nice plot asm.
Wouldn't you be able to save a cycle by moving the sub r2,r2,#16 to up just below your 16 pixel load? This would fill in 1 cycle of your load stall and would be effectively free. My asm is ARM asm is a bit rusty since my Archimedes days, so I may be wrong. :wink:
Regards,

Steve.
Varkentje:

Yes, it *may* save a cycle (though with the full processing of the 8 registers it probably wouldn't, well spotted though :)

I used to have an Archimedes too.. first an A310, then an A3000 which was customised with an ARM3 and a whole 4MB RAM :) I've got a StrongARM'd RiscPC sitting on my desk at home, too. Lovely machines they were <sniff>.

Cheers,

Refractor