defender-security
/
vendor
/
spomky-labs
/
cbor-php
/
src
/
OtherObject
/
SinglePrecisionFloatObject.php
SinglePrecisionFloatObject.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.1, at vendor/spomky-labs/cbor-php/src/OtherObject/SinglePrecisionFloatObject.php
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /* |
| 6 | * The MIT License (MIT) |
| 7 | * |
| 8 | * Copyright (c) 2018-2020 Spomky-Labs |
| 9 | * |
| 10 | * This software may be modified and distributed under the terms |
| 11 | * of the MIT license. See the LICENSE file for details. |
| 12 | */ |
| 13 | |
| 14 | namespace CBOR\OtherObject; |
| 15 | |
| 16 | use Brick\Math\BigInteger; |
| 17 | use CBOR\OtherObject as Base; |
| 18 | use CBOR\Utils; |
| 19 | use const INF; |
| 20 | use InvalidArgumentException; |
| 21 | use const NAN; |
| 22 | |
| 23 | final class SinglePrecisionFloatObject extends Base |
| 24 | { |
| 25 | public static function supportedAdditionalInformation(): array |
| 26 | { |
| 27 | return [self::OBJECT_SINGLE_PRECISION_FLOAT]; |
| 28 | } |
| 29 | |
| 30 | public static function createFromLoadedData(int $additionalInformation, ?string $data): Base |
| 31 | { |
| 32 | return new self($additionalInformation, $data); |
| 33 | } |
| 34 | |
| 35 | public static function create(string $value): self |
| 36 | { |
| 37 | if (mb_strlen($value, '8bit') !== 4) { |
| 38 | throw new InvalidArgumentException('The value is not a valid single precision floating point'); |
| 39 | } |
| 40 | |
| 41 | return new self(self::OBJECT_SINGLE_PRECISION_FLOAT, $value); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface |
| 46 | */ |
| 47 | public function getNormalizedData(bool $ignoreTags = false) |
| 48 | { |
| 49 | return $this->normalize(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return float|int |
| 54 | */ |
| 55 | public function normalize() |
| 56 | { |
| 57 | $exponent = $this->getExponent(); |
| 58 | $mantissa = $this->getMantissa(); |
| 59 | $sign = $this->getSign(); |
| 60 | |
| 61 | if ($exponent === 0) { |
| 62 | $val = $mantissa * 2 ** (-(126 + 23)); |
| 63 | } elseif ($exponent !== 0b11111111) { |
| 64 | $val = ($mantissa + (1 << 23)) * 2 ** ($exponent - (127 + 23)); |
| 65 | } else { |
| 66 | $val = $mantissa === 0 ? INF : NAN; |
| 67 | } |
| 68 | |
| 69 | return $sign * $val; |
| 70 | } |
| 71 | |
| 72 | public function getExponent(): int |
| 73 | { |
| 74 | $data = $this->data; |
| 75 | Utils::assertString($data, 'Invalid data'); |
| 76 | |
| 77 | return Utils::binToBigInteger($data)->shiftedRight(23)->and(Utils::hexToBigInteger('ff'))->toInt(); |
| 78 | } |
| 79 | |
| 80 | public function getMantissa(): int |
| 81 | { |
| 82 | $data = $this->data; |
| 83 | Utils::assertString($data, 'Invalid data'); |
| 84 | |
| 85 | return Utils::binToBigInteger($data)->and(Utils::hexToBigInteger('7fffff'))->toInt(); |
| 86 | } |
| 87 | |
| 88 | public function getSign(): int |
| 89 | { |
| 90 | $data = $this->data; |
| 91 | Utils::assertString($data, 'Invalid data'); |
| 92 | $sign = Utils::binToBigInteger($data)->shiftedRight(31); |
| 93 | |
| 94 | return $sign->isEqualTo(BigInteger::one()) ? -1 : 1; |
| 95 | } |
| 96 | } |
| 97 |