From 5508f29ef95fedfa10472ba52e57bed66870a8de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:24:27 +0200 Subject: [PATCH 1/9] Use pow() directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is anyway what will happen inside Util::pow for negative parameter. Signed-off-by: Michal Čihař --- libraries/advisor.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/advisor.lib.php b/libraries/advisor.lib.php index cacd296309..6f3e0b0241 100644 --- a/libraries/advisor.lib.php +++ b/libraries/advisor.lib.php @@ -32,7 +32,7 @@ function ADVISOR_bytime($num, $precision) $num = round($num, $precision); if ($num == 0) { - $num = '<' . PMA\libraries\Util::pow(10, -$precision); + $num = '<' . pow(10, -$precision); } return "$num $per"; From e8ddbd30ebad99d50e9bf43e1a3efe118060faac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:28:35 +0200 Subject: [PATCH 2/9] Do not use Util::pow with sqrt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using bigger precision and passing it to standard precision makes no sense. Signed-off-by: Michal Čihař --- libraries/gis/GISPolygon.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libraries/gis/GISPolygon.php b/libraries/gis/GISPolygon.php index bc03d2ab5b..cfccae8da9 100644 --- a/libraries/gis/GISPolygon.php +++ b/libraries/gis/GISPolygon.php @@ -538,10 +538,7 @@ class GISPolygon extends GISGeometry // Always keep $epsilon < 1 to go with the reduction logic down here $epsilon = 0.1; - $denominator = sqrt( - Util::pow(($y1 - $y0), 2) - + Util::pow(($x0 - $x1), 2) - ); + $denominator = sqrt(pow(($y1 - $y0), 2) + pow(($x0 - $x1), 2)); $pointA = array(); $pointB = array(); From b1f2146b7e87c21e98ce09203a2257c2674573a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:30:12 +0200 Subject: [PATCH 3/9] Avoid using Util::pow when we do not need big precision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/gis/GISPolygon.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/gis/GISPolygon.php b/libraries/gis/GISPolygon.php index cfccae8da9..e77c6a1475 100644 --- a/libraries/gis/GISPolygon.php +++ b/libraries/gis/GISPolygon.php @@ -563,7 +563,7 @@ class GISPolygon extends GISGeometry //If both are outside the polygon reduce the epsilon and //recalculate the points(reduce exponentially for faster convergence) - $epsilon = Util::pow($epsilon, 2); + $epsilon = pow($epsilon, 2); if ($epsilon == 0) { return false; } From c4d0f1d35b2bc4e1a994c2158709d1beccfe0f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:30:53 +0200 Subject: [PATCH 4/9] Using built in pow is certainly good enough for 32 bit numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/ip_allow_deny.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ip_allow_deny.lib.php b/libraries/ip_allow_deny.lib.php index 6d4b9937bb..1fb0129ff9 100644 --- a/libraries/ip_allow_deny.lib.php +++ b/libraries/ip_allow_deny.lib.php @@ -72,7 +72,7 @@ function PMA_ipv4MaskTest($testRange, $ipToTest) for ($i = 0; $i < 31; $i++) { if ($i < $regs[5] - 1) { - $maskl = $maskl + PMA\libraries\Util::pow(2, (30 - $i)); + $maskl = $maskl + pow(2, (30 - $i)); } // end if } // end for From 1997cbc77b5f0303148dfd10e2e2fd4c9809cd64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:31:29 +0200 Subject: [PATCH 5/9] Using built in pow is good enough for up to exponent up to 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/controllers/server/ServerVariablesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/controllers/server/ServerVariablesController.php b/libraries/controllers/server/ServerVariablesController.php index bb38b27c14..917983e4eb 100644 --- a/libraries/controllers/server/ServerVariablesController.php +++ b/libraries/controllers/server/ServerVariablesController.php @@ -182,7 +182,7 @@ class ServerVariablesController extends Controller 'gb' => 3, 'gib' => 3 ); - $value = floatval($matches[1]) * Util::pow( + $value = floatval($matches[1]) * pow( 1024, $exp[mb_strtolower($matches[3])] ); From 4d9febb8559bc2d1c721d1d822b1bed7924a2819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:32:03 +0200 Subject: [PATCH 6/9] Use built in pow for column calculation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We're certainly not able to process more fields than integer can store. Signed-off-by: Michal Čihař --- libraries/import.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/import.lib.php b/libraries/import.lib.php index 49caa1d9cb..4184a69ad3 100644 --- a/libraries/import.lib.php +++ b/libraries/import.lib.php @@ -476,7 +476,7 @@ function PMA_getColumnNumberFromName($name) // base26 to base10 conversion : multiply each number // with corresponding value of the position, in this case // $i=0 : 1; $i=1 : 26; $i=2 : 676; ... - $column_number += $number * PMA\libraries\Util::pow(26, $i); + $column_number += $number * pow(26, $i); } return $column_number; } From 41c485557543b8233406b75d6c2227f2f1ac2c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 09:33:15 +0200 Subject: [PATCH 7/9] Remove no longer used Util::pow and tests for it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/Util.php | 70 ------------- test/libraries/common/PMA_pow_test.php | 133 ------------------------- 2 files changed, 203 deletions(-) delete mode 100644 test/libraries/common/PMA_pow_test.php diff --git a/libraries/Util.php b/libraries/Util.php index 85dfae508f..a40a09549d 100644 --- a/libraries/Util.php +++ b/libraries/Util.php @@ -29,76 +29,6 @@ if (! defined('PHPMYADMIN')) { */ class Util { - - /** - * Detects which function to use for pow. - * - * @return string Function name. - */ - public static function detectPow() - { - if (function_exists('bcpow')) { - // BCMath Arbitrary Precision Mathematics Function - return 'bcpow'; - } elseif (function_exists('gmp_pow')) { - // GMP Function - return 'gmp_pow'; - } else { - // PHP function - return 'pow'; - } - } - - /** - * Exponential expression / raise number into power - * - * @param string $base base to raise - * @param string $exp exponent to use - * @param string $use_function pow function to use, or false for auto-detect - * - * @return mixed string or float - */ - public static function pow($base, $exp, $use_function = '') - { - static $pow_function = null; - - if ($pow_function == null) { - $pow_function = self::detectPow(); - } - - if (! $use_function) { - if ($exp < 0) { - $use_function = 'pow'; - } else { - $use_function = $pow_function; - } - } - - if (($exp < 0) && ($use_function != 'pow')) { - return false; - } - - switch ($use_function) { - case 'bcpow' : - // bcscale() needed for testing pow() with base values < 1 - bcscale(10); - $pow = bcpow($base, $exp); - break; - case 'gmp_pow' : - $pow = gmp_strval(gmp_pow($base, $exp)); - break; - case 'pow' : - $base = $base; - $exp = (int) $exp; - $pow = pow($base, $exp); - break; - default: - $pow = $use_function($base, $exp); - } - - return $pow; - } - /** * Checks whether configuration value tells to show icons. * diff --git a/test/libraries/common/PMA_pow_test.php b/test/libraries/common/PMA_pow_test.php deleted file mode 100644 index 2cfe27d41e..0000000000 --- a/test/libraries/common/PMA_pow_test.php +++ /dev/null @@ -1,133 +0,0 @@ -assertEquals( - '1267650600228229401496703205376', - PMA\libraries\Util::pow(2, 100) - ); - } - - /** - * Test forpow - * - * @return void - */ - public function testBcpow() - { - if (function_exists('bcpow')) { - $this->assertEquals( - '1267650600228229401496703205376', - PMA\libraries\Util::pow(2, 100, 'bcpow') - ); - } else { - $this->markTestSkipped('function bcpow() does not exist'); - } - } - - /** - * Test forpow - * - * @return void - */ - public function testGmppow() - { - if (function_exists('gmp_pow')) { - $this->assertEquals( - '1267650600228229401496703205376', - PMA\libraries\Util::pow(2, 100, 'gmp_pow') - ); - } else { - $this->markTestSkipped('function gmp_pow() does not exist'); - } - } - - /** - * Test forpow - * - * @return void - */ - public function testNegativeExp() - { - $this->assertEquals( - 0.25, - PMA\libraries\Util::pow(2, -2) - ); - } - - /** - * Test forpow - * - * @return void - */ - public function testNegativeExpPow() - { - if (function_exists('pow')) { - $this->assertEquals( - 0.25, - PMA\libraries\Util::pow(2, -2, 'pow') - ); - } else { - $this->markTestSkipped('function pow() does not exist'); - } - } - - /** - * Test forpow - * - * @return void - */ - public function testNegativeExpBcpow() - { - if (function_exists('bcpow')) { - $this->assertEquals( - false, - PMA\libraries\Util::pow(2, -2, 'bcpow') - ); - } else { - $this->markTestSkipped('function bcpow() does not exist'); - } - } - - /** - * Test forpow - * - * @return void - */ - public function testNegativeExpGmppow() - { - if (function_exists('gmp_pow')) { - $this->assertEquals( - false, - PMA\libraries\Util::pow(2, -2, 'gmp_pow') - ); - } else { - $this->markTestSkipped('function gmp_pow() does not exist'); - } - } -} From 0060c36ac9a687f3d03b281565b7f787bb23d6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 10:04:55 +0200 Subject: [PATCH 8/9] Remove usage of Util::pow with forcing builtin pow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/Util.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Util.php b/libraries/Util.php index a40a09549d..0532e8d2b9 100644 --- a/libraries/Util.php +++ b/libraries/Util.php @@ -1497,7 +1497,7 @@ class Util * So if we have 3,6,9,12.. free digits ($digits_left - $cur_digits) * to use, then lower the SI prefix */ - $cur_digits = floor(log10($value / self::pow(1000, $d, 'pow'))+1); + $cur_digits = floor(log10($value / pow(1000, $d))+1); if ($digits_left > $cur_digits) { $d -= floor(($digits_left - $cur_digits)/3); } @@ -1506,7 +1506,7 @@ class Util $d = 0; } - $value = round($value / (self::pow(1000, $d, 'pow') / $dh)) /$dh; + $value = round($value / (pow(1000, $d) / $dh)) /$dh; $unit = $units[$d]; // number_format is not multibyte safe, str_replace is safe From f143245f62e5a75547df0e50845b65237716965b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 27 Sep 2016 10:05:33 +0200 Subject: [PATCH 9/9] Use builtin pow for calculations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/Util.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libraries/Util.php b/libraries/Util.php index 0532e8d2b9..cc053bbfcc 100644 --- a/libraries/Util.php +++ b/libraries/Util.php @@ -1374,15 +1374,15 @@ class Util __('EiB') ); - $dh = self::pow(10, $comma); - $li = self::pow(10, $limes); + $dh = pow(10, $comma); + $li = pow(10, $limes); $unit = $byteUnits[0]; for ($d = 6, $ex = 15; $d >= 1; $d--, $ex-=3) { - $unitSize = $li * self::pow(10, $ex); + $unitSize = $li * pow(10, $ex); if (isset($byteUnits[$d]) && $value >= $unitSize) { // use 1024.0 to avoid integer overflow on 64-bit machines - $value = round($value / (self::pow(1024, $d) / $dh)) /$dh; + $value = round($value / (pow(1024, $d) / $dh)) /$dh; $unit = $byteUnits[$d]; break 1; } // end if @@ -1451,7 +1451,7 @@ class Util __(',') ); if (($originalValue != 0) && (floatval($value) == 0)) { - $value = ' <' . (1 / self::pow(10, $digits_right)); + $value = ' <' . (1 / pow(10, $digits_right)); } return $value; } @@ -1485,7 +1485,7 @@ class Util $sign = ''; } - $dh = self::pow(10, $digits_right); + $dh = pow(10, $digits_right); /* * This gives us the right SI prefix already, @@ -1525,7 +1525,7 @@ class Util if ($originalValue != 0 && floatval($value) == 0) { return ' <' . number_format( - (1 / self::pow(10, $digits_right)), + (1 / pow(10, $digits_right)), $digits_right, /* l10n: Decimal separator */ __('.'), @@ -1551,13 +1551,13 @@ class Util if (preg_match('/^[0-9]+GB$/', $formatted_size)) { $return_value = mb_substr($formatted_size, 0, -2) - * self::pow(1024, 3); + * pow(1024, 3); } elseif (preg_match('/^[0-9]+MB$/', $formatted_size)) { $return_value = mb_substr($formatted_size, 0, -2) - * self::pow(1024, 2); + * pow(1024, 2); } elseif (preg_match('/^[0-9]+K$/', $formatted_size)) { $return_value = mb_substr($formatted_size, 0, -1) - * self::pow(1024, 1); + * pow(1024, 1); } return $return_value; }// end of the 'extractValueFromFormattedSize' function