Advertisement

La Mothe - Optimated vector length-allgorithm

Started by October 26, 2000 06:53 AM
35 comments, last by Wunibald 24 years, 2 months ago
Disclaimer: This has everything to do with this thread!

To avoid improper typecasting
                          float x= -10.5;  int *p = (int *)&x  *p &= 0x7FFFFFFF;     


This is fun What''s next...
Thanx for all your good answers.

Well, I tested the speed of the FastLength-Function yesterday.
Baskuenen is right: this method is out of date. I was a lot faster calculating the exact value by sqrt(X*X, Y*Y, Z*Z) than approximating.
I didn't know the compiler replaces a " * 1024" by "<< 10".
So bitshifting only make sense if I have to multiply by a combined value? (e.g. X*320 == X<<8 + X<<6)? Or does the compiler do that anyway? Well, I'm gonna test it :0).

@Baskuenen: Of course I do length comparisons without the sqrt on both sides :0).

@Neocron: (you are adding to the innaccuracy of the approximation by casting the float/double to int before multiplying by 1024)
>>I know it's not the same. But my tests showed that in one case,
I reach 96.35% and in the other just about the same. So I don't care.


@Null and Void: Yes, I am the same person :0). Really good stuff and people around here on GameDev!



Edited by - Wunibald on October 27, 2000 2:33:55 AM
(Computer && !M$ == Fish && !Bike)
Advertisement
You would only see a performance increase if you were bit shifting an unsigned int instead of an int. The compiler has to do a bunch of stuff behind the scenes to take care of the topmost bit....my professor tested this is one of our classes in the bilinear filtering algorithm....there was a 10% increase in speed when shifting the unsigned int... try testing it for yourself, you should see and increase.
quote: Original post by Anonymous Poster

You would only see a performance increase if you were bit shifting an unsigned int instead of an int. The compiler has to do a bunch of stuff behind the scenes to take care of the topmost bit....my professor tested this is one of our classes in the bilinear filtering algorithm....there was a 10% increase in speed when shifting the unsigned int... try testing it for yourself, you should see and increase.


I've tested it out. I gain no speed doing bitshifting. Be it signed or unsigned values. I compared k * 320 vs. (k<<8) + (k<<6).
Then I did k * 321, which was much slower.
Modern compilers seem to do everything (I use BCB5) :0).

So I can forget about bitshifting to gain speed.

Edited by - Wunibald on October 27, 2000 4:14:06 AM
(Computer && !M$ == Fish && !Bike)
quote: Original post by baskuenen
- Use some asm FPU pipelining optimization. Use the FSQRT function - not the C version.



Actually, I have tried to use the fsqrt function as you suggested with marginal increase in speed (less that 5%) w/o the proper error checking (divide by zero stuff).

You may as well just use the C version because once you add the error checking, it would take just as long to execute. This is of course, unless you don''t use the intrinsic functions, which when compiling with the "maximize speed" or "minimize size" options you will do automatically.

Regards,
Jumpster
Regards,JumpsterSemper Fi
go here
its a nvidia-file bout faster math on pcs, inclusiv sqrt and so..

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Advertisement
Even if you were not using the sqrt on both sides of the equation, its faster to square the other side and compare, than sqrt one side and compare.

Unless you actually need the actual length (which you don't cause you were willing to settle for 96% accuracy ) don't use the sqrt at all! and sqrtf just cast the result as a float.


You can't bit shift floats. And I'm pretty sure X,Y,Z were floats.
suppose float X=1.3445
int x= int(X)<<10; //screws ups
result is not x= 1.3445*1024; result is x= 1 * 1024;
that's why you must
int x = int(X*1024); //uses fixed-point math

...
quote:
          float x= -10.5;  int *p = (int *)&x  *p &= 0x7FFFFFFF;         


ack! now you're deferencing a pointer just to get the abs value! oh wait, no you're not. That work? I would except some complaints from the compiler about using a bitwise and on a pointer...

Edited by - Magmai Kai Holmlor on October 28, 2000 10:23:56 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
all he does is:

create a float:
float x=-10.5;

create a pointer to an int:
int* p;

he saves the adress of the float into the intpointer(just a pointer, so always 4bytes for adresses, not important on what it s pointing)
p=&x

sure vc++ returns with an error, its a float, so you cant do it..

so you have to "convert" the pointer (there is nothing actualy happening, just no error
p=(int*)&x

and then he AND 0x7FFFFFFF, means he switches the first bit to 0, and the first bit of a float specifies if it is positif (then its zero), or negativ(one).. so he just changes the first bit of the float to zero, thats all, and then your float is positiv

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Neocron
I love cute defines...
        // This changes to absolute the value of the parameter// and return the value.#define fabs(x) (*((float*)(*((long*)&x) &= 0x7FFFFFFF)))        


And NO this is NOT LISP This work well under VC++ (without warnings) but you cannot handle the return value under WinNT (memory protection...).

Baskuenen
If you're seeking for fun, you should check out the International Obfuscated C Code Contest... I will participate next year (this year's round is already over) and it is very cool.
Watch out they are often using Watcom and Linux... (I've downloaded DjGPP for compiling most of entries and 90% work).

http://www.ioccc.org/

I just saw that "Poltras" reversed was "Sartlop"... I was sure there was something evil with this nick.

Edited by - Poltras on October 30, 2000 12:41:18 PM
Now I know what I'm made of, and I'm afraid of it...
quote: Original post by Poltras

Baskuenen
If you''re seeking for fun, you should check out the International Obfuscated C Code Contest... I will participate next year (this year''s round is already over) and it is very cool.
Watch out they are often using Watcom and Linux... (I''ve downloaded DjGPP for compiling most of entries and 90% work).


Sounds interresting, but my English is not good enough to understand what "Obfuscated" actually means?!?
I used Watcom a lot back in the old DOS days, but I''m not a huge Unix/Linux fan. I use a 100% free legal student version of MSVC, but I don''t know if I may use this in any contest? Where is this taking place? Europe? The Netherlands?


Anyway, for our own homebrew fabs() version - Poltras''s define seems about the best there is.
Poltras: If you don''t mind - I want to add a small () thing
    #define fabs(x) (*((float*)(*((long*)&(x)) &= 0x7FFFFFFF)))         


Some people like inline functions, but then you have to make multiple functions (float, double, ...). This define handles em all!
Never thought I''d have so much fun with a "simple" function like this one

This topic is closed to new replies.

Advertisement