/** * Pack a long. * * If it is a 32bit PHP we suppose that this log is treated by bcmath * TODO 32bit * * @param int|string $value * * @return string the packed long */ public static function packLong($value) { if (PHP_INT_SIZE > 4) { $value = (int) $value; $binaryString = chr($value >> 56 & 0xff) . chr($value >> 48 & 0xff) . chr($value >> 40 & 0xff) . chr($value >> 32 & 0xff) . chr($value >> 24 & 0xff) . chr($value >> 16 & 0xff) . chr($value >> 8 & 0xff) . chr($value & 0xff); } else { /* * To get the two's complement of a binary number, * the bits are inverted, or "flipped", * by using the bitwise NOT operation; * the value of 1 is then added to the resulting value */ $bitString = ''; $isNegative = $value[0] == '-'; if (function_exists("bcmod")) { //add 1 for the two's complement if ($isNegative) { $value = bcadd($value, '1'); } while ($value !== '0') { $bitString = (string) abs((int) bcmod($value, '2')) . $bitString; $value = bcdiv($value, '2'); } } elseif (function_exists("gmp_mod")) { //add 1 for the two's complement if ($isNegative) { $value = gmp_strval(gmp_add($value, '1')); } while ($value !== '0') { $bitString = gmp_strval(gmp_abs(gmp_mod($value, '2'))) . $bitString; $value = gmp_strval(gmp_div_q($value, '2')); } } else { while ($value != 0) { list($value, $remainder) = self::str2bin((string) $value); $bitString = $remainder . $bitString; } } //Now do the logical not for the two's complement last phase if ($isNegative) { $len = strlen($bitString); for ($x = 0; $x < $len; $x++) { $bitString[$x] = $bitString[$x] == '1' ? '0' : '1'; } } //pad to have 64 bit if ($bitString != '' && $isNegative) { $bitString = str_pad($bitString, 64, '1', STR_PAD_LEFT); } else { $bitString = str_pad($bitString, 64, '0', STR_PAD_LEFT); } $hi = substr($bitString, 0, 32); $lo = substr($bitString, 32, 32); $hiBin = pack('H*', str_pad(base_convert($hi, 2, 16), 8, 0, STR_PAD_LEFT)); $loBin = pack('H*', str_pad(base_convert($lo, 2, 16), 8, 0, STR_PAD_LEFT)); $binaryString = $hiBin . $loBin; } return $binaryString; }
function deposited_or_withdrawn($deposit, $withdraw) { $net = gmp_sub($deposit, $withdraw); $abs = gmp_abs($net); if (gmp_cmp($net, 0) < 0) { $word = _("withdrawn"); } else { $word = _("deposited"); } return array($abs, $word); }
protected function calculateContentLength() { $nrOfOctets = 1; // we need at least one octet $tmpValue = gmp_abs(gmp_init($this->value, 10)); while (gmp_cmp($tmpValue, 127) > 0) { $tmpValue = $this->rightShift($tmpValue, 8); $nrOfOctets++; } return $nrOfOctets; }
/** * Encode negative integer to DER content. * * @param \GMP|resource $num * @return string */ private static function _encodeNegativeInteger($num) { $num = gmp_abs($num); // compute number of bytes required $width = 1; if ($num > 128) { $tmp = $num; do { $width++; $tmp >>= 8; } while ($tmp > 128); } // compute two's complement 2^n - x $num = gmp_pow("2", 8 * $width) - $num; $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); // if first bit is 0, prepend full inverted byte // to represent negative two's complement if (!(ord($bin[0]) & 0x80)) { $bin = chr(0xff) . $bin; } return $bin; }
/** * Get the absolute value * * @access public * @return self */ public function abs() { $result = gmp_abs($this->getRawValue()); return static::factory($result); }
/** * Converts a BigInteger to a binary string. * * @return string */ public function toBytes() { if (gmp_cmp($this->value, gmp_init(0)) === 0) { return ''; } $temp = gmp_strval(gmp_abs($this->value), 16); $temp = mb_strlen($temp, '8bit') & 1 ? '0' . $temp : $temp; $temp = hex2bin($temp); return ltrim($temp, chr(0)); }
/** * Creates a new decimal which is a result of a division of $a by $b. * * @param Decimal2 $a * @param Decimal2 $b * @return Decimal2 */ public static function div(Decimal2 $a, Decimal2 $b) { $strA = strval($a); $strB = strval($b); $sign_a = '-' === $strA[0] ? -1 : 1; $sign_b = '-' === $strB[0] ? -1 : 1; $ret = new Decimal2('0'); $ret->cents = gmp_strval(gmp_div_q(gmp_add(gmp_mul(gmp_abs(gmp_init($a->cents, 10)), 100), 50), gmp_abs(gmp_init($b->cents, 10)), GMP_ROUND_ZERO)); if ($sign_a * $sign_b < 0) { $ret->cents = gmp_strval(gmp_neg(gmp_init($ret->cents, 10))); } return $ret; }
/** * This method returns the absolute value of this object's value. * * @access public * @static * @param IInteger\Type $x the operand * @return IInteger\Type the result */ public static function abs(IInteger\Type $x) : IInteger\Type { return IInteger\Type::box(gmp_strval(gmp_abs($x->unbox()))); }
/** * Absolute value. * * @access public * @return \Topxia\Service\Util\Phpsec\Math\BigInteger */ public function abs() { $temp = new static(); switch (MATH_BIGINTEGER_MODE) { case self::MODE_GMP: $temp->value = gmp_abs($this->value); break; case self::MODE_BCMATH: $temp->value = bccomp($this->value, '0', 0) < 0 ? substr($this->value, 1) : $this->value; break; default: $temp->value = $this->value; } return $temp; }
/** * Returns the absolute value of a Math_Integer number * * @param object Math_Integer $int1 * @return object Math_Integer on success, PEAR_Error otherwise * @access public */ function &abs(&$int1) { /*{{{*/ if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) { return $err; } switch (MATH_INTLIB) { /*{{{*/ case 'gmp': $tmp = gmp_strval(gmp_abs($int1->getValue())); break; case 'bcmath': if ($int1->getValue() < 0) { $tmp = bcmul(-1, $int1->getValue()); } else { $tmp = $int1->getValue(); } break; case 'std': $tmp = abs($int1->getValue()); break; } /*}}}*/ return new Math_Integer($tmp); }
<?php gmp_abs();
<?php var_dump(gmp_strval(gmp_abs(""))); var_dump(gmp_strval(gmp_abs("0"))); var_dump(gmp_strval(gmp_abs(0))); var_dump(gmp_strval(gmp_abs(-1.1111111111111111E+20))); var_dump(gmp_strval(gmp_abs("111111111111111111111"))); var_dump(gmp_strval(gmp_abs("-111111111111111111111"))); var_dump(gmp_strval(gmp_abs("0000"))); var_dump(gmp_strval(gmp_abs("09876543"))); var_dump(gmp_strval(gmp_abs("-099987654"))); var_dump(gmp_abs()); var_dump(gmp_abs(1, 2)); var_dump(gmp_abs(array())); echo "Done\n";
/** * Get absolute value of a big integer * * @param string $operand * @return string */ public function abs($operand) { $result = gmp_abs($operand); return gmp_strval($result); }
/** * Absolute value. * * @return Math_BigInteger * @access public */ public function abs() { $temp = new Math_BigInteger(); switch (self::$mode) { case Math_BigInteger::MODE_GMP: $temp->value = gmp_abs($this->value); break; case Math_BigInteger::MODE_BCMATH: $temp->value = bccomp($this->value, '0', 0) < 0 ? substr($this->value, 1) : $this->value; break; default: $temp->value = $this->value; } return $temp; }
/** * Converts the value to an absolute number. * * @return BigInteger */ public function abs() : BigInteger { $value = gmp_abs($this->value); return $this->assignValue($value); }
/** * Return Least Common Multiple of two numbers * @param int $a * @param int $b * @return int */ private function lcm($a, $b) { return gmp_abs(gmp_div_q(gmp_mul($a, $b), gmp_gcd($a, $b))); }
/** * Get server information * * @throws InvalidPacketException * @throws SocketException * * @return array Returns an array with information on success */ public function GetInfo() { if (!$this->Connected) { throw new SocketException('Not connected.', SocketException::NOT_CONNECTED); } $this->Socket->Write(self::A2S_INFO, "Source Engine Query"); $Buffer = $this->Socket->Read(); $Type = $Buffer->GetByte(); // Old GoldSource protocol, HLTV still uses it if ($Type === self::S2A_INFO_OLD && $this->Socket->Engine === self::GOLDSOURCE) { /** * If we try to read data again, and we get the result with type S2A_INFO (0x49) * That means this server is running dproto, * Because it sends answer for both protocols */ $Server['Address'] = $Buffer->GetString(); $Server['HostName'] = $Buffer->GetString(); $Server['Map'] = $Buffer->GetString(); $Server['ModDir'] = $Buffer->GetString(); $Server['ModDesc'] = $Buffer->GetString(); $Server['Players'] = $Buffer->GetByte(); $Server['MaxPlayers'] = $Buffer->GetByte(); $Server['Protocol'] = $Buffer->GetByte(); $Server['Dedicated'] = Chr($Buffer->GetByte()); $Server['Os'] = Chr($Buffer->GetByte()); $Server['Password'] = $Buffer->GetByte() === 1; $Server['IsMod'] = $Buffer->GetByte() === 1; if ($Server['IsMod']) { $Mod['Url'] = $Buffer->GetString(); $Mod['Download'] = $Buffer->GetString(); $Buffer->Get(1); // NULL byte $Mod['Version'] = $Buffer->GetLong(); $Mod['Size'] = $Buffer->GetLong(); $Mod['ServerSide'] = $Buffer->GetByte() === 1; $Mod['CustomDLL'] = $Buffer->GetByte() === 1; } $Server['Secure'] = $Buffer->GetByte() === 1; $Server['Bots'] = $Buffer->GetByte(); if (isset($Mod)) { $Server['Mod'] = $Mod; } return $Server; } if ($Type !== self::S2A_INFO) { throw new InvalidPacketException('GetInfo: Packet header mismatch. (0x' . DecHex($Type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH); } $Server['Protocol'] = $Buffer->GetByte(); $Server['HostName'] = $Buffer->GetString(); $Server['Map'] = $Buffer->GetString(); $Server['ModDir'] = $Buffer->GetString(); $Server['ModDesc'] = $Buffer->GetString(); $Server['AppID'] = $Buffer->GetShort(); $Server['Players'] = $Buffer->GetByte(); $Server['MaxPlayers'] = $Buffer->GetByte(); $Server['Bots'] = $Buffer->GetByte(); $Server['Dedicated'] = Chr($Buffer->GetByte()); $Server['Os'] = Chr($Buffer->GetByte()); $Server['Password'] = $Buffer->GetByte() === 1; $Server['Secure'] = $Buffer->GetByte() === 1; // The Ship (they violate query protocol spec by modifying the response) if ($Server['AppID'] === 2400) { $Server['GameMode'] = $Buffer->GetByte(); $Server['WitnessCount'] = $Buffer->GetByte(); $Server['WitnessTime'] = $Buffer->GetByte(); } $Server['Version'] = $Buffer->GetString(); // Extra Data Flags if ($Buffer->Remaining() > 0) { $Server['ExtraDataFlags'] = $Flags = $Buffer->GetByte(); // The server's game port if ($Flags & 0x80) { $Server['GamePort'] = $Buffer->GetShort(); } // The server's steamid // Want to play around with this? // You can use https://github.com/xPaw/SteamID.php if ($Flags & 0x10) { $SteamIDLower = $Buffer->GetUnsignedLong(); $SteamIDInstance = $Buffer->GetUnsignedLong(); // This gets shifted by 32 bits, which should be steamid instance $SteamID = 0; if (PHP_INT_SIZE === 4) { if (extension_loaded('gmp')) { $SteamIDLower = gmp_abs($SteamIDLower); $SteamIDInstance = gmp_abs($SteamIDInstance); $SteamID = gmp_strval(gmp_or($SteamIDLower, gmp_mul($SteamIDInstance, gmp_pow(2, 32)))); } else { throw new \RuntimeException('Either 64-bit PHP installation or "gmp" module is required to correctly parse server\'s steamid.'); } } else { $SteamID = $SteamIDLower | $SteamIDInstance << 32; } $Server['SteamID'] = $SteamID; unset($SteamIDLower, $SteamIDInstance, $SteamID); } // The spectator port and then the spectator server name if ($Flags & 0x40) { $Server['SpecPort'] = $Buffer->GetShort(); $Server['SpecName'] = $Buffer->GetString(); } // The game tag data string for the server if ($Flags & 0x20) { $Server['GameTags'] = $Buffer->GetString(); } // GameID -- alternative to AppID? if ($Flags & 0x1) { $Server['GameID'] = $Buffer->GetUnsignedLong() | $Buffer->GetUnsignedLong() << 32; } if ($Buffer->Remaining() > 0) { throw new InvalidPacketException('GetInfo: unread data? ' . $Buffer->Remaining() . ' bytes remaining in the buffer. Please report it to the library developer.', InvalidPacketException::BUFFER_NOT_EMPTY); } } return $Server; }
protected function readTextRecordInner(&$content, &$pos, $recordType) { switch ($recordType) { case Constants::RECORD_TYPE_ZERO_TEXT: case Constants::RECORD_TYPE_ZERO_TEXT_WITH_END_ELEMENT: return '0'; break; case Constants::RECORD_TYPE_ONE_TEXT: case Constants::RECORD_TYPE_ONE_TEXT_WITH_END_ELEMENT: return '1'; break; case Constants::RECORD_TYPE_FALSE_TEXT: case Constants::RECORD_TYPE_FALSE_TEXT_WITH_END_ELEMENT: return 'false'; break; case Constants::RECORD_TYPE_TRUE_TEXT: case Constants::RECORD_TYPE_TRUE_TEXT_WITH_END_ELEMENT: return 'true'; break; case Constants::RECORD_TYPE_INT8_TEXT: case Constants::RECORD_TYPE_INT8_TEXT_WITH_END_ELEMENT: $record = unpack('c*', $content[$pos]); $pos += 1; return (string) $record[1]; break; case Constants::RECORD_TYPE_INT16_TEXT: case Constants::RECORD_TYPE_INT16_TEXT_WITH_END_ELEMENT: $record = unpack('s*', substr($content, $pos, 2)); $pos += 2; return (string) $record[1]; break; case Constants::RECORD_TYPE_INT32_TEXT: case Constants::RECORD_TYPE_INT32_TEXT_WITH_END_ELEMENT: $record = unpack('l*', substr($content, $pos, 4)); $pos += 4; return (string) $record[1]; break; case Constants::RECORD_TYPE_INT64_TEXT: case Constants::RECORD_TYPE_INT64_TEXT_WITH_END_ELEMENT: if (!function_exists('gmp_init')) { throw new DecodingException('Int64 requires GMP extension'); } list(, $int64Hex) = unpack('H*', strrev(substr($content, $pos, 8))); $pos += 8; return (string) gmp_strval(gmp_init($int64Hex, 16), 10); break; case Constants::RECORD_TYPE_FLOAT_TEXT: case Constants::RECORD_TYPE_FLOAT_TEXT_WITH_END_ELEMENT: $record = unpack('f*', substr($content, $pos, 4)); $pos += 4; return (string) $record[1]; break; case Constants::RECORD_TYPE_DOUBLE_TEXT: case Constants::RECORD_TYPE_DOUBLE_TEXT_WITH_END_ELEMENT: $record = unpack('d*', substr($content, $pos, 8)); $pos += 8; return (string) $record[1]; break; case Constants::RECORD_TYPE_DECIMAL_TEXT: case Constants::RECORD_TYPE_DECIMAL_TEXT_WITH_END_ELEMENT: if (!function_exists('gmp_init')) { throw new DecodingException('Decimal requires GMP extension'); } $pos += 2; // First 2 bytes reserved $scale = ord($content[$pos]); $pos += 1; $sign = ord($content[$pos]); $pos += 1; list(, $hi32Hex) = unpack('H*', strrev(substr($content, $pos, 4))); $pos += 4; $hi32 = gmp_init($hi32Hex, 16); list(, $lo64Hex) = unpack('H*', strrev(substr($content, $pos, 8))); $pos += 8; $lo64 = gmp_init($lo64Hex, 16); $value = gmp_add(gmp_mul($hi32, gmp_pow(2, 64)), $lo64); $record = gmp_strval($value, 10); if ($scale > 0) { if ($scale > strlen($record)) { $record = str_repeat('0', $scale - strlen($record)) . $record; } $record = substr($record, 0, strlen($record) - $scale) . '.' . substr($record, $scale * -1); $record = trim($record, '0'); if ($record[0] == '.') { $record = '0' . $record; } } if ($sign == 0x80) { $record = '-' . $record; } return $record; break; case Constants::RECORD_TYPE_DATETIME_TEXT: case Constants::RECORD_TYPE_DATETIME_TEXT_WITH_END_ELEMENT: if (!function_exists('gmp_init')) { throw new DecodingException('Datetime requires GMP extension'); } $binary = ''; for ($i = 0; $i < 8; ++$i) { list(, $byteint) = unpack('C*', $content[$pos + $i]); $binary = sprintf('%08b', $byteint) . $binary; } $pos += 8; $value = gmp_init(substr($binary, 2, 62), 2); // Only calc with seconds, since PHP datetime doesn't support fractions $secsRemaining = gmp_div($value, '10000000'); // Compensate for using unix timestamp $epochSecsRemaining = gmp_sub($secsRemaining, '62135596800'); // Load PHP datetime with seconds remaining $datetime = new DateTime('@' . gmp_strval($epochSecsRemaining, 10), new DateTimeZone('UTC')); $date = $datetime->format('Y-m-d'); $time = $datetime->format('H:i:s'); // Get fractions $fraction = substr(gmp_strval($value, 10), -7); // Join different time elements if ($fraction !== '0000000') { $record = "{$date}T{$time}.{$fraction}"; } elseif ($time !== '00:00:00') { $record = "{$date}T{$time}"; } else { $record = "{$date}"; } // Add timezone info $tz = gmp_intval(gmp_init(substr($binary, 0, 2), 2)); if ($tz == 2) { // TODO: local time handling } elseif ($tz == 1) { $record .= 'Z'; } return $record; break; case Constants::RECORD_TYPE_CHARS8_TEXT: case Constants::RECORD_TYPE_CHARS8_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('C*', $content[$pos]); $pos += 1; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return $record; break; case Constants::RECORD_TYPE_CHARS16_TEXT: case Constants::RECORD_TYPE_CHARS16_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('S*', substr($content, $pos, 2)); $pos += 2; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return $record; break; case Constants::RECORD_TYPE_CHARS32_TEXT: case Constants::RECORD_TYPE_CHARS32_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('l*', substr($content, $pos, 4)); $pos += 4; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return $record; break; case Constants::RECORD_TYPE_BYTES8_TEXT: case Constants::RECORD_TYPE_BYTES8_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('C*', $content[$pos]); $pos += 1; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return base64_encode($record); break; case Constants::RECORD_TYPE_BYTES16_TEXT: case Constants::RECORD_TYPE_BYTES16_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('S*', substr($content, $pos, 2)); $pos += 2; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return base64_encode($record); break; case Constants::RECORD_TYPE_BYTES32_TEXT: case Constants::RECORD_TYPE_BYTES32_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('l*', substr($content, $pos, 4)); $pos += 4; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return base64_encode($record); break; case Constants::RECORD_TYPE_START_LIST_TEXT: $record = ''; while (ord($content[$pos]) != Constants::RECORD_TYPE_END_LIST_TEXT) { if ($record !== '') { $record .= ' '; } $record .= $this->readTextRecord($content, $pos); } $pos += 1; // skip 1 for end list return $record; break; case Constants::RECORD_TYPE_EMPTY_TEXT: case Constants::RECORD_TYPE_EMPTY_TEXT_WITH_END_ELEMENT: return ''; break; case Constants::RECORD_TYPE_DICTIONARY_TEXT: case Constants::RECORD_TYPE_DICTIONARY_TEXT_WITH_END_ELEMENT: return $this->readDictionaryString($content, $pos); break; case Constants::RECORD_TYPE_UNIQUEID_TEXT: case Constants::RECORD_TYPE_UNIQUEID_TEXT_WITH_END_ELEMENT: case Constants::RECORD_TYPE_UUID_TEXT: case Constants::RECORD_TYPE_UUID_TEXT_WITH_END_ELEMENT: list(, $data1) = unpack('H*', strrev(substr($content, $pos, 4))); $pos += 4; list(, $data2) = unpack('H*', strrev(substr($content, $pos, 2))); $pos += 2; list(, $data3) = unpack('H*', strrev(substr($content, $pos, 2))); $pos += 2; list(, $data4) = unpack('H*', substr($content, $pos, 2)); $pos += 2; list(, $data5) = unpack('H*', substr($content, $pos, 6)); $pos += 6; $record = "{$data1}-{$data2}-{$data3}-{$data4}-{$data5}"; if ($recordType == Constants::RECORD_TYPE_UNIQUEID_TEXT || $recordType == Constants::RECORD_TYPE_UNIQUEID_TEXT_WITH_END_ELEMENT) { $record = 'urn:uuid:' . $record; } return $record; break; case Constants::RECORD_TYPE_TIMESPAN_TEXT: case Constants::RECORD_TYPE_TIMESPAN_TEXT_WITH_END_ELEMENT: if (!function_exists('gmp_init')) { throw new DecodingException('Timespan requires GMP extension'); } $value = ''; for ($i = 0; $i < 8; $i++) { list(, $byte) = unpack('C*', $content[$pos + $i]); $value = sprintf('%08b', $byte) . $value; } $pos += 8; $value = gmp_init($value, 2); $minus = false; if (gmp_testbit($value, 63)) { $minus = true; $mask = gmp_init(str_repeat('1', 64), '2'); $value = gmp_and(gmp_com($value), $mask); $value = gmp_add($value, 1); } $remaining = gmp_abs($value); list($days, $remaining) = gmp_div_qr($remaining, gmp_init('864000000000')); list($hours, $remaining) = gmp_div_qr($remaining, gmp_init('36000000000')); list($mins, $remaining) = gmp_div_qr($remaining, gmp_init('600000000')); list($secs, $remaining) = gmp_div_qr($remaining, gmp_init('10000000')); $fracs = $remaining; $days = gmp_intval($days); $hours = gmp_intval($hours); $mins = gmp_intval($mins); $secs = gmp_intval($secs); $fracs = gmp_intval($fracs); $record = $minus ? '-P' : 'P'; if ($days > 0) { $record .= $days . 'D'; } if ($hours > 0 || $mins > 0 || $secs > 0 || $fracs > 0) { $record .= 'T'; if ($hours > 0) { $record .= $hours . 'H'; } if ($mins > 0) { $record .= $mins . 'M'; } if ($secs > 0 || $fracs > 0) { $record .= $secs; if ($fracs > 0) { $record .= '.' . $fracs; } $record .= 'S'; } } return $record; break; case Constants::RECORD_TYPE_UINT64_TEXT: case Constants::RECORD_TYPE_UINT64_TEXT_WITH_END_ELEMENT: if (!function_exists('gmp_init')) { throw new DecodingException('Uint64 requires GMP extension'); } list(, $uint64Hex) = unpack('H*', strrev(substr($content, $pos, 8))); $pos += 8; return (string) gmp_strval(gmp_init($uint64Hex, 16), 10); break; case Constants::RECORD_TYPE_BOOL_TEXT: case Constants::RECORD_TYPE_BOOL_TEXT_WITH_END_ELEMENT: $record = ord($content[$pos]); $pos += 1; switch ($record) { case 0: return 'false'; break; case 1: return 'true'; break; } throw new DecodingException(sprintf('Unknown boolean value 0x%02X at position %d.', $record, $pos)); break; case Constants::RECORD_TYPE_UNICODECHARS8_TEXT: case Constants::RECORD_TYPE_UNICODECHARS8_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('C*', $content[$pos]); $pos += 1; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return mb_convert_encoding($record, 'UTF-8', 'UTF-16'); break; case Constants::RECORD_TYPE_UNICODECHARS16_TEXT: case Constants::RECORD_TYPE_UNICODECHARS16_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('S*', substr($content, $pos, 2)); $pos += 2; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return mb_convert_encoding($record, 'UTF-8', 'UTF-16'); break; case Constants::RECORD_TYPE_UNICODECHARS32_TEXT: case Constants::RECORD_TYPE_UNICODECHARS32_TEXT_WITH_END_ELEMENT: list(, $recordLength) = unpack('l*', substr($content, $pos, 4)); $pos += 4; $record = substr($content, $pos, $recordLength); $pos += $recordLength; return mb_convert_encoding($record, 'UTF-8', 'UTF-16'); break; case Constants::RECORD_TYPE_QNAMEDICTIONARY_TEXT: case Constants::RECORD_TYPE_QNAMEDICTIONARY_TEXT_WITH_END_ELEMENT: $prefix = chr(97 + ord($content[$pos])); $pos += 1; $name = $this->readDictionaryString($content, $pos); return $prefix . ':' . $name; break; default: throw new DecodingException(sprintf('Unknown record type 0x%02X at position %d.', $recordType, $pos)); break; } }
<?php // gmp_abs $abs1 = gmp_abs("274982683358"); $abs2 = gmp_abs("-274982683358"); echo gmp_strval($abs1) . "\n"; echo gmp_strval($abs2) . "\n"; // gmp_add $sum = gmp_add("123456789012345", "76543210987655"); echo gmp_strval($sum) . "\n"; // gmp_and $and1 = gmp_and("0xfffffffff4", "0x4"); $and2 = gmp_and("0xfffffffff4", "0x8"); echo gmp_strval($and1) . "\n"; echo gmp_strval($and2) . "\n"; // gmp_clrbit $clrbit = gmp_init("0xff"); gmp_clrbit($clrbit, 0); echo gmp_strval($clrbit) . "\n"; // gmp_cmp $cmp1 = gmp_cmp("1234", "1000"); // greater than $cmp2 = gmp_cmp("1000", "1234"); // less than $cmp3 = gmp_cmp("1234", "1234"); // equal to echo "{$cmp1} {$cmp2} {$cmp3}" . "\n"; // gmp_com $com = gmp_com("1234"); echo gmp_strval($com) . "\n"; // gmp_div_q
/** * Return the absolute value of the number * * @return \Chippyash\Type\Number\GMPIntType */ public function abs() { return new self(gmp_abs($this->value)); }
/** * Convert a Unix timestamp to an internal TAI array * * @internal * * @param string|float|integer $stamp Unix timestamp to convert into the * internal TAI array representation * @return string[] */ public static function toInternalFromUnix($stamp) { $stamp = is_null($stamp) ? microtime(true) : (is_numeric($stamp) ? $stamp : 0); $time = []; if (BC::comp($stamp, BC::sub(0, BC::pow(2, 62, 18), 18), 18) === -1) { $stamp = BC::sub(0, BC::pow(2, 62, 18), 18); } elseif (BC::comp($stamp, BC::sub(BC::pow(2, 63, 18), BC::pow(2, 62, 18), 18), 18) >= 0) { $stamp = BC::sub(BC::sub(BC::pow(2, 63, 18), BC::pow(2, 62, 18), 18), BC::pow(10, -18, 18), 18); } $unix_seconds = BC::div($stamp, 1, 0); $time['seconds'] = BC::add($unix_seconds, BC::pow(2, 62, 18), 0); $time['nano'] = gmp_strval(gmp_abs(BC::mul(BC::sub($stamp, $unix_seconds, 18), BC::pow(10, 9, 18), 0)), 10); $time['atto'] = gmp_strval(gmp_abs(BC::mul(BC::sub(BC::mul(BC::sub($stamp, $unix_seconds, 18), BC::pow(10, 9, 18), 9), BC::mul(BC::comp($unix_seconds, 0, 18), $time['nano'], 18), 18), BC::pow(10, 9, 18), 0)), 10); return $time; }
public function floatValue() { $stringValue = gmp_strval($this->resource); $floatValue = floatval($stringValue); if (is_int($floatValue) && (string) $floatValue !== $stringValue || !is_float($floatValue)) { throw new WrongArgumentException('can\'t convert to float'); } else { // is_float($floatValue) $absValue = abs($floatValue); $exponent = floor($absValue == 0 ? 0 : log10($absValue)); $mantiss = (int) floor($floatValue * pow(10, -$exponent)); if (gmp_cmp(gmp_abs($this->resource), gmp_abs(gmp_sub(gmp_abs($this->resource), gmp_mul($mantiss, gmp_pow(10, $exponent))))) < 0) { throw new WrongArgumentException('can\'t convert to float'); } } return $floatValue; }
/** * Absolute value. * * @return Math_BigInteger * @access public */ function abs() { $temp = new Math_BigInteger(); switch (MATH_BIGINTEGER_MODE) { case MATH_BIGINTEGER_MODE_GMP: $temp->value = gmp_abs($this->value); break; case MATH_BIGINTEGER_MODE_BCMATH: $temp->value = bccomp($this->value, '0', 0) < 0 ? substr($this->value, 1) : $this->value; break; default: $temp->value = $this->value; } return $temp; }
/** * Convert a Unix timestamp to an internal TAI array * * @internal * * @param string|float|integer $stamp Unix timestamp to convert into the * internal TAI array representation * @return string[] */ public static function toInternalFromUnix($stamp) { $stamp = is_null($stamp) ? microtime(true) : $stamp; $time = []; if (bccomp($stamp, bcsub(0, bcpow(2, 62))) === -1) { $stamp = bcsub(0, bcpow(2, 62)); } elseif (bccomp($stamp, bcsub(bcpow(2, 63), bcpow(2, 62))) >= 0) { $stamp = bcsub(bcsub(bcpow(2, 63), bcpow(2, 62)), bcpow(10, -18)); } $unix_seconds = bcdiv($stamp, 1, 0); $time['seconds'] = bcadd($unix_seconds, bcpow(2, 62), 0); $time['nano'] = gmp_strval(gmp_abs(bcmul(bcsub($stamp, $unix_seconds), bcpow(10, 9), 0)), 10); $time['atto'] = gmp_strval(gmp_abs(bcmul(bcsub(bcmul(bcsub($stamp, $unix_seconds), bcpow(10, 9), 9), bcmul(bccomp($unix_seconds, 0), $time['nano'])), bcpow(10, 9), 0)), 10); return $time; }