PDA

View Full Version : Doubles/Floats



toneburst
01-06-2010, 08:26 AM
Quick query.

in the following code;


float4 VertexInterp(isolevel, p1, p2, valp1, valp2)
double isolevel, valp1, valp2;
float4 p1, p2;
{
float mu;
float4 p = 0;

if (fabs(isolevel-valp1) < 0.00001) return(p1);
if (fabs(isolevel-valp2) < 0.00001) return(p2);
if (fabs(valp1-valp2) < 0.00001) return(p1);
mu = isolevel - valp1) / (valp2 - valp1;
p = mix(p1,p2,mu);
return(p);
}
}

is it good to explicitly convert the result of the calculation

isolevel - valp1) / (valp2 - valp1
(where valp1 and valp2 are doubles) into a float, or can it be left as it is?

I presume I'd cast the result as a float like so

mu = (float)(isolevel - valp1) / (valp2 - valp1);
but am wondering if it's necessary or desirable to do so.

a|x
http://machinesdontcare.wordpress.com

toneburst
01-06-2010, 08:37 AM
Sorry, just realised I made some typos in the code above.


mu = isolevel - valp1) / (valp2 - valp1;

should of course have been


mu = (isolevel - valp1) / (valp2 - valp1);

and


mu = (float)(isolevel - valp1) / (valp2 - valp1);

should have been


mu = (float)((isolevel - valp1) / (valp2 - valp1));

(I think).

a|x

dbs2
01-11-2010, 06:26 AM
My guess is yes.
The compiler is extremely unlikely to do any conversion before the write to mu unless you set some flag because it can lose precision. So you will get actual double math with the code the way it is.

A quick search suggests that the intel latency difference between a single precision and a double precision divide is between 15 and 7 cycles depending on the radix of the divider.