| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| 17.1 Abstract Interface | ||
| 17.2 GMP driver | ||
| 17.3 BSD MP driver | ||
| 17.4 OpenSSL bignum driver | ||
| 17.5 Numeric driver interface |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following types are always defined in the same manner:
Lisp_Object type.
fixnum and bignum types.
integer and ratio types.
double.
float and bigfloat types.
rational and floating types.
real and complex types. Since there is
no complex type, this is currently equivalent to the real
type.
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:
bignump(x) is false for all x. Any attempt to create a
bignum causes an error to be raised.
ratiop(x) is false for all x. Any attempt to create a ratio
causes an error to be raised.
bigfloat(x) is false for all x. Any attempt to create a
bigfloat causes an error to be raised.
We (provide) the following symbols, so that Lisp code can
determine when the various numeric types are available for use:
(provide 'bignum) if HAVE_BIGNUM is set
(provde 'ratio) if HAVE_RATIO is set
(provide 'bigfloat) if HAVE_BIGFLOAT is set
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.
make_fixnum(x) if x is guaranteed to fit into a Lisp fixnum.
make_integer(x) if x is a signed C integer of any type.
make_unsigned_integer(x) if x is an unsigned C integer of any type.
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] | [ ? ] |
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] | [ ? ] |
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] | [ ? ] |
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.1 Bignum interface | ||
| 17.5.2 Ratio interface | ||
| 17.5.3 Bigfloat interface |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
b can be
used. This function typically allocates memory for the bignum.
b, typically
deallocating the memory dedicated to that bignum. The object referred
to by b must not be accessed after this call returns.
b is positive, zero, or negative, respectively.
b is an even number, zero if it is odd.
b is an odd number, zero if it is even.
b can be represented in a C
int, zero otherwise.
b can be represented in a C
unsigned int, zero otherwise.
b can be represented in a C
long int, zero otherwise.
b can be represented in a C
unsigned long int, zero otherwise.
b can be represented in a C
long long int, zero otherwise.
b can be represented in a C
unsigned long long int, zero otherwise.
b into a string representation using the given base,
which is an integer between 2 and 36, inclusive.
b into a C int. If b does not fit into a
C int, the results are undefined.
b into a C unsigned int. If b does not
fit into a C unsigned int, the results are undefined.
b into a C long int. If b does not fit
into a C long int, the results are undefined.
b into a C unsigned long int. If b does
not fit into a C unsigned long int, the results are undefined.
b into a C long long int. If b does not
fit into a C long long int, the results are undefined.
b into a C unsigned long long int. If b
does not fit into a C unsigned long long int, the results are
undefined.
b into a C double. If b is too large to
represent as a C double, the results are implementation-specific.
Typically, the result is positive or negative infinity, for
implementations that can represent infinity.
b2 to the bignum b1 by copying.
b the value of the integer contained in string s,
which is a number in base base.
b the value of the long integer l.
b the value of the unsigned long integer l.
b the value of the long long integer l.
b the value of the unsigned long long integer l.
b to the truncated value of d; i.e, the fractional
part of d is dropped.
b to the truncated value of r; i.e., the
non-integer part of r is dropped. This function exists only if
the driver supports ratios.
b to the truncated value of f; i.e., the
fractional part of f is dropped. This function exists only if
the driver supports bigfloats.
b1 is
greater than, equal to, or less than b2, respectively.
b1 is less than b2, zero otherwise.
b1 is less than or equal to b2, zero
otherwise.
b1 is equal to b2, zero otherwise.
b1 is greater than or equal to b2, zero
otherwise.
b1 is greater than b2, zero otherwise.
b to the negation of b2.
b to the absolute value of b2.
b to the sum of b1 and b2.
b to the value of b1 minus b2.
b to the product of b1 and b2.
b1 is evenly divisible by b2.
b to the truncated value of b1 divided by b2.
That is, this function rounds toward zero.
b to the ceiling of b1 divided by b2. That
is, ths function rounds toward positive infinity.
b to the floor of b1 divided by b2. That is,
ths function rounds toward negative infinity.
b to b1 mod b2; i.e., the remainder after
dividing b1 by b2. The sign is ignored; b is
always nonnegative after this operation.
res to b to the powth power.
res to the greatest common divisor of b1 and
b2.
res to the least common multiple of b1 and b2.
res to the bitwise AND of b1 and b2.
res to the bitwise inclusive OR of b1 and b2.
res to the bitwise exclusive OR of b1 and b2.
res to the bitwise NOT of b.
bitth bit of b to one, where the zeroth bit is
the least significant.
bitth bit of b (i.e., sets it to zero), where
the zeroth bit is the least significant.
bitth bit of b, where the zeroth bit is the
least significant.
res to the value of b shifted left by bits
bits.
res to the value of b shifted right by bits
bits.
res to a random number between zero (inclusive) and
limit (exclusive).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
r can be
used. This function typically allocates memory for the ratio.
r, typically
deallocating the memory dedicated to that ratio. The object referred
to by r must not be accessed after this call returns.
r is positive, zero, or negative, respectively.
r.
r.
r.
r into a string representation using the given base,
which is an integer between 2 and 36, inclusive.
r into a C int by truncating the ratio. If the
truncation of r does not fit into a C int, the results are
undefined.
r into a C unsigned int by truncating the ratio.
If the truncation of r does not fit into a C unsigned int,
the results are undefined.
r into a C long int by truncating the ratio. If
the truncation of r does not fit into a C long int, the
results are undefined.
r into a C unsigned long int by truncating the
ratio. If r does not fit into a C unsigned long int, the
results are undefined.
r into a C double. If r is too large to
represent as a C double, the results are implementation-specific.
Typically, the result is positive or negative infinity, for
implementations that can represent infinity.
r2 to the ratio r1 by copying.
r the value of the ratio contained in string s,
which is a pair of numbers in base base separated by a forward
slash.
r the value of the long integer l.
r the value of the unsigned long integer l.
r to the exact value of d. There is no rounding.
r to the value of b; i.e., the denominator is set
to one.
b to the exact value of f. There is no rounding.
This function exists only if the driver supports bigfloats.
r the canonicalized ratio resulting from the division of
num by den.
r the canonicalized ratio resulting from the division of
num by den.
r the canonicalized ratio resulting from the division of
num by den.
r1 is
greater than, equal to, or less than r2, respectively.
r1 is less than r2, zero otherwise.
r1 is less than or equal to r2, zero
otherwise.
r1 is equal to r2, zero otherwise.
r1 is greater than or equal to r2, zero
otherwise.
r1 is greater than r2, zero otherwise.
q to the negation of q2.
q to the absolute value of q2.
q to the inverse of q2.
res to the sum of q1 and q2.
res to the value of q1 minus q2.
res to the product of q1 and q2.
res to the value of q1 divided by q2.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
f can be
used. This function typically allocates memory for the bigfloat.
bigfloat_init, but also sets the precision of f to
at least prec bits.
f, typically
deallocating the memory dedicated to that bigfloat. The object referred
to by f must not be accessed after this call returns.
f is positive, zero, or negative, respectively.
f in bits.
f to at least prec bits.
f into a string representation using the given base,
which is an integer between 2 and 36, inclusive.
f into a C int by truncating the bigfloat. If the
truncation of f does not fit into a C int, the results are
undefined.
f into a C unsigned int by truncating the bigfloat.
If the truncation of f does not fit into a C unsigned int,
the results are undefined.
f into a C long int by truncating the bigfloat. If
the truncation of f does not fit into a C long int, the
results are undefined.
f into a C unsigned long int by truncating the
bigfloat. If f does not fit into a C unsigned long int, the
results are undefined.
f into a C double. If f is too large to
represent as a C double, the results are implementation-specific.
Typically, the result is positive or negative infinity, for
implementations that can represent infinity.
f2 to the bigfloat f1 by copying.
f the value of the bigfloat contained in string s.
f the value of the long integer l.
f the value of the unsigned long integer l.
f the value of d.
f to the value of b.
b to the result of dividing the numerator of r by
its denominator.
f1 is
greater than, equal to, or less than f2, respectively.
f1 is less than f2, zero otherwise.
f1 is less than or equal to f2, zero
otherwise.
f1 is equal to f2, zero otherwise.
f1 is greater than or equal to f2, zero
otherwise.
f1 is greater than f2, zero otherwise.
f to the negation of f2.
f to the absolute value of f2.
res to the sum of f1 and f2.
res to the value of f1 minus f2.
res to the product of f1 and f2.
res to the value of f1 divided by f2.
res to the ceiling of f.
res to the floor of f.
res to the truncation of f.
res to the square root of f.
res to the value of f raised to the expth power.
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on September 25, 2026 using texi2html 1.82.