gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > Reordering halfwords [ASM optimization]

#177549 - sverx - Thu Aug 16, 2012 9:29 am

Do anyone can suggest the faster method to move halfwords to the locations I need?
- One register contains the 'B' halfword in the higher half (lower half is zero)
- Another register contains the 'D' halfword in the higher half (lower half is zero)
- a third register contains the 'C' halfword in the higher half and the 'A' halfword in the lower half

I have to rearrange them in 2 registers so that in one register I get 'A' in the lower half and 'B' in the higher half, and in another register I get the 'C' in the lower half and 'D' in the higher half (thus I can write the 4 'ordered' halfwords to memory using a single STRD/STM)

Thanks :)
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS (and GBA!)|A DS Homebrewer's Diary

#177553 - Dwedit - Thu Aug 16, 2012 4:00 pm

So the problem is something like this:
Given:
r1 = BBBB0000
r2 = DDDD0000
r3 = CCCCAAAA
Make:
r1 = BBBBAAAA
r2 = DDDDCCCC

use this:
r2 = r2 | (r3 >> 16)
r3 = r3 << 16
r1 = r1 | (r3 >> 16)
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#177554 - sverx - Fri Aug 17, 2012 7:57 am

Thanks Dwedit! Once more I realize I always fail completely at asking questions :| The matter is that I already do something similar, and I was wondering if I really needed 3 instructions. I'm not an expert in ARM asm so maybe there is some way I'm missing to calculate r1 value using a single instruction, after calculating r2 as you did. Maybe it's simply impossible... :|
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS (and GBA!)|A DS Homebrewer's Diary

#177555 - Miked0801 - Tue Aug 21, 2012 5:48 pm

Nope, pretty sure this takes 3 instructions. You are already using the barrel shifter on 2 ops so I think that's about as good as it gets.

#177557 - sverx - Wed Aug 22, 2012 8:06 am

yesterday evening I tried using a 'SMLABB' instruction, but I had no luck since it performs a signed multiplication, so I've got a sign extension on the halfword msb :| Beside that, I'm not even sure it would have been a faster option, even if it surely was a single instruction instead of two...
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS (and GBA!)|A DS Homebrewer's Diary