xref: /netbsd-src/external/lgpl3/gmp/dist/demos/expr/exprz.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
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 3 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.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include "gmp.h"
24 #include "expr-impl.h"
25 
26 
27 /* Change this to "#define TRACE(x) x" to get some traces. */
28 #define TRACE(x)
29 
30 
31 /* These are macros, so need function wrappers. */
32 static int
33 e_mpz_sgn (mpz_srcptr x)
34 {
35   return mpz_sgn (x);
36 }
37 static int
38 e_mpz_odd_p (mpz_srcptr x)
39 {
40   return mpz_odd_p (x);
41 }
42 static int
43 e_mpz_even_p (mpz_srcptr x)
44 {
45   return mpz_even_p (x);
46 }
47 
48 /* These wrapped because MPEXPR_TYPE_I_ functions are expected to return
49    "int" whereas these return "unsigned long".  */
50 static void
51 e_mpz_hamdist (mpz_ptr w, mpz_srcptr x, mpz_srcptr y)
52 {
53   mpz_set_ui (w, mpz_hamdist (x, y));
54 }
55 static void
56 e_mpz_popcount (mpz_ptr w, mpz_srcptr x)
57 {
58   mpz_set_ui (w, mpz_popcount (x));
59 }
60 static void
61 e_mpz_scan0 (mpz_ptr w, mpz_srcptr x, unsigned long start)
62 {
63   mpz_set_ui (w, mpz_scan0 (x, start));
64 }
65 static void
66 e_mpz_scan1 (mpz_ptr w, mpz_srcptr x, unsigned long start)
67 {
68   mpz_set_ui (w, mpz_scan1 (x, start));
69 }
70 
71 /* These wrapped because they're in-place whereas MPEXPR_TYPE_BINARY_UI
72    expects a separate source and destination.  Actually the parser will
73    normally pass w==x anyway.  */
74 static void
75 e_mpz_setbit (mpz_ptr w, mpz_srcptr x, unsigned long n)
76 {
77   if (w != x)
78     mpz_set (w, x);
79   mpz_setbit (w, n);
80 }
81 static void
82 e_mpz_clrbit (mpz_ptr w, mpz_srcptr x, unsigned long n)
83 {
84   if (w != x)
85     mpz_set (w, x);
86   mpz_clrbit (w, n);
87 }
88 
89 static const struct mpexpr_operator_t  _mpz_expr_standard_table[] = {
90 
91   { "**",  (mpexpr_fun_t) mpz_pow_ui,
92     MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC,                  220 },
93 
94   { "~",   (mpexpr_fun_t) mpz_com,
95     MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                          210 },
96   { "!",   (mpexpr_fun_t) e_mpz_sgn,
97     MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX,                    210 },
98   { "-",   (mpexpr_fun_t) mpz_neg,
99     MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                          210 },
100 
101   { "*",   (mpexpr_fun_t) mpz_mul,          MPEXPR_TYPE_BINARY,      200 },
102   { "/",   (mpexpr_fun_t) mpz_tdiv_q,       MPEXPR_TYPE_BINARY,      200 },
103   { "%",   (mpexpr_fun_t) mpz_tdiv_r,       MPEXPR_TYPE_BINARY,      200 },
104 
105   { "+",   (mpexpr_fun_t) mpz_add,          MPEXPR_TYPE_BINARY,      190 },
106   { "-",   (mpexpr_fun_t) mpz_sub,          MPEXPR_TYPE_BINARY,      190 },
107 
108   { "<<",  (mpexpr_fun_t) mpz_mul_2exp,     MPEXPR_TYPE_BINARY_UI,   180 },
109   { ">>",  (mpexpr_fun_t) mpz_tdiv_q_2exp,  MPEXPR_TYPE_BINARY_UI,   180 },
110 
111   { "<=",  (mpexpr_fun_t) mpz_cmp,          MPEXPR_TYPE_CMP_LE,      170 },
112   { "<",   (mpexpr_fun_t) mpz_cmp,          MPEXPR_TYPE_CMP_LT,      170 },
113   { ">=",  (mpexpr_fun_t) mpz_cmp,          MPEXPR_TYPE_CMP_GE,      170 },
114   { ">",   (mpexpr_fun_t) mpz_cmp,          MPEXPR_TYPE_CMP_GT,      170 },
115 
116   { "==",  (mpexpr_fun_t) mpz_cmp,          MPEXPR_TYPE_CMP_EQ,      160 },
117   { "!=",  (mpexpr_fun_t) mpz_cmp,          MPEXPR_TYPE_CMP_NE,      160 },
118 
119   { "&",   (mpexpr_fun_t) mpz_and,          MPEXPR_TYPE_BINARY,      150 },
120   { "^",   (mpexpr_fun_t) mpz_xor,          MPEXPR_TYPE_BINARY,      140 },
121   { "|",   (mpexpr_fun_t) mpz_ior,          MPEXPR_TYPE_BINARY,      130 },
122   { "&&",  (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 },
123   { "||",  (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_OR,  110 },
124 
125   { ":",   NULL,                            MPEXPR_TYPE_COLON,       101 },
126   { "?",   (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_QUESTION,    100 },
127 
128   { ")",   NULL,                            MPEXPR_TYPE_CLOSEPAREN,   4 },
129   { "(",   NULL,                            MPEXPR_TYPE_OPENPAREN,    3 },
130   { ",",   NULL,                            MPEXPR_TYPE_ARGSEP,       2 },
131   { "$",   NULL,                            MPEXPR_TYPE_VARIABLE,     1 },
132 
133   { "abs",       (mpexpr_fun_t) mpz_abs,           MPEXPR_TYPE_UNARY         },
134   { "bin",       (mpexpr_fun_t) mpz_bin_ui,        MPEXPR_TYPE_BINARY_UI     },
135   { "clrbit",    (mpexpr_fun_t) e_mpz_clrbit,      MPEXPR_TYPE_BINARY_UI     },
136   { "cmp",       (mpexpr_fun_t) mpz_cmp,           MPEXPR_TYPE_I_BINARY      },
137   { "cmpabs",    (mpexpr_fun_t) mpz_cmpabs,        MPEXPR_TYPE_I_BINARY      },
138   { "congruent_p",(mpexpr_fun_t)mpz_congruent_p,   MPEXPR_TYPE_I_TERNARY     },
139   { "divisible_p",(mpexpr_fun_t)mpz_divisible_p,   MPEXPR_TYPE_I_BINARY      },
140   { "even_p",    (mpexpr_fun_t) e_mpz_even_p,      MPEXPR_TYPE_I_UNARY       },
141   { "fib",       (mpexpr_fun_t) mpz_fib_ui,        MPEXPR_TYPE_UNARY_UI      },
142   { "fac",       (mpexpr_fun_t) mpz_fac_ui,        MPEXPR_TYPE_UNARY_UI      },
143   { "gcd",       (mpexpr_fun_t) mpz_gcd,           MPEXPR_TYPE_BINARY
144 						   | MPEXPR_TYPE_PAIRWISE    },
145   { "hamdist",   (mpexpr_fun_t) e_mpz_hamdist,     MPEXPR_TYPE_BINARY        },
146   { "invert",    (mpexpr_fun_t) mpz_invert,        MPEXPR_TYPE_BINARY        },
147   { "jacobi",    (mpexpr_fun_t) mpz_jacobi,        MPEXPR_TYPE_I_BINARY      },
148   { "kronecker", (mpexpr_fun_t) mpz_kronecker,     MPEXPR_TYPE_I_BINARY      },
149   { "lcm",       (mpexpr_fun_t) mpz_lcm,           MPEXPR_TYPE_BINARY
150 						   | MPEXPR_TYPE_PAIRWISE    },
151   { "lucnum",    (mpexpr_fun_t) mpz_lucnum_ui,     MPEXPR_TYPE_UNARY_UI      },
152   { "max",       (mpexpr_fun_t) mpz_cmp,           MPEXPR_TYPE_MAX
153 						   | MPEXPR_TYPE_PAIRWISE    },
154   { "min",       (mpexpr_fun_t) mpz_cmp,           MPEXPR_TYPE_MIN
155 						   | MPEXPR_TYPE_PAIRWISE    },
156   { "nextprime", (mpexpr_fun_t) mpz_nextprime,     MPEXPR_TYPE_UNARY         },
157   { "odd_p",     (mpexpr_fun_t) e_mpz_odd_p,       MPEXPR_TYPE_I_UNARY       },
158   { "perfect_power_p", (mpexpr_fun_t)mpz_perfect_power_p, MPEXPR_TYPE_I_UNARY},
159   { "perfect_square_p",(mpexpr_fun_t)mpz_perfect_square_p,MPEXPR_TYPE_I_UNARY},
160   { "popcount",  (mpexpr_fun_t) e_mpz_popcount,    MPEXPR_TYPE_UNARY         },
161   { "powm",      (mpexpr_fun_t) mpz_powm,          MPEXPR_TYPE_TERNARY       },
162   { "probab_prime_p",  (mpexpr_fun_t)mpz_probab_prime_p,  MPEXPR_TYPE_I_UNARY},
163   { "root",      (mpexpr_fun_t) mpz_root,          MPEXPR_TYPE_BINARY_UI     },
164   { "scan0",     (mpexpr_fun_t) e_mpz_scan0,       MPEXPR_TYPE_BINARY_UI     },
165   { "scan1",     (mpexpr_fun_t) e_mpz_scan1,       MPEXPR_TYPE_BINARY_UI     },
166   { "setbit",    (mpexpr_fun_t) e_mpz_setbit,      MPEXPR_TYPE_BINARY_UI     },
167   { "tstbit",    (mpexpr_fun_t) mpz_tstbit,        MPEXPR_TYPE_I_BINARY_UI   },
168   { "sgn",       (mpexpr_fun_t) e_mpz_sgn,         MPEXPR_TYPE_I_UNARY       },
169   { "sqrt",      (mpexpr_fun_t) mpz_sqrt,          MPEXPR_TYPE_UNARY         },
170   { NULL }
171 };
172 
173 /* The table is available globally only through a pointer, so the table size
174    can change without breaking binary compatibility. */
175 const struct mpexpr_operator_t * const mpz_expr_standard_table
176 = _mpz_expr_standard_table;
177 
178 
179 int
180 #if HAVE_STDARG
181 mpz_expr (mpz_ptr res, int base, const char *e, ...)
182 #else
183 mpz_expr (va_alist)
184      va_dcl
185 #endif
186 {
187   mpz_srcptr  var[MPEXPR_VARIABLES];
188   va_list     ap;
189   int         ret;
190 #if HAVE_STDARG
191   va_start (ap, e);
192 #else
193   mpz_ptr     res;
194   int         base;
195   const char  *e;
196   va_start (ap);
197   res  = va_arg (ap, mpz_ptr);
198   base = va_arg (ap, int);
199   e    = va_arg (ap, const char *);
200 #endif
201 
202   TRACE (printf ("mpz_expr(): base %d, %s\n", base, e));
203   ret = mpexpr_va_to_var ((void **) var, ap);
204   va_end (ap);
205 
206   if (ret != MPEXPR_RESULT_OK)
207     return ret;
208 
209   return mpz_expr_a (mpz_expr_standard_table, res, base, e, strlen(e), var);
210 }
211