From 294a0ced03ea014d839f887d4d39ad456b5fb7d6 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 17 Mar 2013 21:38:31 +0100 Subject: [PATCH 1/4] Added missing function log2 ... generating the base-2 logarithm of a number much faster than with log(x, 2) --- ext/standard/basic_functions.c | 5 ++ ext/standard/math.c | 13 ++++ ext/standard/php_math.h | 1 + ext/standard/tests/math/log2_basic.phpt | 52 ++++++++++++++++ .../tests/math/log2_basiclong_64bit.phpt | 60 +++++++++++++++++++ ext/standard/tests/math/log2_error.phpt | 31 ++++++++++ ext/standard/tests/math/log2_variation.phpt | 56 +++++++++++++++++ 7 files changed, 218 insertions(+) create mode 100644 ext/standard/tests/math/log2_basic.phpt create mode 100644 ext/standard/tests/math/log2_basiclong_64bit.phpt create mode 100644 ext/standard/tests/math/log2_error.phpt create mode 100644 ext/standard/tests/math/log2_variation.phpt diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index a40fdd23979f1..98a373903e336 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1729,6 +1729,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_log, 0, 0, 1) ZEND_ARG_INFO(0, base) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_log2, 0) + ZEND_ARG_INFO(0, number) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO(arginfo_log10, 0) ZEND_ARG_INFO(0, number) ZEND_END_ARG_INFO() @@ -2916,6 +2920,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(pow, arginfo_pow) PHP_FE(exp, arginfo_exp) PHP_FE(log, arginfo_log) + PHP_FE(log2, arginfo_log2) PHP_FE(log10, arginfo_log10) PHP_FE(sqrt, arginfo_sqrt) PHP_FE(hypot, arginfo_hypot) diff --git a/ext/standard/math.c b/ext/standard/math.c index be2d655263f13..1077e58d871e4 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -716,6 +716,19 @@ PHP_FUNCTION(log) } /* }}} */ +/* {{{ proto float log2(float number) + Returns the base-2 logarithm of the number */ +PHP_FUNCTION(log2) +{ + double num; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) { + return; + } + RETURN_DOUBLE(log2(num)); +} +/* }}} */ + /* {{{ proto float log10(float number) Returns the base-10 logarithm of the number */ PHP_FUNCTION(log10) diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 116acd9df5e8b..bcc49693f751d 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -39,6 +39,7 @@ PHP_FUNCTION(atan2); PHP_FUNCTION(pi); PHP_FUNCTION(exp); PHP_FUNCTION(log); +PHP_FUNCTION(log2); PHP_FUNCTION(log10); PHP_FUNCTION(is_finite); PHP_FUNCTION(is_infinite); diff --git a/ext/standard/tests/math/log2_basic.phpt b/ext/standard/tests/math/log2_basic.phpt new file mode 100644 index 0000000000000..f2acf272beb2a --- /dev/null +++ b/ext/standard/tests/math/log2_basic.phpt @@ -0,0 +1,52 @@ +--TEST-- +Test return type and value for expected input log2() +--INI-- +precision = 14 +--FILE-- + +--EXPECTF-- +log2 1 = float(%f) +Pass +log2 10 = float(%f) +Pass +log2 100 = float(%f) +Pass diff --git a/ext/standard/tests/math/log2_basiclong_64bit.phpt b/ext/standard/tests/math/log2_basiclong_64bit.phpt new file mode 100644 index 0000000000000..6d1f716b3a076 --- /dev/null +++ b/ext/standard/tests/math/log2_basiclong_64bit.phpt @@ -0,0 +1,60 @@ +--TEST-- +Test log2 function : 64bit long tests +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECT-- +--- testing: 9223372036854775807 --- +float(18.964889726831) +--- testing: -9223372036854775808 --- +float(NAN) +--- testing: 2147483647 --- +float(9.3319298653812) +--- testing: -2147483648 --- +float(NAN) +--- testing: 9223372034707292160 --- +float(18.96488972673) +--- testing: -9223372034707292160 --- +float(NAN) +--- testing: 2147483648 --- +float(9.3319298655834) +--- testing: -2147483649 --- +float(NAN) +--- testing: 4294967294 --- +float(9.6329598610452) +--- testing: 4294967295 --- +float(9.6329598611463) +--- testing: 4294967293 --- +float(9.632959860944) +--- testing: 9223372036854775806 --- +float(18.964889726831) +--- testing: 9.2233720368548E+18 --- +float(18.964889726831) +--- testing: -9223372036854775807 --- +float(NAN) +--- testing: -9.2233720368548E+18 --- +float(NAN) +===DONE=== diff --git a/ext/standard/tests/math/log2_error.phpt b/ext/standard/tests/math/log2_error.phpt new file mode 100644 index 0000000000000..6ca8f96f37d11 --- /dev/null +++ b/ext/standard/tests/math/log2_error.phpt @@ -0,0 +1,31 @@ +--TEST-- +Test wrong number of arguments for log2() +--INI-- +precision = 14 +--FILE-- + +--EXPECTF-- +Too many arguments + +Warning: log2() expects exactly 1 parameter, 2 given in %s on line %d +NULL + +Too few arguments + +Warning: log2() expects exactly 1 parameter, 0 given in %s on line %d +NULL diff --git a/ext/standard/tests/math/log2_variation.phpt b/ext/standard/tests/math/log2_variation.phpt new file mode 100644 index 0000000000000..32ae7f5f2ff7d --- /dev/null +++ b/ext/standard/tests/math/log2_variation.phpt @@ -0,0 +1,56 @@ +--TEST-- +Test variations in usage of log2() +--INI-- +precision = 10 +--FILE-- + +--EXPECTF-- +float(4.523561956) +float(NAN) +float(4.551516018) +float(NAN) +float(4.523561956) +float(4.523561956) +float(4.523561956) +float(4.551516018) +float(4.551516018) + +Warning: log2() expects parameter 1 to be double, string given in /home/mabe/workspace/php-src/ext/standard/tests/math/log2_variation.php on line 27 +NULL +float(9.965784285) + +Notice: A non well formed numeric value encountered in /home/mabe/workspace/php-src/ext/standard/tests/math/log2_variation.php on line 27 +float(9.965784285) +float(-INF) +float(0) +float(-INF) From 90911421b3e566a20b080c544d613bdd7af86b7e Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Sun, 17 Mar 2013 22:12:49 +0100 Subject: [PATCH 2/4] fixed phpt of log2 variation --- ext/standard/tests/math/log2_variation.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/standard/tests/math/log2_variation.phpt b/ext/standard/tests/math/log2_variation.phpt index 32ae7f5f2ff7d..aebde8fa7bf4f 100644 --- a/ext/standard/tests/math/log2_variation.phpt +++ b/ext/standard/tests/math/log2_variation.phpt @@ -45,11 +45,11 @@ float(4.523561956) float(4.551516018) float(4.551516018) -Warning: log2() expects parameter 1 to be double, string given in /home/mabe/workspace/php-src/ext/standard/tests/math/log2_variation.php on line 27 +Warning: log2() expects parameter 1 to be double, string given in %s on line %d NULL float(9.965784285) -Notice: A non well formed numeric value encountered in /home/mabe/workspace/php-src/ext/standard/tests/math/log2_variation.php on line 27 +Notice: A non well formed numeric value encountered in %s on line %d float(9.965784285) float(-INF) float(0) From d13fcadf99972e08020d3d5b275c85f29d4cb22d Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Thu, 21 Mar 2013 17:35:12 +0100 Subject: [PATCH 3/4] fixed unit tests for log2 on 64bit env --- .../tests/math/log2_basiclong_64bit.phpt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ext/standard/tests/math/log2_basiclong_64bit.phpt b/ext/standard/tests/math/log2_basiclong_64bit.phpt index 6d1f716b3a076..dbcdc4cd71994 100644 --- a/ext/standard/tests/math/log2_basiclong_64bit.phpt +++ b/ext/standard/tests/math/log2_basiclong_64bit.phpt @@ -28,31 +28,31 @@ foreach ($longVals as $longVal) { ===DONE=== --EXPECT-- --- testing: 9223372036854775807 --- -float(18.964889726831) +float(63) --- testing: -9223372036854775808 --- float(NAN) --- testing: 2147483647 --- -float(9.3319298653812) +float(30.999999999328) --- testing: -2147483648 --- float(NAN) --- testing: 9223372034707292160 --- -float(18.96488972673) +float(62.999999999664) --- testing: -9223372034707292160 --- float(NAN) --- testing: 2147483648 --- -float(9.3319298655834) +float(31) --- testing: -2147483649 --- float(NAN) --- testing: 4294967294 --- -float(9.6329598610452) +float(31.999999999328) --- testing: 4294967295 --- -float(9.6329598611463) +float(31.999999999664) --- testing: 4294967293 --- -float(9.632959860944) +float(31.999999998992) --- testing: 9223372036854775806 --- -float(18.964889726831) +float(53) --- testing: 9.2233720368548E+18 --- -float(18.964889726831) +float(63) --- testing: -9223372036854775807 --- float(NAN) --- testing: -9.2233720368548E+18 --- From 5cf0709dd43835fd3e483a474b1f80795fda52ca Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Fri, 22 Mar 2013 11:58:31 +0100 Subject: [PATCH 4/4] fixed unit tests of log2 variations on 64bit env --- ext/standard/tests/math/log2_basiclong_64bit.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/tests/math/log2_basiclong_64bit.phpt b/ext/standard/tests/math/log2_basiclong_64bit.phpt index dbcdc4cd71994..11f82f8add031 100644 --- a/ext/standard/tests/math/log2_basiclong_64bit.phpt +++ b/ext/standard/tests/math/log2_basiclong_64bit.phpt @@ -50,7 +50,7 @@ float(31.999999999664) --- testing: 4294967293 --- float(31.999999998992) --- testing: 9223372036854775806 --- -float(53) +float(63) --- testing: 9.2233720368548E+18 --- float(63) --- testing: -9223372036854775807 ---