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 Any thoughts on this ? I know that byte clamping can be easily done with the Sxx instruction and logic operations |
How about
Code:
add.w d1,d0 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 |
@roondar I'll take that one if it works :D
yeah, am expecting Ross to come along with a branchless xor version ;) |
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).
|
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 :D |
Quote:
Well, there's only what, 4,294,967,296 different possible test cases? Easy peasy, should be able to run through them all. :D |
I don't usually do this right after an add, but here it goes :
Code:
add.w d1,d0 |
Quote:
|
Quote:
|
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!
|
Quote:
Yes! Awesome. I need to learn those CC's more thoroughly! |
Quote:
I need some rest :) |
Quote:
Wow!! that's really nice! |
Code:
moveq #0,d3 |
All times are GMT +2. The time now is 13:04. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.