[BACK]Return to exprz.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / demos / expr

Annotation of OpenXM_contrib/gmp/demos/expr/exprz.c, Revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>