How to do fast floating-point multiplication by 1, 0, and -1?

Started by
15 comments, last by technobot 20 years, 8 months ago
Here''s a suggestion: write nine functions (one for each possible 3D vector with component values of -1, 0, and 1). You might gain enough from the faster functions to overcome the branch overhead.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Advertisement
Maybe you could do something with template specializations?

template<float TNum> void DoStuff( float& f ){}template<> void DoStuff<0.0f>( float &f ){    f = 0;}template<> void DoStuff<1.0f>( float& f ){    f = f;}template<> void DoStuff<-1.0f>( float& f ){    f = -f;}



:::: [ Triple Buffer V2.0 ] ::::

[edited by - ifoobar on July 29, 2003 10:49:49 AM]
[size=2]aliak.net
quote:Original post by Sneftel
1. Will the variables that are -1, 0, or 1 be set to the constants -1/0/1, or will mathematical calculations produce these numbers?

2. In the equations you just showed us, which are the variables that are 0/1/-1?


1. constants
2. the bla''s with even indices

davepermen: he was talking about, and I quote, "C implementations on the Intel optimizing compiler running on a Pentium 3". Sounds very much like a software implementation to me.... Nevertheless, what you''re saying does indeed make sence for me.

JohnBolton: calling the functions would probably be more expensive... (yes, I know, I know -- profile first, talk later )

IFooBar: no templates (of this sort, at least) in Delphi.

Michael K.,
Co-designer and Graphics Programmer of "The Keepers"


We come in peace... surrender or die!
Michael K.
quote:Original post by xMcBaiNx
that and... NEVER TEST FLOATS FOR EQUALITY. :D


Why the hell not? I know you could get problems with not being exactly that number you''re testing, but then you could resort to checking a range. An equality test is just a range test with a range size of 0. You do have to worry about the type, float constants might not mix with double variables well. What''s wrong with that?
My stuff.Shameless promotion: FreePop: The GPL god-sim.
quote:Original post by Doc
quote:Original post by xMcBaiNx
that and... NEVER TEST FLOATS FOR EQUALITY. :D


Why the hell not? I know you could get problems with not being exactly that number you''re testing, but then you could resort to checking a range. An equality test is just a range test with a range size of 0. You do have to worry about the type, float constants might not mix with double variables well. What''s wrong with that?


Beacause, basically 0.0 != -0.0. They are different numbers.

Si if you write if( x == 0.0f) then when you get x=-0.0f coming along your if will false which is almost certainly not desired behaviour.

Technically, taking the IF route, you''d probably want to do something like the following:

#define SMALL_ASS_NUMBER 0.00001inline double LameMul(double x, double y){  if(x < -SMALL_ASS_NUMBER) return -y;  if(x >  SMALL_ASS_NUMBER) return  y;  return 0;} 


That''d do the trick, and would account for float screwups.

(Keep in mind I randomly picked the small constant value, because I don''t remember what the standard one should be. It''s a different suggested value for doubles and floats).

Josh
That''s also two branches and a function call. That''s awful performance, even if it''s inline.

I''d just do a fmul. Chances are that more than a few bit ops would be slower than one fmul. Though, the right answer is to try different things and profile.

I like pie.
[sub]My spoon is too big.[/sub]

This topic is closed to new replies.

Advertisement