GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* mpz expression evaluation, simple part12Copyright 2000, 2001, 2002 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of the GNU Lesser General Public License as published by8the Free Software Foundation; either version 2.1 of the License, or (at your9option) any later version.1011The GNU MP Library is distributed in the hope that it will be useful, but12WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public14License for more details.1516You should have received a copy of the GNU Lesser General Public License17along with the GNU MP Library; see the file COPYING.LIB. If not, write to18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,19MA 02110-1301, USA. */2021#include <ctype.h>22#include <stdio.h>23#include <string.h>24#include "gmp.h"25#include "expr-impl.h"262728/* Change this to "#define TRACE(x) x" to get some traces. */29#define TRACE(x)303132/* These are macros, so need function wrappers. */33static int34e_mpz_sgn (mpz_srcptr x)35{36return mpz_sgn (x);37}38static int39e_mpz_odd_p (mpz_srcptr x)40{41return mpz_odd_p (x);42}43static int44e_mpz_even_p (mpz_srcptr x)45{46return mpz_even_p (x);47}4849/* These wrapped because MPEXPR_TYPE_I_ functions are expected to return50"int" whereas these return "unsigned long". */51static void52e_mpz_hamdist (mpz_ptr w, mpz_srcptr x, mpz_srcptr y)53{54mpz_set_ui (w, mpz_hamdist (x, y));55}56static void57e_mpz_popcount (mpz_ptr w, mpz_srcptr x)58{59mpz_set_ui (w, mpz_popcount (x));60}61static void62e_mpz_scan0 (mpz_ptr w, mpz_srcptr x, unsigned long start)63{64mpz_set_ui (w, mpz_scan0 (x, start));65}66static void67e_mpz_scan1 (mpz_ptr w, mpz_srcptr x, unsigned long start)68{69mpz_set_ui (w, mpz_scan1 (x, start));70}7172/* These wrapped because they're in-place whereas MPEXPR_TYPE_BINARY_UI73expects a separate source and destination. Actually the parser will74normally pass w==x anyway. */75static void76e_mpz_setbit (mpz_ptr w, mpz_srcptr x, unsigned long n)77{78if (w != x)79mpz_set (w, x);80mpz_setbit (w, n);81}82static void83e_mpz_clrbit (mpz_ptr w, mpz_srcptr x, unsigned long n)84{85if (w != x)86mpz_set (w, x);87mpz_clrbit (w, n);88}8990static __gmp_const struct mpexpr_operator_t _mpz_expr_standard_table[] = {9192{ "**", (mpexpr_fun_t) mpz_pow_ui,93MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 },9495{ "~", (mpexpr_fun_t) mpz_com,96MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },97{ "!", (mpexpr_fun_t) e_mpz_sgn,98MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 },99{ "-", (mpexpr_fun_t) mpz_neg,100MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },101102{ "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 200 },103{ "/", (mpexpr_fun_t) mpz_tdiv_q, MPEXPR_TYPE_BINARY, 200 },104{ "%", (mpexpr_fun_t) mpz_tdiv_r, MPEXPR_TYPE_BINARY, 200 },105106{ "+", (mpexpr_fun_t) mpz_add, MPEXPR_TYPE_BINARY, 190 },107{ "-", (mpexpr_fun_t) mpz_sub, MPEXPR_TYPE_BINARY, 190 },108109{ "<<", (mpexpr_fun_t) mpz_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 },110{ ">>", (mpexpr_fun_t) mpz_tdiv_q_2exp, MPEXPR_TYPE_BINARY_UI, 180 },111112{ "<=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LE, 170 },113{ "<", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LT, 170 },114{ ">=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GE, 170 },115{ ">", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GT, 170 },116117{ "==", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_EQ, 160 },118{ "!=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_NE, 160 },119120{ "&", (mpexpr_fun_t) mpz_and, MPEXPR_TYPE_BINARY, 150 },121{ "^", (mpexpr_fun_t) mpz_xor, MPEXPR_TYPE_BINARY, 140 },122{ "|", (mpexpr_fun_t) mpz_ior, MPEXPR_TYPE_BINARY, 130 },123{ "&&", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },124{ "||", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 },125126{ ":", NULL, MPEXPR_TYPE_COLON, 101 },127{ "?", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_QUESTION, 100 },128129{ ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 },130{ "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 },131{ ",", NULL, MPEXPR_TYPE_ARGSEP, 2 },132{ "$", NULL, MPEXPR_TYPE_VARIABLE, 1 },133134{ "abs", (mpexpr_fun_t) mpz_abs, MPEXPR_TYPE_UNARY },135{ "bin", (mpexpr_fun_t) mpz_bin_ui, MPEXPR_TYPE_BINARY_UI },136{ "clrbit", (mpexpr_fun_t) e_mpz_clrbit, MPEXPR_TYPE_BINARY_UI },137{ "cmp", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_I_BINARY },138{ "cmpabs", (mpexpr_fun_t) mpz_cmpabs, MPEXPR_TYPE_I_BINARY },139{ "congruent_p",(mpexpr_fun_t)mpz_congruent_p, MPEXPR_TYPE_I_TERNARY },140{ "divisible_p",(mpexpr_fun_t)mpz_divisible_p, MPEXPR_TYPE_I_BINARY },141{ "even_p", (mpexpr_fun_t) e_mpz_even_p, MPEXPR_TYPE_I_UNARY },142{ "fib", (mpexpr_fun_t) mpz_fib_ui, MPEXPR_TYPE_UNARY_UI },143{ "fac", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI },144{ "gcd", (mpexpr_fun_t) mpz_gcd, MPEXPR_TYPE_BINARY145| MPEXPR_TYPE_PAIRWISE },146{ "hamdist", (mpexpr_fun_t) e_mpz_hamdist, MPEXPR_TYPE_BINARY },147{ "invert", (mpexpr_fun_t) mpz_invert, MPEXPR_TYPE_BINARY },148{ "jacobi", (mpexpr_fun_t) mpz_jacobi, MPEXPR_TYPE_I_BINARY },149{ "kronecker", (mpexpr_fun_t) mpz_kronecker, MPEXPR_TYPE_I_BINARY },150{ "lcm", (mpexpr_fun_t) mpz_lcm, MPEXPR_TYPE_BINARY151| MPEXPR_TYPE_PAIRWISE },152{ "lucnum", (mpexpr_fun_t) mpz_lucnum_ui, MPEXPR_TYPE_UNARY_UI },153{ "max", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MAX154| MPEXPR_TYPE_PAIRWISE },155{ "min", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MIN156| MPEXPR_TYPE_PAIRWISE },157{ "nextprime", (mpexpr_fun_t) mpz_nextprime, MPEXPR_TYPE_UNARY },158{ "odd_p", (mpexpr_fun_t) e_mpz_odd_p, MPEXPR_TYPE_I_UNARY },159{ "perfect_power_p", (mpexpr_fun_t)mpz_perfect_power_p, MPEXPR_TYPE_I_UNARY},160{ "perfect_square_p",(mpexpr_fun_t)mpz_perfect_square_p,MPEXPR_TYPE_I_UNARY},161{ "popcount", (mpexpr_fun_t) e_mpz_popcount, MPEXPR_TYPE_UNARY },162{ "powm", (mpexpr_fun_t) mpz_powm, MPEXPR_TYPE_TERNARY },163{ "probab_prime_p", (mpexpr_fun_t)mpz_probab_prime_p, MPEXPR_TYPE_I_UNARY},164{ "root", (mpexpr_fun_t) mpz_root, MPEXPR_TYPE_BINARY_UI },165{ "scan0", (mpexpr_fun_t) e_mpz_scan0, MPEXPR_TYPE_BINARY_UI },166{ "scan1", (mpexpr_fun_t) e_mpz_scan1, MPEXPR_TYPE_BINARY_UI },167{ "setbit", (mpexpr_fun_t) e_mpz_setbit, MPEXPR_TYPE_BINARY_UI },168{ "tstbit", (mpexpr_fun_t) mpz_tstbit, MPEXPR_TYPE_I_BINARY_UI },169{ "sgn", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_I_UNARY },170{ "sqrt", (mpexpr_fun_t) mpz_sqrt, MPEXPR_TYPE_UNARY },171{ NULL }172};173174/* The table is available globally only through a pointer, so the table size175can change without breaking binary compatibility. */176__gmp_const struct mpexpr_operator_t * __gmp_const mpz_expr_standard_table177= _mpz_expr_standard_table;178179180int181#if HAVE_STDARG182mpz_expr (mpz_ptr res, int base, __gmp_const char *e, ...)183#else184mpz_expr (va_alist)185va_dcl186#endif187{188mpz_srcptr var[MPEXPR_VARIABLES];189va_list ap;190int ret;191#if HAVE_STDARG192va_start (ap, e);193#else194mpz_ptr res;195int base;196__gmp_const char *e;197va_start (ap);198res = va_arg (ap, mpz_ptr);199base = va_arg (ap, int);200e = va_arg (ap, __gmp_const char *);201#endif202203TRACE (printf ("mpz_expr(): base %d, %s\n", base, e));204ret = mpexpr_va_to_var ((void **) var, ap);205va_end (ap);206207if (ret != MPEXPR_RESULT_OK)208return ret;209210return mpz_expr_a (mpz_expr_standard_table, res, base, e, strlen(e), var);211}212213214215