<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>fixed or float?</title>
		<link>http://www.allegro.cc/forums/view/585500</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 22 May 2006 19:44:18 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m having a hard time determining whether or not I should use fixed point or floating point numbers for my game. I&#39;m using a rotated tile map so I&#39;m going to be using a lot of trig functions. I am aware of the fact that modern day FPUs are fast enough that using fixed point numbers isn&#39;t necessary anymore, but Allegros fixed point trig functions are considerably faster than the standard C ones. Also, I am worried about float to int performance issues. I&#39;ve read that such conversions are quite slow, but are fixed point conversions any faster? Or is that something I should not worry about. I was thinking about making a sin/cos lookup table, but in the process of using it I&#39;d be casting a float to an int. Is that going to be an issue? Or am I blowing this performance thing out of proportion?</p><p>Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonny Cook)</author>
		<pubDate>Sat, 20 May 2006 07:17:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Although most will tell you that in nowadays computers you should use floats, I would tell you to think something: will your game run in a slow computer? (in other words, your minimun requirement). If you want to run the game under DOS in a 233MHz, you should use fixed points. If you plan on running the game on &gt;1GHz computers, just go with floats.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Sat, 20 May 2006 08:58:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>yes i&#39;d say the only case where you could use fixed is for angles like in rotate_sprite() etc...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Frank Drebin)</author>
		<pubDate>Sat, 20 May 2006 13:02:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>float.</p><p>fixed is for like P1&#39;s
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (A J)</author>
		<pubDate>Sat, 20 May 2006 17:28:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, so I converted all my fixed math to floating point math, and pretty much everything is just as good as better, but what kills my FPS is the float to in conversions. When I draw my rotated tile map, I use two casts, and that&#39;s enough to lower my FPS from like, 320 to 180. Since I loop through every pixel on the screen, that&#39;s using 153,600 float to int casts each time I draw the map. However, if I use floats throughout the rest of my program, and switch to fixed math for my drawing code, overall I get a 20 FPS increase. This, of course, means I need to convert my normal 360 degrees into allegros 256 degrees, but since I precalculate the sine and cosine at the beginning of the function this doesn&#39;t seem to make a difference.<br />Here&#39;s my code:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">void</span> DrawMap<span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>dest, View <span class="k3">&amp;</span>view<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>    <a href="http://www.allegro.cc/manual/fixed" target="_blank"><span class="a">fixed</span></a> sine <span class="k3">=</span> fsin<span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span><span class="k3">-</span>view.angle <span class="k3">*</span> <span class="n">256</span>.<span class="n">0f</span><span class="k3">/</span><span class="n">360</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">)</span>, cosine <span class="k3">=</span> fcos<span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span><span class="k3">-</span>view.angle <span class="k3">*</span> <span class="n">256</span>.<span class="n">0f</span><span class="k3">/</span><span class="n">360</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td>    <span class="k1">int</span> viewX <span class="k3">=</span> <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>view.pos.x, viewY <span class="k3">=</span> <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>view.pos.y<span class="k2">;</span></td></tr><tr><td class="number">4</td><td>    <span class="k1">int</span> rotateX <span class="k3">=</span> <span class="k3">-</span>view.rotateX, rotateY <span class="k3">=</span> <span class="k3">-</span>view.rotateY<span class="k2">;</span></td></tr><tr><td class="number">5</td><td>    <span class="k1">unsigned</span> <span class="k1">int</span> destW <span class="k3">=</span> dest-&gt;w, destH <span class="k3">=</span> dest-&gt;h<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>    <span class="k1">int</span> c<span class="k2">;</span>  <span class="c">// Color</span></td></tr><tr><td class="number">7</td><td>    <span class="k1">int</span> sx, sy<span class="k2">;</span>  <span class="c">// Source pixel</span></td></tr><tr><td class="number">8</td><td>    <span class="k1">int</span> tx, ty<span class="k2">;</span>  <span class="c">// Tile coordinates</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td>    <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> screen_y <span class="k3">=</span> <span class="n">0</span>, cy <span class="k3">=</span> rotateY<span class="k2">;</span> screen_y <span class="k3">&lt;</span> destH<span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span> screen_y, <span class="k3">+</span><span class="k3">+</span> cy<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>        <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> screen_x <span class="k3">=</span> <span class="n">0</span>, cx <span class="k3">=</span> rotateX<span class="k2">;</span> screen_x <span class="k3">&lt;</span> destW<span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span> screen_x, <span class="k3">+</span><span class="k3">+</span> cx<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>            sx <span class="k3">=</span> <a href="http://www.allegro.cc/manual/fixtoi" target="_blank"><span class="a">fixtoi</span></a><span class="k2">(</span>cx<span class="k3">*</span>cosine <span class="k3">-</span> cy<span class="k3">*</span>sine<span class="k2">)</span> <span class="k3">+</span> viewX<span class="k2">;</span></td></tr><tr><td class="number">13</td><td>            sy <span class="k3">=</span> <a href="http://www.allegro.cc/manual/fixtoi" target="_blank"><span class="a">fixtoi</span></a><span class="k2">(</span>cx<span class="k3">*</span>sine <span class="k3">+</span> cy<span class="k3">*</span>cosine<span class="k2">)</span> <span class="k3">+</span> viewY<span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>            <span class="k1">if</span> <span class="k2">(</span>sx <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">0</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> sy <span class="k3">&gt;</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>                tx <span class="k3">=</span> sx <span class="k3">&gt;</span><span class="k3">&gt;</span> <span class="n">4</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>                ty <span class="k3">=</span> sy <span class="k3">&gt;</span><span class="k3">&gt;</span> <span class="n">4</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>                </td></tr><tr><td class="number">19</td><td>                <span class="k1">if</span> <span class="k2">(</span>tx <span class="k3">&lt;</span> MAP_W <span class="k3">&amp;</span><span class="k3">&amp;</span> ty <span class="k3">&lt;</span> MAP_H<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">20</td><td>                    Pixel<span class="k2">(</span>dest, screen_x, screen_y<span class="k2">)</span> <span class="k3">=</span> Pixel<span class="k2">(</span>tile<span class="k2">[</span>GetTile<span class="k2">(</span>tx, ty<span class="k2">)</span><span class="k2">]</span>, sx <span class="k3">&amp;</span> <span class="n">15</span>, sy <span class="k3">&amp;</span> <span class="n">15</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>                <span class="k2">}</span></td></tr><tr><td class="number">22</td><td>            <span class="k2">}</span></td></tr><tr><td class="number">23</td><td>        <span class="k2">}</span></td></tr><tr><td class="number">24</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">25</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

Anyway, I guess I&#39;ll just use floats and switch to fixed for those tight loops. Thanks for all your answers!</p><p>[edit]<br />Hmm... it&#39;s not letting me give anyone credit. Maybe it&#39;s broken, or maybe I chose the wrong thread type.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonny Cook)</author>
		<pubDate>Sat, 20 May 2006 18:32:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The Allegro fixed point trig <u>already</u> uses lookup tables.</p><p>You can get a slight speedup at times (always profile to be sure) if you mix fixed point and floating point, e.g. have some integer stuff shifting and adding while some floating point divide is going on.  Also, there&#39;s an old (I don&#39;t know if it&#39;s still valid) trick to convert float to int and back again by having a temporary double that you store the product of your float and a const, then grab the lower 32 bits of the mantissa (you need a union to do this) which is your integer.  I forget the exact value of the const, but it&#39;s a power of two.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Sun, 21 May 2006 21:59:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve been experimenting with different techniques, but I&#39;ve only been comparing them by FPS differences. I&#39;ve never used profiling, perhaps I should check it out. </p><p>That seems like quite a lot just to convert a float to an int... would that really be faster? </p><p>And I wonder, are the ftofix and fixtof functions very slow?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonny Cook)</author>
		<pubDate>Mon, 22 May 2006 03:52:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I hinghly doubt floats vs fixed makes big difference if you use them to calculate rotated sprites. Most probably drawing one rotated sprite is about as fast as couple hundred or thouseand float or fixed computations.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Mon, 22 May 2006 13:16:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If the calculus you do with the angles can be optimized to bit operations (that is: &gt;&gt;, &lt;&lt;, &amp;, |, ^) may be fixed would be faster.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Niunio)</author>
		<pubDate>Mon, 22 May 2006 19:44:18 +0000</pubDate>
	</item>
</rss>
