From 45fee7d59915576a2eb2bc42ca1baa0e1426c2aa Mon Sep 17 00:00:00 2001 From: Juanjo Garcia-Ripoll Date: Sun, 6 Oct 2013 09:28:22 +0200 Subject: [PATCH] Removed gmp/demos directory, which is not needed --- src/gmp/demos/Makefile.am | 41 - src/gmp/demos/Makefile.in | 646 ------ src/gmp/demos/calc/Makefile.am | 38 - src/gmp/demos/calc/Makefile.in | 548 ----- src/gmp/demos/calc/README | 66 - src/gmp/demos/calc/calc-common.h | 36 - src/gmp/demos/calc/calc-config-h.in | 22 - src/gmp/demos/calc/calc.c | 1659 -------------- src/gmp/demos/calc/calc.h | 43 - src/gmp/demos/calc/calc.y | 319 --- src/gmp/demos/calc/calclex.c | 1704 -------------- src/gmp/demos/calc/calclex.l | 114 - src/gmp/demos/calc/calcread.c | 147 -- src/gmp/demos/expr/Makefile.am | 45 - src/gmp/demos/expr/Makefile.in | 541 ----- src/gmp/demos/expr/README | 492 ---- src/gmp/demos/expr/expr-impl.h | 137 -- src/gmp/demos/expr/expr.c | 825 ------- src/gmp/demos/expr/expr.h | 131 -- src/gmp/demos/expr/exprf.c | 129 -- src/gmp/demos/expr/exprfa.c | 182 -- src/gmp/demos/expr/exprq.c | 161 -- src/gmp/demos/expr/exprqa.c | 91 - src/gmp/demos/expr/exprv.c | 49 - src/gmp/demos/expr/exprz.c | 213 -- src/gmp/demos/expr/exprza.c | 99 - src/gmp/demos/expr/run-expr.c | 233 -- src/gmp/demos/expr/t-expr.c | 501 ----- src/gmp/demos/factorize.c | 368 --- src/gmp/demos/isprime.c | 68 - src/gmp/demos/perl/GMP.pm | 653 ------ src/gmp/demos/perl/GMP.xs | 3203 --------------------------- src/gmp/demos/perl/GMP/Mpf.pm | 97 - src/gmp/demos/perl/GMP/Mpq.pm | 80 - src/gmp/demos/perl/GMP/Mpz.pm | 92 - src/gmp/demos/perl/GMP/Rand.pm | 35 - src/gmp/demos/perl/INSTALL | 79 - src/gmp/demos/perl/Makefile.PL | 73 - src/gmp/demos/perl/sample.pl | 45 - src/gmp/demos/perl/test.pl | 2170 ------------------ src/gmp/demos/perl/test2.pl | 66 - src/gmp/demos/perl/typemap | 99 - src/gmp/demos/pexpr-config-h.in | 46 - src/gmp/demos/pexpr.c | 1365 ------------ src/gmp/demos/primes.c | 388 ---- src/gmp/demos/qcn.c | 173 -- 46 files changed, 18312 deletions(-) delete mode 100644 src/gmp/demos/Makefile.am delete mode 100644 src/gmp/demos/Makefile.in delete mode 100644 src/gmp/demos/calc/Makefile.am delete mode 100644 src/gmp/demos/calc/Makefile.in delete mode 100644 src/gmp/demos/calc/README delete mode 100644 src/gmp/demos/calc/calc-common.h delete mode 100644 src/gmp/demos/calc/calc-config-h.in delete mode 100644 src/gmp/demos/calc/calc.c delete mode 100644 src/gmp/demos/calc/calc.h delete mode 100644 src/gmp/demos/calc/calc.y delete mode 100644 src/gmp/demos/calc/calclex.c delete mode 100644 src/gmp/demos/calc/calclex.l delete mode 100644 src/gmp/demos/calc/calcread.c delete mode 100644 src/gmp/demos/expr/Makefile.am delete mode 100644 src/gmp/demos/expr/Makefile.in delete mode 100644 src/gmp/demos/expr/README delete mode 100644 src/gmp/demos/expr/expr-impl.h delete mode 100644 src/gmp/demos/expr/expr.c delete mode 100644 src/gmp/demos/expr/expr.h delete mode 100644 src/gmp/demos/expr/exprf.c delete mode 100644 src/gmp/demos/expr/exprfa.c delete mode 100644 src/gmp/demos/expr/exprq.c delete mode 100644 src/gmp/demos/expr/exprqa.c delete mode 100644 src/gmp/demos/expr/exprv.c delete mode 100644 src/gmp/demos/expr/exprz.c delete mode 100644 src/gmp/demos/expr/exprza.c delete mode 100644 src/gmp/demos/expr/run-expr.c delete mode 100644 src/gmp/demos/expr/t-expr.c delete mode 100644 src/gmp/demos/factorize.c delete mode 100644 src/gmp/demos/isprime.c delete mode 100644 src/gmp/demos/perl/GMP.pm delete mode 100644 src/gmp/demos/perl/GMP.xs delete mode 100644 src/gmp/demos/perl/GMP/Mpf.pm delete mode 100644 src/gmp/demos/perl/GMP/Mpq.pm delete mode 100644 src/gmp/demos/perl/GMP/Mpz.pm delete mode 100644 src/gmp/demos/perl/GMP/Rand.pm delete mode 100644 src/gmp/demos/perl/INSTALL delete mode 100644 src/gmp/demos/perl/Makefile.PL delete mode 100644 src/gmp/demos/perl/sample.pl delete mode 100644 src/gmp/demos/perl/test.pl delete mode 100644 src/gmp/demos/perl/test2.pl delete mode 100644 src/gmp/demos/perl/typemap delete mode 100644 src/gmp/demos/pexpr-config-h.in delete mode 100644 src/gmp/demos/pexpr.c delete mode 100644 src/gmp/demos/primes.c delete mode 100644 src/gmp/demos/qcn.c diff --git a/src/gmp/demos/Makefile.am b/src/gmp/demos/Makefile.am deleted file mode 100644 index d14c25201..000000000 --- a/src/gmp/demos/Makefile.am +++ /dev/null @@ -1,41 +0,0 @@ -## Process this file with automake to generate Makefile.in - -# Copyright 2000, 2001, 2002 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or (at your -# option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -SUBDIRS = calc expr -EXTRA_DIST = perl - -INCLUDES = -I$(top_srcdir) -LDADD = $(top_builddir)/libgmp.la - -qcn_LDADD = $(LDADD) $(LIBM) -primes_LDADD = $(LDADD) $(LIBM) - -# None of these programs are built by default, but "make " will -# build them once libgmp.la is built. -# -EXTRA_PROGRAMS = factorize isprime pexpr primes qcn - -CLEANFILES = $(EXTRA_PROGRAMS) - -allprogs: $(EXTRA_PROGRAMS) - cd calc; $(MAKE) $(AM_MAKEFLAGS) allprogs - cd expr; $(MAKE) $(AM_MAKEFLAGS) allprogs diff --git a/src/gmp/demos/Makefile.in b/src/gmp/demos/Makefile.in deleted file mode 100644 index 9a30d4ad1..000000000 --- a/src/gmp/demos/Makefile.in +++ /dev/null @@ -1,646 +0,0 @@ -# Makefile.in generated by automake 1.9.6 from Makefile.am. -# @configure_input@ - -# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, -# 2003, 2004, 2005 Free Software Foundation, Inc. -# This Makefile.in is free software; the Free Software Foundation -# gives unlimited permission to copy and/or distribute it, -# with or without modifications, as long as this notice is preserved. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY, to the extent permitted by law; without -# even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - -@SET_MAKE@ - -# Copyright 2000, 2001, 2002 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or (at your -# option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. -srcdir = @srcdir@ -top_srcdir = @top_srcdir@ -VPATH = @srcdir@ -pkgdatadir = $(datadir)/@PACKAGE@ -pkglibdir = $(libdir)/@PACKAGE@ -pkgincludedir = $(includedir)/@PACKAGE@ -top_builddir = .. -am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd -INSTALL = @INSTALL@ -install_sh_DATA = $(install_sh) -c -m 644 -install_sh_PROGRAM = $(install_sh) -c -install_sh_SCRIPT = $(install_sh) -c -INSTALL_HEADER = $(INSTALL_DATA) -transform = $(program_transform_name) -NORMAL_INSTALL = : -PRE_INSTALL = : -POST_INSTALL = : -NORMAL_UNINSTALL = : -PRE_UNINSTALL = : -POST_UNINSTALL = : -build_triplet = @build@ -host_triplet = @host@ -ANSI2KNR = $(top_builddir)/ansi2knr -LIBOBJDIR = -EXTRA_PROGRAMS = factorize$(EXEEXT) isprime$(EXEEXT) pexpr$(EXEEXT) \ - primes$(EXEEXT) qcn$(EXEEXT) -subdir = demos -DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in \ - $(srcdir)/pexpr-config-h.in -ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 -am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ - $(top_srcdir)/configure.in -am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ - $(ACLOCAL_M4) -mkinstalldirs = $(install_sh) -d -CONFIG_HEADER = $(top_builddir)/config.h -CONFIG_CLEAN_FILES = pexpr-config.h -factorize_SOURCES = factorize.c -factorize_OBJECTS = factorize$U.$(OBJEXT) -factorize_LDADD = $(LDADD) -factorize_DEPENDENCIES = $(top_builddir)/libgmp.la -isprime_SOURCES = isprime.c -isprime_OBJECTS = isprime$U.$(OBJEXT) -isprime_LDADD = $(LDADD) -isprime_DEPENDENCIES = $(top_builddir)/libgmp.la -pexpr_SOURCES = pexpr.c -pexpr_OBJECTS = pexpr$U.$(OBJEXT) -pexpr_LDADD = $(LDADD) -pexpr_DEPENDENCIES = $(top_builddir)/libgmp.la -primes_SOURCES = primes.c -primes_OBJECTS = primes$U.$(OBJEXT) -am__DEPENDENCIES_1 = $(top_builddir)/libgmp.la -am__DEPENDENCIES_2 = -primes_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) -qcn_SOURCES = qcn.c -qcn_OBJECTS = qcn$U.$(OBJEXT) -qcn_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_2) -DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) -depcomp = -am__depfiles_maybe = -COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ - $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -LTCOMPILE = $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) \ - $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ - $(AM_CFLAGS) $(CFLAGS) -CCLD = $(CC) -LINK = $(LIBTOOL) --tag=CC --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ - $(AM_LDFLAGS) $(LDFLAGS) -o $@ -SOURCES = factorize.c isprime.c pexpr.c primes.c qcn.c -DIST_SOURCES = factorize.c isprime.c pexpr.c primes.c qcn.c -RECURSIVE_TARGETS = all-recursive check-recursive dvi-recursive \ - html-recursive info-recursive install-data-recursive \ - install-exec-recursive install-info-recursive \ - install-recursive installcheck-recursive installdirs-recursive \ - pdf-recursive ps-recursive uninstall-info-recursive \ - uninstall-recursive -ETAGS = etags -CTAGS = ctags -DIST_SUBDIRS = $(SUBDIRS) -DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) -ABI = @ABI@ -ACLOCAL = @ACLOCAL@ -AMTAR = @AMTAR@ -AR = @AR@ -AS = @AS@ -AUTOCONF = @AUTOCONF@ -AUTOHEADER = @AUTOHEADER@ -AUTOMAKE = @AUTOMAKE@ -AWK = @AWK@ -BITS_PER_MP_LIMB = @BITS_PER_MP_LIMB@ -CALLING_CONVENTIONS_OBJS = @CALLING_CONVENTIONS_OBJS@ -CC = @CC@ -CCAS = @CCAS@ -CC_FOR_BUILD = @CC_FOR_BUILD@ -CFLAGS = @CFLAGS@ -CPP = @CPP@ -CPPFLAGS = @CPPFLAGS@ -CPP_FOR_BUILD = @CPP_FOR_BUILD@ -CXX = @CXX@ -CXXCPP = @CXXCPP@ -CXXFLAGS = @CXXFLAGS@ -CYGPATH_W = @CYGPATH_W@ -DEFN_LONG_LONG_LIMB = @DEFN_LONG_LONG_LIMB@ -DEFS = @DEFS@ -DLLTOOL = @DLLTOOL@ -DSYMUTIL = @DSYMUTIL@ -DUMPBIN = @DUMPBIN@ -ECHO_C = @ECHO_C@ -ECHO_N = @ECHO_N@ -ECHO_T = @ECHO_T@ -EGREP = @EGREP@ -ENABLE_STATIC_FALSE = @ENABLE_STATIC_FALSE@ -ENABLE_STATIC_TRUE = @ENABLE_STATIC_TRUE@ -EXEEXT = @EXEEXT@ -EXEEXT_FOR_BUILD = @EXEEXT_FOR_BUILD@ -FGREP = @FGREP@ -GMP_LDFLAGS = @GMP_LDFLAGS@ -GMP_NAIL_BITS = @GMP_NAIL_BITS@ -GREP = @GREP@ -HAVE_CLOCK_01 = @HAVE_CLOCK_01@ -HAVE_CPUTIME_01 = @HAVE_CPUTIME_01@ -HAVE_GETRUSAGE_01 = @HAVE_GETRUSAGE_01@ -HAVE_GETTIMEOFDAY_01 = @HAVE_GETTIMEOFDAY_01@ -HAVE_HOST_CPU_FAMILY_power = @HAVE_HOST_CPU_FAMILY_power@ -HAVE_HOST_CPU_FAMILY_powerpc = @HAVE_HOST_CPU_FAMILY_powerpc@ -HAVE_SIGACTION_01 = @HAVE_SIGACTION_01@ -HAVE_SIGALTSTACK_01 = @HAVE_SIGALTSTACK_01@ -HAVE_SIGSTACK_01 = @HAVE_SIGSTACK_01@ -HAVE_STACK_T_01 = @HAVE_STACK_T_01@ -HAVE_SYS_RESOURCE_H_01 = @HAVE_SYS_RESOURCE_H_01@ -INSTALL_DATA = @INSTALL_DATA@ -INSTALL_PROGRAM = @INSTALL_PROGRAM@ -INSTALL_SCRIPT = @INSTALL_SCRIPT@ -INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ -LD = @LD@ -LDFLAGS = @LDFLAGS@ -LEX = @LEX@ -LEXLIB = @LEXLIB@ -LEX_OUTPUT_ROOT = @LEX_OUTPUT_ROOT@ -LIBCURSES = @LIBCURSES@ -LIBGMPXX_LDFLAGS = @LIBGMPXX_LDFLAGS@ -LIBGMP_DLL = @LIBGMP_DLL@ -LIBGMP_LDFLAGS = @LIBGMP_LDFLAGS@ -LIBM = @LIBM@ -LIBM_FOR_BUILD = @LIBM_FOR_BUILD@ -LIBOBJS = @LIBOBJS@ -LIBREADLINE = @LIBREADLINE@ -LIBS = @LIBS@ -LIBTOOL = @LIBTOOL@ -LIPO = @LIPO@ -LN_S = @LN_S@ -LTLIBOBJS = @LTLIBOBJS@ -M4 = @M4@ -MAINT = @MAINT@ -MAINTAINER_MODE_FALSE = @MAINTAINER_MODE_FALSE@ -MAINTAINER_MODE_TRUE = @MAINTAINER_MODE_TRUE@ -MAKEINFO = @MAKEINFO@ -MANIFEST_TOOL = @MANIFEST_TOOL@ -NM = @NM@ -NMEDIT = @NMEDIT@ -OBJDUMP = @OBJDUMP@ -OBJEXT = @OBJEXT@ -OTOOL = @OTOOL@ -OTOOL64 = @OTOOL64@ -PACKAGE = @PACKAGE@ -PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ -PACKAGE_NAME = @PACKAGE_NAME@ -PACKAGE_STRING = @PACKAGE_STRING@ -PACKAGE_TARNAME = @PACKAGE_TARNAME@ -PACKAGE_URL = @PACKAGE_URL@ -PACKAGE_VERSION = @PACKAGE_VERSION@ -PATH_SEPARATOR = @PATH_SEPARATOR@ -RANLIB = @RANLIB@ -SED = @SED@ -SET_MAKE = @SET_MAKE@ -SHELL = @SHELL@ -SPEED_CYCLECOUNTER_OBJ = @SPEED_CYCLECOUNTER_OBJ@ -STRIP = @STRIP@ -TAL_OBJECT = @TAL_OBJECT@ -TUNE_SQR_OBJ = @TUNE_SQR_OBJ@ -U = @U@ -U_FOR_BUILD = @U_FOR_BUILD@ -VERSION = @VERSION@ -WANT_CXX_FALSE = @WANT_CXX_FALSE@ -WANT_CXX_TRUE = @WANT_CXX_TRUE@ -WANT_MPBSD_FALSE = @WANT_MPBSD_FALSE@ -WANT_MPBSD_TRUE = @WANT_MPBSD_TRUE@ -WITH_READLINE_01 = @WITH_READLINE_01@ -YACC = @YACC@ -YFLAGS = @YFLAGS@ -ac_ct_AR = @ac_ct_AR@ -ac_ct_CC = @ac_ct_CC@ -ac_ct_CXX = @ac_ct_CXX@ -ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ -am__leading_dot = @am__leading_dot@ -am__tar = @am__tar@ -am__untar = @am__untar@ -bindir = @bindir@ -build = @build@ -build_alias = @build_alias@ -build_cpu = @build_cpu@ -build_os = @build_os@ -build_vendor = @build_vendor@ -datadir = @datadir@ -datarootdir = @datarootdir@ -docdir = @docdir@ -dvidir = @dvidir@ -exec_prefix = @exec_prefix@ -gmp_srclinks = @gmp_srclinks@ -host = @host@ -host_alias = @host_alias@ -host_cpu = @host_cpu@ -host_os = @host_os@ -host_vendor = @host_vendor@ -htmldir = @htmldir@ -includedir = @includedir@ -infodir = @infodir@ -install_sh = @install_sh@ -libdir = @libdir@ -libexecdir = @libexecdir@ -localedir = @localedir@ -localstatedir = @localstatedir@ -mandir = @mandir@ -mkdir_p = @mkdir_p@ -mpn_objects = @mpn_objects@ -mpn_objs_in_libgmp = @mpn_objs_in_libgmp@ -mpn_objs_in_libmp = @mpn_objs_in_libmp@ -oldincludedir = @oldincludedir@ -pdfdir = @pdfdir@ -prefix = @prefix@ -program_transform_name = @program_transform_name@ -psdir = @psdir@ -sbindir = @sbindir@ -sharedstatedir = @sharedstatedir@ -sysconfdir = @sysconfdir@ -target_alias = @target_alias@ -SUBDIRS = calc expr -EXTRA_DIST = perl -INCLUDES = -I$(top_srcdir) -LDADD = $(top_builddir)/libgmp.la -qcn_LDADD = $(LDADD) $(LIBM) -primes_LDADD = $(LDADD) $(LIBM) -CLEANFILES = $(EXTRA_PROGRAMS) -all: all-recursive - -.SUFFIXES: -.SUFFIXES: .c .lo .o .obj -$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) - @for dep in $?; do \ - case '$(am__configure_deps)' in \ - *$$dep*) \ - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ - && exit 0; \ - exit 1;; \ - esac; \ - done; \ - echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu --ignore-deps demos/Makefile'; \ - cd $(top_srcdir) && \ - $(AUTOMAKE) --gnu --ignore-deps demos/Makefile -.PRECIOUS: Makefile -Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status - @case '$?' in \ - *config.status*) \ - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ - *) \ - echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ - cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ - esac; - -$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh - -$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh -$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh -pexpr-config.h: $(top_builddir)/config.status $(srcdir)/pexpr-config-h.in - cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ -factorize$(EXEEXT): $(factorize_OBJECTS) $(factorize_DEPENDENCIES) - @rm -f factorize$(EXEEXT) - $(LINK) $(factorize_LDFLAGS) $(factorize_OBJECTS) $(factorize_LDADD) $(LIBS) -isprime$(EXEEXT): $(isprime_OBJECTS) $(isprime_DEPENDENCIES) - @rm -f isprime$(EXEEXT) - $(LINK) $(isprime_LDFLAGS) $(isprime_OBJECTS) $(isprime_LDADD) $(LIBS) -pexpr$(EXEEXT): $(pexpr_OBJECTS) $(pexpr_DEPENDENCIES) - @rm -f pexpr$(EXEEXT) - $(LINK) $(pexpr_LDFLAGS) $(pexpr_OBJECTS) $(pexpr_LDADD) $(LIBS) -primes$(EXEEXT): $(primes_OBJECTS) $(primes_DEPENDENCIES) - @rm -f primes$(EXEEXT) - $(LINK) $(primes_LDFLAGS) $(primes_OBJECTS) $(primes_LDADD) $(LIBS) -qcn$(EXEEXT): $(qcn_OBJECTS) $(qcn_DEPENDENCIES) - @rm -f qcn$(EXEEXT) - $(LINK) $(qcn_LDFLAGS) $(qcn_OBJECTS) $(qcn_LDADD) $(LIBS) - -mostlyclean-compile: - -rm -f *.$(OBJEXT) - -distclean-compile: - -rm -f *.tab.c -$(top_builddir)/ansi2knr: - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) ansi2knr - -mostlyclean-kr: - -test "$U" = "" || rm -f *_.c - -.c.o: - $(COMPILE) -c $< - -.c.obj: - $(COMPILE) -c `$(CYGPATH_W) '$<'` - -.c.lo: - $(LTCOMPILE) -c -o $@ $< -factorize_.c: factorize.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/factorize.c; then echo $(srcdir)/factorize.c; else echo factorize.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -isprime_.c: isprime.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/isprime.c; then echo $(srcdir)/isprime.c; else echo isprime.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -pexpr_.c: pexpr.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/pexpr.c; then echo $(srcdir)/pexpr.c; else echo pexpr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -primes_.c: primes.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/primes.c; then echo $(srcdir)/primes.c; else echo primes.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -qcn_.c: qcn.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/qcn.c; then echo $(srcdir)/qcn.c; else echo qcn.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -factorize_.$(OBJEXT) factorize_.lo isprime_.$(OBJEXT) isprime_.lo \ -pexpr_.$(OBJEXT) pexpr_.lo primes_.$(OBJEXT) primes_.lo qcn_.$(OBJEXT) \ -qcn_.lo : $(ANSI2KNR) - -mostlyclean-libtool: - -rm -f *.lo - -clean-libtool: - -rm -rf .libs _libs - -distclean-libtool: - -rm -f libtool config.lt -uninstall-info-am: - -# This directory's subdirectories are mostly independent; you can cd -# into them and run `make' without going through this Makefile. -# To change the values of `make' variables: instead of editing Makefiles, -# (1) if the variable is set in `config.status', edit `config.status' -# (which will cause the Makefiles to be regenerated when you run `make'); -# (2) otherwise, pass the desired values on the `make' command line. -$(RECURSIVE_TARGETS): - @failcom='exit 1'; \ - for f in x $$MAKEFLAGS; do \ - case $$f in \ - *=* | --[!k]*);; \ - *k*) failcom='fail=yes';; \ - esac; \ - done; \ - dot_seen=no; \ - target=`echo $@ | sed s/-recursive//`; \ - list='$(SUBDIRS)'; for subdir in $$list; do \ - echo "Making $$target in $$subdir"; \ - if test "$$subdir" = "."; then \ - dot_seen=yes; \ - local_target="$$target-am"; \ - else \ - local_target="$$target"; \ - fi; \ - (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \ - || eval $$failcom; \ - done; \ - if test "$$dot_seen" = "no"; then \ - $(MAKE) $(AM_MAKEFLAGS) "$$target-am" || exit 1; \ - fi; test -z "$$fail" - -mostlyclean-recursive clean-recursive distclean-recursive \ -maintainer-clean-recursive: - @failcom='exit 1'; \ - for f in x $$MAKEFLAGS; do \ - case $$f in \ - *=* | --[!k]*);; \ - *k*) failcom='fail=yes';; \ - esac; \ - done; \ - dot_seen=no; \ - case "$@" in \ - distclean-* | maintainer-clean-*) list='$(DIST_SUBDIRS)' ;; \ - *) list='$(SUBDIRS)' ;; \ - esac; \ - rev=''; for subdir in $$list; do \ - if test "$$subdir" = "."; then :; else \ - rev="$$subdir $$rev"; \ - fi; \ - done; \ - rev="$$rev ."; \ - target=`echo $@ | sed s/-recursive//`; \ - for subdir in $$rev; do \ - echo "Making $$target in $$subdir"; \ - if test "$$subdir" = "."; then \ - local_target="$$target-am"; \ - else \ - local_target="$$target"; \ - fi; \ - (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \ - || eval $$failcom; \ - done && test -z "$$fail" -tags-recursive: - list='$(SUBDIRS)'; for subdir in $$list; do \ - test "$$subdir" = . || (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) tags); \ - done -ctags-recursive: - list='$(SUBDIRS)'; for subdir in $$list; do \ - test "$$subdir" = . || (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) ctags); \ - done - -ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - mkid -fID $$unique -tags: TAGS - -TAGS: tags-recursive $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - if ($(ETAGS) --etags-include --version) >/dev/null 2>&1; then \ - include_option=--etags-include; \ - empty_fix=.; \ - else \ - include_option=--include; \ - empty_fix=; \ - fi; \ - list='$(SUBDIRS)'; for subdir in $$list; do \ - if test "$$subdir" = .; then :; else \ - test ! -f $$subdir/TAGS || \ - tags="$$tags $$include_option=$$here/$$subdir/TAGS"; \ - fi; \ - done; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ - test -n "$$unique" || unique=$$empty_fix; \ - $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ - $$tags $$unique; \ - fi -ctags: CTAGS -CTAGS: ctags-recursive $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - test -z "$(CTAGS_ARGS)$$tags$$unique" \ - || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ - $$tags $$unique - -GTAGS: - here=`$(am__cd) $(top_builddir) && pwd` \ - && cd $(top_srcdir) \ - && gtags -i $(GTAGS_ARGS) $$here - -distclean-tags: - -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags - -distdir: $(DISTFILES) - @srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \ - topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \ - list='$(DISTFILES)'; for file in $$list; do \ - case $$file in \ - $(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \ - $(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \ - esac; \ - if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ - dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ - if test "$$dir" != "$$file" && test "$$dir" != "."; then \ - dir="/$$dir"; \ - $(mkdir_p) "$(distdir)$$dir"; \ - else \ - dir=''; \ - fi; \ - if test -d $$d/$$file; then \ - if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ - cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ - fi; \ - cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ - else \ - test -f $(distdir)/$$file \ - || cp -p $$d/$$file $(distdir)/$$file \ - || exit 1; \ - fi; \ - done - list='$(DIST_SUBDIRS)'; for subdir in $$list; do \ - if test "$$subdir" = .; then :; else \ - test -d "$(distdir)/$$subdir" \ - || $(mkdir_p) "$(distdir)/$$subdir" \ - || exit 1; \ - distdir=`$(am__cd) $(distdir) && pwd`; \ - top_distdir=`$(am__cd) $(top_distdir) && pwd`; \ - (cd $$subdir && \ - $(MAKE) $(AM_MAKEFLAGS) \ - top_distdir="$$top_distdir" \ - distdir="$$distdir/$$subdir" \ - distdir) \ - || exit 1; \ - fi; \ - done -check-am: all-am -check: check-recursive -all-am: Makefile -installdirs: installdirs-recursive -installdirs-am: -install: install-recursive -install-exec: install-exec-recursive -install-data: install-data-recursive -uninstall: uninstall-recursive - -install-am: all-am - @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am - -installcheck: installcheck-recursive -install-strip: - $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ - install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ - `test -z '$(STRIP)' || \ - echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install -mostlyclean-generic: - -clean-generic: - -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) - -distclean-generic: - -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) - -maintainer-clean-generic: - @echo "This command is intended for maintainers to use" - @echo "it deletes files that may require special tools to rebuild." -clean: clean-recursive - -clean-am: clean-generic clean-libtool mostlyclean-am - -distclean: distclean-recursive - -rm -f Makefile -distclean-am: clean-am distclean-compile distclean-generic \ - distclean-libtool distclean-tags - -dvi: dvi-recursive - -dvi-am: - -html: html-recursive - -info: info-recursive - -info-am: - -install-data-am: - -install-exec-am: - -install-info: install-info-recursive - -install-man: - -installcheck-am: - -maintainer-clean: maintainer-clean-recursive - -rm -f Makefile -maintainer-clean-am: distclean-am maintainer-clean-generic - -mostlyclean: mostlyclean-recursive - -mostlyclean-am: mostlyclean-compile mostlyclean-generic mostlyclean-kr \ - mostlyclean-libtool - -pdf: pdf-recursive - -pdf-am: - -ps: ps-recursive - -ps-am: - -uninstall-am: uninstall-info-am - -uninstall-info: uninstall-info-recursive - -.PHONY: $(RECURSIVE_TARGETS) CTAGS GTAGS all all-am check check-am \ - clean clean-generic clean-libtool clean-recursive ctags \ - ctags-recursive distclean distclean-compile distclean-generic \ - distclean-libtool distclean-recursive distclean-tags distdir \ - dvi dvi-am html html-am info info-am install install-am \ - install-data install-data-am install-exec install-exec-am \ - install-info install-info-am install-man install-strip \ - installcheck installcheck-am installdirs installdirs-am \ - maintainer-clean maintainer-clean-generic \ - maintainer-clean-recursive mostlyclean mostlyclean-compile \ - mostlyclean-generic mostlyclean-kr mostlyclean-libtool \ - mostlyclean-recursive pdf pdf-am ps ps-am tags tags-recursive \ - uninstall uninstall-am uninstall-info-am - - -allprogs: $(EXTRA_PROGRAMS) - cd calc; $(MAKE) $(AM_MAKEFLAGS) allprogs - cd expr; $(MAKE) $(AM_MAKEFLAGS) allprogs -# Tell versions [3.59,3.63) of GNU make to not export all variables. -# Otherwise a system limit (for SysV at least) may be exceeded. -.NOEXPORT: diff --git a/src/gmp/demos/calc/Makefile.am b/src/gmp/demos/calc/Makefile.am deleted file mode 100644 index 0f820618d..000000000 --- a/src/gmp/demos/calc/Makefile.am +++ /dev/null @@ -1,38 +0,0 @@ -## Process this file with automake to generate Makefile.in - -# Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or (at your -# option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -INCLUDES = -I$(top_srcdir) - -# $(LEXLIB) is not actually needed for flex (which means the distributed -# calclex.c), but it's included here for the benefit of anyone rebuilding -# with some other lex. -# -LDADD = $(top_builddir)/libgmp.la $(LIBREADLINE) $(LIBCURSES) $(LEXLIB) - -EXTRA_PROGRAMS = calc -AM_YFLAGS = -d -calc_SOURCES = calc.y calclex.l calcread.c calc-common.h -BUILT_SOURCES = calc.h - -CLEANFILES = $(EXTRA_PROGRAMS) - -allprogs: $(EXTRA_PROGRAMS) diff --git a/src/gmp/demos/calc/Makefile.in b/src/gmp/demos/calc/Makefile.in deleted file mode 100644 index 0006c32c1..000000000 --- a/src/gmp/demos/calc/Makefile.in +++ /dev/null @@ -1,548 +0,0 @@ -# Makefile.in generated by automake 1.9.6 from Makefile.am. -# @configure_input@ - -# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, -# 2003, 2004, 2005 Free Software Foundation, Inc. -# This Makefile.in is free software; the Free Software Foundation -# gives unlimited permission to copy and/or distribute it, -# with or without modifications, as long as this notice is preserved. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY, to the extent permitted by law; without -# even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - -@SET_MAKE@ - -# Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or (at your -# option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. -srcdir = @srcdir@ -top_srcdir = @top_srcdir@ -VPATH = @srcdir@ -pkgdatadir = $(datadir)/@PACKAGE@ -pkglibdir = $(libdir)/@PACKAGE@ -pkgincludedir = $(includedir)/@PACKAGE@ -top_builddir = ../.. -am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd -INSTALL = @INSTALL@ -install_sh_DATA = $(install_sh) -c -m 644 -install_sh_PROGRAM = $(install_sh) -c -install_sh_SCRIPT = $(install_sh) -c -INSTALL_HEADER = $(INSTALL_DATA) -transform = $(program_transform_name) -NORMAL_INSTALL = : -PRE_INSTALL = : -POST_INSTALL = : -NORMAL_UNINSTALL = : -PRE_UNINSTALL = : -POST_UNINSTALL = : -build_triplet = @build@ -host_triplet = @host@ -ANSI2KNR = $(top_builddir)/ansi2knr -LIBOBJDIR = -EXTRA_PROGRAMS = calc$(EXEEXT) -subdir = demos/calc -DIST_COMMON = README $(srcdir)/Makefile.am $(srcdir)/Makefile.in \ - $(srcdir)/calc-config-h.in calc.c calc.h calclex.c -ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 -am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ - $(top_srcdir)/configure.in -am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ - $(ACLOCAL_M4) -mkinstalldirs = $(install_sh) -d -CONFIG_HEADER = $(top_builddir)/config.h -CONFIG_CLEAN_FILES = calc-config.h -am_calc_OBJECTS = calc$U.$(OBJEXT) calclex$U.$(OBJEXT) \ - calcread$U.$(OBJEXT) -calc_OBJECTS = $(am_calc_OBJECTS) -calc_LDADD = $(LDADD) -am__DEPENDENCIES_1 = -calc_DEPENDENCIES = $(top_builddir)/libgmp.la $(am__DEPENDENCIES_1) \ - $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) -DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) -depcomp = -am__depfiles_maybe = -COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ - $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -LTCOMPILE = $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) \ - $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ - $(AM_CFLAGS) $(CFLAGS) -CCLD = $(CC) -LINK = $(LIBTOOL) --tag=CC --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ - $(AM_LDFLAGS) $(LDFLAGS) -o $@ -LEXCOMPILE = $(LEX) $(LFLAGS) $(AM_LFLAGS) -LTLEXCOMPILE = $(LIBTOOL) --mode=compile $(LEX) $(LFLAGS) $(AM_LFLAGS) -YACCCOMPILE = $(YACC) $(YFLAGS) $(AM_YFLAGS) -LTYACCCOMPILE = $(LIBTOOL) --mode=compile $(YACC) $(YFLAGS) \ - $(AM_YFLAGS) -SOURCES = $(calc_SOURCES) -DIST_SOURCES = $(calc_SOURCES) -ETAGS = etags -CTAGS = ctags -DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) -ABI = @ABI@ -ACLOCAL = @ACLOCAL@ -AMTAR = @AMTAR@ -AR = @AR@ -AS = @AS@ -AUTOCONF = @AUTOCONF@ -AUTOHEADER = @AUTOHEADER@ -AUTOMAKE = @AUTOMAKE@ -AWK = @AWK@ -BITS_PER_MP_LIMB = @BITS_PER_MP_LIMB@ -CALLING_CONVENTIONS_OBJS = @CALLING_CONVENTIONS_OBJS@ -CC = @CC@ -CCAS = @CCAS@ -CC_FOR_BUILD = @CC_FOR_BUILD@ -CFLAGS = @CFLAGS@ -CPP = @CPP@ -CPPFLAGS = @CPPFLAGS@ -CPP_FOR_BUILD = @CPP_FOR_BUILD@ -CXX = @CXX@ -CXXCPP = @CXXCPP@ -CXXFLAGS = @CXXFLAGS@ -CYGPATH_W = @CYGPATH_W@ -DEFN_LONG_LONG_LIMB = @DEFN_LONG_LONG_LIMB@ -DEFS = @DEFS@ -DLLTOOL = @DLLTOOL@ -DSYMUTIL = @DSYMUTIL@ -DUMPBIN = @DUMPBIN@ -ECHO_C = @ECHO_C@ -ECHO_N = @ECHO_N@ -ECHO_T = @ECHO_T@ -EGREP = @EGREP@ -ENABLE_STATIC_FALSE = @ENABLE_STATIC_FALSE@ -ENABLE_STATIC_TRUE = @ENABLE_STATIC_TRUE@ -EXEEXT = @EXEEXT@ -EXEEXT_FOR_BUILD = @EXEEXT_FOR_BUILD@ -FGREP = @FGREP@ -GMP_LDFLAGS = @GMP_LDFLAGS@ -GMP_NAIL_BITS = @GMP_NAIL_BITS@ -GREP = @GREP@ -HAVE_CLOCK_01 = @HAVE_CLOCK_01@ -HAVE_CPUTIME_01 = @HAVE_CPUTIME_01@ -HAVE_GETRUSAGE_01 = @HAVE_GETRUSAGE_01@ -HAVE_GETTIMEOFDAY_01 = @HAVE_GETTIMEOFDAY_01@ -HAVE_HOST_CPU_FAMILY_power = @HAVE_HOST_CPU_FAMILY_power@ -HAVE_HOST_CPU_FAMILY_powerpc = @HAVE_HOST_CPU_FAMILY_powerpc@ -HAVE_SIGACTION_01 = @HAVE_SIGACTION_01@ -HAVE_SIGALTSTACK_01 = @HAVE_SIGALTSTACK_01@ -HAVE_SIGSTACK_01 = @HAVE_SIGSTACK_01@ -HAVE_STACK_T_01 = @HAVE_STACK_T_01@ -HAVE_SYS_RESOURCE_H_01 = @HAVE_SYS_RESOURCE_H_01@ -INSTALL_DATA = @INSTALL_DATA@ -INSTALL_PROGRAM = @INSTALL_PROGRAM@ -INSTALL_SCRIPT = @INSTALL_SCRIPT@ -INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ -LD = @LD@ -LDFLAGS = @LDFLAGS@ -LEX = @LEX@ -LEXLIB = @LEXLIB@ -LEX_OUTPUT_ROOT = @LEX_OUTPUT_ROOT@ -LIBCURSES = @LIBCURSES@ -LIBGMPXX_LDFLAGS = @LIBGMPXX_LDFLAGS@ -LIBGMP_DLL = @LIBGMP_DLL@ -LIBGMP_LDFLAGS = @LIBGMP_LDFLAGS@ -LIBM = @LIBM@ -LIBM_FOR_BUILD = @LIBM_FOR_BUILD@ -LIBOBJS = @LIBOBJS@ -LIBREADLINE = @LIBREADLINE@ -LIBS = @LIBS@ -LIBTOOL = @LIBTOOL@ -LIPO = @LIPO@ -LN_S = @LN_S@ -LTLIBOBJS = @LTLIBOBJS@ -M4 = @M4@ -MAINT = @MAINT@ -MAINTAINER_MODE_FALSE = @MAINTAINER_MODE_FALSE@ -MAINTAINER_MODE_TRUE = @MAINTAINER_MODE_TRUE@ -MAKEINFO = @MAKEINFO@ -MANIFEST_TOOL = @MANIFEST_TOOL@ -NM = @NM@ -NMEDIT = @NMEDIT@ -OBJDUMP = @OBJDUMP@ -OBJEXT = @OBJEXT@ -OTOOL = @OTOOL@ -OTOOL64 = @OTOOL64@ -PACKAGE = @PACKAGE@ -PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ -PACKAGE_NAME = @PACKAGE_NAME@ -PACKAGE_STRING = @PACKAGE_STRING@ -PACKAGE_TARNAME = @PACKAGE_TARNAME@ -PACKAGE_URL = @PACKAGE_URL@ -PACKAGE_VERSION = @PACKAGE_VERSION@ -PATH_SEPARATOR = @PATH_SEPARATOR@ -RANLIB = @RANLIB@ -SED = @SED@ -SET_MAKE = @SET_MAKE@ -SHELL = @SHELL@ -SPEED_CYCLECOUNTER_OBJ = @SPEED_CYCLECOUNTER_OBJ@ -STRIP = @STRIP@ -TAL_OBJECT = @TAL_OBJECT@ -TUNE_SQR_OBJ = @TUNE_SQR_OBJ@ -U = @U@ -U_FOR_BUILD = @U_FOR_BUILD@ -VERSION = @VERSION@ -WANT_CXX_FALSE = @WANT_CXX_FALSE@ -WANT_CXX_TRUE = @WANT_CXX_TRUE@ -WANT_MPBSD_FALSE = @WANT_MPBSD_FALSE@ -WANT_MPBSD_TRUE = @WANT_MPBSD_TRUE@ -WITH_READLINE_01 = @WITH_READLINE_01@ -YACC = @YACC@ -YFLAGS = @YFLAGS@ -ac_ct_AR = @ac_ct_AR@ -ac_ct_CC = @ac_ct_CC@ -ac_ct_CXX = @ac_ct_CXX@ -ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ -am__leading_dot = @am__leading_dot@ -am__tar = @am__tar@ -am__untar = @am__untar@ -bindir = @bindir@ -build = @build@ -build_alias = @build_alias@ -build_cpu = @build_cpu@ -build_os = @build_os@ -build_vendor = @build_vendor@ -datadir = @datadir@ -datarootdir = @datarootdir@ -docdir = @docdir@ -dvidir = @dvidir@ -exec_prefix = @exec_prefix@ -gmp_srclinks = @gmp_srclinks@ -host = @host@ -host_alias = @host_alias@ -host_cpu = @host_cpu@ -host_os = @host_os@ -host_vendor = @host_vendor@ -htmldir = @htmldir@ -includedir = @includedir@ -infodir = @infodir@ -install_sh = @install_sh@ -libdir = @libdir@ -libexecdir = @libexecdir@ -localedir = @localedir@ -localstatedir = @localstatedir@ -mandir = @mandir@ -mkdir_p = @mkdir_p@ -mpn_objects = @mpn_objects@ -mpn_objs_in_libgmp = @mpn_objs_in_libgmp@ -mpn_objs_in_libmp = @mpn_objs_in_libmp@ -oldincludedir = @oldincludedir@ -pdfdir = @pdfdir@ -prefix = @prefix@ -program_transform_name = @program_transform_name@ -psdir = @psdir@ -sbindir = @sbindir@ -sharedstatedir = @sharedstatedir@ -sysconfdir = @sysconfdir@ -target_alias = @target_alias@ -INCLUDES = -I$(top_srcdir) - -# $(LEXLIB) is not actually needed for flex (which means the distributed -# calclex.c), but it's included here for the benefit of anyone rebuilding -# with some other lex. -# -LDADD = $(top_builddir)/libgmp.la $(LIBREADLINE) $(LIBCURSES) $(LEXLIB) -AM_YFLAGS = -d -calc_SOURCES = calc.y calclex.l calcread.c calc-common.h -BUILT_SOURCES = calc.h -CLEANFILES = $(EXTRA_PROGRAMS) -all: $(BUILT_SOURCES) - $(MAKE) $(AM_MAKEFLAGS) all-am - -.SUFFIXES: -.SUFFIXES: .c .l .lo .o .obj .y -$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) - @for dep in $?; do \ - case '$(am__configure_deps)' in \ - *$$dep*) \ - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ - && exit 0; \ - exit 1;; \ - esac; \ - done; \ - echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu --ignore-deps demos/calc/Makefile'; \ - cd $(top_srcdir) && \ - $(AUTOMAKE) --gnu --ignore-deps demos/calc/Makefile -.PRECIOUS: Makefile -Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status - @case '$?' in \ - *config.status*) \ - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ - *) \ - echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ - cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ - esac; - -$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh - -$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh -$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh -calc-config.h: $(top_builddir)/config.status $(srcdir)/calc-config-h.in - cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ -calc.h: calc.c - @if test ! -f $@; then \ - rm -f calc.c; \ - $(MAKE) calc.c; \ - else :; fi -calc$(EXEEXT): $(calc_OBJECTS) $(calc_DEPENDENCIES) - @rm -f calc$(EXEEXT) - $(LINK) $(calc_LDFLAGS) $(calc_OBJECTS) $(calc_LDADD) $(LIBS) - -mostlyclean-compile: - -rm -f *.$(OBJEXT) - -distclean-compile: - -rm -f *.tab.c -$(top_builddir)/ansi2knr: - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) ansi2knr - -mostlyclean-kr: - -test "$U" = "" || rm -f *_.c - -.c.o: - $(COMPILE) -c $< - -.c.obj: - $(COMPILE) -c `$(CYGPATH_W) '$<'` - -.c.lo: - $(LTCOMPILE) -c -o $@ $< -calc_.c: calc.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/calc.c; then echo $(srcdir)/calc.c; else echo calc.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -calclex_.c: calclex.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/calclex.c; then echo $(srcdir)/calclex.c; else echo calclex.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -calcread_.c: calcread.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/calcread.c; then echo $(srcdir)/calcread.c; else echo calcread.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -calc_.$(OBJEXT) calc_.lo calclex_.$(OBJEXT) calclex_.lo \ -calcread_.$(OBJEXT) calcread_.lo : $(ANSI2KNR) - -.l.c: - $(LEXCOMPILE) $< - sed '/^#/ s|$(LEX_OUTPUT_ROOT)\.c|$@|' $(LEX_OUTPUT_ROOT).c >$@ - rm -f $(LEX_OUTPUT_ROOT).c - -.y.c: - $(YACCCOMPILE) $< - if test -f y.tab.h; then \ - to=`echo "$*_H" | sed \ - -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \ - -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`; \ - sed -e "/^#/!b" -e "s/Y_TAB_H/$$to/g" -e "s|y\.tab\.h|$*.h|" \ - y.tab.h >$*.ht; \ - rm -f y.tab.h; \ - if cmp -s $*.ht $*.h; then \ - rm -f $*.ht ;\ - else \ - mv $*.ht $*.h; \ - fi; \ - fi - if test -f y.output; then \ - mv y.output $*.output; \ - fi - sed '/^#/ s|y\.tab\.c|$@|' y.tab.c >$@t && mv $@t $@ - rm -f y.tab.c - -mostlyclean-libtool: - -rm -f *.lo - -clean-libtool: - -rm -rf .libs _libs - -distclean-libtool: - -rm -f libtool config.lt -uninstall-info-am: - -ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - mkid -fID $$unique -tags: TAGS - -TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ - test -n "$$unique" || unique=$$empty_fix; \ - $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ - $$tags $$unique; \ - fi -ctags: CTAGS -CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - test -z "$(CTAGS_ARGS)$$tags$$unique" \ - || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ - $$tags $$unique - -GTAGS: - here=`$(am__cd) $(top_builddir) && pwd` \ - && cd $(top_srcdir) \ - && gtags -i $(GTAGS_ARGS) $$here - -distclean-tags: - -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags - -distdir: $(DISTFILES) - @srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \ - topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \ - list='$(DISTFILES)'; for file in $$list; do \ - case $$file in \ - $(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \ - $(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \ - esac; \ - if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ - dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ - if test "$$dir" != "$$file" && test "$$dir" != "."; then \ - dir="/$$dir"; \ - $(mkdir_p) "$(distdir)$$dir"; \ - else \ - dir=''; \ - fi; \ - if test -d $$d/$$file; then \ - if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ - cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ - fi; \ - cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ - else \ - test -f $(distdir)/$$file \ - || cp -p $$d/$$file $(distdir)/$$file \ - || exit 1; \ - fi; \ - done -check-am: all-am -check: $(BUILT_SOURCES) - $(MAKE) $(AM_MAKEFLAGS) check-am -all-am: Makefile -installdirs: -install: $(BUILT_SOURCES) - $(MAKE) $(AM_MAKEFLAGS) install-am -install-exec: install-exec-am -install-data: install-data-am -uninstall: uninstall-am - -install-am: all-am - @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am - -installcheck: installcheck-am -install-strip: - $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ - install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ - `test -z '$(STRIP)' || \ - echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install -mostlyclean-generic: - -clean-generic: - -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) - -distclean-generic: - -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) - -maintainer-clean-generic: - @echo "This command is intended for maintainers to use" - @echo "it deletes files that may require special tools to rebuild." - -rm -f calc.c - -rm -f calc.h - -rm -f calclex.c - -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES) -clean: clean-am - -clean-am: clean-generic clean-libtool mostlyclean-am - -distclean: distclean-am - -rm -f Makefile -distclean-am: clean-am distclean-compile distclean-generic \ - distclean-libtool distclean-tags - -dvi: dvi-am - -dvi-am: - -html: html-am - -info: info-am - -info-am: - -install-data-am: - -install-exec-am: - -install-info: install-info-am - -install-man: - -installcheck-am: - -maintainer-clean: maintainer-clean-am - -rm -f Makefile -maintainer-clean-am: distclean-am maintainer-clean-generic - -mostlyclean: mostlyclean-am - -mostlyclean-am: mostlyclean-compile mostlyclean-generic mostlyclean-kr \ - mostlyclean-libtool - -pdf: pdf-am - -pdf-am: - -ps: ps-am - -ps-am: - -uninstall-am: uninstall-info-am - -.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ - clean-libtool ctags distclean distclean-compile \ - distclean-generic distclean-libtool distclean-tags distdir dvi \ - dvi-am html html-am info info-am install install-am \ - install-data install-data-am install-exec install-exec-am \ - install-info install-info-am install-man install-strip \ - installcheck installcheck-am installdirs maintainer-clean \ - maintainer-clean-generic mostlyclean mostlyclean-compile \ - mostlyclean-generic mostlyclean-kr mostlyclean-libtool pdf \ - pdf-am ps ps-am tags uninstall uninstall-am uninstall-info-am - - -allprogs: $(EXTRA_PROGRAMS) -# Tell versions [3.59,3.63) of GNU make to not export all variables. -# Otherwise a system limit (for SysV at least) may be exceeded. -.NOEXPORT: diff --git a/src/gmp/demos/calc/README b/src/gmp/demos/calc/README deleted file mode 100644 index 0a272c08d..000000000 --- a/src/gmp/demos/calc/README +++ /dev/null @@ -1,66 +0,0 @@ -Copyright 2001 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - - - - DEMONSTRATION CALCULATOR PROGRAM - - -This is a simple program, meant only to show one way to use GMP with yacc -and lex to make a calculator. Usage and comments on the implementation can -be found in calc.y. - -Within a GMP build tree, the generated Makefile can be used to build the -program, - - make calc - -(or on a DOS system, "make calc.exe"). - -Elsewhere, once GMP has been installed, the program can be compiled with for -instance - - gcc calc.c calclex.c -lgmp -o calc - -Or if GNU readline is used then - - gcc calc.c calclex.c calcread.c -lgmp -lreadline -o calc - -(again, on a DOS system "-o calc.exe"). - -Readline support can be enabled or disabled in calc-config.h. That file is -created by the GMP ./configure based on the --with-readline option. The -default is --with-readline=detect, which means to use readline if available. -"yes" can be used to force it to be used, or "no" to not use it. - -The supplied calc.c was generated by GNU bison, but a standard yacc should -work too. - -The supplied calclex.c was generated by GNU flex, but a standard lex should -work too. The readline support may or may not work with a standard lex (see -comments with input() in calcread.c). Note also that a standard lex will -require its library "-ll" on the compile command line. "./configure" sets -this up in the GMP build tree Makefile. - - - ----------------- -Local variables: -mode: text -fill-column: 76 -End: diff --git a/src/gmp/demos/calc/calc-common.h b/src/gmp/demos/calc/calc-common.h deleted file mode 100644 index 228d870a5..000000000 --- a/src/gmp/demos/calc/calc-common.h +++ /dev/null @@ -1,36 +0,0 @@ -/* Prototypes etc for calc program. - -Copyright 2001 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include /* for size_t */ -#ifndef NO_CALC_H -#include "calc.h" -#endif -#include "calc-config.h" - -struct calc_keywords_t { - char *name; - int value; -}; - -extern int calc_option_readline; -extern int calc_more_input; -extern const struct calc_keywords_t calc_keywords[]; - -int calc_input (char *buf, size_t max_size); -void calc_init_readline (void); diff --git a/src/gmp/demos/calc/calc-config-h.in b/src/gmp/demos/calc/calc-config-h.in deleted file mode 100644 index e2bc46753..000000000 --- a/src/gmp/demos/calc/calc-config-h.in +++ /dev/null @@ -1,22 +0,0 @@ -/* Templates for calc program configuration. -*- mode:c -*- - -Copyright 2001 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - -/* Define if GNU readline should be used. */ -#define WITH_READLINE @WITH_READLINE_01@ diff --git a/src/gmp/demos/calc/calc.c b/src/gmp/demos/calc/calc.c deleted file mode 100644 index 15f6bc007..000000000 --- a/src/gmp/demos/calc/calc.c +++ /dev/null @@ -1,1659 +0,0 @@ -/* A Bison parser, made from calc.y - by GNU bison 1.34. */ - -#define YYBISON 1 /* Identify Bison output. */ - -# define EOS 257 -# define BAD 258 -# define HELP 259 -# define HEX 260 -# define DECIMAL 261 -# define QUIT 262 -# define ABS 263 -# define BIN 264 -# define FIB 265 -# define GCD 266 -# define KRON 267 -# define LCM 268 -# define LUCNUM 269 -# define NEXTPRIME 270 -# define POWM 271 -# define ROOT 272 -# define SQRT 273 -# define NUMBER 274 -# define VARIABLE 275 -# define LOR 276 -# define LAND 277 -# define EQ 278 -# define NE 279 -# define LE 280 -# define GE 281 -# define LSHIFT 282 -# define RSHIFT 283 -# define UMINUS 284 - -#line 1 "calc.y" - -/* A simple integer desk calculator using yacc and gmp. - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - -/* This is a simple program, meant only to show one way to use GMP for this - sort of thing. There's few features, and error checking is minimal. - Standard input is read, calc_help() below shows the inputs accepted. - - Expressions are evaluated as they're read. If user defined functions - were wanted it'd be necessary to build a parse tree like pexpr.c does, or - a list of operations for a stack based evaluator. That would also make - it possible to detect and optimize evaluations "mod m" like pexpr.c does. - - A stack is used for intermediate values in the expression evaluation, - separate from the yacc parser stack. This is simple, makes error - recovery easy, minimizes the junk around mpz calls in the rules, and - saves initializing or clearing "mpz_t"s during a calculation. A - disadvantage though is that variables must be copied to the stack to be - worked on. A more sophisticated calculator or language system might be - able to avoid that when executing a compiled or semi-compiled form. - - Avoiding repeated initializing and clearing of "mpz_t"s is important. In - this program the time spent parsing is obviously much greater than any - possible saving from this, but a proper calculator or language should - take some trouble over it. Don't be surprised if an init/clear takes 3 - or more times as long as a 10 limb addition, depending on the system (see - the mpz_init_realloc_clear example in tune/README). */ - - -#include -#include -#include -#include "gmp.h" -#define NO_CALC_H /* because it conflicts with normal calc.c stuff */ -#include "calc-common.h" - - -#define numberof(x) (sizeof (x) / sizeof ((x)[0])) - - -void -calc_help (void) -{ - printf ("Examples:\n"); - printf (" 2+3*4 expressions are evaluated\n"); - printf (" x=5^6 variables a to z can be set and used\n"); - printf ("Operators:\n"); - printf (" + - * arithmetic\n"); - printf (" / %% division and remainder (rounding towards negative infinity)\n"); - printf (" ^ exponentiation\n"); - printf (" ! factorial\n"); - printf (" << >> left and right shifts\n"); - printf (" <= >= > \\ comparisons, giving 1 if true, 0 if false\n"); - printf (" == != < /\n"); - printf (" && || logical and/or, giving 1 if true, 0 if false\n"); - printf ("Functions:\n"); - printf (" abs(n) absolute value\n"); - printf (" bin(n,m) binomial coefficient\n"); - printf (" fib(n) fibonacci number\n"); - printf (" gcd(a,b,..) greatest common divisor\n"); - printf (" kron(a,b) kronecker symbol\n"); - printf (" lcm(a,b,..) least common multiple\n"); - printf (" lucnum(n) lucas number\n"); - printf (" nextprime(n) next prime after n\n"); - printf (" powm(b,e,m) modulo powering, b^e%%m\n"); - printf (" root(n,r) r-th root\n"); - printf (" sqrt(n) square root\n"); - printf ("Other:\n"); - printf (" hex \\ set hex or decimal for input and output\n"); - printf (" decimal / (\"0x\" can be used for hex too)\n"); - printf (" quit exit program (EOF works too)\n"); - printf (" ; statements are separated with a ; or newline\n"); - printf (" \\ continue expressions with \\ before newline\n"); - printf (" # xxx comments are # though to newline\n"); - printf ("Hex numbers must be entered in upper case, to distinguish them from the\n"); - printf ("variables a to f (like in bc).\n"); -} - - -int ibase = 0; -int obase = 10; - - -/* The stack is a fixed size, which means there's a limit on the nesting - allowed in expressions. A more sophisticated program could let it grow - dynamically. */ - -mpz_t stack[100]; -mpz_ptr sp = stack[0]; - -#define CHECK_OVERFLOW() \ - if (sp >= stack[numberof(stack)]) \ - { \ - fprintf (stderr, \ - "Value stack overflow, too much nesting in expression\n"); \ - YYERROR; \ - } - -#define CHECK_EMPTY() \ - if (sp != stack[0]) \ - { \ - fprintf (stderr, "Oops, expected the value stack to be empty\n"); \ - sp = stack[0]; \ - } - - -mpz_t variable[26]; - -#define CHECK_VARIABLE(var) \ - if ((var) < 0 || (var) >= numberof (variable)) \ - { \ - fprintf (stderr, "Oops, bad variable somehow: %d\n", var); \ - YYERROR; \ - } - - -#define CHECK_UI(name,z) \ - if (! mpz_fits_ulong_p (z)) \ - { \ - fprintf (stderr, "%s too big\n", name); \ - YYERROR; \ - } - - -#line 143 "calc.y" -#ifndef YYSTYPE -typedef union { - char *str; - int var; -} yystype; -# define YYSTYPE yystype -#endif -#ifndef YYDEBUG -# define YYDEBUG 0 -#endif - - - -#define YYFINAL 118 -#define YYFLAG -32768 -#define YYNTBASE 44 - -/* YYTRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */ -#define YYTRANSLATE(x) ((unsigned)(x) <= 284 ? yytranslate[x] : 50) - -/* YYTRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX. */ -static const char yytranslate[] = -{ - 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 39, 2, 2, 2, 36, 2, 2, - 41, 42, 34, 32, 43, 33, 2, 35, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 24, 40, 25, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 38, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 1, 3, 4, 5, - 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 26, 27, - 28, 29, 30, 31, 37 -}; - -#if YYDEBUG -static const short yyprhs[] = -{ - 0, 0, 2, 5, 8, 12, 15, 16, 18, 22, - 24, 26, 28, 30, 34, 38, 42, 46, 50, 54, - 58, 62, 66, 69, 72, 76, 80, 84, 88, 92, - 96, 100, 104, 109, 116, 121, 126, 133, 138, 143, - 148, 157, 164, 169, 171, 173, 175, 179, 181 -}; -static const short yyrhs[] = -{ - 46, 0, 45, 46, 0, 46, 3, 0, 45, 46, - 3, 0, 1, 3, 0, 0, 47, 0, 21, 40, - 47, 0, 5, 0, 6, 0, 7, 0, 8, 0, - 41, 47, 42, 0, 47, 32, 47, 0, 47, 33, - 47, 0, 47, 34, 47, 0, 47, 35, 47, 0, - 47, 36, 47, 0, 47, 38, 47, 0, 47, 30, - 47, 0, 47, 31, 47, 0, 47, 39, 0, 33, - 47, 0, 47, 24, 47, 0, 47, 28, 47, 0, - 47, 26, 47, 0, 47, 27, 47, 0, 47, 29, - 47, 0, 47, 25, 47, 0, 47, 23, 47, 0, - 47, 22, 47, 0, 9, 41, 47, 42, 0, 10, - 41, 47, 43, 47, 42, 0, 11, 41, 47, 42, - 0, 12, 41, 48, 42, 0, 13, 41, 47, 43, - 47, 42, 0, 14, 41, 49, 42, 0, 15, 41, - 47, 42, 0, 16, 41, 47, 42, 0, 17, 41, - 47, 43, 47, 43, 47, 42, 0, 18, 41, 47, - 43, 47, 42, 0, 19, 41, 47, 42, 0, 21, - 0, 20, 0, 47, 0, 48, 43, 47, 0, 47, - 0, 49, 43, 47, 0 -}; - -#endif - -#if YYDEBUG -/* YYRLINE[YYN] -- source line where rule number YYN was defined. */ -static const short yyrline[] = -{ - 0, 167, 169, 171, 173, 174, 176, 178, 183, 189, - 190, 191, 192, 197, 199, 200, 201, 202, 203, 204, - 206, 208, 210, 212, 214, 215, 216, 217, 218, 219, - 221, 222, 224, 225, 227, 229, 230, 232, 233, 235, - 236, 237, 239, 241, 247, 257, 259, 261, 263 -}; -#endif - - -#if (YYDEBUG) || defined YYERROR_VERBOSE - -/* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */ -static const char *const yytname[] = -{ - "$", "error", "$undefined.", "EOS", "BAD", "HELP", "HEX", "DECIMAL", - "QUIT", "ABS", "BIN", "FIB", "GCD", "KRON", "LCM", "LUCNUM", - "NEXTPRIME", "POWM", "ROOT", "SQRT", "NUMBER", "VARIABLE", "LOR", - "LAND", "'<'", "'>'", "EQ", "NE", "LE", "GE", "LSHIFT", "RSHIFT", "'+'", - "'-'", "'*'", "'/'", "'%'", "UMINUS", "'^'", "'!'", "'='", "'('", "')'", - "','", "top", "statements", "statement", "e", "gcdlist", "lcmlist", 0 -}; -#endif - -/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ -static const short yyr1[] = -{ - 0, 44, 44, 45, 45, 45, 46, 46, 46, 46, - 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, - 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, - 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, - 47, 47, 47, 47, 47, 48, 48, 49, 49 -}; - -/* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ -static const short yyr2[] = -{ - 0, 1, 2, 2, 3, 2, 0, 1, 3, 1, - 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 2, 2, 3, 3, 3, 3, 3, 3, - 3, 3, 4, 6, 4, 4, 6, 4, 4, 4, - 8, 6, 4, 1, 1, 1, 3, 1, 3 -}; - -/* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE - doesn't specify something else to do. Zero means the default is an - error. */ -static const short yydefact[] = -{ - 0, 0, 9, 10, 11, 12, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 44, 43, 0, - 0, 6, 1, 7, 5, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 43, 23, 0, - 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, - 0, 0, 45, 0, 0, 47, 0, 0, 0, 0, - 0, 0, 8, 13, 4, 31, 30, 24, 29, 26, - 27, 25, 28, 20, 21, 14, 15, 16, 17, 18, - 19, 32, 0, 34, 35, 0, 0, 37, 0, 38, - 39, 0, 0, 42, 0, 46, 0, 48, 0, 0, - 33, 36, 0, 41, 0, 40, 0, 0, 0 -}; - -static const short yydefgoto[] = -{ - 116, 21, 22, 23, 63, 66 -}; - -static const short yypact[] = -{ - 39, 17,-32768,-32768,-32768,-32768, -20, 0, 2, 25, - 28, 30, 33, 34, 37, 40, 46,-32768, -18, 122, - 122, 89, 67, 462,-32768, 122, 122, 122, 122, 122, - 122, 122, 122, 122, 122, 122, 122,-32768, -36, 252, - 87,-32768, 122, 122, 122, 122, 122, 122, 122, 122, - 122, 122, 122, 122, 122, 122, 122, 122,-32768, 273, - 142, 294, 462, -38, 164, 462, -24, 315, 336, 186, - 208, 357, 462,-32768,-32768, 479, 495, 511, 511, 511, - 511, 511, 511, 29, 29, 50, 50, -36, -36, -36, - -36,-32768, 122,-32768,-32768, 122, 122,-32768, 122,-32768, - -32768, 122, 122,-32768, 378, 462, 399, 462, 230, 420, - -32768,-32768, 122,-32768, 441,-32768, 91, 92,-32768 -}; - -static const short yypgoto[] = -{ - -32768,-32768, 90, -19,-32768,-32768 -}; - - -#define YYLAST 550 - - -static const short yytable[] = -{ - 38, 39, 57, 58, 94, 95, 59, 60, 61, 62, - 64, 65, 67, 68, 69, 70, 71, 72, 97, 98, - 24, 25, 36, 75, 76, 77, 78, 79, 80, 81, - 82, 83, 84, 85, 86, 87, 88, 89, 90, -6, - 1, 26, -6, 27, 2, 3, 4, 5, 6, 7, - 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, - 18, 52, 53, 54, 55, 56, 28, 57, 58, 29, - 41, 30, 19, 104, 31, 32, 105, 106, 33, 107, - 20, 34, 108, 109, 54, 55, 56, 35, 57, 58, - 74, 117, 118, 114, 2, 3, 4, 5, 6, 7, - 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, - 18, 40, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, - 20, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 37, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, - 0, 0, 0, 20, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, - 57, 58, 0, 0, 0, 92, 42, 43, 44, 45, - 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, - 56, 0, 57, 58, 0, 0, 0, 96, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, - 54, 55, 56, 0, 57, 58, 0, 0, 0, 101, - 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, - 0, 102, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, - 0, 0, 0, 112, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, - 57, 58, 0, 0, 73, 42, 43, 44, 45, 46, - 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, - 0, 57, 58, 0, 0, 91, 42, 43, 44, 45, - 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, - 56, 0, 57, 58, 0, 0, 93, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 0, 57, 58, 0, 0, 99, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, - 54, 55, 56, 0, 57, 58, 0, 0, 100, 42, - 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, - 53, 54, 55, 56, 0, 57, 58, 0, 0, 103, - 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, - 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, - 110, 42, 43, 44, 45, 46, 47, 48, 49, 50, - 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, - 0, 111, 42, 43, 44, 45, 46, 47, 48, 49, - 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, - 0, 0, 113, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, - 58, 0, 0, 115, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, - 57, 58, 43, 44, 45, 46, 47, 48, 49, 50, - 51, 52, 53, 54, 55, 56, 0, 57, 58, 44, - 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 0, 57, 58,-32768,-32768,-32768,-32768,-32768, - -32768, 50, 51, 52, 53, 54, 55, 56, 0, 57, - 58 -}; - -static const short yycheck[] = -{ - 19, 20, 38, 39, 42, 43, 25, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, 42, 43, - 3, 41, 40, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, - 1, 41, 3, 41, 5, 6, 7, 8, 9, 10, - 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, - 21, 32, 33, 34, 35, 36, 41, 38, 39, 41, - 3, 41, 33, 92, 41, 41, 95, 96, 41, 98, - 41, 41, 101, 102, 34, 35, 36, 41, 38, 39, - 3, 0, 0, 112, 5, 6, 7, 8, 9, 10, - 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, - 21, 21, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, 33, -1, -1, -1, -1, -1, -1, -1, - 41, 9, 10, 11, 12, 13, 14, 15, 16, 17, - 18, 19, 20, 21, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, 33, -1, -1, -1, -1, - -1, -1, -1, 41, 22, 23, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, - 38, 39, -1, -1, -1, 43, 22, 23, 24, 25, - 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, - 36, -1, 38, 39, -1, -1, -1, 43, 22, 23, - 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, - 34, 35, 36, -1, 38, 39, -1, -1, -1, 43, - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, -1, 38, 39, -1, -1, - -1, 43, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, -1, 38, 39, - -1, -1, -1, 43, 22, 23, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, - 38, 39, -1, -1, 42, 22, 23, 24, 25, 26, - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, - -1, 38, 39, -1, -1, 42, 22, 23, 24, 25, - 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, - 36, -1, 38, 39, -1, -1, 42, 22, 23, 24, - 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, - 35, 36, -1, 38, 39, -1, -1, 42, 22, 23, - 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, - 34, 35, 36, -1, 38, 39, -1, -1, 42, 22, - 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, - 33, 34, 35, 36, -1, 38, 39, -1, -1, 42, - 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, -1, 38, 39, -1, -1, - 42, 22, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 34, 35, 36, -1, 38, 39, -1, - -1, 42, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, -1, 38, 39, - -1, -1, 42, 22, 23, 24, 25, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, -1, 38, - 39, -1, -1, 42, 22, 23, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, - 38, 39, 23, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 34, 35, 36, -1, 38, 39, 24, - 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, - 35, 36, -1, 38, 39, 24, 25, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, -1, 38, - 39 -}; -/* -*-C-*- Note some compilers choke on comments on `#line' lines. */ -#line 3 "/usr/share/bison/bison.simple" - -/* Skeleton output parser for bison, - - Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002 Free Software - Foundation, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. */ - -/* As a special exception, when this file is copied by Bison into a - Bison output file, you may use that output file without restriction. - This special exception was added by the Free Software Foundation - in version 1.24 of Bison. */ - -/* This is the parser code that is written into each bison parser when - the %semantic_parser declaration is not specified in the grammar. - It was written by Richard Stallman by simplifying the hairy parser - used when %semantic_parser is specified. */ - -/* All symbols defined below should begin with yy or YY, to avoid - infringing on user name space. This should be done even for local - variables, as they might otherwise be expanded by user macros. - There are some unavoidable exceptions within include files to - define necessary library symbols; they are noted "INFRINGES ON - USER NAME SPACE" below. */ - -#if ! defined (yyoverflow) || defined (YYERROR_VERBOSE) - -/* The parser invokes alloca or malloc; define the necessary symbols. */ - -# if YYSTACK_USE_ALLOCA -# define YYSTACK_ALLOC alloca -# else -# ifndef YYSTACK_USE_ALLOCA -# if defined (alloca) || defined (_ALLOCA_H) -# define YYSTACK_ALLOC alloca -# else -# ifdef __GNUC__ -# define YYSTACK_ALLOC __builtin_alloca -# endif -# endif -# endif -# endif - -# ifdef YYSTACK_ALLOC - /* Pacify GCC's `empty if-body' warning. */ -# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) -# else -# if defined (__STDC__) || defined (__cplusplus) -# include /* INFRINGES ON USER NAME SPACE */ -# define YYSIZE_T size_t -# endif -# define YYSTACK_ALLOC malloc -# define YYSTACK_FREE free -# endif - -/* A type that is properly aligned for any stack member. */ -union yyalloc -{ - short yyss; - YYSTYPE yyvs; -# if YYLSP_NEEDED - YYLTYPE yyls; -# endif -}; - -/* The size of the maximum gap between one aligned stack and the next. */ -# define YYSTACK_GAP_MAX (sizeof (union yyalloc) - 1) - -/* The size of an array large to enough to hold all stacks, each with - N elements. */ -# if YYLSP_NEEDED -# define YYSTACK_BYTES(N) \ - ((N) * (sizeof (short) + sizeof (YYSTYPE) + sizeof (YYLTYPE)) \ - + 2 * YYSTACK_GAP_MAX) -# else -# define YYSTACK_BYTES(N) \ - ((N) * (sizeof (short) + sizeof (YYSTYPE)) \ - + YYSTACK_GAP_MAX) -# endif - -/* Relocate the TYPE STACK from its old location to the new one. The - local variables YYSIZE and YYSTACKSIZE give the old and new number of - elements in the stack, and YYPTR gives the new location of the - stack. Advance YYPTR to a properly aligned location for the next - stack. */ -# define YYSTACK_RELOCATE(Type, Stack) \ - do \ - { \ - YYSIZE_T yynewbytes; \ - yymemcpy ((char *) yyptr, (char *) (Stack), \ - yysize * (YYSIZE_T) sizeof (Type)); \ - Stack = &yyptr->Stack; \ - yynewbytes = yystacksize * sizeof (Type) + YYSTACK_GAP_MAX; \ - yyptr += yynewbytes / sizeof (*yyptr); \ - } \ - while (0) - -#endif /* ! defined (yyoverflow) || defined (YYERROR_VERBOSE) */ - - -#if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__) -# define YYSIZE_T __SIZE_TYPE__ -#endif -#if ! defined (YYSIZE_T) && defined (size_t) -# define YYSIZE_T size_t -#endif -#if ! defined (YYSIZE_T) -# if defined (__STDC__) || defined (__cplusplus) -# include /* INFRINGES ON USER NAME SPACE */ -# define YYSIZE_T size_t -# endif -#endif -#if ! defined (YYSIZE_T) -# define YYSIZE_T unsigned int -#endif - -#define yyerrok (yyerrstatus = 0) -#define yyclearin (yychar = YYEMPTY) -#define YYEMPTY -2 -#define YYEOF 0 -#define YYACCEPT goto yyacceptlab -#define YYABORT goto yyabortlab -#define YYERROR goto yyerrlab1 -/* Like YYERROR except do call yyerror. This remains here temporarily - to ease the transition to the new meaning of YYERROR, for GCC. - Once GCC version 2 has supplanted version 1, this can go. */ -#define YYFAIL goto yyerrlab -#define YYRECOVERING() (!!yyerrstatus) -#define YYBACKUP(Token, Value) \ -do \ - if (yychar == YYEMPTY && yylen == 1) \ - { \ - yychar = (Token); \ - yylval = (Value); \ - yychar1 = YYTRANSLATE (yychar); \ - YYPOPSTACK; \ - goto yybackup; \ - } \ - else \ - { \ - yyerror ("syntax error: cannot back up"); \ - YYERROR; \ - } \ -while (0) - -#define YYTERROR 1 -#define YYERRCODE 256 - - -/* YYLLOC_DEFAULT -- Compute the default location (before the actions - are run). - - When YYLLOC_DEFAULT is run, CURRENT is set the location of the - first token. By default, to implement support for ranges, extend - its range to the last symbol. */ - -#ifndef YYLLOC_DEFAULT -# define YYLLOC_DEFAULT(Current, Rhs, N) \ - Current.last_line = Rhs[N].last_line; \ - Current.last_column = Rhs[N].last_column; -#endif - - -/* YYLEX -- calling `yylex' with the right arguments. */ - -#if YYPURE -# if YYLSP_NEEDED -# ifdef YYLEX_PARAM -# define YYLEX yylex (&yylval, &yylloc, YYLEX_PARAM) -# else -# define YYLEX yylex (&yylval, &yylloc) -# endif -# else /* !YYLSP_NEEDED */ -# ifdef YYLEX_PARAM -# define YYLEX yylex (&yylval, YYLEX_PARAM) -# else -# define YYLEX yylex (&yylval) -# endif -# endif /* !YYLSP_NEEDED */ -#else /* !YYPURE */ -# define YYLEX yylex () -#endif /* !YYPURE */ - - -/* Enable debugging if requested. */ -#if YYDEBUG - -# ifndef YYFPRINTF -# include /* INFRINGES ON USER NAME SPACE */ -# define YYFPRINTF fprintf -# endif - -# define YYDPRINTF(Args) \ -do { \ - if (yydebug) \ - YYFPRINTF Args; \ -} while (0) -/* Nonzero means print parse trace. It is left uninitialized so that - multiple parsers can coexist. */ -int yydebug; -#else /* !YYDEBUG */ -# define YYDPRINTF(Args) -#endif /* !YYDEBUG */ - -/* YYINITDEPTH -- initial size of the parser's stacks. */ -#ifndef YYINITDEPTH -# define YYINITDEPTH 200 -#endif - -/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only - if the built-in stack extension method is used). - - Do not make this value too large; the results are undefined if - SIZE_MAX < YYSTACK_BYTES (YYMAXDEPTH) - evaluated with infinite-precision integer arithmetic. */ - -#if YYMAXDEPTH == 0 -# undef YYMAXDEPTH -#endif - -#ifndef YYMAXDEPTH -# define YYMAXDEPTH 10000 -#endif - -#if ! defined (yyoverflow) && ! defined (yymemcpy) -# if __GNUC__ > 1 /* GNU C and GNU C++ define this. */ -# define yymemcpy __builtin_memcpy -# else /* not GNU C or C++ */ - -/* This is the most reliable way to avoid incompatibilities - in available built-in functions on various systems. */ -static void -# if defined (__STDC__) || defined (__cplusplus) -yymemcpy (char *yyto, const char *yyfrom, YYSIZE_T yycount) -# else -yymemcpy (yyto, yyfrom, yycount) - char *yyto; - const char *yyfrom; - YYSIZE_T yycount; -# endif -{ - register const char *yyf = yyfrom; - register char *yyt = yyto; - register YYSIZE_T yyi = yycount; - - while (yyi-- != 0) - *yyt++ = *yyf++; -} -# endif -#endif - -#ifdef YYERROR_VERBOSE - -# ifndef yystrlen -# if defined (__GLIBC__) && defined (_STRING_H) -# define yystrlen strlen -# else -/* Return the length of YYSTR. */ -static YYSIZE_T -# if defined (__STDC__) || defined (__cplusplus) -yystrlen (const char *yystr) -# else -yystrlen (yystr) - const char *yystr; -# endif -{ - register const char *yys = yystr; - - while (*yys++ != '\0') - continue; - - return yys - yystr - 1; -} -# endif -# endif - -# ifndef yystpcpy -# if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE) -# define yystpcpy stpcpy -# else -/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in - YYDEST. */ -static char * -# if defined (__STDC__) || defined (__cplusplus) -yystpcpy (char *yydest, const char *yysrc) -# else -yystpcpy (yydest, yysrc) - char *yydest; - const char *yysrc; -# endif -{ - register char *yyd = yydest; - register const char *yys = yysrc; - - while ((*yyd++ = *yys++) != '\0') - continue; - - return yyd - 1; -} -# endif -# endif -#endif - -#line 319 "/usr/share/bison/bison.simple" - - -/* The user can define YYPARSE_PARAM as the name of an argument to be passed - into yyparse. The argument should have type void *. - It should actually point to an object. - Grammar actions can access the variable by casting it - to the proper pointer type. */ - -#ifdef YYPARSE_PARAM -# if defined (__STDC__) || defined (__cplusplus) -# define YYPARSE_PARAM_ARG void *YYPARSE_PARAM -# define YYPARSE_PARAM_DECL -# else -# define YYPARSE_PARAM_ARG YYPARSE_PARAM -# define YYPARSE_PARAM_DECL void *YYPARSE_PARAM; -# endif -#else /* !YYPARSE_PARAM */ -# define YYPARSE_PARAM_ARG -# define YYPARSE_PARAM_DECL -#endif /* !YYPARSE_PARAM */ - -/* Prevent warning if -Wstrict-prototypes. */ -#ifdef __GNUC__ -# ifdef YYPARSE_PARAM -int yyparse (void *); -# else -int yyparse (void); -# endif -#endif - -/* YY_DECL_VARIABLES -- depending whether we use a pure parser, - variables are global, or local to YYPARSE. */ - -#define YY_DECL_NON_LSP_VARIABLES \ -/* The lookahead symbol. */ \ -int yychar; \ - \ -/* The semantic value of the lookahead symbol. */ \ -YYSTYPE yylval; \ - \ -/* Number of parse errors so far. */ \ -int yynerrs; - -#if YYLSP_NEEDED -# define YY_DECL_VARIABLES \ -YY_DECL_NON_LSP_VARIABLES \ - \ -/* Location data for the lookahead symbol. */ \ -YYLTYPE yylloc; -#else -# define YY_DECL_VARIABLES \ -YY_DECL_NON_LSP_VARIABLES -#endif - - -/* If nonreentrant, generate the variables here. */ - -#if !YYPURE -YY_DECL_VARIABLES -#endif /* !YYPURE */ - -int -yyparse (YYPARSE_PARAM_ARG) - YYPARSE_PARAM_DECL -{ - /* If reentrant, generate the variables here. */ -#if YYPURE - YY_DECL_VARIABLES -#endif /* !YYPURE */ - - register int yystate; - register int yyn; - int yyresult; - /* Number of tokens to shift before error messages enabled. */ - int yyerrstatus; - /* Lookahead token as an internal (translated) token number. */ - int yychar1 = 0; - - /* Three stacks and their tools: - `yyss': related to states, - `yyvs': related to semantic values, - `yyls': related to locations. - - Refer to the stacks thru separate pointers, to allow yyoverflow - to reallocate them elsewhere. */ - - /* The state stack. */ - short yyssa[YYINITDEPTH]; - short *yyss = yyssa; - register short *yyssp; - - /* The semantic value stack. */ - YYSTYPE yyvsa[YYINITDEPTH]; - YYSTYPE *yyvs = yyvsa; - register YYSTYPE *yyvsp; - -#if YYLSP_NEEDED - /* The location stack. */ - YYLTYPE yylsa[YYINITDEPTH]; - YYLTYPE *yyls = yylsa; - YYLTYPE *yylsp; -#endif - -#if YYLSP_NEEDED -# define YYPOPSTACK (yyvsp--, yyssp--, yylsp--) -#else -# define YYPOPSTACK (yyvsp--, yyssp--) -#endif - - YYSIZE_T yystacksize = YYINITDEPTH; - - - /* The variables used to return semantic value and location from the - action routines. */ - YYSTYPE yyval; -#if YYLSP_NEEDED - YYLTYPE yyloc; -#endif - - /* When reducing, the number of symbols on the RHS of the reduced - rule. */ - int yylen; - - YYDPRINTF ((stderr, "Starting parse\n")); - - yystate = 0; - yyerrstatus = 0; - yynerrs = 0; - yychar = YYEMPTY; /* Cause a token to be read. */ - - /* Initialize stack pointers. - Waste one element of value and location stack - so that they stay on the same level as the state stack. - The wasted elements are never initialized. */ - - yyssp = yyss; - yyvsp = yyvs; -#if YYLSP_NEEDED - yylsp = yyls; -#endif - goto yysetstate; - -/*------------------------------------------------------------. -| yynewstate -- Push a new state, which is found in yystate. | -`------------------------------------------------------------*/ - yynewstate: - /* In all cases, when you get here, the value and location stacks - have just been pushed. so pushing a state here evens the stacks. - */ - yyssp++; - - yysetstate: - *yyssp = yystate; - - if (yyssp >= yyss + yystacksize - 1) - { - /* Get the current used size of the three stacks, in elements. */ - YYSIZE_T yysize = yyssp - yyss + 1; - -#ifdef yyoverflow - { - /* Give user a chance to reallocate the stack. Use copies of - these so that the &'s don't force the real ones into - memory. */ - YYSTYPE *yyvs1 = yyvs; - short *yyss1 = yyss; - - /* Each stack pointer address is followed by the size of the - data in use in that stack, in bytes. */ -# if YYLSP_NEEDED - YYLTYPE *yyls1 = yyls; - /* This used to be a conditional around just the two extra args, - but that might be undefined if yyoverflow is a macro. */ - yyoverflow ("parser stack overflow", - &yyss1, yysize * sizeof (*yyssp), - &yyvs1, yysize * sizeof (*yyvsp), - &yyls1, yysize * sizeof (*yylsp), - &yystacksize); - yyls = yyls1; -# else - yyoverflow ("parser stack overflow", - &yyss1, yysize * sizeof (*yyssp), - &yyvs1, yysize * sizeof (*yyvsp), - &yystacksize); -# endif - yyss = yyss1; - yyvs = yyvs1; - } -#else /* no yyoverflow */ - /* Extend the stack our own way. */ - if (yystacksize >= YYMAXDEPTH) - goto yyoverflowlab; - yystacksize *= 2; - if (yystacksize > YYMAXDEPTH) - yystacksize = YYMAXDEPTH; - - { - short *yyss1 = yyss; - union yyalloc *yyptr = - (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); - if (! yyptr) - goto yyoverflowlab; - YYSTACK_RELOCATE (short, yyss); - YYSTACK_RELOCATE (YYSTYPE, yyvs); -# if YYLSP_NEEDED - YYSTACK_RELOCATE (YYLTYPE, yyls); -# endif -# undef YYSTACK_RELOCATE - if (yyss1 != yyssa) - YYSTACK_FREE (yyss1); - } -#endif /* no yyoverflow */ - - yyssp = yyss + yysize - 1; - yyvsp = yyvs + yysize - 1; -#if YYLSP_NEEDED - yylsp = yyls + yysize - 1; -#endif - - YYDPRINTF ((stderr, "Stack size increased to %lu\n", - (unsigned long int) yystacksize)); - - if (yyssp >= yyss + yystacksize - 1) - YYABORT; - } - - YYDPRINTF ((stderr, "Entering state %d\n", yystate)); - - goto yybackup; - - -/*-----------. -| yybackup. | -`-----------*/ -yybackup: - -/* Do appropriate processing given the current state. */ -/* Read a lookahead token if we need one and don't already have one. */ -/* yyresume: */ - - /* First try to decide what to do without reference to lookahead token. */ - - yyn = yypact[yystate]; - if (yyn == YYFLAG) - goto yydefault; - - /* Not known => get a lookahead token if don't already have one. */ - - /* yychar is either YYEMPTY or YYEOF - or a valid token in external form. */ - - if (yychar == YYEMPTY) - { - YYDPRINTF ((stderr, "Reading a token: ")); - yychar = YYLEX; - } - - /* Convert token to internal form (in yychar1) for indexing tables with */ - - if (yychar <= 0) /* This means end of input. */ - { - yychar1 = 0; - yychar = YYEOF; /* Don't call YYLEX any more */ - - YYDPRINTF ((stderr, "Now at end of input.\n")); - } - else - { - yychar1 = YYTRANSLATE (yychar); - -#if YYDEBUG - /* We have to keep this `#if YYDEBUG', since we use variables - which are defined only if `YYDEBUG' is set. */ - if (yydebug) - { - YYFPRINTF (stderr, "Next token is %d (%s", - yychar, yytname[yychar1]); - /* Give the individual parser a way to print the precise - meaning of a token, for further debugging info. */ -# ifdef YYPRINT - YYPRINT (stderr, yychar, yylval); -# endif - YYFPRINTF (stderr, ")\n"); - } -#endif - } - - yyn += yychar1; - if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1) - goto yydefault; - - yyn = yytable[yyn]; - - /* yyn is what to do for this token type in this state. - Negative => reduce, -yyn is rule number. - Positive => shift, yyn is new state. - New state is final state => don't bother to shift, - just return success. - 0, or most negative number => error. */ - - if (yyn < 0) - { - if (yyn == YYFLAG) - goto yyerrlab; - yyn = -yyn; - goto yyreduce; - } - else if (yyn == 0) - goto yyerrlab; - - if (yyn == YYFINAL) - YYACCEPT; - - /* Shift the lookahead token. */ - YYDPRINTF ((stderr, "Shifting token %d (%s), ", - yychar, yytname[yychar1])); - - /* Discard the token being shifted unless it is eof. */ - if (yychar != YYEOF) - yychar = YYEMPTY; - - *++yyvsp = yylval; -#if YYLSP_NEEDED - *++yylsp = yylloc; -#endif - - /* Count tokens shifted since error; after three, turn off error - status. */ - if (yyerrstatus) - yyerrstatus--; - - yystate = yyn; - goto yynewstate; - - -/*-----------------------------------------------------------. -| yydefault -- do the default action for the current state. | -`-----------------------------------------------------------*/ -yydefault: - yyn = yydefact[yystate]; - if (yyn == 0) - goto yyerrlab; - goto yyreduce; - - -/*-----------------------------. -| yyreduce -- Do a reduction. | -`-----------------------------*/ -yyreduce: - /* yyn is the number of a rule to reduce with. */ - yylen = yyr2[yyn]; - - /* If YYLEN is nonzero, implement the default value of the action: - `$$ = $1'. - - Otherwise, the following line sets YYVAL to the semantic value of - the lookahead token. This behavior is undocumented and Bison - users should not rely upon it. Assigning to YYVAL - unconditionally makes the parser a bit smaller, and it avoids a - GCC warning that YYVAL may be used uninitialized. */ - yyval = yyvsp[1-yylen]; - -#if YYLSP_NEEDED - /* Similarly for the default location. Let the user run additional - commands if for instance locations are ranges. */ - yyloc = yylsp[1-yylen]; - YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen); -#endif - -#if YYDEBUG - /* We have to keep this `#if YYDEBUG', since we use variables which - are defined only if `YYDEBUG' is set. */ - if (yydebug) - { - int yyi; - - YYFPRINTF (stderr, "Reducing via rule %d (line %d), ", - yyn, yyrline[yyn]); - - /* Print the symbols being reduced, and their result. */ - for (yyi = yyprhs[yyn]; yyrhs[yyi] > 0; yyi++) - YYFPRINTF (stderr, "%s ", yytname[yyrhs[yyi]]); - YYFPRINTF (stderr, " -> %s\n", yytname[yyr1[yyn]]); - } -#endif - - switch (yyn) { - -case 5: -#line 174 "calc.y" -{ sp = stack[0]; yyerrok; } - break; -case 7: -#line 178 "calc.y" -{ - mpz_out_str (stdout, obase, sp); putchar ('\n'); - sp--; - CHECK_EMPTY (); - } - break; -case 8: -#line 183 "calc.y" -{ - CHECK_VARIABLE (yyvsp[-2].var); - mpz_swap (variable[yyvsp[-2].var], sp); - sp--; - CHECK_EMPTY (); - } - break; -case 9: -#line 189 "calc.y" -{ calc_help (); } - break; -case 10: -#line 190 "calc.y" -{ ibase = 16; obase = -16; } - break; -case 11: -#line 191 "calc.y" -{ ibase = 0; obase = 10; } - break; -case 12: -#line 192 "calc.y" -{ exit (0); } - break; -case 14: -#line 199 "calc.y" -{ sp--; mpz_add (sp, sp, sp+1); } - break; -case 15: -#line 200 "calc.y" -{ sp--; mpz_sub (sp, sp, sp+1); } - break; -case 16: -#line 201 "calc.y" -{ sp--; mpz_mul (sp, sp, sp+1); } - break; -case 17: -#line 202 "calc.y" -{ sp--; mpz_fdiv_q (sp, sp, sp+1); } - break; -case 18: -#line 203 "calc.y" -{ sp--; mpz_fdiv_r (sp, sp, sp+1); } - break; -case 19: -#line 204 "calc.y" -{ CHECK_UI ("Exponent", sp); - sp--; mpz_pow_ui (sp, sp, mpz_get_ui (sp+1)); } - break; -case 20: -#line 206 "calc.y" -{ CHECK_UI ("Shift count", sp); - sp--; mpz_mul_2exp (sp, sp, mpz_get_ui (sp+1)); } - break; -case 21: -#line 208 "calc.y" -{ CHECK_UI ("Shift count", sp); - sp--; mpz_fdiv_q_2exp (sp, sp, mpz_get_ui (sp+1)); } - break; -case 22: -#line 210 "calc.y" -{ CHECK_UI ("Factorial", sp); - mpz_fac_ui (sp, mpz_get_ui (sp)); } - break; -case 23: -#line 212 "calc.y" -{ mpz_neg (sp, sp); } - break; -case 24: -#line 214 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) < 0); } - break; -case 25: -#line 215 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) <= 0); } - break; -case 26: -#line 216 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) == 0); } - break; -case 27: -#line 217 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) != 0); } - break; -case 28: -#line 218 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) >= 0); } - break; -case 29: -#line 219 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) > 0); } - break; -case 30: -#line 221 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_sgn (sp) && mpz_sgn (sp+1)); } - break; -case 31: -#line 222 "calc.y" -{ sp--; mpz_set_ui (sp, mpz_sgn (sp) || mpz_sgn (sp+1)); } - break; -case 32: -#line 224 "calc.y" -{ mpz_abs (sp, sp); } - break; -case 33: -#line 225 "calc.y" -{ sp--; CHECK_UI ("Binomial base", sp+1); - mpz_bin_ui (sp, sp, mpz_get_ui (sp+1)); } - break; -case 34: -#line 227 "calc.y" -{ CHECK_UI ("Fibonacci", sp); - mpz_fib_ui (sp, mpz_get_ui (sp)); } - break; -case 36: -#line 230 "calc.y" -{ sp--; mpz_set_si (sp, - mpz_kronecker (sp, sp+1)); } - break; -case 38: -#line 233 "calc.y" -{ CHECK_UI ("Lucas number", sp); - mpz_lucnum_ui (sp, mpz_get_ui (sp)); } - break; -case 39: -#line 235 "calc.y" -{ mpz_nextprime (sp, sp); } - break; -case 40: -#line 236 "calc.y" -{ sp -= 2; mpz_powm (sp, sp, sp+1, sp+2); } - break; -case 41: -#line 237 "calc.y" -{ sp--; CHECK_UI ("Nth-root", sp+1); - mpz_root (sp, sp, mpz_get_ui (sp+1)); } - break; -case 42: -#line 239 "calc.y" -{ mpz_sqrt (sp, sp); } - break; -case 43: -#line 241 "calc.y" -{ - sp++; - CHECK_OVERFLOW (); - CHECK_VARIABLE (yyvsp[0].var); - mpz_set (sp, variable[yyvsp[0].var]); - } - break; -case 44: -#line 247 "calc.y" -{ - sp++; - CHECK_OVERFLOW (); - if (mpz_set_str (sp, yyvsp[0].str, ibase) != 0) - { - fprintf (stderr, "Invalid number: %s\n", yyvsp[0].str); - YYERROR; - } - } - break; -case 46: -#line 259 "calc.y" -{ sp--; mpz_gcd (sp, sp, sp+1); } - break; -case 48: -#line 263 "calc.y" -{ sp--; mpz_lcm (sp, sp, sp+1); } - break; -} - -#line 705 "/usr/share/bison/bison.simple" - - - yyvsp -= yylen; - yyssp -= yylen; -#if YYLSP_NEEDED - yylsp -= yylen; -#endif - -#if YYDEBUG - if (yydebug) - { - short *yyssp1 = yyss - 1; - YYFPRINTF (stderr, "state stack now"); - while (yyssp1 != yyssp) - YYFPRINTF (stderr, " %d", *++yyssp1); - YYFPRINTF (stderr, "\n"); - } -#endif - - *++yyvsp = yyval; -#if YYLSP_NEEDED - *++yylsp = yyloc; -#endif - - /* Now `shift' the result of the reduction. Determine what state - that goes to, based on the state we popped back to and the rule - number reduced by. */ - - yyn = yyr1[yyn]; - - yystate = yypgoto[yyn - YYNTBASE] + *yyssp; - if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp) - yystate = yytable[yystate]; - else - yystate = yydefgoto[yyn - YYNTBASE]; - - goto yynewstate; - - -/*------------------------------------. -| yyerrlab -- here on detecting error | -`------------------------------------*/ -yyerrlab: - /* If not already recovering from an error, report this error. */ - if (!yyerrstatus) - { - ++yynerrs; - -#ifdef YYERROR_VERBOSE - yyn = yypact[yystate]; - - if (yyn > YYFLAG && yyn < YYLAST) - { - YYSIZE_T yysize = 0; - char *yymsg; - int yyx, yycount; - - yycount = 0; - /* Start YYX at -YYN if negative to avoid negative indexes in - YYCHECK. */ - for (yyx = yyn < 0 ? -yyn : 0; - yyx < (int) (sizeof (yytname) / sizeof (char *)); yyx++) - if (yycheck[yyx + yyn] == yyx) - yysize += yystrlen (yytname[yyx]) + 15, yycount++; - yysize += yystrlen ("parse error, unexpected ") + 1; - yysize += yystrlen (yytname[YYTRANSLATE (yychar)]); - yymsg = (char *) YYSTACK_ALLOC (yysize); - if (yymsg != 0) - { - char *yyp = yystpcpy (yymsg, "parse error, unexpected "); - yyp = yystpcpy (yyp, yytname[YYTRANSLATE (yychar)]); - - if (yycount < 5) - { - yycount = 0; - for (yyx = yyn < 0 ? -yyn : 0; - yyx < (int) (sizeof (yytname) / sizeof (char *)); - yyx++) - if (yycheck[yyx + yyn] == yyx) - { - const char *yyq = ! yycount ? ", expecting " : " or "; - yyp = yystpcpy (yyp, yyq); - yyp = yystpcpy (yyp, yytname[yyx]); - yycount++; - } - } - yyerror (yymsg); - YYSTACK_FREE (yymsg); - } - else - yyerror ("parse error; also virtual memory exhausted"); - } - else -#endif /* defined (YYERROR_VERBOSE) */ - yyerror ("parse error"); - } - goto yyerrlab1; - - -/*--------------------------------------------------. -| yyerrlab1 -- error raised explicitly by an action | -`--------------------------------------------------*/ -yyerrlab1: - if (yyerrstatus == 3) - { - /* If just tried and failed to reuse lookahead token after an - error, discard it. */ - - /* return failure if at end of input */ - if (yychar == YYEOF) - YYABORT; - YYDPRINTF ((stderr, "Discarding token %d (%s).\n", - yychar, yytname[yychar1])); - yychar = YYEMPTY; - } - - /* Else will try to reuse lookahead token after shifting the error - token. */ - - yyerrstatus = 3; /* Each real token shifted decrements this */ - - goto yyerrhandle; - - -/*-------------------------------------------------------------------. -| yyerrdefault -- current state does not do anything special for the | -| error token. | -`-------------------------------------------------------------------*/ -yyerrdefault: -#if 0 - /* This is wrong; only states that explicitly want error tokens - should shift them. */ - - /* If its default is to accept any token, ok. Otherwise pop it. */ - yyn = yydefact[yystate]; - if (yyn) - goto yydefault; -#endif - - -/*---------------------------------------------------------------. -| yyerrpop -- pop the current state because it cannot handle the | -| error token | -`---------------------------------------------------------------*/ -yyerrpop: - if (yyssp == yyss) - YYABORT; - yyvsp--; - yystate = *--yyssp; -#if YYLSP_NEEDED - yylsp--; -#endif - -#if YYDEBUG - if (yydebug) - { - short *yyssp1 = yyss - 1; - YYFPRINTF (stderr, "Error: state stack now"); - while (yyssp1 != yyssp) - YYFPRINTF (stderr, " %d", *++yyssp1); - YYFPRINTF (stderr, "\n"); - } -#endif - -/*--------------. -| yyerrhandle. | -`--------------*/ -yyerrhandle: - yyn = yypact[yystate]; - if (yyn == YYFLAG) - goto yyerrdefault; - - yyn += YYTERROR; - if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR) - goto yyerrdefault; - - yyn = yytable[yyn]; - if (yyn < 0) - { - if (yyn == YYFLAG) - goto yyerrpop; - yyn = -yyn; - goto yyreduce; - } - else if (yyn == 0) - goto yyerrpop; - - if (yyn == YYFINAL) - YYACCEPT; - - YYDPRINTF ((stderr, "Shifting error token, ")); - - *++yyvsp = yylval; -#if YYLSP_NEEDED - *++yylsp = yylloc; -#endif - - yystate = yyn; - goto yynewstate; - - -/*-------------------------------------. -| yyacceptlab -- YYACCEPT comes here. | -`-------------------------------------*/ -yyacceptlab: - yyresult = 0; - goto yyreturn; - -/*-----------------------------------. -| yyabortlab -- YYABORT comes here. | -`-----------------------------------*/ -yyabortlab: - yyresult = 1; - goto yyreturn; - -/*---------------------------------------------. -| yyoverflowab -- parser overflow comes here. | -`---------------------------------------------*/ -yyoverflowlab: - yyerror ("parser stack overflow"); - yyresult = 2; - /* Fall through. */ - -yyreturn: -#ifndef yyoverflow - if (yyss != yyssa) - YYSTACK_FREE (yyss); -#endif - return yyresult; -} -#line 265 "calc.y" - - -yyerror (char *s) -{ - fprintf (stderr, "%s\n", s); -} - -int calc_option_readline = -1; - -int -main (int argc, char *argv[]) -{ - int i; - - for (i = 1; i < argc; i++) - { - if (strcmp (argv[i], "--readline") == 0) - calc_option_readline = 1; - else if (strcmp (argv[i], "--noreadline") == 0) - calc_option_readline = 0; - else if (strcmp (argv[i], "--help") == 0) - { - printf ("Usage: calc [--option]...\n"); - printf (" --readline use readline\n"); - printf (" --noreadline don't use readline\n"); - printf (" --help this message\n"); - printf ("Readline is only available when compiled in,\n"); - printf ("and in that case it's the default on a tty.\n"); - exit (0); - } - else - { - fprintf (stderr, "Unrecognised option: %s\n", argv[i]); - exit (1); - } - } - -#if WITH_READLINE - calc_init_readline (); -#else - if (calc_option_readline == 1) - { - fprintf (stderr, "Readline support not available\n"); - exit (1); - } -#endif - - for (i = 0; i < numberof (variable); i++) - mpz_init (variable[i]); - - for (i = 0; i < numberof (stack); i++) - mpz_init (stack[i]); - - return yyparse (); -} diff --git a/src/gmp/demos/calc/calc.h b/src/gmp/demos/calc/calc.h deleted file mode 100644 index 448cc425c..000000000 --- a/src/gmp/demos/calc/calc.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef BISON_CALC_H -# define BISON_CALC_H - -#ifndef YYSTYPE -typedef union { - char *str; - int var; -} yystype; -# define YYSTYPE yystype -#endif -# define EOS 257 -# define BAD 258 -# define HELP 259 -# define HEX 260 -# define DECIMAL 261 -# define QUIT 262 -# define ABS 263 -# define BIN 264 -# define FIB 265 -# define GCD 266 -# define KRON 267 -# define LCM 268 -# define LUCNUM 269 -# define NEXTPRIME 270 -# define POWM 271 -# define ROOT 272 -# define SQRT 273 -# define NUMBER 274 -# define VARIABLE 275 -# define LOR 276 -# define LAND 277 -# define EQ 278 -# define NE 279 -# define LE 280 -# define GE 281 -# define LSHIFT 282 -# define RSHIFT 283 -# define UMINUS 284 - - -extern YYSTYPE yylval; - -#endif /* not BISON_CALC_H */ diff --git a/src/gmp/demos/calc/calc.y b/src/gmp/demos/calc/calc.y deleted file mode 100644 index daf832f37..000000000 --- a/src/gmp/demos/calc/calc.y +++ /dev/null @@ -1,319 +0,0 @@ -%{ -/* A simple integer desk calculator using yacc and gmp. - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - -/* This is a simple program, meant only to show one way to use GMP for this - sort of thing. There's few features, and error checking is minimal. - Standard input is read, calc_help() below shows the inputs accepted. - - Expressions are evaluated as they're read. If user defined functions - were wanted it'd be necessary to build a parse tree like pexpr.c does, or - a list of operations for a stack based evaluator. That would also make - it possible to detect and optimize evaluations "mod m" like pexpr.c does. - - A stack is used for intermediate values in the expression evaluation, - separate from the yacc parser stack. This is simple, makes error - recovery easy, minimizes the junk around mpz calls in the rules, and - saves initializing or clearing "mpz_t"s during a calculation. A - disadvantage though is that variables must be copied to the stack to be - worked on. A more sophisticated calculator or language system might be - able to avoid that when executing a compiled or semi-compiled form. - - Avoiding repeated initializing and clearing of "mpz_t"s is important. In - this program the time spent parsing is obviously much greater than any - possible saving from this, but a proper calculator or language should - take some trouble over it. Don't be surprised if an init/clear takes 3 - or more times as long as a 10 limb addition, depending on the system (see - the mpz_init_realloc_clear example in tune/README). */ - - -#include -#include -#include -#include "gmp.h" -#define NO_CALC_H /* because it conflicts with normal calc.c stuff */ -#include "calc-common.h" - - -#define numberof(x) (sizeof (x) / sizeof ((x)[0])) - - -void -calc_help (void) -{ - printf ("Examples:\n"); - printf (" 2+3*4 expressions are evaluated\n"); - printf (" x=5^6 variables a to z can be set and used\n"); - printf ("Operators:\n"); - printf (" + - * arithmetic\n"); - printf (" / %% division and remainder (rounding towards negative infinity)\n"); - printf (" ^ exponentiation\n"); - printf (" ! factorial\n"); - printf (" << >> left and right shifts\n"); - printf (" <= >= > \\ comparisons, giving 1 if true, 0 if false\n"); - printf (" == != < /\n"); - printf (" && || logical and/or, giving 1 if true, 0 if false\n"); - printf ("Functions:\n"); - printf (" abs(n) absolute value\n"); - printf (" bin(n,m) binomial coefficient\n"); - printf (" fib(n) fibonacci number\n"); - printf (" gcd(a,b,..) greatest common divisor\n"); - printf (" kron(a,b) kronecker symbol\n"); - printf (" lcm(a,b,..) least common multiple\n"); - printf (" lucnum(n) lucas number\n"); - printf (" nextprime(n) next prime after n\n"); - printf (" powm(b,e,m) modulo powering, b^e%%m\n"); - printf (" root(n,r) r-th root\n"); - printf (" sqrt(n) square root\n"); - printf ("Other:\n"); - printf (" hex \\ set hex or decimal for input and output\n"); - printf (" decimal / (\"0x\" can be used for hex too)\n"); - printf (" quit exit program (EOF works too)\n"); - printf (" ; statements are separated with a ; or newline\n"); - printf (" \\ continue expressions with \\ before newline\n"); - printf (" # xxx comments are # though to newline\n"); - printf ("Hex numbers must be entered in upper case, to distinguish them from the\n"); - printf ("variables a to f (like in bc).\n"); -} - - -int ibase = 0; -int obase = 10; - - -/* The stack is a fixed size, which means there's a limit on the nesting - allowed in expressions. A more sophisticated program could let it grow - dynamically. */ - -mpz_t stack[100]; -mpz_ptr sp = stack[0]; - -#define CHECK_OVERFLOW() \ - if (sp >= stack[numberof(stack)]) \ - { \ - fprintf (stderr, \ - "Value stack overflow, too much nesting in expression\n"); \ - YYERROR; \ - } - -#define CHECK_EMPTY() \ - if (sp != stack[0]) \ - { \ - fprintf (stderr, "Oops, expected the value stack to be empty\n"); \ - sp = stack[0]; \ - } - - -mpz_t variable[26]; - -#define CHECK_VARIABLE(var) \ - if ((var) < 0 || (var) >= numberof (variable)) \ - { \ - fprintf (stderr, "Oops, bad variable somehow: %d\n", var); \ - YYERROR; \ - } - - -#define CHECK_UI(name,z) \ - if (! mpz_fits_ulong_p (z)) \ - { \ - fprintf (stderr, "%s too big\n", name); \ - YYERROR; \ - } - -%} - -%union { - char *str; - int var; -} - -%token EOS BAD -%token HELP HEX DECIMAL QUIT -%token ABS BIN FIB GCD KRON LCM LUCNUM NEXTPRIME POWM ROOT SQRT -%token NUMBER -%token VARIABLE - -/* operators, increasing precedence */ -%left LOR -%left LAND -%nonassoc '<' '>' EQ NE LE GE -%left LSHIFT RSHIFT -%left '+' '-' -%left '*' '/' '%' -%nonassoc UMINUS -%right '^' -%nonassoc '!' - -%% - -top: - statement - | statements statement; - -statements: - statement EOS - | statements statement EOS - | error EOS { sp = stack[0]; yyerrok; }; - -statement: - /* empty */ - | e { - mpz_out_str (stdout, obase, sp); putchar ('\n'); - sp--; - CHECK_EMPTY (); - } - | VARIABLE '=' e { - CHECK_VARIABLE ($1); - mpz_swap (variable[$1], sp); - sp--; - CHECK_EMPTY (); - } - | HELP { calc_help (); } - | HEX { ibase = 16; obase = -16; } - | DECIMAL { ibase = 0; obase = 10; } - | QUIT { exit (0); }; - -/* "e" leaves it's value on the top of the mpz stack. A rule like "e '+' e" - will have done a reduction for the first "e" first and the second "e" - second, so the code receives the values in that order on the stack. */ -e: - '(' e ')' /* value on stack */ - | e '+' e { sp--; mpz_add (sp, sp, sp+1); } - | e '-' e { sp--; mpz_sub (sp, sp, sp+1); } - | e '*' e { sp--; mpz_mul (sp, sp, sp+1); } - | e '/' e { sp--; mpz_fdiv_q (sp, sp, sp+1); } - | e '%' e { sp--; mpz_fdiv_r (sp, sp, sp+1); } - | e '^' e { CHECK_UI ("Exponent", sp); - sp--; mpz_pow_ui (sp, sp, mpz_get_ui (sp+1)); } - | e LSHIFT e { CHECK_UI ("Shift count", sp); - sp--; mpz_mul_2exp (sp, sp, mpz_get_ui (sp+1)); } - | e RSHIFT e { CHECK_UI ("Shift count", sp); - sp--; mpz_fdiv_q_2exp (sp, sp, mpz_get_ui (sp+1)); } - | e '!' { CHECK_UI ("Factorial", sp); - mpz_fac_ui (sp, mpz_get_ui (sp)); } - | '-' e %prec UMINUS { mpz_neg (sp, sp); } - - | e '<' e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) < 0); } - | e LE e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) <= 0); } - | e EQ e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) == 0); } - | e NE e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) != 0); } - | e GE e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) >= 0); } - | e '>' e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) > 0); } - - | e LAND e { sp--; mpz_set_ui (sp, mpz_sgn (sp) && mpz_sgn (sp+1)); } - | e LOR e { sp--; mpz_set_ui (sp, mpz_sgn (sp) || mpz_sgn (sp+1)); } - - | ABS '(' e ')' { mpz_abs (sp, sp); } - | BIN '(' e ',' e ')' { sp--; CHECK_UI ("Binomial base", sp+1); - mpz_bin_ui (sp, sp, mpz_get_ui (sp+1)); } - | FIB '(' e ')' { CHECK_UI ("Fibonacci", sp); - mpz_fib_ui (sp, mpz_get_ui (sp)); } - | GCD '(' gcdlist ')' /* value on stack */ - | KRON '(' e ',' e ')' { sp--; mpz_set_si (sp, - mpz_kronecker (sp, sp+1)); } - | LCM '(' lcmlist ')' /* value on stack */ - | LUCNUM '(' e ')' { CHECK_UI ("Lucas number", sp); - mpz_lucnum_ui (sp, mpz_get_ui (sp)); } - | NEXTPRIME '(' e ')' { mpz_nextprime (sp, sp); } - | POWM '(' e ',' e ',' e ')' { sp -= 2; mpz_powm (sp, sp, sp+1, sp+2); } - | ROOT '(' e ',' e ')' { sp--; CHECK_UI ("Nth-root", sp+1); - mpz_root (sp, sp, mpz_get_ui (sp+1)); } - | SQRT '(' e ')' { mpz_sqrt (sp, sp); } - - | VARIABLE { - sp++; - CHECK_OVERFLOW (); - CHECK_VARIABLE ($1); - mpz_set (sp, variable[$1]); - } - | NUMBER { - sp++; - CHECK_OVERFLOW (); - if (mpz_set_str (sp, $1, ibase) != 0) - { - fprintf (stderr, "Invalid number: %s\n", $1); - YYERROR; - } - }; - -gcdlist: - e /* value on stack */ - | gcdlist ',' e { sp--; mpz_gcd (sp, sp, sp+1); }; - -lcmlist: - e /* value on stack */ - | lcmlist ',' e { sp--; mpz_lcm (sp, sp, sp+1); }; - -%% - -yyerror (char *s) -{ - fprintf (stderr, "%s\n", s); -} - -int calc_option_readline = -1; - -int -main (int argc, char *argv[]) -{ - int i; - - for (i = 1; i < argc; i++) - { - if (strcmp (argv[i], "--readline") == 0) - calc_option_readline = 1; - else if (strcmp (argv[i], "--noreadline") == 0) - calc_option_readline = 0; - else if (strcmp (argv[i], "--help") == 0) - { - printf ("Usage: calc [--option]...\n"); - printf (" --readline use readline\n"); - printf (" --noreadline don't use readline\n"); - printf (" --help this message\n"); - printf ("Readline is only available when compiled in,\n"); - printf ("and in that case it's the default on a tty.\n"); - exit (0); - } - else - { - fprintf (stderr, "Unrecognised option: %s\n", argv[i]); - exit (1); - } - } - -#if WITH_READLINE - calc_init_readline (); -#else - if (calc_option_readline == 1) - { - fprintf (stderr, "Readline support not available\n"); - exit (1); - } -#endif - - for (i = 0; i < numberof (variable); i++) - mpz_init (variable[i]); - - for (i = 0; i < numberof (stack); i++) - mpz_init (stack[i]); - - return yyparse (); -} diff --git a/src/gmp/demos/calc/calclex.c b/src/gmp/demos/calc/calclex.c deleted file mode 100644 index 30775df29..000000000 --- a/src/gmp/demos/calc/calclex.c +++ /dev/null @@ -1,1704 +0,0 @@ -#line 2 "calclex.c" -/* A lexical scanner generated by flex */ - -/* Scanner skeleton version: - * $Header$ - */ - -#define FLEX_SCANNER -#define YY_FLEX_MAJOR_VERSION 2 -#define YY_FLEX_MINOR_VERSION 5 - -#include -#include - -/* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */ -#ifdef c_plusplus -#ifndef __cplusplus -#define __cplusplus -#endif -#endif - - -#ifdef __cplusplus - -#include -#ifndef _WIN32 -#include -#endif - -/* Use prototypes in function declarations. */ -#define YY_USE_PROTOS - -/* The "const" storage-class-modifier is valid. */ -#define YY_USE_CONST - -#else /* ! __cplusplus */ - -#if __STDC__ - -#define YY_USE_PROTOS -#define YY_USE_CONST - -#endif /* __STDC__ */ -#endif /* ! __cplusplus */ - -#ifdef __TURBOC__ - #pragma warn -rch - #pragma warn -use -#include -#include -#define YY_USE_CONST -#define YY_USE_PROTOS -#endif - -#ifdef YY_USE_CONST -#define yyconst const -#else -#define yyconst -#endif - - -#ifdef YY_USE_PROTOS -#define YY_PROTO(proto) proto -#else -#define YY_PROTO(proto) () -#endif - -/* Returned upon end-of-file. */ -#define YY_NULL 0 - -/* Promotes a possibly negative, possibly signed char to an unsigned - * integer for use as an array index. If the signed char is negative, - * we want to instead treat it as an 8-bit unsigned char, hence the - * double cast. - */ -#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) - -/* Enter a start condition. This macro really ought to take a parameter, - * but we do it the disgusting crufty way forced on us by the ()-less - * definition of BEGIN. - */ -#define BEGIN yy_start = 1 + 2 * - -/* Translate the current start state into a value that can be later handed - * to BEGIN to return to the state. The YYSTATE alias is for lex - * compatibility. - */ -#define YY_START ((yy_start - 1) / 2) -#define YYSTATE YY_START - -/* Action number for EOF rule of a given start state. */ -#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) - -/* Special action meaning "start processing a new file". */ -#define YY_NEW_FILE yyrestart( yyin ) - -#define YY_END_OF_BUFFER_CHAR 0 - -/* Size of default input buffer. */ -#define YY_BUF_SIZE 16384 - -typedef struct yy_buffer_state *YY_BUFFER_STATE; - -extern int yyleng; -extern FILE *yyin, *yyout; - -#define EOB_ACT_CONTINUE_SCAN 0 -#define EOB_ACT_END_OF_FILE 1 -#define EOB_ACT_LAST_MATCH 2 - -/* The funky do-while in the following #define is used to turn the definition - * int a single C statement (which needs a semi-colon terminator). This - * avoids problems with code like: - * - * if ( condition_holds ) - * yyless( 5 ); - * else - * do_something_else(); - * - * Prior to using the do-while the compiler would get upset at the - * "else" because it interpreted the "if" statement as being all - * done when it reached the ';' after the yyless() call. - */ - -/* Return all but the first 'n' matched characters back to the input stream. */ - -#define yyless(n) \ - do \ - { \ - /* Undo effects of setting up yytext. */ \ - *yy_cp = yy_hold_char; \ - YY_RESTORE_YY_MORE_OFFSET \ - yy_c_buf_p = yy_cp = yy_bp + n - YY_MORE_ADJ; \ - YY_DO_BEFORE_ACTION; /* set up yytext again */ \ - } \ - while ( 0 ) - -#define unput(c) yyunput( c, yytext_ptr ) - -/* The following is because we cannot portably get our hands on size_t - * (without autoconf's help, which isn't available because we want - * flex-generated scanners to compile on their own). - */ -typedef unsigned int yy_size_t; - - -struct yy_buffer_state - { - FILE *yy_input_file; - - char *yy_ch_buf; /* input buffer */ - char *yy_buf_pos; /* current position in input buffer */ - - /* Size of input buffer in bytes, not including room for EOB - * characters. - */ - yy_size_t yy_buf_size; - - /* Number of characters read into yy_ch_buf, not including EOB - * characters. - */ - int yy_n_chars; - - /* Whether we "own" the buffer - i.e., we know we created it, - * and can realloc() it to grow it, and should free() it to - * delete it. - */ - int yy_is_our_buffer; - - /* Whether this is an "interactive" input source; if so, and - * if we're using stdio for input, then we want to use getc() - * instead of fread(), to make sure we stop fetching input after - * each newline. - */ - int yy_is_interactive; - - /* Whether we're considered to be at the beginning of a line. - * If so, '^' rules will be active on the next match, otherwise - * not. - */ - int yy_at_bol; - - /* Whether to try to fill the input buffer when we reach the - * end of it. - */ - int yy_fill_buffer; - - int yy_buffer_status; -#define YY_BUFFER_NEW 0 -#define YY_BUFFER_NORMAL 1 - /* When an EOF's been seen but there's still some text to process - * then we mark the buffer as YY_EOF_PENDING, to indicate that we - * shouldn't try reading from the input source any more. We might - * still have a bunch of tokens to match, though, because of - * possible backing-up. - * - * When we actually see the EOF, we change the status to "new" - * (via yyrestart()), so that the user can continue scanning by - * just pointing yyin at a new input file. - */ -#define YY_BUFFER_EOF_PENDING 2 - }; - -static YY_BUFFER_STATE yy_current_buffer = 0; - -/* We provide macros for accessing buffer states in case in the - * future we want to put the buffer states in a more general - * "scanner state". - */ -#define YY_CURRENT_BUFFER yy_current_buffer - - -/* yy_hold_char holds the character lost when yytext is formed. */ -static char yy_hold_char; - -static int yy_n_chars; /* number of characters read into yy_ch_buf */ - - -int yyleng; - -/* Points to current character in buffer. */ -static char *yy_c_buf_p = (char *) 0; -static int yy_init = 1; /* whether we need to initialize */ -static int yy_start = 0; /* start state number */ - -/* Flag which is used to allow yywrap()'s to do buffer switches - * instead of setting up a fresh yyin. A bit of a hack ... - */ -static int yy_did_buffer_switch_on_eof; - -void yyrestart YY_PROTO(( FILE *input_file )); - -void yy_switch_to_buffer YY_PROTO(( YY_BUFFER_STATE new_buffer )); -void yy_load_buffer_state YY_PROTO(( void )); -YY_BUFFER_STATE yy_create_buffer YY_PROTO(( FILE *file, int size )); -void yy_delete_buffer YY_PROTO(( YY_BUFFER_STATE b )); -void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, FILE *file )); -void yy_flush_buffer YY_PROTO(( YY_BUFFER_STATE b )); -#define YY_FLUSH_BUFFER yy_flush_buffer( yy_current_buffer ) - -YY_BUFFER_STATE yy_scan_buffer YY_PROTO(( char *base, yy_size_t size )); -YY_BUFFER_STATE yy_scan_string YY_PROTO(( yyconst char *yy_str )); -YY_BUFFER_STATE yy_scan_bytes YY_PROTO(( yyconst char *bytes, int len )); - -static void *yy_flex_alloc YY_PROTO(( yy_size_t )); -static void *yy_flex_realloc YY_PROTO(( void *, yy_size_t )); -static void yy_flex_free YY_PROTO(( void * )); - -#define yy_new_buffer yy_create_buffer - -#define yy_set_interactive(is_interactive) \ - { \ - if ( ! yy_current_buffer ) \ - yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \ - yy_current_buffer->yy_is_interactive = is_interactive; \ - } - -#define yy_set_bol(at_bol) \ - { \ - if ( ! yy_current_buffer ) \ - yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \ - yy_current_buffer->yy_at_bol = at_bol; \ - } - -#define YY_AT_BOL() (yy_current_buffer->yy_at_bol) - -typedef unsigned char YY_CHAR; -FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; -typedef int yy_state_type; -extern char *yytext; -#define yytext_ptr yytext - -static yy_state_type yy_get_previous_state YY_PROTO(( void )); -static yy_state_type yy_try_NUL_trans YY_PROTO(( yy_state_type current_state )); -static int yy_get_next_buffer YY_PROTO(( void )); -static void yy_fatal_error YY_PROTO(( yyconst char msg[] )); - -/* Done after the current pattern has been matched and before the - * corresponding action - sets up yytext. - */ -#define YY_DO_BEFORE_ACTION \ - yytext_ptr = yy_bp; \ - yyleng = (int) (yy_cp - yy_bp); \ - yy_hold_char = *yy_cp; \ - *yy_cp = '\0'; \ - yy_c_buf_p = yy_cp; - -#define YY_NUM_RULES 19 -#define YY_END_OF_BUFFER 20 -static yyconst short int yy_accept[39] = - { 0, - 0, 0, 20, 18, 1, 2, 7, 6, 7, 18, - 16, 16, 2, 7, 7, 7, 16, 17, 18, 18, - 11, 6, 5, 6, 14, 16, 0, 12, 8, 10, - 9, 13, 16, 17, 3, 15, 4, 0 - } ; - -static yyconst int yy_ec[256] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, - 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 4, 1, 5, 1, 6, 7, 1, 6, - 6, 6, 6, 6, 6, 1, 6, 8, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 1, 10, 11, - 12, 13, 1, 1, 14, 14, 14, 14, 14, 14, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 16, 15, 15, - 1, 17, 1, 6, 1, 1, 15, 15, 15, 15, - - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, - 15, 15, 1, 18, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1 - } ; - -static yyconst int yy_meta[19] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, - 1, 1, 1, 2, 3, 2, 1, 1 - } ; - -static yyconst short int yy_base[43] = - { 0, - 0, 0, 39, 49, 49, 49, 26, 16, 49, 30, - 20, 19, 49, 9, 22, 10, 9, 0, 29, 13, - 49, 23, 49, 24, 49, 0, 0, 49, 49, 49, - 49, 49, 13, 0, 49, 49, 49, 49, 41, 28, - 43, 45 - } ; - -static yyconst short int yy_def[43] = - { 0, - 38, 1, 38, 38, 38, 38, 38, 39, 38, 38, - 40, 40, 38, 38, 38, 38, 41, 42, 38, 38, - 38, 39, 38, 39, 38, 12, 12, 38, 38, 38, - 38, 38, 41, 42, 38, 38, 38, 0, 38, 38, - 38, 38 - } ; - -static yyconst short int yy_nxt[68] = - { 0, - 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, - 14, 15, 16, 17, 18, 18, 19, 20, 23, 28, - 29, 31, 32, 34, 34, 23, 37, 34, 34, 26, - 36, 35, 24, 30, 38, 27, 25, 21, 38, 24, - 24, 22, 22, 22, 33, 33, 34, 34, 3, 38, - 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, - 38, 38, 38, 38, 38, 38, 38 - } ; - -static yyconst short int yy_chk[68] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 8, 14, - 14, 16, 16, 17, 17, 22, 24, 33, 33, 40, - 20, 19, 8, 15, 12, 11, 10, 7, 3, 22, - 24, 39, 39, 39, 41, 41, 42, 42, 38, 38, - 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, - 38, 38, 38, 38, 38, 38, 38 - } ; - -static yy_state_type yy_last_accepting_state; -static char *yy_last_accepting_cpos; - -/* The intent behind this definition is that it'll catch - * any uses of REJECT which flex missed. - */ -#define REJECT reject_used_but_not_detected -#define yymore() yymore_used_but_not_detected -#define YY_MORE_ADJ 0 -#define YY_RESTORE_YY_MORE_OFFSET -char *yytext; -#line 1 "calclex.l" -#define INITIAL 0 -/* Lexical analyzer for calc program. - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#line 21 "calclex.l" -#include -#include "calc-common.h" - - -#if WITH_READLINE -/* Let GNU flex use readline. See the calcread.c redefined input() for a - way that might work for a standard lex too. */ -#define YY_INPUT(buf,result,max_size) \ - result = calc_input (buf, max_size); -#endif - - -/* Non-zero when reading the second or subsequent line of an expression, - used to give a different prompt when using readline. */ -int calc_more_input = 0; - - -const struct calc_keywords_t calc_keywords[] = { - { "abs", ABS }, - { "bin", BIN }, - { "decimal", DECIMAL }, - { "fib", FIB }, - { "hex", HEX }, - { "help", HELP }, - { "gcd", GCD }, - { "kron", KRON }, - { "lcm", LCM }, - { "lucnum", LUCNUM }, - { "nextprime", NEXTPRIME }, - { "powm", POWM }, - { "quit", QUIT }, - { "root", ROOT }, - { "sqrt", SQRT }, - { NULL } -}; -#line 444 "calclex.c" - -/* Macros after this point can all be overridden by user definitions in - * section 1. - */ - -#ifndef YY_SKIP_YYWRAP -#ifdef __cplusplus -extern "C" int yywrap YY_PROTO(( void )); -#else -extern int yywrap YY_PROTO(( void )); -#endif -#endif - -#ifndef YY_NO_UNPUT -static void yyunput YY_PROTO(( int c, char *buf_ptr )); -#endif - -#ifndef yytext_ptr -static void yy_flex_strncpy YY_PROTO(( char *, yyconst char *, int )); -#endif - -#ifdef YY_NEED_STRLEN -static int yy_flex_strlen YY_PROTO(( yyconst char * )); -#endif - -#ifndef YY_NO_INPUT -#ifdef __cplusplus -static int yyinput YY_PROTO(( void )); -#else -static int input YY_PROTO(( void )); -#endif -#endif - -#if YY_STACK_USED -static int yy_start_stack_ptr = 0; -static int yy_start_stack_depth = 0; -static int *yy_start_stack = 0; -#ifndef YY_NO_PUSH_STATE -static void yy_push_state YY_PROTO(( int new_state )); -#endif -#ifndef YY_NO_POP_STATE -static void yy_pop_state YY_PROTO(( void )); -#endif -#ifndef YY_NO_TOP_STATE -static int yy_top_state YY_PROTO(( void )); -#endif - -#else -#define YY_NO_PUSH_STATE 1 -#define YY_NO_POP_STATE 1 -#define YY_NO_TOP_STATE 1 -#endif - -#ifdef YY_MALLOC_DECL -YY_MALLOC_DECL -#else -#if __STDC__ -#ifndef __cplusplus -#include -#endif -#else -/* Just try to get by without declaring the routines. This will fail - * miserably on non-ANSI systems for which sizeof(size_t) != sizeof(int) - * or sizeof(void*) != sizeof(int). - */ -#endif -#endif - -/* Amount of stuff to slurp up with each read. */ -#ifndef YY_READ_BUF_SIZE -#define YY_READ_BUF_SIZE 8192 -#endif - -/* Copy whatever the last rule matched to the standard output. */ - -#ifndef ECHO -/* This used to be an fputs(), but since the string might contain NUL's, - * we now use fwrite(). - */ -#define ECHO (void) fwrite( yytext, yyleng, 1, yyout ) -#endif - -/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, - * is returned in "result". - */ -#ifndef YY_INPUT -#define YY_INPUT(buf,result,max_size) \ - if ( yy_current_buffer->yy_is_interactive ) \ - { \ - int c = '*', n; \ - for ( n = 0; n < max_size && \ - (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ - buf[n] = (char) c; \ - if ( c == '\n' ) \ - buf[n++] = (char) c; \ - if ( c == EOF && ferror( yyin ) ) \ - YY_FATAL_ERROR( "input in flex scanner failed" ); \ - result = n; \ - } \ - else \ - { \ - errno=0; \ - while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ - { \ - if( errno != EINTR) \ - { \ - YY_FATAL_ERROR( "input in flex scanner failed" ); \ - break; \ - } \ - errno=0; \ - clearerr(yyin); \ - } \ - } -#endif - -/* No semi-colon after return; correct usage is to write "yyterminate();" - - * we don't want an extra ';' after the "return" because that will cause - * some compilers to complain about unreachable statements. - */ -#ifndef yyterminate -#define yyterminate() return YY_NULL -#endif - -/* Number of entries by which start-condition stack grows. */ -#ifndef YY_START_STACK_INCR -#define YY_START_STACK_INCR 25 -#endif - -/* Report a fatal error. */ -#ifndef YY_FATAL_ERROR -#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) -#endif - -/* Default declaration of generated scanner - a define so the user can - * easily add parameters. - */ -#ifndef YY_DECL -#define YY_DECL int yylex YY_PROTO(( void )) -#endif - -/* Code executed at the beginning of each rule, after yytext and yyleng - * have been set up. - */ -#ifndef YY_USER_ACTION -#define YY_USER_ACTION -#endif - -/* Code executed at the end of each rule. */ -#ifndef YY_BREAK -#define YY_BREAK break; -#endif - -#define YY_RULE_SETUP \ - YY_USER_ACTION - -YY_DECL - { - register yy_state_type yy_current_state; - register char *yy_cp, *yy_bp; - register int yy_act; - -#line 58 "calclex.l" - - -#line 609 "calclex.c" - - if ( yy_init ) - { - yy_init = 0; - -#ifdef YY_USER_INIT - YY_USER_INIT; -#endif - - if ( ! yy_start ) - yy_start = 1; /* first start state */ - - if ( ! yyin ) - yyin = stdin; - - if ( ! yyout ) - yyout = stdout; - - if ( ! yy_current_buffer ) - yy_current_buffer = - yy_create_buffer( yyin, YY_BUF_SIZE ); - - yy_load_buffer_state(); - } - - while ( 1 ) /* loops until end-of-file is reached */ - { - yy_cp = yy_c_buf_p; - - /* Support of yytext. */ - *yy_cp = yy_hold_char; - - /* yy_bp points to the position in yy_ch_buf of the start of - * the current run. - */ - yy_bp = yy_cp; - - yy_current_state = yy_start; -yy_match: - do - { - register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; - if ( yy_accept[yy_current_state] ) - { - yy_last_accepting_state = yy_current_state; - yy_last_accepting_cpos = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 39 ) - yy_c = yy_meta[(unsigned int) yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; - ++yy_cp; - } - while ( yy_base[yy_current_state] != 49 ); - -yy_find_action: - yy_act = yy_accept[yy_current_state]; - if ( yy_act == 0 ) - { /* have to back up */ - yy_cp = yy_last_accepting_cpos; - yy_current_state = yy_last_accepting_state; - yy_act = yy_accept[yy_current_state]; - } - - YY_DO_BEFORE_ACTION; - - -do_action: /* This label is used only to access EOF actions. */ - - - switch ( yy_act ) - { /* beginning of action switch */ - case 0: /* must back up */ - /* undo the effects of YY_DO_BEFORE_ACTION */ - *yy_cp = yy_hold_char; - yy_cp = yy_last_accepting_cpos; - yy_current_state = yy_last_accepting_state; - goto yy_find_action; - -case 1: -YY_RULE_SETUP -#line 60 "calclex.l" -{ /* white space is skipped */ } - YY_BREAK -case 2: -YY_RULE_SETUP -#line 62 "calclex.l" -{ /* semicolon or newline separates statements */ - calc_more_input = 0; - return EOS; } - YY_BREAK -case 3: -YY_RULE_SETUP -#line 65 "calclex.l" -{ /* escaped newlines are skipped */ } - YY_BREAK -case 4: -YY_RULE_SETUP -#line 68 "calclex.l" -{ - /* comment through to escaped newline is skipped */ } - YY_BREAK -case 5: -YY_RULE_SETUP -#line 70 "calclex.l" -{ /* comment through to newline is a separator */ - calc_more_input = 0; - return EOS; } - YY_BREAK -case 6: -YY_RULE_SETUP -#line 73 "calclex.l" -{ /* comment through to EOF skipped */ } - YY_BREAK -case 7: -YY_RULE_SETUP -#line 76 "calclex.l" -{ return yytext[0]; } - YY_BREAK -case 8: -YY_RULE_SETUP -#line 77 "calclex.l" -{ return LE; } - YY_BREAK -case 9: -YY_RULE_SETUP -#line 78 "calclex.l" -{ return GE; } - YY_BREAK -case 10: -YY_RULE_SETUP -#line 79 "calclex.l" -{ return EQ; } - YY_BREAK -case 11: -YY_RULE_SETUP -#line 80 "calclex.l" -{ return NE; } - YY_BREAK -case 12: -YY_RULE_SETUP -#line 81 "calclex.l" -{ return LSHIFT; } - YY_BREAK -case 13: -YY_RULE_SETUP -#line 82 "calclex.l" -{ return RSHIFT; } - YY_BREAK -case 14: -YY_RULE_SETUP -#line 83 "calclex.l" -{ return LAND; } - YY_BREAK -case 15: -YY_RULE_SETUP -#line 84 "calclex.l" -{ return LOR; } - YY_BREAK -case 16: -YY_RULE_SETUP -#line 86 "calclex.l" -{ - yylval.str = yytext; - return NUMBER; } - YY_BREAK -case 17: -YY_RULE_SETUP -#line 90 "calclex.l" -{ - int i; - - for (i = 0; calc_keywords[i].name != NULL; i++) - if (strcmp (yytext, calc_keywords[i].name) == 0) - return calc_keywords[i].value; - - if (yytext[0] >= 'a' && yytext[0] <= 'z' && yytext[1] == '\0') - { - yylval.var = yytext[0] - 'a'; - return VARIABLE; - } - - return BAD; -} - YY_BREAK -case 18: -YY_RULE_SETUP -#line 106 "calclex.l" -{ return BAD; } - YY_BREAK -case 19: -YY_RULE_SETUP -#line 108 "calclex.l" -ECHO; - YY_BREAK -#line 808 "calclex.c" -case YY_STATE_EOF(INITIAL): - yyterminate(); - - case YY_END_OF_BUFFER: - { - /* Amount of text matched not including the EOB char. */ - int yy_amount_of_matched_text = (int) (yy_cp - yytext_ptr) - 1; - - /* Undo the effects of YY_DO_BEFORE_ACTION. */ - *yy_cp = yy_hold_char; - YY_RESTORE_YY_MORE_OFFSET - - if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_NEW ) - { - /* We're scanning a new file or input source. It's - * possible that this happened because the user - * just pointed yyin at a new source and called - * yylex(). If so, then we have to assure - * consistency between yy_current_buffer and our - * globals. Here is the right place to do so, because - * this is the first action (other than possibly a - * back-up) that will match for the new input source. - */ - yy_n_chars = yy_current_buffer->yy_n_chars; - yy_current_buffer->yy_input_file = yyin; - yy_current_buffer->yy_buffer_status = YY_BUFFER_NORMAL; - } - - /* Note that here we test for yy_c_buf_p "<=" to the position - * of the first EOB in the buffer, since yy_c_buf_p will - * already have been incremented past the NUL character - * (since all states make transitions on EOB to the - * end-of-buffer state). Contrast this with the test - * in input(). - */ - if ( yy_c_buf_p <= &yy_current_buffer->yy_ch_buf[yy_n_chars] ) - { /* This was really a NUL. */ - yy_state_type yy_next_state; - - yy_c_buf_p = yytext_ptr + yy_amount_of_matched_text; - - yy_current_state = yy_get_previous_state(); - - /* Okay, we're now positioned to make the NUL - * transition. We couldn't have - * yy_get_previous_state() go ahead and do it - * for us because it doesn't know how to deal - * with the possibility of jamming (and we don't - * want to build jamming into it because then it - * will run more slowly). - */ - - yy_next_state = yy_try_NUL_trans( yy_current_state ); - - yy_bp = yytext_ptr + YY_MORE_ADJ; - - if ( yy_next_state ) - { - /* Consume the NUL. */ - yy_cp = ++yy_c_buf_p; - yy_current_state = yy_next_state; - goto yy_match; - } - - else - { - yy_cp = yy_c_buf_p; - goto yy_find_action; - } - } - - else switch ( yy_get_next_buffer() ) - { - case EOB_ACT_END_OF_FILE: - { - yy_did_buffer_switch_on_eof = 0; - - if ( yywrap() ) - { - /* Note: because we've taken care in - * yy_get_next_buffer() to have set up - * yytext, we can now set up - * yy_c_buf_p so that if some total - * hoser (like flex itself) wants to - * call the scanner after we return the - * YY_NULL, it'll still work - another - * YY_NULL will get returned. - */ - yy_c_buf_p = yytext_ptr + YY_MORE_ADJ; - - yy_act = YY_STATE_EOF(YY_START); - goto do_action; - } - - else - { - if ( ! yy_did_buffer_switch_on_eof ) - YY_NEW_FILE; - } - break; - } - - case EOB_ACT_CONTINUE_SCAN: - yy_c_buf_p = - yytext_ptr + yy_amount_of_matched_text; - - yy_current_state = yy_get_previous_state(); - - yy_cp = yy_c_buf_p; - yy_bp = yytext_ptr + YY_MORE_ADJ; - goto yy_match; - - case EOB_ACT_LAST_MATCH: - yy_c_buf_p = - &yy_current_buffer->yy_ch_buf[yy_n_chars]; - - yy_current_state = yy_get_previous_state(); - - yy_cp = yy_c_buf_p; - yy_bp = yytext_ptr + YY_MORE_ADJ; - goto yy_find_action; - } - break; - } - - default: - YY_FATAL_ERROR( - "fatal flex scanner internal error--no action found" ); - } /* end of action switch */ - } /* end of scanning one token */ - } /* end of yylex */ - - -/* yy_get_next_buffer - try to read in a new buffer - * - * Returns a code representing an action: - * EOB_ACT_LAST_MATCH - - * EOB_ACT_CONTINUE_SCAN - continue scanning from current position - * EOB_ACT_END_OF_FILE - end of file - */ - -static int yy_get_next_buffer() - { - register char *dest = yy_current_buffer->yy_ch_buf; - register char *source = yytext_ptr; - register int number_to_move, i; - int ret_val; - - if ( yy_c_buf_p > &yy_current_buffer->yy_ch_buf[yy_n_chars + 1] ) - YY_FATAL_ERROR( - "fatal flex scanner internal error--end of buffer missed" ); - - if ( yy_current_buffer->yy_fill_buffer == 0 ) - { /* Don't try to fill the buffer, so this is an EOF. */ - if ( yy_c_buf_p - yytext_ptr - YY_MORE_ADJ == 1 ) - { - /* We matched a single character, the EOB, so - * treat this as a final EOF. - */ - return EOB_ACT_END_OF_FILE; - } - - else - { - /* We matched some text prior to the EOB, first - * process it. - */ - return EOB_ACT_LAST_MATCH; - } - } - - /* Try to read more data. */ - - /* First move last chars to start of buffer. */ - number_to_move = (int) (yy_c_buf_p - yytext_ptr) - 1; - - for ( i = 0; i < number_to_move; ++i ) - *(dest++) = *(source++); - - if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_EOF_PENDING ) - /* don't do the read, it's not guaranteed to return an EOF, - * just force an EOF - */ - yy_current_buffer->yy_n_chars = yy_n_chars = 0; - - else - { - int num_to_read = - yy_current_buffer->yy_buf_size - number_to_move - 1; - - while ( num_to_read <= 0 ) - { /* Not enough room in the buffer - grow it. */ -#ifdef YY_USES_REJECT - YY_FATAL_ERROR( -"input buffer overflow, can't enlarge buffer because scanner uses REJECT" ); -#else - - /* just a shorter name for the current buffer */ - YY_BUFFER_STATE b = yy_current_buffer; - - int yy_c_buf_p_offset = - (int) (yy_c_buf_p - b->yy_ch_buf); - - if ( b->yy_is_our_buffer ) - { - int new_size = b->yy_buf_size * 2; - - if ( new_size <= 0 ) - b->yy_buf_size += b->yy_buf_size / 8; - else - b->yy_buf_size *= 2; - - b->yy_ch_buf = (char *) - /* Include room in for 2 EOB chars. */ - yy_flex_realloc( (void *) b->yy_ch_buf, - b->yy_buf_size + 2 ); - } - else - /* Can't grow it, we don't own it. */ - b->yy_ch_buf = 0; - - if ( ! b->yy_ch_buf ) - YY_FATAL_ERROR( - "fatal error - scanner input buffer overflow" ); - - yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset]; - - num_to_read = yy_current_buffer->yy_buf_size - - number_to_move - 1; -#endif - } - - if ( num_to_read > YY_READ_BUF_SIZE ) - num_to_read = YY_READ_BUF_SIZE; - - /* Read in more data. */ - YY_INPUT( (&yy_current_buffer->yy_ch_buf[number_to_move]), - yy_n_chars, num_to_read ); - - yy_current_buffer->yy_n_chars = yy_n_chars; - } - - if ( yy_n_chars == 0 ) - { - if ( number_to_move == YY_MORE_ADJ ) - { - ret_val = EOB_ACT_END_OF_FILE; - yyrestart( yyin ); - } - - else - { - ret_val = EOB_ACT_LAST_MATCH; - yy_current_buffer->yy_buffer_status = - YY_BUFFER_EOF_PENDING; - } - } - - else - ret_val = EOB_ACT_CONTINUE_SCAN; - - yy_n_chars += number_to_move; - yy_current_buffer->yy_ch_buf[yy_n_chars] = YY_END_OF_BUFFER_CHAR; - yy_current_buffer->yy_ch_buf[yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR; - - yytext_ptr = &yy_current_buffer->yy_ch_buf[0]; - - return ret_val; - } - - -/* yy_get_previous_state - get the state just before the EOB char was reached */ - -static yy_state_type yy_get_previous_state() - { - register yy_state_type yy_current_state; - register char *yy_cp; - - yy_current_state = yy_start; - - for ( yy_cp = yytext_ptr + YY_MORE_ADJ; yy_cp < yy_c_buf_p; ++yy_cp ) - { - register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); - if ( yy_accept[yy_current_state] ) - { - yy_last_accepting_state = yy_current_state; - yy_last_accepting_cpos = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 39 ) - yy_c = yy_meta[(unsigned int) yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; - } - - return yy_current_state; - } - - -/* yy_try_NUL_trans - try to make a transition on the NUL character - * - * synopsis - * next_state = yy_try_NUL_trans( current_state ); - */ - -#ifdef YY_USE_PROTOS -static yy_state_type yy_try_NUL_trans( yy_state_type yy_current_state ) -#else -static yy_state_type yy_try_NUL_trans( yy_current_state ) -yy_state_type yy_current_state; -#endif - { - register int yy_is_jam; - register char *yy_cp = yy_c_buf_p; - - register YY_CHAR yy_c = 1; - if ( yy_accept[yy_current_state] ) - { - yy_last_accepting_state = yy_current_state; - yy_last_accepting_cpos = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 39 ) - yy_c = yy_meta[(unsigned int) yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; - yy_is_jam = (yy_current_state == 38); - - return yy_is_jam ? 0 : yy_current_state; - } - - -#ifndef YY_NO_UNPUT -#ifdef YY_USE_PROTOS -static void yyunput( int c, register char *yy_bp ) -#else -static void yyunput( c, yy_bp ) -int c; -register char *yy_bp; -#endif - { - register char *yy_cp = yy_c_buf_p; - - /* undo effects of setting up yytext */ - *yy_cp = yy_hold_char; - - if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 ) - { /* need to shift things up to make room */ - /* +2 for EOB chars. */ - register int number_to_move = yy_n_chars + 2; - register char *dest = &yy_current_buffer->yy_ch_buf[ - yy_current_buffer->yy_buf_size + 2]; - register char *source = - &yy_current_buffer->yy_ch_buf[number_to_move]; - - while ( source > yy_current_buffer->yy_ch_buf ) - *--dest = *--source; - - yy_cp += (int) (dest - source); - yy_bp += (int) (dest - source); - yy_current_buffer->yy_n_chars = - yy_n_chars = yy_current_buffer->yy_buf_size; - - if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 ) - YY_FATAL_ERROR( "flex scanner push-back overflow" ); - } - - *--yy_cp = (char) c; - - - yytext_ptr = yy_bp; - yy_hold_char = *yy_cp; - yy_c_buf_p = yy_cp; - } -#endif /* ifndef YY_NO_UNPUT */ - - -#ifdef __cplusplus -static int yyinput() -#else -static int input() -#endif - { - int c; - - *yy_c_buf_p = yy_hold_char; - - if ( *yy_c_buf_p == YY_END_OF_BUFFER_CHAR ) - { - /* yy_c_buf_p now points to the character we want to return. - * If this occurs *before* the EOB characters, then it's a - * valid NUL; if not, then we've hit the end of the buffer. - */ - if ( yy_c_buf_p < &yy_current_buffer->yy_ch_buf[yy_n_chars] ) - /* This was really a NUL. */ - *yy_c_buf_p = '\0'; - - else - { /* need more input */ - int offset = yy_c_buf_p - yytext_ptr; - ++yy_c_buf_p; - - switch ( yy_get_next_buffer() ) - { - case EOB_ACT_LAST_MATCH: - /* This happens because yy_g_n_b() - * sees that we've accumulated a - * token and flags that we need to - * try matching the token before - * proceeding. But for input(), - * there's no matching to consider. - * So convert the EOB_ACT_LAST_MATCH - * to EOB_ACT_END_OF_FILE. - */ - - /* Reset buffer status. */ - yyrestart( yyin ); - - /* fall through */ - - case EOB_ACT_END_OF_FILE: - { - if ( yywrap() ) - return EOF; - - if ( ! yy_did_buffer_switch_on_eof ) - YY_NEW_FILE; -#ifdef __cplusplus - return yyinput(); -#else - return input(); -#endif - } - - case EOB_ACT_CONTINUE_SCAN: - yy_c_buf_p = yytext_ptr + offset; - break; - } - } - } - - c = *(unsigned char *) yy_c_buf_p; /* cast for 8-bit char's */ - *yy_c_buf_p = '\0'; /* preserve yytext */ - yy_hold_char = *++yy_c_buf_p; - - - return c; - } - - -#ifdef YY_USE_PROTOS -void yyrestart( FILE *input_file ) -#else -void yyrestart( input_file ) -FILE *input_file; -#endif - { - if ( ! yy_current_buffer ) - yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); - - yy_init_buffer( yy_current_buffer, input_file ); - yy_load_buffer_state(); - } - - -#ifdef YY_USE_PROTOS -void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ) -#else -void yy_switch_to_buffer( new_buffer ) -YY_BUFFER_STATE new_buffer; -#endif - { - if ( yy_current_buffer == new_buffer ) - return; - - if ( yy_current_buffer ) - { - /* Flush out information for old buffer. */ - *yy_c_buf_p = yy_hold_char; - yy_current_buffer->yy_buf_pos = yy_c_buf_p; - yy_current_buffer->yy_n_chars = yy_n_chars; - } - - yy_current_buffer = new_buffer; - yy_load_buffer_state(); - - /* We don't actually know whether we did this switch during - * EOF (yywrap()) processing, but the only time this flag - * is looked at is after yywrap() is called, so it's safe - * to go ahead and always set it. - */ - yy_did_buffer_switch_on_eof = 1; - } - - -#ifdef YY_USE_PROTOS -void yy_load_buffer_state( void ) -#else -void yy_load_buffer_state() -#endif - { - yy_n_chars = yy_current_buffer->yy_n_chars; - yytext_ptr = yy_c_buf_p = yy_current_buffer->yy_buf_pos; - yyin = yy_current_buffer->yy_input_file; - yy_hold_char = *yy_c_buf_p; - } - - -#ifdef YY_USE_PROTOS -YY_BUFFER_STATE yy_create_buffer( FILE *file, int size ) -#else -YY_BUFFER_STATE yy_create_buffer( file, size ) -FILE *file; -int size; -#endif - { - YY_BUFFER_STATE b; - - b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) ); - if ( ! b ) - YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); - - b->yy_buf_size = size; - - /* yy_ch_buf has to be 2 characters longer than the size given because - * we need to put in 2 end-of-buffer characters. - */ - b->yy_ch_buf = (char *) yy_flex_alloc( b->yy_buf_size + 2 ); - if ( ! b->yy_ch_buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); - - b->yy_is_our_buffer = 1; - - yy_init_buffer( b, file ); - - return b; - } - - -#ifdef YY_USE_PROTOS -void yy_delete_buffer( YY_BUFFER_STATE b ) -#else -void yy_delete_buffer( b ) -YY_BUFFER_STATE b; -#endif - { - if ( ! b ) - return; - - if ( b == yy_current_buffer ) - yy_current_buffer = (YY_BUFFER_STATE) 0; - - if ( b->yy_is_our_buffer ) - yy_flex_free( (void *) b->yy_ch_buf ); - - yy_flex_free( (void *) b ); - } - - -#ifndef _WIN32 -#include -#else -#ifndef YY_ALWAYS_INTERACTIVE -#ifndef YY_NEVER_INTERACTIVE -extern int isatty YY_PROTO(( int )); -#endif -#endif -#endif - -#ifdef YY_USE_PROTOS -void yy_init_buffer( YY_BUFFER_STATE b, FILE *file ) -#else -void yy_init_buffer( b, file ) -YY_BUFFER_STATE b; -FILE *file; -#endif - - - { - yy_flush_buffer( b ); - - b->yy_input_file = file; - b->yy_fill_buffer = 1; - -#if YY_ALWAYS_INTERACTIVE - b->yy_is_interactive = 1; -#else -#if YY_NEVER_INTERACTIVE - b->yy_is_interactive = 0; -#else - b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; -#endif -#endif - } - - -#ifdef YY_USE_PROTOS -void yy_flush_buffer( YY_BUFFER_STATE b ) -#else -void yy_flush_buffer( b ) -YY_BUFFER_STATE b; -#endif - - { - if ( ! b ) - return; - - b->yy_n_chars = 0; - - /* We always need two end-of-buffer characters. The first causes - * a transition to the end-of-buffer state. The second causes - * a jam in that state. - */ - b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; - b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; - - b->yy_buf_pos = &b->yy_ch_buf[0]; - - b->yy_at_bol = 1; - b->yy_buffer_status = YY_BUFFER_NEW; - - if ( b == yy_current_buffer ) - yy_load_buffer_state(); - } - - -#ifndef YY_NO_SCAN_BUFFER -#ifdef YY_USE_PROTOS -YY_BUFFER_STATE yy_scan_buffer( char *base, yy_size_t size ) -#else -YY_BUFFER_STATE yy_scan_buffer( base, size ) -char *base; -yy_size_t size; -#endif - { - YY_BUFFER_STATE b; - - if ( size < 2 || - base[size-2] != YY_END_OF_BUFFER_CHAR || - base[size-1] != YY_END_OF_BUFFER_CHAR ) - /* They forgot to leave room for the EOB's. */ - return 0; - - b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) ); - if ( ! b ) - YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); - - b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ - b->yy_buf_pos = b->yy_ch_buf = base; - b->yy_is_our_buffer = 0; - b->yy_input_file = 0; - b->yy_n_chars = b->yy_buf_size; - b->yy_is_interactive = 0; - b->yy_at_bol = 1; - b->yy_fill_buffer = 0; - b->yy_buffer_status = YY_BUFFER_NEW; - - yy_switch_to_buffer( b ); - - return b; - } -#endif - - -#ifndef YY_NO_SCAN_STRING -#ifdef YY_USE_PROTOS -YY_BUFFER_STATE yy_scan_string( yyconst char *yy_str ) -#else -YY_BUFFER_STATE yy_scan_string( yy_str ) -yyconst char *yy_str; -#endif - { - int len; - for ( len = 0; yy_str[len]; ++len ) - ; - - return yy_scan_bytes( yy_str, len ); - } -#endif - - -#ifndef YY_NO_SCAN_BYTES -#ifdef YY_USE_PROTOS -YY_BUFFER_STATE yy_scan_bytes( yyconst char *bytes, int len ) -#else -YY_BUFFER_STATE yy_scan_bytes( bytes, len ) -yyconst char *bytes; -int len; -#endif - { - YY_BUFFER_STATE b; - char *buf; - yy_size_t n; - int i; - - /* Get memory for full buffer, including space for trailing EOB's. */ - n = len + 2; - buf = (char *) yy_flex_alloc( n ); - if ( ! buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); - - for ( i = 0; i < len; ++i ) - buf[i] = bytes[i]; - - buf[len] = buf[len+1] = YY_END_OF_BUFFER_CHAR; - - b = yy_scan_buffer( buf, n ); - if ( ! b ) - YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); - - /* It's okay to grow etc. this buffer, and we should throw it - * away when we're done. - */ - b->yy_is_our_buffer = 1; - - return b; - } -#endif - - -#ifndef YY_NO_PUSH_STATE -#ifdef YY_USE_PROTOS -static void yy_push_state( int new_state ) -#else -static void yy_push_state( new_state ) -int new_state; -#endif - { - if ( yy_start_stack_ptr >= yy_start_stack_depth ) - { - yy_size_t new_size; - - yy_start_stack_depth += YY_START_STACK_INCR; - new_size = yy_start_stack_depth * sizeof( int ); - - if ( ! yy_start_stack ) - yy_start_stack = (int *) yy_flex_alloc( new_size ); - - else - yy_start_stack = (int *) yy_flex_realloc( - (void *) yy_start_stack, new_size ); - - if ( ! yy_start_stack ) - YY_FATAL_ERROR( - "out of memory expanding start-condition stack" ); - } - - yy_start_stack[yy_start_stack_ptr++] = YY_START; - - BEGIN(new_state); - } -#endif - - -#ifndef YY_NO_POP_STATE -static void yy_pop_state() - { - if ( --yy_start_stack_ptr < 0 ) - YY_FATAL_ERROR( "start-condition stack underflow" ); - - BEGIN(yy_start_stack[yy_start_stack_ptr]); - } -#endif - - -#ifndef YY_NO_TOP_STATE -static int yy_top_state() - { - return yy_start_stack[yy_start_stack_ptr - 1]; - } -#endif - -#ifndef YY_EXIT_FAILURE -#define YY_EXIT_FAILURE 2 -#endif - -#ifdef YY_USE_PROTOS -static void yy_fatal_error( yyconst char msg[] ) -#else -static void yy_fatal_error( msg ) -char msg[]; -#endif - { - (void) fprintf( stderr, "%s\n", msg ); - exit( YY_EXIT_FAILURE ); - } - - - -/* Redefine yyless() so it works in section 3 code. */ - -#undef yyless -#define yyless(n) \ - do \ - { \ - /* Undo effects of setting up yytext. */ \ - yytext[yyleng] = yy_hold_char; \ - yy_c_buf_p = yytext + n; \ - yy_hold_char = *yy_c_buf_p; \ - *yy_c_buf_p = '\0'; \ - yyleng = n; \ - } \ - while ( 0 ) - - -/* Internal utility routines. */ - -#ifndef yytext_ptr -#ifdef YY_USE_PROTOS -static void yy_flex_strncpy( char *s1, yyconst char *s2, int n ) -#else -static void yy_flex_strncpy( s1, s2, n ) -char *s1; -yyconst char *s2; -int n; -#endif - { - register int i; - for ( i = 0; i < n; ++i ) - s1[i] = s2[i]; - } -#endif - -#ifdef YY_NEED_STRLEN -#ifdef YY_USE_PROTOS -static int yy_flex_strlen( yyconst char *s ) -#else -static int yy_flex_strlen( s ) -yyconst char *s; -#endif - { - register int n; - for ( n = 0; s[n]; ++n ) - ; - - return n; - } -#endif - - -#ifdef YY_USE_PROTOS -static void *yy_flex_alloc( yy_size_t size ) -#else -static void *yy_flex_alloc( size ) -yy_size_t size; -#endif - { - return (void *) malloc( size ); - } - -#ifdef YY_USE_PROTOS -static void *yy_flex_realloc( void *ptr, yy_size_t size ) -#else -static void *yy_flex_realloc( ptr, size ) -void *ptr; -yy_size_t size; -#endif - { - /* The cast to (char *) in the following accommodates both - * implementations that use char* generic pointers, and those - * that use void* generic pointers. It works with the latter - * because both ANSI C and C++ allow castless assignment from - * any pointer type to void*, and deal with argument conversions - * as though doing an assignment. - */ - return (void *) realloc( (char *) ptr, size ); - } - -#ifdef YY_USE_PROTOS -static void yy_flex_free( void *ptr ) -#else -static void yy_flex_free( ptr ) -void *ptr; -#endif - { - free( ptr ); - } - -#if YY_MAIN -int main() - { - yylex(); - return 0; - } -#endif -#line 108 "calclex.l" - - -int -yywrap () -{ - return 1; -} diff --git a/src/gmp/demos/calc/calclex.l b/src/gmp/demos/calc/calclex.l deleted file mode 100644 index c487c5413..000000000 --- a/src/gmp/demos/calc/calclex.l +++ /dev/null @@ -1,114 +0,0 @@ -/* Lexical analyzer for calc program. - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -%{ -#include -#include "calc-common.h" - - -#if WITH_READLINE -/* Let GNU flex use readline. See the calcread.c redefined input() for a - way that might work for a standard lex too. */ -#define YY_INPUT(buf,result,max_size) \ - result = calc_input (buf, max_size); -#endif - - -/* Non-zero when reading the second or subsequent line of an expression, - used to give a different prompt when using readline. */ -int calc_more_input = 0; - - -const struct calc_keywords_t calc_keywords[] = { - { "abs", ABS }, - { "bin", BIN }, - { "decimal", DECIMAL }, - { "fib", FIB }, - { "hex", HEX }, - { "help", HELP }, - { "gcd", GCD }, - { "kron", KRON }, - { "lcm", LCM }, - { "lucnum", LUCNUM }, - { "nextprime", NEXTPRIME }, - { "powm", POWM }, - { "quit", QUIT }, - { "root", ROOT }, - { "sqrt", SQRT }, - { NULL } -}; -%} - -%% - -[ \t\f] { /* white space is skipped */ } - -[;\n] { /* semicolon or newline separates statements */ - calc_more_input = 0; - return EOS; } -\\\n { /* escaped newlines are skipped */ } - - -#(([^\\\n]*)\\)+\n { - /* comment through to escaped newline is skipped */ } -#[^\n]*\n { /* comment through to newline is a separator */ - calc_more_input = 0; - return EOS; } -#[^\n]* { /* comment through to EOF skipped */ } - - -[-+*/%()<>^!=,] { return yytext[0]; } -"<=" { return LE; } -">=" { return GE; } -"==" { return EQ; } -"!=" { return NE; } -"<<" { return LSHIFT; } -">>" { return RSHIFT; } -"&&" { return LAND; } -"||" { return LOR; } - -(0[xX])?[0-9A-F]+ { - yylval.str = yytext; - return NUMBER; } - -[a-zA-Z][a-zA-Z0-9]* { - int i; - - for (i = 0; calc_keywords[i].name != NULL; i++) - if (strcmp (yytext, calc_keywords[i].name) == 0) - return calc_keywords[i].value; - - if (yytext[0] >= 'a' && yytext[0] <= 'z' && yytext[1] == '\0') - { - yylval.var = yytext[0] - 'a'; - return VARIABLE; - } - - return BAD; -} - -. { return BAD; } - -%% - -int -yywrap () -{ - return 1; -} diff --git a/src/gmp/demos/calc/calcread.c b/src/gmp/demos/calc/calcread.c deleted file mode 100644 index a3a3635c0..000000000 --- a/src/gmp/demos/calc/calcread.c +++ /dev/null @@ -1,147 +0,0 @@ -/* Readline support for calc program. - -Copyright 2000, 2001 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include "calc-common.h" - -#if WITH_READLINE -#include /* for FILE for old versions of readline/readline.h */ -#include /* for free */ -#include /* for strdup */ -#include /* for isatty */ -#include -#include - -#include "gmp.h" - - -/* change this to "#define TRACE(x) x" for a few diagnostics */ -#define TRACE(x) - - -#define MIN(x,y) ((x) < (y) ? (x) : (y)) - -char * -calc_completion_entry (const char *text, int state) -{ - static int index, len; - char *name; - - if (!state) - { - index = 0; - len = strlen (text); - } - TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n", - text, state, index, len)); - while ((name = calc_keywords[index].name) != NULL) - { - index++; - if (memcmp (name, text, len) == 0) - return (strdup (name)); - } - return NULL; -} - -void -calc_init_readline (void) -{ - /* By default use readline when the input is a tty. It's a bit contrary - to the GNU interface conventions to make the behaviour depend on where - the input is coming from, but this is pretty convenient. */ - if (calc_option_readline == -1) - { - calc_option_readline = isatty (fileno (stdin)); - TRACE (printf ("calc_option_readline %d\n", calc_option_readline)); - } - - if (calc_option_readline) - { - printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version); - printf ("Type \"help\" for help.\n"); - rl_readline_name = "gmp-calc"; - rl_completion_entry_function = calc_completion_entry; - } -} - - -/* This function is supposed to return YY_NULL to indicate EOF, but that - constant is only in calclex.c and we don't want to clutter calclex.l with - this readline stuff, so instead just hard code 0 for YY_NULL. That's - it's defined value on unix anyway. */ - -int -calc_input (char *buf, size_t max_size) -{ - if (calc_option_readline) - { - static char *line = NULL; - static size_t line_size = 0; - static size_t upto = 0; - size_t copy_size; - - if (upto >= line_size) - { - if (line != NULL) - free (line); - - line = readline (calc_more_input ? "more> " : "> "); - calc_more_input = 1; - if (line == NULL) - return 0; - TRACE (printf ("readline: %s\n", line)); - - if (line[0] != '\0') - add_history (line); - - line_size = strlen (line); - line[line_size] = '\n'; - line_size++; - upto = 0; - } - - copy_size = MIN (line_size-upto, max_size); - memcpy (buf, line+upto, copy_size); - upto += copy_size; - return copy_size; - } - else - { - /* not readline */ - return fread (buf, 1, max_size, stdin); - } -} - - -/* This redefined input() might let a traditional lex use the readline - support here. Apparently POSIX doesn't specify whether an override like - this will work, so maybe it'll work or maybe it won't. This function is - also not particularly efficient, but don't worry about that, since flex - is the preferred parser. */ - -int -input (void) -{ - char c; - if (calc_input (&c, 1) != 1) - return EOF; - else - return (int) c; -} - -#endif /* WITH_READLINE */ diff --git a/src/gmp/demos/expr/Makefile.am b/src/gmp/demos/expr/Makefile.am deleted file mode 100644 index 4e0645b46..000000000 --- a/src/gmp/demos/expr/Makefile.am +++ /dev/null @@ -1,45 +0,0 @@ -## Process this file with automake to generate Makefile.in - -# Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or (at your -# option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/tests - -# FIXME: This is a workaround for a bug in automake 1.8.4. When the only -# library is in EXTRA_LIBRARIES, $(ARFLAGS) is used but no default setting -# for that variable is established. We give an explicit ARFLAGS=cru the -# same as generated for lib_LIBRARIES or noinst_LIBRARIES. -# -ARFLAGS = cru - -EXTRA_LIBRARIES = libexpr.a -libexpr_a_SOURCES = expr.h expr-impl.h \ - expr.c exprv.c exprz.c exprza.c exprq.c exprqa.c exprf.c exprfa.c - -EXTRA_PROGRAMS = run-expr t-expr -LDADD = libexpr.a $(top_builddir)/libgmp.la -t_expr_LDADD = $(top_builddir)/tests/libtests.la $(LDADD) - -CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LIBRARIES) - -allprogs: $(EXTRA_PROGRAMS) - -$(top_builddir)/tests/libtests.la: - cd $(top_builddir)/tests; $(MAKE) $(AM_MAKEFLAGS) libtests.la diff --git a/src/gmp/demos/expr/Makefile.in b/src/gmp/demos/expr/Makefile.in deleted file mode 100644 index 8663551ae..000000000 --- a/src/gmp/demos/expr/Makefile.in +++ /dev/null @@ -1,541 +0,0 @@ -# Makefile.in generated by automake 1.9.6 from Makefile.am. -# @configure_input@ - -# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, -# 2003, 2004, 2005 Free Software Foundation, Inc. -# This Makefile.in is free software; the Free Software Foundation -# gives unlimited permission to copy and/or distribute it, -# with or without modifications, as long as this notice is preserved. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY, to the extent permitted by law; without -# even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - -@SET_MAKE@ - -# Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or (at your -# option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. -srcdir = @srcdir@ -top_srcdir = @top_srcdir@ -VPATH = @srcdir@ -pkgdatadir = $(datadir)/@PACKAGE@ -pkglibdir = $(libdir)/@PACKAGE@ -pkgincludedir = $(includedir)/@PACKAGE@ -top_builddir = ../.. -am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd -INSTALL = @INSTALL@ -install_sh_DATA = $(install_sh) -c -m 644 -install_sh_PROGRAM = $(install_sh) -c -install_sh_SCRIPT = $(install_sh) -c -INSTALL_HEADER = $(INSTALL_DATA) -transform = $(program_transform_name) -NORMAL_INSTALL = : -PRE_INSTALL = : -POST_INSTALL = : -NORMAL_UNINSTALL = : -PRE_UNINSTALL = : -POST_UNINSTALL = : -build_triplet = @build@ -host_triplet = @host@ -ANSI2KNR = $(top_builddir)/ansi2knr -LIBOBJDIR = -EXTRA_PROGRAMS = run-expr$(EXEEXT) t-expr$(EXEEXT) -subdir = demos/expr -DIST_COMMON = README $(srcdir)/Makefile.am $(srcdir)/Makefile.in -ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 -am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ - $(top_srcdir)/configure.in -am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ - $(ACLOCAL_M4) -mkinstalldirs = $(install_sh) -d -CONFIG_HEADER = $(top_builddir)/config.h -CONFIG_CLEAN_FILES = -libexpr_a_AR = $(AR) $(ARFLAGS) -libexpr_a_LIBADD = -am_libexpr_a_OBJECTS = expr$U.$(OBJEXT) exprv$U.$(OBJEXT) \ - exprz$U.$(OBJEXT) exprza$U.$(OBJEXT) exprq$U.$(OBJEXT) \ - exprqa$U.$(OBJEXT) exprf$U.$(OBJEXT) exprfa$U.$(OBJEXT) -libexpr_a_OBJECTS = $(am_libexpr_a_OBJECTS) -run_expr_SOURCES = run-expr.c -run_expr_OBJECTS = run-expr$U.$(OBJEXT) -run_expr_LDADD = $(LDADD) -run_expr_DEPENDENCIES = libexpr.a $(top_builddir)/libgmp.la -t_expr_SOURCES = t-expr.c -t_expr_OBJECTS = t-expr$U.$(OBJEXT) -am__DEPENDENCIES_1 = libexpr.a $(top_builddir)/libgmp.la -t_expr_DEPENDENCIES = $(top_builddir)/tests/libtests.la \ - $(am__DEPENDENCIES_1) -DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) -depcomp = -am__depfiles_maybe = -COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ - $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -LTCOMPILE = $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) \ - $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ - $(AM_CFLAGS) $(CFLAGS) -CCLD = $(CC) -LINK = $(LIBTOOL) --tag=CC --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ - $(AM_LDFLAGS) $(LDFLAGS) -o $@ -SOURCES = $(libexpr_a_SOURCES) run-expr.c t-expr.c -DIST_SOURCES = $(libexpr_a_SOURCES) run-expr.c t-expr.c -ETAGS = etags -CTAGS = ctags -DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) -ABI = @ABI@ -ACLOCAL = @ACLOCAL@ -AMTAR = @AMTAR@ -AR = @AR@ -AS = @AS@ -AUTOCONF = @AUTOCONF@ -AUTOHEADER = @AUTOHEADER@ -AUTOMAKE = @AUTOMAKE@ -AWK = @AWK@ -BITS_PER_MP_LIMB = @BITS_PER_MP_LIMB@ -CALLING_CONVENTIONS_OBJS = @CALLING_CONVENTIONS_OBJS@ -CC = @CC@ -CCAS = @CCAS@ -CC_FOR_BUILD = @CC_FOR_BUILD@ -CFLAGS = @CFLAGS@ -CPP = @CPP@ -CPPFLAGS = @CPPFLAGS@ -CPP_FOR_BUILD = @CPP_FOR_BUILD@ -CXX = @CXX@ -CXXCPP = @CXXCPP@ -CXXFLAGS = @CXXFLAGS@ -CYGPATH_W = @CYGPATH_W@ -DEFN_LONG_LONG_LIMB = @DEFN_LONG_LONG_LIMB@ -DEFS = @DEFS@ -DLLTOOL = @DLLTOOL@ -DSYMUTIL = @DSYMUTIL@ -DUMPBIN = @DUMPBIN@ -ECHO_C = @ECHO_C@ -ECHO_N = @ECHO_N@ -ECHO_T = @ECHO_T@ -EGREP = @EGREP@ -ENABLE_STATIC_FALSE = @ENABLE_STATIC_FALSE@ -ENABLE_STATIC_TRUE = @ENABLE_STATIC_TRUE@ -EXEEXT = @EXEEXT@ -EXEEXT_FOR_BUILD = @EXEEXT_FOR_BUILD@ -FGREP = @FGREP@ -GMP_LDFLAGS = @GMP_LDFLAGS@ -GMP_NAIL_BITS = @GMP_NAIL_BITS@ -GREP = @GREP@ -HAVE_CLOCK_01 = @HAVE_CLOCK_01@ -HAVE_CPUTIME_01 = @HAVE_CPUTIME_01@ -HAVE_GETRUSAGE_01 = @HAVE_GETRUSAGE_01@ -HAVE_GETTIMEOFDAY_01 = @HAVE_GETTIMEOFDAY_01@ -HAVE_HOST_CPU_FAMILY_power = @HAVE_HOST_CPU_FAMILY_power@ -HAVE_HOST_CPU_FAMILY_powerpc = @HAVE_HOST_CPU_FAMILY_powerpc@ -HAVE_SIGACTION_01 = @HAVE_SIGACTION_01@ -HAVE_SIGALTSTACK_01 = @HAVE_SIGALTSTACK_01@ -HAVE_SIGSTACK_01 = @HAVE_SIGSTACK_01@ -HAVE_STACK_T_01 = @HAVE_STACK_T_01@ -HAVE_SYS_RESOURCE_H_01 = @HAVE_SYS_RESOURCE_H_01@ -INSTALL_DATA = @INSTALL_DATA@ -INSTALL_PROGRAM = @INSTALL_PROGRAM@ -INSTALL_SCRIPT = @INSTALL_SCRIPT@ -INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ -LD = @LD@ -LDFLAGS = @LDFLAGS@ -LEX = @LEX@ -LEXLIB = @LEXLIB@ -LEX_OUTPUT_ROOT = @LEX_OUTPUT_ROOT@ -LIBCURSES = @LIBCURSES@ -LIBGMPXX_LDFLAGS = @LIBGMPXX_LDFLAGS@ -LIBGMP_DLL = @LIBGMP_DLL@ -LIBGMP_LDFLAGS = @LIBGMP_LDFLAGS@ -LIBM = @LIBM@ -LIBM_FOR_BUILD = @LIBM_FOR_BUILD@ -LIBOBJS = @LIBOBJS@ -LIBREADLINE = @LIBREADLINE@ -LIBS = @LIBS@ -LIBTOOL = @LIBTOOL@ -LIPO = @LIPO@ -LN_S = @LN_S@ -LTLIBOBJS = @LTLIBOBJS@ -M4 = @M4@ -MAINT = @MAINT@ -MAINTAINER_MODE_FALSE = @MAINTAINER_MODE_FALSE@ -MAINTAINER_MODE_TRUE = @MAINTAINER_MODE_TRUE@ -MAKEINFO = @MAKEINFO@ -MANIFEST_TOOL = @MANIFEST_TOOL@ -NM = @NM@ -NMEDIT = @NMEDIT@ -OBJDUMP = @OBJDUMP@ -OBJEXT = @OBJEXT@ -OTOOL = @OTOOL@ -OTOOL64 = @OTOOL64@ -PACKAGE = @PACKAGE@ -PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ -PACKAGE_NAME = @PACKAGE_NAME@ -PACKAGE_STRING = @PACKAGE_STRING@ -PACKAGE_TARNAME = @PACKAGE_TARNAME@ -PACKAGE_URL = @PACKAGE_URL@ -PACKAGE_VERSION = @PACKAGE_VERSION@ -PATH_SEPARATOR = @PATH_SEPARATOR@ -RANLIB = @RANLIB@ -SED = @SED@ -SET_MAKE = @SET_MAKE@ -SHELL = @SHELL@ -SPEED_CYCLECOUNTER_OBJ = @SPEED_CYCLECOUNTER_OBJ@ -STRIP = @STRIP@ -TAL_OBJECT = @TAL_OBJECT@ -TUNE_SQR_OBJ = @TUNE_SQR_OBJ@ -U = @U@ -U_FOR_BUILD = @U_FOR_BUILD@ -VERSION = @VERSION@ -WANT_CXX_FALSE = @WANT_CXX_FALSE@ -WANT_CXX_TRUE = @WANT_CXX_TRUE@ -WANT_MPBSD_FALSE = @WANT_MPBSD_FALSE@ -WANT_MPBSD_TRUE = @WANT_MPBSD_TRUE@ -WITH_READLINE_01 = @WITH_READLINE_01@ -YACC = @YACC@ -YFLAGS = @YFLAGS@ -ac_ct_AR = @ac_ct_AR@ -ac_ct_CC = @ac_ct_CC@ -ac_ct_CXX = @ac_ct_CXX@ -ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ -am__leading_dot = @am__leading_dot@ -am__tar = @am__tar@ -am__untar = @am__untar@ -bindir = @bindir@ -build = @build@ -build_alias = @build_alias@ -build_cpu = @build_cpu@ -build_os = @build_os@ -build_vendor = @build_vendor@ -datadir = @datadir@ -datarootdir = @datarootdir@ -docdir = @docdir@ -dvidir = @dvidir@ -exec_prefix = @exec_prefix@ -gmp_srclinks = @gmp_srclinks@ -host = @host@ -host_alias = @host_alias@ -host_cpu = @host_cpu@ -host_os = @host_os@ -host_vendor = @host_vendor@ -htmldir = @htmldir@ -includedir = @includedir@ -infodir = @infodir@ -install_sh = @install_sh@ -libdir = @libdir@ -libexecdir = @libexecdir@ -localedir = @localedir@ -localstatedir = @localstatedir@ -mandir = @mandir@ -mkdir_p = @mkdir_p@ -mpn_objects = @mpn_objects@ -mpn_objs_in_libgmp = @mpn_objs_in_libgmp@ -mpn_objs_in_libmp = @mpn_objs_in_libmp@ -oldincludedir = @oldincludedir@ -pdfdir = @pdfdir@ -prefix = @prefix@ -program_transform_name = @program_transform_name@ -psdir = @psdir@ -sbindir = @sbindir@ -sharedstatedir = @sharedstatedir@ -sysconfdir = @sysconfdir@ -target_alias = @target_alias@ -INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/tests - -# FIXME: This is a workaround for a bug in automake 1.8.4. When the only -# library is in EXTRA_LIBRARIES, $(ARFLAGS) is used but no default setting -# for that variable is established. We give an explicit ARFLAGS=cru the -# same as generated for lib_LIBRARIES or noinst_LIBRARIES. -# -ARFLAGS = cru -EXTRA_LIBRARIES = libexpr.a -libexpr_a_SOURCES = expr.h expr-impl.h \ - expr.c exprv.c exprz.c exprza.c exprq.c exprqa.c exprf.c exprfa.c - -LDADD = libexpr.a $(top_builddir)/libgmp.la -t_expr_LDADD = $(top_builddir)/tests/libtests.la $(LDADD) -CLEANFILES = $(EXTRA_PROGRAMS) $(EXTRA_LIBRARIES) -all: all-am - -.SUFFIXES: -.SUFFIXES: .c .lo .o .obj -$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) - @for dep in $?; do \ - case '$(am__configure_deps)' in \ - *$$dep*) \ - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ - && exit 0; \ - exit 1;; \ - esac; \ - done; \ - echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu --ignore-deps demos/expr/Makefile'; \ - cd $(top_srcdir) && \ - $(AUTOMAKE) --gnu --ignore-deps demos/expr/Makefile -.PRECIOUS: Makefile -Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status - @case '$?' in \ - *config.status*) \ - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ - *) \ - echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ - cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ - esac; - -$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh - -$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh -$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps) - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh -libexpr.a: $(libexpr_a_OBJECTS) $(libexpr_a_DEPENDENCIES) - -rm -f libexpr.a - $(libexpr_a_AR) libexpr.a $(libexpr_a_OBJECTS) $(libexpr_a_LIBADD) - $(RANLIB) libexpr.a -run-expr$(EXEEXT): $(run_expr_OBJECTS) $(run_expr_DEPENDENCIES) - @rm -f run-expr$(EXEEXT) - $(LINK) $(run_expr_LDFLAGS) $(run_expr_OBJECTS) $(run_expr_LDADD) $(LIBS) -t-expr$(EXEEXT): $(t_expr_OBJECTS) $(t_expr_DEPENDENCIES) - @rm -f t-expr$(EXEEXT) - $(LINK) $(t_expr_LDFLAGS) $(t_expr_OBJECTS) $(t_expr_LDADD) $(LIBS) - -mostlyclean-compile: - -rm -f *.$(OBJEXT) - -distclean-compile: - -rm -f *.tab.c -$(top_builddir)/ansi2knr: - cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) ansi2knr - -mostlyclean-kr: - -test "$U" = "" || rm -f *_.c - -.c.o: - $(COMPILE) -c $< - -.c.obj: - $(COMPILE) -c `$(CYGPATH_W) '$<'` - -.c.lo: - $(LTCOMPILE) -c -o $@ $< -expr_.c: expr.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/expr.c; then echo $(srcdir)/expr.c; else echo expr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprf_.c: exprf.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprf.c; then echo $(srcdir)/exprf.c; else echo exprf.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprfa_.c: exprfa.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprfa.c; then echo $(srcdir)/exprfa.c; else echo exprfa.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprq_.c: exprq.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprq.c; then echo $(srcdir)/exprq.c; else echo exprq.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprqa_.c: exprqa.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprqa.c; then echo $(srcdir)/exprqa.c; else echo exprqa.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprv_.c: exprv.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprv.c; then echo $(srcdir)/exprv.c; else echo exprv.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprz_.c: exprz.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprz.c; then echo $(srcdir)/exprz.c; else echo exprz.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -exprza_.c: exprza.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/exprza.c; then echo $(srcdir)/exprza.c; else echo exprza.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -run-expr_.c: run-expr.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/run-expr.c; then echo $(srcdir)/run-expr.c; else echo run-expr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -t-expr_.c: t-expr.c $(ANSI2KNR) - $(CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/t-expr.c; then echo $(srcdir)/t-expr.c; else echo t-expr.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > $@ || rm -f $@ -expr_.$(OBJEXT) expr_.lo exprf_.$(OBJEXT) exprf_.lo exprfa_.$(OBJEXT) \ -exprfa_.lo exprq_.$(OBJEXT) exprq_.lo exprqa_.$(OBJEXT) exprqa_.lo \ -exprv_.$(OBJEXT) exprv_.lo exprz_.$(OBJEXT) exprz_.lo \ -exprza_.$(OBJEXT) exprza_.lo run-expr_.$(OBJEXT) run-expr_.lo \ -t-expr_.$(OBJEXT) t-expr_.lo : $(ANSI2KNR) - -mostlyclean-libtool: - -rm -f *.lo - -clean-libtool: - -rm -rf .libs _libs - -distclean-libtool: - -rm -f libtool config.lt -uninstall-info-am: - -ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - mkid -fID $$unique -tags: TAGS - -TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ - test -n "$$unique" || unique=$$empty_fix; \ - $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ - $$tags $$unique; \ - fi -ctags: CTAGS -CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - test -z "$(CTAGS_ARGS)$$tags$$unique" \ - || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ - $$tags $$unique - -GTAGS: - here=`$(am__cd) $(top_builddir) && pwd` \ - && cd $(top_srcdir) \ - && gtags -i $(GTAGS_ARGS) $$here - -distclean-tags: - -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags - -distdir: $(DISTFILES) - @srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \ - topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \ - list='$(DISTFILES)'; for file in $$list; do \ - case $$file in \ - $(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \ - $(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \ - esac; \ - if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ - dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ - if test "$$dir" != "$$file" && test "$$dir" != "."; then \ - dir="/$$dir"; \ - $(mkdir_p) "$(distdir)$$dir"; \ - else \ - dir=''; \ - fi; \ - if test -d $$d/$$file; then \ - if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ - cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ - fi; \ - cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ - else \ - test -f $(distdir)/$$file \ - || cp -p $$d/$$file $(distdir)/$$file \ - || exit 1; \ - fi; \ - done -check-am: all-am -check: check-am -all-am: Makefile -installdirs: -install: install-am -install-exec: install-exec-am -install-data: install-data-am -uninstall: uninstall-am - -install-am: all-am - @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am - -installcheck: installcheck-am -install-strip: - $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ - install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ - `test -z '$(STRIP)' || \ - echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install -mostlyclean-generic: - -clean-generic: - -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) - -distclean-generic: - -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) - -maintainer-clean-generic: - @echo "This command is intended for maintainers to use" - @echo "it deletes files that may require special tools to rebuild." -clean: clean-am - -clean-am: clean-generic clean-libtool mostlyclean-am - -distclean: distclean-am - -rm -f Makefile -distclean-am: clean-am distclean-compile distclean-generic \ - distclean-libtool distclean-tags - -dvi: dvi-am - -dvi-am: - -html: html-am - -info: info-am - -info-am: - -install-data-am: - -install-exec-am: - -install-info: install-info-am - -install-man: - -installcheck-am: - -maintainer-clean: maintainer-clean-am - -rm -f Makefile -maintainer-clean-am: distclean-am maintainer-clean-generic - -mostlyclean: mostlyclean-am - -mostlyclean-am: mostlyclean-compile mostlyclean-generic mostlyclean-kr \ - mostlyclean-libtool - -pdf: pdf-am - -pdf-am: - -ps: ps-am - -ps-am: - -uninstall-am: uninstall-info-am - -.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ - clean-libtool ctags distclean distclean-compile \ - distclean-generic distclean-libtool distclean-tags distdir dvi \ - dvi-am html html-am info info-am install install-am \ - install-data install-data-am install-exec install-exec-am \ - install-info install-info-am install-man install-strip \ - installcheck installcheck-am installdirs maintainer-clean \ - maintainer-clean-generic mostlyclean mostlyclean-compile \ - mostlyclean-generic mostlyclean-kr mostlyclean-libtool pdf \ - pdf-am ps ps-am tags uninstall uninstall-am uninstall-info-am - - -allprogs: $(EXTRA_PROGRAMS) - -$(top_builddir)/tests/libtests.la: - cd $(top_builddir)/tests; $(MAKE) $(AM_MAKEFLAGS) libtests.la -# Tell versions [3.59,3.63) of GNU make to not export all variables. -# Otherwise a system limit (for SysV at least) may be exceeded. -.NOEXPORT: diff --git a/src/gmp/demos/expr/README b/src/gmp/demos/expr/README deleted file mode 100644 index 8900e9619..000000000 --- a/src/gmp/demos/expr/README +++ /dev/null @@ -1,492 +0,0 @@ -Copyright 2001, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301, USA. - - - - - - - GMP EXPRESSION EVALUATION - ------------------------- - - - -THIS CODE IS PRELIMINARY AND MAY BE SUBJECT TO INCOMPATIBLE CHANGES IN -FUTURE VERSIONS OF GMP. - - - -The files in this directory implement a simple scheme of string based -expression parsing and evaluation, supporting mpz, mpq and mpf. - -This will be slower than direct GMP library calls, but may be convenient in -various circumstances, such as while prototyping, or for letting a user -enter values in symbolic form. "2**5723-7" for example is a lot easier to -enter or maintain than the equivalent written out in decimal. - - - -BUILDING - -Nothing in this directory is a normal part of libgmp, and nothing is built -or installed, but various Makefile rules are available to compile -everything. - -All the functions are available through a little library (there's no shared -library since upward binary compatibility is not guaranteed). - - make libexpr.a - -In a program, prototypes are available using - - #include "expr.h" - -run-expr.c is a sample program doing evaluations from the command line. - - make run-expr - ./run-expr '1+2*3' - -t-expr.c is self-test program, it prints nothing if successful. - - make t-expr - ./t-expr - -The expr*.c sources don't depend on gmp-impl.h and can be compiled with just -a standard installed GMP. This isn't true of t-expr though, since it uses -some of the internal tests/libtests.la. - - - -SIMPLE USAGE - -int mpz_expr (mpz_t res, int base, const char *e, ...); -int mpq_expr (mpq_t res, int base, const char *e, ...); -int mpf_expr (mpf_t res, int base, const char *e, ...); - -These functions evaluate simple arithmetic expressions. For example, - - mpz_expr (result, 0, "123+456", NULL); - -Numbers are parsed by mpz_expr and mpq_expr the same as mpz_set_str with the -given base. mpf_expr follows mpf_set_str, but supporting an "0x" prefix for -hex when base==0. - - mpz_expr (result, 0, "0xAAAA * 0x5555", NULL); - -White space, as indicated by isspace(), is ignored except for the -purpose of separating tokens. - -Variables can be included in expressions by putting them in the varargs list -after the string. "a", "b", "c" etc in the expression string designate -those values. For example, - - mpq_t foo, bar; - ... - mpq_expr (q, 10, "2/3 + 1/a + b/2", foo, bar, NULL); - -Here "a" will be the value from foo and "b" from bar. Up to 26 variables -can be included this way. The NULL must be present to indicate the end of -the list. - -Variables can also be written "$a", "$b" etc. This is necessary when using -bases greater than 10 since plain "a", "b" etc will otherwise be interpreted -as numbers. For example, - - mpf_t quux; - mpf_expr (f, 16, "F00F@-6 * $a", quux, NULL); - -All the standard C operators are available, with the usual precedences, plus -"**" for exponentiation at the highest precedence (and right associative). - - Operators Precedence - ** 220 - ~ ! - (unary) 210 - * / % 200 - + - 190 - << >> 180 - <= < >= > 170 - == != 160 - & 150 - ^ 140 - | 130 - && 120 - || 110 - ? : 100/101 - -Currently only mpz_expr has the bitwise ~ % & ^ and | operators. The -precedence numbers are of interest in the advanced usage described below. - -Various functions are available too. For example, - - mpz_expr (res, 10, "gcd(123,456,789) * abs(a)", var, NULL); - -The following is the full set of functions, - - mpz_expr - abs bin clrbit cmp cmpabs congruent_p divisible_p even_p fib fac - gcd hamdist invert jacobi kronecker lcm lucnum max min nextprime - odd_p perfect_power_p perfect_square_p popcount powm - probab_prime_p root scan0 scan1 setbit sgn sqrt - - mpq_expr - abs, cmp, den, max, min, num, sgn - - mpf_expr - abs, ceil, cmp, eq, floor, integer_p, max, min, reldiff, sgn, - sqrt, trunc - -All these are the same as the GMP library functions, except that min and max -don't exist in the library. Note also that min, max, gcd and lcm take any -number of arguments, not just two. - -mpf_expr does all calculations to the precision of the destination variable. - - -Expression parsing can succeed or fail. The return value indicates this, -and will be one of the following - - MPEXPR_RESULT_OK - MPEXPR_RESULT_BAD_VARIABLE - MPEXPR_RESULT_BAD_TABLE - MPEXPR_RESULT_PARSE_ERROR - MPEXPR_RESULT_NOT_UI - -BAD_VARIABLE is when a variable is referenced that hasn't been provided. -For example if "c" is used when only two parameters have been passed. -BAD_TABLE is applicable to the advanced usage described below. - -PARSE_ERROR is a general syntax error, returned for any mal-formed input -string. - -NOT_UI is returned when an attempt is made to use an operand that's bigger -than an "unsigned long" with a function that's restricted to that range. -For example "fib" is mpz_fib_ui and only accepts an "unsigned long". - - - - -ADVANCED USAGE - -int mpz_expr_a (const struct mpexpr_operator_t *table, - mpz_ptr res, int base, const char *e, size_t elen, - mpz_srcptr var[26]) -int mpq_expr_a (const struct mpexpr_operator_t *table, - mpq_ptr res, int base, const char *e, size_t elen, - mpq_srcptr var[26]) -int mpf_expr_a (const struct mpexpr_operator_t *table, - mpf_ptr res, int base, unsigned long prec, - const char *e, size_t elen, - mpf_srcptr var[26]) - -These functions are an advanced interface to expression parsing. - -The string is taken as pointer and length. This makes it possible to parse -an expression in the middle of somewhere without copying and null -terminating it. - -Variables are an array of 26 pointers to the appropriate operands, or NULL -for variables that are not available. Any combination of variables can be -given, for example just "x" and "y" (var[23] and var[24]) could be set. - -Operators and functions are specified with a table. This makes it possible -to provide additional operators or functions, or to completely change the -syntax. The standard tables used by the simple functions above are -available as - - const struct mpexpr_operator_t * const mpz_expr_standard_table; - const struct mpexpr_operator_t * const mpq_expr_standard_table; - const struct mpexpr_operator_t * const mpf_expr_standard_table; - -struct mpexpr_operator_t is the following - - struct mpexpr_operator_t { - const char *name; - mpexpr_fun_t fun; - int type; - int precedence; - }; - - typedef void (*mpexpr_fun_t) (void); - -As an example, the standard mpz_expr table entry for multiplication is as -follows. See the source code for the full set of standard entries. - - { "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 200 }, - -"name" is the string to parse, "fun" is the function to call for it, "type" -indicates what parameters the function takes (among other things), and -"precedence" sets its operator precedence. - -A NULL for "name" indicates the end of the table, so for example an mpf -table with nothing but addition could be - - struct mpexpr_operator_t table[] = { - { "+", (mpexpr_fun_t) mpf_add, MPEXPR_TYPE_BINARY, 190 }, - { NULL } - }; - -A special type MPEXPR_TYPE_NEW_TABLE makes it possible to chain from one -table to another. For example the following would add a "mod" operator to -the standard mpz table, - - struct mpexpr_operator_t table[] = { - { "mod", (mpexpr_fun_t) mpz_fdiv_r, MPEXPR_TYPE_BINARY, 125 }, - { (const char *) mpz_expr_standard_table, NULL, MPEXPR_TYPE_NEW_TABLE } - }; - -Notice the low precedence on "mod", so that for instance "45+26 mod 7" -parses as "(45+26)mod7". - - -Functions are designated by a precedence of 0. They always occur as -"foo(expr)" and so have no need for a precedence level. mpq_abs in the -standard mpq table is - - { "abs", (mpexpr_fun_t) mpq_abs, MPEXPR_TYPE_UNARY }, - -Functions expecting no arguments as in "foo()" can be given with -MPEXPR_TYPE_0ARY, or actual constants to be parsed as just "foo" are -MPEXPR_TYPE_CONSTANT. For example if a "void mpf_const_pi(mpf_t f)" -function existed (which it doesn't) it could be, - - { "pi", (mpexpr_fun_t) mpf_const_pi, MPEXPR_TYPE_CONSTANT }, - - -Parsing of operator names is done by seeking the table entry with the -longest matching name. So for instance operators "<" and "<=" exist, and -when presented with "x <= y" the parser matches "<=" because it's longer. - -Parsing of function names, on the other hand, is done by requiring a whole -alphanumeric word to match. For example presented with "fib2zz(5)" the -parser will attempt to find a function called "fib2zz". A function "fib" -wouldn't be used because it doesn't match the whole word. - -The flag MPEXPR_TYPE_WHOLEWORD can be ORed into an operator type to override -the default parsing style. Similarly MPEXPR_TYPE_OPERATOR into a function. - - -Binary operators are left associative by default, meaning they're evaluated -from left to right, so for example "1+2+3" is treated as "(1+2)+3". -MPEXPR_TYPE_RIGHTASSOC can be ORed into the operator type to work from right -to left as in "1+(2+3)". This is generally what's wanted for -exponentiation, and for example the standard mpz table has - - { "**", (mpexpr_fun_t) mpz_pow_ui, - MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 } - -Unary operators are postfix by default. For example a factorial to be used -as "123!" might be - - { "!", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI, 215 } - -MPEXPR_TYPE_PREFIX can be ORed into the type to get a prefix operator. For -instance negation (unary minus) in the standard mpf table is - - { "-", (mpexpr_fun_t) mpf_neg, - MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, - - -The same operator can exist as a prefix unary and a binary, or as a prefix -and postfix unary, simply by putting two entries in the table. While -parsing the context determines which style is sought. But note that the -same operator can't be both a postfix unary and a binary, since the parser -doesn't try to look ahead to decide which ought to be used. - -When there's two entries for an operator, both prefix or both postfix (or -binary), then the first in the table will be used. This makes it possible -to override an entry in a standard table, for example to change the function -it calls, or perhaps its precedence level. The following would change mpz -division from tdiv to cdiv, - - struct mpexpr_operator_t table[] = { - { "/", (mpexpr_fun_t) mpz_cdiv_q, MPEXPR_TYPE_BINARY, 200 }, - { "%", (mpexpr_fun_t) mpz_cdiv_r, MPEXPR_TYPE_BINARY, 200 }, - { (char *) mpz_expr_standard_table, NULL, MPEXPR_TYPE_NEW_TABLE } - }; - - -The type field indicates what parameters the given function expects. The -following styles of functions are supported. mpz_t is shown, but of course -this is mpq_t for mpq_expr_a, mpf_t for mpf_expr_a, etc. - - MPEXPR_TYPE_CONSTANT void func (mpz_t result); - - MPEXPR_TYPE_0ARY void func (mpz_t result); - MPEXPR_TYPE_I_0ARY int func (void); - - MPEXPR_TYPE_UNARY void func (mpz_t result, mpz_t op); - MPEXPR_TYPE_UNARY_UI void func (mpz_t result, unsigned long op); - MPEXPR_TYPE_I_UNARY int func (mpz_t op); - MPEXPR_TYPE_I_UNARY_UI int func (unsigned long op); - - MPEXPR_TYPE_BINARY void func (mpz_t result, mpz_t op1, mpz_t op2); - MPEXPR_TYPE_BINARY_UI void func (mpz_t result, - mpz_t op1, unsigned long op2); - MPEXPR_TYPE_I_BINARY int func (mpz_t op1, mpz_t op2); - MPEXPR_TYPE_I_BINARY_UI int func (mpz_t op1, unsigned long op2); - - MPEXPR_TYPE_TERNARY void func (mpz_t result, - mpz_t op1, mpz_t op2, mpz_t op3); - MPEXPR_TYPE_TERNARY_UI void func (mpz_t result, mpz_t op1, mpz_t op2, - unsigned long op3); - MPEXPR_TYPE_I_TERNARY int func (mpz_t op1, mpz_t op2, mpz_t op3); - MPEXPR_TYPE_I_TERNARY_UI int func (mpz_t op1, mpz_t op2, - unsigned long op3); - -Notice the pattern of "UI" for the last parameter as an unsigned long, or -"I" for the result as an "int" return value. - -It's important that the declared type for an operator or function matches -the function pointer given. Any mismatch will have unpredictable results. - -For binary functions, a further type attribute is MPEXPR_TYPE_PAIRWISE which -indicates that any number of arguments should be accepted, and evaluated by -applying the given binary function to them pairwise. This is used by gcd, -lcm, min and max. For example the standard mpz gcd is - - { "gcd", (mpexpr_fun_t) mpz_gcd, - MPEXPR_TYPE_BINARY | MPEXPR_TYPE_PAIRWISE }, - -Some special types exist for comparison operators (or functions). -MPEXPR_TYPE_CMP_LT through MPEXPR_TYPE_CMP_GE expect an MPEXPR_TYPE_I_BINARY -function, returning positive, negative or zero like mpz_cmp and similar. -For example the standard mpf "!=" operator is - - { "!=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_NE, 160 }, - -But there's no obligation to use these types, for instance the standard mpq -table just uses a plain MPEXPR_TYPE_I_BINARY and mpq_equal for "==". - -Further special types MPEXPR_TYPE_MIN and MPEXPR_TYPE_MAX exist to implement -the min and max functions, and they take a function like mpf_cmp similarly. -The standard mpf max function is - - { "max", (mpexpr_fun_t) mpf_cmp, - MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE }, - -These can be used as operators too, for instance the following would be the ->? operator which is a feature of GNU C++, - - { ">?", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX, 175 }, - -Other special types are used to define "(" ")" parentheses, "," function -argument separator, "!" through "||" logical booleans, ternary "?" ":", and -the "$" which introduces variables. See the sources for how they should be -used. - - -User definable operator tables will have various uses. For example, - - - a subset of the C operators, to be rid of infrequently used things - - a more mathematical syntax like "." for multiply, "^" for powering, - and "!" for factorial - - a boolean evaluator with "^" for AND, "v" for OR - - variables introduced with "%" instead of "$" - - brackets as "[" and "]" instead of "(" and ")" - -The only fixed parts of the parsing are the treatment of numbers, whitespace -and the two styles of operator/function name recognition. - -As a final example, the following would be a complete mpz table implementing -some operators with a more mathematical syntax. Notice there's no need to -preserve the standard precedence values, anything can be used so long as -they're in the desired relation to each other. There's also no need to have -entries in precedence order, but it's convenient to do so to show what comes -where. - - static const struct mpexpr_operator_t table[] = { - { "^", (mpexpr_fun_t) mpz_pow_ui, - MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 9 }, - - { "!", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI, 8 }, - { "-", (mpexpr_fun_t) mpz_neg, - MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 7 }, - - { "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 6 }, - { "/", (mpexpr_fun_t) mpz_fdiv_q, MPEXPR_TYPE_BINARY, 6 }, - - { "+", (mpexpr_fun_t) mpz_add, MPEXPR_TYPE_BINARY, 5 }, - { "-", (mpexpr_fun_t) mpz_sub, MPEXPR_TYPE_BINARY, 5 }, - - { "mod", (mpexpr_fun_t) mpz_mod, MPEXPR_TYPE_BINARY, 6 }, - - { ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 }, - { "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 }, - { ",", NULL, MPEXPR_TYPE_ARGSEP, 2 }, - - { "$", NULL, MPEXPR_TYPE_VARIABLE, 1 }, - { NULL } - }; - - - - -INTERNALS - -Operator precedence is implemented using a control and data stack, there's -no C recursion. When an expression like 1+2*3 is read the "+" is held on -the control stack and 1 on the data stack until "*" has been parsed and -applied to 2 and 3. This happens any time a higher precedence operator -follows a lower one, or when a right-associative operator like "**" is -repeated. - -Parentheses are handled by making "(" a special prefix unary with a low -precedence so a whole following expression is read. The special operator -")" knows to discard the pending "(". Function arguments are handled -similarly, with the function pretending to be a low precedence prefix unary -operator, and with "," allowed within functions. The same special ")" -operator recognises a pending function and will invoke it appropriately. - -The ternary "? :" operator is also handled using precedences. ":" is one -level higher than "?", so when a valid a?b:c is parsed the ":" finds a "?" -on the control stack. It's a parse error for ":" to find anything else. - - - -FUTURE - -The ternary "?:" operator evaluates the "false" side of its pair, which is -wasteful, though it ought to be harmless. It'd be better if it could -evaluate only the "true" side. Similarly for the logical booleans "&&" and -"||" if they know their result already. - -Functions like MPEXPR_TYPE_BINARY could return a status indicating operand -out of range or whatever, to get an error back through mpz_expr etc. That -would want to be just an option, since plain mpz_add etc have no such -return. - -Could have assignments like "a = b*c" modifying the input variables. -Assignment could be an operator attribute, making it expect an lvalue. -There would want to be a standard table without assignments available -though, so user input could be safely parsed. - -The closing parethesis table entry could specify the type of open paren it -expects, so that "(" and ")" could match and "[" and "]" match but not a -mixture of the two. Currently "[" and "]" can be added, but there's no -error on writing a mixed expression like "2*(3+4]". Maybe also there could -be a way to say that functions can only be written with one or the other -style of parens. - - - ----------------- -Local variables: -mode: text -fill-column: 76 -End: diff --git a/src/gmp/demos/expr/expr-impl.h b/src/gmp/demos/expr/expr-impl.h deleted file mode 100644 index 9082578d1..000000000 --- a/src/gmp/demos/expr/expr-impl.h +++ /dev/null @@ -1,137 +0,0 @@ -/* Implementation specifics for expression evaluation. - -Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - - -/* Same tests as gmp.h. */ -#if defined (__STDC__) \ - || defined (__cplusplus) \ - || defined (_AIX) \ - || defined (__DECC) \ - || (defined (__mips) && defined (_SYSTYPE_SVR4)) \ - || defined (_MSC_VER) \ - || defined (_WIN32) -#define HAVE_STDARG 1 -#include -#else -#define HAVE_STDARG 0 -#include -#endif - -#include "expr.h" - - -#define isasciidigit(c) (isascii (c) && isdigit (c)) -#define isasciicsym(c) (isascii (c) && (isalnum(c) || (c) == '_')) - -#define isasciidigit_in_base(c,base) \ - (isascii (c) \ - && ((isdigit (c) && (c)-'0' < (base)) \ - || (isupper (c) && (c)-'A'+10 < (base)) \ - || (islower (c) && (c)-'a'+10 < (base)))) - - -union mpX_t { - mpz_t z; - mpq_t q; - mpf_t f; -}; - -typedef union mpX_t *mpX_ptr; -typedef __gmp_const union mpX_t *mpX_srcptr; - -typedef void (*mpexpr_fun_one_t) __GMP_PROTO ((mpX_ptr)); -typedef unsigned long (*mpexpr_fun_ui_one_t) __GMP_PROTO ((mpX_ptr)); - -typedef void (*mpexpr_fun_0ary_t) __GMP_PROTO ((mpX_ptr)); -typedef int (*mpexpr_fun_i_0ary_t) __GMP_PROTO ((void)); - -typedef void (*mpexpr_fun_unary_t) __GMP_PROTO ((mpX_ptr, mpX_srcptr)); -typedef void (*mpexpr_fun_unary_ui_t) __GMP_PROTO ((mpX_ptr, unsigned long)); -typedef int (*mpexpr_fun_i_unary_t) __GMP_PROTO ((mpX_srcptr)); -typedef int (*mpexpr_fun_i_unary_ui_t) __GMP_PROTO ((unsigned long)); - -typedef void (*mpexpr_fun_binary_t) __GMP_PROTO ((mpX_ptr, mpX_srcptr, mpX_srcptr)); -typedef void (*mpexpr_fun_binary_ui_t) __GMP_PROTO ((mpX_ptr, mpX_srcptr, unsigned long)); -typedef int (*mpexpr_fun_i_binary_t) __GMP_PROTO ((mpX_srcptr, mpX_srcptr)); -typedef int (*mpexpr_fun_i_binary_ui_t) __GMP_PROTO ((mpX_srcptr, unsigned long)); - -typedef void (*mpexpr_fun_ternary_t) - __GMP_PROTO ((mpX_ptr, mpX_srcptr, mpX_srcptr, mpX_srcptr)); -typedef void (*mpexpr_fun_ternary_ui_t) - __GMP_PROTO ((mpX_ptr, mpX_srcptr, mpX_srcptr, unsigned long)); -typedef int (*mpexpr_fun_i_ternary_t) - __GMP_PROTO ((mpX_srcptr, mpX_srcptr, mpX_srcptr)); -typedef int (*mpexpr_fun_i_ternary_ui_t) - __GMP_PROTO ((mpX_srcptr, mpX_srcptr, unsigned long)); - -typedef size_t (*mpexpr_fun_number_t) - __GMP_PROTO ((mpX_ptr, __gmp_const char *str, size_t len, int base)); -typedef void (*mpexpr_fun_swap_t) __GMP_PROTO ((mpX_ptr, mpX_ptr)); -typedef unsigned long (*mpexpr_fun_get_ui_t) __GMP_PROTO ((mpX_srcptr)); -typedef void (*mpexpr_fun_set_si_t) __GMP_PROTO ((mpX_srcptr, long)); - -struct mpexpr_control_t { - __gmp_const struct mpexpr_operator_t *op; - int argcount; -}; - -#define MPEXPR_VARIABLES 26 - -struct mpexpr_parse_t { - __gmp_const struct mpexpr_operator_t *table; - - mpX_ptr res; - int base; - unsigned long prec; - __gmp_const char *e; - size_t elen; - mpX_srcptr *var; - int error_code; - - int token; - __gmp_const struct mpexpr_operator_t *token_op; - - union mpX_t *data_stack; - int data_top; - int data_alloc; - int data_inited; - - struct mpexpr_control_t *control_stack; - int control_top; - int control_alloc; - - - mpexpr_fun_0ary_t mpX_clear; - mpexpr_fun_i_unary_t mpX_ulong_p; - mpexpr_fun_get_ui_t mpX_get_ui; - mpexpr_fun_unary_ui_t mpX_init; - mpexpr_fun_number_t mpX_number; - mpexpr_fun_unary_t mpX_set; - mpexpr_fun_unary_t mpX_set_or_swap; - mpexpr_fun_set_si_t mpX_set_si; - mpexpr_fun_swap_t mpX_swap; -}; - - -int mpexpr_evaluate __GMP_PROTO ((struct mpexpr_parse_t *p)); -int mpexpr_va_to_var __GMP_PROTO ((void *var[], va_list ap)); -size_t mpexpr_mpz_number __GMP_PROTO ((mpz_ptr res, - __gmp_const char *e, size_t elen, int base)); diff --git a/src/gmp/demos/expr/expr.c b/src/gmp/demos/expr/expr.c deleted file mode 100644 index 00ce5b963..000000000 --- a/src/gmp/demos/expr/expr.c +++ /dev/null @@ -1,825 +0,0 @@ -/* mpexpr_evaluate -- shared code for simple expression evaluation - -Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - -#include -#include -#include - -#include "gmp.h" -#include "expr-impl.h" - - -/* Change this to "#define TRACE(x) x" to get some traces. The trace - printfs junk up the code a bit, but it's very hard to tell what's going - on without them. Set MPX_TRACE to a suitable output function for the - mpz/mpq/mpf being run (if you have the wrong trace function it'll - probably segv). */ - -#define TRACE(x) -#define MPX_TRACE mpz_trace - - -/* A few helper macros copied from gmp-impl.h */ -#define ALLOCATE_FUNC_TYPE(n,type) \ - ((type *) (*allocate_func) ((n) * sizeof (type))) -#define ALLOCATE_FUNC_LIMBS(n) ALLOCATE_FUNC_TYPE (n, mp_limb_t) -#define REALLOCATE_FUNC_TYPE(p, old_size, new_size, type) \ - ((type *) (*reallocate_func) \ - (p, (old_size) * sizeof (type), (new_size) * sizeof (type))) -#define REALLOCATE_FUNC_LIMBS(p, old_size, new_size) \ - REALLOCATE_FUNC_TYPE(p, old_size, new_size, mp_limb_t) -#define FREE_FUNC_TYPE(p,n,type) (*free_func) (p, (n) * sizeof (type)) -#define FREE_FUNC_LIMBS(p,n) FREE_FUNC_TYPE (p, n, mp_limb_t) -#define ASSERT(x) - - - -/* All the error strings are just for diagnostic traces. Only the error - code is actually returned. */ -#define ERROR(str,code) \ - { \ - TRACE (printf ("%s\n", str)); \ - p->error_code = (code); \ - goto done; \ - } - - -#define REALLOC(ptr, alloc, incr, type) \ - do { \ - int new_alloc = (alloc) + (incr); \ - ptr = REALLOCATE_FUNC_TYPE (ptr, alloc, new_alloc, type); \ - (alloc) = new_alloc; \ - } while (0) - - -/* data stack top element */ -#define SP (p->data_stack + p->data_top) - -/* Make sure there's room for another data element above current top. - reallocate_func is fetched for when this macro is used in lookahead(). */ -#define DATA_SPACE() \ - do { \ - if (p->data_top + 1 >= p->data_alloc) \ - { \ - void *(*reallocate_func) (void *, size_t, size_t); \ - mp_get_memory_functions (NULL, &reallocate_func, NULL); \ - TRACE (printf ("grow stack from %d\n", p->data_alloc)); \ - REALLOC (p->data_stack, p->data_alloc, 20, union mpX_t); \ - } \ - ASSERT (p->data_top + 1 <= p->data_inited); \ - if (p->data_top + 1 == p->data_inited) \ - { \ - TRACE (printf ("initialize %d\n", p->data_top + 1)); \ - (*p->mpX_init) (&p->data_stack[p->data_top + 1], p->prec); \ - p->data_inited++; \ - } \ - } while (0) - -#define DATA_PUSH() \ - do { \ - p->data_top++; \ - ASSERT (p->data_top < p->data_alloc); \ - ASSERT (p->data_top < p->data_inited); \ - } while (0) - -/* the last stack entry is never popped, so top>=0 will be true */ -#define DATA_POP(n) \ - do { \ - p->data_top -= (n); \ - ASSERT (p->data_top >= 0); \ - } while (0) - - -/* lookahead() parses the next token. Return 1 if successful, with some - extra data. Return 0 if fail, with reason in p->error_code. - - "prefix" is MPEXPR_TYPE_PREFIX if an operator with that attribute is - preferred, or 0 if an operator without is preferred. */ - -#define TOKEN_EOF -1 /* no extra data */ -#define TOKEN_VALUE -2 /* pushed onto data stack */ -#define TOKEN_OPERATOR -3 /* stored in p->token_op */ -#define TOKEN_FUNCTION -4 /* stored in p->token_op */ - -#define TOKEN_NAME(n) \ - ((n) == TOKEN_EOF ? "TOKEN_EOF" \ - : (n) == TOKEN_VALUE ? "TOKEN_VALUE" \ - : (n) == TOKEN_OPERATOR ? "TOKEN_OPERATOR" \ - : (n) == TOKEN_VALUE ? "TOKEN_FUNCTION" \ - : "UNKNOWN TOKEN") - -/* Functions default to being parsed as whole words, operators to match just - at the start of the string. The type flags override this. */ -#define WHOLEWORD(op) \ - (op->precedence == 0 \ - ? (! (op->type & MPEXPR_TYPE_OPERATOR)) \ - : (op->type & MPEXPR_TYPE_WHOLEWORD)) - -#define isasciispace(c) (isascii (c) && isspace (c)) - -static int -lookahead (struct mpexpr_parse_t *p, int prefix) -{ - __gmp_const struct mpexpr_operator_t *op, *op_found; - size_t oplen, oplen_found, wlen; - int i; - - /* skip white space */ - while (p->elen > 0 && isasciispace (*p->e)) - p->e++, p->elen--; - - if (p->elen == 0) - { - TRACE (printf ("lookahead EOF\n")); - p->token = TOKEN_EOF; - return 1; - } - - DATA_SPACE (); - - /* Get extent of whole word. */ - for (wlen = 0; wlen < p->elen; wlen++) - if (! isasciicsym (p->e[wlen])) - break; - - TRACE (printf ("lookahead at: \"%.*s\" length %u, word %u\n", - (int) p->elen, p->e, p->elen, wlen)); - - op_found = NULL; - oplen_found = 0; - for (op = p->table; op->name != NULL; op++) - { - if (op->type == MPEXPR_TYPE_NEW_TABLE) - { - printf ("new\n"); - op = (struct mpexpr_operator_t *) op->name - 1; - continue; - } - - oplen = strlen (op->name); - if (! ((WHOLEWORD (op) ? wlen == oplen : p->elen >= oplen) - && memcmp (p->e, op->name, oplen) == 0)) - continue; - - /* Shorter matches don't replace longer previous ones. */ - if (op_found && oplen < oplen_found) - continue; - - /* On a match of equal length to a previous one, the old match isn't - replaced if it has the preferred prefix, and if it doesn't then - it's not replaced if the new one also doesn't. */ - if (op_found && oplen == oplen_found - && ((op_found->type & MPEXPR_TYPE_PREFIX) == prefix - || (op->type & MPEXPR_TYPE_PREFIX) != prefix)) - continue; - - /* This is now either the first match seen, or a longer than previous - match, or an equal to previous one but with a preferred prefix. */ - op_found = op; - oplen_found = oplen; - } - - if (op_found) - { - p->e += oplen_found, p->elen -= oplen_found; - - if (op_found->type == MPEXPR_TYPE_VARIABLE) - { - if (p->elen == 0) - ERROR ("end of string expecting a variable", - MPEXPR_RESULT_PARSE_ERROR); - i = p->e[0] - 'a'; - if (i < 0 || i >= MPEXPR_VARIABLES) - ERROR ("bad variable name", MPEXPR_RESULT_BAD_VARIABLE); - goto variable; - } - - if (op_found->precedence == 0) - { - TRACE (printf ("lookahead function: %s\n", op_found->name)); - p->token = TOKEN_FUNCTION; - p->token_op = op_found; - return 1; - } - else - { - TRACE (printf ("lookahead operator: %s\n", op_found->name)); - p->token = TOKEN_OPERATOR; - p->token_op = op_found; - return 1; - } - } - - oplen = (*p->mpX_number) (SP+1, p->e, p->elen, p->base); - if (oplen != 0) - { - p->e += oplen, p->elen -= oplen; - p->token = TOKEN_VALUE; - DATA_PUSH (); - TRACE (MPX_TRACE ("lookahead number", SP)); - return 1; - } - - /* Maybe an unprefixed one character variable */ - i = p->e[0] - 'a'; - if (wlen == 1 && i >= 0 && i < MPEXPR_VARIABLES) - { - variable: - p->e++, p->elen--; - if (p->var[i] == NULL) - ERROR ("NULL variable", MPEXPR_RESULT_BAD_VARIABLE); - TRACE (printf ("lookahead variable: var[%d] = ", i); - MPX_TRACE ("", p->var[i])); - p->token = TOKEN_VALUE; - DATA_PUSH (); - (*p->mpX_set) (SP, p->var[i]); - return 1; - } - - ERROR ("no token matched", MPEXPR_RESULT_PARSE_ERROR); - - done: - return 0; -} - - -/* control stack current top element */ -#define CP (p->control_stack + p->control_top) - -/* make sure there's room for another control element above current top */ -#define CONTROL_SPACE() \ - do { \ - if (p->control_top + 1 >= p->control_alloc) \ - { \ - TRACE (printf ("grow control stack from %d\n", p->control_alloc)); \ - REALLOC (p->control_stack, p->control_alloc, 20, \ - struct mpexpr_control_t); \ - } \ - } while (0) - -/* Push an operator on the control stack, claiming currently to have the - given number of args ready. Local variable "op" is used in case opptr is - a reference through CP. */ -#define CONTROL_PUSH(opptr,args) \ - do { \ - __gmp_const struct mpexpr_operator_t *op = opptr; \ - struct mpexpr_control_t *cp; \ - CONTROL_SPACE (); \ - p->control_top++; \ - ASSERT (p->control_top < p->control_alloc); \ - cp = CP; \ - cp->op = op; \ - cp->argcount = (args); \ - TRACE_CONTROL("control stack push:"); \ - } while (0) - -/* The special operator_done is never popped, so top>=0 will hold. */ -#define CONTROL_POP() \ - do { \ - p->control_top--; \ - ASSERT (p->control_top >= 0); \ - TRACE_CONTROL ("control stack pop:"); \ - } while (0) - -#define TRACE_CONTROL(str) \ - TRACE ({ \ - int i; \ - printf ("%s depth %d:", str, p->control_top); \ - for (i = 0; i <= p->control_top; i++) \ - printf (" \"%s\"(%d)", \ - p->control_stack[i].op->name, \ - p->control_stack[i].argcount); \ - printf ("\n"); \ - }); - - -#define LOOKAHEAD(prefix) \ - do { \ - if (! lookahead (p, prefix)) \ - goto done; \ - } while (0) - -#define CHECK_UI(n) \ - do { \ - if (! (*p->mpX_ulong_p) (n)) \ - ERROR ("operand doesn't fit ulong", MPEXPR_RESULT_NOT_UI); \ - } while (0) - -#define CHECK_ARGCOUNT(str,n) \ - do { \ - if (CP->argcount != (n)) \ - { \ - TRACE (printf ("wrong number of arguments for %s, got %d want %d", \ - str, CP->argcount, n)); \ - ERROR ("", MPEXPR_RESULT_PARSE_ERROR); \ - } \ - } while (0) - - -/* There's two basic states here. In both p->token is the next token. - - "another_expr" is when a whole expression should be parsed. This means a - literal or variable value possibly followed by an operator, or a function - or prefix operator followed by a further whole expression. - - "another_operator" is when an expression has been parsed and its value is - on the top of the data stack (SP) and an optional further postfix or - infix operator should be parsed. - - In "another_operator" precedences determine whether to push the operator - onto the control stack, or instead go to "apply_control" to reduce the - operator currently on top of the control stack. - - When an operator has both a prefix and postfix/infix form, a LOOKAHEAD() - for "another_expr" will seek the prefix form, a LOOKAHEAD() for - "another_operator" will seek the postfix/infix form. The grammar is - simple enough that the next state is known before reading the next token. - - Argument count checking guards against functions consuming the wrong - number of operands from the data stack. The same checks are applied to - operators, but will always pass since a UNARY or BINARY will only ever - parse with the correct operands. */ - -int -mpexpr_evaluate (struct mpexpr_parse_t *p) -{ - void *(*allocate_func) (size_t); - void *(*reallocate_func) (void *, size_t, size_t); - void (*free_func) (void *, size_t); - - mp_get_memory_functions (&allocate_func, &reallocate_func, &free_func); - - TRACE (printf ("mpexpr_evaluate() base %d \"%.*s\"\n", - p->base, (int) p->elen, p->e)); - - /* "done" is a special sentinel at the bottom of the control stack, - precedence -1 is lower than any normal operator. */ - { - static __gmp_const struct mpexpr_operator_t operator_done - = { "DONE", NULL, MPEXPR_TYPE_DONE, -1 }; - - p->control_alloc = 20; - p->control_stack = ALLOCATE_FUNC_TYPE (p->control_alloc, - struct mpexpr_control_t); - p->control_top = 0; - CP->op = &operator_done; - CP->argcount = 1; - } - - p->data_inited = 0; - p->data_alloc = 20; - p->data_stack = ALLOCATE_FUNC_TYPE (p->data_alloc, union mpX_t); - p->data_top = -1; - - p->error_code = MPEXPR_RESULT_OK; - - - another_expr_lookahead: - LOOKAHEAD (MPEXPR_TYPE_PREFIX); - TRACE (printf ("another expr\n")); - - /*another_expr:*/ - switch (p->token) { - case TOKEN_VALUE: - goto another_operator_lookahead; - - case TOKEN_OPERATOR: - TRACE (printf ("operator %s\n", p->token_op->name)); - if (! (p->token_op->type & MPEXPR_TYPE_PREFIX)) - ERROR ("expected a prefix operator", MPEXPR_RESULT_PARSE_ERROR); - - CONTROL_PUSH (p->token_op, 1); - goto another_expr_lookahead; - - case TOKEN_FUNCTION: - CONTROL_PUSH (p->token_op, 1); - - if (p->token_op->type & MPEXPR_TYPE_CONSTANT) - goto apply_control_lookahead; - - LOOKAHEAD (MPEXPR_TYPE_PREFIX); - if (! (p->token == TOKEN_OPERATOR - && p->token_op->type == MPEXPR_TYPE_OPENPAREN)) - ERROR ("expected open paren for function", MPEXPR_RESULT_PARSE_ERROR); - - TRACE (printf ("open paren for function \"%s\"\n", CP->op->name)); - - if ((CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) == MPEXPR_TYPE_NARY(0)) - { - LOOKAHEAD (0); - if (! (p->token == TOKEN_OPERATOR - && p->token_op->type == MPEXPR_TYPE_CLOSEPAREN)) - ERROR ("expected close paren for 0ary function", - MPEXPR_RESULT_PARSE_ERROR); - goto apply_control_lookahead; - } - - goto another_expr_lookahead; - } - ERROR ("unrecognised start of expression", MPEXPR_RESULT_PARSE_ERROR); - - - another_operator_lookahead: - LOOKAHEAD (0); - another_operator: - TRACE (printf ("another operator maybe: %s\n", TOKEN_NAME(p->token))); - - switch (p->token) { - case TOKEN_EOF: - goto apply_control; - - case TOKEN_OPERATOR: - /* The next operator is compared to the one on top of the control stack. - If the next is lower precedence, or the same precedence and not - right-associative, then reduce using the control stack and look at - the next operator again later. */ - -#define PRECEDENCE_TEST_REDUCE(tprec,cprec,ttype,ctype) \ - ((tprec) < (cprec) \ - || ((tprec) == (cprec) && ! ((ttype) & MPEXPR_TYPE_RIGHTASSOC))) - - if (PRECEDENCE_TEST_REDUCE (p->token_op->precedence, CP->op->precedence, - p->token_op->type, CP->op->type)) - { - TRACE (printf ("defer operator: %s (prec %d vs %d, type 0x%X)\n", - p->token_op->name, - p->token_op->precedence, CP->op->precedence, - p->token_op->type)); - goto apply_control; - } - - /* An argsep is a binary operator, but is never pushed on the control - stack, it just accumulates an extra argument for a function. */ - if (p->token_op->type == MPEXPR_TYPE_ARGSEP) - { - if (CP->op->precedence != 0) - ERROR ("ARGSEP not in a function call", MPEXPR_RESULT_PARSE_ERROR); - - TRACE (printf ("argsep for function \"%s\"(%d)\n", - CP->op->name, CP->argcount)); - -#define IS_PAIRWISE(type) \ - (((type) & (MPEXPR_TYPE_MASK_ARGCOUNT | MPEXPR_TYPE_PAIRWISE)) \ - == (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_PAIRWISE)) - - if (IS_PAIRWISE (CP->op->type) && CP->argcount >= 2) - { - TRACE (printf (" will reduce pairwise now\n")); - CP->argcount--; - CONTROL_PUSH (CP->op, 2); - goto apply_control; - } - - CP->argcount++; - goto another_expr_lookahead; - } - - switch (p->token_op->type & MPEXPR_TYPE_MASK_ARGCOUNT) { - case MPEXPR_TYPE_NARY(1): - /* Postfix unary operators can always be applied immediately. The - easiest way to do this is just push it on the control stack and go - to the normal control stack reduction code. */ - - TRACE (printf ("postfix unary operator: %s\n", p->token_op->name)); - if (p->token_op->type & MPEXPR_TYPE_PREFIX) - ERROR ("prefix unary operator used postfix", - MPEXPR_RESULT_PARSE_ERROR); - CONTROL_PUSH (p->token_op, 1); - goto apply_control_lookahead; - - case MPEXPR_TYPE_NARY(2): - CONTROL_PUSH (p->token_op, 2); - goto another_expr_lookahead; - - case MPEXPR_TYPE_NARY(3): - CONTROL_PUSH (p->token_op, 1); - goto another_expr_lookahead; - } - - TRACE (printf ("unrecognised operator \"%s\" type: 0x%X", - CP->op->name, CP->op->type)); - ERROR ("", MPEXPR_RESULT_PARSE_ERROR); - break; - - default: - TRACE (printf ("expecting an operator, got token %d", p->token)); - ERROR ("", MPEXPR_RESULT_PARSE_ERROR); - } - - - apply_control_lookahead: - LOOKAHEAD (0); - apply_control: - /* Apply the top element CP of the control stack. Data values are SP, - SP-1, etc. Result is left as stack top SP after popping consumed - values. - - The use of sp as a duplicate of SP will help compilers that can't - otherwise recognise the various uses of SP as common subexpressions. */ - - TRACE (printf ("apply control: nested %d, \"%s\" 0x%X, %d args\n", - p->control_top, CP->op->name, CP->op->type, CP->argcount)); - - TRACE (printf ("apply 0x%X-ary\n", - CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT)); - switch (CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) { - case MPEXPR_TYPE_NARY(0): - { - mpX_ptr sp; - DATA_SPACE (); - DATA_PUSH (); - sp = SP; - switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) { - case 0: - (* (mpexpr_fun_0ary_t) CP->op->fun) (sp); - break; - case MPEXPR_TYPE_RESULT_INT: - (*p->mpX_set_si) (sp, (long) (* (mpexpr_fun_i_0ary_t) CP->op->fun) ()); - break; - default: - ERROR ("unrecognised 0ary argument calling style", - MPEXPR_RESULT_BAD_TABLE); - } - } - break; - - case MPEXPR_TYPE_NARY(1): - { - mpX_ptr sp = SP; - CHECK_ARGCOUNT ("unary", 1); - TRACE (MPX_TRACE ("before", sp)); - - switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) { - case 0: - /* not a special */ - break; - - case MPEXPR_TYPE_DONE & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special done\n")); - goto done; - - case MPEXPR_TYPE_LOGICAL_NOT & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special logical not\n")); - (*p->mpX_set_si) - (sp, (long) ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) == 0)); - goto apply_control_done; - - case MPEXPR_TYPE_CLOSEPAREN & MPEXPR_TYPE_MASK_SPECIAL: - CONTROL_POP (); - if (CP->op->type == MPEXPR_TYPE_OPENPAREN) - { - TRACE (printf ("close paren matching open paren\n")); - CONTROL_POP (); - goto another_operator; - } - if (CP->op->precedence == 0) - { - TRACE (printf ("close paren for function\n")); - goto apply_control; - } - ERROR ("unexpected close paren", MPEXPR_RESULT_PARSE_ERROR); - - default: - TRACE (printf ("unrecognised special unary operator 0x%X", - CP->op->type & MPEXPR_TYPE_MASK_SPECIAL)); - ERROR ("", MPEXPR_RESULT_BAD_TABLE); - } - - switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) { - case 0: - (* (mpexpr_fun_unary_t) CP->op->fun) (sp, sp); - break; - case MPEXPR_TYPE_LAST_UI: - CHECK_UI (sp); - (* (mpexpr_fun_unary_ui_t) CP->op->fun) - (sp, (*p->mpX_get_ui) (sp)); - break; - case MPEXPR_TYPE_RESULT_INT: - (*p->mpX_set_si) - (sp, (long) (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)); - break; - case MPEXPR_TYPE_RESULT_INT | MPEXPR_TYPE_LAST_UI: - CHECK_UI (sp); - (*p->mpX_set_si) - (sp, - (long) (* (mpexpr_fun_i_unary_ui_t) CP->op->fun) - ((*p->mpX_get_ui) (sp))); - break; - default: - ERROR ("unrecognised unary argument calling style", - MPEXPR_RESULT_BAD_TABLE); - } - } - break; - - case MPEXPR_TYPE_NARY(2): - { - mpX_ptr sp; - - /* pairwise functions are allowed to have just one argument */ - if ((CP->op->type & MPEXPR_TYPE_PAIRWISE) - && CP->op->precedence == 0 - && CP->argcount == 1) - goto apply_control_done; - - CHECK_ARGCOUNT ("binary", 2); - DATA_POP (1); - sp = SP; - TRACE (MPX_TRACE ("lhs", sp); - MPX_TRACE ("rhs", sp+1)); - - if (CP->op->type & MPEXPR_TYPE_MASK_CMP) - { - int type = CP->op->type; - int cmp = (* (mpexpr_fun_i_binary_t) CP->op->fun) - (sp, sp+1); - (*p->mpX_set_si) - (sp, - (long) - (( (cmp < 0) & ((type & MPEXPR_TYPE_MASK_CMP_LT) != 0)) - | ((cmp == 0) & ((type & MPEXPR_TYPE_MASK_CMP_EQ) != 0)) - | ((cmp > 0) & ((type & MPEXPR_TYPE_MASK_CMP_GT) != 0)))); - goto apply_control_done; - } - - switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) { - case 0: - /* not a special */ - break; - - case MPEXPR_TYPE_QUESTION & MPEXPR_TYPE_MASK_SPECIAL: - ERROR ("'?' without ':'", MPEXPR_RESULT_PARSE_ERROR); - - case MPEXPR_TYPE_COLON & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special colon\n")); - CONTROL_POP (); - if (CP->op->type != MPEXPR_TYPE_QUESTION) - ERROR ("':' without '?'", MPEXPR_RESULT_PARSE_ERROR); - - CP->argcount--; - DATA_POP (1); - sp--; - TRACE (MPX_TRACE ("query", sp); - MPX_TRACE ("true", sp+1); - MPX_TRACE ("false", sp+2)); - (*p->mpX_set) - (sp, (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) - ? sp+1 : sp+2); - goto apply_control_done; - - case MPEXPR_TYPE_LOGICAL_AND & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special logical and\n")); - (*p->mpX_set_si) - (sp, - (long) - ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) - && (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1))); - goto apply_control_done; - - case MPEXPR_TYPE_LOGICAL_OR & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special logical and\n")); - (*p->mpX_set_si) - (sp, - (long) - ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) - || (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1))); - goto apply_control_done; - - case MPEXPR_TYPE_MAX & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special max\n")); - if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) < 0) - (*p->mpX_swap) (sp, sp+1); - goto apply_control_done; - case MPEXPR_TYPE_MIN & MPEXPR_TYPE_MASK_SPECIAL: - TRACE (printf ("special min\n")); - if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) > 0) - (*p->mpX_swap) (sp, sp+1); - goto apply_control_done; - - default: - ERROR ("unrecognised special binary operator", - MPEXPR_RESULT_BAD_TABLE); - } - - switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) { - case 0: - (* (mpexpr_fun_binary_t) CP->op->fun) (sp, sp, sp+1); - break; - case MPEXPR_TYPE_LAST_UI: - CHECK_UI (sp+1); - (* (mpexpr_fun_binary_ui_t) CP->op->fun) - (sp, sp, (*p->mpX_get_ui) (sp+1)); - break; - case MPEXPR_TYPE_RESULT_INT: - (*p->mpX_set_si) - (sp, - (long) (* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1)); - break; - case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT: - CHECK_UI (sp+1); - (*p->mpX_set_si) - (sp, - (long) (* (mpexpr_fun_i_binary_ui_t) CP->op->fun) - (sp, (*p->mpX_get_ui) (sp+1))); - break; - default: - ERROR ("unrecognised binary argument calling style", - MPEXPR_RESULT_BAD_TABLE); - } - } - break; - - case MPEXPR_TYPE_NARY(3): - { - mpX_ptr sp; - - CHECK_ARGCOUNT ("ternary", 3); - DATA_POP (2); - sp = SP; - TRACE (MPX_TRACE ("arg1", sp); - MPX_TRACE ("arg2", sp+1); - MPX_TRACE ("arg3", sp+1)); - - switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) { - case 0: - (* (mpexpr_fun_ternary_t) CP->op->fun) (sp, sp, sp+1, sp+2); - break; - case MPEXPR_TYPE_LAST_UI: - CHECK_UI (sp+2); - (* (mpexpr_fun_ternary_ui_t) CP->op->fun) - (sp, sp, sp+1, (*p->mpX_get_ui) (sp+2)); - break; - case MPEXPR_TYPE_RESULT_INT: - (*p->mpX_set_si) - (sp, - (long) (* (mpexpr_fun_i_ternary_t) CP->op->fun) - (sp, sp+1, sp+2)); - break; - case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT: - CHECK_UI (sp+2); - (*p->mpX_set_si) - (sp, - (long) (* (mpexpr_fun_i_ternary_ui_t) CP->op->fun) - (sp, sp+1, (*p->mpX_get_ui) (sp+2))); - break; - default: - ERROR ("unrecognised binary argument calling style", - MPEXPR_RESULT_BAD_TABLE); - } - } - break; - - default: - TRACE (printf ("unrecognised operator type: 0x%X\n", CP->op->type)); - ERROR ("", MPEXPR_RESULT_PARSE_ERROR); - } - - apply_control_done: - TRACE (MPX_TRACE ("result", SP)); - CONTROL_POP (); - goto another_operator; - - done: - if (p->error_code == MPEXPR_RESULT_OK) - { - if (p->data_top != 0) - { - TRACE (printf ("data stack want top at 0, got %d\n", p->data_top)); - p->error_code = MPEXPR_RESULT_PARSE_ERROR; - } - else - (*p->mpX_set_or_swap) (p->res, SP); - } - - { - int i; - for (i = 0; i < p->data_inited; i++) - { - TRACE (printf ("clear %d\n", i)); - (*p->mpX_clear) (p->data_stack+i); - } - } - - FREE_FUNC_TYPE (p->data_stack, p->data_alloc, union mpX_t); - FREE_FUNC_TYPE (p->control_stack, p->control_alloc, struct mpexpr_control_t); - - return p->error_code; -} diff --git a/src/gmp/demos/expr/expr.h b/src/gmp/demos/expr/expr.h deleted file mode 100644 index 4a4743edd..000000000 --- a/src/gmp/demos/expr/expr.h +++ /dev/null @@ -1,131 +0,0 @@ -/* Header for expression evaluation. - -Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - - -#ifndef __EXPR_H__ -#define __EXPR_H__ - -#define MPEXPR_RESULT_OK 0 -#define MPEXPR_RESULT_BAD_VARIABLE 1 -#define MPEXPR_RESULT_BAD_TABLE 2 -#define MPEXPR_RESULT_PARSE_ERROR 3 -#define MPEXPR_RESULT_NOT_UI 4 - - -/* basic types */ -#define MPEXPR_TYPE_NARY(n) ((n) * 0x0100) -#define MPEXPR_TYPE_MASK_ARGCOUNT MPEXPR_TYPE_NARY(0xF) -#define MPEXPR_TYPE_0ARY MPEXPR_TYPE_NARY(0) -#define MPEXPR_TYPE_UNARY MPEXPR_TYPE_NARY(1) -#define MPEXPR_TYPE_BINARY MPEXPR_TYPE_NARY(2) -#define MPEXPR_TYPE_TERNARY MPEXPR_TYPE_NARY(3) - -/* options for all */ -#define MPEXPR_TYPE_LAST_UI 0x0010 -#define MPEXPR_TYPE_RESULT_INT 0x0020 -#define MPEXPR_TYPE_MASK_ARGSTYLE 0x0030 - -#define MPEXPR_TYPE_UNARY_UI (MPEXPR_TYPE_UNARY | MPEXPR_TYPE_LAST_UI) -#define MPEXPR_TYPE_I_UNARY (MPEXPR_TYPE_UNARY | MPEXPR_TYPE_RESULT_INT) -#define MPEXPR_TYPE_I_UNARY_UI (MPEXPR_TYPE_I_UNARY | MPEXPR_TYPE_LAST_UI) -#define MPEXPR_TYPE_BINARY_UI (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_LAST_UI) -#define MPEXPR_TYPE_I_BINARY (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_RESULT_INT) -#define MPEXPR_TYPE_I_BINARY_UI (MPEXPR_TYPE_I_BINARY| MPEXPR_TYPE_LAST_UI) -#define MPEXPR_TYPE_TERNARY_UI (MPEXPR_TYPE_TERNARY | MPEXPR_TYPE_LAST_UI) -#define MPEXPR_TYPE_I_TERNARY (MPEXPR_TYPE_TERNARY | MPEXPR_TYPE_RESULT_INT) -#define MPEXPR_TYPE_I_TERNARY_UI (MPEXPR_TYPE_I_TERNARY|MPEXPR_TYPE_LAST_UI) - -/* 0ary with options */ -#define MPEXPR_TYPE_CONSTANT (MPEXPR_TYPE_0ARY | 0x0040) - -/* unary options */ -#define MPEXPR_TYPE_PREFIX 0x0040 - -/* binary options */ -#define MPEXPR_TYPE_RIGHTASSOC 0x0040 -#define MPEXPR_TYPE_PAIRWISE 0x0080 - -#define MPEXPR_TYPE_MASK_SPECIAL 0x000F - -/* unary specials */ -#define MPEXPR_TYPE_NEW_TABLE (MPEXPR_TYPE_UNARY | 0x001) -#define MPEXPR_TYPE_DONE (MPEXPR_TYPE_UNARY | 0x002) -#define MPEXPR_TYPE_VARIABLE (MPEXPR_TYPE_UNARY | 0x003) -#define MPEXPR_TYPE_LOGICAL_NOT (MPEXPR_TYPE_UNARY | 0x004) -#define MPEXPR_TYPE_CLOSEPAREN (MPEXPR_TYPE_UNARY | 0x005) -#define MPEXPR_TYPE_OPENPAREN (MPEXPR_TYPE_CLOSEPAREN | MPEXPR_TYPE_PREFIX) - -/* binary specials */ -#define MPEXPR_TYPE_LOGICAL_AND (MPEXPR_TYPE_BINARY | 0x001) -#define MPEXPR_TYPE_LOGICAL_OR (MPEXPR_TYPE_BINARY | 0x002) -#define MPEXPR_TYPE_ARGSEP (MPEXPR_TYPE_BINARY | 0x003) -#define MPEXPR_TYPE_QUESTION (MPEXPR_TYPE_BINARY | 0x004) -#define MPEXPR_TYPE_COLON (MPEXPR_TYPE_BINARY | 0x005) -#define MPEXPR_TYPE_MAX (MPEXPR_TYPE_BINARY | 0x006) -#define MPEXPR_TYPE_MIN (MPEXPR_TYPE_BINARY | 0x007) -#define MPEXPR_TYPE_MASK_CMP 0x008 -#define MPEXPR_TYPE_MASK_CMP_LT 0x001 -#define MPEXPR_TYPE_MASK_CMP_EQ 0x002 -#define MPEXPR_TYPE_MASK_CMP_GT 0x004 -#define MPEXPR_TYPE_CMP_LT (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_MASK_CMP \ - | MPEXPR_TYPE_MASK_CMP_LT) -#define MPEXPR_TYPE_CMP_EQ (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_MASK_CMP \ - | MPEXPR_TYPE_MASK_CMP_EQ) -#define MPEXPR_TYPE_CMP_GT (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_MASK_CMP \ - | MPEXPR_TYPE_MASK_CMP_GT) -#define MPEXPR_TYPE_CMP_LE (MPEXPR_TYPE_CMP_LT | MPEXPR_TYPE_MASK_CMP_EQ) -#define MPEXPR_TYPE_CMP_NE (MPEXPR_TYPE_CMP_LT | MPEXPR_TYPE_MASK_CMP_GT) -#define MPEXPR_TYPE_CMP_GE (MPEXPR_TYPE_CMP_GT | MPEXPR_TYPE_MASK_CMP_EQ) - -/* parse options */ -#define MPEXPR_TYPE_WHOLEWORD 0x1000 -#define MPEXPR_TYPE_OPERATOR 0x2000 - - -typedef void (*mpexpr_fun_t) __GMP_PROTO ((void)); - -struct mpexpr_operator_t { - __gmp_const char *name; - mpexpr_fun_t fun; - int type; - int precedence; -}; - - -int mpf_expr_a __GMP_PROTO ((__gmp_const struct mpexpr_operator_t *table, - mpf_ptr res, int base, unsigned long prec, - __gmp_const char *e, size_t elen, - mpf_srcptr var[26])); -int mpf_expr __GMP_PROTO ((mpf_ptr res, int base, __gmp_const char *e, ...)); - -int mpq_expr_a __GMP_PROTO ((__gmp_const struct mpexpr_operator_t *table, - mpq_ptr res, int base, - __gmp_const char *e, size_t elen, - mpq_srcptr var[26])); -int mpq_expr __GMP_PROTO ((mpq_ptr res, int base, __gmp_const char *e, ...)); - -int mpz_expr_a __GMP_PROTO ((__gmp_const struct mpexpr_operator_t *table, - mpz_ptr res, int base, - __gmp_const char *e, size_t elen, - mpz_srcptr var[26])); -int mpz_expr __GMP_PROTO ((mpz_ptr res, int base, __gmp_const char *e, ...)); - -#endif diff --git a/src/gmp/demos/expr/exprf.c b/src/gmp/demos/expr/exprf.c deleted file mode 100644 index 4f8e777df..000000000 --- a/src/gmp/demos/expr/exprf.c +++ /dev/null @@ -1,129 +0,0 @@ -/* mpf expression evaluation - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - -#include -#include -#include "gmp.h" -#include "expr-impl.h" - - -/* Change this to "#define TRACE(x) x" to get some traces. */ -#define TRACE(x) - - -static int -e_mpf_sgn (mpf_srcptr x) -{ - return mpf_sgn (x); -} - - -static __gmp_const struct mpexpr_operator_t _mpf_expr_standard_table[] = { - - { "**", (mpexpr_fun_t) mpf_pow_ui, - MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 }, - - { "!", (mpexpr_fun_t) e_mpf_sgn, - MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 }, - { "-", (mpexpr_fun_t) mpf_neg, - MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, - - { "*", (mpexpr_fun_t) mpf_mul, MPEXPR_TYPE_BINARY, 200 }, - { "/", (mpexpr_fun_t) mpf_div, MPEXPR_TYPE_BINARY, 200 }, - - { "+", (mpexpr_fun_t) mpf_add, MPEXPR_TYPE_BINARY, 190 }, - { "-", (mpexpr_fun_t) mpf_sub, MPEXPR_TYPE_BINARY, 190 }, - - { "<<", (mpexpr_fun_t) mpf_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, - { ">>", (mpexpr_fun_t) mpf_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, - - { "<=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_LE, 170 }, - { "<", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_LT, 170 }, - { ">=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_GE, 170 }, - { ">", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_GT, 170 }, - - { "==", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_EQ, 160 }, - { "!=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_NE, 160 }, - - { "&&", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 }, - { "||", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 }, - - { ":", NULL, MPEXPR_TYPE_COLON, 101 }, - { "?", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_QUESTION, 100 }, - - { ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 }, - { "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 }, - { ",", NULL, MPEXPR_TYPE_ARGSEP, 2 }, - { "$", NULL, MPEXPR_TYPE_VARIABLE, 1 }, - - { "abs", (mpexpr_fun_t) mpf_abs, MPEXPR_TYPE_UNARY }, - { "ceil", (mpexpr_fun_t) mpf_ceil, MPEXPR_TYPE_UNARY }, - { "cmp", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_I_BINARY }, - { "eq", (mpexpr_fun_t) mpf_eq, MPEXPR_TYPE_I_TERNARY_UI }, - { "floor", (mpexpr_fun_t) mpf_floor, MPEXPR_TYPE_UNARY }, - { "integer_p",(mpexpr_fun_t) mpf_integer_p, MPEXPR_TYPE_I_UNARY }, - { "max", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE }, - { "min", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE }, - { "reldiff", (mpexpr_fun_t) mpf_reldiff, MPEXPR_TYPE_BINARY }, - { "sgn", (mpexpr_fun_t) e_mpf_sgn, MPEXPR_TYPE_I_UNARY }, - { "sqrt", (mpexpr_fun_t) mpf_sqrt, MPEXPR_TYPE_UNARY }, - { "trunc", (mpexpr_fun_t) mpf_trunc, MPEXPR_TYPE_UNARY }, - - { NULL } -}; - -__gmp_const struct mpexpr_operator_t * __gmp_const mpf_expr_standard_table -= _mpf_expr_standard_table; - - -int -#if HAVE_STDARG -mpf_expr (mpf_ptr res, int base, __gmp_const char *e, ...) -#else -mpf_expr (va_alist) - va_dcl -#endif -{ - mpf_srcptr var[MPEXPR_VARIABLES]; - va_list ap; - int ret; -#if HAVE_STDARG - va_start (ap, e); -#else - mpf_ptr res; - int base; - __gmp_const char *e; - va_start (ap); - res = va_arg (ap, mpf_ptr); - base = va_arg (ap, int); - e = va_arg (ap, __gmp_const char *); -#endif - - TRACE (printf ("mpf_expr(): base %d, %s\n", base, e)); - ret = mpexpr_va_to_var ((void **) var, ap); - va_end (ap); - - if (ret != MPEXPR_RESULT_OK) - return ret; - - return mpf_expr_a (mpf_expr_standard_table, res, base, - mpf_get_prec (res), e, strlen(e), var); -} diff --git a/src/gmp/demos/expr/exprfa.c b/src/gmp/demos/expr/exprfa.c deleted file mode 100644 index f40e08ecd..000000000 --- a/src/gmp/demos/expr/exprfa.c +++ /dev/null @@ -1,182 +0,0 @@ -/* mpf expression evaluation - -Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - - -/* Future: Bitwise "&", "|" and "&" could be done, if desired. Not sure - those functions would be much value though. */ - - -#include -#include -#include - -#include "gmp.h" -#include "expr-impl.h" - - -/* Change this to "#define TRACE(x) x" to get some traces. */ -#define TRACE(x) - - -static size_t -e_mpf_number (mpf_ptr res, __gmp_const char *e, size_t elen, int base) -{ - char *edup; - size_t i, ret, extra=0; - int mant_base, exp_base; - void *(*allocate_func) (size_t); - void (*free_func) (void *, size_t); - - TRACE (printf ("mpf_number base=%d \"%.*s\"\n", base, (int) elen, e)); - - /* mpf_set_str doesn't currently accept 0x for hex in base==0, so do it - here instead. FIXME: Would prefer to let mpf_set_str handle this. */ - if (base == 0 && elen >= 2 && e[0] == '0' && (e[1] == 'x' || e[1] == 'X')) - { - base = 16; - extra = 2; - e += extra; - elen -= extra; - } - - if (base == 0) - mant_base = 10; - else if (base < 0) - mant_base = -base; - else - mant_base = base; - - /* exponent in decimal if base is negative */ - if (base < 0) - exp_base = 10; - else if (base == 0) - exp_base = 10; - else - exp_base = base; - -#define IS_EXPONENT(c) \ - (c == '@' || (base <= 10 && base >= -10 && (e[i] == 'e' || e[i] == 'E'))) - - i = 0; - for (;;) - { - if (i >= elen) - goto parsed; - if (e[i] == '.') - break; - if (IS_EXPONENT (e[i])) - goto exponent; - if (! isasciidigit_in_base (e[i], mant_base)) - goto parsed; - i++; - } - - /* fraction */ - i++; - for (;;) - { - if (i >= elen) - goto parsed; - if (IS_EXPONENT (e[i])) - goto exponent; - if (! isasciidigit_in_base (e[i], mant_base)) - goto parsed; - i++; - } - - exponent: - i++; - if (i >= elen) - goto parsed; - if (e[i] == '-') - i++; - for (;;) - { - if (i >= elen) - goto parsed; - if (! isasciidigit_in_base (e[i], exp_base)) - break; - i++; - } - - parsed: - TRACE (printf (" parsed i=%u \"%.*s\"\n", i, (int) i, e)); - - mp_get_memory_functions (&allocate_func, NULL, &free_func); - edup = (*allocate_func) (i+1); - memcpy (edup, e, i); - edup[i] = '\0'; - - if (mpf_set_str (res, edup, base) == 0) - ret = i + extra; - else - ret = 0; - - (*free_func) (edup, i+1); - return ret; -} - -static int -e_mpf_ulong_p (mpf_srcptr f) -{ - return mpf_integer_p (f) && mpf_fits_ulong_p (f); -} - -/* Don't want to change the precision of w, can only do an actual swap when - w and x have the same precision. */ -static void -e_mpf_set_or_swap (mpf_ptr w, mpf_ptr x) -{ - if (mpf_get_prec (w) == mpf_get_prec (x)) - mpf_swap (w, x); - else - mpf_set (w, x); -} - - -int -mpf_expr_a (__gmp_const struct mpexpr_operator_t *table, - mpf_ptr res, int base, unsigned long prec, - __gmp_const char *e, size_t elen, - mpf_srcptr var[26]) -{ - struct mpexpr_parse_t p; - - p.table = table; - p.res = (mpX_ptr) res; - p.base = base; - p.prec = prec; - p.e = e; - p.elen = elen; - p.var = (mpX_srcptr *) var; - - p.mpX_clear = (mpexpr_fun_one_t) mpf_clear; - p.mpX_ulong_p = (mpexpr_fun_i_unary_t) e_mpf_ulong_p; - p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpf_get_ui; - p.mpX_init = (mpexpr_fun_unary_ui_t) mpf_init2; - p.mpX_number = (mpexpr_fun_number_t) e_mpf_number; - p.mpX_set = (mpexpr_fun_unary_t) mpf_set; - p.mpX_set_or_swap = (mpexpr_fun_unary_t) e_mpf_set_or_swap; - p.mpX_set_si = (mpexpr_fun_set_si_t) mpf_set_si; - p.mpX_swap = (mpexpr_fun_swap_t) mpf_swap; - - return mpexpr_evaluate (&p); -} diff --git a/src/gmp/demos/expr/exprq.c b/src/gmp/demos/expr/exprq.c deleted file mode 100644 index 90d61aea5..000000000 --- a/src/gmp/demos/expr/exprq.c +++ /dev/null @@ -1,161 +0,0 @@ -/* mpq expression evaluation - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - -#include -#include -#include "gmp.h" -#include "expr-impl.h" - - -/* Change this to "#define TRACE(x) x" to get some traces. */ -#define TRACE(x) - - -static void -e_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e) -{ - mpz_pow_ui (mpq_numref(r), mpq_numref(b), e); - mpz_pow_ui (mpq_denref(r), mpq_denref(b), e); -} - -/* Wrapped because mpq_sgn is a macro. */ -static int -e_mpq_sgn (mpq_srcptr x) -{ - return mpq_sgn (x); -} - -/* Wrapped because mpq_equal only guarantees a non-zero return, whereas we - want 1 or 0 for == and !=. */ -static int -e_mpq_equal (mpq_srcptr x, mpq_srcptr y) -{ - return mpq_equal (x, y) != 0; -} -static int -e_mpq_notequal (mpq_srcptr x, mpq_srcptr y) -{ - return ! mpq_equal (x, y); -} - -static void -e_mpq_num (mpq_ptr w, mpq_srcptr x) -{ - if (w != x) - mpz_set (mpq_numref(w), mpq_numref(x)); - mpz_set_ui (mpq_denref(w), 1L); -} -static void -e_mpq_den (mpq_ptr w, mpq_srcptr x) -{ - if (w == x) - mpz_swap (mpq_numref(w), mpq_denref(w)); - else - mpz_set (mpq_numref(w), mpq_denref(x)); - mpz_set_ui (mpq_denref(w), 1L); -} - - -static __gmp_const struct mpexpr_operator_t _mpq_expr_standard_table[] = { - - { "**", (mpexpr_fun_t) e_mpq_pow_ui, - MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 }, - - { "!", (mpexpr_fun_t) e_mpq_sgn, - MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 }, - { "-", (mpexpr_fun_t) mpq_neg, - MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, - - { "*", (mpexpr_fun_t) mpq_mul, MPEXPR_TYPE_BINARY, 200 }, - { "/", (mpexpr_fun_t) mpq_div, MPEXPR_TYPE_BINARY, 200 }, - - { "+", (mpexpr_fun_t) mpq_add, MPEXPR_TYPE_BINARY, 190 }, - { "-", (mpexpr_fun_t) mpq_sub, MPEXPR_TYPE_BINARY, 190 }, - - { "<<", (mpexpr_fun_t) mpq_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, - { ">>", (mpexpr_fun_t) mpq_div_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, - - { "<=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LE, 170 }, - { "<", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_LT, 170 }, - { ">=", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GE, 170 }, - { ">", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_CMP_GT, 170 }, - - { "==", (mpexpr_fun_t) e_mpq_equal, MPEXPR_TYPE_I_BINARY, 160 }, - { "!=", (mpexpr_fun_t) e_mpq_notequal, MPEXPR_TYPE_I_BINARY, 160 }, - - { "&&", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 }, - { "||", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 }, - - { ":", NULL, MPEXPR_TYPE_COLON, 101 }, - { "?", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_QUESTION, 100 }, - - { ")", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_CLOSEPAREN, 4 }, - { "(", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_OPENPAREN, 3 }, - { ",", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_ARGSEP, 2 }, - { "$", NULL, MPEXPR_TYPE_VARIABLE, 1 }, - - { "abs", (mpexpr_fun_t) mpq_abs, MPEXPR_TYPE_UNARY }, - { "cmp", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_I_BINARY }, - { "den", (mpexpr_fun_t) e_mpq_den, MPEXPR_TYPE_UNARY }, - { "max", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE }, - { "min", (mpexpr_fun_t) mpq_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE }, - { "num", (mpexpr_fun_t) e_mpq_num, MPEXPR_TYPE_UNARY }, - { "sgn", (mpexpr_fun_t) e_mpq_sgn, MPEXPR_TYPE_I_UNARY }, - - { NULL } -}; - -__gmp_const struct mpexpr_operator_t * __gmp_const mpq_expr_standard_table -= _mpq_expr_standard_table; - - -int -#if HAVE_STDARG -mpq_expr (mpq_ptr res, int base, __gmp_const char *e, ...) -#else -mpq_expr (va_alist) - va_dcl -#endif -{ - mpq_srcptr var[MPEXPR_VARIABLES]; - va_list ap; - int ret; -#if HAVE_STDARG - va_start (ap, e); -#else - mpq_ptr res; - int base; - __gmp_const char *e; - va_start (ap); - res = va_arg (ap, mpq_ptr); - base = va_arg (ap, int); - e = va_arg (ap, __gmp_const char *); -#endif - - TRACE (printf ("mpq_expr(): base %d, %s\n", base, e)); - ret = mpexpr_va_to_var ((void **) var, ap); - va_end (ap); - - if (ret != MPEXPR_RESULT_OK) - return ret; - - return mpq_expr_a (mpq_expr_standard_table, res, base, e, strlen(e), var); -} diff --git a/src/gmp/demos/expr/exprqa.c b/src/gmp/demos/expr/exprqa.c deleted file mode 100644 index 70836fd15..000000000 --- a/src/gmp/demos/expr/exprqa.c +++ /dev/null @@ -1,91 +0,0 @@ -/* mpq expression evaluation - -Copyright 2000, 2001, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - - -#include -#include "gmp.h" -#include "expr-impl.h" - - -static int -e_mpq_ulong_p (mpq_srcptr q) -{ - return mpz_fits_ulong_p (mpq_numref (q)) - && mpz_cmp_ui (mpq_denref (q), 1L) == 0; -} - -/* get value as a ui, on the assumption it fits */ -static int -e_mpq_get_ui_fits (mpq_srcptr q) -{ - return mpz_get_ui (mpq_numref (q)); -} - -static void -e_mpq_set_si1 (mpq_ptr q, long num) -{ - mpq_set_si (q, num, 1L); -} - -/* The same as mpz, but putting the result in the numerator. Negatives and - fractions aren't parsed here because '-' and '/' are operators. */ -static size_t -e_mpq_number (mpq_ptr res, __gmp_const char *e, size_t elen, int base) -{ - mpz_set_ui (mpq_denref (res), 1L); - return mpexpr_mpz_number (mpq_numref (res), e, elen, base); -} - - -/* ignoring prec */ -static void -e_mpq_init (mpq_ptr q, unsigned long prec) -{ - mpq_init (q); -} - -int -mpq_expr_a (__gmp_const struct mpexpr_operator_t *table, - mpq_ptr res, int base, - __gmp_const char *e, size_t elen, - mpq_srcptr var[26]) -{ - struct mpexpr_parse_t p; - - p.table = table; - p.res = (mpX_ptr) res; - p.base = base; - p.e = e; - p.elen = elen; - p.var = (mpX_srcptr *) var; - - p.mpX_clear = (mpexpr_fun_one_t) mpq_clear; - p.mpX_ulong_p = (mpexpr_fun_i_unary_t) e_mpq_ulong_p; - p.mpX_get_ui = (mpexpr_fun_get_ui_t) e_mpq_get_ui_fits; - p.mpX_init = (mpexpr_fun_unary_ui_t) e_mpq_init; - p.mpX_number = (mpexpr_fun_number_t) e_mpq_number; - p.mpX_set = (mpexpr_fun_unary_t) mpq_set; - p.mpX_set_or_swap = (mpexpr_fun_unary_t) mpq_swap; - p.mpX_set_si = (mpexpr_fun_set_si_t) e_mpq_set_si1; - p.mpX_swap = (mpexpr_fun_swap_t) mpq_swap; - - return mpexpr_evaluate (&p); -} diff --git a/src/gmp/demos/expr/exprv.c b/src/gmp/demos/expr/exprv.c deleted file mode 100644 index 90ff4696f..000000000 --- a/src/gmp/demos/expr/exprv.c +++ /dev/null @@ -1,49 +0,0 @@ -/* mpz expression evaluation, simple part */ - -/* -Copyright 2000, 2001 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. -*/ - -#include -#include "gmp.h" -#include "expr-impl.h" - - -int -mpexpr_va_to_var (void *var[], va_list ap) -{ - int i = 0; - void *v; - - for (;;) - { - v = va_arg (ap, void *); - if (v == NULL) - break; - if (i >= MPEXPR_VARIABLES) - return MPEXPR_RESULT_BAD_VARIABLE; - var[i++] = v; - } - - while (i < MPEXPR_VARIABLES) - var[i++] = NULL; - - return MPEXPR_RESULT_OK; -} diff --git a/src/gmp/demos/expr/exprz.c b/src/gmp/demos/expr/exprz.c deleted file mode 100644 index 36f7c40ea..000000000 --- a/src/gmp/demos/expr/exprz.c +++ /dev/null @@ -1,213 +0,0 @@ -/* mpz expression evaluation, simple part - -Copyright 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - -#include -#include -#include -#include "gmp.h" -#include "expr-impl.h" - - -/* Change this to "#define TRACE(x) x" to get some traces. */ -#define TRACE(x) - - -/* These are macros, so need function wrappers. */ -static int -e_mpz_sgn (mpz_srcptr x) -{ - return mpz_sgn (x); -} -static int -e_mpz_odd_p (mpz_srcptr x) -{ - return mpz_odd_p (x); -} -static int -e_mpz_even_p (mpz_srcptr x) -{ - return mpz_even_p (x); -} - -/* These wrapped because MPEXPR_TYPE_I_ functions are expected to return - "int" whereas these return "unsigned long". */ -static void -e_mpz_hamdist (mpz_ptr w, mpz_srcptr x, mpz_srcptr y) -{ - mpz_set_ui (w, mpz_hamdist (x, y)); -} -static void -e_mpz_popcount (mpz_ptr w, mpz_srcptr x) -{ - mpz_set_ui (w, mpz_popcount (x)); -} -static void -e_mpz_scan0 (mpz_ptr w, mpz_srcptr x, unsigned long start) -{ - mpz_set_ui (w, mpz_scan0 (x, start)); -} -static void -e_mpz_scan1 (mpz_ptr w, mpz_srcptr x, unsigned long start) -{ - mpz_set_ui (w, mpz_scan1 (x, start)); -} - -/* These wrapped because they're in-place whereas MPEXPR_TYPE_BINARY_UI - expects a separate source and destination. Actually the parser will - normally pass w==x anyway. */ -static void -e_mpz_setbit (mpz_ptr w, mpz_srcptr x, unsigned long n) -{ - if (w != x) - mpz_set (w, x); - mpz_setbit (w, n); -} -static void -e_mpz_clrbit (mpz_ptr w, mpz_srcptr x, unsigned long n) -{ - if (w != x) - mpz_set (w, x); - mpz_clrbit (w, n); -} - -static __gmp_const struct mpexpr_operator_t _mpz_expr_standard_table[] = { - - { "**", (mpexpr_fun_t) mpz_pow_ui, - MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 }, - - { "~", (mpexpr_fun_t) mpz_com, - MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, - { "!", (mpexpr_fun_t) e_mpz_sgn, - MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX, 210 }, - { "-", (mpexpr_fun_t) mpz_neg, - MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 }, - - { "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 200 }, - { "/", (mpexpr_fun_t) mpz_tdiv_q, MPEXPR_TYPE_BINARY, 200 }, - { "%", (mpexpr_fun_t) mpz_tdiv_r, MPEXPR_TYPE_BINARY, 200 }, - - { "+", (mpexpr_fun_t) mpz_add, MPEXPR_TYPE_BINARY, 190 }, - { "-", (mpexpr_fun_t) mpz_sub, MPEXPR_TYPE_BINARY, 190 }, - - { "<<", (mpexpr_fun_t) mpz_mul_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, - { ">>", (mpexpr_fun_t) mpz_tdiv_q_2exp, MPEXPR_TYPE_BINARY_UI, 180 }, - - { "<=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LE, 170 }, - { "<", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_LT, 170 }, - { ">=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GE, 170 }, - { ">", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_GT, 170 }, - - { "==", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_EQ, 160 }, - { "!=", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_CMP_NE, 160 }, - - { "&", (mpexpr_fun_t) mpz_and, MPEXPR_TYPE_BINARY, 150 }, - { "^", (mpexpr_fun_t) mpz_xor, MPEXPR_TYPE_BINARY, 140 }, - { "|", (mpexpr_fun_t) mpz_ior, MPEXPR_TYPE_BINARY, 130 }, - { "&&", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_AND, 120 }, - { "||", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_LOGICAL_OR, 110 }, - - { ":", NULL, MPEXPR_TYPE_COLON, 101 }, - { "?", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_QUESTION, 100 }, - - { ")", NULL, MPEXPR_TYPE_CLOSEPAREN, 4 }, - { "(", NULL, MPEXPR_TYPE_OPENPAREN, 3 }, - { ",", NULL, MPEXPR_TYPE_ARGSEP, 2 }, - { "$", NULL, MPEXPR_TYPE_VARIABLE, 1 }, - - { "abs", (mpexpr_fun_t) mpz_abs, MPEXPR_TYPE_UNARY }, - { "bin", (mpexpr_fun_t) mpz_bin_ui, MPEXPR_TYPE_BINARY_UI }, - { "clrbit", (mpexpr_fun_t) e_mpz_clrbit, MPEXPR_TYPE_BINARY_UI }, - { "cmp", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_I_BINARY }, - { "cmpabs", (mpexpr_fun_t) mpz_cmpabs, MPEXPR_TYPE_I_BINARY }, - { "congruent_p",(mpexpr_fun_t)mpz_congruent_p, MPEXPR_TYPE_I_TERNARY }, - { "divisible_p",(mpexpr_fun_t)mpz_divisible_p, MPEXPR_TYPE_I_BINARY }, - { "even_p", (mpexpr_fun_t) e_mpz_even_p, MPEXPR_TYPE_I_UNARY }, - { "fib", (mpexpr_fun_t) mpz_fib_ui, MPEXPR_TYPE_UNARY_UI }, - { "fac", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI }, - { "gcd", (mpexpr_fun_t) mpz_gcd, MPEXPR_TYPE_BINARY - | MPEXPR_TYPE_PAIRWISE }, - { "hamdist", (mpexpr_fun_t) e_mpz_hamdist, MPEXPR_TYPE_BINARY }, - { "invert", (mpexpr_fun_t) mpz_invert, MPEXPR_TYPE_BINARY }, - { "jacobi", (mpexpr_fun_t) mpz_jacobi, MPEXPR_TYPE_I_BINARY }, - { "kronecker", (mpexpr_fun_t) mpz_kronecker, MPEXPR_TYPE_I_BINARY }, - { "lcm", (mpexpr_fun_t) mpz_lcm, MPEXPR_TYPE_BINARY - | MPEXPR_TYPE_PAIRWISE }, - { "lucnum", (mpexpr_fun_t) mpz_lucnum_ui, MPEXPR_TYPE_UNARY_UI }, - { "max", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MAX - | MPEXPR_TYPE_PAIRWISE }, - { "min", (mpexpr_fun_t) mpz_cmp, MPEXPR_TYPE_MIN - | MPEXPR_TYPE_PAIRWISE }, - { "nextprime", (mpexpr_fun_t) mpz_nextprime, MPEXPR_TYPE_UNARY }, - { "odd_p", (mpexpr_fun_t) e_mpz_odd_p, MPEXPR_TYPE_I_UNARY }, - { "perfect_power_p", (mpexpr_fun_t)mpz_perfect_power_p, MPEXPR_TYPE_I_UNARY}, - { "perfect_square_p",(mpexpr_fun_t)mpz_perfect_square_p,MPEXPR_TYPE_I_UNARY}, - { "popcount", (mpexpr_fun_t) e_mpz_popcount, MPEXPR_TYPE_UNARY }, - { "powm", (mpexpr_fun_t) mpz_powm, MPEXPR_TYPE_TERNARY }, - { "probab_prime_p", (mpexpr_fun_t)mpz_probab_prime_p, MPEXPR_TYPE_I_UNARY}, - { "root", (mpexpr_fun_t) mpz_root, MPEXPR_TYPE_BINARY_UI }, - { "scan0", (mpexpr_fun_t) e_mpz_scan0, MPEXPR_TYPE_BINARY_UI }, - { "scan1", (mpexpr_fun_t) e_mpz_scan1, MPEXPR_TYPE_BINARY_UI }, - { "setbit", (mpexpr_fun_t) e_mpz_setbit, MPEXPR_TYPE_BINARY_UI }, - { "tstbit", (mpexpr_fun_t) mpz_tstbit, MPEXPR_TYPE_I_BINARY_UI }, - { "sgn", (mpexpr_fun_t) e_mpz_sgn, MPEXPR_TYPE_I_UNARY }, - { "sqrt", (mpexpr_fun_t) mpz_sqrt, MPEXPR_TYPE_UNARY }, - { NULL } -}; - -/* The table is available globally only through a pointer, so the table size - can change without breaking binary compatibility. */ -__gmp_const struct mpexpr_operator_t * __gmp_const mpz_expr_standard_table -= _mpz_expr_standard_table; - - -int -#if HAVE_STDARG -mpz_expr (mpz_ptr res, int base, __gmp_const char *e, ...) -#else -mpz_expr (va_alist) - va_dcl -#endif -{ - mpz_srcptr var[MPEXPR_VARIABLES]; - va_list ap; - int ret; -#if HAVE_STDARG - va_start (ap, e); -#else - mpz_ptr res; - int base; - __gmp_const char *e; - va_start (ap); - res = va_arg (ap, mpz_ptr); - base = va_arg (ap, int); - e = va_arg (ap, __gmp_const char *); -#endif - - TRACE (printf ("mpz_expr(): base %d, %s\n", base, e)); - ret = mpexpr_va_to_var ((void **) var, ap); - va_end (ap); - - if (ret != MPEXPR_RESULT_OK) - return ret; - - return mpz_expr_a (mpz_expr_standard_table, res, base, e, strlen(e), var); -} - diff --git a/src/gmp/demos/expr/exprza.c b/src/gmp/demos/expr/exprza.c deleted file mode 100644 index 0381f3ff6..000000000 --- a/src/gmp/demos/expr/exprza.c +++ /dev/null @@ -1,99 +0,0 @@ -/* mpz expression evaluation - -Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - -#include -#include -#include -#include "gmp.h" -#include "expr-impl.h" - - -/* No need to parse '-' since that's handled as an operator. - This function also by mpq_expr_a, so it's not static. */ -size_t -mpexpr_mpz_number (mpz_ptr res, __gmp_const char *e, size_t elen, int base) -{ - char *edup; - size_t i, ret; - int base_effective = (base == 0 ? 10 : base); - void *(*allocate_func) (size_t); - void (*free_func) (void *, size_t); - - i = 0; - if (e[i] == '0') - { - i++; - if (e[i] == 'x' || e[i] == 'b') - i++; - } - - for ( ; i < elen; i++) - if (! isasciidigit_in_base (e[i], base_effective)) - break; - - mp_get_memory_functions (&allocate_func, NULL, &free_func); - edup = (*allocate_func) (i+1); - memcpy (edup, e, i); - edup[i] = '\0'; - - if (mpz_set_str (res, edup, base) == 0) - ret = i; - else - ret = 0; - - (*free_func) (edup, i+1); - return ret; -} - -/* ignoring prec */ -static void -e_mpz_init (mpz_ptr z, unsigned long prec) -{ - mpz_init (z); -} - -int -mpz_expr_a (__gmp_const struct mpexpr_operator_t *table, - mpz_ptr res, int base, - __gmp_const char *e, size_t elen, - mpz_srcptr var[26]) -{ - struct mpexpr_parse_t p; - - p.table = table; - p.res = (mpX_ptr) res; - p.base = base; - p.e = e; - p.elen = elen; - p.var = (mpX_srcptr *) var; - - p.mpX_clear = (mpexpr_fun_one_t) mpz_clear; - p.mpX_ulong_p = (mpexpr_fun_i_unary_t) mpz_fits_ulong_p; - p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpz_get_ui; - p.mpX_init = (mpexpr_fun_unary_ui_t) e_mpz_init; - p.mpX_number = (mpexpr_fun_number_t) mpexpr_mpz_number; - p.mpX_set = (mpexpr_fun_unary_t) mpz_set; - p.mpX_set_or_swap = (mpexpr_fun_unary_t) mpz_swap; - p.mpX_set_si = (mpexpr_fun_set_si_t) mpz_set_si; - p.mpX_swap = (mpexpr_fun_swap_t) mpz_swap; - - return mpexpr_evaluate (&p); -} diff --git a/src/gmp/demos/expr/run-expr.c b/src/gmp/demos/expr/run-expr.c deleted file mode 100644 index 11e4070de..000000000 --- a/src/gmp/demos/expr/run-expr.c +++ /dev/null @@ -1,233 +0,0 @@ -/* Demo program to run expression evaluation. - -Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - - -/* Usage: ./run-expr [-z] [-q] [-f] [-p prec] [-b base] expression... - - Evaluate each argument as a simple expression. By default this is in mpz - integers, but -q selects mpq or -f selects mpf. For mpf the float - precision can be set with -p. In all cases the input base can be set - with -b, or the default is "0" meaning decimal with "0x" allowed. - - This is a pretty trivial program, it's just an easy way to experiment - with the evaluation functions. */ - - -#include -#include - -#include "gmp.h" -#include "expr.h" - - -void -run_expr (int type, int base, unsigned long prec, char *str) -{ - int outbase = (base == 0 ? 10 : base); - int ret; - - switch (type) { - case 'z': - default: - { - mpz_t res, var_a, var_b; - - mpz_init (res); - mpz_init_set_ui (var_a, 55L); - mpz_init_set_ui (var_b, 99L); - - ret = mpz_expr (res, base, str, var_a, var_b, NULL); - printf ("\"%s\" base %d: ", str, base); - if (ret == MPEXPR_RESULT_OK) - { - printf ("result "); - mpz_out_str (stdout, outbase, res); - printf ("\n"); - } - else - printf ("invalid (return code %d)\n", ret); - - mpz_clear (res); - mpz_clear (var_a); - mpz_clear (var_b); - } - break; - - case 'q': - { - mpq_t res, var_a, var_b; - - mpq_init (res); - mpq_init (var_a); - mpq_init (var_b); - - mpq_set_ui (var_a, 55L, 1); - mpq_set_ui (var_b, 99L, 1); - - ret = mpq_expr (res, base, str, var_a, var_b, NULL); - printf ("\"%s\" base %d: ", str, base); - if (ret == MPEXPR_RESULT_OK) - { - printf ("result "); - mpq_out_str (stdout, outbase, res); - printf ("\n"); - } - else - printf ("invalid (return code %d)\n", ret); - - mpq_clear (res); - mpq_clear (var_a); - mpq_clear (var_b); - } - break; - - case 'f': - { - mpf_t res, var_a, var_b; - - mpf_init2 (res, prec); - mpf_init_set_ui (var_a, 55L); - mpf_init_set_ui (var_b, 99L); - - ret = mpf_expr (res, base, str, var_a, var_b, NULL); - printf ("\"%s\" base %d: ", str, base); - if (ret == MPEXPR_RESULT_OK) - { - printf ("result "); - mpf_out_str (stdout, outbase, (size_t) 0, res); - printf ("\n"); - } - else - printf ("invalid (return code %d)\n", ret); - - mpf_clear (res); - mpf_clear (var_a); - mpf_clear (var_b); - } - break; - } -} - -int -main (int argc, char *argv[]) -{ - int type = 'z'; - int base = 0; - unsigned long prec = 64; - int seen_expr = 0; - int opt; - char *arg; - - for (;;) - { - argv++; - arg = argv[0]; - if (arg == NULL) - break; - - if (arg[0] == '-') - { - for (;;) - { - arg++; - opt = arg[0]; - - switch (opt) { - case '\0': - goto end_opt; - - case 'f': - case 'q': - case 'z': - type = opt; - break; - - case 'b': - arg++; - if (arg[0] == '\0') - { - argv++; - arg = argv[0]; - if (arg == NULL) - { - need_arg: - fprintf (stderr, "Need argument for -%c\n", opt); - exit (1); - } - } - base = atoi (arg); - goto end_opt; - - case 'p': - arg++; - if (arg[0] == '\0') - { - argv++; - arg = argv[0]; - if (arg == NULL) - goto need_arg; - } - prec = atoi (arg); - goto end_opt; - - case '-': - arg++; - if (arg[0] != '\0') - { - /* no "--foo" options */ - fprintf (stderr, "Unrecognised option --%s\n", arg); - exit (1); - } - /* stop option interpretation at "--" */ - for (;;) - { - argv++; - arg = argv[0]; - if (arg == NULL) - goto done; - run_expr (type, base, prec, arg); - seen_expr = 1; - } - - default: - fprintf (stderr, "Unrecognised option -%c\n", opt); - exit (1); - } - } - end_opt: - ; - } - else - { - run_expr (type, base, prec, arg); - seen_expr = 1; - } - } - - done: - if (! seen_expr) - { - printf ("Usage: %s [-z] [-q] [-f] [-p prec] [-b base] expression...\n", argv[0]); - exit (1); - } - - return 0; -} diff --git a/src/gmp/demos/expr/t-expr.c b/src/gmp/demos/expr/t-expr.c deleted file mode 100644 index ba1f67090..000000000 --- a/src/gmp/demos/expr/t-expr.c +++ /dev/null @@ -1,501 +0,0 @@ -/* Test expression evaluation (print nothing and exit 0 if successful). - -Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - -#include -#include - -#include "gmp.h" -#include "tests.h" -#include "expr-impl.h" - - -int option_trace = 0; - - -struct data_t { - int base; - const char *expr; - const char *want; -}; - -#define numberof(x) (sizeof (x) / sizeof ((x)[0])) - - -/* These data_xxx[] arrays are tables to be tested with one or more of the - mp?_t types. z=mpz_t, q=mpz_t, f=mpf_t. */ - -struct data_t data_zqf[] = { - - /* various deliberately wrong expressions */ - { 0, "", NULL }, - { 0, "1+", NULL }, - { 0, "+2", NULL }, - { 0, "1,2", NULL }, - { 0, "foo(1,2)", NULL }, - { 0, "1+foo", NULL }, - { 10, "0fff", NULL }, - { 0, "!", NULL }, - { 0, "10!", NULL }, - { 0, "-10!", NULL }, - { 0, "gcd((4,6))", NULL }, - { 0, "()", NULL }, - { 0, "fac(2**1000)", NULL }, - { 0, "$", NULL }, - { 0, "$-", NULL }, - - /* some basics */ - { 10, "123", "123" }, - { 10, "-123", "-123" }, - { 10, "1+2", "3" }, - { 10, "1+2+3", "6" }, - { 10, "1+2*3", "7" }, - { 10, "3*2+1", "7" }, - { 10, "$a", "55" }, - { 10, "b", "99" }, - { 16, "b", "11" }, - { 10, "4**3 * 2 + 1", "129" }, - { 10, "1<2", "1" }, - { 10, "1>2", "0" }, - - { 10, "(123)", "123" }, - - { 10, "sgn(-123)", "-1" }, - { 10, "5-7", "-2" }, - - { 0, "cmp(0,0)", "0" }, - { 0, "cmp(1,0)", "1" }, - { 0, "cmp(0,1)", "-1" }, - { 0, "cmp(-1,0)", "-1" }, - { 0, "cmp(0,-1)", "1" }, - - { 10, "0 ? 123 : 456", "456" }, - { 10, "1 ? 4+5 : 6+7", "9" }, - - { 10, "(123)", "123" }, - { 10, "(2+3)", "5" }, - { 10, "(4+5)*(5+6)", "99" }, - - { 0, "1 << 16", "65536" }, - { 0, "256 >> 4", "16" }, - { 0, "-256 >> 4", "-16" }, - - { 0, "!1", "0" }, - { 0, "!9", "0" }, - { 0, "!0", "1" }, - - { 0, "2**2**2", "16" }, - { 0, "-2**2**2", "-16" }, - - { 0, "0x100", "256" }, - { 10, "0x100", NULL }, - { 10, "0x 100", NULL }, - - { 0, " max ( 1, 2, 3, 4, 5, 6, 7, 8)", "8" }, - { 0, " max ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "9" }, - { 0, " min ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "1" }, - - { 10, "abs(123)", "123" }, - { 10, "abs(-123)", "123" }, - { 10, "abs(0)", "0" }, - - /* filling data stack */ - { 0, "1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1))))))))))))))", "16" }, - - /* filling control stack */ - { 0, "----------------------------------------------------1", "1" }, -}; - - -const struct data_t data_z[] = { - { 0, "divisible_p(333,3)", "1" }, - { 0, "congruent_p(7,1,3)", "1" }, - - { 0, "cmpabs(0,0)", "0" }, - { 0, "cmpabs(1,0)", "1" }, - { 0, "cmpabs(0,1)", "-1" }, - { 0, "cmpabs(-1,0)", "1" }, - { 0, "cmpabs(0,-1)", "-1" }, - - { 0, "odd_p(1)", "1" }, - { 0, "odd_p(0)", "0" }, - { 0, "odd_p(-1)", "1" }, - - { 0, "even_p(1)", "0" }, - { 0, "even_p(0)", "1" }, - { 0, "even_p(-1)", "0" }, - - { 0, "fac(0)", "1" }, - { 0, "fac(1)", "1" }, - { 0, "fac(2)", "2" }, - { 0, "fac(3)", "6" }, - { 0, "fac(10)", "3628800" }, - - { 10, "root(81,4)", "3" }, - - { 10, "gcd(4,6)", "2" }, - { 10, "gcd(4,6,9)", "1" }, - - { 10, "powm(3,2,9)", "0" }, - { 10, "powm(3,2,8)", "1" }, - - /* filling data stack */ - { 0, "1 ? 1 : 1 || 1 && 1 | 1 ^ 1 & 1 == 1 >= 1 << 1 - 1 * 1 ** 1", "1" }, - - /* filling control stack */ - { 0, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1", "1" }, - - { 0, "fib(10)", "55" }, - - { 0, "setbit(0,5)", "32" }, - { 0, "clrbit(32,5)", "0" }, - { 0, "tstbit(32,5)", "1" }, - { 0, "tstbit(32,4)", "0" }, - { 0, "scan0(7,0)", "3" }, - { 0, "scan1(7,0)", "0" }, -}; - -const struct data_t data_zq[] = { - /* expecting failure */ - { 0, "1.2", NULL }, -}; - -const struct data_t data_q[] = { - { 10, "(1/2 + 1/3 + 1/4 + 1/5 + 1/6)*20", "29" }, - { 0, "num(5/9)", "5" }, - { 0, "den(5/9)", "9" }, -}; - -const struct data_t data_zf[] = { - { 10, "sqrt ( 49 )", "7" }, - { 10, "sqrt ( 49 ) + 1", "8" }, - { 10, "sqrt((49))", "7" }, - { 10, "sqrt((((((((49))))))))", "7" }, -}; - -const struct data_t data_f[] = { - { 0, "1@10", "10000000000" }, - { 0, "1.5@10", "15000000000" }, - { 0, "1000@-1", "100" }, - { 0, "10.00@-1", "1" }, - - { 0, "1e10", "10000000000" }, - { 0, "1.5e10", "15000000000" }, - { 0, "1000e-1", "100" }, - { 0, "10.00e-1", "1" }, - - { 16, "1@9", "68719476736" }, - - { 16, "1@10", "18446744073709551616" }, - { -16, "1@10", "1099511627776" }, - - { 0, "ceil(0)", "0" }, - { 0, "ceil(0.25)", "1" }, - { 0, "ceil(0.5)", "1" }, - { 0, "ceil(1.5)", "2" }, - { 0, "ceil(-0.5)", "0" }, - { 0, "ceil(-1.5)", "-1" }, - - /* only simple cases because mpf_eq currently only works on whole limbs */ - { 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,64)", "1" }, - { 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,128)", "0" }, - - { 0, "floor(0)", "0" }, - { 0, "floor(0.25)", "0" }, - { 0, "floor(0.5)", "0" }, - { 0, "floor(1.5)", "1" }, - { 0, "floor(-0.5)", "-1" }, - { 0, "floor(-1.5)", "-2" }, - - { 0, "integer_p(1)", "1" }, - { 0, "integer_p(0.5)", "0" }, - - { 0, "trunc(0)", "0" }, - { 0, "trunc(0.25)", "0" }, - { 0, "trunc(0.5)", "0" }, - { 0, "trunc(1.5)", "1" }, - { 0, "trunc(-0.5)", "0" }, - { 0, "trunc(-1.5)", "-1" }, -}; - -struct datalist_t { - const struct data_t *data; - int num; -}; - -#define DATALIST(data) { data, numberof (data) } - -struct datalist_t list_z[] = { - DATALIST (data_z), - DATALIST (data_zq), - DATALIST (data_zf), - DATALIST (data_zqf), -}; - -struct datalist_t list_q[] = { - DATALIST (data_q), - DATALIST (data_zq), - DATALIST (data_zqf), -}; - -struct datalist_t list_f[] = { - DATALIST (data_zf), - DATALIST (data_zqf), - DATALIST (data_f), -}; - - -void -check_z (void) -{ - const struct data_t *data; - mpz_t a, b, got, want; - int l, i, ret; - - mpz_init (got); - mpz_init (want); - mpz_init_set_ui (a, 55); - mpz_init_set_ui (b, 99); - - for (l = 0; l < numberof (list_z); l++) - { - data = list_z[l].data; - - for (i = 0; i < list_z[l].num; i++) - { - if (option_trace) - printf ("mpz_expr \"%s\"\n", data[i].expr); - - ret = mpz_expr (got, data[i].base, data[i].expr, a, b, NULL); - - if (data[i].want == NULL) - { - /* expect to fail */ - if (ret == MPEXPR_RESULT_OK) - { - printf ("mpz_expr wrong return value, got %d, expected failure\n", ret); - goto error; - } - } - else - { - if (mpz_set_str (want, data[i].want, 0) != 0) - { - printf ("Cannot parse wanted value string\n"); - goto error; - } - if (ret != MPEXPR_RESULT_OK) - { - printf ("mpz_expr failed unexpectedly\n"); - printf (" return value %d\n", ret); - goto error; - } - if (mpz_cmp (got, want) != 0) - { - printf ("mpz_expr wrong result\n"); - printf (" got "); mpz_out_str (stdout, 10, got); - printf ("\n"); - printf (" want "); mpz_out_str (stdout, 10, want); - printf ("\n"); - goto error; - } - } - } - } - mpz_clear (a); - mpz_clear (b); - mpz_clear (got); - mpz_clear (want); - return; - - error: - printf (" base %d\n", data[i].base); - printf (" expr \"%s\"\n", data[i].expr); - if (data[i].want != NULL) - printf (" want \"%s\"\n", data[i].want); - abort (); -} - -void -check_q (void) -{ - const struct data_t *data; - mpq_t a, b, got, want; - int l, i, ret; - - mpq_init (got); - mpq_init (want); - mpq_init (a); - mpq_init (b); - - mpq_set_ui (a, 55, 1); - mpq_set_ui (b, 99, 1); - - for (l = 0; l < numberof (list_q); l++) - { - data = list_q[l].data; - - for (i = 0; i < list_q[l].num; i++) - { - if (option_trace) - printf ("mpq_expr \"%s\"\n", data[i].expr); - - ret = mpq_expr (got, data[i].base, data[i].expr, a, b, NULL); - - if (data[i].want == NULL) - { - /* expect to fail */ - if (ret == MPEXPR_RESULT_OK) - { - printf ("mpq_expr wrong return value, got %d, expected failure\n", ret); - goto error; - } - } - else - { - if (mpz_set_str (mpq_numref(want), data[i].want, 0) != 0) - { - printf ("Cannot parse wanted value string\n"); - goto error; - } - mpz_set_ui (mpq_denref(want), 1); - - if (ret != MPEXPR_RESULT_OK) - { - printf ("mpq_expr failed unexpectedly\n"); - printf (" return value %d\n", ret); - goto error; - } - if (mpq_cmp (got, want) != 0) - { - printf ("mpq_expr wrong result\n"); - printf (" got "); mpq_out_str (stdout, 10, got); - printf ("\n"); - printf (" want "); mpq_out_str (stdout, 10, want); - printf ("\n"); - goto error; - } - } - } - } - mpq_clear (a); - mpq_clear (b); - mpq_clear (got); - mpq_clear (want); - return; - - error: - printf (" base %d\n", data[i].base); - printf (" expr \"%s\"\n", data[i].expr); - if (data[i].want != NULL) - printf (" want \"%s\"\n", data[i].want); - abort (); -} - -void -check_f (void) -{ - const struct data_t *data; - mpf_t a, b, got, want; - int l, i, ret; - - mpf_set_default_prec (200L); - - mpf_init (got); - mpf_init (want); - mpf_init_set_ui (a, 55); - mpf_init_set_ui (b, 99); - - for (l = 0; l < numberof (list_f); l++) - { - data = list_f[l].data; - - for (i = 0; i < list_f[l].num; i++) - { - if (option_trace) - printf ("mpf_expr \"%s\"\n", data[i].expr); - - ret = mpf_expr (got, data[i].base, data[i].expr, a, b, NULL); - - if (data[i].want == NULL) - { - /* expect to fail */ - if (ret == MPEXPR_RESULT_OK) - { - printf ("mpf_expr wrong return value, got %d, expected failure\n", ret); - goto error; - } - } - else - { - if (mpf_set_str (want, data[i].want, 0) != 0) - { - printf ("Cannot parse wanted value string\n"); - goto error; - } - - if (ret != MPEXPR_RESULT_OK) - { - printf ("mpf_expr failed unexpectedly\n"); - printf (" return value %d\n", ret); - goto error; - } - if (mpf_cmp (got, want) != 0) - { - printf ("mpf_expr wrong result\n"); - printf (" got "); mpf_out_str (stdout, 10, 20, got); - printf ("\n"); - printf (" want "); mpf_out_str (stdout, 10, 20, want); - printf ("\n"); - goto error; - } - } - } - } - mpf_clear (a); - mpf_clear (b); - mpf_clear (got); - mpf_clear (want); - return; - - error: - printf (" base %d\n", data[i].base); - printf (" expr \"%s\"\n", data[i].expr); - if (data[i].want != NULL) - printf (" want \"%s\"\n", data[i].want); - abort (); -} - - -int -main (int argc, char *argv[]) -{ - tests_start (); - - if (argc >= 2) - option_trace = 1; - - check_z (); - check_q (); - check_f (); - - tests_end (); - exit (0); -} diff --git a/src/gmp/demos/factorize.c b/src/gmp/demos/factorize.c deleted file mode 100644 index 0b14e7468..000000000 --- a/src/gmp/demos/factorize.c +++ /dev/null @@ -1,368 +0,0 @@ -/* Factoring with Pollard's rho method. - -Copyright 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005 Free Software -Foundation, Inc. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; see the file COPYING. If not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include -#include -#include - -#include "gmp.h" - -int flag_verbose = 0; - -static unsigned add[] = {4, 2, 4, 2, 4, 6, 2, 6}; - -void -factor_using_division (mpz_t t, unsigned int limit) -{ - mpz_t q, r; - unsigned long int f; - int ai; - unsigned *addv = add; - unsigned int failures; - - if (flag_verbose) - { - printf ("[trial division (%u)] ", limit); - fflush (stdout); - } - - mpz_init (q); - mpz_init (r); - - f = mpz_scan1 (t, 0); - mpz_div_2exp (t, t, f); - while (f) - { - printf ("2 "); - fflush (stdout); - --f; - } - - for (;;) - { - mpz_tdiv_qr_ui (q, r, t, 3); - if (mpz_cmp_ui (r, 0) != 0) - break; - mpz_set (t, q); - printf ("3 "); - fflush (stdout); - } - - for (;;) - { - mpz_tdiv_qr_ui (q, r, t, 5); - if (mpz_cmp_ui (r, 0) != 0) - break; - mpz_set (t, q); - printf ("5 "); - fflush (stdout); - } - - failures = 0; - f = 7; - ai = 0; - while (mpz_cmp_ui (t, 1) != 0) - { - mpz_tdiv_qr_ui (q, r, t, f); - if (mpz_cmp_ui (r, 0) != 0) - { - f += addv[ai]; - if (mpz_cmp_ui (q, f) < 0) - break; - ai = (ai + 1) & 7; - failures++; - if (failures > limit) - break; - } - else - { - mpz_swap (t, q); - printf ("%lu ", f); - fflush (stdout); - failures = 0; - } - } - - mpz_clear (q); - mpz_clear (r); -} - -void -factor_using_division_2kp (mpz_t t, unsigned int limit, unsigned long p) -{ - mpz_t r; - mpz_t f; - unsigned int k; - - if (flag_verbose) - { - printf ("[trial division (%u)] ", limit); - fflush (stdout); - } - - mpz_init (r); - mpz_init_set_ui (f, 2 * p); - mpz_add_ui (f, f, 1); - for (k = 1; k < limit; k++) - { - mpz_tdiv_r (r, t, f); - while (mpz_cmp_ui (r, 0) == 0) - { - mpz_tdiv_q (t, t, f); - mpz_tdiv_r (r, t, f); - mpz_out_str (stdout, 10, f); - fflush (stdout); - fputc (' ', stdout); - } - mpz_add_ui (f, f, 2 * p); - } - - mpz_clear (f); - mpz_clear (r); -} - -void -factor_using_pollard_rho (mpz_t n, int a_int, unsigned long p) -{ - mpz_t x, x1, y, P; - mpz_t a; - mpz_t g; - mpz_t t1, t2; - int k, l, c, i; - - if (flag_verbose) - { - printf ("[pollard-rho (%d)] ", a_int); - fflush (stdout); - } - - mpz_init (g); - mpz_init (t1); - mpz_init (t2); - - mpz_init_set_si (a, a_int); - mpz_init_set_si (y, 2); - mpz_init_set_si (x, 2); - mpz_init_set_si (x1, 2); - k = 1; - l = 1; - mpz_init_set_ui (P, 1); - c = 0; - - while (mpz_cmp_ui (n, 1) != 0) - { -S2: - if (p != 0) - { - mpz_powm_ui (x, x, p, n); mpz_add (x, x, a); - } - else - { - mpz_mul (x, x, x); mpz_add (x, x, a); mpz_mod (x, x, n); - } - mpz_sub (t1, x1, x); mpz_mul (t2, P, t1); mpz_mod (P, t2, n); - c++; - if (c == 20) - { - c = 0; - mpz_gcd (g, P, n); - if (mpz_cmp_ui (g, 1) != 0) - goto S4; - mpz_set (y, x); - } -S3: - k--; - if (k > 0) - goto S2; - - mpz_gcd (g, P, n); - if (mpz_cmp_ui (g, 1) != 0) - goto S4; - - mpz_set (x1, x); - k = l; - l = 2 * l; - for (i = 0; i < k; i++) - { - if (p != 0) - { - mpz_powm_ui (x, x, p, n); mpz_add (x, x, a); - } - else - { - mpz_mul (x, x, x); mpz_add (x, x, a); mpz_mod (x, x, n); - } - } - mpz_set (y, x); - c = 0; - goto S2; -S4: - do - { - if (p != 0) - { - mpz_powm_ui (y, y, p, n); mpz_add (y, y, a); - } - else - { - mpz_mul (y, y, y); mpz_add (y, y, a); mpz_mod (y, y, n); - } - mpz_sub (t1, x1, y); mpz_gcd (g, t1, n); - } - while (mpz_cmp_ui (g, 1) == 0); - - mpz_div (n, n, g); /* divide by g, before g is overwritten */ - - if (!mpz_probab_prime_p (g, 3)) - { - do - { - mp_limb_t a_limb; - mpn_random (&a_limb, (mp_size_t) 1); - a_int = (int) a_limb; - } - while (a_int == -2 || a_int == 0); - - if (flag_verbose) - { - printf ("[composite factor--restarting pollard-rho] "); - fflush (stdout); - } - factor_using_pollard_rho (g, a_int, p); - } - else - { - mpz_out_str (stdout, 10, g); - fflush (stdout); - fputc (' ', stdout); - } - mpz_mod (x, x, n); - mpz_mod (x1, x1, n); - mpz_mod (y, y, n); - if (mpz_probab_prime_p (n, 3)) - { - mpz_out_str (stdout, 10, n); - fflush (stdout); - fputc (' ', stdout); - break; - } - } - - mpz_clear (g); - mpz_clear (P); - mpz_clear (t2); - mpz_clear (t1); - mpz_clear (a); - mpz_clear (x1); - mpz_clear (x); - mpz_clear (y); -} - -void -factor (mpz_t t, unsigned long p) -{ - unsigned int division_limit; - - if (mpz_sgn (t) == 0) - return; - - /* Set the trial division limit according the size of t. */ - division_limit = mpz_sizeinbase (t, 2); - if (division_limit > 1000) - division_limit = 1000 * 1000; - else - division_limit = division_limit * division_limit; - - if (p != 0) - factor_using_division_2kp (t, division_limit / 10, p); - else - factor_using_division (t, division_limit); - - if (mpz_cmp_ui (t, 1) != 0) - { - if (flag_verbose) - { - printf ("[is number prime?] "); - fflush (stdout); - } - if (mpz_probab_prime_p (t, 3)) - mpz_out_str (stdout, 10, t); - else - factor_using_pollard_rho (t, 1, p); - } -} - -main (int argc, char *argv[]) -{ - mpz_t t; - unsigned long p; - int i; - - if (argc > 1 && !strcmp (argv[1], "-v")) - { - flag_verbose = 1; - argv++; - argc--; - } - - mpz_init (t); - if (argc > 1) - { - p = 0; - for (i = 1; i < argc; i++) - { - if (!strncmp (argv[i], "-Mp", 3)) - { - p = atoi (argv[i] + 3); - mpz_set_ui (t, 1); - mpz_mul_2exp (t, t, p); - mpz_sub_ui (t, t, 1); - } - else if (!strncmp (argv[i], "-2kp", 4)) - { - p = atoi (argv[i] + 4); - continue; - } - else - { - mpz_set_str (t, argv[i], 0); - } - - if (mpz_cmp_ui (t, 0) == 0) - puts ("-"); - else - { - factor (t, p); - puts (""); - } - } - } - else - { - for (;;) - { - mpz_inp_str (t, stdin, 0); - if (feof (stdin)) - break; - mpz_out_str (stdout, 10, t); printf (" = "); - factor (t, 0); - puts (""); - } - } - - exit (0); -} diff --git a/src/gmp/demos/isprime.c b/src/gmp/demos/isprime.c deleted file mode 100644 index 701f404d2..000000000 --- a/src/gmp/demos/isprime.c +++ /dev/null @@ -1,68 +0,0 @@ -/* Classify numbers as probable primes, primes or composites. - With -q return true if the folowing argument is a (probable) prime. - -Copyright 1999, 2000, 2002, 2005 Free Software Foundation, Inc. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include -#include -#include "gmp.h" - -char *progname; - -void -print_usage_and_exit () -{ - fprintf (stderr, "usage: %s -q nnn\n", progname); - fprintf (stderr, "usage: %s nnn ...\n", progname); - exit (-1); -} - -int -main (int argc, char **argv) -{ - mpz_t n; - int i; - - progname = argv[0]; - - if (argc < 2) - print_usage_and_exit (); - - mpz_init (n); - - if (argc == 3 && strcmp (argv[1], "-q") == 0) - { - if (mpz_set_str (n, argv[2], 0) != 0) - print_usage_and_exit (); - exit (mpz_probab_prime_p (n, 5) == 0); - } - - for (i = 1; i < argc; i++) - { - int class; - if (mpz_set_str (n, argv[i], 0) != 0) - print_usage_and_exit (); - class = mpz_probab_prime_p (n, 5); - mpz_out_str (stdout, 10, n); - if (class == 0) - puts (" is composite"); - else if (class == 1) - puts (" is a probable prime"); - else /* class == 2 */ - puts (" is a prime"); - } - exit (0); -} diff --git a/src/gmp/demos/perl/GMP.pm b/src/gmp/demos/perl/GMP.pm deleted file mode 100644 index e6d7baf01..000000000 --- a/src/gmp/demos/perl/GMP.pm +++ /dev/null @@ -1,653 +0,0 @@ -# GMP perl module - -# Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - -# [Note: The above copyright notice is repeated in the documentation section -# below, in order to get it into man pages etc generated by the various pod -# conversions. When changing, be sure to update below too.] - - -# This code is designed to work with perl 5.005, so it and the sub-packages -# aren't as modern as they could be. - -package GMP; - -require Symbol; -require Exporter; -require DynaLoader; -@ISA = qw(Exporter DynaLoader); - -@EXPORT = qw(); -@EXPORT_OK = qw(version); -%EXPORT_TAGS = ('all' => [qw( - get_d get_d_2exp get_si get_str integer_p - printf sgn sprintf)], - 'constants' => [()]); -Exporter::export_ok_tags('all'); - -$VERSION = '2.00'; -bootstrap GMP $VERSION; - - -# The format string is cut up into "%" specifiers so GMP types can be -# passed to GMP::sprintf_internal. Any "*"s are interpolated before -# calling sprintf_internal, which saves worrying about variable -# argument lists there. -# -# Because sprintf_internal is only called after the conversion and -# operand have been checked there won't be any crashes from a bad -# format string. -# -sub sprintf { - my $fmt = shift; - my $out = ''; - my ($pre, $dummy, $pat, $rest); - - while (($pre, $dummy, $pat, $rest) = ($fmt =~ /^((%%|[^%])*)(%[- +#.*hlLqv\d]*[bcdfeEgGinopsuxX])(.*)$/s)) { - - $out .= $pre; - - my $pat2 = $pat; # $pat with "*"s expanded - my @params = (); # arguments per "*"s - while ($pat2 =~ /[*]/) { - my $arg = shift; - $pat2 =~ s/[*]/$arg/; - push @params, $arg; - } - - if (UNIVERSAL::isa($_[0],"GMP::Mpz")) { - if ($pat2 !~ /[dioxX]$/) { - die "GMP::sprintf: unsupported output format for mpz: $pat2\n"; - } - $pat2 =~ s/(.)$/Z$1/; - $out .= sprintf_internal ($pat2, shift); - - } elsif (UNIVERSAL::isa($_[0],"GMP::Mpq")) { - if ($pat2 !~ /[dioxX]$/) { - die "GMP::sprintf: unsupported output format for mpq: $pat2\n"; - } - $pat2 =~ s/(.)$/Q$1/; - $out .= sprintf_internal ($pat2, shift); - - } elsif (UNIVERSAL::isa($_[0],"GMP::Mpf")) { - if ($pat2 !~ /[eEfgG]$/) { - die "GMP::sprintf: unsupported output format for mpf: $pat2\n"; - } - $pat2 =~ s/(.)$/F$1/; - $out .= sprintf_internal ($pat2, shift); - - } elsif ($pat =~ /n$/) { - # do it this way so h, l or V type modifiers are respected, and use a - # dummy variable to avoid a warning about discarding the value - my $dummy = sprintf "%s$pat", $out, $_[0]; - shift; - - } else { - $out .= sprintf $pat, @params, shift; - } - - $fmt = $rest; - } - $out .= $fmt; - return $out; -} - -sub printf { - if (ref($_[0]) eq 'GLOB') { - my $h = Symbol::qualify_to_ref(shift, caller); - print $h GMP::sprintf(@_); - } else { - print STDOUT GMP::sprintf(@_); - } -} - -1; -__END__ - - - -=head1 NAME - -GMP - Perl interface to the GNU Multiple Precision Arithmetic Library - -=head1 SYNOPSIS - - use GMP; - use GMP::Mpz; - use GMP::Mpq; - use GMP::Mpf; - use GMP::Rand; - -=head1 DESCRIPTION - -This module provides access to GNU MP arbitrary precision integers, -rationals and floating point. - -No functions are exported from these packages by default, but can be -selected in the usual way, or the tag :all for everything. - - use GMP::Mpz qw(gcd, lcm); # just these functions - use GMP::Mpq qw(:all); # everything in mpq - -=head2 GMP::Mpz - -This class provides arbitrary precision integers. A new mpz can be -constructed with C. The initial value can be an integer, float, -string, mpz, mpq or mpf. Floats, mpq and mpf will be automatically -truncated to an integer. - - use GMP::Mpz qw(:all); - my $a = mpz(123); - my $b = mpz("0xFFFF"); - my $c = mpz(1.5); # truncated - -The following overloaded operators are available, and corresponding -assignment forms like C<+=>, - -=over 4 - -=item - -+ - * / % EE EE ** & | ^ ! E E= == != E E= -E=E abs not sqrt - -=back - -C and C<%> round towards zero (as per the C functions in GMP). - -The following functions are available, behaving the same as the -corresponding GMP mpz functions, - -=over 4 - -=item - -bin, cdiv, cdiv_2exp, clrbit, combit, congruent_p, congruent_2exp_p, -divexact, divisible_p, divisible_2exp_p, even_p, fac, fdiv, fdiv_2exp, fib, -fib2, gcd, gcdext, hamdist, invert, jacobi, kronecker, lcm, lucnum, lucnum2, -mod, mpz_export, mpz_import, nextprime, odd_p, perfect_power_p, -perfect_square_p, popcount, powm, probab_prime_p, realloc, remove, root, -roote, scan0, scan1, setbit, sizeinbase, sqrtrem, tdiv, tdiv_2exp, tstbit - -=back - -C, C and C and their C<2exp> variants return a -quotient/remainder pair. C returns a pair F[n] and F[n-1], similarly -C. C and C accept a variable number of arguments (one or -more). C returns a triplet of gcd and two cofactors, for example - - use GMP::Mpz qw(:all); - $a = 7257; - $b = 10701; - ($g, $x, $y) = gcdext ($a, $b); - print "gcd($a,$b) is $g, and $g == $a*$x + $b*$y\n"; - -C and C are so named to avoid the C keyword. -Their parameters are as follows, - - $z = mpz_import ($order, $size, $endian, $nails, $string); - $string = mpz_export ($order, $size, $endian, $nails, $z); - -The order, size, endian and nails parameters are as per the corresponding C -functions. The string input for C is interpreted as byte data -and must be a multiple of $size bytes. C conversely returns a -string of byte data, which will be a multiple of $size bytes. - -C returns the inverse, or undef if it doesn't exist. C -returns a remainder/multiplicty pair. C returns the nth root, and -C returns a root/bool pair, the bool indicating whether the root is -exact. C and C return a root/remainder pair. - -C, C and C expect a variable which they can modify, -it doesn't make sense to pass a literal constant. Only the given variable -is modified, if other variables are referencing the same mpz object then a -new copy is made of it. If the variable isn't an mpz it will be coerced to -one. For instance, - - use GMP::Mpz qw(setbit); - setbit (123, 0); # wrong, don't pass a constant - $a = mpz(6); - $b = $a; - setbit ($a, 0); # $a becomes 7, $b stays at 6 - -C and C return ~0 if no 0 or 1 bit respectively is found. - -=head2 GMP::Mpq - -This class provides rationals with arbitrary precision numerators and -denominators. A new mpq can be constructed with C. The initial value -can be an integer, float, string, mpz, mpq or mpf, or a pair of integers or -mpz's. No precision is lost when converting a float or mpf, the exact value -is retained. - - use GMP::Mpq qw(:all); - $a = mpq(); # zero - $b = mpq(0.5); # gives 1/2 - $b = mpq(14); # integer 14 - $b = mpq(3,4); # fraction 3/4 - $b = mpq("7/12"); # fraction 7/12 - $b = mpq("0xFF/0x100"); # fraction 255/256 - -When a fraction is given, it should be in the canonical form specified in -the GMP manual, which is denominator positive, no common factors, and zero -always represented as 0/1. If not then C can be called to put -it in that form. For example, - - use GMP::Mpq qw(:all); - $q = mpq(21,15); # eek! common factor 3 - canonicalize($q); # get rid of it - -The following overloaded operators are available, and corresponding -assignment forms like C<+=>, - -=over 4 - -=item - -+ - * / EE EE ** ! E E= == != E E= -E=E abs not - -=back - -The following functions are available, - -=over 4 - -=item - -den, inv, num - -=back - -C calculates 1/q, as per the corresponding GMP function. C and -C return an mpz copy of the numerator or denominator respectively. In -the future C and C might give lvalues so the original mpq can be -modified through them, but this is not done currently. - -=head2 GMP::Mpf - -This class provides arbitrary precision floating point numbers. The -mantissa is an arbitrary user-selected precision and the exponent is a fixed -size (one machine word). - -A new mpf can be constructed with C. The initial value can be an -integer, float, string, mpz, mpq or mpf. The second argument specifies the -desired precision in bits, or if omitted then the default precision is used. - - use GMP::Mpf qw(:all); - $a = mpf(); # zero - $b = mpf(-7.5); # default precision - $c = mpf(1.5, 500); # 500 bits precision - $d = mpf("1.0000000000000001"); - -The following overloaded operators are available, with the corresponding -assignment forms like C<+=>, - -=over 4 - -=item - -+ - * / EE EE ** ! E E= == != E E= -E=E abs not sqrt - -=back - -The following functions are available, behaving the same as the -corresponding GMP mpf functions, - -=over 4 - -=item - -ceil, floor, get_default_prec, get_prec, mpf_eq, set_default_prec, set_prec, -trunc - -=back - -C is so named to avoid clashing with the perl C operator. - -C expects a variable which it can modify, it doesn't make sense to -pass a literal constant. Only the given variable is modified, if other -variables are referencing the same mpf object then a new copy is made of it. -If the variable isn't an mpf it will be coerced to one. - -Results are the same precision as inputs, or if two mpf's are given to a -binary operator then the precision of the first is used. For example, - - use GMP::Mpf qw(mpf); - $a = mpf(2.0, 100); - $b = mpf(2.0, 500); - $c = $a + $b; # gives 100 bits precision - -Mpf to string conversion via "" or the usual string contexts uses C<$#> the -same as normal float to string conversions, or defaults to C<%.g> if C<$#> -is not defined. C<%.g> means all significant digits in the selected -precision. - -=head2 GMP class - -The following functions are available in the GMP class, - -=over 4 - -=item - -fits_slong_p, get_d, get_d_2exp, get_si, get_str, integer_p, printf, sgn, -sprintf, version - -=back - -C accepts any integer, string, float, mpz, mpq or mpf operands -and returns a float and an integer exponent, - - ($dbl, $exp) = get_d_2exp (mpf ("3.0")); - # dbl is 0.75, exp is 2 - -C takes an optional second argument which is the base, defaulting -to decimal. A negative base means upper case, as per the C functions. For -integer, integer string, mpz or mpq operands a string is returned. - - use GMP qw(:all); - use GMP::Mpq qw(:all); - print get_str(mpq(-5,8)),"\n"; # -5/8 - print get_str(255,16),"\n"; # ff - -For float, float strings or mpf operands, C accepts an optional -third parameter being how many digits to produce, defaulting to 0 which -means all digits. (Only as many digits as can be accurately represented by -the float precision are ever produced though.) A string/exponent pair is -returned, as per the C mpf_get_str function. For example, - - use GMP qw(:all); - use GMP::Mpf qw(:all); - ($s, $e) = get_str(111.111111111, 10, 4); - printf ".$se$e\n"; # .1111e3 - ($s, $e) = get_str(1.625, 10); - print "0.$s*10^$e\n"; # 0.1625*10^1 - ($s, $e) = get_str(mpf(2)**20, 16); - printf ".%s@%x\n", $s, $e; # .1@14 - -C and C allow formatted output of GMP types. mpz and mpq -values can be used with integer conversions (d, o, x, X) and mpf with float -conversions (f, e, E, g, G). All the standard perl printf features are -available too. For example, - - use GMP::Mpz qw(mpz); - use GMP::Mpf qw(mpf); - GMP::printf ("%d %d %s", 123, mpz(2)**128, 'foo'); - GMP::printf STDERR "%.40f", mpf(1.234); - -In perl 5.6.1 it doesn't seem to work to export C, the plain builtin -C is reached unless calls are C<&printf()> style. Explicit use of -C is suggested. C doesn't suffer this problem. - - use GMP qw(sprintf); - use GMP::Mpq qw(mpq); - $s = sprintf "%x", mpq(15,16); - -C is not exported by default or by tag :all, calling it as -C is recommended. It returns the GMP library version -string, which is not to be confused with the module version number. - -The other GMP module functions behave as per the corresponding GMP routines, -and accept any integer, string, float, mpz, mpq or mpf. For example, - - use GMP qw(:all); - use GMP::Mpz qw(mpz); - $z = mpz(123); - print sgn($z); # gives 1 - -Because each of GMP::Mpz, GMP::Mpq and GMP::Mpf is a sub-class of GMP, -C<-E> style calls work too. - - use GMP qw(:all); - use GMP::Mpq qw(mpf); - $q = mpq(-5,7); - if ($q->integer_p()) # false - ... - -=head2 GMP::Rand - -This class provides objects holding an algorithm and state for random number -generation. C creates a new object, for example, - - use GMP::Rand qw(randstate); - $r = randstate(); - $r = randstate('lc_2exp_size', 64); - $r = randstate('lc_2exp', 43840821, 1, 32); - $r = randstate('mt'); - $r = randstate($another_r); - -With no parameters this corresponds to the C function -C, and is a compromise between speed and randomness. -'lc_2exp_size' corresponds to C, 'lc_2exp' -corresponds to C, and 'mt' corresponds to -C. Or when passed another randstate object, a copy of that -object is made. - -'lc_2exp_size' can fail if the requested size is bigger than the internal -table provides for, in which case undef is returned. The maximum size -currently supported is 128. The other forms always succeed. - -A randstate can be seeded with an integer or mpz, using the C method. -/dev/random might be a good source of randomness, or time() or -Time::HiRes::time() might be adequate, depending on the application. - - $r->seed(time())); - -Random numbers can be generated with the following functions, - -=over 4 - -=item - -mpf_urandomb, mpz_rrandomb, mpz_urandomb, mpz_urandomm, -gmp_urandomb_ui, gmp_urandomm_ui - -=back - -Each constructs a new mpz or mpf and with a distribution per the -corresponding GMP function. For example, - - use GMP::Rand (:all); - $r = randstate(); - $a = mpz_urandomb($r,256); # uniform mpz, 256 bits - $b = mpz_urandomm($r,mpz(3)**100); # uniform mpz, 0 to 3**100-1 - $c = mpz_rrandomb($r,1024); # special mpz, 1024 bits - $f = mpf_urandomb($r,128); # uniform mpf, 128 bits, 0<=$f<1 - $f = gmp_urandomm_ui($r,56); # uniform int, 0 to 55 - -=head2 Coercion - -Arguments to operators and functions are converted as necessary to the -appropriate type. For instance C<**> requires an unsigned integer exponent, -and an mpq argument will be converted, so long as it's an integer in the -apropriate range. - - use GMP::Mpz (mpz); - use GMP::Mpq (mpq); - $p = mpz(3) ** mpq(45); # allowed, 45 is an integer - -It's an error if a conversion to an integer or mpz would cause any -truncation. For example, - - use GMP::Mpz (mpz); - $p = mpz(3) + 1.25; # not allowed - $p = mpz(3) + mpz(1.25); # allowed, explicit truncation - -Comparisons, however, accept any combination of operands and are always done -exactly. For example, - - use GMP::Mpz (mpz); - print mpz(3) < 3.1; # true - -Variables used on the left of an assignment operator like C<+=> are subject -to coercion too. An integer, float or string will change type when an mpz, -mpq or mpf is applied to it. For example, - - use GMP::Mpz (mpz); - $a = 1; - $a += mpz(1234); # $a becomes an mpz - -=head2 Overloading - -The rule for binary operators in the C mechanism is that if both -operands are class objects then the method from the first is used. This -determines the result type when mixing GMP classes. For example, - - use GMP::Mpz (mpz); - use GMP::Mpq (mpq); - use GMP::Mpf (mpf); - $z = mpz(123); - $q = mpq(3,2); - $f = mpf(1.375) - print $q+$f; # gives an mpq - print $f+$z; # gives an mpf - print $z+$f; # not allowed, would lose precision - -=head2 Constants - -A special tag C<:constants> is recognised in the module exports list. It -doesn't select any functions, but indicates that perl constants should be -GMP objects. This can only be used on one of GMP::Mpz, GMP::Mpq or GMP::Mpf -at any one time, since they apply different rules. - -GMP::Mpz will treat constants as mpz's if they're integers, or ordinary -floats if not. For example, - - use GMP::Mpz qw(:constants); - print 764861287634126387126378128,"\n"; # an mpz - print 1.25,"\n"; # a float - -GMP::Mpq is similar, treating integers as mpq's and leaving floats to the -normal perl handling. Something like 3/4 is read as two integer mpq's and a -division, but that's fine since it gives the intended fraction. - - use GMP::Mpq qw(:constants); - print 3/4,"\n"; # an mpq - print 1.25,"\n"; # a float - -GMP::Mpf will treat all constants as mpf's using the default precision. -BEGIN blocks can be used to set that precision while the code is parsed. -For example, - - use GMP::Mpf qw(:constants); - BEGIN { GMP::Mpf::set_default_prec(256); } - print 1/3; - BEGIN { GMP::Mpf::set_default_prec(64); } - print 5/7; - -A similar special tag :noconstants is recognised to turn off the constants -feature. For example, - - use GMP::Mpz qw(:constants); - print 438249738748174928193,"\n"; # an mpz - use GMP::Mpz qw(:noconstants); - print 438249738748174928193,"\n"; # now a float - -All three 'integer', 'binary' and 'float' constant methods are captured. -'float' is captured even for GMP::Mpz and GMP::Mpq since perl by default -treats integer strings as floats if they don't fit a plain integer. - -=head1 SEE ALSO - -GMP manual, L, L. - -=head1 BUGS - -In perl 5.005_03 on i386 FreeBSD, the overloaded constants sometimes provoke -seg faults. Don't know if that's a perl bug or a GMP module bug, though it -does seem to go bad before reaching anything in GMP.xs. - -There's no way to specify an arbitrary base when converting a string to an -mpz (or mpq or mpf), only hex or octal with 0x or 0 (for mpz and mpq, but -not for mpf). - -These modules are not reentrant or thread safe, due to the implementation of -the XSUBs. - -Returning a new object from the various functions is convenient, but -assignment versions could avoid creating new objects. Perhaps they could be -named after the C language functions, eg. mpq_inv($q,$q); - -It'd be good if C and C gave lvalues so the underlying mpq could -be manipulated. - -C could usefully accept %b for mpz, mpq and mpf, and perhaps %x for -mpf too. - -C returning different style values for integer versus float is a -bit unfortunate. With mpz, mpq and mpf objects there's no doubt what it -will do, but on a plain scalar its action depends on whether the scalar was -promoted to a float at any stage, and then on the GMP module rules about -using the integer or float part. - -=head1 INTERNALS - -In usual perl object style, an mpz is a reference to an object blessed into -class C. The object holds a pointer to the C language C -structure. Similarly for mpq, mpf and randstate. - -A free list of mpz and mpq values is kept to avoid repeated initializing and -clearing when objects are created and destroyed. This aims to help speed, -but it's not clear whether it's really needed. - -mpf doesn't use a free list because the precision of new objects can be -different each time. - -No interface to C is provided. It wouldn't be very useful -since there's no way to make an operation store its result in a particular -object. The plain C is useful though, for truncating to a lower -precision, or as a sort of directive that subsequent calculations involving -that variable should use a higher precision. - -The overheads of perl dynamic typing (operator dispatch, operand type -checking or coercion) will mean this interface is slower than using C -directly. - -Some assertion checking is available as a compile-time option. - -=head1 COPYRIGHT - -Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published -by the Free Software Foundation; either version 2.1 of the License, or (at -your option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. - -=cut - -# Local variables: -# perl-indent-level: 2 -# fill-column: 76 -# End: diff --git a/src/gmp/demos/perl/GMP.xs b/src/gmp/demos/perl/GMP.xs deleted file mode 100644 index 4aa4b34aa..000000000 --- a/src/gmp/demos/perl/GMP.xs +++ /dev/null @@ -1,3203 +0,0 @@ -/* GMP module external subroutines. - -Copyright 2001, 2002, 2003 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -MA 02110-1301, USA. */ - - -/* Notes: - - Routines are grouped with the alias feature and a table of function - pointers where possible, since each xsub routine ends up with quite a bit - of code size. Different combinations of arguments and return values have - to be separate though. - - The "INTERFACE:" feature isn't available in perl 5.005 and so isn't used. - "ALIAS:" requires a table lookup with CvXSUBANY(cv).any_i32 (which is - "ix") whereas "INTERFACE:" would have CvXSUBANY(cv).any_dptr as the - function pointer immediately. - - Mixed-type swapped-order assignments like "$a = 123; $a += mpz(456);" - invoke the plain overloaded "+", not "+=", which makes life easier. - - mpz_assume etc types are used with the overloaded operators since such - operators are always called with a class object as the first argument, we - don't need an sv_derived_from() lookup to check. There's assert()s in - MPX_ASSUME() for this though. - - The overload_constant routines reached via overload::constant get 4 - arguments in perl 5.6, not the 3 as documented. This is apparently a - bug, using "..." lets us ignore the extra one. - - There's only a few "si" functions in gmp, so usually SvIV values get - handled with an mpz_set_si into a temporary and then a full precision mpz - routine. This is reasonably efficient. - - Argument types are checked, with a view to preserving all bits in the - operand. Perl is a bit looser in its arithmetic, allowing rounding or - truncation to an intended operand type (IV, UV or NV). - - Bugs: - - The memory leak detection attempted in GMP::END() doesn't work when mpz's - are created as constants because END() is called before they're - destroyed. What's the right place to hook such a check? - - See the bugs section of GMP.pm too. */ - - -/* Comment this out to get assertion checking. */ -#define NDEBUG - -/* Change this to "#define TRACE(x) x" for some diagnostics. */ -#define TRACE(x) - - -#include -#include - -#include "EXTERN.h" -#include "perl.h" -#include "XSUB.h" -#include "patchlevel.h" - -#include "gmp.h" - - -/* Perl 5.005 doesn't have SvIsUV, only 5.6 and up. - Perl 5.8 has SvUOK, but not 5.6, so we don't use that. */ -#ifndef SvIsUV -#define SvIsUV(sv) 0 -#endif -#ifndef SvUVX -#define SvUVX(sv) (croak("GMP: oops, shouldn't be using SvUVX"), 0) -#endif - - -/* Code which doesn't check anything itself, but exists to support other - assert()s. */ -#ifdef NDEBUG -#define assert_support(x) -#else -#define assert_support(x) x -#endif - -/* LONG_MAX + 1 and ULONG_MAX + 1, as a doubles */ -#define LONG_MAX_P1_AS_DOUBLE ((double) ((unsigned long) LONG_MAX + 1)) -#define ULONG_MAX_P1_AS_DOUBLE (2.0 * (double) ((unsigned long) ULONG_MAX/2 + 1)) - -/* Check for perl version "major.minor". - Perl 5.004 doesn't have PERL_REVISION and PERL_VERSION, but that's ok, - we're only interested in tests above that. */ -#if defined (PERL_REVISION) && defined (PERL_VERSION) -#define PERL_GE(major,minor) \ - (PERL_REVISION > (major) \ - || ((major) == PERL_REVISION && PERL_VERSION >= (minor))) -#else -#define PERL_GE(major,minor) (0) -#endif -#define PERL_LT(major,minor) (! PERL_GE(major,minor)) - -/* sv_derived_from etc in 5.005 took "char *" rather than "const char *". - Avoid some compiler warnings by using const only where it works. */ -#if PERL_LT (5,6) -#define classconst -#else -#define classconst const -#endif - -/* In a MINGW or Cygwin DLL build of gmp, the various gmp functions are - given with dllimport directives, which prevents them being used as - initializers for constant data. We give function tables as - "static_functable const ...", which is normally "static const", but for - mingw expands to just "const" making the table an automatic with a - run-time initializer. - - In gcc 3.3.1, the function tables initialized like this end up getting - all the __imp__foo values fetched, even though just one or two will be - used. This is wasteful, but probably not too bad. */ - -#if defined (__MINGW32__) || defined (__CYGWIN__) -#define static_functable -#else -#define static_functable static -#endif - -#define GMP_MALLOC_ID 42 - -static classconst char mpz_class[] = "GMP::Mpz"; -static classconst char mpq_class[] = "GMP::Mpq"; -static classconst char mpf_class[] = "GMP::Mpf"; -static classconst char rand_class[] = "GMP::Rand"; - -static HV *mpz_class_hv; -static HV *mpq_class_hv; -static HV *mpf_class_hv; - -assert_support (static long mpz_count = 0;) -assert_support (static long mpq_count = 0;) -assert_support (static long mpf_count = 0;) -assert_support (static long rand_count = 0;) - -#define TRACE_ACTIVE() \ - assert_support \ - (TRACE (printf (" active %ld mpz, %ld mpq, %ld mpf, %ld randstate\n", \ - mpz_count, mpq_count, mpf_count, rand_count))) - - -/* Each "struct mpz_elem" etc is an mpz_t with a link field tacked on the - end so they can be held on a linked list. */ - -#define CREATE_MPX(type) \ - \ - /* must have mpz_t etc first, for sprintf below */ \ - struct type##_elem { \ - type##_t m; \ - struct type##_elem *next; \ - }; \ - typedef struct type##_elem *type; \ - typedef struct type##_elem *type##_assume; \ - typedef type##_ptr type##_coerce; \ - \ - static type type##_freelist = NULL; \ - \ - static type \ - new_##type (void) \ - { \ - type p; \ - TRACE (printf ("new %s\n", type##_class)); \ - if (type##_freelist != NULL) \ - { \ - p = type##_freelist; \ - type##_freelist = type##_freelist->next; \ - } \ - else \ - { \ - New (GMP_MALLOC_ID, p, 1, struct type##_elem); \ - type##_init (p->m); \ - } \ - TRACE (printf (" p=%p\n", p)); \ - assert_support (type##_count++); \ - TRACE_ACTIVE (); \ - return p; \ - } \ - -CREATE_MPX (mpz) -CREATE_MPX (mpq) - -typedef mpf_ptr mpf; -typedef mpf_ptr mpf_assume; -typedef mpf_ptr mpf_coerce_st0; -typedef mpf_ptr mpf_coerce_def; - - -static mpf -new_mpf (unsigned long prec) -{ - mpf p; - New (GMP_MALLOC_ID, p, 1, __mpf_struct); - mpf_init2 (p, prec); - TRACE (printf (" mpf p=%p\n", p)); - assert_support (mpf_count++); - TRACE_ACTIVE (); - return p; -} - - -/* tmp_mpf_t records an allocated precision with an mpf_t so changes of - precision can be done with just an mpf_set_prec_raw. */ - -struct tmp_mpf_struct { - mpf_t m; - unsigned long allocated_prec; -}; -typedef const struct tmp_mpf_struct *tmp_mpf_srcptr; -typedef struct tmp_mpf_struct *tmp_mpf_ptr; -typedef struct tmp_mpf_struct tmp_mpf_t[1]; - -#define tmp_mpf_init(f) \ - do { \ - mpf_init (f->m); \ - f->allocated_prec = mpf_get_prec (f->m); \ - } while (0) - -static void -tmp_mpf_grow (tmp_mpf_ptr f, unsigned long prec) -{ - mpf_set_prec_raw (f->m, f->allocated_prec); - mpf_set_prec (f->m, prec); - f->allocated_prec = mpf_get_prec (f->m); -} - -#define tmp_mpf_shrink(f) tmp_mpf_grow (f, 1L) - -#define tmp_mpf_set_prec(f,prec) \ - do { \ - if (prec > f->allocated_prec) \ - tmp_mpf_grow (f, prec); \ - else \ - mpf_set_prec_raw (f->m, prec); \ - } while (0) - - -static mpz_t tmp_mpz_0, tmp_mpz_1, tmp_mpz_2; -static mpq_t tmp_mpq_0, tmp_mpq_1; -static tmp_mpf_t tmp_mpf_0, tmp_mpf_1; - -/* for GMP::Mpz::export */ -#define tmp_mpz_4 tmp_mpz_2 - - -#define FREE_MPX_FREELIST(p,type) \ - do { \ - TRACE (printf ("free %s\n", type##_class)); \ - p->next = type##_freelist; \ - type##_freelist = p; \ - assert_support (type##_count--); \ - TRACE_ACTIVE (); \ - assert (type##_count >= 0); \ - } while (0) - -/* this version for comparison, if desired */ -#define FREE_MPX_NOFREELIST(p,type) \ - do { \ - TRACE (printf ("free %s\n", type##_class)); \ - type##_clear (p->m); \ - Safefree (p); \ - assert_support (type##_count--); \ - TRACE_ACTIVE (); \ - assert (type##_count >= 0); \ - } while (0) - -#define free_mpz(z) FREE_MPX_FREELIST (z, mpz) -#define free_mpq(q) FREE_MPX_FREELIST (q, mpq) - - -/* Return a new mortal SV holding the given mpx_ptr pointer. - class_hv should be one of mpz_class_hv etc. */ -#define MPX_NEWMORTAL(mpx_ptr, class_hv) \ - sv_bless (sv_setref_pv (sv_newmortal(), NULL, mpx_ptr), class_hv) - -/* Aliases for use in typemaps */ -typedef char *malloced_string; -typedef const char *const_string; -typedef const char *const_string_assume; -typedef char *string; -typedef SV *order_noswap; -typedef SV *dummy; -typedef SV *SV_copy_0; -typedef unsigned long ulong_coerce; -typedef __gmp_randstate_struct *randstate; -typedef UV gmp_UV; - -#define SvMPX(s,type) ((type) SvIV((SV*) SvRV(s))) -#define SvMPZ(s) SvMPX(s,mpz) -#define SvMPQ(s) SvMPX(s,mpq) -#define SvMPF(s) SvMPX(s,mpf) -#define SvRANDSTATE(s) SvMPX(s,randstate) - -#define MPX_ASSUME(x,sv,type) \ - do { \ - assert (sv_derived_from (sv, type##_class)); \ - x = SvMPX(sv,type); \ - } while (0) - -#define MPZ_ASSUME(z,sv) MPX_ASSUME(z,sv,mpz) -#define MPQ_ASSUME(q,sv) MPX_ASSUME(q,sv,mpq) -#define MPF_ASSUME(f,sv) MPX_ASSUME(f,sv,mpf) - -#define numberof(x) (sizeof (x) / sizeof ((x)[0])) -#define SGN(x) ((x)<0 ? -1 : (x) != 0) -#define ABS(x) ((x)>=0 ? (x) : -(x)) -#define double_integer_p(d) (floor (d) == (d)) - -#define x_mpq_integer_p(q) \ - (mpz_cmp_ui (mpq_denref(q), 1L) == 0) - -#define assert_table(ix) assert (ix >= 0 && ix < numberof (table)) - -#define SV_PTR_SWAP(x,y) \ - do { SV *__tmp = (x); (x) = (y); (y) = __tmp; } while (0) -#define MPF_PTR_SWAP(x,y) \ - do { mpf_ptr __tmp = (x); (x) = (y); (y) = __tmp; } while (0) - - -static void -class_or_croak (SV *sv, classconst char *cl) -{ - if (! sv_derived_from (sv, cl)) - croak("not type %s", cl); -} - - -/* These are macros, wrap them in functions. */ -static int -x_mpz_odd_p (mpz_srcptr z) -{ - return mpz_odd_p (z); -} -static int -x_mpz_even_p (mpz_srcptr z) -{ - return mpz_even_p (z); -} - -static void -x_mpq_pow_ui (mpq_ptr r, mpq_srcptr b, unsigned long e) -{ - mpz_pow_ui (mpq_numref(r), mpq_numref(b), e); - mpz_pow_ui (mpq_denref(r), mpq_denref(b), e); -} - - -static void * -my_gmp_alloc (size_t n) -{ - void *p; - TRACE (printf ("my_gmp_alloc %u\n", n)); - New (GMP_MALLOC_ID, p, n, char); - TRACE (printf (" p=%p\n", p)); - return p; -} - -static void * -my_gmp_realloc (void *p, size_t oldsize, size_t newsize) -{ - TRACE (printf ("my_gmp_realloc %p, %u to %u\n", p, oldsize, newsize)); - Renew (p, newsize, char); - TRACE (printf (" p=%p\n", p)); - return p; -} - -static void -my_gmp_free (void *p, size_t n) -{ - TRACE (printf ("my_gmp_free %p %u\n", p, n)); - Safefree (p); -} - - -#define my_mpx_set_svstr(type) \ - static void \ - my_##type##_set_svstr (type##_ptr x, SV *sv) \ - { \ - const char *str; \ - STRLEN len; \ - TRACE (printf (" my_" #type "_set_svstr\n")); \ - assert (SvPOK(sv) || SvPOKp(sv)); \ - str = SvPV (sv, len); \ - TRACE (printf (" str \"%s\"\n", str)); \ - if (type##_set_str (x, str, 0) != 0) \ - croak ("%s: invalid string: %s", type##_class, str); \ - } - -my_mpx_set_svstr(mpz) -my_mpx_set_svstr(mpq) -my_mpx_set_svstr(mpf) - - -/* very slack */ -static int -x_mpq_cmp_si (mpq_srcptr x, long yn, unsigned long yd) -{ - mpq y; - int ret; - y = new_mpq (); - mpq_set_si (y->m, yn, yd); - ret = mpq_cmp (x, y->m); - free_mpq (y); - return ret; -} - -static int -x_mpq_fits_slong_p (mpq_srcptr q) -{ - return x_mpq_cmp_si (q, LONG_MIN, 1L) >= 0 - && mpq_cmp_ui (q, LONG_MAX, 1L) <= 0; -} - -static int -x_mpz_cmp_q (mpz_ptr x, mpq_srcptr y) -{ - int ret; - mpz_set_ui (mpq_denref(tmp_mpq_0), 1L); - mpz_swap (mpq_numref(tmp_mpq_0), x); - ret = mpq_cmp (tmp_mpq_0, y); - mpz_swap (mpq_numref(tmp_mpq_0), x); - return ret; -} - -static int -x_mpz_cmp_f (mpz_srcptr x, mpf_srcptr y) -{ - tmp_mpf_set_prec (tmp_mpf_0, mpz_sizeinbase (x, 2)); - mpf_set_z (tmp_mpf_0->m, x); - return mpf_cmp (tmp_mpf_0->m, y); -} - - -#define USE_UNKNOWN 0 -#define USE_IVX 1 -#define USE_UVX 2 -#define USE_NVX 3 -#define USE_PVX 4 -#define USE_MPZ 5 -#define USE_MPQ 6 -#define USE_MPF 7 - -/* mg_get is called every time we get a value, even if the private flags are - still set from a previous such call. This is the same as as SvIV and - friends do. - - When POK, we use the PV, even if there's an IV or NV available. This is - because it's hard to be sure there wasn't any rounding in establishing - the IV and/or NV. Cases of overflow, where the PV should definitely be - used, are easy enough to spot, but rounding is hard. So although IV or - NV would be more efficient, we must use the PV to be sure of getting all - the data. Applications should convert once to mpz, mpq or mpf when using - a value repeatedly. - - Zany dual-type scalars like $! where the IV is an error code and the PV - is an error description string won't work with this preference for PV, - but that's too bad. Such scalars should be rare, and unlikely to be used - in bignum calculations. - - When IOK and NOK are both set, we would prefer to use the IV since it can - be converted more efficiently, and because on a 64-bit system the NV may - have less bits than the IV. The following rules are applied, - - - If the NV is not an integer, then we must use that NV, since clearly - the IV was merely established by rounding and is not the full value. - - - In perl prior to 5.8, an NV too big for an IV leaves an overflow value - 0xFFFFFFFF. If the NV is too big to fit an IV then clearly it's the NV - which is the true value and must be used. - - - In perl 5.8 and up, such an overflow doesn't set IOK, so that test is - unnecessary. However when coming from get-magic, IOKp _is_ set, and we - must check for overflow the same as in older perl. - - FIXME: - - We'd like to call mg_get just once, but unfortunately sv_derived_from() - will call it for each of our checks. We could do a string compare like - sv_isa ourselves, but that only tests the exact class, it doesn't - recognise subclassing. There doesn't seem to be a public interface to - the subclassing tests (in the internal isa_lookup() function). */ - -int -use_sv (SV *sv) -{ - double d; - - if (SvGMAGICAL(sv)) - { - mg_get(sv); - - if (SvPOKp(sv)) - return USE_PVX; - - if (SvIOKp(sv)) - { - if (SvIsUV(sv)) - { - if (SvNOKp(sv)) - goto u_or_n; - return USE_UVX; - } - else - { - if (SvNOKp(sv)) - goto i_or_n; - return USE_IVX; - } - } - - if (SvNOKp(sv)) - return USE_NVX; - - goto rok_or_unknown; - } - - if (SvPOK(sv)) - return USE_PVX; - - if (SvIOK(sv)) - { - if (SvIsUV(sv)) - { - if (SvNOK(sv)) - { - if (PERL_LT (5, 8)) - { - u_or_n: - d = SvNVX(sv); - if (d >= ULONG_MAX_P1_AS_DOUBLE || d < 0.0) - return USE_NVX; - } - d = SvNVX(sv); - if (d != floor (d)) - return USE_NVX; - } - return USE_UVX; - } - else - { - if (SvNOK(sv)) - { - if (PERL_LT (5, 8)) - { - i_or_n: - d = SvNVX(sv); - if (d >= LONG_MAX_P1_AS_DOUBLE || d < (double) LONG_MIN) - return USE_NVX; - } - d = SvNVX(sv); - if (d != floor (d)) - return USE_NVX; - } - return USE_IVX; - } - } - - if (SvNOK(sv)) - return USE_NVX; - - rok_or_unknown: - if (SvROK(sv)) - { - if (sv_derived_from (sv, mpz_class)) - return USE_MPZ; - if (sv_derived_from (sv, mpq_class)) - return USE_MPQ; - if (sv_derived_from (sv, mpf_class)) - return USE_MPF; - } - - return USE_UNKNOWN; -} - - -/* Coerce sv to an mpz. Use tmp to hold the converted value if sv isn't - already an mpz (or an mpq of which the numerator can be used). Return - the chosen mpz (tmp or the contents of sv). */ - -static mpz_ptr -coerce_mpz_using (mpz_ptr tmp, SV *sv, int use) -{ - switch (use) { - case USE_IVX: - mpz_set_si (tmp, SvIVX(sv)); - return tmp; - - case USE_UVX: - mpz_set_ui (tmp, SvUVX(sv)); - return tmp; - - case USE_NVX: - { - double d; - d = SvNVX(sv); - if (! double_integer_p (d)) - croak ("cannot coerce non-integer double to mpz"); - mpz_set_d (tmp, d); - return tmp; - } - - case USE_PVX: - my_mpz_set_svstr (tmp, sv); - return tmp; - - case USE_MPZ: - return SvMPZ(sv)->m; - - case USE_MPQ: - { - mpq q = SvMPQ(sv); - if (! x_mpq_integer_p (q->m)) - croak ("cannot coerce non-integer mpq to mpz"); - return mpq_numref(q->m); - } - - case USE_MPF: - { - mpf f = SvMPF(sv); - if (! mpf_integer_p (f)) - croak ("cannot coerce non-integer mpf to mpz"); - mpz_set_f (tmp, f); - return tmp; - } - - default: - croak ("cannot coerce to mpz"); - } -} -static mpz_ptr -coerce_mpz (mpz_ptr tmp, SV *sv) -{ - return coerce_mpz_using (tmp, sv, use_sv (sv)); -} - - -/* Coerce sv to an mpq. If sv is an mpq then just return that, otherwise - use tmp to hold the converted value and return that. */ - -static mpq_ptr -coerce_mpq_using (mpq_ptr tmp, SV *sv, int use) -{ - TRACE (printf ("coerce_mpq_using %p %d\n", tmp, use)); - switch (use) { - case USE_IVX: - mpq_set_si (tmp, SvIVX(sv), 1L); - return tmp; - - case USE_UVX: - mpq_set_ui (tmp, SvUVX(sv), 1L); - return tmp; - - case USE_NVX: - mpq_set_d (tmp, SvNVX(sv)); - return tmp; - - case USE_PVX: - my_mpq_set_svstr (tmp, sv); - return tmp; - - case USE_MPZ: - mpq_set_z (tmp, SvMPZ(sv)->m); - return tmp; - - case USE_MPQ: - return SvMPQ(sv)->m; - - case USE_MPF: - mpq_set_f (tmp, SvMPF(sv)); - return tmp; - - default: - croak ("cannot coerce to mpq"); - } -} -static mpq_ptr -coerce_mpq (mpq_ptr tmp, SV *sv) -{ - return coerce_mpq_using (tmp, sv, use_sv (sv)); -} - - -static void -my_mpf_set_sv_using (mpf_ptr f, SV *sv, int use) -{ - switch (use) { - case USE_IVX: - mpf_set_si (f, SvIVX(sv)); - break; - - case USE_UVX: - mpf_set_ui (f, SvUVX(sv)); - break; - - case USE_NVX: - mpf_set_d (f, SvNVX(sv)); - break; - - case USE_PVX: - my_mpf_set_svstr (f, sv); - break; - - case USE_MPZ: - mpf_set_z (f, SvMPZ(sv)->m); - break; - - case USE_MPQ: - mpf_set_q (f, SvMPQ(sv)->m); - break; - - case USE_MPF: - mpf_set (f, SvMPF(sv)); - break; - - default: - croak ("cannot coerce to mpf"); - } -} - -/* Coerce sv to an mpf. If sv is an mpf then just return that, otherwise - use tmp to hold the converted value (with prec precision). */ -static mpf_ptr -coerce_mpf_using (tmp_mpf_ptr tmp, SV *sv, unsigned long prec, int use) -{ - if (use == USE_MPF) - return SvMPF(sv); - - tmp_mpf_set_prec (tmp, prec); - my_mpf_set_sv_using (tmp->m, sv, use); - return tmp->m; -} -static mpf_ptr -coerce_mpf (tmp_mpf_ptr tmp, SV *sv, unsigned long prec) -{ - return coerce_mpf_using (tmp, sv, prec, use_sv (sv)); -} - - -/* Coerce xv to an mpf and store the pointer in x, ditto for yv to x. If - one of xv or yv is an mpf then use it for the precision, otherwise use - the default precision. */ -unsigned long -coerce_mpf_pair (mpf *xp, SV *xv, mpf *yp, SV *yv) -{ - int x_use = use_sv (xv); - int y_use = use_sv (yv); - unsigned long prec; - mpf x, y; - - if (x_use == USE_MPF) - { - x = SvMPF(xv); - prec = mpf_get_prec (x); - y = coerce_mpf_using (tmp_mpf_0, yv, prec, y_use); - } - else - { - y = coerce_mpf_using (tmp_mpf_0, yv, mpf_get_default_prec(), y_use); - prec = mpf_get_prec (y); - x = coerce_mpf_using (tmp_mpf_1, xv, prec, x_use); - } - *xp = x; - *yp = y; - return prec; -} - - -/* Note that SvUV is not used, since it merely treats the signed IV as if it - was unsigned. We get an IV and check its sign. */ -static unsigned long -coerce_ulong (SV *sv) -{ - long n; - - switch (use_sv (sv)) { - case USE_IVX: - n = SvIVX(sv); - negative_check: - if (n < 0) - goto range_error; - return n; - - case USE_UVX: - return SvUVX(sv); - - case USE_NVX: - { - double d; - d = SvNVX(sv); - if (! double_integer_p (d)) - goto integer_error; - n = SvIV(sv); - } - goto negative_check; - - case USE_PVX: - /* FIXME: Check the string is an integer. */ - n = SvIV(sv); - goto negative_check; - - case USE_MPZ: - { - mpz z = SvMPZ(sv); - if (! mpz_fits_ulong_p (z->m)) - goto range_error; - return mpz_get_ui (z->m); - } - - case USE_MPQ: - { - mpq q = SvMPQ(sv); - if (! x_mpq_integer_p (q->m)) - goto integer_error; - if (! mpz_fits_ulong_p (mpq_numref (q->m))) - goto range_error; - return mpz_get_ui (mpq_numref (q->m)); - } - - case USE_MPF: - { - mpf f = SvMPF(sv); - if (! mpf_integer_p (f)) - goto integer_error; - if (! mpf_fits_ulong_p (f)) - goto range_error; - return mpf_get_ui (f); - } - - default: - croak ("cannot coerce to ulong"); - } - - integer_error: - croak ("not an integer"); - - range_error: - croak ("out of range for ulong"); -} - - -static long -coerce_long (SV *sv) -{ - switch (use_sv (sv)) { - case USE_IVX: - return SvIVX(sv); - - case USE_UVX: - { - UV u = SvUVX(sv); - if (u > (UV) LONG_MAX) - goto range_error; - return u; - } - - case USE_NVX: - { - double d = SvNVX(sv); - if (! double_integer_p (d)) - goto integer_error; - return SvIV(sv); - } - - case USE_PVX: - /* FIXME: Check the string is an integer. */ - return SvIV(sv); - - case USE_MPZ: - { - mpz z = SvMPZ(sv); - if (! mpz_fits_slong_p (z->m)) - goto range_error; - return mpz_get_si (z->m); - } - - case USE_MPQ: - { - mpq q = SvMPQ(sv); - if (! x_mpq_integer_p (q->m)) - goto integer_error; - if (! mpz_fits_slong_p (mpq_numref (q->m))) - goto range_error; - return mpz_get_si (mpq_numref (q->m)); - } - - case USE_MPF: - { - mpf f = SvMPF(sv); - if (! mpf_integer_p (f)) - goto integer_error; - if (! mpf_fits_slong_p (f)) - goto range_error; - return mpf_get_si (f); - } - - default: - croak ("cannot coerce to long"); - } - - integer_error: - croak ("not an integer"); - - range_error: - croak ("out of range for ulong"); -} - - -/* ------------------------------------------------------------------------- */ - -MODULE = GMP PACKAGE = GMP - -BOOT: - TRACE (printf ("GMP boot\n")); - mp_set_memory_functions (my_gmp_alloc, my_gmp_realloc, my_gmp_free); - mpz_init (tmp_mpz_0); - mpz_init (tmp_mpz_1); - mpz_init (tmp_mpz_2); - mpq_init (tmp_mpq_0); - mpq_init (tmp_mpq_1); - tmp_mpf_init (tmp_mpf_0); - tmp_mpf_init (tmp_mpf_1); - mpz_class_hv = gv_stashpv (mpz_class, 1); - mpq_class_hv = gv_stashpv (mpq_class, 1); - mpf_class_hv = gv_stashpv (mpf_class, 1); - - -void -END() -CODE: - TRACE (printf ("GMP end\n")); - TRACE_ACTIVE (); - /* These are not always true, see Bugs at the top of the file. */ - /* assert (mpz_count == 0); */ - /* assert (mpq_count == 0); */ - /* assert (mpf_count == 0); */ - /* assert (rand_count == 0); */ - - -const_string -version() -CODE: - RETVAL = gmp_version; -OUTPUT: - RETVAL - - -bool -fits_slong_p (sv) - SV *sv -CODE: - switch (use_sv (sv)) { - case USE_IVX: - RETVAL = 1; - break; - - case USE_UVX: - { - UV u = SvUVX(sv); - RETVAL = (u <= LONG_MAX); - } - break; - - case USE_NVX: - { - double d = SvNVX(sv); - RETVAL = (d >= (double) LONG_MIN && d < LONG_MAX_P1_AS_DOUBLE); - } - break; - - case USE_PVX: - { - STRLEN len; - const char *str = SvPV (sv, len); - if (mpq_set_str (tmp_mpq_0, str, 0) == 0) - RETVAL = x_mpq_fits_slong_p (tmp_mpq_0); - else - { - /* enough precision for a long */ - tmp_mpf_set_prec (tmp_mpf_0, 2*mp_bits_per_limb); - if (mpf_set_str (tmp_mpf_0->m, str, 10) != 0) - croak ("GMP::fits_slong_p invalid string format"); - RETVAL = mpf_fits_slong_p (tmp_mpf_0->m); - } - } - break; - - case USE_MPZ: - RETVAL = mpz_fits_slong_p (SvMPZ(sv)->m); - break; - - case USE_MPQ: - RETVAL = x_mpq_fits_slong_p (SvMPQ(sv)->m); - break; - - case USE_MPF: - RETVAL = mpf_fits_slong_p (SvMPF(sv)); - break; - - default: - croak ("GMP::fits_slong_p invalid argument"); - } -OUTPUT: - RETVAL - - -double -get_d (sv) - SV *sv -CODE: - switch (use_sv (sv)) { - case USE_IVX: - RETVAL = (double) SvIVX(sv); - break; - - case USE_UVX: - RETVAL = (double) SvUVX(sv); - break; - - case USE_NVX: - RETVAL = SvNVX(sv); - break; - - case USE_PVX: - { - STRLEN len; - RETVAL = atof(SvPV(sv, len)); - } - break; - - case USE_MPZ: - RETVAL = mpz_get_d (SvMPZ(sv)->m); - break; - - case USE_MPQ: - RETVAL = mpq_get_d (SvMPQ(sv)->m); - break; - - case USE_MPF: - RETVAL = mpf_get_d (SvMPF(sv)); - break; - - default: - croak ("GMP::get_d invalid argument"); - } -OUTPUT: - RETVAL - - -void -get_d_2exp (sv) - SV *sv -PREINIT: - double ret; - long exp; -PPCODE: - switch (use_sv (sv)) { - case USE_IVX: - ret = (double) SvIVX(sv); - goto use_frexp; - - case USE_UVX: - ret = (double) SvUVX(sv); - goto use_frexp; - - case USE_NVX: - { - int i_exp; - ret = SvNVX(sv); - use_frexp: - ret = frexp (ret, &i_exp); - exp = i_exp; - } - break; - - case USE_PVX: - /* put strings through mpf to give full exp range */ - tmp_mpf_set_prec (tmp_mpf_0, DBL_MANT_DIG); - my_mpf_set_svstr (tmp_mpf_0->m, sv); - ret = mpf_get_d_2exp (&exp, tmp_mpf_0->m); - break; - - case USE_MPZ: - ret = mpz_get_d_2exp (&exp, SvMPZ(sv)->m); - break; - - case USE_MPQ: - tmp_mpf_set_prec (tmp_mpf_0, DBL_MANT_DIG); - mpf_set_q (tmp_mpf_0->m, SvMPQ(sv)->m); - ret = mpf_get_d_2exp (&exp, tmp_mpf_0->m); - break; - - case USE_MPF: - ret = mpf_get_d_2exp (&exp, SvMPF(sv)); - break; - - default: - croak ("GMP::get_d_2exp invalid argument"); - } - PUSHs (sv_2mortal (newSVnv (ret))); - PUSHs (sv_2mortal (newSViv (exp))); - - -long -get_si (sv) - SV *sv -CODE: - switch (use_sv (sv)) { - case USE_IVX: - RETVAL = SvIVX(sv); - break; - - case USE_UVX: - RETVAL = SvUVX(sv); - break; - - case USE_NVX: - RETVAL = (long) SvNVX(sv); - break; - - case USE_PVX: - RETVAL = SvIV(sv); - break; - - case USE_MPZ: - RETVAL = mpz_get_si (SvMPZ(sv)->m); - break; - - case USE_MPQ: - mpz_set_q (tmp_mpz_0, SvMPQ(sv)->m); - RETVAL = mpz_get_si (tmp_mpz_0); - break; - - case USE_MPF: - RETVAL = mpf_get_si (SvMPF(sv)); - break; - - default: - croak ("GMP::get_si invalid argument"); - } -OUTPUT: - RETVAL - - -void -get_str (sv, ...) - SV *sv -PREINIT: - char *str; - mp_exp_t exp; - mpz_ptr z; - mpq_ptr q; - mpf f; - int base; - int ndigits; -PPCODE: - TRACE (printf ("GMP::get_str\n")); - - if (items >= 2) - base = coerce_long (ST(1)); - else - base = 10; - TRACE (printf (" base=%d\n", base)); - - if (items >= 3) - ndigits = coerce_long (ST(2)); - else - ndigits = 10; - TRACE (printf (" ndigits=%d\n", ndigits)); - - EXTEND (SP, 2); - - switch (use_sv (sv)) { - case USE_IVX: - mpz_set_si (tmp_mpz_0, SvIVX(sv)); - get_tmp_mpz_0: - z = tmp_mpz_0; - goto get_mpz; - - case USE_UVX: - mpz_set_ui (tmp_mpz_0, SvUVX(sv)); - goto get_tmp_mpz_0; - - case USE_NVX: - /* only digits in the original double, not in the coerced form */ - if (ndigits == 0) - ndigits = DBL_DIG; - mpf_set_d (tmp_mpf_0->m, SvNVX(sv)); - f = tmp_mpf_0->m; - goto get_mpf; - - case USE_PVX: - { - /* get_str on a string is not much more than a base conversion */ - STRLEN len; - str = SvPV (sv, len); - if (mpz_set_str (tmp_mpz_0, str, 0) == 0) - { - z = tmp_mpz_0; - goto get_mpz; - } - else if (mpq_set_str (tmp_mpq_0, str, 0) == 0) - { - q = tmp_mpq_0; - goto get_mpq; - } - else - { - /* FIXME: Would like perhaps a precision equivalent to the - number of significant digits of the string, in its given - base. */ - tmp_mpf_set_prec (tmp_mpf_0, strlen(str)); - if (mpf_set_str (tmp_mpf_0->m, str, 10) == 0) - { - f = tmp_mpf_0->m; - goto get_mpf; - } - else - croak ("GMP::get_str invalid string format"); - } - } - break; - - case USE_MPZ: - z = SvMPZ(sv)->m; - get_mpz: - str = mpz_get_str (NULL, base, z); - push_str: - PUSHs (sv_2mortal (newSVpv (str, 0))); - break; - - case USE_MPQ: - q = SvMPQ(sv)->m; - get_mpq: - str = mpq_get_str (NULL, base, q); - goto push_str; - - case USE_MPF: - f = SvMPF(sv); - get_mpf: - str = mpf_get_str (NULL, &exp, base, 0, f); - PUSHs (sv_2mortal (newSVpv (str, 0))); - PUSHs (sv_2mortal (newSViv (exp))); - break; - - default: - croak ("GMP::get_str invalid argument"); - } - - -bool -integer_p (sv) - SV *sv -CODE: - switch (use_sv (sv)) { - case USE_IVX: - case USE_UVX: - RETVAL = 1; - break; - - case USE_NVX: - RETVAL = double_integer_p (SvNVX(sv)); - break; - - case USE_PVX: - { - /* FIXME: Maybe this should be done by parsing the string, not by an - actual conversion. */ - STRLEN len; - const char *str = SvPV (sv, len); - if (mpq_set_str (tmp_mpq_0, str, 0) == 0) - RETVAL = x_mpq_integer_p (tmp_mpq_0); - else - { - /* enough for all digits of the string */ - tmp_mpf_set_prec (tmp_mpf_0, strlen(str)+64); - if (mpf_set_str (tmp_mpf_0->m, str, 10) == 0) - RETVAL = mpf_integer_p (tmp_mpf_0->m); - else - croak ("GMP::integer_p invalid string format"); - } - } - break; - - case USE_MPZ: - RETVAL = 1; - break; - - case USE_MPQ: - RETVAL = x_mpq_integer_p (SvMPQ(sv)->m); - break; - - case USE_MPF: - RETVAL = mpf_integer_p (SvMPF(sv)); - break; - - default: - croak ("GMP::integer_p invalid argument"); - } -OUTPUT: - RETVAL - - -int -sgn (sv) - SV *sv -CODE: - switch (use_sv (sv)) { - case USE_IVX: - RETVAL = SGN (SvIVX(sv)); - break; - - case USE_UVX: - RETVAL = (SvUVX(sv) > 0); - break; - - case USE_NVX: - RETVAL = SGN (SvNVX(sv)); - break; - - case USE_PVX: - { - /* FIXME: Maybe this should be done by parsing the string, not by an - actual conversion. */ - STRLEN len; - const char *str = SvPV (sv, len); - if (mpq_set_str (tmp_mpq_0, str, 0) == 0) - RETVAL = mpq_sgn (tmp_mpq_0); - else - { - /* enough for all digits of the string */ - tmp_mpf_set_prec (tmp_mpf_0, strlen(str)+64); - if (mpf_set_str (tmp_mpf_0->m, str, 10) == 0) - RETVAL = mpf_sgn (tmp_mpf_0->m); - else - croak ("GMP::sgn invalid string format"); - } - } - break; - - case USE_MPZ: - RETVAL = mpz_sgn (SvMPZ(sv)->m); - break; - - case USE_MPQ: - RETVAL = mpq_sgn (SvMPQ(sv)->m); - break; - - case USE_MPF: - RETVAL = mpf_sgn (SvMPF(sv)); - break; - - default: - croak ("GMP::sgn invalid argument"); - } -OUTPUT: - RETVAL - - -# currently undocumented -void -shrink () -CODE: -#define x_mpz_shrink(z) \ - mpz_set_ui (z, 0L); _mpz_realloc (z, 1) -#define x_mpq_shrink(q) \ - x_mpz_shrink (mpq_numref(q)); x_mpz_shrink (mpq_denref(q)) - - x_mpz_shrink (tmp_mpz_0); - x_mpz_shrink (tmp_mpz_1); - x_mpz_shrink (tmp_mpz_2); - x_mpq_shrink (tmp_mpq_0); - x_mpq_shrink (tmp_mpq_1); - tmp_mpf_shrink (tmp_mpf_0); - tmp_mpf_shrink (tmp_mpf_1); - - - -malloced_string -sprintf_internal (fmt, sv) - const_string fmt - SV *sv -CODE: - assert (strlen (fmt) >= 3); - assert (SvROK(sv)); - assert ((sv_derived_from (sv, mpz_class) && fmt[strlen(fmt)-2] == 'Z') - || (sv_derived_from (sv, mpq_class) && fmt[strlen(fmt)-2] == 'Q') - || (sv_derived_from (sv, mpf_class) && fmt[strlen(fmt)-2] == 'F')); - TRACE (printf ("GMP::sprintf_internal\n"); - printf (" fmt |%s|\n", fmt); - printf (" sv |%p|\n", SvMPZ(sv))); - - /* cheat a bit here, SvMPZ works for mpq and mpf too */ - gmp_asprintf (&RETVAL, fmt, SvMPZ(sv)); - - TRACE (printf (" result |%s|\n", RETVAL)); -OUTPUT: - RETVAL - - - -#------------------------------------------------------------------------------ - -MODULE = GMP PACKAGE = GMP::Mpz - -mpz -mpz (...) -ALIAS: - GMP::Mpz::new = 1 -PREINIT: - SV *sv; -CODE: - TRACE (printf ("%s new, ix=%ld, items=%d\n", mpz_class, ix, (int) items)); - RETVAL = new_mpz(); - - switch (items) { - case 0: - mpz_set_ui (RETVAL->m, 0L); - break; - - case 1: - sv = ST(0); - TRACE (printf (" use %d\n", use_sv (sv))); - switch (use_sv (sv)) { - case USE_IVX: - mpz_set_si (RETVAL->m, SvIVX(sv)); - break; - - case USE_UVX: - mpz_set_ui (RETVAL->m, SvUVX(sv)); - break; - - case USE_NVX: - mpz_set_d (RETVAL->m, SvNVX(sv)); - break; - - case USE_PVX: - my_mpz_set_svstr (RETVAL->m, sv); - break; - - case USE_MPZ: - mpz_set (RETVAL->m, SvMPZ(sv)->m); - break; - - case USE_MPQ: - mpz_set_q (RETVAL->m, SvMPQ(sv)->m); - break; - - case USE_MPF: - mpz_set_f (RETVAL->m, SvMPF(sv)); - break; - - default: - goto invalid; - } - break; - - default: - invalid: - croak ("%s new: invalid arguments", mpz_class); - } -OUTPUT: - RETVAL - - -void -overload_constant (str, pv, d1, ...) - const_string_assume str - SV *pv - dummy d1 -PREINIT: - mpz z; -PPCODE: - TRACE (printf ("%s constant: %s\n", mpz_class, str)); - z = new_mpz(); - if (mpz_set_str (z->m, str, 0) == 0) - { - PUSHs (MPX_NEWMORTAL (z, mpz_class_hv)); - } - else - { - free_mpz (z); - PUSHs(pv); - } - - -mpz -overload_copy (z, d1, d2) - mpz_assume z - dummy d1 - dummy d2 -CODE: - RETVAL = new_mpz(); - mpz_set (RETVAL->m, z->m); -OUTPUT: - RETVAL - - -void -DESTROY (z) - mpz_assume z -CODE: - TRACE (printf ("%s DESTROY %p\n", mpz_class, z)); - free_mpz (z); - - -malloced_string -overload_string (z, d1, d2) - mpz_assume z - dummy d1 - dummy d2 -CODE: - TRACE (printf ("%s overload_string %p\n", mpz_class, z)); - RETVAL = mpz_get_str (NULL, 10, z->m); -OUTPUT: - RETVAL - - -mpz -overload_add (xv, yv, order) - SV *xv - SV *yv - SV *order -ALIAS: - GMP::Mpz::overload_sub = 1 - GMP::Mpz::overload_mul = 2 - GMP::Mpz::overload_div = 3 - GMP::Mpz::overload_rem = 4 - GMP::Mpz::overload_and = 5 - GMP::Mpz::overload_ior = 6 - GMP::Mpz::overload_xor = 7 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr); - } table[] = { - { mpz_add }, /* 0 */ - { mpz_sub }, /* 1 */ - { mpz_mul }, /* 2 */ - { mpz_tdiv_q }, /* 3 */ - { mpz_tdiv_r }, /* 4 */ - { mpz_and }, /* 5 */ - { mpz_ior }, /* 6 */ - { mpz_xor }, /* 7 */ - }; -CODE: - assert_table (ix); - if (order == &PL_sv_yes) - SV_PTR_SWAP (xv, yv); - RETVAL = new_mpz(); - (*table[ix].op) (RETVAL->m, - coerce_mpz (tmp_mpz_0, xv), - coerce_mpz (tmp_mpz_1, yv)); -OUTPUT: - RETVAL - - -void -overload_addeq (x, y, o) - mpz_assume x - mpz_coerce y - order_noswap o -ALIAS: - GMP::Mpz::overload_subeq = 1 - GMP::Mpz::overload_muleq = 2 - GMP::Mpz::overload_diveq = 3 - GMP::Mpz::overload_remeq = 4 - GMP::Mpz::overload_andeq = 5 - GMP::Mpz::overload_ioreq = 6 - GMP::Mpz::overload_xoreq = 7 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr); - } table[] = { - { mpz_add }, /* 0 */ - { mpz_sub }, /* 1 */ - { mpz_mul }, /* 2 */ - { mpz_tdiv_q }, /* 3 */ - { mpz_tdiv_r }, /* 4 */ - { mpz_and }, /* 5 */ - { mpz_ior }, /* 6 */ - { mpz_xor }, /* 7 */ - }; -PPCODE: - assert_table (ix); - (*table[ix].op) (x->m, x->m, y); - XPUSHs (ST(0)); - - -mpz -overload_lshift (zv, nv, order) - SV *zv - SV *nv - SV *order -ALIAS: - GMP::Mpz::overload_rshift = 1 - GMP::Mpz::overload_pow = 2 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, unsigned long); - } table[] = { - { mpz_mul_2exp }, /* 0 */ - { mpz_div_2exp }, /* 1 */ - { mpz_pow_ui }, /* 2 */ - }; -CODE: - assert_table (ix); - if (order == &PL_sv_yes) - SV_PTR_SWAP (zv, nv); - RETVAL = new_mpz(); - (*table[ix].op) (RETVAL->m, coerce_mpz (RETVAL->m, zv), coerce_ulong (nv)); -OUTPUT: - RETVAL - - -void -overload_lshifteq (z, n, o) - mpz_assume z - ulong_coerce n - order_noswap o -ALIAS: - GMP::Mpz::overload_rshifteq = 1 - GMP::Mpz::overload_poweq = 2 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, unsigned long); - } table[] = { - { mpz_mul_2exp }, /* 0 */ - { mpz_div_2exp }, /* 1 */ - { mpz_pow_ui }, /* 2 */ - }; -PPCODE: - assert_table (ix); - (*table[ix].op) (z->m, z->m, n); - XPUSHs(ST(0)); - - -mpz -overload_abs (z, d1, d2) - mpz_assume z - dummy d1 - dummy d2 -ALIAS: - GMP::Mpz::overload_neg = 1 - GMP::Mpz::overload_com = 2 - GMP::Mpz::overload_sqrt = 3 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr w, mpz_srcptr x); - } table[] = { - { mpz_abs }, /* 0 */ - { mpz_neg }, /* 1 */ - { mpz_com }, /* 2 */ - { mpz_sqrt }, /* 3 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpz(); - (*table[ix].op) (RETVAL->m, z->m); -OUTPUT: - RETVAL - - -void -overload_inc (z, d1, d2) - mpz_assume z - dummy d1 - dummy d2 -ALIAS: - GMP::Mpz::overload_dec = 1 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr w, mpz_srcptr x, unsigned long y); - } table[] = { - { mpz_add_ui }, /* 0 */ - { mpz_sub_ui }, /* 1 */ - }; -CODE: - assert_table (ix); - (*table[ix].op) (z->m, z->m, 1L); - - -int -overload_spaceship (xv, yv, order) - SV *xv - SV *yv - SV *order -PREINIT: - mpz x; -CODE: - TRACE (printf ("%s overload_spaceship\n", mpz_class)); - MPZ_ASSUME (x, xv); - switch (use_sv (yv)) { - case USE_IVX: - RETVAL = mpz_cmp_si (x->m, SvIVX(yv)); - break; - case USE_UVX: - RETVAL = mpz_cmp_ui (x->m, SvUVX(yv)); - break; - case USE_PVX: - RETVAL = mpz_cmp (x->m, coerce_mpz (tmp_mpz_0, yv)); - break; - case USE_NVX: - RETVAL = mpz_cmp_d (x->m, SvNVX(yv)); - break; - case USE_MPZ: - RETVAL = mpz_cmp (x->m, SvMPZ(yv)->m); - break; - case USE_MPQ: - RETVAL = x_mpz_cmp_q (x->m, SvMPQ(yv)->m); - break; - case USE_MPF: - RETVAL = x_mpz_cmp_f (x->m, SvMPF(yv)); - break; - default: - croak ("%s <=>: invalid operand", mpz_class); - } - RETVAL = SGN (RETVAL); - if (order == &PL_sv_yes) - RETVAL = -RETVAL; -OUTPUT: - RETVAL - - -bool -overload_bool (z, d1, d2) - mpz_assume z - dummy d1 - dummy d2 -ALIAS: - GMP::Mpz::overload_not = 1 -CODE: - RETVAL = (mpz_sgn (z->m) != 0) ^ ix; -OUTPUT: - RETVAL - - -mpz -bin (n, k) - mpz_coerce n - ulong_coerce k -ALIAS: - GMP::Mpz::root = 1 -PREINIT: - /* mpz_root returns an int, hence the cast */ - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, unsigned long); - } table[] = { - { mpz_bin_ui }, /* 0 */ - { (void (*)(mpz_ptr, mpz_srcptr, unsigned long)) mpz_root }, /* 1 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpz(); - (*table[ix].op) (RETVAL->m, n, k); -OUTPUT: - RETVAL - - -void -cdiv (a, d) - mpz_coerce a - mpz_coerce d -ALIAS: - GMP::Mpz::fdiv = 1 - GMP::Mpz::tdiv = 2 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_ptr, mpz_srcptr, mpz_srcptr); - } table[] = { - { mpz_cdiv_qr }, /* 0 */ - { mpz_fdiv_qr }, /* 1 */ - { mpz_tdiv_qr }, /* 2 */ - }; - mpz q, r; -PPCODE: - assert_table (ix); - q = new_mpz(); - r = new_mpz(); - (*table[ix].op) (q->m, r->m, a, d); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (q, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (r, mpz_class_hv)); - - -void -cdiv_2exp (a, d) - mpz_coerce a - ulong_coerce d -ALIAS: - GMP::Mpz::fdiv_2exp = 1 - GMP::Mpz::tdiv_2exp = 2 -PREINIT: - static_functable const struct { - void (*q) (mpz_ptr, mpz_srcptr, unsigned long); - void (*r) (mpz_ptr, mpz_srcptr, unsigned long); - } table[] = { - { mpz_cdiv_q_2exp, mpz_cdiv_r_2exp }, /* 0 */ - { mpz_fdiv_q_2exp, mpz_fdiv_r_2exp }, /* 1 */ - { mpz_tdiv_q_2exp, mpz_tdiv_r_2exp }, /* 2 */ - }; - mpz q, r; -PPCODE: - assert_table (ix); - q = new_mpz(); - r = new_mpz(); - (*table[ix].q) (q->m, a, d); - (*table[ix].r) (r->m, a, d); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (q, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (r, mpz_class_hv)); - - -bool -congruent_p (a, c, d) - mpz_coerce a - mpz_coerce c - mpz_coerce d -PREINIT: -CODE: - RETVAL = mpz_congruent_p (a, c, d); -OUTPUT: - RETVAL - - -bool -congruent_2exp_p (a, c, d) - mpz_coerce a - mpz_coerce c - ulong_coerce d -PREINIT: -CODE: - RETVAL = mpz_congruent_2exp_p (a, c, d); -OUTPUT: - RETVAL - - -mpz -divexact (a, d) - mpz_coerce a - mpz_coerce d -ALIAS: - GMP::Mpz::mod = 1 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr); - } table[] = { - { mpz_divexact }, /* 0 */ - { mpz_mod }, /* 1 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpz(); - (*table[ix].op) (RETVAL->m, a, d); -OUTPUT: - RETVAL - - -bool -divisible_p (a, d) - mpz_coerce a - mpz_coerce d -CODE: - RETVAL = mpz_divisible_p (a, d); -OUTPUT: - RETVAL - - -bool -divisible_2exp_p (a, d) - mpz_coerce a - ulong_coerce d -CODE: - RETVAL = mpz_divisible_2exp_p (a, d); -OUTPUT: - RETVAL - - -bool -even_p (z) - mpz_coerce z -ALIAS: - GMP::Mpz::odd_p = 1 - GMP::Mpz::perfect_square_p = 2 - GMP::Mpz::perfect_power_p = 3 -PREINIT: - static_functable const struct { - int (*op) (mpz_srcptr z); - } table[] = { - { x_mpz_even_p }, /* 0 */ - { x_mpz_odd_p }, /* 1 */ - { mpz_perfect_square_p }, /* 2 */ - { mpz_perfect_power_p }, /* 3 */ - }; -CODE: - assert_table (ix); - RETVAL = (*table[ix].op) (z); -OUTPUT: - RETVAL - - -mpz -fac (n) - ulong_coerce n -ALIAS: - GMP::Mpz::fib = 1 - GMP::Mpz::lucnum = 2 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr r, unsigned long n); - } table[] = { - { mpz_fac_ui }, /* 0 */ - { mpz_fib_ui }, /* 1 */ - { mpz_lucnum_ui }, /* 2 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpz(); - (*table[ix].op) (RETVAL->m, n); -OUTPUT: - RETVAL - - -void -fib2 (n) - ulong_coerce n -ALIAS: - GMP::Mpz::lucnum2 = 1 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr r, mpz_ptr r2, unsigned long n); - } table[] = { - { mpz_fib2_ui }, /* 0 */ - { mpz_lucnum2_ui }, /* 1 */ - }; - mpz r, r2; -PPCODE: - assert_table (ix); - r = new_mpz(); - r2 = new_mpz(); - (*table[ix].op) (r->m, r2->m, n); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (r, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (r2, mpz_class_hv)); - - -mpz -gcd (x, ...) - mpz_coerce x -ALIAS: - GMP::Mpz::lcm = 1 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr w, mpz_srcptr x, mpz_srcptr y); - void (*op_ui) (mpz_ptr w, mpz_srcptr x, unsigned long y); - } table[] = { - /* cast to ignore ulong return from mpz_gcd_ui */ - { mpz_gcd, - (void (*) (mpz_ptr, mpz_srcptr, unsigned long)) mpz_gcd_ui }, /* 0 */ - { mpz_lcm, mpz_lcm_ui }, /* 1 */ - }; - int i; - SV *yv; -CODE: - assert_table (ix); - RETVAL = new_mpz(); - if (items == 1) - mpz_set (RETVAL->m, x); - else - { - for (i = 1; i < items; i++) - { - yv = ST(i); - if (SvIOK(yv)) - (*table[ix].op_ui) (RETVAL->m, x, ABS(SvIVX(yv))); - else - (*table[ix].op) (RETVAL->m, x, coerce_mpz (tmp_mpz_1, yv)); - x = RETVAL->m; - } - } -OUTPUT: - RETVAL - - -void -gcdext (a, b) - mpz_coerce a - mpz_coerce b -PREINIT: - mpz g, x, y; - SV *sv; -PPCODE: - g = new_mpz(); - x = new_mpz(); - y = new_mpz(); - mpz_gcdext (g->m, x->m, y->m, a, b); - EXTEND (SP, 3); - PUSHs (MPX_NEWMORTAL (g, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (x, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (y, mpz_class_hv)); - - -unsigned long -hamdist (x, y) - mpz_coerce x - mpz_coerce y -CODE: - RETVAL = mpz_hamdist (x, y); -OUTPUT: - RETVAL - - -mpz -invert (a, m) - mpz_coerce a - mpz_coerce m -CODE: - RETVAL = new_mpz(); - if (! mpz_invert (RETVAL->m, a, m)) - { - free_mpz (RETVAL); - XSRETURN_UNDEF; - } -OUTPUT: - RETVAL - - -int -jacobi (a, b) - mpz_coerce a - mpz_coerce b -CODE: - RETVAL = mpz_jacobi (a, b); -OUTPUT: - RETVAL - - -int -kronecker (a, b) - SV *a - SV *b -CODE: - if (SvIOK(b)) - RETVAL = mpz_kronecker_si (coerce_mpz(tmp_mpz_0,a), SvIVX(b)); - else if (SvIOK(a)) - RETVAL = mpz_si_kronecker (SvIVX(a), coerce_mpz(tmp_mpz_0,b)); - else - RETVAL = mpz_kronecker (coerce_mpz(tmp_mpz_0,a), - coerce_mpz(tmp_mpz_1,b)); -OUTPUT: - RETVAL - - -void -mpz_export (order, size, endian, nails, z) - int order - size_t size - int endian - size_t nails - mpz_coerce z -PREINIT: - size_t numb, count, bytes, actual_count; - char *data; - SV *sv; -PPCODE: - numb = 8*size - nails; - count = (mpz_sizeinbase (z, 2) + numb-1) / numb; - bytes = count * size; - New (GMP_MALLOC_ID, data, bytes+1, char); - mpz_export (data, &actual_count, order, size, endian, nails, z); - assert (count == actual_count); - data[bytes] = '\0'; - sv = sv_newmortal(); sv_usepvn_mg (sv, data, bytes); PUSHs(sv); - - -mpz -mpz_import (order, size, endian, nails, sv) - int order - size_t size - int endian - size_t nails - SV *sv -PREINIT: - size_t count; - const char *data; - STRLEN len; -CODE: - data = SvPV (sv, len); - if ((len % size) != 0) - croak ("%s mpz_import: string not a multiple of the given size", - mpz_class); - count = len / size; - RETVAL = new_mpz(); - mpz_import (RETVAL->m, count, order, size, endian, nails, data); -OUTPUT: - RETVAL - - -mpz -nextprime (z) - mpz_coerce z -CODE: - RETVAL = new_mpz(); - mpz_nextprime (RETVAL->m, z); -OUTPUT: - RETVAL - - -unsigned long -popcount (x) - mpz_coerce x -CODE: - RETVAL = mpz_popcount (x); -OUTPUT: - RETVAL - - -mpz -powm (b, e, m) - mpz_coerce b - mpz_coerce e - mpz_coerce m -CODE: - RETVAL = new_mpz(); - mpz_powm (RETVAL->m, b, e, m); -OUTPUT: - RETVAL - - -bool -probab_prime_p (z, n) - mpz_coerce z - ulong_coerce n -CODE: - RETVAL = mpz_probab_prime_p (z, n); -OUTPUT: - RETVAL - - -# No attempt to coerce here, only an mpz makes sense. -void -realloc (z, limbs) - mpz z - int limbs -CODE: - _mpz_realloc (z->m, limbs); - - -void -remove (z, f) - mpz_coerce z - mpz_coerce f -PREINIT: - SV *sv; - mpz rem; - unsigned long mult; -PPCODE: - rem = new_mpz(); - mult = mpz_remove (rem->m, z, f); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (rem, mpz_class_hv)); - PUSHs (sv_2mortal (newSViv (mult))); - - -void -roote (z, n) - mpz_coerce z - ulong_coerce n -PREINIT: - SV *sv; - mpz root; - int exact; -PPCODE: - root = new_mpz(); - exact = mpz_root (root->m, z, n); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (root, mpz_class_hv)); - sv = (exact ? &PL_sv_yes : &PL_sv_no); sv_2mortal(sv); PUSHs(sv); - - -void -rootrem (z, n) - mpz_coerce z - ulong_coerce n -PREINIT: - SV *sv; - mpz root; - mpz rem; -PPCODE: - root = new_mpz(); - rem = new_mpz(); - mpz_rootrem (root->m, rem->m, z, n); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (root, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (rem, mpz_class_hv)); - - -# In the past scan0 and scan1 were described as returning ULONG_MAX which -# could be obtained in perl with ~0. That wasn't true on 64-bit systems -# (eg. alpha) with perl 5.005, since in that version IV and UV were still -# 32-bits. -# -# We changed in gmp 4.2 to just say ~0 for the not-found return. It's -# likely most people have used ~0 rather than POSIX::ULONG_MAX(), so this -# change should match existing usage. It only actually makes a difference -# in old perl, since recent versions have gone to 64-bits for IV and UV, the -# same as a ulong. -# -# In perl 5.005 we explicitly mask the mpz return down to 32-bits to get ~0. -# UV_MAX is no good, it reflects the size of the UV type (64-bits), rather -# than the size of the values one ought to be storing in an SV (32-bits). - -gmp_UV -scan0 (z, start) - mpz_coerce z - ulong_coerce start -ALIAS: - GMP::Mpz::scan1 = 1 -PREINIT: - static_functable const struct { - unsigned long (*op) (mpz_srcptr, unsigned long); - } table[] = { - { mpz_scan0 }, /* 0 */ - { mpz_scan1 }, /* 1 */ - }; -CODE: - assert_table (ix); - RETVAL = (*table[ix].op) (z, start); - if (PERL_LT (5,6)) - RETVAL &= 0xFFFFFFFF; -OUTPUT: - RETVAL - - -void -setbit (sv, bit) - SV *sv - ulong_coerce bit -ALIAS: - GMP::Mpz::clrbit = 1 - GMP::Mpz::combit = 2 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, unsigned long); - } table[] = { - { mpz_setbit }, /* 0 */ - { mpz_clrbit }, /* 1 */ - { mpz_combit }, /* 2 */ - }; - int use; - mpz z; -CODE: - use = use_sv (sv); - if (use == USE_MPZ && SvREFCNT(SvRV(sv)) == 1 && ! SvSMAGICAL(sv)) - { - /* our operand is a non-magical mpz with a reference count of 1, so - we can just modify it */ - (*table[ix].op) (SvMPZ(sv)->m, bit); - } - else - { - /* otherwise we need to make a new mpz, from whatever we have, and - operate on that, possibly invoking magic when storing back */ - SV *new_sv; - mpz z = new_mpz (); - mpz_ptr coerce_ptr = coerce_mpz_using (z->m, sv, use); - if (coerce_ptr != z->m) - mpz_set (z->m, coerce_ptr); - (*table[ix].op) (z->m, bit); - new_sv = sv_bless (sv_setref_pv (sv_newmortal(), NULL, z), - mpz_class_hv); - SvSetMagicSV (sv, new_sv); - } - - -void -sqrtrem (z) - mpz_coerce z -PREINIT: - SV *sv; - mpz root; - mpz rem; -PPCODE: - root = new_mpz(); - rem = new_mpz(); - mpz_sqrtrem (root->m, rem->m, z); - EXTEND (SP, 2); - PUSHs (MPX_NEWMORTAL (root, mpz_class_hv)); - PUSHs (MPX_NEWMORTAL (rem, mpz_class_hv)); - - -size_t -sizeinbase (z, base) - mpz_coerce z - int base -CODE: - RETVAL = mpz_sizeinbase (z, base); -OUTPUT: - RETVAL - - -int -tstbit (z, bit) - mpz_coerce z - ulong_coerce bit -CODE: - RETVAL = mpz_tstbit (z, bit); -OUTPUT: - RETVAL - - - -#------------------------------------------------------------------------------ - -MODULE = GMP PACKAGE = GMP::Mpq - - -mpq -mpq (...) -ALIAS: - GMP::Mpq::new = 1 -CODE: - TRACE (printf ("%s new, ix=%ld, items=%d\n", mpq_class, ix, (int) items)); - RETVAL = new_mpq(); - switch (items) { - case 0: - mpq_set_ui (RETVAL->m, 0L, 1L); - break; - case 1: - { - mpq_ptr rp = RETVAL->m; - mpq_ptr cp = coerce_mpq (rp, ST(0)); - if (cp != rp) - mpq_set (rp, cp); - } - break; - case 2: - { - mpz_ptr rp, cp; - rp = mpq_numref (RETVAL->m); - cp = coerce_mpz (rp, ST(0)); - if (cp != rp) - mpz_set (rp, cp); - rp = mpq_denref (RETVAL->m); - cp = coerce_mpz (rp, ST(1)); - if (cp != rp) - mpz_set (rp, cp); - } - break; - default: - croak ("%s new: invalid arguments", mpq_class); - } -OUTPUT: - RETVAL - - -void -overload_constant (str, pv, d1, ...) - const_string_assume str - SV *pv - dummy d1 -PREINIT: - SV *sv; - mpq q; -PPCODE: - TRACE (printf ("%s constant: %s\n", mpq_class, str)); - q = new_mpq(); - if (mpq_set_str (q->m, str, 0) == 0) - { sv = sv_bless (sv_setref_pv (sv_newmortal(), NULL, q), mpq_class_hv); } - else - { free_mpq (q); sv = pv; } - XPUSHs(sv); - - -mpq -overload_copy (q, d1, d2) - mpq_assume q - dummy d1 - dummy d2 -CODE: - RETVAL = new_mpq(); - mpq_set (RETVAL->m, q->m); -OUTPUT: - RETVAL - - -void -DESTROY (q) - mpq_assume q -CODE: - TRACE (printf ("%s DESTROY %p\n", mpq_class, q)); - free_mpq (q); - - -malloced_string -overload_string (q, d1, d2) - mpq_assume q - dummy d1 - dummy d2 -CODE: - TRACE (printf ("%s overload_string %p\n", mpq_class, q)); - RETVAL = mpq_get_str (NULL, 10, q->m); -OUTPUT: - RETVAL - - -mpq -overload_add (xv, yv, order) - SV *xv - SV *yv - SV *order -ALIAS: - GMP::Mpq::overload_sub = 1 - GMP::Mpq::overload_mul = 2 - GMP::Mpq::overload_div = 3 -PREINIT: - static_functable const struct { - void (*op) (mpq_ptr, mpq_srcptr, mpq_srcptr); - } table[] = { - { mpq_add }, /* 0 */ - { mpq_sub }, /* 1 */ - { mpq_mul }, /* 2 */ - { mpq_div }, /* 3 */ - }; -CODE: - TRACE (printf ("%s binary\n", mpf_class)); - assert_table (ix); - if (order == &PL_sv_yes) - SV_PTR_SWAP (xv, yv); - RETVAL = new_mpq(); - (*table[ix].op) (RETVAL->m, - coerce_mpq (tmp_mpq_0, xv), - coerce_mpq (tmp_mpq_1, yv)); -OUTPUT: - RETVAL - - -void -overload_addeq (x, y, o) - mpq_assume x - mpq_coerce y - order_noswap o -ALIAS: - GMP::Mpq::overload_subeq = 1 - GMP::Mpq::overload_muleq = 2 - GMP::Mpq::overload_diveq = 3 -PREINIT: - static_functable const struct { - void (*op) (mpq_ptr, mpq_srcptr, mpq_srcptr); - } table[] = { - { mpq_add }, /* 0 */ - { mpq_sub }, /* 1 */ - { mpq_mul }, /* 2 */ - { mpq_div }, /* 3 */ - }; -PPCODE: - assert_table (ix); - (*table[ix].op) (x->m, x->m, y); - XPUSHs(ST(0)); - - -mpq -overload_lshift (qv, nv, order) - SV *qv - SV *nv - SV *order -ALIAS: - GMP::Mpq::overload_rshift = 1 - GMP::Mpq::overload_pow = 2 -PREINIT: - static_functable const struct { - void (*op) (mpq_ptr, mpq_srcptr, unsigned long); - } table[] = { - { mpq_mul_2exp }, /* 0 */ - { mpq_div_2exp }, /* 1 */ - { x_mpq_pow_ui }, /* 2 */ - }; -CODE: - assert_table (ix); - if (order == &PL_sv_yes) - SV_PTR_SWAP (qv, nv); - RETVAL = new_mpq(); - (*table[ix].op) (RETVAL->m, coerce_mpq (RETVAL->m, qv), coerce_ulong (nv)); -OUTPUT: - RETVAL - - -void -overload_lshifteq (q, n, o) - mpq_assume q - ulong_coerce n - order_noswap o -ALIAS: - GMP::Mpq::overload_rshifteq = 1 - GMP::Mpq::overload_poweq = 2 -PREINIT: - static_functable const struct { - void (*op) (mpq_ptr, mpq_srcptr, unsigned long); - } table[] = { - { mpq_mul_2exp }, /* 0 */ - { mpq_div_2exp }, /* 1 */ - { x_mpq_pow_ui }, /* 2 */ - }; -PPCODE: - assert_table (ix); - (*table[ix].op) (q->m, q->m, n); - XPUSHs(ST(0)); - - -void -overload_inc (q, d1, d2) - mpq_assume q - dummy d1 - dummy d2 -ALIAS: - GMP::Mpq::overload_dec = 1 -PREINIT: - static_functable const struct { - void (*op) (mpz_ptr, mpz_srcptr, mpz_srcptr); - } table[] = { - { mpz_add }, /* 0 */ - { mpz_sub }, /* 1 */ - }; -CODE: - assert_table (ix); - (*table[ix].op) (mpq_numref(q->m), mpq_numref(q->m), mpq_denref(q->m)); - - -mpq -overload_abs (q, d1, d2) - mpq_assume q - dummy d1 - dummy d2 -ALIAS: - GMP::Mpq::overload_neg = 1 -PREINIT: - static_functable const struct { - void (*op) (mpq_ptr w, mpq_srcptr x); - } table[] = { - { mpq_abs }, /* 0 */ - { mpq_neg }, /* 1 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpq(); - (*table[ix].op) (RETVAL->m, q->m); -OUTPUT: - RETVAL - - -int -overload_spaceship (x, y, order) - mpq_assume x - mpq_coerce y - SV *order -CODE: - RETVAL = mpq_cmp (x->m, y); - RETVAL = SGN (RETVAL); - if (order == &PL_sv_yes) - RETVAL = -RETVAL; -OUTPUT: - RETVAL - - -bool -overload_bool (q, d1, d2) - mpq_assume q - dummy d1 - dummy d2 -ALIAS: - GMP::Mpq::overload_not = 1 -CODE: - RETVAL = (mpq_sgn (q->m) != 0) ^ ix; -OUTPUT: - RETVAL - - -bool -overload_eq (x, yv, d) - mpq_assume x - SV *yv - dummy d -ALIAS: - GMP::Mpq::overload_ne = 1 -PREINIT: - int use; -CODE: - use = use_sv (yv); - switch (use) { - case USE_IVX: - case USE_UVX: - case USE_MPZ: - RETVAL = 0; - if (x_mpq_integer_p (x->m)) - { - switch (use) { - case USE_IVX: - RETVAL = (mpz_cmp_si (mpq_numref(x->m), SvIVX(yv)) == 0); - break; - case USE_UVX: - RETVAL = (mpz_cmp_ui (mpq_numref(x->m), SvUVX(yv)) == 0); - break; - case USE_MPZ: - RETVAL = (mpz_cmp (mpq_numref(x->m), SvMPZ(yv)->m) == 0); - break; - } - } - break; - - case USE_MPQ: - RETVAL = (mpq_equal (x->m, SvMPQ(yv)->m) != 0); - break; - - default: - RETVAL = (mpq_equal (x->m, coerce_mpq_using (tmp_mpq_0, yv, use)) != 0); - break; - } - RETVAL ^= ix; -OUTPUT: - RETVAL - - -void -canonicalize (q) - mpq q -CODE: - mpq_canonicalize (q->m); - - -mpq -inv (q) - mpq_coerce q -CODE: - RETVAL = new_mpq(); - mpq_inv (RETVAL->m, q); -OUTPUT: - RETVAL - - -mpz -num (q) - mpq q -ALIAS: - GMP::Mpq::den = 1 -CODE: - RETVAL = new_mpz(); - mpz_set (RETVAL->m, (ix == 0 ? mpq_numref(q->m) : mpq_denref(q->m))); -OUTPUT: - RETVAL - - - -#------------------------------------------------------------------------------ - -MODULE = GMP PACKAGE = GMP::Mpf - - -mpf -mpf (...) -ALIAS: - GMP::Mpf::new = 1 -PREINIT: - unsigned long prec; -CODE: - TRACE (printf ("%s new\n", mpf_class)); - if (items > 2) - croak ("%s new: invalid arguments", mpf_class); - prec = (items == 2 ? coerce_ulong (ST(1)) : mpf_get_default_prec()); - RETVAL = new_mpf (prec); - if (items >= 1) - { - SV *sv = ST(0); - my_mpf_set_sv_using (RETVAL, sv, use_sv(sv)); - } -OUTPUT: - RETVAL - - -mpf -overload_constant (sv, d1, d2, ...) - SV *sv - dummy d1 - dummy d2 -CODE: - assert (SvPOK (sv)); - TRACE (printf ("%s constant: %s\n", mpq_class, SvPVX(sv))); - RETVAL = new_mpf (mpf_get_default_prec()); - my_mpf_set_svstr (RETVAL, sv); -OUTPUT: - RETVAL - - -mpf -overload_copy (f, d1, d2) - mpf_assume f - dummy d1 - dummy d2 -CODE: - TRACE (printf ("%s copy\n", mpf_class)); - RETVAL = new_mpf (mpf_get_prec (f)); - mpf_set (RETVAL, f); -OUTPUT: - RETVAL - - -void -DESTROY (f) - mpf_assume f -CODE: - TRACE (printf ("%s DESTROY %p\n", mpf_class, f)); - mpf_clear (f); - Safefree (f); - assert_support (mpf_count--); - TRACE_ACTIVE (); - - -mpf -overload_add (x, y, order) - mpf_assume x - mpf_coerce_st0 y - SV *order -ALIAS: - GMP::Mpf::overload_sub = 1 - GMP::Mpf::overload_mul = 2 - GMP::Mpf::overload_div = 3 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr, mpf_srcptr, mpf_srcptr); - } table[] = { - { mpf_add }, /* 0 */ - { mpf_sub }, /* 1 */ - { mpf_mul }, /* 2 */ - { mpf_div }, /* 3 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpf (mpf_get_prec (x)); - if (order == &PL_sv_yes) - MPF_PTR_SWAP (x, y); - (*table[ix].op) (RETVAL, x, y); -OUTPUT: - RETVAL - - -void -overload_addeq (x, y, o) - mpf_assume x - mpf_coerce_st0 y - order_noswap o -ALIAS: - GMP::Mpf::overload_subeq = 1 - GMP::Mpf::overload_muleq = 2 - GMP::Mpf::overload_diveq = 3 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr, mpf_srcptr, mpf_srcptr); - } table[] = { - { mpf_add }, /* 0 */ - { mpf_sub }, /* 1 */ - { mpf_mul }, /* 2 */ - { mpf_div }, /* 3 */ - }; -PPCODE: - assert_table (ix); - (*table[ix].op) (x, x, y); - XPUSHs(ST(0)); - - -mpf -overload_lshift (fv, nv, order) - SV *fv - SV *nv - SV *order -ALIAS: - GMP::Mpf::overload_rshift = 1 - GMP::Mpf::overload_pow = 2 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr, mpf_srcptr, unsigned long); - } table[] = { - { mpf_mul_2exp }, /* 0 */ - { mpf_div_2exp }, /* 1 */ - { mpf_pow_ui }, /* 2 */ - }; - mpf f; - unsigned long prec; -CODE: - assert_table (ix); - MPF_ASSUME (f, fv); - prec = mpf_get_prec (f); - if (order == &PL_sv_yes) - SV_PTR_SWAP (fv, nv); - f = coerce_mpf (tmp_mpf_0, fv, prec); - RETVAL = new_mpf (prec); - (*table[ix].op) (RETVAL, f, coerce_ulong (nv)); -OUTPUT: - RETVAL - - -void -overload_lshifteq (f, n, o) - mpf_assume f - ulong_coerce n - order_noswap o -ALIAS: - GMP::Mpf::overload_rshifteq = 1 - GMP::Mpf::overload_poweq = 2 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr, mpf_srcptr, unsigned long); - } table[] = { - { mpf_mul_2exp }, /* 0 */ - { mpf_div_2exp }, /* 1 */ - { mpf_pow_ui }, /* 2 */ - }; -PPCODE: - assert_table (ix); - (*table[ix].op) (f, f, n); - XPUSHs(ST(0)); - - -mpf -overload_abs (f, d1, d2) - mpf_assume f - dummy d1 - dummy d2 -ALIAS: - GMP::Mpf::overload_neg = 1 - GMP::Mpf::overload_sqrt = 2 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr w, mpf_srcptr x); - } table[] = { - { mpf_abs }, /* 0 */ - { mpf_neg }, /* 1 */ - { mpf_sqrt }, /* 2 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpf (mpf_get_prec (f)); - (*table[ix].op) (RETVAL, f); -OUTPUT: - RETVAL - - -void -overload_inc (f, d1, d2) - mpf_assume f - dummy d1 - dummy d2 -ALIAS: - GMP::Mpf::overload_dec = 1 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr w, mpf_srcptr x, unsigned long y); - } table[] = { - { mpf_add_ui }, /* 0 */ - { mpf_sub_ui }, /* 1 */ - }; -CODE: - assert_table (ix); - (*table[ix].op) (f, f, 1L); - - -int -overload_spaceship (xv, yv, order) - SV *xv - SV *yv - SV *order -PREINIT: - mpf x; -CODE: - MPF_ASSUME (x, xv); - switch (use_sv (yv)) { - case USE_IVX: - RETVAL = mpf_cmp_si (x, SvIVX(yv)); - break; - case USE_UVX: - RETVAL = mpf_cmp_ui (x, SvUVX(yv)); - break; - case USE_NVX: - RETVAL = mpf_cmp_d (x, SvNVX(yv)); - break; - case USE_PVX: - { - STRLEN len; - const char *str = SvPV (yv, len); - /* enough for all digits of the string */ - tmp_mpf_set_prec (tmp_mpf_0, strlen(str)+64); - if (mpf_set_str (tmp_mpf_0->m, str, 10) != 0) - croak ("%s <=>: invalid string format", mpf_class); - RETVAL = mpf_cmp (x, tmp_mpf_0->m); - } - break; - case USE_MPZ: - RETVAL = - x_mpz_cmp_f (SvMPZ(yv)->m, x); - break; - case USE_MPF: - RETVAL = mpf_cmp (x, SvMPF(yv)); - break; - default: - RETVAL = mpq_cmp (coerce_mpq (tmp_mpq_0, xv), - coerce_mpq (tmp_mpq_1, yv)); - break; - } - RETVAL = SGN (RETVAL); - if (order == &PL_sv_yes) - RETVAL = -RETVAL; -OUTPUT: - RETVAL - - -bool -overload_bool (f, d1, d2) - mpf_assume f - dummy d1 - dummy d2 -ALIAS: - GMP::Mpf::overload_not = 1 -CODE: - RETVAL = (mpf_sgn (f) != 0) ^ ix; -OUTPUT: - RETVAL - - -mpf -ceil (f) - mpf_coerce_def f -ALIAS: - GMP::Mpf::floor = 1 - GMP::Mpf::trunc = 2 -PREINIT: - static_functable const struct { - void (*op) (mpf_ptr w, mpf_srcptr x); - } table[] = { - { mpf_ceil }, /* 0 */ - { mpf_floor }, /* 1 */ - { mpf_trunc }, /* 2 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpf (mpf_get_prec (f)); - (*table[ix].op) (RETVAL, f); -OUTPUT: - RETVAL - - -unsigned long -get_default_prec () -CODE: - RETVAL = mpf_get_default_prec(); -OUTPUT: - RETVAL - - -unsigned long -get_prec (f) - mpf_coerce_def f -CODE: - RETVAL = mpf_get_prec (f); -OUTPUT: - RETVAL - - -bool -mpf_eq (xv, yv, bits) - SV *xv - SV *yv - ulong_coerce bits -PREINIT: - mpf x, y; -CODE: - TRACE (printf ("%s eq\n", mpf_class)); - coerce_mpf_pair (&x,xv, &y,yv); - RETVAL = mpf_eq (x, y, bits); -OUTPUT: - RETVAL - - -mpf -reldiff (xv, yv) - SV *xv - SV *yv -PREINIT: - mpf x, y; - unsigned long prec; -CODE: - TRACE (printf ("%s reldiff\n", mpf_class)); - prec = coerce_mpf_pair (&x,xv, &y,yv); - RETVAL = new_mpf (prec); - mpf_reldiff (RETVAL, x, y); -OUTPUT: - RETVAL - - -void -set_default_prec (prec) - ulong_coerce prec -CODE: - TRACE (printf ("%s set_default_prec %lu\n", mpf_class, prec)); - mpf_set_default_prec (prec); - - -void -set_prec (sv, prec) - SV *sv - ulong_coerce prec -PREINIT: - mpf_ptr old_f, new_f; - int use; -CODE: - TRACE (printf ("%s set_prec to %lu\n", mpf_class, prec)); - use = use_sv (sv); - if (use == USE_MPF) - { - old_f = SvMPF(sv); - if (SvREFCNT(SvRV(sv)) == 1) - mpf_set_prec (old_f, prec); - else - { - TRACE (printf (" fork new mpf\n")); - new_f = new_mpf (prec); - mpf_set (new_f, old_f); - goto setref; - } - } - else - { - TRACE (printf (" coerce to mpf\n")); - new_f = new_mpf (prec); - my_mpf_set_sv_using (new_f, sv, use); - setref: - sv_bless (sv_setref_pv (sv, NULL, new_f), mpf_class_hv); - } - - - -#------------------------------------------------------------------------------ - -MODULE = GMP PACKAGE = GMP::Rand - -randstate -new (...) -ALIAS: - GMP::Rand::randstate = 1 -CODE: - TRACE (printf ("%s new\n", rand_class)); - New (GMP_MALLOC_ID, RETVAL, 1, __gmp_randstate_struct); - TRACE (printf (" RETVAL %p\n", RETVAL)); - assert_support (rand_count++); - TRACE_ACTIVE (); - - if (items == 0) - { - gmp_randinit_default (RETVAL); - } - else - { - if (SvROK (ST(0)) && sv_derived_from (ST(0), rand_class)) - { - if (items != 1) - goto invalid; - gmp_randinit_set (RETVAL, SvRANDSTATE (ST(0))); - } - else - { - STRLEN len; - const char *method = SvPV (ST(0), len); - assert (len == strlen (method)); - if (strcmp (method, "lc_2exp") == 0) - { - if (items != 4) - goto invalid; - gmp_randinit_lc_2exp (RETVAL, - coerce_mpz (tmp_mpz_0, ST(1)), - coerce_ulong (ST(2)), - coerce_ulong (ST(3))); - } - else if (strcmp (method, "lc_2exp_size") == 0) - { - if (items != 2) - goto invalid; - if (! gmp_randinit_lc_2exp_size (RETVAL, coerce_ulong (ST(1)))) - { - Safefree (RETVAL); - XSRETURN_UNDEF; - } - } - else if (strcmp (method, "mt") == 0) - { - if (items != 1) - goto invalid; - gmp_randinit_mt (RETVAL); - } - else - { - invalid: - croak ("%s new: invalid arguments", rand_class); - } - } - } -OUTPUT: - RETVAL - - -void -DESTROY (r) - randstate r -CODE: - TRACE (printf ("%s DESTROY\n", rand_class)); - gmp_randclear (r); - Safefree (r); - assert_support (rand_count--); - TRACE_ACTIVE (); - - -void -seed (r, z) - randstate r - mpz_coerce z -CODE: - gmp_randseed (r, z); - - -mpz -mpz_urandomb (r, bits) - randstate r - ulong_coerce bits -ALIAS: - GMP::Rand::mpz_rrandomb = 1 -PREINIT: - static_functable const struct { - void (*fun) (mpz_ptr, gmp_randstate_t r, unsigned long bits); - } table[] = { - { mpz_urandomb }, /* 0 */ - { mpz_rrandomb }, /* 1 */ - }; -CODE: - assert_table (ix); - RETVAL = new_mpz(); - (*table[ix].fun) (RETVAL->m, r, bits); -OUTPUT: - RETVAL - - -mpz -mpz_urandomm (r, m) - randstate r - mpz_coerce m -CODE: - RETVAL = new_mpz(); - mpz_urandomm (RETVAL->m, r, m); -OUTPUT: - RETVAL - - -mpf -mpf_urandomb (r, bits) - randstate r - ulong_coerce bits -CODE: - RETVAL = new_mpf (bits); - mpf_urandomb (RETVAL, r, bits); -OUTPUT: - RETVAL - - -unsigned long -gmp_urandomb_ui (r, bits) - randstate r - ulong_coerce bits -ALIAS: - GMP::Rand::gmp_urandomm_ui = 1 -PREINIT: - static_functable const struct { - unsigned long (*fun) (gmp_randstate_t r, unsigned long bits); - } table[] = { - { gmp_urandomb_ui }, /* 0 */ - { gmp_urandomm_ui }, /* 1 */ - }; -CODE: - assert_table (ix); - RETVAL = (*table[ix].fun) (r, bits); -OUTPUT: - RETVAL diff --git a/src/gmp/demos/perl/GMP/Mpf.pm b/src/gmp/demos/perl/GMP/Mpf.pm deleted file mode 100644 index babb1d1be..000000000 --- a/src/gmp/demos/perl/GMP/Mpf.pm +++ /dev/null @@ -1,97 +0,0 @@ -# GMP mpf module. - -# Copyright 2001, 2003 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -package GMP::Mpf; - -require GMP; -require Exporter; -@ISA = qw(GMP Exporter); -@EXPORT = qw(); -@EXPORT_OK = qw(); -%EXPORT_TAGS = ('all' => [qw( - ceil floor get_default_prec get_prec mpf mpf_eq - reldiff set_default_prec set_prec trunc)], - 'constants' => [@EXPORT], - 'noconstants' => [@EXPORT]); -Exporter::export_ok_tags('all'); - -use overload - '+' => \&overload_add, '+=' => \&overload_addeq, - '-' => \&overload_sub, '-=' => \&overload_subeq, - '*' => \&overload_mul, '*=' => \&overload_muleq, - '/' => \&overload_div, '/=' => \&overload_diveq, - '**' => \&overload_pow, '**=' => \&overload_poweq, - '<<' => \&overload_lshift, '<<=' => \&overload_lshifteq, - '>>' => \&overload_rshift, '>>=' => \&overload_rshifteq, - - 'bool' => \&overload_bool, - 'not' => \&overload_not, - '!' => \&overload_not, - '<=>' => \&overload_spaceship, - '++' => \&overload_inc, - '--' => \&overload_dec, - 'abs' => \&overload_abs, - 'neg' => \&overload_neg, - 'sqrt' => \&overload_sqrt, - '=' => \&overload_copy, - '""' => \&overload_string; - -sub import { - foreach (@_) { - if ($_ eq ':constants') { - overload::constant ('integer' => \&overload_constant, - 'binary' => \&overload_constant, - 'float' => \&overload_constant); - } elsif ($_ eq ':noconstants') { - overload::remove_constant ('integer' => \&overload_constant, - 'binary' => \&overload_constant, - 'float' => \&overload_constant); - } - } - goto &Exporter::import; -} - - -sub overload_string { - my $fmt; - BEGIN { $^W = 0; } - if (defined ($#)) { - $fmt = $#; - BEGIN { $^W = 1; } - # protect against calling sprintf_internal with a bad format - if ($fmt !~ /^((%%|[^%])*%[-+ .\d]*)([eEfgG](%%|[^%])*)$/) { - die "GMP::Mpf: invalid \$# format: $#\n"; - } - $fmt = $1 . 'F' . $3; - } else { - $fmt = '%.Fg'; - } - GMP::sprintf_internal ($fmt, $_[0]); -} - -1; -__END__ - - -# Local variables: -# perl-indent-level: 2 -# End: diff --git a/src/gmp/demos/perl/GMP/Mpq.pm b/src/gmp/demos/perl/GMP/Mpq.pm deleted file mode 100644 index 67c3e202c..000000000 --- a/src/gmp/demos/perl/GMP/Mpq.pm +++ /dev/null @@ -1,80 +0,0 @@ -# GMP mpq module. - -# Copyright 2001 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -package GMP::Mpq; - -require GMP; -require Exporter; -@ISA = qw(GMP Exporter); -@EXPORT = qw(); -@EXPORT_OK = qw(); -%EXPORT_TAGS = ('all' => [qw(canonicalize den inv mpq num)], - 'constants' => [@EXPORT], - 'noconstants' => [@EXPORT] ); -Exporter::export_ok_tags('all'); - -use overload - '+' => \&overload_add, '+=' => \&overload_addeq, - '-' => \&overload_sub, '-=' => \&overload_subeq, - '*' => \&overload_mul, '*=' => \&overload_muleq, - '/' => \&overload_div, '/=' => \&overload_diveq, - '**' => \&overload_pow, '**=' => \&overload_poweq, - '<<' => \&overload_lshift, '<<=' => \&overload_lshifteq, - '>>' => \&overload_rshift, '>>=' => \&overload_rshifteq, - - 'bool' => \&overload_bool, - 'not' => \&overload_not, - '!' => \&overload_not, - '==' => \&overload_eq, - '!=' => \&overload_ne, - '<=>' => \&overload_spaceship, - '++' => \&overload_inc, - '--' => \&overload_dec, - 'abs' => \&overload_abs, - 'neg' => \&overload_neg, - '=' => \&overload_copy, - '""' => \&overload_string; - -my $constants = { }; - -sub import { - foreach (@_) { - if ($_ eq ':constants') { - overload::constant ('integer' => \&overload_constant, - 'binary' => \&overload_constant, - 'float' => \&overload_constant); - } elsif ($_ eq ':noconstants') { - overload::remove_constant ('integer' => \&overload_constant, - 'binary' => \&overload_constant, - 'float' => \&overload_constant); - } - } - goto &Exporter::import; -} - -1; -__END__ - - -# Local variables: -# perl-indent-level: 2 -# End: diff --git a/src/gmp/demos/perl/GMP/Mpz.pm b/src/gmp/demos/perl/GMP/Mpz.pm deleted file mode 100644 index f97538611..000000000 --- a/src/gmp/demos/perl/GMP/Mpz.pm +++ /dev/null @@ -1,92 +0,0 @@ -# GMP mpz module. - -# Copyright 2001, 2002, 2003 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -package GMP::Mpz; - -require GMP; -require Exporter; -@ISA = qw(GMP Exporter); -@EXPORT = qw(); -@EXPORT_OK = qw(); -%EXPORT_TAGS = ('all' => [qw( - bin cdiv cdiv_2exp clrbit combit congruent_p - congruent_2exp_p divexact divisible_p - divisible_2exp_p even_p fac fdiv fdiv_2exp fib - fib2 gcd gcdext hamdist invert jacobi kronecker - lcm lucnum lucnum2 mod mpz mpz_export - mpz_import nextprime odd_p perfect_power_p - perfect_square_p popcount powm probab_prime_p - realloc remove root roote rootrem scan0 scan1 - setbit sizeinbase sqrtrem tdiv tdiv_2exp - tstbit)], - 'constants' => [@EXPORT], - 'noconstants' => [@EXPORT]); -Exporter::export_ok_tags('all'); - -use overload - '+' => \&overload_add, '+=' => \&overload_addeq, - '-' => \&overload_sub, '-=' => \&overload_subeq, - '*' => \&overload_mul, '*=' => \&overload_muleq, - '/' => \&overload_div, '/=' => \&overload_diveq, - '%' => \&overload_rem, '%=' => \&overload_remeq, - '<<' => \&overload_lshift, '<<=' => \&overload_lshifteq, - '>>' => \&overload_rshift, '>>=' => \&overload_rshifteq, - '**' => \&overload_pow, '**=' => \&overload_poweq, - '&' => \&overload_and, '&=' => \&overload_andeq, - '|' => \&overload_ior, '|=' => \&overload_ioreq, - '^' => \&overload_xor, '^=' => \&overload_xoreq, - - 'bool' => \&overload_bool, - 'not' => \&overload_not, - '!' => \&overload_not, - '~' => \&overload_com, - '<=>' => \&overload_spaceship, - '++' => \&overload_inc, - '--' => \&overload_dec, - '=' => \&overload_copy, - 'abs' => \&overload_abs, - 'neg' => \&overload_neg, - 'sqrt' => \&overload_sqrt, - '""' => \&overload_string; - -sub import { - foreach (@_) { - if ($_ eq ':constants') { - overload::constant ('integer' => \&overload_constant, - 'binary' => \&overload_constant, - 'float' => \&overload_constant); - } elsif ($_ eq ':noconstants') { - overload::remove_constant ('integer' => \&overload_constant, - 'binary' => \&overload_constant, - 'float' => \&overload_constant); - } - } - goto &Exporter::import; -} - -1; -__END__ - - -# Local variables: -# perl-indent-level: 2 -# End: diff --git a/src/gmp/demos/perl/GMP/Rand.pm b/src/gmp/demos/perl/GMP/Rand.pm deleted file mode 100644 index b809e9adb..000000000 --- a/src/gmp/demos/perl/GMP/Rand.pm +++ /dev/null @@ -1,35 +0,0 @@ -# GMP random numbers module. - -# Copyright 2001, 2003 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -package GMP::Rand; - -require GMP; -require Exporter; -@ISA = qw(GMP Exporter); -@EXPORT = qw(); -%EXPORT_TAGS = ('all' => [qw( - randstate mpf_urandomb mpz_rrandomb - mpz_urandomb mpz_urandomm gmp_urandomb_ui - gmp_urandomm_ui)]); -Exporter::export_ok_tags('all'); -1; -__END__ diff --git a/src/gmp/demos/perl/INSTALL b/src/gmp/demos/perl/INSTALL deleted file mode 100644 index 291c5bd02..000000000 --- a/src/gmp/demos/perl/INSTALL +++ /dev/null @@ -1,79 +0,0 @@ -Copyright 2001, 2003, 2004 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -The GNU MP Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or (at your -option) any later version. - -The GNU MP Library is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -License for more details. - -You should have received a copy of the GNU Lesser General Public License -along with the GNU MP Library; see the file COPYING.LIB. If not, write to -the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301, USA. - - - - - - GMP PERL MODULE INSTALLATION - - -This module can be compiled within the GMP source directory or moved -elsewhere and compiled. An installed GMP can be used, or a specified -GMP build tree. Both static and shared GMP builds will work. - -The simplest case is when GMP has been installed to a standard system -location - - perl Makefile.PL - make - -If not yet installed then the top-level GMP build directory must be -specified - - perl Makefile.PL GMP_BUILDDIR=/my/gmp/build - make - -In any case, with the module built, the sample program provided can be -run - - perl -Iblib/arch sample.pl - -If you built a shared version of libgmp but haven't yet installed it, -then it might be necessary to add a run-time path to it. For example - - LD_LIBRARY_PATH=/my/gmp/build/.libs perl -Iblib/arch sample.pl - -Documentation is provided in pod format in GMP.pm, and will have been -"man"-ified in the module build - - man -l blib/man3/GMP.3pm -or - man -M`pwd`/blib GMP - -A test script is provided, running a large number of more or less -trivial checks - - make test - -The module and its documentation can be installed in the usual way - - make install - -This will be into /usr/local or wherever the perl Config module -directs, but that can be controlled back at the Makefile.PL stage with -the usual ExtUtils::MakeMaker options. - -Once installed, programs using the GMP module become simply - - perl sample.pl - -And the documentation read directly too - - man GMP diff --git a/src/gmp/demos/perl/Makefile.PL b/src/gmp/demos/perl/Makefile.PL deleted file mode 100644 index 90a5e371e..000000000 --- a/src/gmp/demos/perl/Makefile.PL +++ /dev/null @@ -1,73 +0,0 @@ -# Makefile for GMP perl module. - -# Copyright 2001, 2003, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -# Bugs: -# -# When the generated Makefile re-runs "perl Makefile.PL" the GMP_BUILDDIR -# parameter is lost. - - -use ExtUtils::MakeMaker; - - -# Find and remove our parameters -@ARGV = map { - if (/^GMP_BUILDDIR=(.*)/) { - $GMP_BUILDDIR=$1; (); - } else { - $_; - } -} (@ARGV); - -$INC = ""; -$LIBS = "-lgmp"; -$OBJECT = "GMP.o"; - -if (defined $GMP_BUILDDIR) { - if (! -f "$GMP_BUILDDIR/libgmp.la") { - die "$GMP_BUILDDIR doesn't contain libgmp.la\n" . - "if it's really a gmp build directory then go there and run \"make libgmp.la\"\n"; - } - $INC = "-I$GMP_BUILDDIR $INC"; - $LIBS = "-L$GMP_BUILDDIR/.libs $LIBS"; -} - -WriteMakefile( - NAME => 'GMP', - VERSION => '2.00', - LIBS => [$LIBS], - OBJECT => $OBJECT, - INC => $INC, - clean => { FILES => 'test.tmp' }, - PM => { - 'GMP.pm' => '$(INST_LIBDIR)/GMP.pm', - 'GMP/Mpz.pm' => '$(INST_LIBDIR)/GMP/Mpz.pm', - 'GMP/Mpq.pm' => '$(INST_LIBDIR)/GMP/Mpq.pm', - 'GMP/Mpf.pm' => '$(INST_LIBDIR)/GMP/Mpf.pm', - 'GMP/Rand.pm' => '$(INST_LIBDIR)/GMP/Rand.pm', - } - ); - - -# Local variables: -# perl-indent-level: 2 -# End: diff --git a/src/gmp/demos/perl/sample.pl b/src/gmp/demos/perl/sample.pl deleted file mode 100644 index 659059ac8..000000000 --- a/src/gmp/demos/perl/sample.pl +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/perl -w - -# Some sample GMP module operations - -# Copyright 2001, 2004 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - -use strict; - - -use GMP; -print "using GMP module $GMP::VERSION and GMP library ",GMP::version(),"\n"; - - -use GMP::Mpz qw(:all); -print "the 200th fibonacci number is ", fib(200), "\n"; -print "next prime after 10**30 is (probably) ", nextprime(mpz(10)**30), "\n"; - - -use GMP::Mpq qw(:constants); -print "the 7th harmonic number is ", 1+1/2+1/3+1/4+1/5+1/6+1/7, "\n"; -use GMP::Mpq qw(:noconstants); - - -use GMP::Mpf qw(mpf); -my $f = mpf(1,180); -$f >>= 180; -$f += 1; -print "a sample mpf is $f\n"; diff --git a/src/gmp/demos/perl/test.pl b/src/gmp/demos/perl/test.pl deleted file mode 100644 index 7cdbc214e..000000000 --- a/src/gmp/demos/perl/test.pl +++ /dev/null @@ -1,2170 +0,0 @@ -#!/usr/bin/perl -w - -# GMP perl module tests - -# Copyright 2001, 2002, 2003 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -# These tests aim to exercise the many possible combinations of operands -# etc, and to run all functions at least once, which if nothing else will -# check everything intended is in the :all list. -# -# Use the following in .emacs to match test failure messages. -# -# ;; perl "Test" module error messages -# (eval-after-load "compile" -# '(add-to-list -# 'compilation-error-regexp-alist -# '("^.*Failed test [0-9]+ in \\([^ ]+\\) at line \\([0-9]+\\)" 1 2))) - - -use strict; -use Test; - -BEGIN { - plan tests => 123, - onfail => sub { print "there were failures\n" }, -} - -use GMP qw(:all); -use GMP::Mpz qw(:all); -use GMP::Mpq qw(:all); -use GMP::Mpf qw(:all); -use GMP::Rand qw(:all); - -use GMP::Mpz qw(:constants); -use GMP::Mpz qw(:noconstants); -use GMP::Mpq qw(:constants); -use GMP::Mpq qw(:noconstants); -use GMP::Mpf qw(:constants); -use GMP::Mpf qw(:noconstants); - -package Mytie; -use Exporter; -use vars qw($val $fetched $stored); -$val = 0; -$fetched = 0; -$stored = 0; -sub TIESCALAR { - my ($class, $newval) = @_; - my $var = 'mytie dummy refed var'; - $val = $newval; - $fetched = 0; - $stored = 0; - return bless \$var, $class; -} -sub FETCH { - my ($self) = @_; - $fetched++; - return $val; -} -sub STORE { - my ($self, $newval) = @_; - $val = $newval; - $stored++; -} -package main; - -# check Mytie does what it should -{ tie my $t, 'Mytie', 123; - ok ($Mytie::val == 123); - $Mytie::val = 456; - ok ($t == 456); - $t = 789; - ok ($Mytie::val == 789); -} - - -# Usage: str(x) -# Return x forced to a string, not a PVIV. -# -sub str { - my $s = "$_[0]" . ""; - return $s; -} - -my $ivnv_2p128 = 65536.0 * 65536.0 * 65536.0 * 65536.0 - * 65536.0 * 65536.0 * 65536.0 * 65536.0; -kill (0, $ivnv_2p128); -my $str_2p128 = '340282366920938463463374607431768211456'; - -my $uv_max = ~ 0; -my $uv_max_str = ~ 0; -$uv_max_str = "$uv_max_str"; -$uv_max_str = "" . "$uv_max_str"; - - -#------------------------------------------------------------------------------ -# GMP::version - -use GMP qw(version); -print '$GMP::VERSION ',$GMP::VERSION,' GMP::version() ',version(),"\n"; - - -#------------------------------------------------------------------------------ -# GMP::Mpz::new - -ok (mpz(0) == 0); -ok (mpz('0') == 0); -ok (mpz(substr('101',1,1)) == 0); -ok (mpz(0.0) == 0); -ok (mpz(mpz(0)) == 0); -ok (mpz(mpq(0)) == 0); -ok (mpz(mpf(0)) == 0); - -{ tie my $t, 'Mytie', 0; - ok (mpz($t) == 0); - ok ($Mytie::fetched > 0); -} -{ tie my $t, 'Mytie', '0'; - ok (mpz($t) == 0); - ok ($Mytie::fetched > 0); -} -{ tie my $t, 'Mytie', substr('101',1,1); ok (mpz($t) == 0); } -{ tie my $t, 'Mytie', 0.0; ok (mpz($t) == 0); } -{ tie my $t, 'Mytie', mpz(0); ok (mpz($t) == 0); } -{ tie my $t, 'Mytie', mpq(0); ok (mpz($t) == 0); } -{ tie my $t, 'Mytie', mpf(0); ok (mpz($t) == 0); } - -ok (mpz(-123) == -123); -ok (mpz('-123') == -123); -ok (mpz(substr('1-1231',1,4)) == -123); -ok (mpz(-123.0) == -123); -ok (mpz(mpz(-123)) == -123); -ok (mpz(mpq(-123)) == -123); -ok (mpz(mpf(-123)) == -123); - -{ tie my $t, 'Mytie', -123; ok (mpz($t) == -123); } -{ tie my $t, 'Mytie', '-123'; ok (mpz($t) == -123); } -{ tie my $t, 'Mytie', substr('1-1231',1,4); ok (mpz($t) == -123); } -{ tie my $t, 'Mytie', -123.0; ok (mpz($t) == -123); } -{ tie my $t, 'Mytie', mpz(-123); ok (mpz($t) == -123); } -{ tie my $t, 'Mytie', mpq(-123); ok (mpz($t) == -123); } -{ tie my $t, 'Mytie', mpf(-123); ok (mpz($t) == -123); } - -ok (mpz($ivnv_2p128) == $str_2p128); -{ tie my $t, 'Mytie', $ivnv_2p128; ok (mpz($t) == $str_2p128); } - -ok (mpz($uv_max) > 0); -ok (mpz($uv_max) == mpz($uv_max_str)); -{ tie my $t, 'Mytie', $uv_max; ok (mpz($t) > 0); } -{ tie my $t, 'Mytie', $uv_max; ok (mpz($t) == mpz($uv_max_str)); } - -{ my $s = '999999999999999999999999999999'; - kill (0, $s); - ok (mpz($s) == '999999999999999999999999999999'); - tie my $t, 'Mytie', $s; - ok (mpz($t) == '999999999999999999999999999999'); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_abs - -ok (abs(mpz(0)) == 0); -ok (abs(mpz(123)) == 123); -ok (abs(mpz(-123)) == 123); - -{ my $x = mpz(-123); $x = abs($x); ok ($x == 123); } -{ my $x = mpz(0); $x = abs($x); ok ($x == 0); } -{ my $x = mpz(123); $x = abs($x); ok ($x == 123); } - -{ tie my $t, 'Mytie', mpz(0); ok (abs($t) == 0); } -{ tie my $t, 'Mytie', mpz(123); ok (abs($t) == 123); } -{ tie my $t, 'Mytie', mpz(-123); ok (abs($t) == 123); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_add - -ok (mpz(0) + 1 == 1); -ok (mpz(-1) + 1 == 0); -ok (1 + mpz(0) == 1); -ok (1 + mpz(-1) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_addeq - -{ my $a = mpz(7); $a += 1; ok ($a == 8); } -{ my $a = mpz(7); my $b = $a; $a += 1; ok ($a == 8); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_and - -ok ((mpz(3) & 1) == 1); -ok ((mpz(3) & 4) == 0); - -{ my $a = mpz(3); $a &= 1; ok ($a == 1); } -{ my $a = mpz(3); $a &= 4; ok ($a == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_bool - -if (mpz(0)) { ok (0); } else { ok (1); } -if (mpz(123)) { ok (1); } else { ok (0); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_com - -ok (~ mpz(0) == -1); -ok (~ mpz(1) == -2); -ok (~ mpz(-2) == 1); -ok (~ mpz(0xFF) == -0x100); -ok (~ mpz(-0x100) == 0xFF); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_dec - -{ my $a = mpz(0); ok ($a-- == 0); ok ($a == -1); } -{ my $a = mpz(0); ok (--$a == -1); } - -{ my $a = mpz(0); my $b = $a; $a--; ok ($a == -1); ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_div - -ok (mpz(6) / 2 == 3); -ok (mpz(-6) / 2 == -3); -ok (mpz(6) / -2 == -3); -ok (mpz(-6) / -2 == 3); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_diveq - -{ my $a = mpz(21); $a /= 3; ok ($a == 7); } -{ my $a = mpz(21); my $b = $a; $a /= 3; ok ($a == 7); ok ($b == 21); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_eq - -{ my $a = mpz(0); - my $b = $a; - $a = mpz(1); - ok ($a == 1); - ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_inc - -{ my $a = mpz(0); ok ($a++ == 0); ok ($a == 1); } -{ my $a = mpz(0); ok (++$a == 1); } - -{ my $a = mpz(0); my $b = $a; $a++; ok ($a == 1); ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_ior - -ok ((mpz(3) | 1) == 3); -ok ((mpz(3) | 4) == 7); - -{ my $a = mpz(3); $a |= 1; ok ($a == 3); } -{ my $a = mpz(3); $a |= 4; ok ($a == 7); } - -ok ((mpz("0xAA") | mpz("0x55")) == mpz("0xFF")); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_lshift - -{ my $a = mpz(7) << 1; ok ($a == 14); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_lshifteq - -{ my $a = mpz(7); $a <<= 1; ok ($a == 14); } -{ my $a = mpz(7); my $b = $a; $a <<= 1; ok ($a == 14); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_mul - -ok (mpz(2) * 3 == 6); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_muleq - -{ my $a = mpz(7); $a *= 3; ok ($a == 21); } -{ my $a = mpz(7); my $b = $a; $a *= 3; ok ($a == 21); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_neg - -ok (- mpz(0) == 0); -ok (- mpz(123) == -123); -ok (- mpz(-123) == 123); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_not - -if (not mpz(0)) { ok (1); } else { ok (0); } -if (not mpz(123)) { ok (0); } else { ok (1); } - -ok ((! mpz(0)) == 1); -ok ((! mpz(123)) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_pow - -ok (mpz(0) ** 1 == 0); -ok (mpz(1) ** 1 == 1); -ok (mpz(2) ** 0 == 1); -ok (mpz(2) ** 1 == 2); -ok (mpz(2) ** 2 == 4); -ok (mpz(2) ** 3 == 8); -ok (mpz(2) ** 4 == 16); - -ok (mpz(0) ** mpz(1) == 0); -ok (mpz(1) ** mpz(1) == 1); -ok (mpz(2) ** mpz(0) == 1); -ok (mpz(2) ** mpz(1) == 2); -ok (mpz(2) ** mpz(2) == 4); -ok (mpz(2) ** mpz(3) == 8); -ok (mpz(2) ** mpz(4) == 16); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_poweq - -{ my $a = mpz(3); $a **= 4; ok ($a == 81); } -{ my $a = mpz(3); my $b = $a; $a **= 4; ok ($a == 81); ok ($b == 3); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_rem - -ok (mpz(-8) % 3 == -2); -ok (mpz(-7) % 3 == -1); -ok (mpz(-6) % 3 == 0); -ok (mpz(6) % 3 == 0); -ok (mpz(7) % 3 == 1); -ok (mpz(8) % 3 == 2); - -{ my $a = mpz(24); $a %= 7; ok ($a == 3); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_rshift - -{ my $a = mpz(32) >> 1; ok ($a == 16); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_rshifteq - -{ my $a = mpz(32); $a >>= 1; ok ($a == 16); } -{ my $a = mpz(32); my $b = $a; $a >>= 1; ok ($a == 16); ok ($b == 32); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_spaceship - -ok (mpz(0) < 1); -ok (mpz(0) > -1); - -ok (mpz(0) != 1); -ok (mpz(0) != -1); -ok (mpz(1) != 0); -ok (mpz(1) != -1); -ok (mpz(-1) != 0); -ok (mpz(-1) != 1); - -ok (mpz(0) < 1.0); -ok (mpz(0) < '1'); -ok (mpz(0) < substr('-1',1,1)); -ok (mpz(0) < mpz(1)); -ok (mpz(0) < mpq(1)); -ok (mpz(0) < mpf(1)); -ok (mpz(0) < $uv_max); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_sqrt - -ok (sqrt(mpz(0)) == 0); -ok (sqrt(mpz(1)) == 1); -ok (sqrt(mpz(4)) == 2); -ok (sqrt(mpz(81)) == 9); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_string - -{ my $x = mpz(0); ok("$x" eq "0"); } -{ my $x = mpz(123); ok("$x" eq "123"); } -{ my $x = mpz(-123); ok("$x" eq "-123"); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_sub - -ok (mpz(0) - 1 == -1); -ok (mpz(1) - 1 == 0); -ok (1 - mpz(0) == 1); -ok (1 - mpz(1) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_subeq - -{ my $a = mpz(7); $a -= 1; ok ($a == 6); } -{ my $a = mpz(7); my $b = $a; $a -= 1; ok ($a == 6); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::overload_xor - -ok ((mpz(3) ^ 1) == 2); -ok ((mpz(3) ^ 4) == 7); - -{ my $a = mpz(3); $a ^= 1; ok ($a == 2); } -{ my $a = mpz(3); $a ^= 4; ok ($a == 7); } - - -#------------------------------------------------------------------------------ -# GMP::Mpz::bin - -ok (bin(2,0) == 1); -ok (bin(2,1) == 2); -ok (bin(2,2) == 1); - -ok (bin(3,0) == 1); -ok (bin(3,1) == 3); -ok (bin(3,2) == 3); -ok (bin(3,3) == 1); - - -#------------------------------------------------------------------------------ -# GMP::Mpz::cdiv - -{ my ($q, $r); - ($q, $r) = cdiv (16, 3); - ok ($q == 6); - ok ($r == -2); - ($q, $r) = cdiv (16, -3); - ok ($q == -5); - ok ($r == 1); - ($q, $r) = cdiv (-16, 3); - ok ($q == -5); - ok ($r == -1); - ($q, $r) = cdiv (-16, -3); - ok ($q == 6); - ok ($r == 2); -} - - -#------------------------------------------------------------------------------ -# GMP::Mpz::cdiv_2exp - -{ my ($q, $r); - ($q, $r) = cdiv_2exp (23, 2); - ok ($q == 6); - ok ($r == -1); - ($q, $r) = cdiv_2exp (-23, 2); - ok ($q == -5); - ok ($r == -3); -} - - -#------------------------------------------------------------------------------ -# GMP::Mpz::clrbit - -{ my $a = mpz(3); clrbit ($a, 1); ok ($a == 1); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } -{ my $a = mpz(3); clrbit ($a, 2); ok ($a == 3); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } - -{ my $a = 3; clrbit ($a, 1); ok ($a == 1); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } -{ my $a = 3; clrbit ($a, 2); ok ($a == 3); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } - -# mutate only given variable -{ my $a = mpz(3); - my $b = $a; - clrbit ($a, 0); - ok ($a == 2); - ok ($b == 3); -} -{ my $a = 3; - my $b = $a; - clrbit ($a, 0); - ok ($a == 2); - ok ($b == 3); -} - -{ tie my $a, 'Mytie', mpz(3); - clrbit ($a, 1); - ok ($Mytie::fetched > 0); # used fetch - ok ($Mytie::stored > 0); # used store - ok ($a == 1); # expected result - ok (UNIVERSAL::isa($a,"GMP::Mpz")); - ok (tied($a)); # still tied -} -{ tie my $a, 'Mytie', 3; - clrbit ($a, 1); - ok ($Mytie::fetched > 0); # used fetch - ok ($Mytie::stored > 0); # used store - ok ($a == 1); # expected result - ok (UNIVERSAL::isa($a,"GMP::Mpz")); - ok (tied($a)); # still tied -} - -{ my $b = mpz(3); - tie my $a, 'Mytie', $b; - clrbit ($a, 0); - ok ($a == 2); - ok ($b == 3); - ok (tied($a)); -} -{ my $b = 3; - tie my $a, 'Mytie', $b; - clrbit ($a, 0); - ok ($a == 2); - ok ($b == 3); - ok (tied($a)); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::combit - -{ my $a = mpz(3); combit ($a, 1); ok ($a == 1); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } -{ my $a = mpz(3); combit ($a, 2); ok ($a == 7); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } - -{ my $a = 3; combit ($a, 1); ok ($a == 1); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } -{ my $a = 3; combit ($a, 2); ok ($a == 7); - ok (UNIVERSAL::isa($a,"GMP::Mpz")); } - -# mutate only given variable -{ my $a = mpz(3); - my $b = $a; - combit ($a, 0); - ok ($a == 2); - ok ($b == 3); -} -{ my $a = 3; - my $b = $a; - combit ($a, 0); - ok ($a == 2); - ok ($b == 3); -} - -{ tie my $a, 'Mytie', mpz(3); - combit ($a, 2); - ok ($Mytie::fetched > 0); # used fetch - ok ($Mytie::stored > 0); # used store - ok ($a == 7); # expected result - ok (UNIVERSAL::isa($a,"GMP::Mpz")); - ok (tied($a)); # still tied -} -{ tie my $a, 'Mytie', 3; - combit ($a, 2); - ok ($Mytie::fetched > 0); # used fetch - ok ($Mytie::stored > 0); # used store - ok ($a == 7); # expected result - ok (UNIVERSAL::isa($a,"GMP::Mpz")); - ok (tied($a)); # still tied -} - -{ my $b = mpz(3); - tie my $a, 'Mytie', $b; - combit ($a, 0); - ok ($a == 2); - ok ($b == 3); - ok (tied($a)); -} -{ my $b = 3; - tie my $a, 'Mytie', $b; - combit ($a, 0); - ok ($a == 2); - ok ($b == 3); - ok (tied($a)); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::congruent_p - -ok ( congruent_p (21, 0, 7)); -ok (! congruent_p (21, 1, 7)); -ok ( congruent_p (21, 5, 8)); -ok (! congruent_p (21, 6, 8)); - - -#------------------------------------------------------------------------------ -# GMP::Mpz::congruent_2exp_p - -ok ( congruent_2exp_p (20, 0, 2)); -ok (! congruent_2exp_p (21, 0, 2)); -ok (! congruent_2exp_p (20, 1, 2)); - -#------------------------------------------------------------------------------ -# GMP::Mpz::divexact - -ok (divexact(27,3) == 9); -ok (divexact(27,-3) == -9); -ok (divexact(-27,3) == -9); -ok (divexact(-27,-3) == 9); - -#------------------------------------------------------------------------------ -# GMP::Mpz::divisible_p - -ok ( divisible_p (21, 7)); -ok (! divisible_p (21, 8)); - -#------------------------------------------------------------------------------ -# GMP::Mpz::divisible_2exp_p - -ok ( divisible_2exp_p (20, 2)); -ok (! divisible_2exp_p (21, 2)); - -#------------------------------------------------------------------------------ -# GMP::Mpz::even_p - -ok (! even_p(mpz(-3))); -ok ( even_p(mpz(-2))); -ok (! even_p(mpz(-1))); -ok ( even_p(mpz(0))); -ok (! even_p(mpz(1))); -ok ( even_p(mpz(2))); -ok (! even_p(mpz(3))); - -#------------------------------------------------------------------------------ -# GMP::Mpz::export - -{ my $s = mpz_export (1, 2, 1, 0, "0x61626364"); - ok ($s eq 'abcd'); } -{ my $s = mpz_export (-1, 2, 1, 0, "0x61626364"); - ok ($s eq 'cdab'); } -{ my $s = mpz_export (1, 2, -1, 0, "0x61626364"); - ok ($s eq 'badc'); } -{ my $s = mpz_export (-1, 2, -1, 0, "0x61626364"); - ok ($s eq 'dcba'); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::fac - -ok (fac(0) == 1); -ok (fac(1) == 1); -ok (fac(2) == 2); -ok (fac(3) == 6); -ok (fac(4) == 24); -ok (fac(5) == 120); - -#------------------------------------------------------------------------------ -# GMP::Mpz::fdiv - -{ my ($q, $r); - ($q, $r) = fdiv (16, 3); - ok ($q == 5); - ok ($r == 1); - ($q, $r) = fdiv (16, -3); - ok ($q == -6); - ok ($r == -2); - ($q, $r) = fdiv (-16, 3); - ok ($q == -6); - ok ($r == 2); - ($q, $r) = fdiv (-16, -3); - ok ($q == 5); - ok ($r == -1); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::fdiv_2exp - -{ my ($q, $r); - ($q, $r) = fdiv_2exp (23, 2); - ok ($q == 5); - ok ($r == 3); - ($q, $r) = fdiv_2exp (-23, 2); - ok ($q == -6); - ok ($r == 1); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::fib - -ok (fib(0) == 0); -ok (fib(1) == 1); -ok (fib(2) == 1); -ok (fib(3) == 2); -ok (fib(4) == 3); -ok (fib(5) == 5); -ok (fib(6) == 8); - -#------------------------------------------------------------------------------ -# GMP::Mpz::fib2 - -{ my ($a, $b) = fib2(0); ok($a==0); ok($b==1); } -{ my ($a, $b) = fib2(1); ok($a==1); ok($b==0); } -{ my ($a, $b) = fib2(2); ok($a==1); ok($b==1); } -{ my ($a, $b) = fib2(3); ok($a==2); ok($b==1); } -{ my ($a, $b) = fib2(4); ok($a==3); ok($b==2); } -{ my ($a, $b) = fib2(5); ok($a==5); ok($b==3); } -{ my ($a, $b) = fib2(6); ok($a==8); ok($b==5); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::gcd - -ok (gcd (21) == 21); -ok (gcd (21,15) == 3); -ok (gcd (21,15,30,57) == 3); -ok (gcd (21,-15) == 3); -ok (gcd (-21,15) == 3); -ok (gcd (-21,-15) == 3); - -#------------------------------------------------------------------------------ -# GMP::Mpz::gcdext - -{ - my ($g, $x, $y) = gcdext (3,5); - ok ($g == 1); - ok ($x == 2); - ok ($y == -1); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::hamdist - -ok (hamdist(5,7) == 1); - -#------------------------------------------------------------------------------ -# GMP::Mpz::import - -{ my $z = mpz_import (1, 2, 1, 0, 'abcd'); - ok ($z == 0x61626364); } -{ my $z = mpz_import (-1, 2, 1, 0, 'abcd'); - ok ($z == 0x63646162); } -{ my $z = mpz_import (1, 2, -1, 0, 'abcd'); - ok ($z == 0x62616463); } -{ my $z = mpz_import (-1, 2, -1, 0, 'abcd'); - ok ($z == 0x64636261); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::invert - -ok (invert(1,123) == 1); -ok (invert(6,7) == 6); -ok (! defined invert(2,8)); - -#------------------------------------------------------------------------------ -# GMP::Mpz::jacobi, GMP::Mpz::kronecker - -foreach my $i ([ 1, 19, 1 ], - [ 4, 19, 1 ], - [ 5, 19, 1 ], - [ 6, 19, 1 ], - [ 7, 19, 1 ], - [ 9, 19, 1 ], - [ 11, 19, 1 ], - [ 16, 19, 1 ], - [ 17, 19, 1 ], - [ 2, 19, -1 ], - [ 3, 19, -1 ], - [ 8, 19, -1 ], - [ 10, 19, -1 ], - [ 12, 19, -1 ], - [ 13, 19, -1 ], - [ 14, 19, -1 ], - [ 15, 19, -1 ], - [ 18, 19, -1 ]) { - foreach my $fun (\&jacobi, \&kronecker) { - ok (&$fun ($$i[0], $$i[1]) == $$i[2]); - - ok (&$fun ($$i[0], str($$i[1])) == $$i[2]); - ok (&$fun (str($$i[0]), $$i[1]) == $$i[2]); - ok (&$fun (str($$i[0]), str($$i[1])) == $$i[2]); - - ok (&$fun ($$i[0], mpz($$i[1])) == $$i[2]); - ok (&$fun (mpz($$i[0]), $$i[1]) == $$i[2]); - ok (&$fun (mpz($$i[0]), mpz($$i[1])) == $$i[2]); - } -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::lcm - -ok (lcm (2) == 2); -ok (lcm (0) == 0); -ok (lcm (0,0) == 0); -ok (lcm (0,0,0) == 0); -ok (lcm (0,0,0,0) == 0); -ok (lcm (2,0) == 0); -ok (lcm (-2,0) == 0); -ok (lcm (2,3) == 6); -ok (lcm (2,3,4) == 12); -ok (lcm (2,-3) == 6); -ok (lcm (-2,3) == 6); -ok (lcm (-2,-3) == 6); -ok (lcm (mpz(2)**512,1) == mpz(2)**512); -ok (lcm (mpz(2)**512,-1) == mpz(2)**512); -ok (lcm (-mpz(2)**512,1) == mpz(2)**512); -ok (lcm (-mpz(2)**512,-1) == mpz(2)**512); -ok (lcm (mpz(2)**512,mpz(2)**512) == mpz(2)**512); -ok (lcm (mpz(2)**512,-mpz(2)**512) == mpz(2)**512); -ok (lcm (-mpz(2)**512,mpz(2)**512) == mpz(2)**512); -ok (lcm (-mpz(2)**512,-mpz(2)**512) == mpz(2)**512); - -#------------------------------------------------------------------------------ -# GMP::Mpz::lucnum - -ok (lucnum(0) == 2); -ok (lucnum(1) == 1); -ok (lucnum(2) == 3); -ok (lucnum(3) == 4); -ok (lucnum(4) == 7); -ok (lucnum(5) == 11); -ok (lucnum(6) == 18); - -#------------------------------------------------------------------------------ -# GMP::Mpz::lucnum2 - -{ my ($a, $b) = lucnum2(0); ok($a==2); ok($b==-1); } -{ my ($a, $b) = lucnum2(1); ok($a==1); ok($b==2); } -{ my ($a, $b) = lucnum2(2); ok($a==3); ok($b==1); } -{ my ($a, $b) = lucnum2(3); ok($a==4); ok($b==3); } -{ my ($a, $b) = lucnum2(4); ok($a==7); ok($b==4); } -{ my ($a, $b) = lucnum2(5); ok($a==11); ok($b==7); } -{ my ($a, $b) = lucnum2(6); ok($a==18); ok($b==11); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::nextprime - -ok (nextprime(2) == 3); -ok (nextprime(3) == 5); -ok (nextprime(5) == 7); -ok (nextprime(7) == 11); -ok (nextprime(11) == 13); - -#------------------------------------------------------------------------------ -# GMP::Mpz::perfect_power_p - -# ok ( perfect_power_p(mpz(-27))); -# ok (! perfect_power_p(mpz(-9))); -# ok (! perfect_power_p(mpz(-1))); -ok ( perfect_power_p(mpz(0))); -ok ( perfect_power_p(mpz(1))); -ok (! perfect_power_p(mpz(2))); -ok (! perfect_power_p(mpz(3))); -ok ( perfect_power_p(mpz(4))); -ok ( perfect_power_p(mpz(9))); -ok ( perfect_power_p(mpz(27))); -ok ( perfect_power_p(mpz(81))); - -#------------------------------------------------------------------------------ -# GMP::Mpz::perfect_square_p - -ok (! perfect_square_p(mpz(-9))); -ok (! perfect_square_p(mpz(-1))); -ok ( perfect_square_p(mpz(0))); -ok ( perfect_square_p(mpz(1))); -ok (! perfect_square_p(mpz(2))); -ok (! perfect_square_p(mpz(3))); -ok ( perfect_square_p(mpz(4))); -ok ( perfect_square_p(mpz(9))); -ok (! perfect_square_p(mpz(27))); -ok ( perfect_square_p(mpz(81))); - -#------------------------------------------------------------------------------ -# GMP::Mpz::popcount - -ok (popcount(7) == 3); - -#------------------------------------------------------------------------------ -# GMP::Mpz::powm - -ok (powm (3,2,8) == 1); - -#------------------------------------------------------------------------------ -# GMP::Mpz::probab_prime_p - -ok ( probab_prime_p(89,1)); -ok (! probab_prime_p(81,1)); - -#------------------------------------------------------------------------------ -# GMP::Mpz::realloc - -{ my $z = mpz(123); - realloc ($z, 512); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::remove - -{ - my ($rem, $mult); - ($rem, $mult) = remove(12,3); - ok ($rem == 4); - ok ($mult == 1); - ($rem, $mult) = remove(12,2); - ok ($rem == 3); - ok ($mult == 2); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::root - -ok (root(0,2) == 0); -ok (root(8,3) == 2); -ok (root(-8,3) == -2); -ok (root(81,4) == 3); -ok (root(243,5) == 3); - -#------------------------------------------------------------------------------ -# GMP::Mpz::roote - -{ my ($r,$e); - ($r, $e) = roote(0,2); - ok ($r == 0); - ok ($e); - ($r, $e) = roote(81,4); - ok ($r == 3); - ok ($e); - ($r, $e) = roote(85,4); - ok ($r == 3); - ok (! $e); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::rootrem - -{ my ($root, $rem) = rootrem (mpz(0), 1); - ok ($root == 0); ok ($rem == 0); } -{ my ($root, $rem) = rootrem (mpz(0), 2); - ok ($root == 0); ok ($rem == 0); } -{ my ($root, $rem) = rootrem (mpz(64), 2); - ok ($root == 8); ok ($rem == 0); } -{ my ($root, $rem) = rootrem (mpz(64), 3); - ok ($root == 4); ok ($rem == 0); } -{ my ($root, $rem) = rootrem (mpz(65), 3); - ok ($root == 4); ok ($rem == 1); } - -#------------------------------------------------------------------------------ -# GMP::Mpz::scan0 - -ok (scan0 (0, 0) == 0); -ok (scan0 (1, 0) == 1); -ok (scan0 (3, 0) == 2); -ok (scan0 (-1, 0) == ~0); -ok (scan0 (-2, 1) == ~0); - -#------------------------------------------------------------------------------ -# GMP::Mpz::scan1 - -ok (scan1 (1, 0) == 0); -ok (scan1 (2, 0) == 1); -ok (scan1 (4, 0) == 2); -ok (scan1 (0, 0) == ~0); -ok (scan1 (3, 2) == ~0); - -#------------------------------------------------------------------------------ -# GMP::Mpz::setbit - -{ my $a = mpz(3); setbit ($a, 1); ok ($a == 3); } -{ my $a = mpz(3); setbit ($a, 2); ok ($a == 7); } - -{ my $a = 3; setbit ($a, 1); ok ($a == 3); } -{ my $a = 3; setbit ($a, 2); ok ($a == 7); } - -# mutate only given variable -{ my $a = mpz(0); - my $b = $a; - setbit ($a, 0); - ok ($a == 1); - ok ($b == 0); -} -{ my $a = 0; - my $b = $a; - setbit ($a, 0); - ok ($a == 1); - ok ($b == 0); -} - -{ tie my $a, 'Mytie', mpz(3); - setbit ($a, 2); - ok ($Mytie::fetched > 0); # used fetch - ok ($Mytie::stored > 0); # used store - ok ($a == 7); # expected result - ok (UNIVERSAL::isa($a,"GMP::Mpz")); - ok (tied($a)); # still tied -} -{ tie my $a, 'Mytie', 3; - setbit ($a, 2); - ok ($Mytie::fetched > 0); # used fetch - ok ($Mytie::stored > 0); # used store - ok ($a == 7); # expected result - ok (UNIVERSAL::isa($a,"GMP::Mpz")); - ok (tied($a)); # still tied -} - -{ my $b = mpz(2); - tie my $a, 'Mytie', $b; - setbit ($a, 0); - ok ($a == 3); - ok ($b == 2); - ok (tied($a)); -} -{ my $b = 2; - tie my $a, 'Mytie', $b; - setbit ($a, 0); - ok ($a == 3); - ok ($b == 2); - ok (tied($a)); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::sizeinbase - -ok (sizeinbase(1,10) == 1); -ok (sizeinbase(100,10) == 3); -ok (sizeinbase(9999,10) == 5); - -#------------------------------------------------------------------------------ -# GMP::Mpz::sqrtrem - -{ - my ($root, $rem) = sqrtrem(mpz(0)); - ok ($root == 0); - ok ($rem == 0); -} -{ - my ($root, $rem) = sqrtrem(mpz(1)); - ok ($root == 1); - ok ($rem == 0); -} -{ - my ($root, $rem) = sqrtrem(mpz(2)); - ok ($root == 1); - ok ($rem == 1); -} -{ - my ($root, $rem) = sqrtrem(mpz(9)); - ok ($root == 3); - ok ($rem == 0); -} -{ - my ($root, $rem) = sqrtrem(mpz(35)); - ok ($root == 5); - ok ($rem == 10); -} -{ - my ($root, $rem) = sqrtrem(mpz(0)); - ok ($root == 0); - ok ($rem == 0); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::tdiv - -{ my ($q, $r); - ($q, $r) = tdiv (16, 3); - ok ($q == 5); - ok ($r == 1); - ($q, $r) = tdiv (16, -3); - ok ($q == -5); - ok ($r == 1); - ($q, $r) = tdiv (-16, 3); - ok ($q == -5); - ok ($r == -1); - ($q, $r) = tdiv (-16, -3); - ok ($q == 5); - ok ($r == -1); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::tdiv_2exp - -{ my ($q, $r); - ($q, $r) = tdiv_2exp (23, 2); - ok ($q == 5); - ok ($r == 3); - ($q, $r) = tdiv_2exp (-23, 2); - ok ($q == -5); - ok ($r == -3); -} - -#------------------------------------------------------------------------------ -# GMP::Mpz::tstbit - -ok (tstbit (6, 0) == 0); -ok (tstbit (6, 1) == 1); -ok (tstbit (6, 2) == 1); -ok (tstbit (6, 3) == 0); - - - - -#------------------------------------------------------------------------------ -# GMP::Mpq - -#------------------------------------------------------------------------------ -# GMP::Mpq::new - -ok (mpq(0) == 0); -ok (mpq('0') == 0); -ok (mpq(substr('101',1,1)) == 0); -ok (mpq(0.0) == 0); -ok (mpq(mpz(0)) == 0); -ok (mpq(mpq(0)) == 0); -ok (mpq(mpf(0)) == 0); - -{ tie my $t, 'Mytie', 0; ok (mpq($t) == 0); } -{ tie my $t, 'Mytie', '0'; ok (mpq($t) == 0); } -{ tie my $t, 'Mytie', substr('101',1,1); ok (mpq($t) == 0); } -{ tie my $t, 'Mytie', 0.0; ok (mpq($t) == 0); } -{ tie my $t, 'Mytie', mpz(0); ok (mpq($t) == 0); } -{ tie my $t, 'Mytie', mpq(0); ok (mpq($t) == 0); } -{ tie my $t, 'Mytie', mpf(0); ok (mpq($t) == 0); } - -ok (mpq(-123) == -123); -ok (mpq('-123') == -123); -ok (mpq(substr('1-1231',1,4)) == -123); -ok (mpq(-123.0) == -123); -ok (mpq(mpz(-123)) == -123); -ok (mpq(mpq(-123)) == -123); -ok (mpq(mpf(-123)) == -123); - -{ tie my $t, 'Mytie', -123; ok (mpq($t) == -123); } -{ tie my $t, 'Mytie', '-123'; ok (mpq($t) == -123); } -{ tie my $t, 'Mytie', substr('1-1231',1,4); ok (mpq($t) == -123); } -{ tie my $t, 'Mytie', -123.0; ok (mpq($t) == -123); } -{ tie my $t, 'Mytie', mpz(-123); ok (mpq($t) == -123); } -{ tie my $t, 'Mytie', mpq(-123); ok (mpq($t) == -123); } -{ tie my $t, 'Mytie', mpf(-123); ok (mpq($t) == -123); } - -ok (mpq($ivnv_2p128) == $str_2p128); -{ tie my $t, 'Mytie', $ivnv_2p128; ok (mpq($t) == $str_2p128); } - -ok (mpq('3/2') == mpq(3,2)); -ok (mpq('3/1') == mpq(3,1)); -ok (mpq('-3/2') == mpq(-3,2)); -ok (mpq('-3/1') == mpq(-3,1)); -ok (mpq('0x3') == mpq(3,1)); -ok (mpq('0b111') == mpq(7,1)); -ok (mpq('0b0') == mpq(0,1)); - -ok (mpq($uv_max) > 0); -ok (mpq($uv_max) == mpq($uv_max_str)); -{ tie my $t, 'Mytie', $uv_max; ok (mpq($t) > 0); } -{ tie my $t, 'Mytie', $uv_max; ok (mpq($t) == mpq($uv_max_str)); } - -{ my $x = 123.5; - kill (0, $x); - ok (mpq($x) == 123.5); - tie my $t, 'Mytie', $x; - ok (mpq($t) == 123.5); -} - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_abs - -ok (abs(mpq(0)) == 0); -ok (abs(mpq(123)) == 123); -ok (abs(mpq(-123)) == 123); - -{ my $x = mpq(-123); $x = abs($x); ok ($x == 123); } -{ my $x = mpq(0); $x = abs($x); ok ($x == 0); } -{ my $x = mpq(123); $x = abs($x); ok ($x == 123); } - -{ tie my $t, 'Mytie', mpq(0); ok (abs($t) == 0); } -{ tie my $t, 'Mytie', mpq(123); ok (abs($t) == 123); } -{ tie my $t, 'Mytie', mpq(-123); ok (abs($t) == 123); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_add - -ok (mpq(0) + 1 == 1); -ok (mpq(-1) + 1 == 0); -ok (1 + mpq(0) == 1); -ok (1 + mpq(-1) == 0); - -ok (mpq(1,2)+mpq(1,3) == mpq(5,6)); -ok (mpq(1,2)+mpq(-1,3) == mpq(1,6)); -ok (mpq(-1,2)+mpq(1,3) == mpq(-1,6)); -ok (mpq(-1,2)+mpq(-1,3) == mpq(-5,6)); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_addeq - -{ my $a = mpq(7); $a += 1; ok ($a == 8); } -{ my $a = mpq(7); my $b = $a; $a += 1; ok ($a == 8); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_bool - -if (mpq(0)) { ok (0); } else { ok (1); } -if (mpq(123)) { ok (1); } else { ok (0); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_dec - -{ my $a = mpq(0); ok ($a-- == 0); ok ($a == -1); } -{ my $a = mpq(0); ok (--$a == -1); } - -{ my $a = mpq(0); my $b = $a; $a--; ok ($a == -1); ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_div - -ok (mpq(6) / 2 == 3); -ok (mpq(-6) / 2 == -3); -ok (mpq(6) / -2 == -3); -ok (mpq(-6) / -2 == 3); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_diveq - -{ my $a = mpq(21); $a /= 3; ok ($a == 7); } -{ my $a = mpq(21); my $b = $a; $a /= 3; ok ($a == 7); ok ($b == 21); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_eq - -{ my $a = mpq(0); - my $b = $a; - $a = mpq(1); - ok ($a == 1); - ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_inc - -{ my $a = mpq(0); ok ($a++ == 0); ok ($a == 1); } -{ my $a = mpq(0); ok (++$a == 1); } - -{ my $a = mpq(0); my $b = $a; $a++; ok ($a == 1); ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_lshift - -{ my $a = mpq(7) << 1; ok ($a == 14); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_lshifteq - -{ my $a = mpq(7); $a <<= 1; ok ($a == 14); } -{ my $a = mpq(7); my $b = $a; $a <<= 1; ok ($a == 14); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_mul - -ok (mpq(2) * 3 == 6); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_muleq - -{ my $a = mpq(7); $a *= 3; ok ($a == 21); } -{ my $a = mpq(7); my $b = $a; $a *= 3; ok ($a == 21); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_neg - -ok (- mpq(0) == 0); -ok (- mpq(123) == -123); -ok (- mpq(-123) == 123); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_not - -if (not mpq(0)) { ok (1); } else { ok (0); } -if (not mpq(123)) { ok (0); } else { ok (1); } - -ok ((! mpq(0)) == 1); -ok ((! mpq(123)) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_pow - -ok (mpq(0) ** 1 == 0); -ok (mpq(1) ** 1 == 1); -ok (mpq(2) ** 0 == 1); -ok (mpq(2) ** 1 == 2); -ok (mpq(2) ** 2 == 4); -ok (mpq(2) ** 3 == 8); -ok (mpq(2) ** 4 == 16); - -ok (mpq(0) ** mpq(1) == 0); -ok (mpq(1) ** mpq(1) == 1); -ok (mpq(2) ** mpq(0) == 1); -ok (mpq(2) ** mpq(1) == 2); -ok (mpq(2) ** mpq(2) == 4); -ok (mpq(2) ** mpq(3) == 8); -ok (mpq(2) ** mpq(4) == 16); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_poweq - -{ my $a = mpq(3); $a **= 4; ok ($a == 81); } -{ my $a = mpq(3); my $b = $a; $a **= 4; ok ($a == 81); ok ($b == 3); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_rshift - -{ my $a = mpq(32) >> 1; ok ($a == 16); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_rshifteq - -{ my $a = mpq(32); $a >>= 1; ok ($a == 16); } -{ my $a = mpq(32); my $b = $a; $a >>= 1; ok ($a == 16); ok ($b == 32); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_spaceship - -ok (mpq(0) < 1); -ok (mpq(0) > -1); - -ok (mpq(0) != 1); -ok (mpq(0) != -1); -ok (mpq(1) != 0); -ok (mpq(1) != -1); -ok (mpq(-1) != 0); -ok (mpq(-1) != 1); - -ok (mpq(3,2) > 1); -ok (mpq(3,2) < 2); - -ok (mpq(0) < 1.0); -ok (mpq(0) < '1'); -ok (mpq(0) < substr('-1',1,1)); -ok (mpq(0) < mpz(1)); -ok (mpq(0) < mpq(1)); -ok (mpq(0) < mpf(1)); -ok (mpq(0) < $uv_max); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_string - -{ my $x = mpq(0); ok("$x" eq "0"); } -{ my $x = mpq(123); ok("$x" eq "123"); } -{ my $x = mpq(-123); ok("$x" eq "-123"); } - -{ my $q = mpq(5,7); ok("$q" eq "5/7"); } -{ my $q = mpq(-5,7); ok("$q" eq "-5/7"); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_sub - -ok (mpq(0) - 1 == -1); -ok (mpq(1) - 1 == 0); -ok (1 - mpq(0) == 1); -ok (1 - mpq(1) == 0); - -ok (mpq(1,2)-mpq(1,3) == mpq(1,6)); -ok (mpq(1,2)-mpq(-1,3) == mpq(5,6)); -ok (mpq(-1,2)-mpq(1,3) == mpq(-5,6)); -ok (mpq(-1,2)-mpq(-1,3) == mpq(-1,6)); - -#------------------------------------------------------------------------------ -# GMP::Mpq::overload_subeq - -{ my $a = mpq(7); $a -= 1; ok ($a == 6); } -{ my $a = mpq(7); my $b = $a; $a -= 1; ok ($a == 6); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::canonicalize - -{ my $q = mpq(21,15); canonicalize($q); - ok (num($q) == 7); - ok (den($q) == 5); -} - -#------------------------------------------------------------------------------ -# GMP::Mpq::den - -{ my $q = mpq(5,9); ok (den($q) == 9); } - -#------------------------------------------------------------------------------ -# GMP::Mpq::num - -{ my $q = mpq(5,9); ok (num($q) == 5); } - - - - -#------------------------------------------------------------------------------ -# GMP::Mpf - -#------------------------------------------------------------------------------ -# GMP::Mpf::new - -ok (mpf(0) == 0); -ok (mpf('0') == 0); -ok (mpf(substr('101',1,1)) == 0); -ok (mpf(0.0) == 0); -ok (mpf(mpz(0)) == 0); -ok (mpf(mpq(0)) == 0); -ok (mpf(mpf(0)) == 0); - -{ tie my $t, 'Mytie', 0; ok (mpf($t) == 0); } -{ tie my $t, 'Mytie', '0'; ok (mpf($t) == 0); } -{ tie my $t, 'Mytie', substr('101',1,1); ok (mpf($t) == 0); } -{ tie my $t, 'Mytie', 0.0; ok (mpf($t) == 0); } -{ tie my $t, 'Mytie', mpz(0); ok (mpf($t) == 0); } -{ tie my $t, 'Mytie', mpq(0); ok (mpf($t) == 0); } -{ tie my $t, 'Mytie', mpf(0); ok (mpf($t) == 0); } - -ok (mpf(-123) == -123); -ok (mpf('-123') == -123); -ok (mpf(substr('1-1231',1,4)) == -123); -ok (mpf(-123.0) == -123); -ok (mpf(mpz(-123)) == -123); -ok (mpf(mpq(-123)) == -123); -ok (mpf(mpf(-123)) == -123); - -{ tie my $t, 'Mytie', -123; ok (mpf($t) == -123); } -{ tie my $t, 'Mytie', '-123'; ok (mpf($t) == -123); } -{ tie my $t, 'Mytie', substr('1-1231',1,4); ok (mpf($t) == -123); } -{ tie my $t, 'Mytie', -123.0; ok (mpf($t) == -123); } -{ tie my $t, 'Mytie', mpz(-123); ok (mpf($t) == -123); } -{ tie my $t, 'Mytie', mpq(-123); ok (mpf($t) == -123); } -{ tie my $t, 'Mytie', mpf(-123); ok (mpf($t) == -123); } - -ok (mpf($ivnv_2p128) == $str_2p128); -{ tie my $t, 'Mytie', $ivnv_2p128; ok (mpf($t) == $str_2p128); } - -ok (mpf(-1.5) == -1.5); -ok (mpf(-1.0) == -1.0); -ok (mpf(-0.5) == -0.5); -ok (mpf(0) == 0); -ok (mpf(0.5) == 0.5); -ok (mpf(1.0) == 1.0); -ok (mpf(1.5) == 1.5); - -ok (mpf("-1.5") == -1.5); -ok (mpf("-1.0") == -1.0); -ok (mpf("-0.5") == -0.5); -ok (mpf("0") == 0); -ok (mpf("0.5") == 0.5); -ok (mpf("1.0") == 1.0); -ok (mpf("1.5") == 1.5); - -ok (mpf($uv_max) > 0); -ok (mpf($uv_max) == mpf($uv_max_str)); -{ tie my $t, 'Mytie', $uv_max; ok (mpf($t) > 0); } -{ tie my $t, 'Mytie', $uv_max; ok (mpf($t) == mpf($uv_max_str)); } - -{ my $x = 123.5; - kill (0, $x); - ok (mpf($x) == 123.5); - tie my $t, 'Mytie', $x; - ok (mpf($t) == 123.5); -} - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_abs - -ok (abs(mpf(0)) == 0); -ok (abs(mpf(123)) == 123); -ok (abs(mpf(-123)) == 123); - -{ my $x = mpf(-123); $x = abs($x); ok ($x == 123); } -{ my $x = mpf(0); $x = abs($x); ok ($x == 0); } -{ my $x = mpf(123); $x = abs($x); ok ($x == 123); } - -{ tie my $t, 'Mytie', mpf(0); ok (abs($t) == 0); } -{ tie my $t, 'Mytie', mpf(123); ok (abs($t) == 123); } -{ tie my $t, 'Mytie', mpf(-123); ok (abs($t) == 123); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_add - -ok (mpf(0) + 1 == 1); -ok (mpf(-1) + 1 == 0); -ok (1 + mpf(0) == 1); -ok (1 + mpf(-1) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_addeq - -{ my $a = mpf(7); $a += 1; ok ($a == 8); } -{ my $a = mpf(7); my $b = $a; $a += 1; ok ($a == 8); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_bool - -if (mpf(0)) { ok (0); } else { ok (1); } -if (mpf(123)) { ok (1); } else { ok (0); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_dec - -{ my $a = mpf(0); ok ($a-- == 0); ok ($a == -1); } -{ my $a = mpf(0); ok (--$a == -1); } - -{ my $a = mpf(0); my $b = $a; $a--; ok ($a == -1); ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_div - -ok (mpf(6) / 2 == 3); -ok (mpf(-6) / 2 == -3); -ok (mpf(6) / -2 == -3); -ok (mpf(-6) / -2 == 3); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_diveq - -{ my $a = mpf(21); $a /= 3; ok ($a == 7); } -{ my $a = mpf(21); my $b = $a; $a /= 3; ok ($a == 7); ok ($b == 21); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_eq - -{ my $a = mpf(0); - my $b = $a; - $a = mpf(1); - ok ($a == 1); - ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_inc - -{ my $a = mpf(0); ok ($a++ == 0); ok ($a == 1); } -{ my $a = mpf(0); ok (++$a == 1); } - -{ my $a = mpf(0); my $b = $a; $a++; ok ($a == 1); ok ($b == 0); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_lshift - -{ my $a = mpf(7) << 1; ok ($a == 14); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_lshifteq - -{ my $a = mpf(7); $a <<= 1; ok ($a == 14); } -{ my $a = mpf(7); my $b = $a; $a <<= 1; ok ($a == 14); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_mul - -ok (mpf(2) * 3 == 6); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_muleq - -{ my $a = mpf(7); $a *= 3; ok ($a == 21); } -{ my $a = mpf(7); my $b = $a; $a *= 3; ok ($a == 21); ok ($b == 7); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_neg - -ok (- mpf(0) == 0); -ok (- mpf(123) == -123); -ok (- mpf(-123) == 123); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_not - -if (not mpf(0)) { ok (1); } else { ok (0); } -if (not mpf(123)) { ok (0); } else { ok (1); } - -ok ((! mpf(0)) == 1); -ok ((! mpf(123)) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_pow - -ok (mpf(0) ** 1 == 0); -ok (mpf(1) ** 1 == 1); -ok (mpf(2) ** 0 == 1); -ok (mpf(2) ** 1 == 2); -ok (mpf(2) ** 2 == 4); -ok (mpf(2) ** 3 == 8); -ok (mpf(2) ** 4 == 16); - -ok (mpf(0) ** mpf(1) == 0); -ok (mpf(1) ** mpf(1) == 1); -ok (mpf(2) ** mpf(0) == 1); -ok (mpf(2) ** mpf(1) == 2); -ok (mpf(2) ** mpf(2) == 4); -ok (mpf(2) ** mpf(3) == 8); -ok (mpf(2) ** mpf(4) == 16); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_poweq - -{ my $a = mpf(3); $a **= 4; ok ($a == 81); } -{ my $a = mpf(3); my $b = $a; $a **= 4; ok ($a == 81); ok ($b == 3); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_rshift - -{ my $a = mpf(32) >> 1; ok ($a == 16); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_rshifteq - -{ my $a = mpf(32); $a >>= 1; ok ($a == 16); } -{ my $a = mpf(32); my $b = $a; $a >>= 1; ok ($a == 16); ok ($b == 32); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_sqrt - -ok (sqrt(mpf(0)) == 0); -ok (sqrt(mpf(1)) == 1); -ok (sqrt(mpf(4)) == 2); -ok (sqrt(mpf(81)) == 9); - -ok (sqrt(mpf(0.25)) == 0.5); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_spaceship - -ok (mpf(0) < 1); -ok (mpf(0) > -1); - -ok (mpf(0) != 1); -ok (mpf(0) != -1); -ok (mpf(1) != 0); -ok (mpf(1) != -1); -ok (mpf(-1) != 0); -ok (mpf(-1) != 1); - -ok (mpf(0) < 1.0); -ok (mpf(0) < '1'); -ok (mpf(0) < substr('-1',1,1)); -ok (mpf(0) < mpz(1)); -ok (mpf(0) < mpq(1)); -ok (mpf(0) < mpf(1)); -ok (mpf(0) < $uv_max); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_string - -{ my $x = mpf(0); ok ("$x" eq "0"); } -{ my $x = mpf(123); ok ("$x" eq "123"); } -{ my $x = mpf(-123); ok ("$x" eq "-123"); } - -{ my $f = mpf(0.25); ok ("$f" eq "0.25"); } -{ my $f = mpf(-0.25); ok ("$f" eq "-0.25"); } -{ my $f = mpf(1.25); ok ("$f" eq "1.25"); } -{ my $f = mpf(-1.25); ok ("$f" eq "-1.25"); } -{ my $f = mpf(1000000); ok ("$f" eq "1000000"); } -{ my $f = mpf(-1000000); ok ("$f" eq "-1000000"); } - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_sub - -ok (mpf(0) - 1 == -1); -ok (mpf(1) - 1 == 0); -ok (1 - mpf(0) == 1); -ok (1 - mpf(1) == 0); - -#------------------------------------------------------------------------------ -# GMP::Mpf::overload_subeq - -{ my $a = mpf(7); $a -= 1; ok ($a == 6); } -{ my $a = mpf(7); my $b = $a; $a -= 1; ok ($a == 6); ok ($b == 7); } - - -#------------------------------------------------------------------------------ -# GMP::Mpf::ceil - -ok (ceil (mpf(-7.5)) == -7.0); -ok (ceil (mpf(7.5)) == 8.0); - -#------------------------------------------------------------------------------ -# GMP::Mpf::floor - -ok (floor(mpf(-7.5)) == -8.0); -ok (floor(mpf(7.5)) == 7.0); - -#------------------------------------------------------------------------------ -# GMP::Mpf::mpf_eq - -{ my $old_prec = get_default_prec(); - set_default_prec(128); - - ok ( mpf_eq (mpz("0x10000000000000001"), mpz("0x10000000000000002"), 1)); - ok (! mpf_eq (mpz("0x11"), mpz("0x12"), 128)); - - set_default_prec($old_prec); -} - -#------------------------------------------------------------------------------ -# GMP::Mpf::get_default_prec - -get_default_prec(); - -#------------------------------------------------------------------------------ -# GMP::Mpf::get_prec - -{ my $x = mpf(1.0, 512); - ok (get_prec ($x) == 512); -} - -#------------------------------------------------------------------------------ -# GMP::Mpf::reldiff - -ok (reldiff (2,4) == 1); -ok (reldiff (4,2) == 0.5); - -#------------------------------------------------------------------------------ -# GMP::Mpf::set_default_prec - -{ my $old_prec = get_default_prec(); - - set_default_prec(512); - ok (get_default_prec () == 512); - - set_default_prec($old_prec); -} - -#------------------------------------------------------------------------------ -# GMP::Mpf::set_prec - -{ my $x = mpf(1.0, 512); - my $y = $x; - set_prec ($x, 1024); - ok (get_prec ($x) == 1024); - ok (get_prec ($y) == 512); -} - -#------------------------------------------------------------------------------ -# GMP::Mpf::trunc - -ok (trunc(mpf(-7.5)) == -7.0); -ok (trunc(mpf(7.5)) == 7.0); - - - -#------------------------------------------------------------------------------ -# GMP::Rand - -#------------------------------------------------------------------------------ -# GMP::Rand::new - -{ my $r = randstate(); ok (defined $r); } -{ my $r = randstate('lc_2exp', 1, 2, 3); ok (defined $r); } -{ my $r = randstate('lc_2exp_size', 64); ok (defined $r); } -{ my $r = randstate('lc_2exp_size', 999999999); ok (! defined $r); } -{ my $r = randstate('mt'); ok (defined $r); } - -{ # copying a randstate results in same sequence - my $r1 = randstate('lc_2exp_size', 64); - $r1->seed(123); - my $r2 = randstate($r1); - for (1 .. 20) { - my $z1 = mpz_urandomb($r1, 20); - my $z2 = mpz_urandomb($r2, 20); - ok ($z1 == $z2); - } -} - -#------------------------------------------------------------------------------ -# GMP::Rand::seed - -{ my $r = randstate(); - $r->seed(123); - $r->seed(time()); -} - -#------------------------------------------------------------------------------ -# GMP::Rand::mpf_urandomb - -{ my $r = randstate(); - my $f = mpf_urandomb($r,1024); - ok (UNIVERSAL::isa($f,"GMP::Mpf")); } - -#------------------------------------------------------------------------------ -# GMP::Rand::mpz_urandomb - -{ my $r = randstate(); - my $z = mpz_urandomb($r, 1024); - ok (UNIVERSAL::isa($z,"GMP::Mpz")); } - -#------------------------------------------------------------------------------ -# GMP::Rand::mpz_rrandomb - -{ my $r = randstate(); - my $z = mpz_rrandomb($r, 1024); - ok (UNIVERSAL::isa($z,"GMP::Mpz")); } - -#------------------------------------------------------------------------------ -# GMP::Rand::mpz_urandomm - -{ my $r = randstate(); - my $z = mpz_urandomm($r, mpz(3)**100); - ok (UNIVERSAL::isa($z,"GMP::Mpz")); } - -#------------------------------------------------------------------------------ -# GMP::Rand::mpz_urandomb_ui - -{ my $r = randstate(); - foreach (1 .. 20) { - my $u = gmp_urandomb_ui($r,8); - ok ($u >= 0); - ok ($u < 256); - } -} - -#------------------------------------------------------------------------------ -# GMP::Rand::mpz_urandomm_ui - -{ my $r = randstate(); - foreach (1 .. 20) { - my $u = gmp_urandomm_ui($r,8); - ok ($u >= 0); - ok ($u < 8); - } -} - - - - -#------------------------------------------------------------------------------ -# GMP module - -#------------------------------------------------------------------------------ -# GMP::fits_slong_p - -ok (GMP::fits_slong_p(0)); - -# in perl 5.005 uv_max is only 32-bits on a 64-bit system, so won't exceed a -# long -# ok (! GMP::fits_slong_p($uv_max)); - -ok (GMP::fits_slong_p(0.0)); - -ok (GMP::fits_slong_p('0')); - -ok (GMP::fits_slong_p(substr('999999999999999999999999999999',1,1))); - -ok (! mpz("-9999999999999999999999999999999999999999999")->fits_slong_p()); -ok ( mpz(-123)->fits_slong_p()); -ok ( mpz(0)->fits_slong_p()); -ok ( mpz(123)->fits_slong_p()); -ok (! mpz("9999999999999999999999999999999999999999999")->fits_slong_p()); - -ok (! mpq("-9999999999999999999999999999999999999999999")->fits_slong_p()); -ok ( mpq(-123)->fits_slong_p()); -ok ( mpq(0)->fits_slong_p()); -ok ( mpq(123)->fits_slong_p()); -ok (! mpq("9999999999999999999999999999999999999999999")->fits_slong_p()); - -ok (! mpf("-9999999999999999999999999999999999999999999")->fits_slong_p()); -ok ( mpf(-123)->fits_slong_p()); -ok ( mpf(0)->fits_slong_p()); -ok ( mpf(123)->fits_slong_p()); -ok (! mpf("9999999999999999999999999999999999999999999")->fits_slong_p()); - -#------------------------------------------------------------------------------ -# GMP::get_d - -ok (GMP::get_d(123) == 123.0); - -ok (GMP::get_d($uv_max) > 0); - -ok (GMP::get_d(123.0) == 123.0); - -ok (GMP::get_d('123') == 123.0); - -ok (GMP::get_d(mpz(123)) == 123.0); - -ok (GMP::get_d(mpq(123)) == 123.0); - -ok (GMP::get_d(mpf(123)) == 123.0); - -#------------------------------------------------------------------------------ -# GMP::get_d_2exp - -{ my ($dbl, $exp) = get_d_2exp (0); - ok ($dbl == 0); ok ($exp == 0); } -{ my ($dbl, $exp) = get_d_2exp (1); - ok ($dbl == 0.5); ok ($exp == 1); } - -{ my ($dbl, $exp) = get_d_2exp ($uv_max); - ok ($dbl > 0.0); ok ($exp > 0); } - -{ my ($dbl, $exp) = get_d_2exp (0.5); - ok ($dbl == 0.5); ok ($exp == 0); } -{ my ($dbl, $exp) = get_d_2exp (0.25); - ok ($dbl == 0.5); ok ($exp == -1); } - -{ my ($dbl, $exp) = get_d_2exp ("1.0"); - ok ($dbl == 0.5); ok ($exp == 1); } - -{ my ($dbl, $exp) = get_d_2exp (mpz ("256")); - ok ($dbl == 0.5); ok ($exp == 9); } - -{ my ($dbl, $exp) = get_d_2exp (mpq ("1/16")); - ok ($dbl == 0.5); ok ($exp == -3); } - -{ my ($dbl, $exp) = get_d_2exp (mpf ("1.5")); - ok ($dbl == 0.75); ok ($exp == 1); } -{ my ($dbl, $exp) = get_d_2exp (mpf ("3.0")); - ok ($dbl == 0.75); ok ($exp == 2); } - -#------------------------------------------------------------------------------ -# GMP::get_str - -ok (get_str(-123) eq '-123'); -ok (get_str('-123') eq '-123'); -ok (get_str(substr('x-123x',1,4)) eq '-123'); -ok (get_str(mpz(-123)) eq '-123'); -ok (get_str(mpq(-123)) eq '-123'); - -ok (get_str(-123,10) eq '-123'); -ok (get_str('-123',10) eq '-123'); -ok (get_str(substr('x-123x',1,4),10) eq '-123'); -ok (get_str(mpz(-123),10) eq '-123'); -ok (get_str(mpq(-123),10) eq '-123'); - -ok (get_str(-123,16) eq '-7b'); -ok (get_str('-123',16) eq '-7b'); -ok (get_str(substr('x-123x',1,4),16) eq '-7b'); -ok (get_str(mpz(-123),16) eq '-7b'); -ok (get_str(mpq(-123),16) eq '-7b'); - -ok (get_str(-123,-16) eq '-7B'); -ok (get_str('-123',-16) eq '-7B'); -ok (get_str(substr('x-123x',1,4),-16) eq '-7B'); -ok (get_str(mpz(-123),-16) eq '-7B'); -ok (get_str(mpq(-123),-16) eq '-7B'); - -# is a float in past versions of perl without UV type -{ my ($str, $exp) = get_str($uv_max); - ok ($str eq $uv_max_str); } - -ok (get_str(mpq(5/8)) eq "5/8"); -ok (get_str(mpq(-5/8)) eq "-5/8"); -ok (get_str(mpq(255/256),16) eq "ff/100"); -ok (get_str(mpq(255/256),-16) eq "FF/100"); -ok (get_str(mpq(-255/256),16) eq "-ff/100"); -ok (get_str(mpq(-255/256),-16) eq "-FF/100"); - -{ my ($s,$e) = get_str(1.5, 10); ok ($s eq '15'); ok ($e == 1); } -{ my ($s,$e) = get_str(mpf(1.5), 10); ok ($s eq '15'); ok ($e == 1); } - -{ my ($s,$e) = get_str(-1.5, 10); ok ($s eq '-15'); ok ($e == 1); } -{ my ($s,$e) = get_str(mpf(-1.5), 10); ok ($s eq '-15'); ok ($e == 1); } - -{ my ($s,$e) = get_str(1.5, 16); ok ($s eq '18'); ok ($e == 1); } -{ my ($s,$e) = get_str(mpf(1.5), 16); ok ($s eq '18'); ok ($e == 1); } - -{ my ($s,$e) = get_str(-1.5, 16); ok ($s eq '-18'); ok ($e == 1); } -{ my ($s,$e) = get_str(mpf(-1.5), 16); ok ($s eq '-18'); ok ($e == 1); } - -{ my ($s,$e) = get_str(65536.0, 16); ok ($s eq '1'); ok ($e == 5); } -{ my ($s,$e) = get_str(mpf(65536.0), 16); ok ($s eq '1'); ok ($e == 5); } - -{ my ($s,$e) = get_str(1.625, 16); ok ($s eq '1a'); ok ($e == 1); } -{ my ($s,$e) = get_str(mpf(1.625), 16); ok ($s eq '1a'); ok ($e == 1); } - -{ my ($s,$e) = get_str(1.625, -16); ok ($s eq '1A'); ok ($e == 1); } -{ my ($s,$e) = get_str(mpf(1.625), -16); ok ($s eq '1A'); ok ($e == 1); } - -{ my ($s, $e) = get_str(255.0,16,0); ok ($s eq "ff"); ok ($e == 2); } -{ my ($s, $e) = get_str(mpf(255.0),16,0); ok ($s eq "ff"); ok ($e == 2); } - -{ my ($s, $e) = get_str(255.0,-16,0); ok ($s eq "FF"); ok ($e == 2); } -{ my ($s, $e) = get_str(mpf(255.0),-16,0); ok ($s eq "FF"); ok ($e == 2); } - -#------------------------------------------------------------------------------ -# GMP::get_si - -ok (GMP::get_si(123) == 123.0); - -# better not assume anything about the relatives sizes of long and UV -ok (GMP::get_si($uv_max) != 0); - -ok (GMP::get_si(123.0) == 123.0); - -ok (GMP::get_si('123') == 123.0); - -ok (GMP::get_si(mpz(123)) == 123.0); - -ok (GMP::get_si(mpq(123)) == 123.0); - -ok (GMP::get_si(mpf(123)) == 123.0); - -#------------------------------------------------------------------------------ -# GMP::integer_p - -ok ( GMP::integer_p (0)); -ok ( GMP::integer_p (123)); -ok ( GMP::integer_p (-123)); - -ok ( GMP::integer_p ($uv_max)); - -ok ( GMP::integer_p (0.0)); -ok ( GMP::integer_p (123.0)); -ok ( GMP::integer_p (-123.0)); -ok (! GMP::integer_p (0.5)); -ok (! GMP::integer_p (123.5)); -ok (! GMP::integer_p (-123.5)); - -ok ( GMP::integer_p ('0')); -ok ( GMP::integer_p ('123')); -ok ( GMP::integer_p ('-123')); -ok (! GMP::integer_p ('0.5')); -ok (! GMP::integer_p ('123.5')); -ok (! GMP::integer_p ('-123.5')); -ok (! GMP::integer_p ('5/8')); - -ok ( GMP::integer_p (mpz(1))); - -ok ( GMP::integer_p (mpq(1))); -ok (! GMP::integer_p (mpq(1,2))); - -ok ( GMP::integer_p (mpf(1.0))); -ok (! GMP::integer_p (mpf(1.5))); - -#------------------------------------------------------------------------------ -# GMP::odd_p - -ok (! odd_p(0)); -ok ( odd_p(1)); -ok (! odd_p(2)); - -ok ( odd_p($uv_max)); - -ok ( odd_p(mpz(-3))); -ok (! odd_p(mpz(-2))); -ok ( odd_p(mpz(-1))); -ok (! odd_p(mpz(0))); -ok ( odd_p(mpz(1))); -ok (! odd_p(mpz(2))); -ok ( odd_p(mpz(3))); - -#------------------------------------------------------------------------------ -# GMP::printf - -GMP::printf ("hello world\n"); - -sub via_printf { - my $s; - open TEMP, ">test.tmp" or die; - GMP::printf TEMP @_; - close TEMP or die; - open TEMP, "sgn() == -1); -ok (mpz(0) ->sgn() == 0); -ok (mpz(123) ->sgn() == 1); - -ok (mpq(-123)->sgn() == -1); -ok (mpq(0) ->sgn() == 0); -ok (mpq(123) ->sgn() == 1); - -ok (mpf(-123)->sgn() == -1); -ok (mpf(0) ->sgn() == 0); -ok (mpf(123) ->sgn() == 1); - - - -#------------------------------------------------------------------------------ -# overloaded constants - -if ($] > 5.00503) { - if (! do 'test2.pl') { - die "Cannot run test2.pl\n"; - } -} - - - - -#------------------------------------------------------------------------------ -# $# stuff -# -# For some reason "local $#" doesn't leave $# back at its default undefined -# state when exiting the block. - -{ local $# = 'hi %.0f there'; - my $f = mpf(123); - ok ("$f" eq 'hi 123 there'); } - - - -# Local variables: -# perl-indent-level: 2 -# End: diff --git a/src/gmp/demos/perl/test2.pl b/src/gmp/demos/perl/test2.pl deleted file mode 100644 index dd11b3b44..000000000 --- a/src/gmp/demos/perl/test2.pl +++ /dev/null @@ -1,66 +0,0 @@ -# GMP perl module tests (part 2) - -# Copyright 2001 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -# The following uses of :constants seem to provoke segvs in perl 5.005_03, -# so they're kept separate file to be run only on suitable perl versions. - - -use GMP::Mpz qw(:constants); -{ - my $a = 123; - ok (UNIVERSAL::isa ($a, "GMP::Mpz")); -} -use GMP::Mpz qw(:noconstants); - -use GMP::Mpq qw(:constants); -{ - my $a = 123; - ok (UNIVERSAL::isa ($a, "GMP::Mpq")); -} -use GMP::Mpq qw(:noconstants); - -use GMP::Mpf qw(:constants); -{ - my $a = 123; - ok (UNIVERSAL::isa ($a, "GMP::Mpf")); -} -use GMP::Mpf qw(:noconstants); - - -# compiled constants unchanged by clrbit etc when re-executed -foreach (0, 1, 2) { - use GMP::Mpz qw(:constants); - my $a = 15; - my $b = 6; - use GMP::Mpz qw(:noconstants); - clrbit ($a, 0); - ok ($a == 14); - setbit ($b, 0); - ok ($b == 7); -} - -1; - - -# Local variables: -# perl-indent-level: 2 -# End: diff --git a/src/gmp/demos/perl/typemap b/src/gmp/demos/perl/typemap deleted file mode 100644 index 474a1b63f..000000000 --- a/src/gmp/demos/perl/typemap +++ /dev/null @@ -1,99 +0,0 @@ -# GMP module external subroutine type mappings. - -# Copyright 2001, 2003 Free Software Foundation, Inc. -# -# This file is part of the GNU MP Library. -# -# The GNU MP Library is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation; either version 2.1 of the License, or (at -# your option) any later version. -# -# The GNU MP Library is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with the GNU MP Library; see the file COPYING.LIB. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -# MA 02110-1301, USA. - - -TYPEMAP -const_string T_PV -const_string_assume CONST_STRING_ASSUME -mpz MPZ -mpq MPQ -mpf MPF -mpz_assume MPZ_ASSUME -mpq_assume MPQ_ASSUME -mpf_assume MPF_ASSUME -mpz_coerce MPZ_COERCE -mpq_coerce MPQ_COERCE -mpf_coerce_st0 MPF_COERCE_ST0 -mpf_coerce_def MPF_COERCE_DEF -randstate RANDSTATE -ulong_coerce ULONG_COERCE -malloced_string MALLOCED_STRING -order_noswap ORDER_NOSWAP -dummy DUMMY -# perl 5.005 doesn't have UV in its standard typemap, so use this instead -gmp_UV GMP_UV - - -INPUT -MPZ - class_or_croak ($arg, mpz_class); $var = SvMPZ($arg); -MPQ - class_or_croak ($arg, mpq_class); $var = SvMPQ($arg); -MPF - class_or_croak ($arg, mpf_class); $var = SvMPF($arg); -MPZ_ASSUME - MPZ_ASSUME ($var, $arg) -MPQ_ASSUME - MPQ_ASSUME ($var, $arg) -MPF_ASSUME - MPF_ASSUME ($var, $arg) -MPZ_COERCE - $var = coerce_mpz (tmp_mpz_${(my $stnum=$arg)=~s/[^0-9]//g;\$stnum}, $arg) -MPQ_COERCE - $var = coerce_mpq (tmp_mpq_${(my $stnum=$arg)=~s/[^0-9]//g;\$stnum}, $arg) -MPF_COERCE_ST0 - /* precision follows ST(0) */ - assert (sv_derived_from (ST(0), mpf_class)); - $var = coerce_mpf (tmp_mpf_${(my $stnum=$arg)=~s/[^0-9]//g;\$stnum}, - $arg, mpf_get_prec (SvMPF(ST(0)))) -MPF_COERCE_DEF - /* default precision used */ - $var = coerce_mpf (tmp_mpf_${(my $stnum=$arg)=~s/[^0-9]//g;\$stnum}, - $arg, mpf_get_default_prec()) -RANDSTATE - class_or_croak ($arg, rand_class); $var = SvRANDSTATE($arg); -ULONG_COERCE - $var = coerce_ulong ($arg) -ORDER_NOSWAP - assert ($arg != &PL_sv_yes); -DUMMY - /* dummy $var */ -CONST_STRING_ASSUME - /* No need to check for SvPOKp and use SvPV, this mapping is - only used for overload_constant, which always gets literal - strings. */ - assert (SvPOK ($arg)); - $var = SvPVX ($arg); - - -OUTPUT -MPZ - sv_bless (sv_setref_pv ($arg, NULL, $var), mpz_class_hv); -MPQ - sv_bless (sv_setref_pv ($arg, NULL, $var), mpq_class_hv); -MPF - sv_bless (sv_setref_pv ($arg, NULL, $var), mpf_class_hv); -RANDSTATE - sv_setref_pv ($arg, rand_class, $var); -MALLOCED_STRING - sv_usepvn_mg ($arg, $var, strlen($var)); -GMP_UV - sv_setuv ($arg, (UV) ($var)); diff --git a/src/gmp/demos/pexpr-config-h.in b/src/gmp/demos/pexpr-config-h.in deleted file mode 100644 index 9cec4e16b..000000000 --- a/src/gmp/demos/pexpr-config-h.in +++ /dev/null @@ -1,46 +0,0 @@ -/* Templates for pexpr program configuration. -*- mode:c -*- - -Copyright 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - -/* Define if you have the header file. */ -#define HAVE_SYS_RESOURCE_H @HAVE_SYS_RESOURCE_H_01@ - -/* Define if you have the `clock' function. */ -#define HAVE_CLOCK @HAVE_CLOCK_01@ - -/* Define if you have the `cputime' function. */ -#define HAVE_CPUTIME @HAVE_CPUTIME_01@ - -/* Define if you have the `getrusage' function. */ -#define HAVE_GETRUSAGE @HAVE_GETRUSAGE_01@ - -/* Define if you have the `gettimeofday' function. */ -#define HAVE_GETTIMEOFDAY @HAVE_GETTIMEOFDAY_01@ - -/* Define if you have the `sigaction' function. */ -#define HAVE_SIGACTION @HAVE_SIGACTION_01@ - -/* Define if you have the `sigaltstack' function. */ -#define HAVE_SIGALTSTACK @HAVE_SIGALTSTACK_01@ - -/* Define if you have the `sigstack' function. */ -#define HAVE_SIGSTACK @HAVE_SIGSTACK_01@ - -/* Define if the system has the type `stack_t'. */ -#define HAVE_STACK_T @HAVE_STACK_T_01@ diff --git a/src/gmp/demos/pexpr.c b/src/gmp/demos/pexpr.c deleted file mode 100644 index c1069745c..000000000 --- a/src/gmp/demos/pexpr.c +++ /dev/null @@ -1,1365 +0,0 @@ -/* Program for computing integer expressions using the GNU Multiple Precision - Arithmetic Library. - -Copyright 1997, 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - -/* This expressions evaluator works by building an expression tree (using a - recursive descent parser) which is then evaluated. The expression tree is - useful since we want to optimize certain expressions (like a^b % c). - - Usage: pexpr [options] expr ... - (Assuming you called the executable `pexpr' of course.) - - Command line options: - - -b print output in binary - -o print output in octal - -d print output in decimal (the default) - -x print output in hexadecimal - -b print output in base NUM - -t print timing information - -html output html - -wml output wml - -split split long lines each 80th digit -*/ - -/* Define LIMIT_RESOURCE_USAGE if you want to make sure the program doesn't - use up extensive resources (cpu, memory). Useful for the GMP demo on the - GMP web site, since we cannot load the server too much. */ - -#include "pexpr-config.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#if HAVE_SYS_RESOURCE_H -#include -#endif - -#include "gmp.h" - -/* SunOS 4 and HPUX 9 don't define a canonical SIGSTKSZ, use a default. */ -#ifndef SIGSTKSZ -#define SIGSTKSZ 4096 -#endif - - -#define TIME(t,func) \ - do { int __t0, __t, __tmp; \ - __t0 = cputime (); \ - {func;} \ - __tmp = cputime () - __t0; \ - (t) = __tmp; \ - } while (0) - -/* GMP version 1.x compatibility. */ -#if ! (__GNU_MP_VERSION >= 2) -typedef MP_INT __mpz_struct; -typedef __mpz_struct mpz_t[1]; -typedef __mpz_struct *mpz_ptr; -#define mpz_fdiv_q mpz_div -#define mpz_fdiv_r mpz_mod -#define mpz_tdiv_q_2exp mpz_div_2exp -#define mpz_sgn(Z) ((Z)->size < 0 ? -1 : (Z)->size > 0) -#endif - -/* GMP version 2.0 compatibility. */ -#if ! (__GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1) -#define mpz_swap(a,b) \ - do { __mpz_struct __t; __t = *a; *a = *b; *b = __t;} while (0) -#endif - -jmp_buf errjmpbuf; - -enum op_t {NOP, LIT, NEG, NOT, PLUS, MINUS, MULT, DIV, MOD, REM, INVMOD, POW, - AND, IOR, XOR, SLL, SRA, POPCNT, HAMDIST, GCD, LCM, SQRT, ROOT, FAC, - LOG, LOG2, FERMAT, MERSENNE, FIBONACCI, RANDOM, NEXTPRIME, BINOM}; - -/* Type for the expression tree. */ -struct expr -{ - enum op_t op; - union - { - struct {struct expr *lhs, *rhs;} ops; - mpz_t val; - } operands; -}; - -typedef struct expr *expr_t; - -void cleanup_and_exit __GMP_PROTO ((int)); - -char *skipspace __GMP_PROTO ((char *)); -void makeexp __GMP_PROTO ((expr_t *, enum op_t, expr_t, expr_t)); -void free_expr __GMP_PROTO ((expr_t)); -char *expr __GMP_PROTO ((char *, expr_t *)); -char *term __GMP_PROTO ((char *, expr_t *)); -char *power __GMP_PROTO ((char *, expr_t *)); -char *factor __GMP_PROTO ((char *, expr_t *)); -int match __GMP_PROTO ((char *, char *)); -int matchp __GMP_PROTO ((char *, char *)); -int cputime __GMP_PROTO ((void)); - -void mpz_eval_expr __GMP_PROTO ((mpz_ptr, expr_t)); -void mpz_eval_mod_expr __GMP_PROTO ((mpz_ptr, expr_t, mpz_ptr)); - -char *error; -int flag_print = 1; -int print_timing = 0; -int flag_html = 0; -int flag_wml = 0; -int flag_splitup_output = 0; -char *newline = ""; -gmp_randstate_t rstate; - - - -/* cputime() returns user CPU time measured in milliseconds. */ -#if ! HAVE_CPUTIME -#if HAVE_GETRUSAGE -int -cputime (void) -{ - struct rusage rus; - - getrusage (0, &rus); - return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000; -} -#else -#if HAVE_CLOCK -int -cputime (void) -{ - if (CLOCKS_PER_SEC < 100000) - return clock () * 1000 / CLOCKS_PER_SEC; - return clock () / (CLOCKS_PER_SEC / 1000); -} -#else -int -cputime (void) -{ - return 0; -} -#endif -#endif -#endif - - -int -stack_downwards_helper (char *xp) -{ - char y; - return &y < xp; -} -int -stack_downwards_p (void) -{ - char x; - return stack_downwards_helper (&x); -} - - -void -setup_error_handler (void) -{ -#if HAVE_SIGACTION - struct sigaction act; - act.sa_handler = cleanup_and_exit; - sigemptyset (&(act.sa_mask)); -#define SIGNAL(sig) sigaction (sig, &act, NULL) -#else - struct { int sa_flags; } act; -#define SIGNAL(sig) signal (sig, cleanup_and_exit) -#endif - act.sa_flags = 0; - - /* Set up a stack for signal handling. A typical cause of error is stack - overflow, and in such situation a signal can not be delivered on the - overflown stack. */ -#if HAVE_SIGALTSTACK - { - /* AIX uses stack_t, MacOS uses struct sigaltstack, various other - systems have both. */ -#if HAVE_STACK_T - stack_t s; -#else - struct sigaltstack s; -#endif - s.ss_sp = malloc (SIGSTKSZ); - s.ss_size = SIGSTKSZ; - s.ss_flags = 0; - if (sigaltstack (&s, NULL) != 0) - perror("sigaltstack"); - act.sa_flags = SA_ONSTACK; - } -#else -#if HAVE_SIGSTACK - { - struct sigstack s; - s.ss_sp = malloc (SIGSTKSZ); - if (stack_downwards_p ()) - s.ss_sp += SIGSTKSZ; - s.ss_onstack = 0; - if (sigstack (&s, NULL) != 0) - perror("sigstack"); - act.sa_flags = SA_ONSTACK; - } -#else -#endif -#endif - -#ifdef LIMIT_RESOURCE_USAGE - { - struct rlimit limit; - - limit.rlim_cur = limit.rlim_max = 0; - setrlimit (RLIMIT_CORE, &limit); - - limit.rlim_cur = 3; - limit.rlim_max = 4; - setrlimit (RLIMIT_CPU, &limit); - - limit.rlim_cur = limit.rlim_max = 16 * 1024 * 1024; - setrlimit (RLIMIT_DATA, &limit); - - getrlimit (RLIMIT_STACK, &limit); - limit.rlim_cur = 4 * 1024 * 1024; - setrlimit (RLIMIT_STACK, &limit); - - SIGNAL (SIGXCPU); - } -#endif /* LIMIT_RESOURCE_USAGE */ - - SIGNAL (SIGILL); - SIGNAL (SIGSEGV); -#ifdef SIGBUS /* not in mingw */ - SIGNAL (SIGBUS); -#endif - SIGNAL (SIGFPE); - SIGNAL (SIGABRT); -} - -int -main (int argc, char **argv) -{ - struct expr *e; - int i; - mpz_t r; - int errcode = 0; - char *str; - int base = 10; - - setup_error_handler (); - - gmp_randinit (rstate, GMP_RAND_ALG_LC, 128); - - { -#if HAVE_GETTIMEOFDAY - struct timeval tv; - gettimeofday (&tv, NULL); - gmp_randseed_ui (rstate, tv.tv_sec + tv.tv_usec); -#else - time_t t; - time (&t); - gmp_randseed_ui (rstate, t); -#endif - } - - mpz_init (r); - - while (argc > 1 && argv[1][0] == '-') - { - char *arg = argv[1]; - - if (arg[1] >= '0' && arg[1] <= '9') - break; - - if (arg[1] == 't') - print_timing = 1; - else if (arg[1] == 'b' && arg[2] >= '0' && arg[2] <= '9') - { - base = atoi (arg + 2); - if (base < 2 || base > 36) - { - fprintf (stderr, "error: invalid output base\n"); - exit (-1); - } - } - else if (arg[1] == 'b' && arg[2] == 0) - base = 2; - else if (arg[1] == 'x' && arg[2] == 0) - base = 16; - else if (arg[1] == 'X' && arg[2] == 0) - base = -16; - else if (arg[1] == 'o' && arg[2] == 0) - base = 8; - else if (arg[1] == 'd' && arg[2] == 0) - base = 10; - else if (strcmp (arg, "-html") == 0) - { - flag_html = 1; - newline = "
"; - } - else if (strcmp (arg, "-wml") == 0) - { - flag_wml = 1; - newline = "
"; - } - else if (strcmp (arg, "-split") == 0) - { - flag_splitup_output = 1; - } - else if (strcmp (arg, "-noprint") == 0) - { - flag_print = 0; - } - else - { - fprintf (stderr, "error: unknown option `%s'\n", arg); - exit (-1); - } - argv++; - argc--; - } - - for (i = 1; i < argc; i++) - { - int s; - int jmpval; - - /* Set up error handler for parsing expression. */ - jmpval = setjmp (errjmpbuf); - if (jmpval != 0) - { - fprintf (stderr, "error: %s%s\n", error, newline); - fprintf (stderr, " %s%s\n", argv[i], newline); - if (! flag_html) - { - /* ??? Dunno how to align expression position with arrow in - HTML ??? */ - fprintf (stderr, " "); - for (s = jmpval - (long) argv[i]; --s >= 0; ) - putc (' ', stderr); - fprintf (stderr, "^\n"); - } - - errcode |= 1; - continue; - } - - str = expr (argv[i], &e); - - if (str[0] != 0) - { - fprintf (stderr, - "error: garbage where end of expression expected%s\n", - newline); - fprintf (stderr, " %s%s\n", argv[i], newline); - if (! flag_html) - { - /* ??? Dunno how to align expression position with arrow in - HTML ??? */ - fprintf (stderr, " "); - for (s = str - argv[i]; --s; ) - putc (' ', stderr); - fprintf (stderr, "^\n"); - } - - errcode |= 1; - free_expr (e); - continue; - } - - /* Set up error handler for evaluating expression. */ - if (setjmp (errjmpbuf)) - { - fprintf (stderr, "error: %s%s\n", error, newline); - fprintf (stderr, " %s%s\n", argv[i], newline); - if (! flag_html) - { - /* ??? Dunno how to align expression position with arrow in - HTML ??? */ - fprintf (stderr, " "); - for (s = str - argv[i]; --s >= 0; ) - putc (' ', stderr); - fprintf (stderr, "^\n"); - } - - errcode |= 2; - continue; - } - - if (print_timing) - { - int t; - TIME (t, mpz_eval_expr (r, e)); - printf ("computation took %d ms%s\n", t, newline); - } - else - mpz_eval_expr (r, e); - - if (flag_print) - { - size_t out_len; - char *tmp, *s; - - out_len = mpz_sizeinbase (r, base >= 0 ? base : -base) + 2; -#ifdef LIMIT_RESOURCE_USAGE - if (out_len > 100000) - { - printf ("result is about %ld digits, not printing it%s\n", - (long) out_len - 3, newline); - exit (-2); - } -#endif - tmp = malloc (out_len); - - if (print_timing) - { - int t; - printf ("output conversion "); - TIME (t, mpz_get_str (tmp, base, r)); - printf ("took %d ms%s\n", t, newline); - } - else - mpz_get_str (tmp, base, r); - - out_len = strlen (tmp); - if (flag_splitup_output) - { - for (s = tmp; out_len > 80; s += 80) - { - fwrite (s, 1, 80, stdout); - printf ("%s\n", newline); - out_len -= 80; - } - - fwrite (s, 1, out_len, stdout); - } - else - { - fwrite (tmp, 1, out_len, stdout); - } - - free (tmp); - printf ("%s\n", newline); - } - else - { - printf ("result is approximately %ld digits%s\n", - (long) mpz_sizeinbase (r, base >= 0 ? base : -base), - newline); - } - - free_expr (e); - } - - exit (errcode); -} - -char * -expr (char *str, expr_t *e) -{ - expr_t e2; - - str = skipspace (str); - if (str[0] == '+') - { - str = term (str + 1, e); - } - else if (str[0] == '-') - { - str = term (str + 1, e); - makeexp (e, NEG, *e, NULL); - } - else if (str[0] == '~') - { - str = term (str + 1, e); - makeexp (e, NOT, *e, NULL); - } - else - { - str = term (str, e); - } - - for (;;) - { - str = skipspace (str); - switch (str[0]) - { - case 'p': - if (match ("plus", str)) - { - str = term (str + 4, &e2); - makeexp (e, PLUS, *e, e2); - } - else - return str; - break; - case 'm': - if (match ("minus", str)) - { - str = term (str + 5, &e2); - makeexp (e, MINUS, *e, e2); - } - else - return str; - break; - case '+': - str = term (str + 1, &e2); - makeexp (e, PLUS, *e, e2); - break; - case '-': - str = term (str + 1, &e2); - makeexp (e, MINUS, *e, e2); - break; - default: - return str; - } - } -} - -char * -term (char *str, expr_t *e) -{ - expr_t e2; - - str = power (str, e); - for (;;) - { - str = skipspace (str); - switch (str[0]) - { - case 'm': - if (match ("mul", str)) - { - str = power (str + 3, &e2); - makeexp (e, MULT, *e, e2); - break; - } - if (match ("mod", str)) - { - str = power (str + 3, &e2); - makeexp (e, MOD, *e, e2); - break; - } - return str; - case 'd': - if (match ("div", str)) - { - str = power (str + 3, &e2); - makeexp (e, DIV, *e, e2); - break; - } - return str; - case 'r': - if (match ("rem", str)) - { - str = power (str + 3, &e2); - makeexp (e, REM, *e, e2); - break; - } - return str; - case 'i': - if (match ("invmod", str)) - { - str = power (str + 6, &e2); - makeexp (e, REM, *e, e2); - break; - } - return str; - case 't': - if (match ("times", str)) - { - str = power (str + 5, &e2); - makeexp (e, MULT, *e, e2); - break; - } - if (match ("thru", str)) - { - str = power (str + 4, &e2); - makeexp (e, DIV, *e, e2); - break; - } - if (match ("through", str)) - { - str = power (str + 7, &e2); - makeexp (e, DIV, *e, e2); - break; - } - return str; - case '*': - str = power (str + 1, &e2); - makeexp (e, MULT, *e, e2); - break; - case '/': - str = power (str + 1, &e2); - makeexp (e, DIV, *e, e2); - break; - case '%': - str = power (str + 1, &e2); - makeexp (e, MOD, *e, e2); - break; - default: - return str; - } - } -} - -char * -power (char *str, expr_t *e) -{ - expr_t e2; - - str = factor (str, e); - while (str[0] == '!') - { - str++; - makeexp (e, FAC, *e, NULL); - } - str = skipspace (str); - if (str[0] == '^') - { - str = power (str + 1, &e2); - makeexp (e, POW, *e, e2); - } - return str; -} - -int -match (char *s, char *str) -{ - char *ostr = str; - int i; - - for (i = 0; s[i] != 0; i++) - { - if (str[i] != s[i]) - return 0; - } - str = skipspace (str + i); - return str - ostr; -} - -int -matchp (char *s, char *str) -{ - char *ostr = str; - int i; - - for (i = 0; s[i] != 0; i++) - { - if (str[i] != s[i]) - return 0; - } - str = skipspace (str + i); - if (str[0] == '(') - return str - ostr + 1; - return 0; -} - -struct functions -{ - char *spelling; - enum op_t op; - int arity; /* 1 or 2 means real arity; 0 means arbitrary. */ -}; - -struct functions fns[] = -{ - {"sqrt", SQRT, 1}, -#if __GNU_MP_VERSION >= 2 - {"root", ROOT, 2}, - {"popc", POPCNT, 1}, - {"hamdist", HAMDIST, 2}, -#endif - {"gcd", GCD, 0}, -#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 - {"lcm", LCM, 0}, -#endif - {"and", AND, 0}, - {"ior", IOR, 0}, -#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 - {"xor", XOR, 0}, -#endif - {"plus", PLUS, 0}, - {"pow", POW, 2}, - {"minus", MINUS, 2}, - {"mul", MULT, 0}, - {"div", DIV, 2}, - {"mod", MOD, 2}, - {"rem", REM, 2}, -#if __GNU_MP_VERSION >= 2 - {"invmod", INVMOD, 2}, -#endif - {"log", LOG, 2}, - {"log2", LOG2, 1}, - {"F", FERMAT, 1}, - {"M", MERSENNE, 1}, - {"fib", FIBONACCI, 1}, - {"Fib", FIBONACCI, 1}, - {"random", RANDOM, 1}, - {"nextprime", NEXTPRIME, 1}, - {"binom", BINOM, 2}, - {"binomial", BINOM, 2}, - {"fac", FAC, 1}, - {"fact", FAC, 1}, - {"factorial", FAC, 1}, - {"", NOP, 0} -}; - -char * -factor (char *str, expr_t *e) -{ - expr_t e1, e2; - - str = skipspace (str); - - if (isalpha (str[0])) - { - int i; - int cnt; - - for (i = 0; fns[i].op != NOP; i++) - { - if (fns[i].arity == 1) - { - cnt = matchp (fns[i].spelling, str); - if (cnt != 0) - { - str = expr (str + cnt, &e1); - str = skipspace (str); - if (str[0] != ')') - { - error = "expected `)'"; - longjmp (errjmpbuf, (int) (long) str); - } - makeexp (e, fns[i].op, e1, NULL); - return str + 1; - } - } - } - - for (i = 0; fns[i].op != NOP; i++) - { - if (fns[i].arity != 1) - { - cnt = matchp (fns[i].spelling, str); - if (cnt != 0) - { - str = expr (str + cnt, &e1); - str = skipspace (str); - - if (str[0] != ',') - { - error = "expected `,' and another operand"; - longjmp (errjmpbuf, (int) (long) str); - } - - str = skipspace (str + 1); - str = expr (str, &e2); - str = skipspace (str); - - if (fns[i].arity == 0) - { - while (str[0] == ',') - { - makeexp (&e1, fns[i].op, e1, e2); - str = skipspace (str + 1); - str = expr (str, &e2); - str = skipspace (str); - } - } - - if (str[0] != ')') - { - error = "expected `)'"; - longjmp (errjmpbuf, (int) (long) str); - } - - makeexp (e, fns[i].op, e1, e2); - return str + 1; - } - } - } - } - - if (str[0] == '(') - { - str = expr (str + 1, e); - str = skipspace (str); - if (str[0] != ')') - { - error = "expected `)'"; - longjmp (errjmpbuf, (int) (long) str); - } - str++; - } - else if (str[0] >= '0' && str[0] <= '9') - { - expr_t res; - char *s, *sc; - - res = malloc (sizeof (struct expr)); - res -> op = LIT; - mpz_init (res->operands.val); - - s = str; - while (isalnum (str[0])) - str++; - sc = malloc (str - s + 1); - memcpy (sc, s, str - s); - sc[str - s] = 0; - - mpz_set_str (res->operands.val, sc, 0); - *e = res; - free (sc); - } - else - { - error = "operand expected"; - longjmp (errjmpbuf, (int) (long) str); - } - return str; -} - -char * -skipspace (char *str) -{ - while (str[0] == ' ') - str++; - return str; -} - -/* Make a new expression with operation OP and right hand side - RHS and left hand side lhs. Put the result in R. */ -void -makeexp (expr_t *r, enum op_t op, expr_t lhs, expr_t rhs) -{ - expr_t res; - res = malloc (sizeof (struct expr)); - res -> op = op; - res -> operands.ops.lhs = lhs; - res -> operands.ops.rhs = rhs; - *r = res; - return; -} - -/* Free the memory used by expression E. */ -void -free_expr (expr_t e) -{ - if (e->op != LIT) - { - free_expr (e->operands.ops.lhs); - if (e->operands.ops.rhs != NULL) - free_expr (e->operands.ops.rhs); - } - else - { - mpz_clear (e->operands.val); - } -} - -/* Evaluate the expression E and put the result in R. */ -void -mpz_eval_expr (mpz_ptr r, expr_t e) -{ - mpz_t lhs, rhs; - - switch (e->op) - { - case LIT: - mpz_set (r, e->operands.val); - return; - case PLUS: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_add (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; - case MINUS: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_sub (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; - case MULT: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_mul (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; - case DIV: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_fdiv_q (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; - case MOD: - mpz_init (rhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_abs (rhs, rhs); - mpz_eval_mod_expr (r, e->operands.ops.lhs, rhs); - mpz_clear (rhs); - return; - case REM: - /* Check if lhs operand is POW expression and optimize for that case. */ - if (e->operands.ops.lhs->op == POW) - { - mpz_t powlhs, powrhs; - mpz_init (powlhs); - mpz_init (powrhs); - mpz_init (rhs); - mpz_eval_expr (powlhs, e->operands.ops.lhs->operands.ops.lhs); - mpz_eval_expr (powrhs, e->operands.ops.lhs->operands.ops.rhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_powm (r, powlhs, powrhs, rhs); - if (mpz_cmp_si (rhs, 0L) < 0) - mpz_neg (r, r); - mpz_clear (powlhs); - mpz_clear (powrhs); - mpz_clear (rhs); - return; - } - - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_fdiv_r (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; -#if __GNU_MP_VERSION >= 2 - case INVMOD: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_invert (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; -#endif - case POW: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - if (mpz_cmpabs_ui (lhs, 1) <= 0) - { - /* For 0^rhs and 1^rhs, we just need to verify that - rhs is well-defined. For (-1)^rhs we need to - determine (rhs mod 2). For simplicity, compute - (rhs mod 2) for all three cases. */ - expr_t two, et; - two = malloc (sizeof (struct expr)); - two -> op = LIT; - mpz_init_set_ui (two->operands.val, 2L); - makeexp (&et, MOD, e->operands.ops.rhs, two); - e->operands.ops.rhs = et; - } - - mpz_eval_expr (rhs, e->operands.ops.rhs); - if (mpz_cmp_si (rhs, 0L) == 0) - /* x^0 is 1 */ - mpz_set_ui (r, 1L); - else if (mpz_cmp_si (lhs, 0L) == 0) - /* 0^y (where y != 0) is 0 */ - mpz_set_ui (r, 0L); - else if (mpz_cmp_ui (lhs, 1L) == 0) - /* 1^y is 1 */ - mpz_set_ui (r, 1L); - else if (mpz_cmp_si (lhs, -1L) == 0) - /* (-1)^y just depends on whether y is even or odd */ - mpz_set_si (r, (mpz_get_ui (rhs) & 1) ? -1L : 1L); - else if (mpz_cmp_si (rhs, 0L) < 0) - /* x^(-n) is 0 */ - mpz_set_ui (r, 0L); - else - { - unsigned long int cnt; - unsigned long int y; - /* error if exponent does not fit into an unsigned long int. */ - if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0) - goto pow_err; - - y = mpz_get_ui (rhs); - /* x^y == (x/(2^c))^y * 2^(c*y) */ -#if __GNU_MP_VERSION >= 2 - cnt = mpz_scan1 (lhs, 0); -#else - cnt = 0; -#endif - if (cnt != 0) - { - if (y * cnt / cnt != y) - goto pow_err; - mpz_tdiv_q_2exp (lhs, lhs, cnt); - mpz_pow_ui (r, lhs, y); - mpz_mul_2exp (r, r, y * cnt); - } - else - mpz_pow_ui (r, lhs, y); - } - mpz_clear (lhs); mpz_clear (rhs); - return; - pow_err: - error = "result of `pow' operator too large"; - mpz_clear (lhs); mpz_clear (rhs); - longjmp (errjmpbuf, 1); - case GCD: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_gcd (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; -#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 - case LCM: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_lcm (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; -#endif - case AND: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_and (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; - case IOR: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_ior (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; -#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 - case XOR: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_xor (r, lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - return; -#endif - case NEG: - mpz_eval_expr (r, e->operands.ops.lhs); - mpz_neg (r, r); - return; - case NOT: - mpz_eval_expr (r, e->operands.ops.lhs); - mpz_com (r, r); - return; - case SQRT: - mpz_init (lhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - if (mpz_sgn (lhs) < 0) - { - error = "cannot take square root of negative numbers"; - mpz_clear (lhs); - longjmp (errjmpbuf, 1); - } - mpz_sqrt (r, lhs); - return; -#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 - case ROOT: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - if (mpz_sgn (rhs) <= 0) - { - error = "cannot take non-positive root orders"; - mpz_clear (lhs); mpz_clear (rhs); - longjmp (errjmpbuf, 1); - } - if (mpz_sgn (lhs) < 0 && (mpz_get_ui (rhs) & 1) == 0) - { - error = "cannot take even root orders of negative numbers"; - mpz_clear (lhs); mpz_clear (rhs); - longjmp (errjmpbuf, 1); - } - - { - unsigned long int nth = mpz_get_ui (rhs); - if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0) - { - /* If we are asked to take an awfully large root order, cheat and - ask for the largest order we can pass to mpz_root. This saves - some error prone special cases. */ - nth = ~(unsigned long int) 0; - } - mpz_root (r, lhs, nth); - } - mpz_clear (lhs); mpz_clear (rhs); - return; -#endif - case FAC: - mpz_eval_expr (r, e->operands.ops.lhs); - if (mpz_size (r) > 1) - { - error = "result of `!' operator too large"; - longjmp (errjmpbuf, 1); - } - mpz_fac_ui (r, mpz_get_ui (r)); - return; -#if __GNU_MP_VERSION >= 2 - case POPCNT: - mpz_eval_expr (r, e->operands.ops.lhs); - { long int cnt; - cnt = mpz_popcount (r); - mpz_set_si (r, cnt); - } - return; - case HAMDIST: - { long int cnt; - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - cnt = mpz_hamdist (lhs, rhs); - mpz_clear (lhs); mpz_clear (rhs); - mpz_set_si (r, cnt); - } - return; -#endif - case LOG2: - mpz_eval_expr (r, e->operands.ops.lhs); - { unsigned long int cnt; - if (mpz_sgn (r) <= 0) - { - error = "logarithm of non-positive number"; - longjmp (errjmpbuf, 1); - } - cnt = mpz_sizeinbase (r, 2); - mpz_set_ui (r, cnt - 1); - } - return; - case LOG: - { unsigned long int cnt; - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - if (mpz_sgn (lhs) <= 0) - { - error = "logarithm of non-positive number"; - mpz_clear (lhs); mpz_clear (rhs); - longjmp (errjmpbuf, 1); - } - if (mpz_cmp_ui (rhs, 256) >= 0) - { - error = "logarithm base too large"; - mpz_clear (lhs); mpz_clear (rhs); - longjmp (errjmpbuf, 1); - } - cnt = mpz_sizeinbase (lhs, mpz_get_ui (rhs)); - mpz_set_ui (r, cnt - 1); - mpz_clear (lhs); mpz_clear (rhs); - } - return; - case FERMAT: - { - unsigned long int t; - mpz_init (lhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - t = (unsigned long int) 1 << mpz_get_ui (lhs); - if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0 || t == 0) - { - error = "too large Mersenne number index"; - mpz_clear (lhs); - longjmp (errjmpbuf, 1); - } - mpz_set_ui (r, 1); - mpz_mul_2exp (r, r, t); - mpz_add_ui (r, r, 1); - mpz_clear (lhs); - } - return; - case MERSENNE: - mpz_init (lhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0) - { - error = "too large Mersenne number index"; - mpz_clear (lhs); - longjmp (errjmpbuf, 1); - } - mpz_set_ui (r, 1); - mpz_mul_2exp (r, r, mpz_get_ui (lhs)); - mpz_sub_ui (r, r, 1); - mpz_clear (lhs); - return; - case FIBONACCI: - { mpz_t t; - unsigned long int n, i; - mpz_init (lhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0) - { - error = "Fibonacci index out of range"; - mpz_clear (lhs); - longjmp (errjmpbuf, 1); - } - n = mpz_get_ui (lhs); - mpz_clear (lhs); - -#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1 - mpz_fib_ui (r, n); -#else - mpz_init_set_ui (t, 1); - mpz_set_ui (r, 1); - - if (n <= 2) - mpz_set_ui (r, 1); - else - { - for (i = 3; i <= n; i++) - { - mpz_add (t, t, r); - mpz_swap (t, r); - } - } - mpz_clear (t); -#endif - } - return; - case RANDOM: - { - unsigned long int n; - mpz_init (lhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0) - { - error = "random number size out of range"; - mpz_clear (lhs); - longjmp (errjmpbuf, 1); - } - n = mpz_get_ui (lhs); - mpz_clear (lhs); - mpz_urandomb (r, rstate, n); - } - return; - case NEXTPRIME: - { - mpz_eval_expr (r, e->operands.ops.lhs); - mpz_nextprime (r, r); - } - return; - case BINOM: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_expr (lhs, e->operands.ops.lhs); - mpz_eval_expr (rhs, e->operands.ops.rhs); - { - unsigned long int k; - if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0) - { - error = "k too large in (n over k) expression"; - mpz_clear (lhs); mpz_clear (rhs); - longjmp (errjmpbuf, 1); - } - k = mpz_get_ui (rhs); - mpz_bin_ui (r, lhs, k); - } - mpz_clear (lhs); mpz_clear (rhs); - return; - default: - abort (); - } -} - -/* Evaluate the expression E modulo MOD and put the result in R. */ -void -mpz_eval_mod_expr (mpz_ptr r, expr_t e, mpz_ptr mod) -{ - mpz_t lhs, rhs; - - switch (e->op) - { - case POW: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod); - mpz_eval_expr (rhs, e->operands.ops.rhs); - mpz_powm (r, lhs, rhs, mod); - mpz_clear (lhs); mpz_clear (rhs); - return; - case PLUS: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod); - mpz_eval_mod_expr (rhs, e->operands.ops.rhs, mod); - mpz_add (r, lhs, rhs); - if (mpz_cmp_si (r, 0L) < 0) - mpz_add (r, r, mod); - else if (mpz_cmp (r, mod) >= 0) - mpz_sub (r, r, mod); - mpz_clear (lhs); mpz_clear (rhs); - return; - case MINUS: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod); - mpz_eval_mod_expr (rhs, e->operands.ops.rhs, mod); - mpz_sub (r, lhs, rhs); - if (mpz_cmp_si (r, 0L) < 0) - mpz_add (r, r, mod); - else if (mpz_cmp (r, mod) >= 0) - mpz_sub (r, r, mod); - mpz_clear (lhs); mpz_clear (rhs); - return; - case MULT: - mpz_init (lhs); mpz_init (rhs); - mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod); - mpz_eval_mod_expr (rhs, e->operands.ops.rhs, mod); - mpz_mul (r, lhs, rhs); - mpz_mod (r, r, mod); - mpz_clear (lhs); mpz_clear (rhs); - return; - default: - mpz_init (lhs); - mpz_eval_expr (lhs, e); - mpz_mod (r, lhs, mod); - mpz_clear (lhs); - return; - } -} - -void -cleanup_and_exit (int sig) -{ - switch (sig) { -#ifdef LIMIT_RESOURCE_USAGE - case SIGXCPU: - printf ("expression took too long to evaluate%s\n", newline); - break; -#endif - case SIGFPE: - printf ("divide by zero%s\n", newline); - break; - default: - printf ("expression required too much memory to evaluate%s\n", newline); - break; - } - exit (-2); -} diff --git a/src/gmp/demos/primes.c b/src/gmp/demos/primes.c deleted file mode 100644 index 8f5320d08..000000000 --- a/src/gmp/demos/primes.c +++ /dev/null @@ -1,388 +0,0 @@ -/* List and count primes. - Written by tege while on holiday in Rodupp, August 2001. - Between 10 and 500 times faster than previous program. - -Copyright 2001, 2002 Free Software Foundation, Inc. - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 2 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -#include -#include -#include -#include -#include - -/* IDEAS: - * Do not fill primes[] with real primes when the range [fr,to] is small, - when fr,to are relatively large. Fill primes[] with odd numbers instead. - [Probably a bad idea, since the primes[] array would become very large.] - * Separate small primes and large primes when sieving. Either the Montgomery - way (i.e., having a large array a multiple of L1 cache size), or just - separate loops for primes <= S and primes > S. The latter primes do not - require an inner loop, since they will touch the sieving array at most once. - * Pre-fill sieving array with an appropriately aligned ...00100100... pattern, - then omit 3 from primes array. (May require similar special handling of 3 - as we now have for 2.) - * A large SIEVE_LIMIT currently implies very large memory usage, mainly due - to the sieving array in make_primelist, but also because of the primes[] - array. We might want to stage the program, using sieve_region/find_primes - to build primes[]. Make report() a function pointer, as part of achieving - this. - * Store primes[] as two arrays, one array with primes represented as delta - values using just 8 bits (if gaps are too big, store bogus primes!) - and one array with "rem" values. The latter needs 32-bit values. - * A new entry point, mpz_probab_prime_likely_p, would be useful. - * Improve command line syntax and versatility. "primes -f FROM -t TO", - allow either to be omitted for open interval. (But disallow - "primes -c -f FROM" since that would be infinity.) Allow printing a - limited *number* of primes using syntax like "primes -f FROM -n NUMBER". - * When looking for maxgaps, we should not perform any primality testing until - we find possible record gaps. Should speed up the searches tremendously. - */ - -#include "gmp.h" - -struct primes -{ - unsigned int prime; - int rem; -}; - -struct primes *primes; -unsigned long n_primes; - -void find_primes __GMP_PROTO ((unsigned char *, mpz_t, unsigned long, mpz_t)); -void sieve_region __GMP_PROTO ((unsigned char *, mpz_t, unsigned long)); -void make_primelist __GMP_PROTO ((unsigned long)); - -int flag_print = 1; -int flag_count = 0; -int flag_maxgap = 0; -unsigned long maxgap = 0; -unsigned long total_primes = 0; - -void -report (mpz_t prime) -{ - total_primes += 1; - if (flag_print) - { - mpz_out_str (stdout, 10, prime); - printf ("\n"); - } - if (flag_maxgap) - { - static unsigned long prev_prime_low = 0; - unsigned long gap; - if (prev_prime_low != 0) - { - gap = mpz_get_ui (prime) - prev_prime_low; - if (maxgap < gap) - maxgap = gap; - } - prev_prime_low = mpz_get_ui (prime); - } -} - -int -main (int argc, char *argv[]) -{ - char *progname = argv[0]; - mpz_t fr, to; - mpz_t fr2, to2; - unsigned long sieve_lim; - unsigned long est_n_primes; - unsigned char *s; - mpz_t tmp; - mpz_t siev_sqr_lim; - - while (argc != 1) - { - if (strcmp (argv[1], "-c") == 0) - { - flag_count = 1; - argv++; - argc--; - } - else if (strcmp (argv[1], "-p") == 0) - { - flag_print = 2; - argv++; - argc--; - } - else if (strcmp (argv[1], "-g") == 0) - { - flag_maxgap = 1; - argv++; - argc--; - } - else - break; - } - - if (flag_count || flag_maxgap) - flag_print--; /* clear unless an explicit -p */ - - mpz_init (fr); - mpz_init (to); - mpz_init (fr2); - mpz_init (to2); - - if (argc == 3) - { - mpz_set_str (fr, argv[1], 0); - if (argv[2][0] == '+') - { - mpz_set_str (to, argv[2] + 1, 0); - mpz_add (to, to, fr); - } - else - mpz_set_str (to, argv[2], 0); - } - else if (argc == 2) - { - mpz_set_ui (fr, 0); - mpz_set_str (to, argv[1], 0); - } - else - { - fprintf (stderr, "usage: %s [-c] [-p] [-g] [from [+]]to\n", progname); - exit (1); - } - - mpz_set (fr2, fr); - if (mpz_cmp_ui (fr2, 3) < 0) - { - mpz_set_ui (fr2, 2); - report (fr2); - mpz_set_ui (fr2, 3); - } - mpz_setbit (fr2, 0); /* make odd */ - mpz_sub_ui (to2, to, 1); - mpz_setbit (to2, 0); /* make odd */ - - mpz_init (tmp); - mpz_init (siev_sqr_lim); - - mpz_sqrt (tmp, to2); -#define SIEVE_LIMIT 10000000 - if (mpz_cmp_ui (tmp, SIEVE_LIMIT) < 0) - { - sieve_lim = mpz_get_ui (tmp); - } - else - { - sieve_lim = SIEVE_LIMIT; - mpz_sub (tmp, to2, fr2); - if (mpz_cmp_ui (tmp, sieve_lim) < 0) - sieve_lim = mpz_get_ui (tmp); /* limit sieving for small ranges */ - } - mpz_set_ui (siev_sqr_lim, sieve_lim + 1); - mpz_mul_ui (siev_sqr_lim, siev_sqr_lim, sieve_lim + 1); - - est_n_primes = (size_t) (sieve_lim / log((double) sieve_lim) * 1.13) + 10; - primes = malloc (est_n_primes * sizeof primes[0]); - make_primelist (sieve_lim); - assert (est_n_primes >= n_primes); - -#if DEBUG - printf ("sieve_lim = %lu\n", sieve_lim); - printf ("n_primes = %lu (3..%u)\n", - n_primes, primes[n_primes - 1].prime); -#endif - -#define S (1 << 15) /* FIXME: Figure out L1 cache size */ - s = malloc (S/2); - while (mpz_cmp (fr2, to2) <= 0) - { - unsigned long rsize; - rsize = S; - mpz_add_ui (tmp, fr2, rsize); - if (mpz_cmp (tmp, to2) > 0) - { - mpz_sub (tmp, to2, fr2); - rsize = mpz_get_ui (tmp) + 2; - } -#if DEBUG - printf ("Sieving region ["); mpz_out_str (stdout, 10, fr2); - printf (","); mpz_add_ui (tmp, fr2, rsize - 2); - mpz_out_str (stdout, 10, tmp); printf ("]\n"); -#endif - sieve_region (s, fr2, rsize); - find_primes (s, fr2, rsize / 2, siev_sqr_lim); - - mpz_add_ui (fr2, fr2, S); - } - free (s); - - if (flag_count) - printf ("Pi(interval) = %lu\n", total_primes); - - if (flag_maxgap) - printf ("max gap: %lu\n", maxgap); - - return 0; -} - -/* Find primes in region [fr,fr+rsize). Requires that fr is odd and that - rsize is even. The sieving array s should be aligned for "long int" and - have rsize/2 entries, rounded up to the nearest multiple of "long int". */ -void -sieve_region (unsigned char *s, mpz_t fr, unsigned long rsize) -{ - unsigned long ssize = rsize / 2; - unsigned long start, start2, prime; - unsigned long i; - mpz_t tmp; - - mpz_init (tmp); - -#if 0 - /* initialize sieving array */ - for (ii = 0; ii < (ssize + sizeof (long) - 1) / sizeof (long); ii++) - ((long *) s) [ii] = ~0L; -#else - { - long k; - long *se = (long *) (s + ((ssize + sizeof (long) - 1) & -sizeof (long))); - for (k = -((ssize + sizeof (long) - 1) / sizeof (long)); k < 0; k++) - se[k] = ~0L; - } -#endif - - for (i = 0; i < n_primes; i++) - { - prime = primes[i].prime; - - if (primes[i].rem >= 0) - { - start2 = primes[i].rem; - } - else - { - mpz_set_ui (tmp, prime); - mpz_mul_ui (tmp, tmp, prime); - if (mpz_cmp (fr, tmp) <= 0) - { - mpz_sub (tmp, tmp, fr); - if (mpz_cmp_ui (tmp, 2 * ssize) > 0) - break; /* avoid overflow at next line, also speedup */ - start = mpz_get_ui (tmp); - } - else - { - start = (prime - mpz_tdiv_ui (fr, prime)) % prime; - if (start % 2 != 0) - start += prime; /* adjust if even divisable */ - } - start2 = start / 2; - } - -#if 0 - for (ii = start2; ii < ssize; ii += prime) - s[ii] = 0; - primes[i].rem = ii - ssize; -#else - { - long k; - unsigned char *se = s + ssize; /* point just beyond sieving range */ - for (k = start2 - ssize; k < 0; k += prime) - se[k] = 0; - primes[i].rem = k; - } -#endif - } - mpz_clear (tmp); -} - -/* Find primes in region [fr,fr+rsize), using the previously sieved s[]. */ -void -find_primes (unsigned char *s, mpz_t fr, unsigned long ssize, - mpz_t siev_sqr_lim) -{ - unsigned long j, ij; - mpz_t tmp; - - mpz_init (tmp); - for (j = 0; j < (ssize + sizeof (long) - 1) / sizeof (long); j++) - { - if (((long *) s) [j] != 0) - { - for (ij = 0; ij < sizeof (long); ij++) - { - if (s[j * sizeof (long) + ij] != 0) - { - if (j * sizeof (long) + ij >= ssize) - goto out; - mpz_add_ui (tmp, fr, (j * sizeof (long) + ij) * 2); - if (mpz_cmp (tmp, siev_sqr_lim) < 0 || - mpz_probab_prime_p (tmp, 3)) - report (tmp); - } - } - } - } - out: - mpz_clear (tmp); -} - -/* Generate a lits of primes and store in the global array primes[]. */ -void -make_primelist (unsigned long maxprime) -{ -#if 1 - unsigned char *s; - unsigned long ssize = maxprime / 2; - unsigned long i, ii, j; - - s = malloc (ssize); - memset (s, ~0, ssize); - for (i = 3; ; i += 2) - { - unsigned long isqr = i * i; - if (isqr >= maxprime) - break; - if (s[i * i / 2 - 1] == 0) - continue; /* only sieve with primes */ - for (ii = i * i / 2 - 1; ii < ssize; ii += i) - s[ii] = 0; - } - n_primes = 0; - for (j = 0; j < ssize; j++) - { - if (s[j] != 0) - { - primes[n_primes].prime = j * 2 + 3; - primes[n_primes].rem = -1; - n_primes++; - } - } - /* FIXME: This should not be needed if fencepost errors were fixed... */ - if (primes[n_primes - 1].prime > maxprime) - n_primes--; - free (s); -#else - unsigned long i; - n_primes = 0; - for (i = 3; i <= maxprime; i += 2) - { - if (i < 7 || (i % 3 != 0 && i % 5 != 0 && i % 7 != 0)) - { - primes[n_primes].prime = i; - primes[n_primes].rem = -1; - n_primes++; - } - } -#endif -} diff --git a/src/gmp/demos/qcn.c b/src/gmp/demos/qcn.c deleted file mode 100644 index 2861fcef9..000000000 --- a/src/gmp/demos/qcn.c +++ /dev/null @@ -1,173 +0,0 @@ -/* Use mpz_kronecker_ui() to calculate an estimate for the quadratic - class number h(d), for a given negative fundamental discriminant, using - Dirichlet's analytic formula. - -Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc. - -This file is part of the GNU MP Library. - -This program is free software; you can redistribute it and/or modify it -under the terms of the GNU General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your option) -any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -more details. - -You should have received a copy of the GNU General Public License along with -this program; if not, write to the Free Software Foundation, Inc., 51 Franklin -Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - -/* Usage: qcn [-p limit] ... - - A fundamental discriminant means one of the form D or 4*D with D - square-free. Each argument is checked to see it's congruent to 0 or 1 - mod 4 (as all discriminants must be), and that it's negative, but there's - no check on D being square-free. - - This program is a bit of a toy, there are better methods for calculating - the class number and class group structure. - - Reference: - - Daniel Shanks, "Class Number, A Theory of Factorization, and Genera", - Proc. Symp. Pure Math., vol 20, 1970, pages 415-440. - -*/ - -#include -#include -#include -#include - -#include "gmp.h" - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - - -/* A simple but slow primality test. */ -int -prime_p (unsigned long n) -{ - unsigned long i, limit; - - if (n == 2) - return 1; - if (n < 2 || !(n&1)) - return 0; - - limit = (unsigned long) floor (sqrt ((double) n)); - for (i = 3; i <= limit; i+=2) - if ((n % i) == 0) - return 0; - - return 1; -} - - -/* The formula is as follows, with d < 0. - - w * sqrt(-d) inf p - h(d) = ------------ * product -------- - 2 * pi p=2 p - (d/p) - - - (d/p) is the Kronecker symbol and the product is over primes p. w is 6 - when d=-3, 4 when d=-4, or 2 otherwise. - - Calculating the product up to p=infinity would take a long time, so for - the estimate primes up to 132,000 are used. Shanks found this giving an - accuracy of about 1 part in 1000, in normal cases. */ - -unsigned long p_limit = 132000; - -double -qcn_estimate (mpz_t d) -{ - double h; - unsigned long p; - - /* p=2 */ - h = sqrt (-mpz_get_d (d)) / M_PI - * 2.0 / (2.0 - mpz_kronecker_ui (d, 2)); - - if (mpz_cmp_si (d, -3) == 0) h *= 3; - else if (mpz_cmp_si (d, -4) == 0) h *= 2; - - for (p = 3; p <= p_limit; p += 2) - if (prime_p (p)) - h *= (double) p / (double) (p - mpz_kronecker_ui (d, p)); - - return h; -} - - -void -qcn_str (char *num) -{ - mpz_t z; - - mpz_init_set_str (z, num, 0); - - if (mpz_sgn (z) >= 0) - { - mpz_out_str (stdout, 0, z); - printf (" is not supported (negatives only)\n"); - } - else if (mpz_fdiv_ui (z, 4) != 0 && mpz_fdiv_ui (z, 4) != 1) - { - mpz_out_str (stdout, 0, z); - printf (" is not a discriminant (must == 0 or 1 mod 4)\n"); - } - else - { - printf ("h("); - mpz_out_str (stdout, 0, z); - printf (") approx %.1f\n", qcn_estimate (z)); - } - mpz_clear (z); -} - - -int -main (int argc, char *argv[]) -{ - int i; - int saw_number = 0; - - for (i = 1; i < argc; i++) - { - if (strcmp (argv[i], "-p") == 0) - { - i++; - if (i >= argc) - { - fprintf (stderr, "Missing argument to -p\n"); - exit (1); - } - p_limit = atoi (argv[i]); - } - else - { - qcn_str (argv[i]); - saw_number = 1; - } - } - - if (! saw_number) - { - /* some default output */ - qcn_str ("-85702502803"); /* is 16259 */ - qcn_str ("-328878692999"); /* is 1499699 */ - qcn_str ("-928185925902146563"); /* is 52739552 */ - qcn_str ("-84148631888752647283"); /* is 496652272 */ - return 0; - } - - return 0; -}