<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>Irrlicht Engine: fast_atof.h Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <table class="irrlicht" > <tr valign="middle"> <td><font size="2"><a class="qindex" href="index.html"><font color="#FFFFFF">Home</font></a> | <a class="qindex" href="namespaces.html"><font color="#FFFFFF">Namespaces</font></a> | <a class="qindex" href="hierarchy.html"><font color="#FFFFFF">Hierarchy</font></a> | <a class="qindex" href="classes.html"><font color="#FFFFFF">Alphabetical List</font></a> | <a class="qindex" href="annotated.html"><font color="#FFFFFF"> Class list</font></a> | <a class="qindex" href="files.html"><font color="#FFFFFF">Files</font></a> | <a class="qindex" href="namespacemembers.html"><font color="#FFFFFF"> Namespace Members</font></a> | <a class="qindex" href="functions.html"><font color="#FFFFFF">Class members</font></a> | <a class="qindex" href="globals.html"><font color="#FFFFFF">File members</font></a> | <a class="qindex" href="pages.html"><font color="#FFFFFF">Tutorials</font></a></font> </td> </tr> </table> <!-- Generated by Doxygen 1.5.6 --> <h1>fast_atof.h</h1><a href="fast__atof_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (C) 2002-2009 Nikolaus Gebhardt</span> <a name="l00002"></a>00002 <span class="comment">// This file is part of the "Irrlicht Engine" and the "irrXML" project.</span> <a name="l00003"></a>00003 <span class="comment">// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h</span> <a name="l00004"></a>00004 <a name="l00005"></a>00005 <span class="preprocessor">#ifndef __FAST_A_TO_F_H_INCLUDED__</span> <a name="l00006"></a>00006 <span class="preprocessor"></span><span class="preprocessor">#define __FAST_A_TO_F_H_INCLUDED__</span> <a name="l00007"></a>00007 <span class="preprocessor"></span> <a name="l00008"></a>00008 <span class="preprocessor">#include "<a class="code" href="irr_math_8h.html">irrMath.h</a>"</span> <a name="l00009"></a>00009 <a name="l00010"></a>00010 <span class="keyword">namespace </span>irr <a name="l00011"></a>00011 { <a name="l00012"></a>00012 <span class="keyword">namespace </span>core <a name="l00013"></a>00013 { <a name="l00014"></a>00014 <a name="l00015"></a>00015 <span class="comment">// we write [17] here instead of [] to work around a swig bug</span> <a name="l00016"></a><a class="code" href="namespaceirr_1_1core.html#e473ba500eefc080b4680e3445e44b02">00016</a> <span class="keyword">const</span> <span class="keywordtype">float</span> <a class="code" href="namespaceirr_1_1core.html#e473ba500eefc080b4680e3445e44b02">fast_atof_table</a>[17] = { <a name="l00017"></a>00017 0.f, <a name="l00018"></a>00018 0.1f, <a name="l00019"></a>00019 0.01f, <a name="l00020"></a>00020 0.001f, <a name="l00021"></a>00021 0.0001f, <a name="l00022"></a>00022 0.00001f, <a name="l00023"></a>00023 0.000001f, <a name="l00024"></a>00024 0.0000001f, <a name="l00025"></a>00025 0.00000001f, <a name="l00026"></a>00026 0.000000001f, <a name="l00027"></a>00027 0.0000000001f, <a name="l00028"></a>00028 0.00000000001f, <a name="l00029"></a>00029 0.000000000001f, <a name="l00030"></a>00030 0.0000000000001f, <a name="l00031"></a>00031 0.00000000000001f, <a name="l00032"></a>00032 0.000000000000001f, <a name="l00033"></a>00033 0.0000000000000001f <a name="l00034"></a>00034 }; <a name="l00035"></a>00035 <a name="l00044"></a><a class="code" href="namespaceirr_1_1core.html#6ff97e442233218d18acd56add48766f">00044</a> <span class="keyword">inline</span> <a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> <a class="code" href="namespaceirr_1_1core.html#6ff97e442233218d18acd56add48766f">strtol10</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* in, <span class="keyword">const</span> <span class="keywordtype">char</span>** out=0) <a name="l00045"></a>00045 { <a name="l00046"></a>00046 <span class="keywordflow">if</span>(!in) <a name="l00047"></a>00047 <span class="keywordflow">return</span> 0; <a name="l00048"></a>00048 <a name="l00049"></a>00049 <span class="keywordtype">bool</span> negative = <span class="keyword">false</span>; <a name="l00050"></a>00050 <span class="keywordflow">if</span>(<span class="charliteral">'-'</span> == *in) <a name="l00051"></a>00051 { <a name="l00052"></a>00052 negative = <span class="keyword">true</span>; <a name="l00053"></a>00053 ++in; <a name="l00054"></a>00054 } <a name="l00055"></a>00055 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(<span class="charliteral">'+'</span> == *in) <a name="l00056"></a>00056 ++in; <a name="l00057"></a>00057 <a name="l00058"></a>00058 <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> unsignedValue = 0; <a name="l00059"></a>00059 <a name="l00060"></a>00060 <span class="keywordflow">while</span> ( ( *in >= <span class="charliteral">'0'</span>) && ( *in <= <span class="charliteral">'9'</span> )) <a name="l00061"></a>00061 { <a name="l00062"></a>00062 unsignedValue = ( unsignedValue * 10 ) + ( *in - <span class="charliteral">'0'</span> ); <a name="l00063"></a>00063 ++in; <a name="l00064"></a>00064 <a name="l00065"></a>00065 <span class="keywordflow">if</span>(unsignedValue > (<a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a>)INT_MAX) <a name="l00066"></a>00066 { <a name="l00067"></a>00067 unsignedValue = (<a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a>)INT_MAX; <a name="l00068"></a>00068 <span class="keywordflow">break</span>; <a name="l00069"></a>00069 } <a name="l00070"></a>00070 } <a name="l00071"></a>00071 <span class="keywordflow">if</span> (out) <a name="l00072"></a>00072 *out = in; <a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="keywordflow">if</span>(negative) <a name="l00075"></a>00075 <span class="keywordflow">return</span> -((<a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a>)unsignedValue); <a name="l00076"></a>00076 <span class="keywordflow">else</span> <a name="l00077"></a>00077 <span class="keywordflow">return</span> (<a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a>)unsignedValue; <a name="l00078"></a>00078 } <a name="l00079"></a>00079 <a name="l00086"></a><a class="code" href="namespaceirr_1_1core.html#032ec5ae63987749c7b30c18ea4ccd92">00086</a> <span class="keyword">inline</span> <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> <a class="code" href="namespaceirr_1_1core.html#032ec5ae63987749c7b30c18ea4ccd92">strtof10</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* in, <span class="keyword">const</span> <span class="keywordtype">char</span> * * out = 0) <a name="l00087"></a>00087 { <a name="l00088"></a>00088 <span class="keywordflow">if</span>(out) <a name="l00089"></a>00089 *out = in; <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="keywordflow">if</span>(!in) <a name="l00092"></a>00092 <span class="keywordflow">return</span> 0.f; <a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> MAX_SAFE_U32_VALUE = UINT_MAX / 10 - 10; <a name="l00095"></a>00095 <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> floatValue = 0.f; <a name="l00096"></a>00096 <a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> intValue = 0; <a name="l00097"></a>00097 <a name="l00098"></a>00098 <span class="comment">// Use integer arithmetic for as long as possible, for speed</span> <a name="l00099"></a>00099 <span class="comment">// and precision.</span> <a name="l00100"></a>00100 <span class="keywordflow">while</span> ( ( *in >= <span class="charliteral">'0'</span>) && ( *in <= <span class="charliteral">'9'</span> ) ) <a name="l00101"></a>00101 { <a name="l00102"></a>00102 <span class="comment">// If it looks like we're going to overflow, bail out</span> <a name="l00103"></a>00103 <span class="comment">// now and start using floating point.</span> <a name="l00104"></a>00104 <span class="keywordflow">if</span>(intValue >= MAX_SAFE_U32_VALUE) <a name="l00105"></a>00105 <span class="keywordflow">break</span>; <a name="l00106"></a>00106 <a name="l00107"></a>00107 intValue = ( intValue * 10) + ( *in - <span class="charliteral">'0'</span> ); <a name="l00108"></a>00108 ++in; <a name="l00109"></a>00109 } <a name="l00110"></a>00110 <a name="l00111"></a>00111 floatValue = (<a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a>)intValue; <a name="l00112"></a>00112 <a name="l00113"></a>00113 <span class="comment">// If there are any digits left to parse, then we need to use </span> <a name="l00114"></a>00114 <span class="comment">// floating point arithmetic from here.</span> <a name="l00115"></a>00115 <span class="keywordflow">while</span> ( ( *in >= <span class="charliteral">'0'</span>) && ( *in <= <span class="charliteral">'9'</span> ) ) <a name="l00116"></a>00116 { <a name="l00117"></a>00117 floatValue = ( floatValue * 10.f ) + (<a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a>)( *in - <span class="charliteral">'0'</span> ); <a name="l00118"></a>00118 ++in; <a name="l00119"></a>00119 <span class="keywordflow">if</span>(floatValue > <a class="code" href="irr_math_8h.html#7fbf1534f9e0f0bfdb86fb928902474c">FLT_MAX</a>) <span class="comment">// Just give up.</span> <a name="l00120"></a>00120 <span class="keywordflow">break</span>; <a name="l00121"></a>00121 } <a name="l00122"></a>00122 <a name="l00123"></a>00123 <span class="keywordflow">if</span>(out) <a name="l00124"></a>00124 *out = in; <a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="keywordflow">return</span> floatValue; <a name="l00127"></a>00127 } <a name="l00128"></a>00128 <a name="l00136"></a><a class="code" href="namespaceirr_1_1core.html#43119c0bac67d095346dbd826c62971c">00136</a> <span class="keyword">inline</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="namespaceirr_1_1core.html#43119c0bac67d095346dbd826c62971c">fast_atof_move</a>( <span class="keyword">const</span> <span class="keywordtype">char</span> * in, <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> & out) <a name="l00137"></a>00137 { <a name="l00138"></a>00138 <span class="comment">// Please run this regression test when making any modifications to this function:</span> <a name="l00139"></a>00139 <span class="comment">// https://sourceforge.net/tracker/download.php?group_id=74339&atid=540676&file_id=298968&aid=1865300</span> <a name="l00140"></a>00140 <a name="l00141"></a>00141 out = 0.f; <a name="l00142"></a>00142 <span class="keywordflow">if</span>(!in) <a name="l00143"></a>00143 <span class="keywordflow">return</span> 0; <a name="l00144"></a>00144 <a name="l00145"></a>00145 <span class="keywordtype">bool</span> negative = <span class="keyword">false</span>; <a name="l00146"></a>00146 <span class="keywordflow">if</span>(*in == <span class="charliteral">'-'</span>) <a name="l00147"></a>00147 { <a name="l00148"></a>00148 negative = <span class="keyword">true</span>; <a name="l00149"></a>00149 ++in; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <a name="l00152"></a>00152 <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> value = <a class="code" href="namespaceirr_1_1core.html#032ec5ae63987749c7b30c18ea4ccd92">strtof10</a> ( in, &in ); <a name="l00153"></a>00153 <a name="l00154"></a>00154 <span class="keywordflow">if</span> (*in == <span class="charliteral">'.'</span>) <a name="l00155"></a>00155 { <a name="l00156"></a>00156 ++in; <a name="l00157"></a>00157 <a name="l00158"></a>00158 <span class="keyword">const</span> <span class="keywordtype">char</span> * afterDecimal = in; <a name="l00159"></a>00159 <a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a> decimal = <a class="code" href="namespaceirr_1_1core.html#032ec5ae63987749c7b30c18ea4ccd92">strtof10</a> ( in, &afterDecimal ); <a name="l00160"></a>00160 decimal *= <a class="code" href="namespaceirr_1_1core.html#e473ba500eefc080b4680e3445e44b02">fast_atof_table</a>[afterDecimal - in]; <a name="l00161"></a>00161 <a name="l00162"></a>00162 value += decimal; <a name="l00163"></a>00163 <a name="l00164"></a>00164 in = afterDecimal; <a name="l00165"></a>00165 } <a name="l00166"></a>00166 <a name="l00167"></a>00167 <span class="keywordflow">if</span> (<span class="charliteral">'e'</span> == *in || <span class="charliteral">'E'</span> == *in) <a name="l00168"></a>00168 { <a name="l00169"></a>00169 ++in; <a name="l00170"></a>00170 <span class="comment">// Assume that the exponent is a whole number.</span> <a name="l00171"></a>00171 <span class="comment">// strtol10() will deal with both + and - signs,</span> <a name="l00172"></a>00172 <span class="comment">// but cast to (f32) to prevent overflow at FLT_MAX</span> <a name="l00173"></a>00173 value *= (<a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a>)pow(10.0f, (<a class="code" href="namespaceirr.html#0277be98d67dc26ff93b1a6a1d086b07" title="32 bit floating point variable.">f32</a>)<a class="code" href="namespaceirr_1_1core.html#6ff97e442233218d18acd56add48766f">strtol10</a>(in, &in)); <a name="l00174"></a>00174 } <a name="l00175"></a>00175 <a name="l00176"></a>00176 <span class="keywordflow">if</span>(negative) <a name="l00177"></a>00177 out = -value; <a name="l00178"></a>00178 <span class="keywordflow">else</span> <a name="l00179"></a>00179 out = value; <a name="l00180"></a>00180 <a name="l00181"></a>00181 <span class="keywordflow">return</span> in; <a name="l00182"></a>00182 } <a name="l00183"></a>00183 <a name="l00186"></a><a class="code" href="namespaceirr_1_1core.html#6a37f35d36ab86749b7d42e35bda82a9">00186</a> <span class="keyword">inline</span> <span class="keywordtype">float</span> <a class="code" href="namespaceirr_1_1core.html#6a37f35d36ab86749b7d42e35bda82a9">fast_atof</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* floatAsString) <a name="l00187"></a>00187 { <a name="l00188"></a>00188 <span class="keywordtype">float</span> ret; <a name="l00189"></a>00189 <a class="code" href="namespaceirr_1_1core.html#43119c0bac67d095346dbd826c62971c">fast_atof_move</a>(floatAsString, ret); <a name="l00190"></a>00190 <span class="keywordflow">return</span> ret; <a name="l00191"></a>00191 } <a name="l00192"></a>00192 <a name="l00193"></a>00193 } <span class="comment">// end namespace core</span> <a name="l00194"></a>00194 } <span class="comment">// end namespace irr</span> <a name="l00195"></a>00195 <a name="l00196"></a>00196 <span class="preprocessor">#endif</span> <a name="l00197"></a>00197 <span class="preprocessor"></span> </pre></div></div> <hr size="1"> <address style="align: right;"> <small> </small> </address> <table width="100%" border="0" cellspacing="0" cellpadding="2"> <tr> <td width="0"> <div align="left"><small><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="irrlicht.png" alt="The Irrlicht Engine" align="middle" border=0 width=88 height=31></a></small></div></td> <td> <div align="left"><small><em><font size="2">The <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht Engine</a> Documentation © 2003-2009 by Nikolaus Gebhardt. Generated on Sun Jan 10 09:24:03 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> (1.5.6)</font></em></small></div></td> </tr> </table> <address style="align: right;"> </address> </body> </html>