DaviesX
May 7th, 2012, 05:36 AM
I have seen a tip that said the standard type convertion of float to in is slow, is that true for gcc ?
If so, how slow it will be ?
They said this code can make the convertion become faster.
#include <stdio.h>
#include <time.h>
inline int FLoatToInt ( float fNumber )
{
int ival = *(int *)&fNumber;
int mantissa = (ival & 0x07fffff) | 0x800000;
int exponent = 150 - ((ival >> 23) & 0xff);
if (exponent < 0)
return mantissa << -exponent;
else
return mantissa >> exponent;
}
int main()
{
int begin = clock ();
float i;
int integer;
for ( i = 0; i < 100000.0f; i ++ )
{
for ( float j = i; j < i + 1.0f; j += 0.01 )
{
integer = FLoatToInt ( j );
// integer = (int)j;
}
}
int end = clock ();
printf ( "%d %d", (int)integer, end - begin );
return 0;
}
if it is faster, why doesn't someone who made compiler use this method ? Is there any downside using the code ?:)
If so, how slow it will be ?
They said this code can make the convertion become faster.
#include <stdio.h>
#include <time.h>
inline int FLoatToInt ( float fNumber )
{
int ival = *(int *)&fNumber;
int mantissa = (ival & 0x07fffff) | 0x800000;
int exponent = 150 - ((ival >> 23) & 0xff);
if (exponent < 0)
return mantissa << -exponent;
else
return mantissa >> exponent;
}
int main()
{
int begin = clock ();
float i;
int integer;
for ( i = 0; i < 100000.0f; i ++ )
{
for ( float j = i; j < i + 1.0f; j += 0.01 )
{
integer = FLoatToInt ( j );
// integer = (int)j;
}
}
int end = clock ();
printf ( "%d %d", (int)integer, end - begin );
return 0;
}
if it is faster, why doesn't someone who made compiler use this method ? Is there any downside using the code ?:)