diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-04-05 23:26:25 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-04-06 02:03:32 +0900 |
| commit | 3d0f0b06ee25571034a13a43d2ca660ef687afa9 (patch) | |
| tree | 9d82454afbd8d26d41180db8718c863f9225e83f /src/WebAssembly/BinaryFormat/Decoder.php | |
| parent | 24d00a02f51138f1191c8b1f72ccdb14fc622b11 (diff) | |
| download | php-waddiwasi-3d0f0b06ee25571034a13a43d2ca660ef687afa9.tar.gz php-waddiwasi-3d0f0b06ee25571034a13a43d2ca660ef687afa9.tar.zst php-waddiwasi-3d0f0b06ee25571034a13a43d2ca660ef687afa9.zip | |
fix: pass ConversionsTest, FloatExprsTest, FloatLiteralsTest
Diffstat (limited to 'src/WebAssembly/BinaryFormat/Decoder.php')
| -rw-r--r-- | src/WebAssembly/BinaryFormat/Decoder.php | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/WebAssembly/BinaryFormat/Decoder.php b/src/WebAssembly/BinaryFormat/Decoder.php index e64fac8..cce3c5c 100644 --- a/src/WebAssembly/BinaryFormat/Decoder.php +++ b/src/WebAssembly/BinaryFormat/Decoder.php @@ -1091,12 +1091,24 @@ final class Decoder private function decodeF32(): float { $buf = $this->stream->read(4); - $result = unpack('g', $buf); + $result = unpack('V', $buf); if ($result === false) { throw new InvalidBinaryFormatException("f32"); } - assert(isset($result[1]) && is_float($result[1])); - return $result[1]; + 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]; + } } /** |
