1 // Copyright (C) 2002-2005 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
3 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
4 // ----------------------------------------------------------------------------
5 // Copyright (C) 2014
6 //              David Freese, W1HKJ
7 //
8 // This file is part of fldigi
9 //
10 // fldigi is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // fldigi is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 // ----------------------------------------------------------------------------
23 
24 
25 #ifndef __FAST_A_TO_F_H_INCLUDED__
26 #define __FAST_A_TO_F_H_INCLUDED__
27 
28 #include <stdlib.h>
29 #include <math.h>
30 
31 namespace irr
32 {
33 namespace core
34 {
35 
36 // Disable the use of powf(3) on mingw32 because it breaks cross compilation
37 // on Debian unstable.
38 #ifndef __MINGW32__
39 const float fast_atof_table[] =	{
40 										0.f,
41 										0.1f,
42 										0.01f,
43 										0.001f,
44 										0.0001f,
45 										0.00001f,
46 										0.000001f,
47 										0.0000001f,
48 										0.00000001f,
49 										0.000000001f,
50 										0.0000000001f,
51 										0.00000000001f,
52 										0.000000000001f,
53 										0.0000000000001f,
54 										0.00000000000001f,
55 										0.000000000000001f
56 									};
57 
58 //! Provides a fast function for converting a string into a float,
59 //! about 6 times faster than atof in win32.
60 // If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
fast_atof_move(char * c,float & out)61 inline char* fast_atof_move(char* c, float& out)
62 {
63 	bool inv = false;
64 	char *t;
65 	float f;
66 
67 	if (*c=='-')
68 	{
69 		c++;
70 		inv = true;
71 	}
72 
73 	f = (float)strtol(c, &t, 10);
74 
75 	c = t;
76 
77 	if (*c == '.')
78 	{
79 		c++;
80 
81 		float pl = (float)strtol(c, &t, 10);
82 		pl *= fast_atof_table[t-c];
83 
84 		f += pl;
85 
86 		c = t;
87 
88 		if (*c == 'e')
89 		{
90 			++c;
91 			float exp = (float)strtol(c, &t, 10);
92 			f *= (float)pow(10.0f, exp);
93 			c = t;
94 		}
95 	}
96 
97 	if (inv)
98 		f *= -1.0f;
99 
100 	out = f;
101 	return c;
102 }
103 
104 //! Provides a fast function for converting a string into a float,
105 //! about 6 times faster than atof in win32.
106 // If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
fast_atof_move_const(const char * c,float & out)107 inline const char* fast_atof_move_const(const char* c, float& out)
108 {
109 	bool inv = false;
110 	char *t;
111 	float f;
112 
113 	if (*c=='-')
114 	{
115 		c++;
116 		inv = true;
117 	}
118 
119 	f = (float)strtol(c, &t, 10);
120 
121 	c = t;
122 
123 	if (*c == '.')
124 	{
125 		c++;
126 
127 		float pl = (float)strtol(c, &t, 10);
128 		pl *= fast_atof_table[t-c];
129 
130 		f += pl;
131 
132 		c = t;
133 
134 		if (*c == 'e')
135 		{
136 			++c;
137 			f32 exp = (f32)strtol(c, &t, 10);
138 			f *= (f32)powf(10.0f, exp);
139 			c = t;
140 		}
141 	}
142 
143 	if (inv)
144 		f *= -1.0f;
145 
146 	out = f;
147 	return c;
148 }
149 
150 
fast_atof(const char * c)151 inline float fast_atof(const char* c)
152 {
153 	float ret;
154 	fast_atof_move_const(c, ret);
155 	return ret;
156 }
157 #else // __MINGW32__
158 inline float fast_atof(const char* c)
159 {
160 	return atof(c);
161 }
162 #endif // !__MINGW32__
163 
164 } // end namespace core
165 }// end namespace irr
166 
167 #endif
168 
169