Skip to content

Commit

Permalink
Use native math instead of GMP when its possible
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed Nov 23, 2021
1 parent 7d177ec commit 583d5cc
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/VarInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ public static function readUint(string $bin, int $offset = 0, $max_len = 0): arr
$i = $offset;
$max_i = $max_len + $offset;
while (isset($bin[$i])) {
$use_gmp = $i > 6;
$b = ord($bin[$i]);
if ($b < 0x80) {
if ($max_len > 0 && ($i > $max_i || $i === $max_i && $b > 1)) {
throw new InvalidArgumentException('The number overflows allowed limits of max byte len = ' . $max_len);
}
$result = gmp_or($x, gmp_shiftl($b, $s));
return [gmp_cmp($result, PHP_INT_MAX) >= 0 ? gmp_strval($result) : gmp_intval($result), $i + 1];
$result = $use_gmp
? gmp_or($x, gmp_shiftl($b, $s))
: $x | $b << $s
;
return [$use_gmp ? gmp_strval($result) :$result, $i + 1];
}
$x = gmp_strval(gmp_or($x, gmp_shiftl(gmp_strval(gmp_and($b, 0x7f)), $s)));
$x = $use_gmp
? gmp_strval(gmp_or($x, gmp_shiftl(gmp_strval(gmp_and($b, 0x7f)), $s)))
: $x | (($b & 0x7f) << $s)
;
$s += 7;
++$i;
}
Expand Down

0 comments on commit 583d5cc

Please sign in to comment.