Introduction

GHC currently implements the Integer and Fractional types by using the ​The GNU MP Bignum Library (GMP) which supports arbitrary precision mathematical calculations. GMP is fast, memory efficient, and offers many high level signed integer functions (140 of them), as well as many rational and floating point arithmetic functions. The current GHC implementation only uses those functions necessary for the Prelude.

GMP memory is integrated with the RunTime System's (RTS's) Storage Manager (SM)--the RTS's Garbage Collector (GC). GMP memory is allocated from the GC heap, so values produced by GMP are under the control of the RTS and its GC. The current implementation is memory efficient wile allowing the RTS and its GC to maintain control of GMP evaluations.

If you want to help with replacing GMP or do it yourself, you will have to work with the GC and RTS system. The parts you will have to modify are written in C and C--, with configuration and assembly done through the Makefiles. You should have an understanding of:

how the GC works and how memory from GMP is integrated with it;

some C--/Cmm (this is fairly basic if you know C well, though the same adage for knowing C well holds for C--: if you know Assembler well enough to understand and debug C in it you will be much better off), the only real documentation on C-- itself is in the ​C-- manual (PDF), from cminusminus.org; the implementation of C-- for GHC is performed by several Haskell modules in the directory compiler/cmm/ of the HEAD branch, see ​http://darcs.haskell.org/ghc), and see the new Commentary Cmm page; and,

Caveat

Beware! The main interest here is replacing GMP--GHC is still belongs to the University of Glasgow and those in charge still retain the purview to accept or reject a proposed solution.

Reasons for Replacing GMP as the Bignum library

There are several problems with the current GMP implementation:

Licensing

GMP is licensed under the ​GNU Lesser General Public License (LGPL), a kind of "copyleft" license. According to the terms of the LGPL, paragraph 5, you may distribute a program that is designed to be compiled and dynamically linked with the library under the terms of your choice (i.e., commercially) but if your program incorporates portions of the library, if it is linked statically, then your program is a "derivative"--a "work based on the library"--and according to paragraph 2, section c, you "must cause the whole of the work to be licensed" under the terms of the LGPL (including for free).

The LGPL licensing for GMP is a problem for the overall licensing of binary programs compiled with GHC because most distributions (and builds) of GHC use static libraries. (Dynamic libraries are currently distributed only for OS X.) The LGPL licensing situation may be worse: even though ​The Glasgow Haskell Compiler License is essentially a "free software" license (BSD3), according to paragraph 2 of the LGPL, GHC must be distributed under the terms of the LGPL!

Memory Structure; Simultaneous Access to GMP by Foreign (C) code in the Same Binary

In the current GMP implementation, GMP is configured to use GHC's GC memory and GMP can only have one allocator for memory. Since any single binary containing Haskell code compiled with GHC contains the RTS and GMP, C code--including foreign calls to GMP from Haskell code (say you need a GMP function that is not a primitive)--in the same binary cannot use GMP. This problem was noted in ​bug Ticket #311. The Simon Peyton-Jones suggested that a simple renaming of GHC-GMP functions would solve this problem and Bulat Ziganshin suggested simply using an automated tool to do this. See ​Replacement for GMP. Different function names would make GMP into a separate, custom GHC library leaving the C part of the program free to use GMP.

GHC does not have a custom-modified version of GMP (in fact, GHC uses the system build of GMP if that is available). The memory configuration of GMP uses GMP's ​Custom Allocation routines. Alternative libraries may not have this facility built in.

Other Improvements to Integer

Most of the suggestions in this section come from discussions in the glasgow-haskell-users list thread ​returning to Cost of Integer. In particular, ​John Meacham's suggestion to use a ForeignPtr to data held by the normal GMP system library and store the value in an unboxed Int if the number of significant digits in Integer could fit into the size of an Int.

where the Int# counts the number of ​limbs (a GMP term referring to parts of a multi-precision number that fit into a 32 or 64 bit word, depending on the machine) and the ByteArr# is the actual array in RTS-GC memory holding the limbs. The sign of the Int# is used to indicate the sign of the number represented by the ByteArr#.

This current implementation of Integer means that there are two separate constructors for small and large Integers (S# Int# and J# Int# ByteArr#). The suggestion discussed by ​John Meacham, ​Lennart Augustsson, ​Simon Marlow and ​Bulat Ziganshin was to change the representation of Integer so the Int# does the work of S# and J#: the Int# could be either a pointer to the Bignum library array of limbs or, if the number of significant digits could fit into say, 31 bits, to use the extra bit as an indicator of that fact and hold the entire value in the Int#, thereby saving the memory from S# and J#.

interoperability between Haskell and other languages, especially C, would be more difficult so you would have to define a new primitive, say #Int30 for the representation; and,

representing a Haskell constructor (the Int#) inside a pointer--a bit-size constructor--would limit the number of constructors you would be able to have (depending on the size of a pointer object, say the C99 uintptr_t, on a particular machine).

Files related to GMP in the GHC Compiler Source Code

Note: references are relative to the main directory of the source distribution; links below are to the darcs repository at ​http://darcs.haskell.org/ghc, created with the [[GhcFile(path/to/file)]] script (see Commentary).

results from initialising each struct (mp_tmp2, etc.) on each call, in order to convert the data from the J# Int# ByteArray# in the RTS to the GMP structure before passing it to GMP. There are at least two possible alternatives to this:

(a) wrap the replacement MP-library array/structure for arbitrary precision integers in a closure so you do not have to rebuild the struct from on each MP-library call; or

(b) use ForeignPtr (in Cmm, Weak Pointers--difficult to implement) to foreign threads holding the the struct/array

Related to replacing GMP, some operations in CgPrimOP.hs such as IntAddCOp may benefit from operations defined in a replacement MP library (or, more generally, simple optimisation). For example:

emitPrimOp[res_r,res_c]IntAddCOp[aa,bb]live{-
With some bit-twiddling, we can define int{Add,Sub}Czh portably in
C, and without needing any comparisons. This may not be the
fastest way to do it - if you have better code, please send it! --SDM
Return : r = a + b, c = 0 if no overflow, 1 on overflow.
We currently don't make use of the r value if c is != 0 (i.e.
overflow), we just convert to big integers and try again. This
could be improved by making r and c the correct values for
plugging into a new J#.
{ r = ((I_)(a)) + ((I_)(b));\
c = ((StgWord)(~(((I_)(a))^((I_)(b))) & (((I_)(a))^r)))\
>> (BITS_IN (I_) - 1);\
}
Wading through the mass of bracketry, it seems to reduce to:
c = ( (~(a^b)) & (a^r) ) >>unsigned (BITS_IN(I_)-1)
-}=stmtsC[CmmAssignres_r(CmmMachOpmo_wordAdd[aa,bb]),CmmAssignres_c$CmmMachOpmo_wordUShr[CmmMachOpmo_wordAnd[CmmMachOpmo_wordNot[CmmMachOpmo_wordXor[aa,bb]],CmmMachOpmo_wordXor[aa,CmmRegres_r]],CmmLit(mkIntCLit(wORD_SIZE_IN_BITS-1))]]

If an integer add were to overflow here, the addition operation would be performed twice; even if the integer add did not overflow one extra operation is performed. Is this an acceptable price for no comparisons?