08 August 2006, 17:47 | #1 |
Thalion Webshrine
Join Date: Jan 2004
Location: Oxford
Posts: 14,448
|
Help coding
Is there a simple way to Mirror data?
My input data sets are very confined, they are just "one-hot". Meaning that only 1-bit can be set at once. Code:
0001 ($1) = 1000 ($8) 0010 ($2) = 0100 ($4) 0100 ($4) = 0010 ($2) 1000 ($8) = 0001 ($1) Code:
0001 ($1) = 0111 ($7) 0010 ($2) = 1011 ($B) 0100 ($4) = 1101 ($D) 1000 ($8) = 1110 ($E) I have the usual MOVE, NOT, AND, XOR, OR, SL, SR opcodes etc. Last edited by alexh; 09 August 2006 at 15:12. |
08 August 2006, 23:03 | #2 |
Registered User
Join Date: Aug 2004
Location: Vienna / Austria
Age: 44
Posts: 257
|
Im pretty sure they aint an easy way. I`d use a 256-entry table with inverted bytes.
|
08 August 2006, 23:17 | #3 |
2 contact me: email only!
Join Date: May 2001
Location: Auckland / New Zealand
Posts: 3,187
|
I'm not sure what you mean by one hot, do you mean they are always one bit set? If that is the case, you can get around the table problem, as you know which bit is set so can mirror it by setting the mirrored bit straight away then doing the not operation on it.
But if that isn't the case, a 256 byte table that you generate is probably the best way to go, and you can fill in the mirrored not'd version at the same time meaning a byte lookup will be very quick. |
09 August 2006, 02:45 | #4 |
Mostly Harmless
Join Date: Aug 2004
Location: Northern Ireland
Posts: 1,149
|
I remember asking this question years ago on csa.games I think. I wanted to reverse the order of bits in a byte or word. The best anyone came up with was a lookup table, just like the others have said already.
Also, avoid the switch statement approach if performance is important. |
09 August 2006, 15:03 | #5 | |||
Thalion Webshrine
Join Date: Jan 2004
Location: Oxford
Posts: 14,448
|
Quote:
Quote:
Quote:
But I was hoping there was a simple arithmetic operation so that I wouldnt have to do any SUBI+BCC instructions. Last edited by alexh; 09 August 2006 at 15:42. |
|||
09 August 2006, 15:54 | #6 | |
Mostly Harmless
Join Date: Aug 2004
Location: Northern Ireland
Posts: 1,149
|
Quote:
If you're on 020-040 you could look at the BFFFO (Bit Field Find First One) instruction. IIRC it exists on 060 as well but is emulated. I don't know the exact syntax of BFFFO but your code would be something like this. Code:
; > d0.l = input status value bfffo d0{0:31},d1 ; d1 = bit position of first 1 bit neg.b d1 ; get d1 = 3-d1 addq.b #3,d1 moveq #0,d0 ; set only bit d1 in d0 bset d1,d0 not.b d0 ; invert d0 to get final value |
|
09 August 2006, 16:00 | #7 |
Going nowhere
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 9,016
|
What machine is this for where memory is so critical?
|
09 August 2006, 16:36 | #8 |
Thalion Webshrine
Join Date: Jan 2004
Location: Oxford
Posts: 14,448
|
It's a PicoController inside a hardware RAID5 controller that I designed (using the 68k instructions as a template)
It has no RAM at all. Just 16*48-bit registers and a 512 entry instruction RAM. One of the team want to do something it wasnt supposed to do and is looking for a workaround using the minimum number of instructions and the minimum number of cycles. Had I known they would ever have wanted to do anything like this I would have added it to the load instruction. It would have added little more than 4 x 2:1 mux's and 4 wires (i.e. nothing!) |
09 August 2006, 18:29 | #9 |
Moderator
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,650
|
For this example, you can do it without bit mirroring, by looking at the values:
Code:
Flip: ;d0=4-bit input addq.b #6,d0 cmp.b #7,d0 beq.s .done cmp.b #14,d0 beq.s .done addq.b #3,d0 .done: rts |
09 August 2006, 19:05 | #10 |
Thalion Webshrine
Join Date: Jan 2004
Location: Oxford
Posts: 14,448
|
Top man.
|
09 August 2006, 21:22 | #11 |
Registered User
Join Date: Aug 2004
Location: Vienna / Austria
Age: 44
Posts: 257
|
You could use x = 0x8 / x. but thats likely not supported or slow
If a variable shift is availabe, you could use a special bitmask v = 111100010000 and shift that to the right v=v>>x, then add x>>2 to the result x = x>>2+v. The low 4 bits contain the mirrored pattern. Im not fluent at m68k assembly, I added some C-style remarks to make it clear Code:
Flip: ;d0=4-bit input move #F10, d1 ;d1 = 0xF10 lsr.w d0,d1 ; d1 = (d1 >> d0) lsr.b #2, d0 ; d0 = (d0>>2) add d1,d0 ; d0 = d0 + d1 andi.b #7, d0 ; d0 = d0&0x7 rts Dunno which one would be faster/smaller on m68k |
09 August 2006, 21:31 | #12 |
Moderator
Join Date: Jan 2002
Location: France
Posts: 491
|
Here a 4bit mirroring:
Code:
input : d0 output : d1 roxr.b #1,d0 addx.b d1,d1 roxr.b #1,d0 addx.b d1,d1 roxr.b #1,d0 addx.b d1,d1 roxr.b #1,d0 addx.b d1,d1 not.b d1 ;invert value and.b #%1111,d1 |
11 August 2006, 00:37 | #13 |
Registered User
Join Date: Aug 2004
Location: Poland
Posts: 142
|
Optimized Photon version:
Code:
d0 - input d1 - output add.b #6,d0 move.l d0,d1 and.b #4,d0 bne done add.b #3,d1 done: ps. send more puzzles like this one ;) |
14 August 2006, 01:40 | #14 |
Moderator
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,650
|
Great! Or even:
Code:
addq.b #6,d0 btst #2,d0 bne.s .done addq.b #3,d0 .done: |
15 August 2006, 09:24 | #15 |
Thalion Webshrine
Join Date: Jan 2004
Location: Oxford
Posts: 14,448
|
@AGN & Photon
Cool. I can do that. I dont have the instruction BTST in my hardware but unlike the 68k I do have 3 operands in my instructions (src1, src2, dest) Have to see which is faster. Avoiding branches is good in 99% of situations, but using delay slots can mean zero penalty in some situations. (This isnt a situation where you can use a delay slot on it's own but maybe the other code fits around it) Last edited by alexh; 15 August 2006 at 09:29. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Any of you Coding on Amiga? | Amiga Forever | Coders. General | 42 | 31 January 2012 02:58 |
Coding a bootblock | Yesideez | Coders. General | 15 | 23 May 2010 00:13 |
What gives you coding inspiration? | pmc | Coders. General | 55 | 16 November 2009 21:45 |
Need help with audio coding. | Thorham | Coders. General | 6 | 05 March 2008 08:38 |
Coding with Devpac 3.18 | Seoman | Coders. General | 8 | 08 November 2007 13:34 |
|
|