Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563738 views
1
/* mpz expression evaluation, simple part
2
3
Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
4
5
This file is part of the GNU MP Library.
6
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
11
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
16
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
21
22
#include <ctype.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include "gmp.h"
26
#include "expr-impl.h"
27
28
29
/* Change this to "#define TRACE(x) x" to get some traces. */
30
#define TRACE(x)
31
32
33
/* These are macros, so need function wrappers. */
34
static int
35
e_mpz_sgn (mpz_srcptr x)
36
{
37
return mpz_sgn (x);
38
}
39
static int
40
e_mpz_odd_p (mpz_srcptr x)
41
{
42
return mpz_odd_p (x);
43
}
44
static int
45
e_mpz_even_p (mpz_srcptr x)
46
{
47
return mpz_even_p (x);
48
}
49
50
/* These wrapped because MPEXPR_TYPE_I_ functions are expected to return
51
"int" whereas these return "unsigned long". */
52
static void
53
e_mpz_hamdist (mpz_ptr w, mpz_srcptr x, mpz_srcptr y)
54
{
55
mpz_set_ui (w, mpz_hamdist (x, y));
56
}
57
static void
58
e_mpz_popcount (mpz_ptr w, mpz_srcptr x)
59
{
60
mpz_set_ui (w, mpz_popcount (x));
61
}
62
static void
63
e_mpz_scan0 (mpz_ptr w, mpz_srcptr x, unsigned long start)
64
{
65
mpz_set_ui (w, mpz_scan0 (x, start));
66
}
67
static void
68
e_mpz_scan1 (mpz_ptr w, mpz_srcptr x, unsigned long start)
69
{
70
mpz_set_ui (w, mpz_scan1 (x, start));
71
}
72
73
/* These wrapped because they're in-place whereas MPEXPR_TYPE_BINARY_UI
74
expects a separate source and destination. Actually the parser will
75
normally pass w==x anyway. */
76
static void
77
e_mpz_setbit (mpz_ptr w, mpz_srcptr x, unsigned long n)
78
{
79
if (w != x)
80
mpz_set (w, x);
81
mpz_setbit (w, n);
82
}
83
static void
84
e_mpz_clrbit (mpz_ptr w, mpz_srcptr x, unsigned long n)
85
{
86
if (w != x)
87
mpz_set (w, x);
88
mpz_clrbit (w, n);
89
}
90
91
static __gmp_const struct mpexpr_operator_t _mpz_expr_standard_table[] = {
92
93
{ "**", (mpexpr_fun_t) mpz_pow_ui,
94
MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 },
95
96
{ "~", (mpexpr_fun_t) mpz_com,
97
MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },
98
{ "!", (mpexpr_fun_t) e_mpz_sgn,
99
MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 },
100
{ "-", (mpexpr_fun_t) mpz_neg,
101
MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },
102
103
{ "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 200 },
104
{ "/", (mpexpr_fun_t) mpz_tdiv_q, MPEXPR_TYPE_BINARY, 200 },
105
{ "%", (mpexpr_fun_t) mpz_tdiv_r, MPEXPR_TYPE_BINARY, 200 },
106
107
{ "+", (mpexpr_fun_t) mpz_add, MPEXPR_TYPE_BINARY, 190 },
108
{ "-", (mpexpr_fun_t) mpz_sub, MPEXPR_TYPE_BINARY, 190 },
109
110
{ "<<", (mpexpr_fun_t) mpz_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 },
111
{ ">>", (mpexpr_fun_t) mpz_tdiv_q_2exp, MPEXPR_TYPE_BINARY_UI, 180 },
112
113
{ "<=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LE, 170 },
114
{ "<", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LT, 170 },
115
{ ">=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GE, 170 },
116
{ ">", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GT, 170 },
117
118
{ "==", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_EQ, 160 },
119
{ "!=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_NE, 160 },
120
121
{ "&", (mpexpr_fun_t) mpz_and, MPEXPR_TYPE_BINARY, 150 },
122
{ "^", (mpexpr_fun_t) mpz_xor, MPEXPR_TYPE_BINARY, 140 },
123
{ "|", (mpexpr_fun_t) mpz_ior, MPEXPR_TYPE_BINARY, 130 },
124
{ "&&", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },
125
{ "||", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 },
126
127
{ ":", NULL, MPEXPR_TYPE_COLON, 101 },
128
{ "?", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_QUESTION, 100 },
129
130
{ ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 },
131
{ "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 },
132
{ ",", NULL, MPEXPR_TYPE_ARGSEP, 2 },
133
{ "$", NULL, MPEXPR_TYPE_VARIABLE, 1 },
134
135
{ "abs", (mpexpr_fun_t) mpz_abs, MPEXPR_TYPE_UNARY },
136
{ "bin", (mpexpr_fun_t) mpz_bin_ui, MPEXPR_TYPE_BINARY_UI },
137
{ "clrbit", (mpexpr_fun_t) e_mpz_clrbit, MPEXPR_TYPE_BINARY_UI },
138
{ "cmp", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_I_BINARY },
139
{ "cmpabs", (mpexpr_fun_t) mpz_cmpabs, MPEXPR_TYPE_I_BINARY },
140
{ "congruent_p",(mpexpr_fun_t)mpz_congruent_p, MPEXPR_TYPE_I_TERNARY },
141
{ "divisible_p",(mpexpr_fun_t)mpz_divisible_p, MPEXPR_TYPE_I_BINARY },
142
{ "even_p", (mpexpr_fun_t) e_mpz_even_p, MPEXPR_TYPE_I_UNARY },
143
{ "fib", (mpexpr_fun_t) mpz_fib_ui, MPEXPR_TYPE_UNARY_UI },
144
{ "fac", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI },
145
{ "gcd", (mpexpr_fun_t) mpz_gcd, MPEXPR_TYPE_BINARY
146
| MPEXPR_TYPE_PAIRWISE },
147
{ "hamdist", (mpexpr_fun_t) e_mpz_hamdist, MPEXPR_TYPE_BINARY },
148
{ "invert", (mpexpr_fun_t) mpz_invert, MPEXPR_TYPE_BINARY },
149
{ "jacobi", (mpexpr_fun_t) mpz_jacobi, MPEXPR_TYPE_I_BINARY },
150
{ "kronecker", (mpexpr_fun_t) mpz_kronecker, MPEXPR_TYPE_I_BINARY },
151
{ "lcm", (mpexpr_fun_t) mpz_lcm, MPEXPR_TYPE_BINARY
152
| MPEXPR_TYPE_PAIRWISE },
153
{ "lucnum", (mpexpr_fun_t) mpz_lucnum_ui, MPEXPR_TYPE_UNARY_UI },
154
{ "max", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MAX
155
| MPEXPR_TYPE_PAIRWISE },
156
{ "min", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MIN
157
| MPEXPR_TYPE_PAIRWISE },
158
{ "nextprime", (mpexpr_fun_t) mpz_nextprime, MPEXPR_TYPE_UNARY },
159
{ "odd_p", (mpexpr_fun_t) e_mpz_odd_p, MPEXPR_TYPE_I_UNARY },
160
{ "perfect_power_p", (mpexpr_fun_t)mpz_perfect_power_p, MPEXPR_TYPE_I_UNARY},
161
{ "perfect_square_p",(mpexpr_fun_t)mpz_perfect_square_p,MPEXPR_TYPE_I_UNARY},
162
{ "popcount", (mpexpr_fun_t) e_mpz_popcount, MPEXPR_TYPE_UNARY },
163
{ "powm", (mpexpr_fun_t) mpz_powm, MPEXPR_TYPE_TERNARY },
164
{ "probab_prime_p", (mpexpr_fun_t)mpz_probab_prime_p, MPEXPR_TYPE_I_UNARY},
165
{ "root", (mpexpr_fun_t) mpz_root, MPEXPR_TYPE_BINARY_UI },
166
{ "scan0", (mpexpr_fun_t) e_mpz_scan0, MPEXPR_TYPE_BINARY_UI },
167
{ "scan1", (mpexpr_fun_t) e_mpz_scan1, MPEXPR_TYPE_BINARY_UI },
168
{ "setbit", (mpexpr_fun_t) e_mpz_setbit, MPEXPR_TYPE_BINARY_UI },
169
{ "tstbit", (mpexpr_fun_t) mpz_tstbit, MPEXPR_TYPE_I_BINARY_UI },
170
{ "sgn", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_I_UNARY },
171
{ "sqrt", (mpexpr_fun_t) mpz_sqrt, MPEXPR_TYPE_UNARY },
172
{ NULL }
173
};
174
175
/* The table is available globally only through a pointer, so the table size
176
can change without breaking binary compatibility. */
177
__gmp_const struct mpexpr_operator_t * __gmp_const mpz_expr_standard_table
178
= _mpz_expr_standard_table;
179
180
181
int
182
#if HAVE_STDARG
183
mpz_expr (mpz_ptr res, int base, __gmp_const char *e, ...)
184
#else
185
mpz_expr (va_alist)
186
va_dcl
187
#endif
188
{
189
mpz_srcptr var[MPEXPR_VARIABLES];
190
va_list ap;
191
int ret;
192
#if HAVE_STDARG
193
va_start (ap, e);
194
#else
195
mpz_ptr res;
196
int base;
197
__gmp_const char *e;
198
va_start (ap);
199
res = va_arg (ap, mpz_ptr);
200
base = va_arg (ap, int);
201
e = va_arg (ap, __gmp_const char *);
202
#endif
203
204
TRACE (printf ("mpz_expr(): base %d, %s\n", base, e));
205
ret = mpexpr_va_to_var ((void **) var, ap);
206
va_end (ap);
207
208
if (ret != MPEXPR_RESULT_OK)
209
return ret;
210
211
return mpz_expr_a (mpz_expr_standard_table, res, base, e, strlen(e), var);
212
}
213
214
215