<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>GMP</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css">
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20201002</a>
</div>
</div>
<div id="header">
<h1>GMP</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>The <a href="https://gmplib.org">GMP</a> library (GNU Multiple Precision
arithmetic library) is a library for arbitrary precision integer
arithmetic.  MLton uses the GMP library to implement the
<a href="BasisLibrary"> Basis Library</a> <span class="monospaced">IntInf</span> module.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_known_issues">Known issues</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
There is a known problem with the GMP library (prior to version
4.2.x), where it requires a lot of stack space for some computations,
e.g. <span class="monospaced">IntInf.toString</span> of a million digit number.  If you run with
stack size limited, you may see a segfault in such programs.  This
problem is mentioned in the <a href="https://gmplib.org/#FAQ">GMP FAQ</a>, where
they describe two solutions.
</p>
<div class="ulist"><ul>
<li>
<p>
Increase (or unlimit) your stack space.  From your program, use
<span class="monospaced">setrlimit</span>, or from the shell, use <span class="monospaced">ulimit</span>.
</p>
</li>
<li>
<p>
Configure and rebuild <span class="monospaced">libgmp</span> with <span class="monospaced">--disable-alloca</span>, which will
cause it to allocate temporaries using <span class="monospaced">malloc</span> instead of on the
stack.
</p>
</li>
</ul></div>
</li>
<li>
<p>
On some platforms, the GMP library may be configured to use one of
multiple ABIs (Application Binary Interfaces).  For example, on some
32-bit architectures, GMP may be configured to represent a limb as
either a 32-bit <span class="monospaced">long</span> or as a 64-bit <span class="monospaced">long long</span>.  Similarly, GMP
may be configured to use specific CPU features.
</p>
<div class="paragraph"><p>In order to efficiently use the GMP library, MLton represents an
<span class="monospaced">IntInf.int</span> value in a manner compatible with the GMP library&#8217;s
representation of a limb.  Hence, it is important that MLton and the
GMP library agree upon the representation of a limb.</p></div>
<div class="ulist"><ul>
<li>
<p>
When using a source package of MLton, building will detect the
GMP library&#8217;s representation of a limb.
</p>
</li>
<li>
<p>
When using a binary package of MLton that is dynamically linked
against the GMP library, the build machine and the install machine
must have the GMP library configured with the same representation of
a limb.  (On the other hand, the build machine need not have the GMP
library configured with CPU features compatible with the install
machine.)
</p>
</li>
<li>
<p>
When using a binary package of MLton that is statically linked
against the GMP library, the build machine and the install machine
need not have the GMP library configured with the same
representation of a limb.  (On the other hand, the build machine must
have the GMP library configured with CPU features compatible with
the install machine.)
</p>
<div class="paragraph"><p>However, MLton will be configured with the representation of a limb
from the GMP library of the build machine.  Executables produced by
MLton will be incompatible with the GMP library of the install
machine.  To <em>reconfigure</em> MLton with the representation of a limb
from the GMP library of the install machine, one must edit:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>/usr/lib/mlton/self/sizes</pre>
</div></div>
<div class="paragraph"><p>changing the</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>mplimb = ??</pre>
</div></div>
<div class="paragraph"><p>entry so that <span class="monospaced">??</span> corresponds to the bytes in a limb; and, one must edit:</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>/usr/lib/mlton/sml/basis/config/c/arch-os/c-types.sml</pre>
</div></div>
<div class="paragraph"><p>changing the</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>(* from "gmp.h" *)
structure C_MPLimb = struct open Word?? type t = word end
functor C_MPLimb_ChooseWordN (A: CHOOSE_WORDN_ARG) = ChooseWordN_Word?? (A)</pre>
</div></div>
<div class="paragraph"><p>entries so that <span class="monospaced">??</span> corresponds to the bits in a limb.</p></div>
</li>
</ul></div>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
