    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Optimizing Big Number Arithmetic Without SSE</title>
        <link>https://members.accu.org/index.php/journals/1849</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #119 - February 2014 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c334/">o119</a>
                    (6)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c334-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c334+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Optimizing Big Number Arithmetic Without SSE</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 02 February 2014 07:53:06 +00:00 or Sun, 02 February 2014 07:53:06 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Addition and multiplication can be slow for big numbers. Sergey Ignatchenko and Dmytro Ivanchykhin try to speed things up.</p>
<p><strong>Body:</strong>&nbsp;<p class="xxx">Disclaimer: as usual, the opinions within this article are those of â€˜No Bugsâ€™ Bunny, and do not necessarily coincide with the opinions of the translator or the <em>Overload</em> editor. Please also keep in mind that translation difficulties from Lapine (like those described in [<a href="#[Loganberry04]">Loganberry04</a>]) might have prevented providing an exact translation. In addition, both the translators and <em>Overload</em> expressly disclaim all responsibility from any action or inaction resulting from reading this article.</p>

<p>There is one thing which has puzzled me for a while, and it is the performance of programs written in C when it comes to big numbers. It may or may not help with the decades-long â€˜C vs Fortranâ€™ performance debate, but letâ€™s concentrate on one single and reasonably well-defined thing â€“ big number arithmetic in C and see if it can be improved.</p>

<p>In fact, there are very few things which gain from being rewritten in assembler (compared to C), but big number arithmetic is one of them, with relatively little progress in this direction over the years. Letâ€™s take a look at OpenSSL (a library which is among the most concerned about big number performance: 99% of SSL connections use RSA these days, and RSA_performance == Big_Number_Performance, and RSA is notoriously sslloooowww). Run:</p>

<pre class="programlisting">
  openssl speed rsa</pre>
  
<p>(if youâ€™re on Windows, you may need to install OpenSSL first) and see what it shows. In most cases, at the end of the benchmark report it will show something like</p>

<pre class="programlisting">
compiler: gcc -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DKRB5_MIT -DL_ENDIAN -DTERMIO -Wall -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wa,--noexecstack -DOPENSSL_BN_ASM_PART_WORDS -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DRMD160_ASM -DAES_ASM -DWHIRLPOOL_ASM</pre>

<p>Note those <code>-DOPENSSL_BN_ASM</code> in the output â€“ it means that OpenSSL prefers to use assembler for big number calculations. It was the case back in 1998, and it is still the case now (last time I checked, the difference between C and asm implementations was 2x, but it was long ago, so things may have easily changed since). But why should this be the case? Why with all the optimizations compilers are doing now, should such a common and trivial thing still need to be written in asm (which has its own drawbacks â€“ from the need to write it manually for each and every platform, to sub-optimality of generic asm when it comes to pipeline optimizations â€“ and hand-rewriting asm for each new CPU -march/-mtune is not realistic)? If it can perform in asm but cannot perform in C, it means that all hardware support is present, but the performance is lost somewhere in between C developer and generated binary code; in short â€“ the compiler cannot produce good code. This article will try to perform some analysis of this phenomenon.</p>

<h2>The problem</h2>

<p>For the purposes of this article, letâ€™s restrict our analysis to the two most basic operations in big-number arithmetic: addition and multiplication. Also in this article we wonâ€™t go into SSE/SSE2, concentrating on much simpler things which still can provide substantial performance gains. The reason why I decided to stay away from SSE/SSE2/... is because while the whole area of vectorization has been extensively studied recently, it seems that people have forgotten about the good old plain instruction set, which is still capable of delivering good performance; combined with the fact that SSE has its own issues which make it not so optimal in certain scenarios, and another suggestion that the most optimal implementation would probably parallelise execution between the SSE engine and the good old plain instruction set.</p>

<h2>Mind the gap</h2>

<p>When Iâ€™m speaking about addition: sure, C/C++ has operator <code>+</code>. However, to add, say, two 128-bit numbers, we need to do more than just repeating twice an addition of two <code>uint64_t</code>â€™s in the C/C++ sense, we also need to obtain carry (65th bit of result) which may easily occur when adding lower 64-bit parts of our operands.</p>

<pre class="programlisting">
  uint128_t c = (uint128_t)a + (uint128_t)b;</pre>

<p>can be rewritten as</p>

<pre class="programlisting">
  pair&lt;bool,uint64_t&gt; cLo =
    add65(lower64(a) + lower64(b));
  uint64_t cHi = higher64(a)+higher64(b)+cLo.first;</pre>
  
<p>(assuming that <code>lower64()</code> returns low 64 bits, <code>higher64()</code> returns high 64 bits, and <code>add65()</code> returns a pair representing <code>&lt;carry flag,64_bits_of_sum&gt;</code>)</p>

<p>It is this carry flag which is causing all the trouble with additions. In x64 assembler, this whole thing can be written as something like</p>

<pre class="programlisting">
  add reg_holding_lower64_of_a,
    reg_holding_lower64_of_b
  adc reg_holding_higher64_of_a,
    reg_holding_higher64_of_b</pre>
	
<p>â€“ and thatâ€™s it. However, as in C there is no such thing as a â€˜carry flagâ€™, developers are doing all kinds of stuff to simulate it, usually with pretty bad results for performance.</p>

<p>With multiplication it is also not that easy, and for a similar reason. In mathematics, when multiplying 64 bits by 64 bits, the result is 128-bit long. In assembler (at least in x64, and also probably in ARM) the result is also 128-bit long. However, in C, the result of 64bit-by-64-bit multiplication is 64-bit long, which, when working with big number arithmetic, leads to four-fold (!) increase in number of multiplications. Ouch.</p>

<p>It should be noted that at least on x64 there is an instruction which multiplies 64 bits by 64 bits and returns lower 64 bits of the result, and this instruction is somewhat faster than full-scale 64-bit-by-64-bit-returning-128-bit multiplication. Still, as weâ€™ll see, it is not enough to compensate for the four-fold increase in number of multiplications described above.</p>

<p>Overall, it looks that there is a substantial gap between the capabilities of the hardware and the capabilities of the C. The whole thing seems to be one incarnation of the Over-Generic Use of Abstractions [NoBugs11] which, as it often happens, leads to a waste of resources.<a href="#FN01"><sup>1</sup></a> However, the question is not exactly â€œWho is to blame?â€ [<a href="#[Herzen45]">Herzen45</a>], but â€œWhat Is to Be Done?â€ [<a href="#[Chernyshevsky63]">Chernyshevsky63</a>].</p>

<h3>So far, so bad</h3>

<p>So, what can be done in this regard? Iâ€™ve tried to perform some very basic experiments (based on things which were rather well-known some time ago, but apparently forgotten now) trying to implement <code>uint256_t</code> keeping in mind considerations outlined above, and to compare its performance with the performance of <code>boost::uint256_t</code> under recent versions of both MSVC and GCC (see the â€˜Benchmarkâ€™ section for details). The results Iâ€™ve managed to obtain show between 1.8x and 6.3x improvements over <code>boost::uint256_t</code> (both for addition and for multiplication), which, as I feel, indicate a step in the right direction.</p>

<h3>Addition</h3>

<p>To simplify the description, Iâ€™ll use <code>uint128_t</code> as an example; however, the same approach can be easily generalized to any other types (including <code>uint256_t</code> which Iâ€™ve used for benchmarks).</p>

<p>One thing which Iâ€™ve tried to do to deal with addition-with-carry, was:</p>

<pre class="programlisting">
struct myuint128_t { uint64_t lo; uint64_t hi; };
inline my_uint128_t add128(my_uint128_t a,
                           my_uint128_t b) {
  my_uint128_t ret;
  ret.cLo = a.lo + b.lo;
  ret.cHi = a.hi + b.hi + (ret.cLo&lt;b.lo);
  return ret;
}
</pre>

<p>This is mathematically equivalent to the 128-bit addition, and it (with subtle differences depending on compiler, etc.) indeed compiles into something like:</p>

<pre class="programlisting">
  mov rax, a.lo
  
  add rax, b.lo //got cLo in rax
  cmp rax, b.lo //setting carry flag to
                //â€œret.cLo &lt; b.loâ€ mov rbx, a.hi
  sbb rdx,rdx   //!
  neg rdx       //!
  add rbx, rdx
  add rbx, b.hi</pre>
  
<p>Compilers could do significantly better if they would remove two lines marked with (<code>
!</code>), and replace the <code>add</code> in the next line with <code>adc</code> (lines marked with '<code>!</code>' move carry to <code>rdx</code>, which is then added to addition of <code>rbx</code>, <code>rdx</code> â€“ <code>ad</code>
 will do the same thing and faster). Actually, compilers can go further and optimize away <code>cmp rax, b.lo</code> too (on the grounds that carry flag has already been set in the previous line). After these optimizations, the code would look as follows:</p>
 
<pre class="programlisting">
  mov rax, a.lo
  add rax, b.lo //got cLo in rax
  mov rbx, a.hi
  adc rbx, b.hi</pre>

<p>This looks as a perfect asm for our 128-bit addition, doesnâ€™t it? And from this point, compiler can optimize it from a pipelining optimization point of view to its heartâ€™s content.</p>

<p>It is interesting to note that despite all the unnecessary stuff, our C still performs reasonably well compared to <code>boost::</code>.</p>

<p><code>boost::</code> uses an alternative approach, splitting <code>uint128_t</code> into four <code>uint32_t</code>
â€™s and performing four additions; as my tests show, it seems to be slower (and I also feel that optimizing my code should be easier for compiler writers â€“ as outlined above, only two rather relatively minor optimizations are necessary to speed multi-word addition to the â€˜idealâ€™ level).</p>

<p>A word of caution: while this â€œa.hi + b.hi + (cLo&lt;b.lo)â€ technique seems to be handled reasonably optimally by most modern compilers, when trying to compile my code which uses <code>uint64_t</code> for 32-bit platforms, with both GCC and MSVC, they tend to generate very ugly code (with conditional jumps, pipeline stalls etc.); while I didnâ€™t see such behaviour in any other scenario â€“ I cannot be 100% sure that it is the only case when the compiler goes crazy. When compiling for 32-bit platforms, one may use similar technique, but using natively supported <code>uint32_t</code>â€™s instead of simulated <code>uint64_t</code>â€™s.</p>

<h3>Multiplication</h3>

<p>With multiplication, things tend to get significantly more complicated. To write 64-by-64 multiplication in bare C (and without <code>uint128_t</code> which is not natively supported by most of the compilers), one would need to write something like Listing 1.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
inline my_uint128_t mul64x64to128(uint64_t a,
                                  uint64_t b) {
  my_uint128_t ret;
  uint64_t t0 = ((uint64_t)(uint32_t)a) *
                ((uint64_t)(uint32_t)b);
  uint64_t t1 = (a&gt;&gt;32)*((uint64_t)(uint32_t)b) +
                (t0&gt;&gt;32);
  uint64_t t2 = (b&gt;&gt;32)*((uint64_t)(uint32_t)a) +
                ((uint32_t)t1);
  ret.lo = (((uint64_t)((uint32_t)t2))&lt;&lt;32) +
                ((uint32_t)t0);
  ret.hi = (a&gt;&gt;32) * (b&gt;&gt;32);
  ret.hi += (t2&gt;&gt;32) + (t1&gt;&gt;32);
  return ret;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>However, in most cases we can access 64-bit-by-64-bit-returning-128-bit multiplication instruction (which, as Iâ€™ve seen, is available on most CPUs in modern use). In particular, on MSVC there is an â€˜intrinsicâ€™ <code>_umul128</code>, which does exactly what we need. Then, our multiplication of <code>uint64_t</code>â€™s can be rewritten as:</p>

<pre class="programlisting">
  inline my_uint128_t mul64x64to128(uint64_t a,
                                    uint64_t b) {
    my_uint128_t ret;
    ret.lo = _umul128(a, b, &amp;(ret.hi));
    return ret;
  }</pre>
  
<p>In fact, a pure-C/C++ (without intrinsics) implementation uses 6 â€˜type Aâ€™ (64-bit-by-64-bit-returning-64-bit) multiplications, and an intrinsic-based one uses 3 multiplications, two being â€˜type Aâ€™ and one being â€˜type Bâ€™ (64-bit-by-64-bit-returning-128-bit). However, the difference in speed between the two is less than 2x, and I tend to attribute it to â€˜type Aâ€™ multiplications being faster than â€˜type Bâ€™ ones, at least on x64 CPUs Iâ€™ve tested my programs on. Still, intrinsic-based version looks significantly faster (than both my own pure-C implementation, and than <code>boost::</code> pure-C implementation).</p>

<p>Under GCC, there is no such intrinsic, but it can be simulated using the following 5-line GCC inline asm:</p>

<pre class="programlisting">
  void multiply(uint64_t&amp; rhi, uint64_t&amp; rlo,
                uint64_t a, uint64_t b)
  {
    __asm__(
    &quot;    mulq  %[b]\n&quot;
    :&quot;=d&quot;(rhi),&quot;=a&quot;(rlo)
    :&quot;1&quot;(a),[b]&quot;rm&quot;(b));
  }</pre>
  
<p>This one (from [<a href="#[Crypto++]">Crypto++</a>]) works, too:</p>

<pre class="programlisting">
  #define MultiplyWordsLoHi(p0, p1, a, b)  asm  \
  (&quot;mulq %3&quot; : &quot;=a&quot;(p0), &quot;=d&quot;(p1) : &quot;a&quot;(a),     \
  &quot;r&quot;(b) : &quot;cc&quot;);
</pre>

<h2>Benchmarks</h2>

<p>Benchmark results (benchmarking was performed for 256-bit integers, but the same approach can be generalized to big integers of any size; also, where applicable, boost version 1.55.0 has been used):</p>

<table class="journaltable">
	<tr>
		<th></th>
		<th>Visual Studio 2013</th>
		<th>GCC 4.8.1</th>
	</tr>
	<tr>
		<td>Addition (<code>boost::uint256_t</code>)</td>
		<td>0.0202 op/clock</td>
		<td>0.0202 op/clock</td>
	</tr>
	<tr>
		<td>Addition (<code>uint256_t </code>according to present article)</td>
		<td>0.0366 op/clock</td>
		<td>0.1282 op/clock</td>
	</tr>
	<tr>
		<td>Advantage over <code>boost::uint256_t</code></td>
		<td>1.83x</td>
		<td>6.34x</td>
	</tr>
	<tr>
		<td>Multiplication (<code>boost::uint256_t</code>)</td>
		<td>0.0160 op/clock</td>
		<td>0.0197op/clock</td>
	</tr>
	<tr>
		<td>Multiplication (<code>uint256_t</code> according to present article)</td>
		<td>0.0285 op/clock</td>
		<td>0.0549 op/clock</td>
	</tr>
	<tr>
		<td>Advantage over <code>boost::uint256_t</code></td>
		<td>1.78x</td>
		<td>2.79x</td>
	</tr>
</table>

<p>It should be noted that absolute â€˜op/clockâ€™ numbers should not be used for comparison between different computers (let alone comparison between different architectures/compilers).</p>


<h2>Conclusion</h2>

<p>Weâ€™ve taken a look at problems with big number arithmetic in C, and described some optimizations which may speed things up significantly, providing somewhere around 2x to 6x gains over the current <code>boost::</code> implementation.</p>

<p>Also note that while all examples were provided for x64 asm, very similar problems (and very similar solutions) seem to apply also to x86 and to ARM (both these platforms have both â€œadd with carryâ€ and â€œmultiply_with_double_width_resultâ€ asm-level operations, and these two operations are the only things necessary to deal with the big number arithmetic reasonably efficiently).</p>

<img src="http://accu.org/content/images/journals/ol119/IgnatchenkoIvanchykhin/IgnatchenkoIvanchykhin-1.png" />

<h2>References</h2>

<p class="bibliomixed"><a id="[Chernyshevsky63]"></a>[Chernyshevsky63] Nikolai Chernyshevsky, <em>What Is to Be Done?</em>, 1863</p>

<p class="bibliomixed"><a id="[Crypto++]"></a>[Crypto++] <a href="http://www.cryptopp.com/docs/ref/integer_8cpp_source.html" xml:link="simple" show="replace" actuate="user" CLASS="URL">http://www.cryptopp.com/docs/ref/integer_8cpp_source.html</a></p>

<p class="bibliomixed"><a id="[Herzen45]"></a>[Herzen45]  Alexander Herzen, <em>Who is to Blame?</em>, 1845â€“1846</p>

<p class="bibliomixed"><a id="[Loganberry04]"></a>[Loganberry04]  David â€˜Loganberryâ€™, Frithaes! â€“ an Introduction to Colloquial Lapine!, <a href="http://bitsnbobstones.watershipdown.org/lapine/overview.html" xml:link="simple" show="replace" actuate="user" CLASS="URL">http://bitsnbobstones.watershipdown.org/lapine/overview.html</a></p>

<p class="bibliomixed"><a id="[NoBugs11]"></a>[NoBugs11] Sergey Ignatchenko. â€˜Over-generic use of abstractions as a major cause of wasting resourcesâ€™. <em>Overload</em>, August 2011</p>

<h2>Acknowledgement</h2>

<p>Cartoon by Sergey Gordeev from Gordeev Animation Graphics, Prague.</p>

<p class="footnotes"></p>
<ol>
	<li><a id="FN01"></a>Here an abstraction, which I see as being over-generic, is an abstraction of C operators, which often return the result of the same size as the size of its arguments</li>
</ol>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
