Endian Puzzle
rokicki
Posts: 1,000
Okay, here's a cute puzzle for all you propellerheads.
The Propeller stores data in its main memory in little-endian format.
Turns out for one application I need to do a byte swizzle operation,
that converts a 32-bit little-endian word to a 32-bit big-endian word.
(That is, I want the bits in the bytes to stay in the same order, but the
bytes in the word to be reversed; so $deafc089 would become
$89c0afde.)
What is the fewest number of assembly instructions that suffice?
Assume input and output can be in registers (the same or
different, your choice) and you can use as many other registers
as you need.
I want fewest instructions executed, not fewest instructions of
code.
I've got what I think is a good solution, but I'm not going to spoil
it by presenting the answer here. My instruction count is at
http://tomas.rokicki.com/proppuzzle.txt (but my solution is not
there). See what you can come up with.
Another interesting puzzle is what is the worst case bit-swizzle
in terms of required instructions.
If no one answers this post, I'll just patent the code.
Post Edited (rokicki) : 11/20/2006 11:48:07 PM GMT
The Propeller stores data in its main memory in little-endian format.
Turns out for one application I need to do a byte swizzle operation,
that converts a 32-bit little-endian word to a 32-bit big-endian word.
(That is, I want the bits in the bytes to stay in the same order, but the
bytes in the word to be reversed; so $deafc089 would become
$89c0afde.)
What is the fewest number of assembly instructions that suffice?
Assume input and output can be in registers (the same or
different, your choice) and you can use as many other registers
as you need.
I want fewest instructions executed, not fewest instructions of
code.
I've got what I think is a good solution, but I'm not going to spoil
it by presenting the answer here. My instruction count is at
http://tomas.rokicki.com/proppuzzle.txt (but my solution is not
there). See what you can come up with.
Another interesting puzzle is what is the worst case bit-swizzle
in terms of required instructions.
If no one answers this post, I'll just patent the code.
Post Edited (rokicki) : 11/20/2006 11:48:07 PM GMT
Comments
I've PMed you with an answer that matches your instruction count. This is a great puzzle, so I didn't want to spoil it by posting my solution publicly.
-Phil
My "solution" didn't work. (I misunderstood some instructions in the Propeller.) Egg on my face.
Nonetheless, Phil was able to obtain an amazing and short solution that equalled my purported
length. So the puzzle stands, even if I don't.
Good going, Phil!
I cracked up at this one.
Now, to try my hand at this.
You ruined my dinner...... I was too busy trying to figure out something clever.
I figured out how to do it in 6, and then I "thought" I figured out a way to do it in 4 using movi,ror,shl, and a tjnz·and then my head blew up.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Yeah, I'm at the same place. I really feel like I oughtta be able to do this cleverly, but it's gonna wind up being a space-for-time tradeoff.
Tom's got my solution in PM, but I'm sure Phil has totally owned me at this.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
··· REV··· reg,#0··· 'reverse the order of all 32 bits
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
I had six instructions in eight longs. But upon seeing Chip's claim, and reexamining my code, I realized the second extra long isn't necessary. Rokiki has my new solution.
-Phil
Update: Actually, my seven doesn't include source and result registers. Chip, were you counting those in your six?
Post Edited (Phil Pilgrim (PhiPi)) : 11/21/2006 5:28:13 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Post Edited (Chip Gracey (Parallax)) : 11/21/2006 5:31:59 AM GMT
No, I didn't use ANDN. But it would've worked just as well in place of what I did use.
-Phil
Ya know, the rules didn't specify that the code had to work more than once. You could even eliminate that temporary register!
-Phil
I would submit my verson of the code to do it. But I'm sure you guys got me beat.
This posting has solutions below, so stop reading
here if you want to keep working on it.
My original solution was: but then I realized: rev zeros the
high bits, rather than preserving them.
Ouch.
Phil was the first to give a correct
six-instruction solution: I never thought of rotating 'around'!
Cliff posted a similar, but different, solution,
that cut the number of required constants: And then I heard from Chip himself, with this
solution with an elegant use of andn: Phil then figured out how to duplicate Cliff's and
Chip's accomplishment, but in a totally unexpected
way: Wow, it's amazing what you can do with these
instructions!
-Phil
I fixed the tags in your post.
I hope that is okay.
Bean
This is amazing stuff everyone!
KK
http://forums.parallax.com/showthread.php/147580-Any-shorter-way-to-reverse-bytes-in-long-using-only-1-extra-long-(in-pasm)?p=1179932#post1179932