2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
2017-02-20 15:28:46 +00:00
|
|
|
* YAP Prolog @(#)YapEval.h 1.2
|
2001-04-09 20:54:03 +01:00
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
2017-02-20 15:28:46 +00:00
|
|
|
* File: YapEval.h *
|
2001-04-09 20:54:03 +01:00
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: arithmetical functions info *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
/**
|
|
|
|
|
2017-02-20 15:28:46 +00:00
|
|
|
@file YapEval.h
|
2016-01-31 10:23:03 +00:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
@defgroup arithmetic Arithmetic in YAP
|
2014-04-21 11:14:18 +01:00
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
@ingroup builtins
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2014-12-24 15:32:29 +00:00
|
|
|
+ See @ref arithmetic_preds for the predicates that implement arithment
|
2014-09-11 20:06:57 +01:00
|
|
|
|
|
|
|
+ See @ref arithmetic_cmps for the arithmetic comparisons supported in YAP
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
+ See @ref arithmetic_operators for what arithmetic operations are supported
|
|
|
|
in YAP
|
2014-09-11 20:06:57 +01:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
YAP supports several different numeric types:
|
|
|
|
<ul>
|
|
|
|
<li><b>Tagged integers</b><p>
|
|
|
|
|
|
|
|
YAP supports integers of word size: 32 bits on 32-bit machines, and
|
|
|
|
64-bits on 64-bit machines.The engine transprently tags smaller
|
|
|
|
integers are tagged so that they fit in a single word. These are the
|
|
|
|
so called <em>tagged integers</em>.
|
|
|
|
|
|
|
|
<li><b>Large integers</b><p>
|
|
|
|
Larger integers that still fit in a cell
|
|
|
|
are represented in the Prolog goal stack. The difference between
|
|
|
|
these integers and tagged integers should be transparent to the programmer.
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li><b>Multiple Precision Integers</b><p>
|
|
|
|
|
|
|
|
When YAP is built using the GNU multiple precision arithmetic library
|
|
|
|
(GMP), integer arithmetic is unbounded, which means that the size of
|
|
|
|
integers is only limited by available memory. The type of integer
|
|
|
|
support can be detected using the Prolog flags bounded, min_integer
|
|
|
|
and max_integer. As the use of GMP is default, most of the following
|
|
|
|
descriptions assume unbounded integer arithmetic.
|
|
|
|
|
|
|
|
</li> <li><b>Rational numbers (Q)</b><p> Rational numbers are
|
|
|
|
quotients of two integers. Rational arithmetic is provided if GMP is
|
|
|
|
used. Rational numbers that are returned from is/2 are canonical,
|
|
|
|
which means the denominator _M_ is positive and that the numerator _N_
|
|
|
|
and _M_ have no common divisors. Rational numbers are introduced in
|
2014-12-24 15:32:29 +00:00
|
|
|
the computation using the rational/1,
|
|
|
|
rationalize/1 or the rdiv/2
|
2014-04-21 11:14:18 +01:00
|
|
|
(rational division) function.
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li><b>Floating point numbers</b><p>
|
|
|
|
|
|
|
|
Floating point numbers are represented using the C-type double. On
|
|
|
|
most today platforms these are 64-bit IEEE-754 floating point
|
2014-12-24 15:32:29 +00:00
|
|
|
numbers. YAP now includes the built-in predicates isinf/1 and to
|
|
|
|
isnan/1 tests. </li> </ul>
|
2014-04-21 11:14:18 +01:00
|
|
|
|
|
|
|
Arithmetic functions that require integer arguments accept, in addition
|
|
|
|
to integers, rational numbers with denominator `1' and floating point
|
|
|
|
numbers that can be accurately converted to integers. If the required
|
|
|
|
argument is a float the argument is converted to float. Note that
|
|
|
|
conversion of integers to floating point numbers may raise an overflow
|
|
|
|
exception. In all other cases, arguments are converted to the same type
|
|
|
|
using the order integer to rational number to floating point number.
|
|
|
|
|
|
|
|
Evaluation generates the following _Call_
|
|
|
|
exceptions:
|
|
|
|
|
|
|
|
@exception "error(instantiation_error, Call )" if not ground
|
|
|
|
|
|
|
|
@exception "type_error(evaluable( V ), Call)" if not evaluable term
|
|
|
|
@exception "type_error(integer( V ), Call)" if must be integer
|
|
|
|
@exception "type_error(float( V ), Call)" if must be float
|
|
|
|
|
|
|
|
@exception "domain_error(out_of_range( V ), Call)" if argument invalid
|
2016-03-30 17:35:03 +01:00
|
|
|
@exception "domain_error(not_less_than_zero( V ), Call)" if argument must be
|
|
|
|
positive or zero
|
|
|
|
|
|
|
|
@exception "evaluation_error(undefined( V ), Call)" result is not defined
|
|
|
|
(nan)
|
|
|
|
@exception "evaluation_error(overflow( V ), Call)" result is arithmetic
|
|
|
|
overflow
|
2014-04-21 11:14:18 +01:00
|
|
|
|
2016-01-31 10:23:03 +00:00
|
|
|
@tableofcontents
|
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
@secreflist
|
2014-09-15 09:13:50 +01:00
|
|
|
@refitem is/2
|
|
|
|
@refitem isnan/1
|
2014-04-21 11:14:18 +01:00
|
|
|
@endsecreflist
|
|
|
|
|
2016-01-31 10:23:03 +00:00
|
|
|
@{
|
2014-04-21 11:14:18 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
2016-01-31 10:23:03 +00:00
|
|
|
#ifndef EVAL_H
|
|
|
|
#define EVAL_H 1
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* C library used to implement floating point functions */
|
2002-06-25 17:13:56 +01:00
|
|
|
#if HAVE_MATH_H
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <math.h>
|
2002-06-25 17:13:56 +01:00
|
|
|
#endif
|
2009-10-20 09:21:59 +01:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
2008-03-25 22:03:14 +00:00
|
|
|
#ifdef HAVE_IEEEFP_H
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
2008-03-25 22:03:14 +00:00
|
|
|
#ifdef HAVE_LIMITS_H
|
2001-04-09 20:54:03 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2014-10-15 11:06:07 +01:00
|
|
|
#ifdef HAVE_FENV_H
|
|
|
|
#include <fenv.h>
|
|
|
|
#endif
|
2015-01-05 16:06:32 +00:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef LONG_MAX
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Int_MAX LONG_MAX
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Int_MAX ((Int)((~((CELL)0)) >> 1))
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
#ifdef LONG_MIN
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Int_MIN LONG_MIN
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Int_MIN (-Int_MAX - (CELL)1)
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#define PLMAXTAGGEDINT (MAX_ABS_INT - ((CELL)1))
|
|
|
|
#define PLMINTAGGEDINT (-MAX_ABS_INT)
|
2013-11-15 15:45:55 +00:00
|
|
|
|
2013-11-05 17:12:05 +00:00
|
|
|
#define PLMAXINT Int_MAX
|
|
|
|
#define PLMININT Int_MIN
|
|
|
|
|
2014-11-27 15:11:26 +00:00
|
|
|
#ifndef INFINITY
|
2016-03-30 17:35:03 +01:00
|
|
|
#define INFINITY (1.0 / 0.0)
|
2014-11-27 15:11:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NAN
|
2016-03-30 17:35:03 +01:00
|
|
|
#define NAN (0.0 / 0.0)
|
2014-11-27 15:11:26 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-06 13:58:08 +01:00
|
|
|
/**
|
|
|
|
* @addtogroup arithmetic_operators
|
2016-03-30 17:35:03 +01:00
|
|
|
* @enum arith0_op constant operators
|
2014-05-06 13:58:08 +01:00
|
|
|
* @brief specifies the available unary arithmetic operators
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
typedef enum {
|
2014-05-06 13:58:08 +01:00
|
|
|
/** pi [ISO]
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
An approximation to the value of <em>pi</em>, that is, the ratio of a
|
|
|
|
circle's circumference to its diameter.
|
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_pi,
|
2014-05-06 13:58:08 +01:00
|
|
|
/** e
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
Euler's number, the base of the natural logarithms (approximately
|
|
|
|
2.718281828).
|
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_e,
|
2014-05-06 13:58:08 +01:00
|
|
|
/** epsilon
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
The difference between the float `1.0` and the next largest floating point
|
|
|
|
number.
|
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
*/
|
2009-10-20 09:21:59 +01:00
|
|
|
op_epsilon,
|
2014-05-06 13:58:08 +01:00
|
|
|
/** inf
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
Infinity according to the IEEE Floating-Point standard. Note that
|
|
|
|
evaluating this term will generate a domain error in the `iso` language mode.
|
|
|
|
Also note that
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
|
|
|
* ?- +inf =:= -inf.
|
|
|
|
* false.
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-03-30 17:35:03 +01:00
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_inf,
|
|
|
|
op_nan,
|
|
|
|
op_random,
|
|
|
|
op_cputime,
|
|
|
|
op_heapused,
|
|
|
|
op_localsp,
|
|
|
|
op_globalsp,
|
|
|
|
op_b,
|
|
|
|
op_env,
|
|
|
|
op_tr,
|
|
|
|
op_stackfree
|
|
|
|
} arith0_op;
|
|
|
|
|
2014-05-06 13:58:08 +01:00
|
|
|
/**
|
|
|
|
* @addtogroup arithmetic_operators
|
2016-03-30 17:35:03 +01:00
|
|
|
* @enum arith1_op unary operators
|
2014-05-06 13:58:08 +01:00
|
|
|
* @brief specifies the available unary arithmetic operators
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
typedef enum {
|
2014-05-12 16:36:41 +01:00
|
|
|
/** \+ _X_: the value of _X_ .
|
2016-03-30 17:35:03 +01:00
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
|
|
|
* X =:= +X.
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_uplus,
|
2014-05-12 16:36:41 +01:00
|
|
|
/** \- _X_: the complement of _X_ .
|
2016-03-30 17:35:03 +01:00
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
|
|
|
* 0-X =:= -X.
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_uminus,
|
2014-05-12 16:36:41 +01:00
|
|
|
/** \\ _X_, The bitwise negation of _X_ .
|
2016-03-30 17:35:03 +01:00
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
2014-05-12 16:36:41 +01:00
|
|
|
* \X /\ X =:= 0.
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
*
|
2016-03-30 17:35:03 +01:00
|
|
|
* Note that the number of bits of an integer is at least the size in bits of
|
|
|
|
* a Prolog term cell.
|
2014-05-06 13:58:08 +01:00
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_unot,
|
2014-05-12 16:36:41 +01:00
|
|
|
/** exp( _X_ ), natural exponentiation of _X_ .
|
2016-03-30 17:35:03 +01:00
|
|
|
*
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
2014-05-12 16:36:41 +01:00
|
|
|
* X = 0.0, abs(1.0 - exp( _X_ )) < 0.0001
|
2014-05-06 13:58:08 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
*
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_exp,
|
2014-05-12 16:36:41 +01:00
|
|
|
/** log( _X_ ), natural logarithm of _X_ .
|
|
|
|
*
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
|
|
|
* X = 1.0, abs( log( exp( _X_ )) -1.0) < 0.0001
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
*
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_log,
|
2014-05-06 23:01:04 +01:00
|
|
|
/** log10( _X_ ) [ISO]
|
2016-03-30 17:35:03 +01:00
|
|
|
*
|
2014-05-06 23:01:04 +01:00
|
|
|
* Decimal logarithm.
|
|
|
|
*
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.prolog}
|
2016-03-30 17:35:03 +01:00
|
|
|
* ?- between(1, 10, I), Delta is log10(I*10) + log10(1/(I*10)), format('0
|
|
|
|
* == ~3g~n',[Delta]), fail.
|
2014-05-06 23:01:04 +01:00
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 0
|
|
|
|
* 0 == 2.22e-16
|
|
|
|
* 0 == 0
|
|
|
|
* false.
|
2014-05-07 09:38:56 +01:00
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-05-06 23:01:04 +01:00
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
op_log10,
|
|
|
|
op_sqrt,
|
|
|
|
op_sin,
|
|
|
|
op_cos,
|
|
|
|
op_tan,
|
|
|
|
op_sinh,
|
|
|
|
op_cosh,
|
|
|
|
op_tanh,
|
|
|
|
op_asin,
|
|
|
|
op_acos,
|
|
|
|
op_atan,
|
|
|
|
op_asinh,
|
|
|
|
op_acosh,
|
|
|
|
op_atanh,
|
|
|
|
op_floor,
|
|
|
|
op_ceiling,
|
|
|
|
op_round,
|
|
|
|
op_truncate,
|
|
|
|
op_integer,
|
|
|
|
op_float,
|
|
|
|
op_abs,
|
2009-10-20 09:50:51 +01:00
|
|
|
op_lsb,
|
2008-12-04 23:33:32 +00:00
|
|
|
op_msb,
|
2009-10-20 10:03:10 +01:00
|
|
|
op_popcount,
|
2008-12-04 23:33:32 +00:00
|
|
|
op_ffracp,
|
|
|
|
op_fintp,
|
|
|
|
op_sign,
|
|
|
|
op_lgamma,
|
2010-03-05 00:01:04 +00:00
|
|
|
op_erf,
|
|
|
|
op_erfc,
|
2010-05-28 09:53:56 +01:00
|
|
|
op_rational,
|
|
|
|
op_rationalize,
|
2008-12-04 23:33:32 +00:00
|
|
|
op_random1
|
|
|
|
} arith1_op;
|
|
|
|
|
2014-05-06 13:58:08 +01:00
|
|
|
/**
|
|
|
|
* @addtogroup arithmetic_operators
|
2016-03-30 17:35:03 +01:00
|
|
|
* @enum arith2_op binary operators
|
2014-05-06 13:58:08 +01:00
|
|
|
* @brief specifies the available unary arithmetic operators
|
|
|
|
*/
|
2008-12-04 23:33:32 +00:00
|
|
|
typedef enum {
|
|
|
|
op_plus,
|
|
|
|
op_minus,
|
|
|
|
op_times,
|
|
|
|
op_fdiv,
|
|
|
|
op_mod,
|
|
|
|
op_rem,
|
|
|
|
op_div,
|
2010-08-31 03:50:33 +01:00
|
|
|
op_idiv,
|
2008-12-04 23:33:32 +00:00
|
|
|
op_sll,
|
|
|
|
op_slr,
|
|
|
|
op_and,
|
|
|
|
op_or,
|
|
|
|
op_xor,
|
|
|
|
op_atan2,
|
|
|
|
/* C-Prolog exponentiation */
|
|
|
|
op_power,
|
|
|
|
/* ISO-Prolog exponentiation */
|
|
|
|
/* op_power, */
|
2009-04-25 01:03:00 +01:00
|
|
|
op_power2,
|
2008-12-04 23:33:32 +00:00
|
|
|
/* Quintus exponentiation */
|
|
|
|
/* op_power, */
|
|
|
|
op_gcd,
|
|
|
|
op_min,
|
2010-05-27 12:24:15 +01:00
|
|
|
op_max,
|
|
|
|
op_rdiv
|
2008-12-04 23:33:32 +00:00
|
|
|
} arith2_op;
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
yap_error_number Yap_MathException__(USES_REGS1);
|
|
|
|
Functor EvalArg(Term);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* Needed to handle numbers:
|
2016-03-30 17:35:03 +01:00
|
|
|
these two macros are fundamental in the integer/float conversions */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#ifdef C_PROLOG
|
2016-03-30 17:35:03 +01:00
|
|
|
#define FlIsInt(X) ((X) == (Int)(X) && IntInBnd((X)))
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2016-03-30 17:35:03 +01:00
|
|
|
#define FlIsInt(X) (FALSE)
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef M_WILLIAMS
|
2016-03-30 17:35:03 +01:00
|
|
|
#define MkEvalFl(X) MkFloatTerm(X)
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2016-03-30 17:35:03 +01:00
|
|
|
#define MkEvalFl(X) (FlIsInt(X) ? MkIntTerm((Int)(X)) : MkFloatTerm(X))
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Macros used by some of the eval functions */
|
2016-03-30 17:35:03 +01:00
|
|
|
#define REvalInt(I) \
|
|
|
|
{ \
|
|
|
|
eval_int = (I); \
|
|
|
|
return (FInt); \
|
|
|
|
}
|
|
|
|
#define REvalFl(F) \
|
|
|
|
{ \
|
|
|
|
eval_flt = (F); \
|
|
|
|
return (FFloat); \
|
|
|
|
}
|
2016-10-20 04:44:59 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#define REvalError() \
|
|
|
|
{ return (FError); }
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* this macro, dependent on the particular implementation
|
2016-03-30 17:35:03 +01:00
|
|
|
is used to interface the arguments into the C libraries */
|
|
|
|
#ifdef MPW
|
|
|
|
#define FL(X) ((extended)(X))
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2016-03-30 17:35:03 +01:00
|
|
|
#define FL(X) ((double)(X))
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
void Yap_InitConstExps(void);
|
|
|
|
void Yap_InitUnaryExps(void);
|
|
|
|
void Yap_InitBinaryExps(void);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
int Yap_ReInitConstExps(void);
|
|
|
|
int Yap_ReInitUnaryExps(void);
|
|
|
|
int Yap_ReInitBinaryExps(void);
|
2008-12-04 23:33:32 +00:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
Term Yap_eval_atom(Int);
|
|
|
|
Term Yap_eval_unary(Int, Term);
|
|
|
|
Term Yap_eval_binary(Int, Term, Term);
|
2015-09-29 23:11:00 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
Term Yap_InnerEval__(Term USES_REGS);
|
2015-09-29 23:11:00 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Yap_EvalError(id, t, ...) \
|
|
|
|
Yap_EvalError__(__FILE__, __FUNCTION__, __LINE__, id, t, __VA_ARGS__)
|
2017-03-23 12:28:46 +00:00
|
|
|
void Yap_EvalError__(const char *, const char *, int, yap_error_number, Term,
|
2016-03-30 17:35:03 +01:00
|
|
|
...);
|
2015-09-29 23:11:00 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Yap_ArithError(id, t, ...) \
|
2016-12-05 20:50:04 +00:00
|
|
|
Yap_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, t, __VA_ARGS__)
|
2016-10-20 04:44:59 +01:00
|
|
|
#define Yap_BinError(id) \
|
|
|
|
Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, 0L, "")
|
|
|
|
#define Yap_AbsmiError(id) \
|
|
|
|
Yap_Error__(__FILE__, __FUNCTION__, __LINE__, id, 0L, "")
|
2017-03-23 12:28:46 +00:00
|
|
|
|
2009-05-22 17:21:39 +01:00
|
|
|
|
2012-06-29 22:44:08 +01:00
|
|
|
#include "inline-only.h"
|
2014-10-14 15:53:24 +01:00
|
|
|
|
2014-10-15 11:06:07 +01:00
|
|
|
#define Yap_MathException() Yap_MathException__(PASS_REGS1)
|
|
|
|
|
2014-10-14 15:53:24 +01:00
|
|
|
#define Yap_InnerEval(x) Yap_InnerEval__(x PASS_REGS)
|
|
|
|
#define Yap_Eval(x) Yap_Eval__(x PASS_REGS)
|
2014-10-15 11:06:07 +01:00
|
|
|
#define Yap_FoundArithError() Yap_FoundArithError__(PASS_REGS1)
|
2014-10-14 15:53:24 +01:00
|
|
|
|
|
|
|
INLINE_ONLY inline EXTERN Term Yap_Eval__(Term t USES_REGS);
|
2012-06-29 22:44:08 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
INLINE_ONLY inline EXTERN Term Yap_Eval__(Term t USES_REGS) {
|
|
|
|
if (t == 0L || (!IsVarTerm(t) && IsNumTerm(t)))
|
2010-01-04 23:16:42 +00:00
|
|
|
return t;
|
|
|
|
return Yap_InnerEval(t);
|
|
|
|
}
|
|
|
|
|
2016-06-03 16:53:43 +01:00
|
|
|
#if HAVE_FECLEAREXCEPT
|
2016-03-30 17:35:03 +01:00
|
|
|
inline static void Yap_ClearExs(void) { feclearexcept(FE_ALL_EXCEPT); }
|
2016-06-03 16:53:43 +01:00
|
|
|
#else
|
2016-10-20 04:44:59 +01:00
|
|
|
inline static void Yap_ClearExs(void) {}
|
2016-06-03 16:53:43 +01:00
|
|
|
#endif
|
2014-10-15 11:06:07 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
inline static yap_error_number Yap_FoundArithError__(USES_REGS1) {
|
2014-10-16 10:49:11 +01:00
|
|
|
if (LOCAL_Error_TYPE != YAP_NO_ERROR)
|
|
|
|
return LOCAL_Error_TYPE;
|
2016-03-30 17:35:03 +01:00
|
|
|
if (trueGlobalPrologFlag(
|
|
|
|
ARITHMETIC_EXCEPTIONS_FLAG)) // test support for exception
|
2014-10-19 11:27:53 +01:00
|
|
|
return Yap_MathException();
|
|
|
|
return YAP_NO_ERROR;
|
2009-05-22 17:21:39 +01:00
|
|
|
}
|
2014-10-15 11:06:07 +01:00
|
|
|
|
2015-08-07 22:57:53 +01:00
|
|
|
static inline Term takeIndicator(Term t) {
|
|
|
|
Term ts[2];
|
2016-03-30 17:35:03 +01:00
|
|
|
if (IsAtomTerm(t)) {
|
|
|
|
ts[0] = t;
|
|
|
|
ts[1] = MkIntTerm(0);
|
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
ts[0] = TermNil;
|
|
|
|
ts[1] = MkIntTerm(2);
|
|
|
|
} else {
|
2015-08-07 22:57:53 +01:00
|
|
|
CACHE_REGS
|
|
|
|
ts[0] = MkAtomTerm(NameOfFunctor(FunctorOfTerm(t)));
|
|
|
|
ts[1] = MkIntegerTerm(ArityOfFunctor(FunctorOfTerm(t)));
|
|
|
|
}
|
2016-03-30 17:35:03 +01:00
|
|
|
return Yap_MkApplTerm(FunctorSlash, 2, ts);
|
2015-04-09 01:51:36 +01:00
|
|
|
}
|
|
|
|
|
2014-10-15 11:06:07 +01:00
|
|
|
Atom Yap_NameOfUnaryOp(int i);
|
|
|
|
Atom Yap_NameOfBinaryOp(int i);
|
2009-05-22 17:21:39 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#define RINT(v) return (MkIntegerTerm(v))
|
|
|
|
#define RFLOAT(v) return (MkFloatTerm(v))
|
|
|
|
#define RBIG(v) return (Yap_MkBigIntTerm(v))
|
2016-10-20 04:44:59 +01:00
|
|
|
#define RERROR() \
|
|
|
|
{ \
|
|
|
|
return (0L); \
|
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
static inline blob_type ETypeOfTerm(Term t) {
|
|
|
|
if (IsIntTerm(t))
|
2008-12-04 23:33:32 +00:00
|
|
|
return long_int_e;
|
|
|
|
if (IsApplTerm(t)) {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
if (f == FunctorDouble)
|
|
|
|
return double_e;
|
|
|
|
if (f == FunctorLongInt)
|
|
|
|
return long_int_e;
|
2010-05-27 12:24:15 +01:00
|
|
|
if (f == FunctorBigInt) {
|
2008-12-04 23:33:32 +00:00
|
|
|
return big_int_e;
|
2010-05-27 12:24:15 +01:00
|
|
|
}
|
2008-12-04 23:33:32 +00:00
|
|
|
}
|
|
|
|
return db_ref_e;
|
|
|
|
}
|
2008-11-28 15:54:08 +00:00
|
|
|
|
|
|
|
#if USE_GMP
|
2013-12-02 14:49:41 +00:00
|
|
|
char *Yap_mpz_to_string(MP_INT *b, char *s, size_t sz, int base);
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
Term Yap_gmq_rdiv_int_int(Int, Int);
|
|
|
|
Term Yap_gmq_rdiv_int_big(Int, Term);
|
|
|
|
Term Yap_gmq_rdiv_big_int(Term, Int);
|
|
|
|
Term Yap_gmq_rdiv_big_big(Term, Term);
|
|
|
|
|
|
|
|
Term Yap_gmp_add_ints(Int, Int);
|
|
|
|
Term Yap_gmp_sub_ints(Int, Int);
|
|
|
|
Term Yap_gmp_mul_ints(Int, Int);
|
|
|
|
Term Yap_gmp_sll_ints(Int, Int);
|
|
|
|
Term Yap_gmp_add_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_sub_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_sub_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_mul_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_div_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_div_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_div2_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_fdiv_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_fdiv_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_and_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_ior_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_xor_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_sll_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_add_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_sub_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_mul_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_div_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_div2_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_fdiv_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_and_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_ior_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_xor_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_mod_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_mod_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_mod_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_rem_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_rem_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_rem_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_exp_int_int(Int, Int);
|
|
|
|
Term Yap_gmp_exp_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_exp_big_int(Term, Int);
|
|
|
|
Term Yap_gmp_exp_big_big(Term, Term);
|
|
|
|
Term Yap_gmp_gcd_int_big(Int, Term);
|
|
|
|
Term Yap_gmp_gcd_big_big(Term, Term);
|
|
|
|
|
|
|
|
Term Yap_gmp_big_from_64bits(YAP_LONG_LONG);
|
|
|
|
|
|
|
|
Term Yap_gmp_float_to_big(Float);
|
|
|
|
Term Yap_gmp_float_to_rational(Float);
|
|
|
|
Term Yap_gmp_float_rationalize(Float);
|
2013-04-25 23:15:04 +01:00
|
|
|
Float Yap_gmp_to_float(Term);
|
2016-03-30 17:35:03 +01:00
|
|
|
Term Yap_gmp_add_float_big(Float, Term);
|
|
|
|
Term Yap_gmp_sub_float_big(Float, Term);
|
|
|
|
Term Yap_gmp_sub_big_float(Term, Float);
|
|
|
|
Term Yap_gmp_mul_float_big(Float, Term);
|
|
|
|
Term Yap_gmp_fdiv_float_big(Float, Term);
|
|
|
|
Term Yap_gmp_fdiv_big_float(Term, Float);
|
|
|
|
|
|
|
|
int Yap_gmp_cmp_big_int(Term, Int);
|
2016-05-13 11:43:01 +01:00
|
|
|
int Yap_gmp_cmp_int_big(Int, Term);
|
2016-03-30 17:35:03 +01:00
|
|
|
int Yap_gmp_cmp_big_float(Term, Float);
|
2010-05-27 12:24:15 +01:00
|
|
|
#define Yap_gmp_cmp_float_big(D, T) (-Yap_gmp_cmp_big_float(T, D))
|
2016-03-30 17:35:03 +01:00
|
|
|
int Yap_gmp_cmp_big_big(Term, Term);
|
2010-05-27 12:24:15 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
int Yap_gmp_tcmp_big_int(Term, Int);
|
2016-05-13 11:43:01 +01:00
|
|
|
int Yap_gmp_tcmp_int_big(Int, Term);
|
2016-03-30 17:35:03 +01:00
|
|
|
int Yap_gmp_tcmp_big_float(Term, Float);
|
2010-05-28 12:07:01 +01:00
|
|
|
#define Yap_gmp_tcmp_float_big(D, T) (-Yap_gmp_tcmp_big_float(T, D))
|
2016-03-30 17:35:03 +01:00
|
|
|
int Yap_gmp_tcmp_big_big(Term, Term);
|
|
|
|
|
|
|
|
Term Yap_gmp_neg_int(Int);
|
|
|
|
Term Yap_gmp_abs_big(Term);
|
|
|
|
Term Yap_gmp_neg_big(Term);
|
|
|
|
Term Yap_gmp_unot_big(Term);
|
|
|
|
Term Yap_gmp_floor(Term);
|
|
|
|
Term Yap_gmp_ceiling(Term);
|
|
|
|
Term Yap_gmp_round(Term);
|
|
|
|
Term Yap_gmp_trunc(Term);
|
|
|
|
Term Yap_gmp_float_fractional_part(Term);
|
|
|
|
Term Yap_gmp_float_integer_part(Term);
|
|
|
|
Term Yap_gmp_sign(Term);
|
|
|
|
Term Yap_gmp_lsb(Term);
|
|
|
|
Term Yap_gmp_msb(Term);
|
|
|
|
Term Yap_gmp_popcount(Term);
|
|
|
|
|
|
|
|
char *Yap_gmp_to_string(Term, char *, size_t, int);
|
|
|
|
size_t Yap_gmp_to_size(Term, int);
|
|
|
|
|
|
|
|
int Yap_term_to_existing_big(Term, MP_INT *);
|
|
|
|
int Yap_term_to_existing_rat(Term, MP_RAT *);
|
|
|
|
|
|
|
|
void Yap_gmp_set_bit(Int i, Term t);
|
2008-11-28 15:54:08 +00:00
|
|
|
#endif
|
2009-06-05 18:18:10 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#define Yap_Mk64IntegerTerm(i) __Yap_Mk64IntegerTerm((i)PASS_REGS)
|
2013-03-26 20:01:52 +00:00
|
|
|
|
2015-01-18 01:32:13 +00:00
|
|
|
INLINE_ONLY inline EXTERN Term __Yap_Mk64IntegerTerm(YAP_LONG_LONG USES_REGS);
|
2009-06-05 18:18:10 +01:00
|
|
|
|
2012-06-30 19:42:14 +01:00
|
|
|
INLINE_ONLY inline EXTERN Term
|
2016-03-30 17:35:03 +01:00
|
|
|
__Yap_Mk64IntegerTerm(YAP_LONG_LONG i USES_REGS) {
|
2009-06-05 20:19:02 +01:00
|
|
|
if (i <= Int_MAX && i >= Int_MIN) {
|
2009-06-05 18:18:10 +01:00
|
|
|
return MkIntegerTerm((Int)i);
|
|
|
|
} else {
|
|
|
|
#if USE_GMP
|
|
|
|
return Yap_gmp_big_from_64bits(i);
|
|
|
|
#else
|
|
|
|
return MkIntTerm(-1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
#if __clang__ && FALSE /* not in OSX yet */
|
|
|
|
#define DO_ADD() \
|
|
|
|
if (__builtin_sadd_overflow(i1, i2, &z)) { \
|
|
|
|
goto overflow; \
|
|
|
|
}
|
2014-01-17 09:39:29 +00:00
|
|
|
#endif
|
2013-04-30 21:23:01 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
inline static Term add_int(Int i, Int j USES_REGS) {
|
2013-04-30 21:23:01 +01:00
|
|
|
#if USE_GMP
|
2016-03-30 17:35:03 +01:00
|
|
|
UInt w = (UInt)i + (UInt)j;
|
2014-01-17 09:39:29 +00:00
|
|
|
if (i > 0) {
|
2016-03-30 17:35:03 +01:00
|
|
|
if (j > 0 && (Int)w < 0)
|
|
|
|
goto overflow;
|
2014-01-17 09:39:29 +00:00
|
|
|
} else {
|
2016-03-30 17:35:03 +01:00
|
|
|
if (j < 0 && (Int)w > 0)
|
|
|
|
goto overflow;
|
2013-04-30 21:23:01 +01:00
|
|
|
}
|
2016-03-30 17:35:03 +01:00
|
|
|
RINT((Int)w);
|
|
|
|
/* Integer overflow, we need to use big integers */
|
|
|
|
overflow:
|
|
|
|
return Yap_gmp_add_ints(i, j);
|
2013-04-30 21:23:01 +01:00
|
|
|
#else
|
2016-03-30 17:35:03 +01:00
|
|
|
RINT(i + j);
|
2013-04-30 21:23:01 +01:00
|
|
|
#endif
|
|
|
|
}
|
2014-01-17 09:39:29 +00:00
|
|
|
|
2014-10-24 15:18:32 +01:00
|
|
|
/* calculate the most significant bit for an integer */
|
2016-03-30 17:35:03 +01:00
|
|
|
Int Yap_msb(Int inp USES_REGS);
|
2014-10-24 15:18:32 +01:00
|
|
|
|
2016-03-30 17:35:03 +01:00
|
|
|
static inline Term p_plus(Term t1, Term t2 USES_REGS) {
|
2014-01-17 09:39:29 +00:00
|
|
|
switch (ETypeOfTerm(t1)) {
|
|
|
|
case long_int_e:
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
|
|
|
case long_int_e:
|
|
|
|
/* two integers */
|
2016-03-30 17:35:03 +01:00
|
|
|
return add_int(IntegerOfTerm(t1), IntegerOfTerm(t2) PASS_REGS);
|
|
|
|
case double_e: {
|
|
|
|
/* integer, double */
|
|
|
|
Float fl1 = (Float)IntegerOfTerm(t1);
|
|
|
|
Float fl2 = FloatOfTerm(t2);
|
|
|
|
RFLOAT(fl1 + fl2);
|
|
|
|
}
|
2014-01-17 09:39:29 +00:00
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2016-03-30 17:35:03 +01:00
|
|
|
return (Yap_gmp_add_int_big(IntegerOfTerm(t1), t2));
|
2014-01-17 09:39:29 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
case double_e:
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
|
|
|
case long_int_e:
|
|
|
|
/* float * integer */
|
2016-03-30 17:35:03 +01:00
|
|
|
RFLOAT(FloatOfTerm(t1) + IntegerOfTerm(t2));
|
2014-01-17 09:39:29 +00:00
|
|
|
case double_e:
|
2016-03-30 17:35:03 +01:00
|
|
|
RFLOAT(FloatOfTerm(t1) + FloatOfTerm(t2));
|
2014-01-17 09:39:29 +00:00
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
2016-03-30 17:35:03 +01:00
|
|
|
return Yap_gmp_add_float_big(FloatOfTerm(t1), t2);
|
2014-01-17 09:39:29 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
case big_int_e:
|
|
|
|
#ifdef USE_GMP
|
|
|
|
switch (ETypeOfTerm(t2)) {
|
|
|
|
case long_int_e:
|
|
|
|
return Yap_gmp_add_int_big(IntegerOfTerm(t2), t1);
|
|
|
|
case big_int_e:
|
|
|
|
/* two bignums */
|
|
|
|
return Yap_gmp_add_big_big(t1, t2);
|
|
|
|
case double_e:
|
2016-03-30 17:35:03 +01:00
|
|
|
return Yap_gmp_add_float_big(FloatOfTerm(t2), t1);
|
2014-01-17 09:39:29 +00:00
|
|
|
default:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
RERROR();
|
|
|
|
}
|
|
|
|
|
2015-01-05 16:06:32 +00:00
|
|
|
#ifndef PI
|
|
|
|
#ifdef M_PI
|
|
|
|
#define PI M_PI
|
|
|
|
#else
|
|
|
|
#define PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef M_E
|
|
|
|
#define M_E 2.7182818284590452354
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef INFINITY
|
2016-03-30 17:35:03 +01:00
|
|
|
#define INFINITY (1.0 / 0.0)
|
2015-01-05 16:06:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NAN
|
2016-03-30 17:35:03 +01:00
|
|
|
#define NAN (0.0 / 0.0)
|
2015-01-05 16:06:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* copied from SWI-Prolog */
|
|
|
|
#ifndef DBL_EPSILON /* normal for IEEE 64-bit double */
|
|
|
|
#define DBL_EPSILON 0.00000000000000022204
|
|
|
|
#endif
|
2016-01-31 10:23:03 +00:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
#endif
|