From c9977c0132b2a6a3df994dbdf58c27689658f20f Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 18 Jul 2014 06:59:26 -0700 Subject: * Makefile (conftest, conftest2): Link math library so we can test for math functions. * arith.c (log2_init): New static function. (log2, logtwo): New functions. (l2): New static variable. (arith_init): Call log2_init. * configure (lang_flags): Switching _XOPEN_SOURCE from 500 to 600 to reveal log2. Adding test for log2. * eval.c (eval_init): Register log2 intrinsic. * lib.h (logtwo): Declared. * txr.1: Documented log2. --- ChangeLog | 20 ++++++++++++++++++++ Makefile | 4 ++-- arith.c | 28 ++++++++++++++++++++++++++++ configure | 20 +++++++++++++++++++- eval.c | 1 + lib.h | 1 + txr.1 | 6 +++++- 7 files changed, 76 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 767c6151..b8765212 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2014-07-18 Kaz Kylheku + + * Makefile (conftest, conftest2): Link math + library so we can test for math functions. + + * arith.c (log2_init): New static function. + (log2, logtwo): New functions. + (l2): New static variable. + (arith_init): Call log2_init. + + * configure (lang_flags): Switching _XOPEN_SOURCE + from 500 to 600 to reveal log2. + Adding test for log2. + + * eval.c (eval_init): Register log2 intrinsic. + + * lib.h (logtwo): Declared. + + * txr.1: Documented log2. + 2014-07-15 Kaz Kylheku * match.c (v_do, v_require): Set up and tear down environment frame, diff --git a/Makefile b/Makefile index 0f5b4d39..653ab995 100644 --- a/Makefile +++ b/Makefile @@ -218,10 +218,10 @@ config.make config.h: # conftest: conftest.c - $(CC) $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ $^ -lm conftest2: conftest1.c conftest2.c - $(CC) $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ $^ -lm conftest.syms: conftest.o $(NM) -n -t o -P $^ > $@ diff --git a/arith.c b/arith.c index 76d00d1d..14d3d0e2 100644 --- a/arith.c +++ b/arith.c @@ -1444,6 +1444,33 @@ val logten(val num) return flo(log10(c_flo(to_float(lit("log"), num)))); } +#if HAVE_LOG2 + +static void log2_init(void) +{ +} + +#else + +static double l2; + +static void log2_init(void) +{ + l2 = log(2.0); +} + +double log2(double x) +{ + return log(x)/l2; +} + +#endif + +val logtwo(val num) +{ + return flo(log2(c_flo(to_float(lit("log"), num)))); +} + val expo(val num) { return flo(exp(c_flo(to_float(lit("exp"), num)))); @@ -2038,4 +2065,5 @@ void arith_init(void) mp_set_intptr(&NUM_MAX_MP, NUM_MAX); mp_init(&INT_PTR_MAX_MP); mp_set_intptr(&INT_PTR_MAX_MP, INT_PTR_MAX); + log2_init(); } diff --git a/configure b/configure index 16b84cd7..0d36cff6 100755 --- a/configure +++ b/configure @@ -97,7 +97,7 @@ yacc='$(cross)$(tool_prefix)$(yaccname)' yacc_given= nm='$(cross)$(tool_prefix)nm' opt_flags=-O2 -lang_flags='-ansi -D_XOPEN_SOURCE=500' +lang_flags='-ansi -D_XOPEN_SOURCE=600' diag_flags='-Wall -Werror=implicit-function-declaration -Werror=missing-prototypes -Werror=strict-prototypes' debug_flags=-g inline= @@ -1809,6 +1809,24 @@ else printf "no\n" fi +printf "Checking for log2 ... " + +cat > conftest.c < + +int main(void) +{ + double x = log2(42.0); + return 0; +} +! +if conftest ; then + printf "yes\n" + printf "#define HAVE_LOG2 1\n" >> config.h +else + printf "no\n" +fi + # # Dependent variables # diff --git a/eval.c b/eval.c index e8c1c7da..b5ded23a 100644 --- a/eval.c +++ b/eval.c @@ -3585,6 +3585,7 @@ void eval_init(void) reg_fun(intern(lit("atan2"), user_package), func_n2(atang2)); reg_fun(intern(lit("log"), user_package), func_n1(loga)); reg_fun(intern(lit("log10"), user_package), func_n1(logten)); + reg_fun(intern(lit("log2"), user_package), func_n1(logtwo)); reg_fun(intern(lit("exp"), user_package), func_n1(expo)); reg_fun(intern(lit("sqrt"), user_package), func_n1(sqroot)); reg_fun(intern(lit("cum-norm-dist"), user_package), func_n1(cum_norm_dist)); diff --git a/lib.h b/lib.h index 81fea9b8..27b5c544 100644 --- a/lib.h +++ b/lib.h @@ -544,6 +544,7 @@ val atang(val); val atang2(val, val); val loga(val); val logten(val num); +val logtwo(val num); val expo(val); val logand(val, val); val logior(val, val); diff --git a/txr.1 b/txr.1 index 20b977ab..ea2b06ae 100644 --- a/txr.1 +++ b/txr.1 @@ -10871,7 +10871,7 @@ functions. The argument to asin and acos must be in the range -1.0 to 1.0. The atan2 function converts the rectilinear coordinates and to an angle in polar coordinates in the range [0, 2pi). -.SS Functions exp, log, log10 +.SS Functions exp, log, log10 and log2 .TP Syntax: @@ -10879,6 +10879,7 @@ Syntax: (exp ) (log ) (log10 ) + (log2 ) .TP Description: @@ -10892,6 +10893,9 @@ be a positive value. The log10 function calculates the base 10 logarithm of its argument, which must be a positive value. +The log2 function calculates the base 2 logarithm of its argument, which must +be a positive value. + Integer arguments are converted to floats. .SS Functions expt, sqrt, isqrt -- cgit v1.2.3