2007-05-20 11:03:49 -07:00
|
|
|
// Copyright (C) 2002-2007 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
|
|
|
|
|
|
|
|
#ifndef __FAST_A_TO_F_H_INCLUDED__
|
|
|
|
#define __FAST_A_TO_F_H_INCLUDED__
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "irrMath.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace core
|
|
|
|
{
|
|
|
|
|
|
|
|
const float fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
|
|
|
|
0.f,
|
|
|
|
0.1f,
|
|
|
|
0.01f,
|
|
|
|
0.001f,
|
|
|
|
0.0001f,
|
|
|
|
0.00001f,
|
|
|
|
0.000001f,
|
|
|
|
0.0000001f,
|
|
|
|
0.00000001f,
|
|
|
|
0.000000001f,
|
|
|
|
0.0000000001f,
|
|
|
|
0.00000000001f,
|
|
|
|
0.000000000001f,
|
|
|
|
0.0000000000001f,
|
|
|
|
0.00000000000001f,
|
|
|
|
0.000000000000001f
|
|
|
|
};
|
|
|
|
|
2007-08-13 16:28:44 -07:00
|
|
|
inline u32 strtol10(const char* in, const char** out=0)
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
u32 value = 0;
|
|
|
|
c8 symbol;
|
|
|
|
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
symbol = *in;
|
|
|
|
if ( symbol < '0' || symbol > '9' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
value = ( value * 10 ) + ( symbol - '0' );
|
2007-08-13 16:28:44 -07:00
|
|
|
++in;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
2007-08-13 16:28:44 -07:00
|
|
|
if (out)
|
|
|
|
*out = in;
|
2007-05-20 11:03:49 -07:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Provides a fast function for converting a string into a float,
|
|
|
|
//! about 6 times faster than atof in win32.
|
|
|
|
// If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
|
|
|
|
inline const char* fast_atof_move( const char* c, float& out)
|
|
|
|
{
|
|
|
|
bool inv = false;
|
|
|
|
const char *t;
|
|
|
|
float f;
|
|
|
|
|
|
|
|
if (*c=='-')
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
inv = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//f = (float)strtol(c, &t, 10);
|
2007-08-13 16:28:44 -07:00
|
|
|
f = (float) strtol10 ( c, &c );
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
if (*c == '.')
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
|
|
|
|
//float pl = (float)strtol(c, &t, 10);
|
2007-08-13 16:28:44 -07:00
|
|
|
float pl = (float) strtol10 ( c, &t );
|
2007-05-20 11:03:49 -07:00
|
|
|
pl *= fast_atof_table[t-c];
|
|
|
|
|
|
|
|
f += pl;
|
|
|
|
|
|
|
|
c = t;
|
|
|
|
|
|
|
|
if (*c == 'e')
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
//float exp = (float)strtol(c, &t, 10);
|
2007-06-09 07:32:18 -07:00
|
|
|
bool einv = (*c=='-');
|
|
|
|
if (einv)
|
|
|
|
c++;
|
|
|
|
|
2007-08-13 16:28:44 -07:00
|
|
|
float exp = (float)strtol10(c, &c);
|
2007-06-09 07:32:18 -07:00
|
|
|
if (einv)
|
|
|
|
exp *= -1.0f;
|
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
f *= (float)pow(10.0f, exp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inv)
|
|
|
|
f *= -1.0f;
|
|
|
|
|
|
|
|
out = f;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float fast_atof(const char* c)
|
|
|
|
{
|
|
|
|
float ret;
|
|
|
|
fast_atof_move(c, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace core
|
|
|
|
} // end namespace irr
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|