Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418385/* mpz expression evaluation, simple part12Copyright 2000-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 either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */2930#include <ctype.h>31#include <stdio.h>32#include <string.h>33#include "gmp.h"34#include "expr-impl.h"353637/* Change this to "#define TRACE(x) x" to get some traces. */38#define TRACE(x)394041/* These are macros, so need function wrappers. */42static int43e_mpz_sgn (mpz_srcptr x)44{45return mpz_sgn (x);46}47static int48e_mpz_odd_p (mpz_srcptr x)49{50return mpz_odd_p (x);51}52static int53e_mpz_even_p (mpz_srcptr x)54{55return mpz_even_p (x);56}5758/* These wrapped because MPEXPR_TYPE_I_ functions are expected to return59"int" whereas these return "unsigned long". */60static void61e_mpz_hamdist (mpz_ptr w, mpz_srcptr x, mpz_srcptr y)62{63mpz_set_ui (w, mpz_hamdist (x, y));64}65static void66e_mpz_popcount (mpz_ptr w, mpz_srcptr x)67{68mpz_set_ui (w, mpz_popcount (x));69}70static void71e_mpz_scan0 (mpz_ptr w, mpz_srcptr x, unsigned long start)72{73mpz_set_ui (w, mpz_scan0 (x, start));74}75static void76e_mpz_scan1 (mpz_ptr w, mpz_srcptr x, unsigned long start)77{78mpz_set_ui (w, mpz_scan1 (x, start));79}8081/* These wrapped because they're in-place whereas MPEXPR_TYPE_BINARY_UI82expects a separate source and destination. Actually the parser will83normally pass w==x anyway. */84static void85e_mpz_setbit (mpz_ptr w, mpz_srcptr x, unsigned long n)86{87if (w != x)88mpz_set (w, x);89mpz_setbit (w, n);90}91static void92e_mpz_clrbit (mpz_ptr w, mpz_srcptr x, unsigned long n)93{94if (w != x)95mpz_set (w, x);96mpz_clrbit (w, n);97}9899static const struct mpexpr_operator_t _mpz_expr_standard_table[] = {100101{ "**", (mpexpr_fun_t) mpz_pow_ui,102MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 },103104{ "~", (mpexpr_fun_t) mpz_com,105MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },106{ "!", (mpexpr_fun_t) e_mpz_sgn,107MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 },108{ "-", (mpexpr_fun_t) mpz_neg,109MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },110111{ "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 200 },112{ "/", (mpexpr_fun_t) mpz_tdiv_q, MPEXPR_TYPE_BINARY, 200 },113{ "%", (mpexpr_fun_t) mpz_tdiv_r, MPEXPR_TYPE_BINARY, 200 },114115{ "+", (mpexpr_fun_t) mpz_add, MPEXPR_TYPE_BINARY, 190 },116{ "-", (mpexpr_fun_t) mpz_sub, MPEXPR_TYPE_BINARY, 190 },117118{ "<<", (mpexpr_fun_t) mpz_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 },119{ ">>", (mpexpr_fun_t) mpz_tdiv_q_2exp, MPEXPR_TYPE_BINARY_UI, 180 },120121{ "<=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LE, 170 },122{ "<", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LT, 170 },123{ ">=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GE, 170 },124{ ">", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GT, 170 },125126{ "==", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_EQ, 160 },127{ "!=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_NE, 160 },128129{ "&", (mpexpr_fun_t) mpz_and, MPEXPR_TYPE_BINARY, 150 },130{ "^", (mpexpr_fun_t) mpz_xor, MPEXPR_TYPE_BINARY, 140 },131{ "|", (mpexpr_fun_t) mpz_ior, MPEXPR_TYPE_BINARY, 130 },132{ "&&", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },133{ "||", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 },134135{ ":", NULL, MPEXPR_TYPE_COLON, 101 },136{ "?", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_QUESTION, 100 },137138{ ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 },139{ "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 },140{ ",", NULL, MPEXPR_TYPE_ARGSEP, 2 },141{ "$", NULL, MPEXPR_TYPE_VARIABLE, 1 },142143{ "abs", (mpexpr_fun_t) mpz_abs, MPEXPR_TYPE_UNARY },144{ "bin", (mpexpr_fun_t) mpz_bin_ui, MPEXPR_TYPE_BINARY_UI },145{ "clrbit", (mpexpr_fun_t) e_mpz_clrbit, MPEXPR_TYPE_BINARY_UI },146{ "cmp", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_I_BINARY },147{ "cmpabs", (mpexpr_fun_t) mpz_cmpabs, MPEXPR_TYPE_I_BINARY },148{ "congruent_p",(mpexpr_fun_t)mpz_congruent_p, MPEXPR_TYPE_I_TERNARY },149{ "divisible_p",(mpexpr_fun_t)mpz_divisible_p, MPEXPR_TYPE_I_BINARY },150{ "even_p", (mpexpr_fun_t) e_mpz_even_p, MPEXPR_TYPE_I_UNARY },151{ "fib", (mpexpr_fun_t) mpz_fib_ui, MPEXPR_TYPE_UNARY_UI },152{ "fac", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI },153{ "gcd", (mpexpr_fun_t) mpz_gcd, MPEXPR_TYPE_BINARY154| MPEXPR_TYPE_PAIRWISE },155{ "hamdist", (mpexpr_fun_t) e_mpz_hamdist, MPEXPR_TYPE_BINARY },156{ "invert", (mpexpr_fun_t) mpz_invert, MPEXPR_TYPE_BINARY },157{ "jacobi", (mpexpr_fun_t) mpz_jacobi, MPEXPR_TYPE_I_BINARY },158{ "kronecker", (mpexpr_fun_t) mpz_kronecker, MPEXPR_TYPE_I_BINARY },159{ "lcm", (mpexpr_fun_t) mpz_lcm, MPEXPR_TYPE_BINARY160| MPEXPR_TYPE_PAIRWISE },161{ "lucnum", (mpexpr_fun_t) mpz_lucnum_ui, MPEXPR_TYPE_UNARY_UI },162{ "max", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MAX163| MPEXPR_TYPE_PAIRWISE },164{ "min", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MIN165| MPEXPR_TYPE_PAIRWISE },166{ "nextprime", (mpexpr_fun_t) mpz_nextprime, MPEXPR_TYPE_UNARY },167{ "odd_p", (mpexpr_fun_t) e_mpz_odd_p, MPEXPR_TYPE_I_UNARY },168{ "perfect_power_p", (mpexpr_fun_t)mpz_perfect_power_p, MPEXPR_TYPE_I_UNARY},169{ "perfect_square_p",(mpexpr_fun_t)mpz_perfect_square_p,MPEXPR_TYPE_I_UNARY},170{ "popcount", (mpexpr_fun_t) e_mpz_popcount, MPEXPR_TYPE_UNARY },171{ "powm", (mpexpr_fun_t) mpz_powm, MPEXPR_TYPE_TERNARY },172{ "probab_prime_p", (mpexpr_fun_t)mpz_probab_prime_p, MPEXPR_TYPE_I_UNARY},173{ "root", (mpexpr_fun_t) mpz_root, MPEXPR_TYPE_BINARY_UI },174{ "scan0", (mpexpr_fun_t) e_mpz_scan0, MPEXPR_TYPE_BINARY_UI },175{ "scan1", (mpexpr_fun_t) e_mpz_scan1, MPEXPR_TYPE_BINARY_UI },176{ "setbit", (mpexpr_fun_t) e_mpz_setbit, MPEXPR_TYPE_BINARY_UI },177{ "tstbit", (mpexpr_fun_t) mpz_tstbit, MPEXPR_TYPE_I_BINARY_UI },178{ "sgn", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_I_UNARY },179{ "sqrt", (mpexpr_fun_t) mpz_sqrt, MPEXPR_TYPE_UNARY },180{ NULL }181};182183/* The table is available globally only through a pointer, so the table size184can change without breaking binary compatibility. */185const struct mpexpr_operator_t * const mpz_expr_standard_table186= _mpz_expr_standard_table;187188189int190mpz_expr (mpz_ptr res, int base, const char *e, ...)191{192mpz_srcptr var[MPEXPR_VARIABLES];193va_list ap;194int ret;195va_start (ap, e);196197TRACE (printf ("mpz_expr(): base %d, %s\n", base, e));198ret = mpexpr_va_to_var ((void **) var, ap);199va_end (ap);200201if (ret != MPEXPR_RESULT_OK)202return ret;203204return mpz_expr_a (mpz_expr_standard_table, res, base, e, strlen(e), var);205}206207208