src/IntMath.php
/**
     * Returns the product of the arguments.
     *
     * The result is the low-order bits of the true mathematical result as
     * represented in a sufficiently wide two's-complement format. If overflow
     * occurs, then the sign of the result may not be the same as the sign of
     * the mathematical product of the two values. Despite the overflow, no
     * exception is thrown in this case.
     *
     * @param integer $a The multiplicand.
     * @param integer $b The multiplier.
     *
     * @return integer The product.
     *
     * @throws InvalidArgumentException If one of the arguments is not an
     *                                  integer.
     */
Returns the product of the arguments.
The result is the low-order bits of the true mathematical result as
represented in a sufficiently wide two's-complement format. If overflow
occurs, then the sign of the result may not be the same as the sign of
the mathematical product of the two values. Despite the overflow, no
exception is thrown in this case.

@param integer $a The multiplicand.
@param integer $b The multiplier.

@return integer The product.

@throws InvalidArgumentException If one of the arguments is not an
integer.
Returns the product of the arguments . The result is the low - order bits of the true mathematical result as represented in a sufficiently wide two ' s - complement format . If overflow occurs then the sign of the result may not be the same as the sign of the mathematical product of the two values . Despite the overflow no exception is thrown in this case . @param integer $a The multiplicand . @param integer $b The multiplier . @return integer The product . @throws InvalidArgumentException If one of the arguments is not an integer .
public static function multiply($a, $b)
    {
        self::assertInteger($a);
        self::assertInteger($b);
        if ($a === 0 || $b === 0) {
            return 0;
        }
        if ($a === \PHP_INT_MIN) {
            return $b & 0x01 ? $a : 0;
        }
        if ($b === \PHP_INT_MIN) {
            return $a & 0x01 ? $b : 0;
        }
        $max = \PHP_INT_MIN;
        if ($a >= 0 && $b >= 0 || $a < 0 && $b < 0) {
            $max = \PHP_INT_MAX;
        }
        if (($b > 0 && $b <= ($max / $a)) || ($b < 0 && $b >= ($max / $a))) {
            return $a * $b;
        }
        $sign = \false;
        if ($a < 0) {
            $sign = !$sign;
            $a = self::negate($a);
        }
        if ($b < 0) {
            $sign = !$sign;
            $b = self::negate($b);
        }
        $product = 0;
        while ($a !== 0) {
            if (($a & 0x01) !== 0) {
                $product = self::add($product, $b);
            }
            $a >>= 1;
            $b <<= 1;
        }
        if ($sign) {
            $product = self::negate($product);
        }
        return $product;
    }
public static function multiply ( $ a , $ b ) { self :: assertInteger ( $ a ) ; self :: assertInteger ( $ b ) ; if ( $ a === 0 || $ b === 0 ) { return 0 ; } if ( $ a === \ PHP_INT_MIN ) { return $ b & 0x01 ? $ a : 0 ; } if ( $ b === \ PHP_INT_MIN ) { return $ a & 0x01 ? $ b : 0 ; } $ max = \ PHP_INT_MIN ; if ( $ a >= 0 && $ b >= 0 || $ a < 0 && $ b < 0 ) { $ max = \ PHP_INT_MAX ; } if ( ( $ b > 0 && $ b <= ( $ max / $ a ) ) || ( $ b < 0 && $ b >= ( $ max / $ a ) ) ) { return $ a * $ b ; } $ sign = \ false ; if ( $ a < 0 ) { $ sign = ! $ sign ; $ a = self :: negate ( $ a ) ; } if ( $ b < 0 ) { $ sign = ! $ sign ; $ b = self :: negate ( $ b ) ; } $ product = 0 ; while ( $ a !== 0 ) { if ( ( $ a & 0x01 ) !== 0 ) { $ product = self :: add ( $ product , $ b ) ; } $ a >>= 1 ; $ b <<= 1 ; } if ( $ sign ) { $ product = self :: negate ( $ product ) ; } return $ product ; }
Returns the product of the arguments.
Returns the product of the arguments .
// If the multiplicand or the multiplier are the smallest integer // supported, then the product is `0` or the smallest integer supported, // according as the other operand is odd or even respectively. // Same sign // Use native operators unless the operation causes an overflow // Signed multiplication can be accomplished by doing an unsigned // multiplication and taking manually care of the negative-signs. // Toggle the signed flag // Negate $a // Toggle the signed flag // Negate $b // Both operands are now positive, perform unsigned multiplication // Test the least significant bit (LSB) of multiplier // If 1, add the multiplicand to the product // Left-shift by one, or divide by 2 // Right-shift by one, or multiply by 2 // Negate the product