EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Archive::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=621)
-   -   dist calculations (https://www.eqemulator.org/forums/showthread.php?t=10414)

Scorpious2k 11-22-2003 01:15 PM

dist calculations
 
Took a little time to look at the dist calcs... in Mob::Dist it reads

Code:

float Mob::Dist(const Mob &other) {
    double xDiff = other.x_pos - x_pos;       
    double yDiff = other.y_pos - y_pos;       
    double zDiff = other.z_pos - z_pos;
    return sqrt( (xDiff * xDiff)
                    + (yDiff * yDiff)
                    + (zDiff * zDiff) );
}

Which looks about as efficient as you can get without going to assembler... but if you went to that level it might look like

Code:

float Mob::Dist(const Mob &other)
{       
        double xDiff = other.x_pos - x_pos;
        double yDiff = other.y_pos - y_pos;
        double zDiff = other.z_pos - z_pos;
        double calctemp;
        float calcdist;

        __asm
        {
                fld        xDiff
                fmul        xDiff
                fst        calctemp
                fld        yDiff
                fmul        yDiff
                fadd        calctemp
                fst        calctemp
                fld        zDiff
                fmul        zDiff
                fadd        calctemp
                fsqrt
                fst        calcdist
        }
        return calcdist;
}

Which might run in under 500 cycles....

Haven't tested this mind you, just a thought :-)

DannMann99 11-22-2003 01:49 PM

Not to get off topic but... i love the sig lol... kind of like the saying:
"I am a compulsive liar". Makes you wonder.

About the code, you should compile it and see what happens.
~Dan

Scorpious2k 11-22-2003 02:05 PM

Quote:

Originally Posted by DannMann99
i love the sig lol...

Thanks, it fits if you know me. :-)

Quote:

About the code, you should compile it and see what happens.
I compiled it to check and see that it was clean, but we aren't running 5.0 at the moment so I have no way to test it. I wrote it sort of off the top of my head, so I'm not positive its exactly right.

I know that Trumpcard is looking for something like this and thought I would toss it out. If it works well enough, I'll do the rest of the dist calcs if he wants.

Mongrel 11-23-2003 07:56 PM

Why use doubles? I doubt we need 64 bit accuracy anyway, so unless someone is running a 64 bit server, floats are 2x faster and use half the memory space.

Trumpcard 11-23-2003 11:59 PM

You're right, those should be floats...

The original programmer put them in as doubles, we've just never gone back around to fixing it...

Good catch on that, didnt even notice it.. I'll fix those in CVS tonight.

Scorpious2k 11-24-2003 01:30 PM

At the assembly level, it doesn't seem to make a difference since they are all converted to 80 bit values on load. And I don't see a difference in the timing in it either.

I still agree the precision should be only what is needed because it creates overhead to convert in C++.

Eglin 02-06-2004 01:42 AM

Re: dist calculations
 
It is usually better to calculate distance squared, since you eliminate the root op.
Quote:

Originally Posted by Scorpious2k
Took a little time to look at the dist calcs... in Mob::Dist it reads
Code:

float Mob::Dist(const Mob &other) {
    double xDiff = other.x_pos - x_pos;       
    double yDiff = other.y_pos - y_pos;       
    double zDiff = other.z_pos - z_pos;
    return sqrt( (xDiff * xDiff)
                    + (yDiff * yDiff)
                    + (zDiff * zDiff) );
}

Which looks about as efficient as you can get without going to assembler... but if you went to that level it might look like
...<snip>...


kathgar 02-06-2004 05:07 PM

Thats what DistNoRoot and DistNoRootNoZ are for


All times are GMT -4. The time now is 07:30 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.