[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17. Numeric Types


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.1 Abstract Interface

The following types are always defined in the same manner:

The remaining types (bignum, ratio, and bigfloat) are defined by library-specific drivers, as detailed in the remaining sections of this chapter. A given driver may define one or more of these types to be empty. In fact, the default configuration makes all three types empty, so that integer and rational are the same types as fixnum, etc. The configure script sets zero or more of the symbols HAVE_BIGNUM, HAVE_RATIO, and HAVE_BIGFLOAT to indicate which types are nonempty. These are the consequences of an empty type:

We (provide) the following symbols, so that Lisp code can determine when the various numeric types are available for use:

C code should use one of the following macros to create a Lisp integer, depending on the type of the C integer to be converted.

The make_integer(x) and make_unsigned_integer(x) macros both create a fixnum if possible, and a bignum otherwise. The value x can be a C integer of any size up to and including the size of a C long long int. The make_fixnum(x) macro is intended to signal the programmer’s intent that only fixnum-sized integers will be used. In some cases, if the compiler cannot prove that the parameter is always in the fixnum range, this macro is also a code optimization.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.2 GMP driver

The GNU Multiple Precision library (http://gmplib.org/) and its fork, the Multiple Precision Integers and Rationals library (http://www.mpir.org/), provide large integers, ratios, and floating point numbers. The same driver supports both GMP and MPIR, and provides all 3 of the optional types: bignums, ratios, and bigfloats, implemented with the types mpz_t, mpq_t, and mpf_t, respectively. Most of the abstract numeric interface is defined in terms of macros that expand directly into GMP or MPIR API calls, since GMP and MPIR provide a rich interface.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.3 BSD MP driver

The BSD MP interface is less rich than its GMP and MPIR counterparts. It provides only large integers, implemented as type MINT *; ratios and bigfloats are not available. BSD MP libraries are supplied by multiple vendors, with some small variations. This driver is intended to work with any variation.

Some BSD MP libraries use function names that are likely cause name collisions, such as gcd, mult, and pow. Others follow the Single Unix Specification recommendation (see https://www.freebsd.org/cgi/man.cgi?query=mp_mcmp&apropos=0&sektion=0&manpath=SunOS+5.10&arch=default&format=html) and prefix all of the function names with “mp_”: mp_gcd, mp_mult, mp_pow, etc. The XEmacs configure script detects which of the two interfaces is in use, and defines MP_PREFIX for the latter case. We use macros (MP_MULT, etc.) to hide this difference.

Another variation is that some, but not all, BSD MP libraries supply a function named move or mp_move that copies its first argument into its second argument. We define the move operation as setting the destination number to the source number plus zero for those libraries that do not supply this function.

The BSD MP interface does not directly support many of the operations that we need, so the implementations sometimes consist of complex functions. See the implementation of bignum_random, for example.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.4 OpenSSL bignum driver

OpenSSL has its own bignum interface, which is the actual implementation behind FreeBSD’s libmp. It only has arbitrary precision integers, and thus, by lacking ratios and bigfloats, is less expressive than the GMP and MPIR interfaces. On the other hand, the API has proper error return codes for out of memory scenarioes, unlike the GMP and MPIR interfaces, which call abort(3) internally, terminating the whole process.

Most of the internal bignum API of XEmacs have direct representations in the OpenSSL API, but some operations, like bignum_set_string or the bit operations require explicit implementations.

The other major BSD derivatives besides FreeBSD have altogether dropped the BSD MP library (see section BSD MP driver), to the point that they don’t even have it in their third party software repositories. In essence, the BSD MP interface has been deprecated. On the other hand, OpenSSL (or LibreSSL) is included in virtually all modern operating system distributions, including Microsoft Windows.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.5 Numeric driver interface


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.5.1 Bignum interface

Each bignum implementation defines HAVE_BIGNUM and an appropriate bignum type. For example, the GMP driver uses this definition:

 
typedef mpz_t bignum;

The following names are defined for each driver, either as real C functions or as C preprocessor macros.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.5.2 Ratio interface

Each ratio implementation defines HAVE_RATIO and an appropriate ratio type. For example, the GMP driver uses this definition:

 
typedef mpq_t ratio;

The following names are defined for each driver, either as real C functions or as C preprocessor macros.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.5.3 Bigfloat interface

Each bigfloat implementation defines HAVE_BIGFLOAT and an appropriate bigfloat type. For example, the GMP driver uses this definition:

 
typedef mpf_t bigfloat;

The following names are defined for each driver, either as real C functions or as C preprocessor macros.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on September 25, 2026 using texi2html 1.82.