English Amiga Board Help coding
 Register Amiga FAQ Rules & Help Members List  /  Moderators List Today's Posts Mark Forums Read

 08 August 2006, 17:47 #1 alexh Thalion Webshrine   Join Date: Jan 2004 Location: Oxford Posts: 12,394 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)``` I actually need to Mirror then invert Code: ```0001 (\$1) = 0111 (\$7) 0010 (\$2) = 1011 (\$B) 0100 (\$4) = 1101 (\$D) 1000 (\$8) = 1110 (\$E)``` If there isnt a simple arithmetical technique I'll just implement a switch statement in ASM. 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 Npl Registered User   Join Date: Aug 2004 Location: Vienna / Austria Age: 41 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 Codetapper 2 contact me: email only!   Join Date: May 2001 Location: Auckland / New Zealand Posts: 3,164 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 girv Registered User   Join Date: Aug 2004 Location: Northern Ireland Posts: 941 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
alexh
Thalion Webshrine

Join Date: Jan 2004
Location: Oxford
Posts: 12,394
Quote:
 Originally Posted by Codetapper I'm not sure what you mean by one hot, do you mean they are always one bit set?
Oops. Sorry for not explaining. One-Hot (in this case) means that only one bit can be set at once. Think of each bit as being a mutually exclusive flag.

Quote:
 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.
The data is an input, an interrupt status register. You dont know which of the four bits is set. It could be any of them.

Quote:
 But if that isn't the case, a 256 byte table that you generate is probably the best way to go
Because it is "one-hot" it's effectively just a 4-entry lookup table. But I dont have any memory (other than instruction memory) so I cannot implement a true lookup table.

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
girv
Registered User

Join Date: Aug 2004
Location: Northern Ireland
Posts: 941
Quote:
 Originally Posted by alexh The data is an input, an interrupt status register. You dont know which of the four bits is set. It could be any of them. Because it is "one-hot" it's effectively just a 4-entry lookup table. But I dont have any memory (other than instruction memory) so I cannot implement a true lookup table. But I was hoping there was a simple arithmetic operation so that I wouldnt have to do any SUBI+BCC instructions.
In your case you'd need only a 9 byte lookup table, using the 4 bits of input information as an index into a byte table holding the output value you need. Can't you spare 9 bytes? It would probably take more in instruction bytes to code an algorithm to do the same job!

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
moveq  #0,d0 ; set only bit d1 in d0
bset   d1,d0
not.b  d0    ; invert d0 to get final value```
I now sit back and wait for the Obvious Mistake Police to pounce.

 09 August 2006, 16:00 #7 Galahad/FLT Going nowhere   Join Date: Oct 2001 Location: United Kingdom Age: 46 Posts: 7,644 What machine is this for where memory is so critical?
 09 August 2006, 16:36 #8 alexh Thalion Webshrine   Join Date: Jan 2004 Location: Oxford Posts: 12,394 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 Photon Moderator   Join Date: Nov 2004 Location: Eksjö / Sweden Posts: 4,819 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 alexh Thalion Webshrine   Join Date: Jan 2004 Location: Oxford Posts: 12,394 Top man.
 09 August 2006, 21:22 #11 Npl Registered User   Join Date: Aug 2004 Location: Vienna / Austria Age: 41 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``` Photons code of course will do the same, but I consider avoiding branches an art Dunno which one would be faster/smaller on m68k
 09 August 2006, 21:31 #12 Psygore Moderator   Join Date: Jan 2002 Location: France Posts: 484 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 AGN 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:``` only 4 and a half instruction - did I fit in cycles? ps. send more puzzles like this one ;)
 14 August 2006, 01:40 #14 Photon Moderator   Join Date: Nov 2004 Location: Eksjö / Sweden Posts: 4,819 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 alexh Thalion Webshrine   Join Date: Jan 2004 Location: Oxford Posts: 12,394 @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)

 Similar Threads Thread Thread Starter Forum Replies Last Post Amiga Forever Coders. General 42 31 January 2012 02:58 Yesideez Coders. General 15 23 May 2010 00:13 pmc Coders. General 55 16 November 2009 21:45 Thorham Coders. General 6 05 March 2008 08:38 Seoman Coders. General 8 08 November 2007 13:34

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home News Main     Amiga scene     Retrogaming General Discussion     Nostalgia & memories Support     New to Emulation or Amiga scene         Member Introductions     support.WinUAE     support.WinFellow     support.OtherUAE     support.FS-UAE         project.AmigaLive     support.Hardware         Hardware mods         Hardware pics     support.Games     support.Demos     support.Apps     support.Amiga Forever     support.Amix     support.Other Requests     request.UAE Wishlist     request.Old Rare Games     request.Demos     request.Apps     request.Modules     request.Music     request.Other     Looking for a game name ?     Games images which need to be WHDified abime.net - Hall Of Light     HOL news     HOL suggestions and feedback     HOL data problems     HOL contributions abime.net - Amiga Magazine Rack     AMR news     AMR suggestions and feedback     AMR data problems     AMR contributions abime.net - Home Projects     project.Amiga Lore     project.EAB     project.IRC     project.Mods Jukebox     project.Wiki abime.net - Hosted Projects     project.aGTW     project.APoV     project.ClassicWB     project.Jambo!     project.Green Amiga Alien GUIDES     project.Maptapper     project.Sprites     project.WinUAE - Kaillera Other Projects     project.Amiga Demo DVD     project.Amiga Game Factory     project.CARE     project.EAB File Server     project.CD32 Conversion     project.Game Cover Art         GCA.Feedback and Suggestions         GCA.Work in Progress         GCA.Cover Requests         GCA.Usefull Programs         GCA.Helpdesk     project.KGLoad     project.MAGE     project.Missing Full Shareware Games     project.SPS (was CAPS)     project.TOSEC (amiga only)     project.WHDLoad         project.Killergorilla's WHD packs Misc     Amiga websites reviews     MarketPlace         Swapshop     Kinky Amiga Stuff     Collections     EAB's competition Coders     Coders. General         Coders. Releases         Coders. Tutorials     Coders. Asm / Hardware     Coders. System         Coders. Scripting         Coders. Nextgen     Coders. Language         Coders. C/C++         Coders. AMOS         Coders. Blitz Basic     Coders. Contest         Coders. Entries Creation     Graphics         Graphics. Work In Progress         Graphics. Finished Work         Graphics. Tutorials     Music         Music. Work In Progress         Music. Finished Work         Music. Tutorials Off Topic     OT - General     OT - Entertainment     OT - Sports     OT - Technical     OT - Gaming

All times are GMT +2. The time now is 20:30.

 -- EAB3 skin ---- EAB2 skin ---- Mobile skin Archive - Top