GMP
===

The https://gmplib.org[GMP] library (GNU Multiple Precision
arithmetic library) is a library for arbitrary precision integer
arithmetic.  MLton uses the GMP library to implement the
<:BasisLibrary: Basis Library> `IntInf` module.

== Known issues ==

* 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. `IntInf.toString` 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 https://gmplib.org/#FAQ[GMP FAQ], where
they describe two solutions.

** Increase (or unlimit) your stack space.  From your program, use
`setrlimit`, or from the shell, use `ulimit`.

** Configure and rebuild `libgmp` with `--disable-alloca`, which will
cause it to allocate temporaries using `malloc` instead of on the
stack.

* 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 `long` or as a 64-bit `long long`.  Similarly, GMP
may be configured to use specific CPU features.
+
In order to efficiently use the GMP library, MLton represents an
`IntInf.int` value in a manner compatible with the GMP library's
representation of a limb.  Hence, it is important that MLton and the
GMP library agree upon the representation of a limb.

** When using a source package of MLton, building will detect the
GMP library's representation of a limb.

** 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.)

** 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.)
+
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 _reconfigure_ MLton with the representation of a limb
from the GMP library of the install machine, one must edit:
+
----
/usr/lib/mlton/self/sizes
----
+
changing the
+
----
mplimb = ??
----
+
entry so that `??` corresponds to the bytes in a limb; and, one must edit:
+
----
/usr/lib/mlton/sml/basis/config/c/arch-os/c-types.sml
----
+
changing the
+
----
(* from "gmp.h" *)
structure C_MPLimb = struct open Word?? type t = word end
functor C_MPLimb_ChooseWordN (A: CHOOSE_WORDN_ARG) = ChooseWordN_Word?? (A)
----
+
entries so that `??` corresponds to the bits in a limb.
