fast_atof.h

00001 // Copyright (C) 2002-2005 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
00004 
00005 #ifndef __FAST_A_TO_F_H_INCLUDED__
00006 #define __FAST_A_TO_F_H_INCLUDED__
00007 
00008 #include <stdlib.h>
00009 #include <math.h>
00010 
00011 namespace irr
00012 {
00013 namespace core
00014 {
00015 
00016 const float fast_atof_table[] = {
00017                                                                                 0.f,
00018                                                                                 0.1f,
00019                                                                                 0.01f,
00020                                                                                 0.001f,
00021                                                                                 0.0001f,
00022                                                                                 0.00001f,
00023                                                                                 0.000001f,
00024                                                                                 0.0000001f,
00025                                                                                 0.00000001f,
00026                                                                                 0.000000001f,
00027                                                                                 0.0000000001f,
00028                                                                                 0.00000000001f,
00029                                                                                 0.000000000001f,
00030                                                                                 0.0000000000001f,
00031                                                                                 0.00000000000001f,
00032                                                                                 0.000000000000001f
00033                                                                         };
00034 
00037 inline char* fast_atof_move(char* c, float& out)
00038 {
00039         bool inv = false;
00040         char *t;
00041         float f;
00042 
00043         if (*c=='-')
00044         {
00045                 c++;
00046                 inv = true;
00047         }
00048 
00049         f = (float)strtol(c, &t, 10);
00050 
00051         c = t;
00052 
00053         if (*c == '.')
00054         {
00055                 c++;
00056 
00057                 float pl = (float)strtol(c, &t, 10);
00058                 pl *= fast_atof_table[t-c];
00059 
00060                 f += pl;
00061 
00062                 c = t;
00063 
00064                 if (*c == 'e')
00065                 {
00066                         ++c;
00067                         float exp = (float)strtol(c, &t, 10);
00068                         f *= (float)pow(10.0f, exp);
00069                         c = t;
00070                 }
00071         }
00072 
00073         if (inv)
00074                 f *= -1.0f;
00075 
00076         out = f;
00077         return c;
00078 }
00079 
00082 inline const char* fast_atof_move_const(const char* c, float& out)
00083 {
00084         bool inv = false;
00085         char *t;
00086         float f;
00087 
00088         if (*c=='-')
00089         {
00090                 c++;
00091                 inv = true;
00092         }
00093 
00094         f = (float)strtol(c, &t, 10);
00095 
00096         c = t;
00097 
00098         if (*c == '.')
00099         {
00100                 c++;
00101 
00102                 float pl = (float)strtol(c, &t, 10);
00103                 pl *= fast_atof_table[t-c];
00104 
00105                 f += pl;
00106 
00107                 c = t;
00108 
00109                 if (*c == 'e')
00110                 {
00111                         ++c;
00112                         f32 exp = (f32)strtol(c, &t, 10);
00113                         f *= (f32)powf(10.0f, exp);
00114                         c = t;
00115                 }
00116         }
00117 
00118         if (inv)
00119                 f *= -1.0f;
00120 
00121         out = f;
00122         return c;
00123 }
00124 
00125 
00126 inline float fast_atof(const char* c)
00127 {
00128         float ret;
00129         fast_atof_move_const(c, ret);
00130         return ret;
00131 }
00132 
00133 } // end namespace core
00134 }// end namespace irr
00135 
00136 #endif
00137 

Generated on Sun Dec 2 17:09:57 2007 for Swarm by  doxygen 1.4.6-NO