diff options
Diffstat (limited to 'extension')
-rw-r--r-- | extension/ChangeLog | 30 | ||||
-rw-r--r-- | extension/Makefile.am | 5 | ||||
-rw-r--r-- | extension/Makefile.in | 43 | ||||
-rw-r--r-- | extension/aclocal.m4 | 1 | ||||
-rw-r--r-- | extension/configh.in | 6 | ||||
-rwxr-xr-x | extension/configure | 88 | ||||
-rw-r--r-- | extension/configure.ac | 13 | ||||
-rw-r--r-- | extension/intdiv.c | 215 |
8 files changed, 383 insertions, 18 deletions
diff --git a/extension/ChangeLog b/extension/ChangeLog index d10dc766..7307a11a 100644 --- a/extension/ChangeLog +++ b/extension/ChangeLog @@ -1,3 +1,23 @@ +2017-04-16 Arnold D. Robbins <arnold@skeeve.com> + + * intdiv.c (func_table): Function is now named intdiv. + +2017-04-14 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * intdiv.c (do_intdiv): On division by zero, return -1 and issue a + warning instead of throwing a fatal error. + +2017-04-13 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * intdiv.c (do_intdiv): On a division by zero fatal error, there's + no need to clear the numerator and denominator and add a fake return. + +2017-04-13 Arnold D. Robbins <arnold@skeeve.com> + + * configure.ac: Alphabetize function list in AC_CHECK_FUNCS. + * intdiv.c: Add descriptive comments to some functions. + (do_intdiv): Make division by zero fatal in MPFR case. + 2017-04-03 Arnold D. Robbins <arnold@skeeve.com> * inplace.c (inplace_end): Correct the function name in the @@ -64,6 +84,16 @@ system headers assume that if this is defined, it must have a numeric value. +2017-01-06 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * intdiv.c: New extension to demonstrate how to implement intdiv + using the new extended-precision math API. + * Makefile.am (pkgextension_LTLIBRARIES): Add intdiv.la. + (intdiv_la_SOURCES, intdiv_la_LDFLAGS, intdiv_la_LIBADD): Add support + for new intdiv library. + * configure.ac (AC_CHECK_FUNCS): Check for fmod needed by intdiv. + (GNUPG_CHECK_MPFR): Add check for MPFR support. + 2016-12-22 Arnold D. Robbins <arnold@skeeve.com> * testext.c (valrep2str): Update for new API types. diff --git a/extension/Makefile.am b/extension/Makefile.am index 6ea16f5d..54975b8e 100644 --- a/extension/Makefile.am +++ b/extension/Makefile.am @@ -39,6 +39,7 @@ pkgextension_LTLIBRARIES = \ fnmatch.la \ fork.la \ inplace.la \ + intdiv.la \ ordchr.la \ readdir.la \ readfile.la \ @@ -72,6 +73,10 @@ inplace_la_SOURCES = inplace.c inplace_la_LDFLAGS = $(MY_MODULE_FLAGS) inplace_la_LIBADD = $(MY_LIBS) +intdiv_la_SOURCES = intdiv.c +intdiv_la_LDFLAGS = $(MY_MODULE_FLAGS) +intdiv_la_LIBADD = $(MY_LIBS) + ordchr_la_SOURCES = ordchr.c ordchr_la_LDFLAGS = $(MY_MODULE_FLAGS) ordchr_la_LIBADD = $(MY_LIBS) diff --git a/extension/Makefile.in b/extension/Makefile.in index c0e2676b..000c3b78 100644 --- a/extension/Makefile.in +++ b/extension/Makefile.in @@ -114,10 +114,10 @@ host_triplet = @host@ subdir = . ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/../m4/arch.m4 \ - $(top_srcdir)/m4/dirfd.m4 $(top_srcdir)/m4/libtool.m4 \ - $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ - $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ - $(top_srcdir)/configure.ac + $(top_srcdir)/../m4/mpfr.m4 $(top_srcdir)/m4/dirfd.m4 \ + $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \ + $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ + $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) DIST_COMMON = $(srcdir)/Makefile.am $(top_srcdir)/configure \ @@ -187,6 +187,12 @@ inplace_la_OBJECTS = $(am_inplace_la_OBJECTS) inplace_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(inplace_la_LDFLAGS) $(LDFLAGS) -o $@ +intdiv_la_DEPENDENCIES = $(am__DEPENDENCIES_1) +am_intdiv_la_OBJECTS = intdiv.lo +intdiv_la_OBJECTS = $(am_intdiv_la_OBJECTS) +intdiv_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(intdiv_la_LDFLAGS) $(LDFLAGS) -o $@ ordchr_la_DEPENDENCIES = $(am__DEPENDENCIES_1) am_ordchr_la_OBJECTS = ordchr.lo ordchr_la_OBJECTS = $(am_ordchr_la_OBJECTS) @@ -277,17 +283,17 @@ am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = SOURCES = $(filefuncs_la_SOURCES) $(fnmatch_la_SOURCES) \ - $(fork_la_SOURCES) $(inplace_la_SOURCES) $(ordchr_la_SOURCES) \ - $(readdir_la_SOURCES) $(readdir_test_la_SOURCES) \ - $(readfile_la_SOURCES) $(revoutput_la_SOURCES) \ - $(revtwoway_la_SOURCES) $(rwarray_la_SOURCES) \ - $(testext_la_SOURCES) $(time_la_SOURCES) + $(fork_la_SOURCES) $(inplace_la_SOURCES) $(intdiv_la_SOURCES) \ + $(ordchr_la_SOURCES) $(readdir_la_SOURCES) \ + $(readdir_test_la_SOURCES) $(readfile_la_SOURCES) \ + $(revoutput_la_SOURCES) $(revtwoway_la_SOURCES) \ + $(rwarray_la_SOURCES) $(testext_la_SOURCES) $(time_la_SOURCES) DIST_SOURCES = $(filefuncs_la_SOURCES) $(fnmatch_la_SOURCES) \ - $(fork_la_SOURCES) $(inplace_la_SOURCES) $(ordchr_la_SOURCES) \ - $(readdir_la_SOURCES) $(readdir_test_la_SOURCES) \ - $(readfile_la_SOURCES) $(revoutput_la_SOURCES) \ - $(revtwoway_la_SOURCES) $(rwarray_la_SOURCES) \ - $(testext_la_SOURCES) $(time_la_SOURCES) + $(fork_la_SOURCES) $(inplace_la_SOURCES) $(intdiv_la_SOURCES) \ + $(ordchr_la_SOURCES) $(readdir_la_SOURCES) \ + $(readdir_test_la_SOURCES) $(readfile_la_SOURCES) \ + $(revoutput_la_SOURCES) $(revtwoway_la_SOURCES) \ + $(rwarray_la_SOURCES) $(testext_la_SOURCES) $(time_la_SOURCES) RECURSIVE_TARGETS = all-recursive check-recursive cscopelist-recursive \ ctags-recursive dvi-recursive html-recursive info-recursive \ install-data-recursive install-dvi-recursive \ @@ -422,6 +428,7 @@ INSTALL_SCRIPT = @INSTALL_SCRIPT@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ LD = @LD@ LDFLAGS = @LDFLAGS@ +LIBMPFR = @LIBMPFR@ LIBOBJS = @LIBOBJS@ LIBS = @LIBS@ LIBTOOL = @LIBTOOL@ @@ -520,6 +527,7 @@ pkgextension_LTLIBRARIES = \ fnmatch.la \ fork.la \ inplace.la \ + intdiv.la \ ordchr.la \ readdir.la \ readfile.la \ @@ -549,6 +557,9 @@ fork_la_LIBADD = $(MY_LIBS) inplace_la_SOURCES = inplace.c inplace_la_LDFLAGS = $(MY_MODULE_FLAGS) inplace_la_LIBADD = $(MY_LIBS) +intdiv_la_SOURCES = intdiv.c +intdiv_la_LDFLAGS = $(MY_MODULE_FLAGS) +intdiv_la_LIBADD = $(MY_LIBS) ordchr_la_SOURCES = ordchr.c ordchr_la_LDFLAGS = $(MY_MODULE_FLAGS) ordchr_la_LIBADD = $(MY_LIBS) @@ -713,6 +724,9 @@ fork.la: $(fork_la_OBJECTS) $(fork_la_DEPENDENCIES) $(EXTRA_fork_la_DEPENDENCIES inplace.la: $(inplace_la_OBJECTS) $(inplace_la_DEPENDENCIES) $(EXTRA_inplace_la_DEPENDENCIES) $(AM_V_CCLD)$(inplace_la_LINK) -rpath $(pkgextensiondir) $(inplace_la_OBJECTS) $(inplace_la_LIBADD) $(LIBS) +intdiv.la: $(intdiv_la_OBJECTS) $(intdiv_la_DEPENDENCIES) $(EXTRA_intdiv_la_DEPENDENCIES) + $(AM_V_CCLD)$(intdiv_la_LINK) -rpath $(pkgextensiondir) $(intdiv_la_OBJECTS) $(intdiv_la_LIBADD) $(LIBS) + ordchr.la: $(ordchr_la_OBJECTS) $(ordchr_la_DEPENDENCIES) $(EXTRA_ordchr_la_DEPENDENCIES) $(AM_V_CCLD)$(ordchr_la_LINK) -rpath $(pkgextensiondir) $(ordchr_la_OBJECTS) $(ordchr_la_LIBADD) $(LIBS) @@ -751,6 +765,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fork.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gawkfts.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/inplace.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/intdiv.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ordchr.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/readdir.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/readdir_test.Plo@am__quote@ diff --git a/extension/aclocal.m4 b/extension/aclocal.m4 index 5665d48e..1e7a343c 100644 --- a/extension/aclocal.m4 +++ b/extension/aclocal.m4 @@ -1211,6 +1211,7 @@ AC_SUBST([am__untar]) ]) # _AM_PROG_TAR m4_include([../m4/arch.m4]) +m4_include([../m4/mpfr.m4]) m4_include([m4/dirfd.m4]) m4_include([m4/libtool.m4]) m4_include([m4/ltoptions.m4]) diff --git a/extension/configh.in b/extension/configh.in index d3a226a5..9d3d9919 100644 --- a/extension/configh.in +++ b/extension/configh.in @@ -27,6 +27,9 @@ /* Define to 1 if you have the `fdopendir' function. */ #undef HAVE_FDOPENDIR +/* Define to 1 if you have the `fmod' function. */ +#undef HAVE_FMOD + /* Define to 1 if you have the `fnmatch' function. */ #undef HAVE_FNMATCH @@ -51,6 +54,9 @@ /* Define to 1 if you have the <memory.h> header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have fully functional mpfr and gmp libraries. */ +#undef HAVE_MPFR + /* Define to 1 if you have the `nanosleep' function. */ #undef HAVE_NANOSLEEP diff --git a/extension/configure b/extension/configure index 40601550..7ae66543 100755 --- a/extension/configure +++ b/extension/configure @@ -635,6 +635,7 @@ ac_subst_vars='am__EXEEXT_FALSE am__EXEEXT_TRUE LTLIBOBJS LIBOBJS +LIBMPFR pkgextensiondir LT_SYS_LIBRARY_PATH OTOOL64 @@ -763,6 +764,7 @@ with_aix_soname with_gnu_ld with_sysroot enable_libtool_lock +with_mpfr ' ac_precious_vars='build_alias host_alias @@ -1416,6 +1418,7 @@ Optional Packages: --with-gnu-ld assume the C compiler uses GNU ld [default=no] --with-sysroot[=DIR] Search for dependent libraries within DIR (or the compiler's sysroot if not specified). + --with-mpfr=DIR look for the mpfr and gmp libraries in DIR Some influential environment variables: CC C compiler command @@ -12859,8 +12862,89 @@ $as_echo "#define TIME_WITH_SYS_TIME 1" >>confdefs.h fi -for ac_func in fdopendir fnmatch gettimeofday \ - getdtablesize nanosleep select statvfs GetSystemTimeAsFileTime +case `uname -m` in +*'Power Macintosh'*) + : ;; +*) + + +# Check whether --with-mpfr was given. +if test "${with_mpfr+set}" = set; then : + withval=$with_mpfr; _do_mpfr=$withval +else + _do_mpfr=yes +fi + + + if test "$_do_mpfr" != "no" ; then + if test -d "$withval" ; then + CPPFLAGS="${CPPFLAGS} -I$withval/include" + LDFLAGS="${LDFLAGS} -L$withval/lib" + fi + + _mpfr_save_libs=$LIBS + _combo="-lmpfr -lgmp" + LIBS="$LIBS $_combo" + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether mpfr via \"$_combo\" is present and usable" >&5 +$as_echo_n "checking whether mpfr via \"$_combo\" is present and usable... " >&6; } + + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + + +#include <stdio.h> +#include <mpfr.h> +#include <gmp.h> + +int +main () +{ + +mpfr_t p; +mpz_t z; +mpfr_init(p); +mpz_init(z); +mpfr_printf("%Rf%Zd", p, z); +mpfr_clear(p); +mpz_clear(z); + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + _found_mpfr=yes +else + _found_mpfr=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext + + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $_found_mpfr" >&5 +$as_echo "$_found_mpfr" >&6; } + + LIBS=$_mpfr_save_libs + + if test $_found_mpfr = yes ; then + +$as_echo "#define HAVE_MPFR 1" >>confdefs.h + + LIBMPFR=$_combo + + break + fi + + unset _mpfr_save_libs + unset _combo + unset _found_mpfr + fi + + ;; +esac + +for ac_func in fdopendir fmod fnmatch getdtablesize \ + gettimeofday nanosleep select statvfs GetSystemTimeAsFileTime do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/extension/configure.ac b/extension/configure.ac index b5b27d03..bde6e3d3 100644 --- a/extension/configure.ac +++ b/extension/configure.ac @@ -69,8 +69,17 @@ AC_HEADER_DIRENT AC_HEADER_MAJOR AC_HEADER_TIME -AC_CHECK_FUNCS(fdopendir fnmatch gettimeofday \ - getdtablesize nanosleep select statvfs GetSystemTimeAsFileTime) +dnl check for mpfr support +case `uname -m` in +*'Power Macintosh'*) + : ;; +*) + GNUPG_CHECK_MPFR + ;; +esac + +AC_CHECK_FUNCS(fdopendir fmod fnmatch getdtablesize \ + gettimeofday nanosleep select statvfs GetSystemTimeAsFileTime) GAWK_FUNC_DIRFD GAWK_PREREQ_DIRFD diff --git a/extension/intdiv.c b/extension/intdiv.c new file mode 100644 index 00000000..77b4290b --- /dev/null +++ b/extension/intdiv.c @@ -0,0 +1,215 @@ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <assert.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <math.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#include "gawkapi.h" + +#ifdef HAVE_MPFR +#include <gmp.h> +#include <mpfr.h> +#endif + +#include "gettext.h" +#define _(msgid) gettext(msgid) +#define N_(msgid) msgid + +static const gawk_api_t *api; /* for convenience macros to work */ +static awk_ext_id_t *ext_id; +static const char *ext_version = "intdiv extension: version 1.0"; + +int plugin_is_GPL_compatible; + +/* double_to_int --- get the integer part of a double */ + +static double +double_to_int(double d) +{ + if (d >= 0) + d = floor(d); + else + d = ceil(d); + return d; +} + +/* array_set_number --- set an array element to a numeric value */ + +static void +array_set_number(awk_array_t array, const char *sub, size_t sublen, double num) +{ + awk_value_t index, tmp; + + set_array_element(array, make_const_string(sub, sublen, & index), make_number(num, & tmp)); +} + +#ifdef HAVE_MPFR + +/* mpz_conv --- convert an awk_value_t to an MPZ value */ + +static mpz_ptr +mpz_conv(const awk_value_t *arg, mpz_ptr tmp) +{ + switch (arg->num_type) { + case AWK_NUMBER_TYPE_MPZ: + return arg->num_ptr; + case AWK_NUMBER_TYPE_MPFR: + if (! mpfr_number_p(arg->num_ptr)) + return NULL; + mpz_init(tmp); + mpfr_get_z(tmp, arg->num_ptr, MPFR_RNDZ); + return tmp; + case AWK_NUMBER_TYPE_DOUBLE: /* can this happen? */ + mpz_init(tmp); + mpz_set_d(tmp, double_to_int(arg->num_value)); + return tmp; + default: /* should never happen */ + fatal(ext_id, _("intdiv: invalid numeric type `%d'"), arg->num_type); + return NULL; + } +} + +/* array_set_mpz --- set an array element to an MPZ value */ + +static void +array_set_mpz(awk_array_t array, const char *sub, size_t sublen, mpz_ptr num) +{ + awk_value_t index, tmp; + + set_array_element(array, make_const_string(sub, sublen, & index), make_number_mpz(num, & tmp)); +} + +#endif + +/* do_intdiv --- do integer division, return quotient and remainder in dest array */ + +/* + * We define the semantics as: + * numerator = int(numerator) + * denominator = int(denonmator) + * quotient = int(numerator / denomator) + * remainder = int(numerator % denomator) + */ + +static awk_value_t * +do_intdiv(int nargs, awk_value_t *result, struct awk_ext_func *unused) +{ + awk_value_t nv, dv, array_param; + awk_array_t array; + + if (! get_argument(0, AWK_NUMBER, & nv)) { + warning(ext_id, _("intdiv: first argument must be numeric")); + return make_number(-1, result); + } + if (! get_argument(1, AWK_NUMBER, & dv)) { + warning(ext_id, _("intdiv: second argument must be numeric")); + return make_number(-1, result); + } + if (! get_argument(2, AWK_ARRAY, & array_param)) { + warning(ext_id, _("intdiv: third argument must be an array")); + return make_number(-1, result); + } + array = array_param.array_cookie; + clear_array(array); + +#ifdef HAVE_MPFR + if (nv.num_type == AWK_NUMBER_TYPE_DOUBLE && dv.num_type == AWK_NUMBER_TYPE_DOUBLE) { +#endif + /* regular precision */ + double num, denom, quotient, remainder; + + num = double_to_int(nv.num_value); + denom = double_to_int(dv.num_value); + + if (denom == 0.0) { + warning(ext_id, _("intdiv: division by zero attempted")); + return make_number(-1, result); + } + + quotient = double_to_int(num / denom); +#ifdef HAVE_FMOD + remainder = fmod(num, denom); +#else /* ! HAVE_FMOD */ + (void) modf(num / denom, & remainder); + remainder = num - remainder * denom; +#endif /* ! HAVE_FMOD */ + remainder = double_to_int(remainder); + + array_set_number(array, "quotient", 8, quotient); + array_set_number(array, "remainder", 9, remainder); +#ifdef HAVE_MPFR + } else { + /* extended precision */ + mpz_ptr numer, denom; + mpz_t numer_tmp, denom_tmp; + mpz_ptr quotient, remainder; + + /* convert numerator and denominator to integer */ + if (!(numer = mpz_conv(&nv, numer_tmp))) { + warning(ext_id, _("intdiv: numerator is not finite")); + return make_number(-1, result); + } + if (!(denom = mpz_conv(&dv, denom_tmp))) { + warning(ext_id, _("intdiv: denominator is not finite")); + if (numer == numer_tmp) + mpz_clear(numer); + return make_number(-1, result); + } + if (mpz_sgn(denom) == 0) { + warning(ext_id, _("intdiv: division by zero attempted")); + if (numer == numer_tmp) + mpz_clear(numer); + if (denom == denom_tmp) + mpz_clear(denom); + return make_number(-1, result); + } + + /* ask gawk to allocate return values for us */ + quotient = get_mpz_ptr(); + remainder = get_mpz_ptr(); + + /* do the division */ + mpz_tdiv_qr(quotient, remainder, numer, denom); + + array_set_mpz(array, "quotient", 8, quotient); + array_set_mpz(array, "remainder", 9, remainder); + + /* release temporary variables */ + if (numer == numer_tmp) + mpz_clear(numer); + if (denom == denom_tmp) + mpz_clear(denom); + } +#endif + + return make_number(0, result); +} + +static awk_ext_func_t func_table[] = { + { "intdiv", do_intdiv, 3, 3, awk_false, NULL }, +}; + +/* init_intdiv --- initialization routine */ + +static awk_bool_t +init_intdiv(void) +{ +#ifdef HAVE_MPFR + check_mpfr_version(intdiv) +#endif + return awk_true; +} + +static awk_bool_t (*init_func)(void) = init_intdiv; + +/* define the dl_load function using the boilerplate macro */ + +dl_load_func(func_table, intdiv, "") |