English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. Asm / Hardware (https://eab.abime.net/forumdisplay.php?f=112)
-   -   Clamped addition of 2 signed words (https://eab.abime.net/showthread.php?t=106602)

DanScott 14 April 2021 15:25

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

roondar 14 April 2021 15:50

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? :p

DanScott 14 April 2021 16:17

@roondar I'll take that one if it works :D

yeah, am expecting Ross to come along with a branchless xor version ;)

roondar 14 April 2021 16:31

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).

ross 14 April 2021 16:48

Quote:

Originally Posted by DanScott (Post 1476917)
@roondar I'll take that one if it works :D

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 :D

coldacid 14 April 2021 16:56

Quote:

Originally Posted by roondar (Post 1476922)
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. :D

meynaf 14 April 2021 17:01

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


roondar 14 April 2021 17:05

Quote:

Originally Posted by coldacid (Post 1476930)
Well, there's only what, 4,294,967,296 different possible test cases? Easy peasy, should be able to run through them all. :D

Yup, I can type really quite quickly :p

ross 14 April 2021 17:16

Quote:

Originally Posted by meynaf (Post 1476933)
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!

roondar 14 April 2021 17:22

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!

Jobbo 14 April 2021 17:34

Quote:

Originally Posted by meynaf (Post 1476933)
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!

ross 14 April 2021 18:11

Quote:

Originally Posted by roondar (Post 1476940)
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 :)

DanScott 14 April 2021 18:19

Quote:

Originally Posted by meynaf (Post 1476933)
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!

Thcm 14 April 2021 23:46

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.


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

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.09576 seconds with 11 queries