diff options
author | Paul Yang <TeBoring@users.noreply.github.com> | 2016-10-25 17:27:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-25 17:27:05 -0700 |
commit | 51c5ff889ccd3836c25f40baafb350f92c9ee103 (patch) | |
tree | 39ba93f36167e2ab73c25ac337da0a97a4df4970 /php/src/Google/Protobuf/Internal/GPBWire.php | |
parent | 58580da37357941d502805be3ae520441be77728 (diff) | |
download | protobuf-51c5ff889ccd3836c25f40baafb350f92c9ee103.tar.gz protobuf-51c5ff889ccd3836c25f40baafb350f92c9ee103.tar.bz2 protobuf-51c5ff889ccd3836c25f40baafb350f92c9ee103.zip |
Fix pure php implementation for 32-bit machine. (#2282)
Diffstat (limited to 'php/src/Google/Protobuf/Internal/GPBWire.php')
-rw-r--r-- | php/src/Google/Protobuf/Internal/GPBWire.php | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/php/src/Google/Protobuf/Internal/GPBWire.php b/php/src/Google/Protobuf/Internal/GPBWire.php index 0e741e15..7e2c124f 100644 --- a/php/src/Google/Protobuf/Internal/GPBWire.php +++ b/php/src/Google/Protobuf/Internal/GPBWire.php @@ -32,10 +32,6 @@ namespace Google\Protobuf\Internal; -use Google\Protobuf\Internal\GPBUtil; -use Google\Protobuf\Internal\Int64; -use Google\Protobuf\Internal\Uint64; - class GPBWire { @@ -150,20 +146,28 @@ class GPBWire public static function zigZagEncode64($int64) { - $a = $int64->copy()->leftShift(1); - $b = $int64->copy()->rightShift(63); - $result = $a->bitXor($b); - $uint64 = Uint64::newValue($result->high, $result->low); - return $uint64; + if (PHP_INT_SIZE == 4) { + if (bccomp($int64, 0) >= 0) { + return bcmul($int64, 2); + } else { + return bcsub(bcmul(bcsub(0, $int64), 2), 1); + } + } else { + return ($int64 << 1) ^ ($int64 >> 63); + } } public static function zigZagDecode64($uint64) { - $a = $uint64->copy()->rightShift(1); - $b = $uint64->oddMask(); - $result = $a->bitXor($b); - $int64 = Int64::newValue($result->high, $result->low); - return $int64; + if (PHP_INT_SIZE == 4) { + if (bcmod($uint64, 2) == 0) { + return bcdiv($uint64, 2, 0); + } else { + return bcsub(0, bcdiv(bcadd($uint64, 1), 2, 0)); + } + } else { + return (($uint64 >> 1) & 0x7FFFFFFFFFFFFFFF) ^ (-($uint64 & 1)); + } } public static function readInt32(&$input, &$value) @@ -227,11 +231,7 @@ class GPBWire public static function readSfixed64(&$input, &$value) { - if (!self::readFixed64($input, $value)) { - return false; - } - $value = Int64::newValue($value->high, $value->low); - return true; + return $input->readLittleEndian64($value); } public static function readFloat(&$input, &$value) @@ -259,7 +259,7 @@ class GPBWire if (!$input->readVarint64($value)) { return false; } - if ($value->high === 0 && $value->low === 0) { + if ($value == 0) { $value = false; } else { $value = true; @@ -324,8 +324,8 @@ class GPBWire public static function writeSint64(&$output, $value) { - $value = GPBWire::zigZagEncode64(GPBUtil::Int64($value)); - return $output->writeVarint64($value->toInteger()); + $value = GPBWire::zigZagEncode64($value); + return $output->writeVarint64($value); } public static function writeFixed32(&$output, $value) @@ -431,9 +431,8 @@ class GPBWire public static function sint64Size($value) { - $value = GPBUtil::Int64($value); $value = self::zigZagEncode64($value); - return self::varint64Size($value->toInteger()); + return self::varint64Size($value); } public static function varint64Size($value) |