aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/WebAssembly/BinaryFormat/Decoder.php
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-04-05 23:26:56 +0900
committernsfisis <nsfisis@gmail.com>2025-04-06 02:03:32 +0900
commitb47f6c38389762aff3b1b86c179517d3e9d9eb87 (patch)
treeff1a3cae32e91a816537fc8dee961a9d4a2d9415 /src/WebAssembly/BinaryFormat/Decoder.php
parent3d0f0b06ee25571034a13a43d2ca660ef687afa9 (diff)
downloadphp-waddiwasi-b47f6c38389762aff3b1b86c179517d3e9d9eb87.tar.gz
php-waddiwasi-b47f6c38389762aff3b1b86c179517d3e9d9eb87.tar.zst
php-waddiwasi-b47f6c38389762aff3b1b86c179517d3e9d9eb87.zip
refactor: add BitOps component
Diffstat (limited to 'src/WebAssembly/BinaryFormat/Decoder.php')
-rw-r--r--src/WebAssembly/BinaryFormat/Decoder.php30
1 files changed, 3 insertions, 27 deletions
diff --git a/src/WebAssembly/BinaryFormat/Decoder.php b/src/WebAssembly/BinaryFormat/Decoder.php
index cce3c5c..f0a5c29 100644
--- a/src/WebAssembly/BinaryFormat/Decoder.php
+++ b/src/WebAssembly/BinaryFormat/Decoder.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\WebAssembly\BinaryFormat;
+use Nsfisis\Waddiwasi\BitOps\BinaryConversion;
use Nsfisis\Waddiwasi\Stream\StreamInterface;
use Nsfisis\Waddiwasi\Stream\UnexpectedEofException;
use Nsfisis\Waddiwasi\WebAssembly\BinaryFormat\Internal\Code;
@@ -39,7 +40,6 @@ use function assert;
use function count;
use function get_class;
use function in_array;
-use function is_float;
use function is_int;
use function ord;
use function sprintf;
@@ -1090,25 +1090,7 @@ final class Decoder
*/
private function decodeF32(): float
{
- $buf = $this->stream->read(4);
- $result = unpack('V', $buf);
- if ($result === false) {
- throw new InvalidBinaryFormatException("f32");
- }
- assert(isset($result[1]) && is_int($result[1]));
- $i = $result[1];
- if (($i & 0b01111111100000000000000000000000) === 0b01111111100000000000000000000000) {
- $sign = ($i & 0b10000000000000000000000000000000) === 0 ? 1 : -1;
- $payload = $i & 0b00000000011111111111111111111111;
- $j = ($sign === 1 ? 0 : PHP_INT_MIN) | 0b0111111111110000000000000000000000000000000000000000000000000000 | ($payload << (52 - 23));
- $result = unpack('d', pack('q', $j));
- assert(isset($result[1]) && is_float($result[1]));
- return $result[1];
- } else {
- $result = unpack('g', $buf);
- assert(isset($result[1]) && is_float($result[1]));
- return $result[1];
- }
+ return BinaryConversion::deserializeF32($this->stream->read(4));
}
/**
@@ -1116,13 +1098,7 @@ final class Decoder
*/
private function decodeF64(): float
{
- $buf = $this->stream->read(8);
- $result = unpack('e', $buf);
- if ($result === false) {
- throw new InvalidBinaryFormatException("f64");
- }
- assert(isset($result[1]) && is_float($result[1]));
- return $result[1];
+ return BinaryConversion::deserializeF64($this->stream->read(8));
}
/**