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.
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