19 December 2013, 20:43 | #1 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Best Way to Convert 32-bit Signed Value to 16 Bit?
Hi,
I need an optimisation for this: Code:
; convert d0 from 32 to 16 bit move.l #$7fff,d1 cmp.l d1,d0 blt .low_d0 move.w d1,d0 bra .ready .low_d0 cmp.l #$ffff8000,d0 bgt .ready move.w #$8000,d0 .ready ags Last edited by AGS; 19 December 2013 at 20:51. |
19 December 2013, 21:39 | #2 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
You want signed 32 bit to signed 16 bit conversion with clamping, right?
|
19 December 2013, 21:44 | #3 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
I guess yes, what is clamping?
|
19 December 2013, 22:06 | #4 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
|
19 December 2013, 22:08 | #5 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Yes, thats what I want.
|
19 December 2013, 22:15 | #6 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
Then you could do it like this, although it's not really optimized, only the program flow is improved:
Code:
move.l #$7fff,d1 cmp.l d1,d0 bgt .l1 move.l #$ffff8000,d1 cmp.l d1,d0 blt .l1 move.w d0,d1 .l1 move.w d1,d0 I'm curious to see if anyone can come up with anything optimized for this. |
19 December 2013, 22:19 | #7 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 7,000
|
How about this:
Code:
tst.l d0 bmi .minus tst.w d0 bpl .ok move.w #$7fff,d0 bra .ok .minus tst.w d0 bmi .ok move.w #$8000,d0 .ok |
19 December 2013, 22:23 | #8 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Looks good!
|
19 December 2013, 22:31 | #9 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
To thomas:
You made a mistake. |
19 December 2013, 23:01 | #10 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
Code:
move.l #-$8000, d1 cmp.l d1, d0 blt .1 not.l d1 cmp.l d1, d0 ble .2 .1 move.w d1, d0 .2 |
19 December 2013, 23:04 | #11 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Impressive!
|
19 December 2013, 23:04 | #12 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
Nice
|
19 December 2013, 23:15 | #13 | |
Registered User
Join Date: Jun 2010
Location: PL?
Posts: 2,766
|
Quote:
|
|
21 December 2013, 16:57 | #14 |
Registered User
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 55
Posts: 1,975
|
Perhaps the fastest version is version based on meynaf's trick.
Code:
move.w D0,A0 cmp.l D0,A0 beq.b OK move.w #$8000,D0 tst.l D0 bmi.b OK not.w D0 OK |
21 December 2013, 17:07 | #15 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
I am impressed that this part of code of mine could really be optimized so good. I thought of solutions, but did not get the answer alone. Don, your Trick is faster as it only moves a word number instead of a long, and does this only when necessary. The previous solution did this always. What is actually "Meynaf's TRick"? What happens when you move a .w into an address register?
Last edited by AGS; 21 December 2013 at 17:14. |
21 December 2013, 17:31 | #16 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
I think it's this:
Code:
; to see if a value is between $FFFF8000 and $7FFF, better put it in An reg : cmpa.w a0,a0 ; cmp with a0.w extended to .l, and a0.l |
21 December 2013, 17:35 | #17 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Hmm, looks interesting but you lost me. Please try again.
|
21 December 2013, 17:41 | #18 |
Computer Nerd
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,762
|
The trick is based on the fact that when you move a word value into an address register it's automatically sign extended to a long word. For the rest I haven't figured it out, either
|
21 December 2013, 17:42 | #19 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
AH, ok, now i understand.
|
21 December 2013, 17:47 | #20 | |
Registered User
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 55
Posts: 1,975
|
Quote:
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
32-bit access on 16-bit bus? | NorthWay | Coders. Asm / Hardware | 7 | 04 September 2013 00:46 |
REQ: 17-Bit Artwork 2 (1988-04)(17-Bit Software) | Sea7 | request.Demos | 5 | 13 May 2011 01:07 |
8 bit to optimized 6 bit palette histogram improvements needed | NovaCoder | Coders. General | 0 | 14 April 2011 02:13 |
My A500 is dying bit by bit :( | Old Fool | support.Hardware | 3 | 03 July 2009 17:12 |
64 bit signed multiply | cdoty | Coders. General | 2 | 16 December 2007 12:24 |
|
|