Fast way of getting absolute value
Hi guys!
Wonder what you guys think of my code. Its purpose is to convert the value in register d7 to its absolute value: Code:
swap d7 ; fetch x-acceleration |
I've got a macro I stole from Kalm's on another forum:
Code:
;(Kalms explained the method itself in the other thread.) Now, to use that you'd simply Edit: Ah wait, it's the abs of two values, not sure this is applicable, but left here for others :) |
Quote:
|
I will use next code, but this is not fastest.
Code:
|
Quote:
Quote:
|
When you are dealing with short code sequences a lot depends on how they are interfacing with the rest of the code (extreme example: super fast pixel draw but you call it with movem/jsr/movem for each pixel), and this opens up several questions...
Would it be faster to load each word individually to get rid of swap+tst? Is source addressing mode simple enough? Do you need the result as individual words or as a longword (simple partial parallelization: tst.l if ccr not set by move, smi, extb.l, tst.w, smi, ext.w, eor.l, so 1 less eor)? Assuming this is for 020+, so branching should be avoided... |
Quote:
0.w = x-acceleration (<0 = move left, >0 = move right) 2.w= y-acceleration (< 0 = move up, > 0 = move down) So one longword-read can fetch both values. I tried to avoid absolute value conversion by simply comparing x-acc and y-acc, but cannot get it to work faultlessly. This is the complete code, with absolute conversion: Code:
krakenSmall |
Quote:
Edit: corrected typos - changed "scc" to "scs"! Code:
move.l objectListAcc(a2),d7 ; get x- and y-acceleration |
Quote:
|
Quote:
Just try it for positive y values greater than positive x values and you will have problems .. --- For a bare 68k I would have no doubts and would simply do: Code:
move.w objectListAcc+2(a2),d7 Code:
move.l objectListAcc(a2),d7 |
Quote:
|
Quote:
Gah - I said beware of typos - my "scc"s should be "scs"! With that correction made, would it work, or is there still something I'm missing? |
Quote:
|
Quote:
I also prefer to avoid branches (there are many examples of how I feel about it on the forum), but in some cases they are advantageous, especially on slower machines. Quote:
orbrings you back to the wrong result. Quote:
|
Quote:
Yeah, I see it now - I think I'm still confused about exactly how the carry flag works on 68k! |
OK this one works, I think:
Code:
move.l objectListAcc(a2),d7 ; get x- and y-acceleration |
Quote:
EDIT: The problem with your algorithm is that in any case (for both the addition and the subtraction) you are doing it on signed values, while what you are asked to do is to do it for unsigned values. That's why in my algorithm I have to first change it, if needed, for both, and then make a comparison. |
Quote:
This testbench tests all eight permutations of +/- Lo/Hi for each operand, and gives the expected result? (0xaa in D1 at the end, or 0x55 if you reverse HI and LO) The inputs do have to be within the range +/- 16383, however. Code:
ORG $1000 |
I grab what Buzzybee wrote and give an example.
0.w = x-acceleration (<0 = move left, >0 = move right) 2.w= y-acceleration (< 0 = move up, > 0 = move down) objectListAcc dc.w a,b ;x-acceleration,y-acceleration Where b>a, generic low values, positive. set d6=0 if object moves up/down - y-acceleration>x-acceleration set d6=$ff if object moves left/right y-acceleration<x-acceleration I'm expecting in this case d6=0 because y-acceleration>x-acceleration (b>a) Now step by step in your algorithm. move.l objectListAcc(a2),d7 ; get x- and y-acceleration d7.h=a; d7.w=b move.w d7,d0 ; fetch y-acceleration in world d0.w=b move.w viewPosition+vPyAccConvertWorldToView(pc),d6 sub.w d6,d0 ; convert to y-acceleration in view we do not care of this swap d7 d7.w=a move.w d7,d6 d6.w=a add.w d0,d6 d6=a+b=positive value smi d6 d6.b=0 sub.w d0,d7 d7=a-b=negative value (as b>a) smi d7 d7.b=$FF eor.b d7,d6 d6=$00^$ff=$ff <- wrong! I don't know how else to describe it to you :D |
Quote:
OK, in that case, switch the operands: Code:
move.l objectListAcc(a2),d7 ; get x- and y-acceleration Code:
move.l objectListAcc(a2),d7 ; get x- and y-acceleration |
All times are GMT +2. The time now is 13:00. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.