tag:blogger.com,1999:blog-315948062015-12-30T00:06:27.498+01:00Chris WaltonReinventing the Wheel since 1987.Chris Waltonhttp://www.blogger.com/profile/05813674116747802267noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-31594806.post-13098890071451508182011-08-14T22:15:00.002+02:002015-04-21T01:05:03.486+02:00Useless: Multiplication... so I was driving, and for some reason, I thought of processors without a builtin multiply instruction. So, the obvious general-purpose-multiplicator would, of course, be something like: <br /><br /><pre class="brush: cpp">u32<br />mul( u16 a, u16 b )<br />{<br /> u32 res = 0;<br /><br /> while ( a-- ) <br /> {<br /> res += b;<br /> }<br /> return res;<br />}<br /></pre><br /><br />Well, obviously, this is slow, so I figured I could do some pre-processing - if the count variable is larger than the value variable, then I can simply swap them, so that I have less loop iterations. <br /><br /><pre class="brush: cpp">u32<br />mulTrivial( u16 a, u16 b )<br />{<br /> u32 res = 0;<br /> <br /> if ( !b )<br /> return 0;<br /> if ( !a )<br /> return 0;<br /><br /> if ( a > b )<br /> {<br /> u16 t = a;<br /> a = b;<br /> b = t;<br /> }<br /><br /> while ( a-- ) <br /> {<br /> res += b;<br /> }<br /> return res;<br />}<br /></pre><br /><br />Well, this is still deadly slow. So, while driving 160kmh/100mph on the Autobahn, I thought of an optimized multiply algorithm. This is probably not original, but I'm proud of myself that I "invented" it independently. It works like this: <br /><br /><pre class="brush: cpp">u32<br />mulOptimized1( u16 _cnt, u16 _val )<br />{<br /> u32 val = (u32) _val;<br /> u32 res = 0;<br /> <br /> if ( !_val )<br /> return 0;<br /> <br /> while ( _cnt ) <br /> {<br /> if ( _cnt & 1 )<br /> {<br /> res += val;<br /> --_cnt;<br /> }<br /> else<br /> {<br /> _cnt >>= 1;<br /> val <<= 1;<br /> }<br /> }<br /> return res;<br />}<br /></pre><br /><br />The basic principle behind this algorithm is that for two values X and Y, X*Y is the same as (X/2)*(2*Y). When _cnt is divisible by two, it takes advantage of the fact that multiplying and dividing by two is trivial (a single bitshift), and thus does a shift so that it requires much less loop iterations. <br /><br />So I spent the few minutes making a test case, with benchmarking and checking that the results are all equal. I know, waste of time, but the results for 16777216 values are: <br /><br /><pre class="brush: text">Algorithm 'Reference': 0.52s<br />Algorithm 'Trivial': 361.42s<br />Algorithm 'Optimized': 2.36s<br /></pre><br /><br />I thought of some more optimizations, the first one reduces the need to do the '_cnt != 0' check every loop iteration where the lowest bit of _cnt is set: <br /><br /><pre class="brush: cpp">u32<br />mulOptimized2( u16 _cnt, u16 _val )<br />{<br /> u32 val = (u32) _val;<br /> u32 res = 0;<br /><br /> if ( !_val )<br /> return 0;<br /><br /> while ( _cnt ) <br /> {<br /> while ( !( _cnt & 1 ) )<br /> {<br /> _cnt >>= 1;<br /> val <<= 1;<br /> }<br /> <br /> res += val;<br /> --_cnt;<br /> }<br /> return res;<br />}<br /></pre><br /><br />And another optimization I thought of is for processors that include a bit-scan instruction, and have a barrel shifter for arbitrary shifts (things like the 8086 only supported shifting by 1), and it looks like this: <br /><br /><pre class="brush: cpp">u32<br />mulOptimized3( u16 _cnt, u16 _val )<br />{<br /> unsigned long val = (u32) _val;<br /> u32 cnt = (u32) _cnt;<br /> u32 res = 0;<br /> unsigned long index;<br /><br /> if ( !_val )<br /> return 0;<br /><br /> while ( _BitScanForward( &index, cnt ) )<br /> {<br /> <br /> cnt >>= index;<br /> val <<= index;<br /> res += val;<br /> --cnt;<br /> }<br /> return res;<br />}<br /></pre><br /><br />(_BitScanForward returns true if cnt is non-zero, and returns the bit index in index). <br /><br />Doing another benchmark run as above, the result is this: <br /><br /><pre class="brush: text">Algorithm 'Reference': 0.52s<br />Algorithm 'Trivial': 366.63s<br />Algorithm 'Optimized1': 2.36s<br />Algorithm 'Optimized2': 2.39s<br />Algorithm 'Optimized3': 1.84s<br /></pre><br /><br />Now sure, these results are measured on a very modern CPU with branch prediction and pipelining and a bunch of other fancy stuff, but I'm still kinda proud that the fastest method I wrote is only 3.5 times as slow as the 'Reference', which is simply the processor's native mul instruction. The bit scan trick worked very well, as I expected, but it's interesting that Optimized2 (which skips the '_cnt != 0' check when it can) is actually slower than Optimized1. I think I need to try this test on some old CPU somehow. Maybe DOSBox has some sort of mode that has cycle-accurate timing, in which case this is a much more reliable test, especially as it's testing something meant for deadly old processors. <br /><br />Now, here's something very interesting. I did the above benchmarks in a Debug build. I tried this again in an optimized Release build (MSVC, obviously), and this is what I got: <br /><br /><pre class="brush: text">Algorithm 'Reference': 0.08s<br />Algorithm 'Trivial': 0.17s<br />Algorithm 'Optimized1': 2.18s<br />Algorithm 'Optimized2': 2.15s<br />Algorithm 'Optimized3': 0.47s<br /></pre><br /><br />Now, the Reference algo and the three Optimized algos ran as expected (the third one performed even better in relation!). However, the Trivial algorithm ran faster than all the other ones. Something wasn't right there, so I took a look at the generated assembly code. The checks and the swap remained in there, but look at what it did to the actual multiplication loop: <br /><br /><pre class="brush: text">002E103C movzx eax,cx <br />002E103F movzx ecx,dx <br />002E1042 imul eax,ecx<br /></pre><br /><br />MSVC was smart enough to optimize that into a single multiplication instruction. I'm fucking amazed. <br /><br />--- <br /><br />I know, this is completely useless, but a few minutes of fun none-the-less, and I learned just how smart MSVC can be in some situations.Chris Waltonhttp://www.blogger.com/profile/05813674116747802267noreply@blogger.com0