English Amiga Board


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

 
 
Thread Tools
Old 14 April 2021, 15:25   #1
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
Clamped addition of 2 signed words

Had a quick looky on t'interwebs... couldn't find an answer easily to this.

Say I have 2 signed words in d0 & d1 (can be using full range -32768 to 32767), and I want to add them together and clamp to full word range (-32768 to 32767)

Is there a snazzy bit-wise trick way of doing this, without having to:

Code:
     ext.l   d0
     ext.l   d1
     add.l  d1,d0
     cmp.l #32767,d0
     ble.s   .NotMax
     move.l #32767,d0
     bra.s  .NotMin
.NotMax
     cmp.l #-32768,d0
     bge.s   .NotMin
     move.l #32768,d0
.NotMin
It doesn't matter about the top word of the register containing crap on exit, as only the bottom word is used later

Any thoughts on this ? I know that byte clamping can be easily done with the Sxx instruction and logic operations

Last edited by DanScott; 14 April 2021 at 15:31.
DanScott is offline  
Old 14 April 2021, 15:50   #2
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,409
How about
Code:
		add.w	d1,d0
		bvc.s	.done
		bmi.s	.positive
		move.w	#-32768,d0
		bra.s	.done
.positive	move.w	#32767,d0
.done		<etc>
Note: untested
Edit: also, I'm now waiting on one of the true wizards to come along with a branchless and much faster version. Where's ross when you need him?
roondar is offline  
Old 14 April 2021, 16:17   #3
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
@roondar I'll take that one if it works

yeah, am expecting Ross to come along with a branchless xor version
DanScott is offline  
Old 14 April 2021, 16:31   #4
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,409
I couldn't help myself and tested it just now. It does indeed seem to work correctly in all cases (assuming I haven't missed one).
roondar is offline  
Old 14 April 2021, 16:48   #5
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by DanScott View Post
@roondar I'll take that one if it works

yeah, am expecting Ross to come along with a branchless xor version
ahaha, this made my day

In fact I was thinking about how to solve this problem, but today doesn't seem to be the right day (I'm not in the mood ).

[off-topic]
It happens when you are gentle and helpful to everyone and you get in return some shit shoveled on you.
[/off-topic]


Luckily you cheered me up a bit
ross is offline  
Old 14 April 2021, 16:56   #6
coldacid
WinUAE 4000/40, V4SA
 
coldacid's Avatar
 
Join Date: Apr 2020
Location: East of Oshawa
Posts: 538
Quote:
Originally Posted by roondar View Post
I couldn't help myself and tested it just now. It does indeed seem to work correctly in all cases (assuming I haven't missed one).

Well, there's only what, 4,294,967,296 different possible test cases? Easy peasy, should be able to run through them all.
coldacid is offline  
Old 14 April 2021, 17:01   #7
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
I don't usually do this right after an add, but here it goes :
Code:
 add.w d1,d0
 bvc.s .done
 spl d0
 ext.w d0
 eori.w #$7fff,d0
.done
meynaf is offline  
Old 14 April 2021, 17:05   #8
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,409
Quote:
Originally Posted by coldacid View Post
Well, there's only what, 4,294,967,296 different possible test cases? Easy peasy, should be able to run through them all.
Yup, I can type really quite quickly
roondar is offline  
Old 14 April 2021, 17:16   #9
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by meynaf View Post
I don't usually do this right after an add, but here it goes :
Code:
 add.w d1,d0
 bvc.s .done
 spl d0
 ext.w d0
 eori.w #$7fff,d0
.done
Nice!
ross is offline  
Old 14 April 2021, 17:22   #10
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,409
I really need to start looking into Sxx more often. I fear I've rather underutilized these instructions so far and seeing the above example that is clearly a shame!
roondar is offline  
Old 14 April 2021, 17:34   #11
Jobbo
Registered User
 
Jobbo's Avatar
 
Join Date: Jun 2020
Location: Druidia
Posts: 387
Quote:
Originally Posted by meynaf View Post
I don't usually do this right after an add, but here it goes :
Code:
 add.w d1,d0
 bvc.s .done
 spl d0
 ext.w d0
 eori.w #$7fff,d0
.done

Yes! Awesome. I need to learn those CC's more thoroughly!
Jobbo is offline  
Old 14 April 2021, 18:11   #12
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by roondar View Post
I really need to start looking into Sxx more often. I fear I've rather underutilized these instructions so far and seeing the above example that is clearly a shame!
I was explaining to someone, just yesterday, the advantages of the Scc instructions, but I'm don't even remember who it was..

I need some rest
ross is offline  
Old 14 April 2021, 18:19   #13
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
Quote:
Originally Posted by meynaf View Post
I don't usually do this right after an add, but here it goes :
Code:
 add.w d1,d0
 bvc.s .done
 spl d0
 ext.w d0
 eori.w #$7fff,d0
.done

Wow!! that's really nice!
DanScott is offline  
Old 14 April 2021, 23:46   #14
Thcm
Registered User
 
Join Date: Dec 2011
Location: Gummersbach
Posts: 18
Code:
 moveq #0,d3
 move.w #$7fff,d2
 
 add.w d1,d0
 bvc.s .done
 move.w d2,d0
 addx.w d3,d0
.done
Adopted from my 6502 mixing routines. Should work, but untested.
Thcm 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
Klondike Deluxe - Renames & Addition DamienD HOL data problems 2 15 December 2019 05:26
Amiga ACA500+ addition and Workbench install jcrubin support.Hardware 1 09 April 2018 17:04
vbcc 0.9e signed multiplication issue? dalton Coders. C/C++ 3 09 January 2018 08:47
64 bit signed multiply cdoty Coders. General 2 16 December 2007 12:24
New category addition for the HOL?? Steve HOL suggestions and feedback 4 29 July 2007 01:04

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 05:51.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09272 seconds with 15 queries