PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / spomky-labs / cbor-php / src / OtherObject / SinglePrecisionFloatObject.php

SinglePrecisionFloatObject.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at vendor/spomky-labs/cbor-php/src/OtherObject/SinglePrecisionFloatObject.php

88 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Assert\Assertion;
17 use Brick\Math\BigInteger;
18 use CBOR\OtherObject as Base;
19 use CBOR\Utils;
20 use InvalidArgumentException;
21
22 final class SinglePrecisionFloatObject extends Base
23 {
24 public static function supportedAdditionalInformation(): array
25 {
26 return [26];
27 }
28
29 public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
30 {
31 return new self($additionalInformation, $data);
32 }
33
34 /**
35 * @return SinglePrecisionFloatObject
36 */
37 public static function create(string $value): self
38 {
39 if (4 !== mb_strlen($value, '8bit')) {
40 throw new InvalidArgumentException('The value is not a valid single precision floating point');
41 }
42
43 return new self(26, $value);
44 }
45
46 public function getNormalizedData(bool $ignoreTags = false)
47 {
48 $exp = $this->getExponent();
49 $mant = $this->getMantissa();
50 $sign = $this->getSign();
51
52 if (0 === $exp) {
53 $val = $mant * 2 ** (-(126 + 23));
54 } elseif (0b11111111 !== $exp) {
55 $val = ($mant + (1 << 23)) * 2 ** ($exp - (127 + 23));
56 } else {
57 $val = 0 === $mant ? INF : NAN;
58 }
59
60 return $sign * $val;
61 }
62
63 public function getExponent(): int
64 {
65 $data = $this->data;
66 Assertion::string($data, 'Invalid data');
67
68 return Utils::binToBigInteger($data)->shiftedRight(23)->and(Utils::hexToBigInteger('ff'))->toInt();
69 }
70
71 public function getMantissa(): int
72 {
73 $data = $this->data;
74 Assertion::string($data, 'Invalid data');
75
76 return Utils::binToBigInteger($data)->and(Utils::hexToBigInteger('7fffff'))->toInt();
77 }
78
79 public function getSign(): int
80 {
81 $data = $this->data;
82 Assertion::string($data, 'Invalid data');
83 $sign = Utils::binToBigInteger($data)->shiftedRight(32);
84
85 return $sign->isEqualTo(BigInteger::one()) ? -1 : 1;
86 }
87 }
88