<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Speed Performance using either float or fixed</title>
		<link>http://www.allegro.cc/forums/view/586085</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 26 Jun 2006 00:57:06 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I know that there were some threads regarding this, but I just want clarification:</p><p>I have the following machines that I test on (all Windows so far):</p><p>P4 XP 2.8GHZ 256MB Nvidia Gfx card<br />Celeron 2000 1.1GHZ 4MB ATI GFX card<br />Celeron 2000 1.1GHZ 128MB ATI GFX card</p><p>All angle and position calculation uses floats. </p><p>On the P4, everything is fine. FPS 60 Runs smoothly.</p><p>On the other machines, runs slower, like 25 - 30 FPS. Was running at least 45 FPS until I changed all of the angle and position values to floats, which works well for me, a lot better than fixed. On slower machines under a P4, do I HAVE to use fixed to get a better performance?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 04:54:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It depends on the calculations, but in older processors fixed may be a good alternative.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Sun, 25 Jun 2006 04:58:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ok. </p><p>So let&#39;s say I have the following 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">float</span> x<span class="k2">;</span></td></tr><tr><td class="number">2</td><td><span class="k1">float</span> x_vel<span class="k2">;</span></td></tr><tr><td class="number">3</td><td><span class="k1">float</span> speed<span class="k2">;</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td>....</td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">if</span> <span class="k2">(</span>x_vel <span class="k3">&gt;</span> <span class="k3">-</span>speed<span class="k2">)</span> </td></tr><tr><td class="number">8</td><td>  x_vel-<span class="k3">=</span><span class="n">0</span>.<span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td>x <span class="k3">+</span><span class="k3">=</span> x_vel<span class="k2">;</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td>...</td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td><a href="http://www.allegro.cc/manual/masked_blit" target="_blank"><span class="a">masked_blit</span></a><span class="k2">(</span>src, dest, <span class="n">0</span>, <span class="n">0</span>, <a href="http://www.allegro.cc/manual/fixtoi" target="_blank"><span class="a">fixtoi</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>x<span class="k2">)</span><span class="k2">)</span>, <a href="http://www.allegro.cc/manual/fixtoi" target="_blank"><span class="a">fixtoi</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>y<span class="k2">)</span><span class="k2">)</span>, src-&gt;w, src-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

This is basically how I&#39;m calculating the positioning. Is the the best way to impletment this?</p><p>Also, Let&#39;s say that I wanted something to move at speed = 5.5, how is declaring speed as a &quot;fixed&quot; type going to help me with that? Use (int)fixtof(speed)+0.5 or something?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 05:09:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I believe ftofix is a slow function, you should calculate everything using fixed points, or just determine your program needs a big processor to run. You would create a constant, like <i>const fixed constant_speed = ftofix(5.5);</i>, that way you would calculate it only once.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Sun, 25 Jun 2006 05:18:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your example code isn&#39;t efficient, you are using floats, casting to fixed, then casting to int, that&#39;s slow no matter how you look at it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve Terry)</author>
		<pubDate>Sun, 25 Jun 2006 05:53:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Kevin Ebbs said:</div><div class="quote"><p>
<span class="source-code"><a href="http://www.allegro.cc/manual/masked_blit" target="_blank"><span class="a">masked_blit</span></a><span class="k2">(</span>src, dest, <span class="n">0</span>, <span class="n">0</span>, <a href="http://www.allegro.cc/manual/fixtoi" target="_blank"><span class="a">fixtoi</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>x<span class="k2">)</span><span class="k2">)</span>, <a href="http://www.allegro.cc/manual/fixtoi" target="_blank"><span class="a">fixtoi</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>y<span class="k2">)</span><span class="k2">)</span>, src-&gt;w, src-&gt;h<span class="k2">)</span><span class="k2">;</span></span>Is the the best way to implement this?
</p></div></div><p>
Why not simply :<br /><span class="source-code"><a href="http://www.allegro.cc/manual/masked_blit" target="_blank"><span class="a">masked_blit</span></a><span class="k2">(</span>src, dest, <span class="n">0</span>, <span class="n">0</span>, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>x, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>y, src-&gt;w, src-&gt;h<span class="k2">)</span><span class="k2">;</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Sun, 25 Jun 2006 06:00:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Why not simply :</p><p>masked_blit(src, dest, 0, 0, (int)x, (int)y, src-&gt;w, src-&gt;h);
</p></div></div><p>

Ooops! I put in code from my old code. Sorry about. Yeah that above masked_blit in the quote is actually what I use.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 06:14:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In the Allegro tests folder, there&#39;s a program called test.exe (at least for me). Check out &quot;Time some stuff&quot; in the Misc menu. As the name suggests, it times a bunch of stuff, including fixed point math and floating point math. Check it out on different computers. On my computer, (AthlonXP 2200), floating point arithmetic is faster than fixed pointer arithmetic, but the fixed point trig functions are much faster than the floating point ones. This probably is due to lookup tables.</p><p>What you could always do is store all the angles and position and everything else as floats, and when you need to do some speed critical trig stuff, just convert them to fixed point numbers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonny Cook)</author>
		<pubDate>Sun, 25 Jun 2006 06:44:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>[edit]<br />Oh yeah, thanks for the tip about the test exe&#39;s.<br />[/edit]</p><p>Well, that&#39;s pretty much what I do for the angle calcs. </p><div class="source-code snippet"><div class="inner"><pre>y <span class="k3">=</span> length <span class="k3">*</span> <a href="http://www.allegro.cc/manual/fixtof" target="_blank"><span class="a">fixtof</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/fixsin" target="_blank"><span class="a">fixsin</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>angle<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
x <span class="k3">=</span> length <span class="k3">*</span> <a href="http://www.allegro.cc/manual/fixtof" target="_blank"><span class="a">fixtof</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/fixcos" target="_blank"><span class="a">fixcos</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>angle<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

But what about for movement calc&#39;s like using velocity to move a sprite?</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>x_vel <span class="k3">&lt;</span> speed<span class="k2">)</span> 
  x_vel<span class="k3">+</span><span class="k3">=</span><span class="n">0</span>.<span class="n">1</span><span class="k2">;</span>

x <span class="k3">+</span><span class="k3">=</span> x_vel<span class="k2">;</span>
</pre></div></div><p>		</p><p>Do i need to change any of these to fixed while doing this type of calc? Or is the pretty much the best I can do when it comes to this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 07:00:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre>y <span class="k3">=</span> length <span class="k3">*</span> <a href="http://www.allegro.cc/manual/fixtof" target="_blank"><span class="a">fixtof</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/fixsin" target="_blank"><span class="a">fixsin</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>angle<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
x <span class="k3">=</span> length <span class="k3">*</span> <a href="http://www.allegro.cc/manual/fixtof" target="_blank"><span class="a">fixtof</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/fixcos" target="_blank"><span class="a">fixcos</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/ftofix" target="_blank"><span class="a">ftofix</span></a><span class="k2">(</span>angle<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Hmm, call me naive or whathaveyou, but that seems, at best, to be a bad idea.  You are converting float-&gt;fix, doing trig, then converting fix-&gt;float.  float-&gt;fix and fix-&gt;float are slow operations (there is no native conversion operation for fixed point numbers, AFAIK).</p><p>I would be very surprised that, if on modern hardware, that is any faster than this:
</p><div class="source-code snippet"><div class="inner"><pre>y <span class="k3">=</span> length <span class="k3">*</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>angle<span class="k2">)</span><span class="k2">;</span>
x <span class="k3">=</span> length <span class="k3">*</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>angle<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Yes, I know fixed point trig utilizes lookup tables, but I doubt the speed gain from lookup tables is going to outweight the speed loss from fix-float or float-fix conversion.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Sun, 25 Jun 2006 07:11:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, </p><p>I know that certain calls are don&#39;t really matter for modern machines, but a lot of people may not have modern machines and I know quite a few people that don&#39;t. So, I would like to make sure that my game can run on machines below P4 as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 07:26:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have a 1.1GHz machine and use floating point just fine. The thing to keep in mind when using floats is to <u>use floats</u>. Don&#39;t go converting them to and from int or fixed or anything like that unless <i>absolutely necessary</i>. There may be a few things you can do to convert to int and be faster (eg. a sine lookup table for slower machines), but these will generally amount to a few FPS at most (and as a side effect, lose a few FPS for most newer machines).</p><p>If you need to convert to int/fixed alot (for say, Allegro functions) it may be better to use fixed. Fixed is also nice when doing bit-wise operations. Allegro&#39;s 3d math functions, though, have both fixed point and floating point variations.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sun, 25 Jun 2006 09:23:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Would someone remind me why people have been using fixed point anymore. i686 handles floats just fine. Newer CPU&#39;s handle them better than fine, they handle them damn fast!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Sun, 25 Jun 2006 10:07:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well,</p><p>I just posted a demo of my game in the depot forum to see how it runs on the majority&#39;s computers. If everyone is able to run it at 60 FPS, then I&#39;ll just assume that mostly everyone has modern computers that can handle float and continue. If not, then I&#39;ll have to rewrite some code as fixed. Hopefully, it&#39;s not the latter.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 10:40:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Would someone remind me why people have been using fixed point anymore.
</p></div></div><p>
Accuracy, for one. You&#39;ll always have 16 bits reserved for sub-integer precision. No more, no less. Makes it easier for checking and comparing. Plus it&#39;s rather fast for converting to int, so it&#39;s very useful for tight speed sensitive loops. And, you can do this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Set range 0..256 (inclusive-exclusive)</span>
fixedNum <span class="k3">&amp;</span><span class="k3">=</span> <span class="n">0xFFFFFF</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sun, 25 Jun 2006 11:54:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I posted the performance I get in the other thread.<br />From what I saw, there&#39;s like 20 objects moving onscreen, not rotated, and moving in straight lines... The screen however seems to be in 800x600 or something, hi color, drawing rather big things (like earth).<br />Your logic step should take like 1% CPU on almost anybody&#39;s computer. even on a 386 sx25, there&#39;s no way it can reach 10% - except if you&#39;ve coded in some super-inefficient way : but then, it will still be only 10%</p><p>Switching from ints to fix to floats could make this time (1% to 10%) double or halve, and nothing more.</p><p>IMO, you should<br />1) profile. My guess can be wrong<br />2) focus on optimizing what takes 90% time. not what takes 10% time.</p><p><b>edit:</b>
</p><div class="quote_container"><div class="title">Goalie Ca said:</div><div class="quote"><p>
Would someone remind me why people have been using fixed point anymore
</p></div></div><p>
Link by ReyBrujo in another thread: <a href="http://en.wikipedia.org/wiki/Floating_point#Problems_with_floating-point">Problems with floating-points</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Sun, 25 Jun 2006 20:51:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey Audric</p><p>I had answered your question on the depot thread:</p><p>this is my game loop 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> example_game_function<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">bool</span> run <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>  <span class="k1">bool</span> draw<span class="k3">=</span><span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>  <span class="k1">while</span> <span class="k2">(</span>run<span class="k2">)</span></td></tr><tr><td class="number">6</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">7</td><td>    <span class="k1">if</span><span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">8</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>    <span class="k1">do</span> </td></tr><tr><td class="number">10</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td>    <span class="c">//put movement, any logic code here</span></td></tr><tr><td class="number">13</td><td>    </td></tr><tr><td class="number">14</td><td>    speed_counter--<span class="k2">;</span></td></tr><tr><td class="number">15</td><td>    curSkip<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>        </td></tr><tr><td class="number">16</td><td>          <span class="k1">if</span> <span class="k2">(</span>curSkip <span class="k3">&gt;</span><span class="k3">=</span> maxSkip<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">17</td><td>                speed_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>                <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>          <span class="k2">}</span></td></tr><tr><td class="number">20</td><td>    <span class="k2">}</span> <span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>    draw<span class="k3">=</span><span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>    </td></tr><tr><td class="number">23</td><td>    <span class="k2">}</span> </td></tr><tr><td class="number">24</td><td>&#160;</td></tr><tr><td class="number">25</td><td>    <span class="k1">if</span><span class="k2">(</span>draw<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">26</td><td>    draw<span class="k3">=</span><span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>    BeginDrawing<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>    <span class="c">// draw, pard'ner!!</span></td></tr><tr><td class="number">29</td><td>          clear<span class="k2">(</span>buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>&#160;</td></tr><tr><td class="number">31</td><td>    <span class="c">//put drawing code ONLY here</span></td></tr><tr><td class="number">32</td><td>&#160;</td></tr><tr><td class="number">33</td><td>    EndDrawing<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>    xframe_count<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">36</td><td>  </td></tr><tr><td class="number">37</td><td>  <span class="k2">}</span> <span class="c">//while</span></td></tr><tr><td class="number">38</td><td><span class="k2">}</span></td></tr><tr><td class="number">39</td><td><span class="c">// using triple buffering</span></td></tr></tbody></table></div></div><p>

Question about Profiling, though. In the results where should I be looking to determine speed of drawing and logic, if I can do that, that is.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevin Epps)</author>
		<pubDate>Sun, 25 Jun 2006 21:58:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Yes, I know fixed point trig utilizes lookup tables, but I doubt the speed gain from lookup tables is going to outweight the speed loss from fix-float or float-fix conversion.
</p></div></div><p>There&#39;s also performance loss from having to cache those lookup tables.</p><p>All CPUs from about 486 DX2/66 have an integrated floating point processor (I think it may have become part of the core at some stage). Processing floats isn&#39;t (much) more expensive than processing ints.</p><p>I would agree that the advantage of fixed is that you get guaranteed accuracy with the number itself, but when you start dealing with increasingly smaller numbers, things start getting ugly. The only time floating point accuracy is threatened is when huge numbers meet tiny numbers.</p><p>If you&#39;re using Direct3D or OpenGL, you&#39;ll need to convert to float anyway, so have a think about that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve++)</author>
		<pubDate>Sun, 25 Jun 2006 22:23:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>one random article about profiling<br /><a href="http://www.redhat.com/magazine/012oct05/features/gcc/">http://www.redhat.com/magazine/012oct05/features/gcc/</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Mon, 26 Jun 2006 00:57:06 +0000</pubDate>
	</item>
</rss>
