14 April 2021, 15:25 | #1 |
Lemon. / Core Design
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 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. |
14 April 2021, 15:50 | #2 |
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> 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? |
14 April 2021, 16:17 | #3 |
Lemon. / Core Design
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 |
14 April 2021, 16:31 | #4 |
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).
|
14 April 2021, 16:48 | #5 | |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
Quote:
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 |
|
14 April 2021, 16:56 | #6 |
WinUAE 4000/40, V4SA
Join Date: Apr 2020
Location: East of Oshawa
Posts: 538
|
|
14 April 2021, 17:01 | #7 |
son of 68k
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 |
14 April 2021, 17:05 | #8 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,409
|
|
14 April 2021, 17:16 | #9 |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
|
14 April 2021, 17:22 | #10 |
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!
|
14 April 2021, 17:34 | #11 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 387
|
|
14 April 2021, 18:11 | #12 | |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
Quote:
I need some rest |
|
14 April 2021, 18:19 | #13 |
Lemon. / Core Design
Join Date: Mar 2016
Location: Tier 5
Posts: 1,211
|
|
14 April 2021, 23:46 | #14 |
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 |
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 |
|
|