/* Do the calculation. */
rscale = MAX(scale, base->n_scale);
- while ( !bc_is_zero(exponent) )
+ if ( !bc_compare(mod, BCG(_one_)) )
{
- (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
- if ( !bc_is_zero(parity) )
+ temp = bc_new_num (1, scale);
+ }
+ else
+ {
+ while ( !bc_is_zero(exponent) )
{
- bc_multiply (temp, power, &temp, rscale);
- (void) bc_modulo (temp, mod, &temp, scale);
+ (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
+ if ( !bc_is_zero(parity) )
+ {
+ bc_multiply (temp, power, &temp, rscale);
+ (void) bc_modulo (temp, mod, &temp, scale);
+ }
+
+ bc_multiply (power, power, &power, rscale);
+ (void) bc_modulo (power, mod, &power, scale);
}
-
- bc_multiply (power, power, &power, rscale);
- (void) bc_modulo (power, mod, &power, scale);
}
/* Assign the value. */
--- /dev/null
+--TEST--
+Bug #54598 (bcpowmod() may return 1 if modulus is 1)
+--SKIPIF--
+<?php
+if (!extension_loaded('bcmath')) die('skip bcmath extension is not available');
+?>
+--FILE--
+<?php
+var_dump(bcpowmod(5, 0, 1));
+var_dump(bcpowmod(5, 0, 1, 3));
+?>
+===DONE===
+--EXPECT--
+string(1) "0"
+string(5) "0.000"
+===DONE===