com php-src: Optimize truncation to zero scale in bc_raisemod (): ext/bcmath/libbcmath/src/raisemod.c

From: Date: Sat, 09 Sep 2017 16:41:59 +0000
Subject: com php-src: Optimize truncation to zero scale in bc_raisemod (): ext/bcmath/libbcmath/src/raisemod.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-99332@lists.php.net to get a copy of this message
Commit: d1e82b39e2c87961565da9f5b869829dd0da0437 Author: Christoph M. Becker <cmbecker69@gmx.de> Sat, 9 Sep 2017 18:41:59 +0200 Parents: 65b575134fe286b2ce8b23181eb70f0ecda5ac16 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=d1e82b39e2c87961565da9f5b869829dd0da0437 Log: Optimize truncation to zero scale in bc_raisemod() There's no need to use a division by one to truncate to zero scale; instead we introduce and use _bc_truncate(), what is more efficient. Changed paths: M ext/bcmath/libbcmath/src/raisemod..c Diff: diff --git a/ext/bcmath/libbcmath/src/raisemod.c b/ext/bcmath/libbcmath/src/raisemod.c index 84788b4..821dddb 100644 --- a/ext/bcmath/libbcmath/src/raisemod.c +++ b/ext/bcmath/libbcmath/src/raisemod.c @@ -38,6 +38,24 @@ #include "bcmath.h" #include "private.h" + +/* Truncate a number to zero scale. To avoid sharing issues (refcount and + shared n_value) the number is copied, this copy is truncated, and the + original number is "freed". */ + +static void +_bc_truncate (bc_num *num) +{ + bc_num temp; + + temp = bc_new_num ((*num)->n_len, 0); + temp->n_sign = (*num)->n_sign; + memcpy (temp->n_value, (*num)->n_value, (*num)->n_len); + bc_free_num (num); + *num = temp; +} + + /* Raise BASE to the EXPO power, reduced modulo MOD. The result is placed in RESULT. If a EXPO is not an integer, only the integer part is used. */ @@ -63,21 +81,21 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale) if (power->n_scale != 0) { bc_rt_warn ("non-zero scale in base"); - bc_divide (power, BCG(_one_), &power, 0); /*truncate */ + _bc_truncate (&power); } /* Check the exponent for scale digits. */ if (exponent->n_scale != 0) { bc_rt_warn ("non-zero scale in exponent"); - bc_divide (exponent, BCG(_one_), &exponent, 0); /*truncate */ + _bc_truncate (&exponent); } /* Check the modulus for scale digits. */ if (modulus->n_scale != 0) { bc_rt_warn ("non-zero scale in modulus"); - bc_divide (modulus, BCG(_one_), &modulus, 0); /*truncate */ + _bc_truncate (&modulus); } /* Do the calculation. */

« previous php.cvs (#99332) next »