English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 19 December 2013, 20:43   #1
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
Happy 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
I guess it converts a SIGNED 32 bit value (comes in d0) into a 16 bit value into d0. Is there a better way to do this? The problem is, values greater than $7fff should become $7fff and values smaller than $8000 should become $8000. Right?

ags

Last edited by AGS; 19 December 2013 at 20:51.
AGS is offline  
Old 19 December 2013, 21:39   #2
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
You want signed 32 bit to signed 16 bit conversion with clamping, right?
Thorham is offline  
Old 19 December 2013, 21:44   #3
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
I guess yes, what is clamping?
AGS is offline  
Old 19 December 2013, 22:06   #4
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
Quote:
Originally Posted by AGS View Post
what is clamping?
It's simply setting a variable to a maximum if the variable is larger than that maximum, or setting it to a minimum if the variable is smaller than that minimum (or both). For example, if A=300 and the maximum is 255, then A is set to 255.
Thorham is offline  
Old 19 December 2013, 22:08   #5
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
Yes, thats what I want.
AGS is offline  
Old 19 December 2013, 22:15   #6
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
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
Having to clamp two ways always sucks. For one way clamping to 255 or 0 you can do it in two instructions, I think. For two way byte clamping you can use a table.

I'm curious to see if anyone can come up with anything optimized for this.
Thorham is offline  
Old 19 December 2013, 22:19   #7
thomas
Registered User
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,373
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
thomas is offline  
Old 19 December 2013, 22:23   #8
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
Looks good!
AGS is offline  
Old 19 December 2013, 22:31   #9
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
To thomas:

You made a mistake.
Thorham is offline  
Old 19 December 2013, 23:01   #10
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,265
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
Leffmann is offline  
Old 19 December 2013, 23:04   #11
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
Impressive!
AGS is offline  
Old 19 December 2013, 23:04   #12
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
Nice
Thorham is offline  
Old 19 December 2013, 23:15   #13
pandy71
Registered User
 
Join Date: Jun 2010
Location: PL
Posts: 1,809
Quote:
Originally Posted by Thorham View Post
You want signed 32 bit to signed 16 bit conversion with clamping, right?
Technically correct is http://en.wikipedia.org/wiki/Saturation_arithmetic
pandy71 is offline  
Old 21 December 2013, 16:57   #14
Don_Adan
Registered User
 
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 53
Posts: 1,408
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
Don_Adan is offline  
Old 21 December 2013, 17:07   #15
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
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.
AGS is offline  
Old 21 December 2013, 17:31   #16
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
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
Thorham is offline  
Old 21 December 2013, 17:35   #17
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
Hmm, looks interesting but you lost me. Please try again.
AGS is offline  
Old 21 December 2013, 17:41   #18
Thorham
Computer Nerd

Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 45
Posts: 3,237
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
Thorham is offline  
Old 21 December 2013, 17:42   #19
AGS
XoXo/Tasko Developer
AGS's Avatar
 
Join Date: Dec 2013
Location: Munich
Age: 46
Posts: 450
AH, ok, now i understand.
AGS is offline  
Old 21 December 2013, 17:47   #20
Don_Adan
Registered User
 
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 53
Posts: 1,408
Quote:
Originally Posted by AGS View Post
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?
This is not my idea, but meynaf's (french coder, available on EAB too) trick, he used this trick in own productions for fast range check (-32768 to +32767). Moving word into address register works as move.w and ext.l together.
Don_Adan is offline  
 


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

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 Jump


All times are GMT +2. The time now is 04:17.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Page generated in 0.11666 seconds with 13 queries