diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Execution/Runtime.php | 41 | ||||
| -rw-r--r-- | src/Utility/BinaryConversion.php | 108 |
2 files changed, 117 insertions, 32 deletions
diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php index ae76b06..1f71303 100644 --- a/src/Execution/Runtime.php +++ b/src/Execution/Runtime.php @@ -18,6 +18,7 @@ use Nsfisis\Waddiwasi\Structure\Types\ResultType; use Nsfisis\Waddiwasi\Structure\Types\TableType; use Nsfisis\Waddiwasi\Structure\Types\ValType; use Nsfisis\Waddiwasi\Structure\Types\ValTypes; +use Nsfisis\Waddiwasi\Utility\BinaryConversion; use RuntimeException; use function abs; use function array_map; @@ -695,19 +696,13 @@ final class Runtime private function execInstrNumericF32ReinterpretI32(Instrs\Numeric\F32ReinterpretI32 $instr): void { $v = $this->stack->popInt(); - $result = unpack('f', pack('l', $v)); - assert($result !== false); - $vAsF32 = $result[1]; - $this->stack->pushValue($vAsF32); + $this->stack->pushValue(BinaryConversion::reinterpretI32AsF32($v)); } private function execInstrNumericF32ReinterpretI64(Instrs\Numeric\F32ReinterpretI64 $instr): void { $v = $this->stack->popInt(); - $result = unpack('f', pack('q', $v)); - assert($result !== false); - $vAsF32 = $result[1]; - $this->stack->pushValue($vAsF32); + $this->stack->pushValue(BinaryConversion::reinterpretI64AsF32($v)); } private function execInstrNumericF32Sqrt(Instrs\Numeric\F32Sqrt $instr): void @@ -881,19 +876,13 @@ final class Runtime private function execInstrNumericF64ReinterpretI32(Instrs\Numeric\F64ReinterpretI32 $instr): void { $v = $this->stack->popInt(); - $result = unpack('d', pack('l', $v)); - assert($result !== false); - $vAsF64 = $result[1]; - $this->stack->pushValue($vAsF64); + $this->stack->pushValue(BinaryConversion::reinterpretI32AsF64($v)); } private function execInstrNumericF64ReinterpretI64(Instrs\Numeric\F64ReinterpretI64 $instr): void { $v = $this->stack->popInt(); - $result = unpack('d', pack('q', $v)); - assert($result !== false); - $vAsF64 = $result[1]; - $this->stack->pushValue($vAsF64); + $this->stack->pushValue(BinaryConversion::reinterpretI64AsF64($v)); } private function execInstrNumericF64Sqrt(Instrs\Numeric\F64Sqrt $instr): void @@ -1105,19 +1094,13 @@ final class Runtime private function execInstrNumericI32ReinterpretF32(Instrs\Numeric\I32ReinterpretF32 $instr): void { $v = $this->stack->popFloat(); - $result = unpack('l', pack('f', $v)); - assert($result !== false); - $vAsI32 = $result[1]; - $this->stack->pushValue($vAsI32); + $this->stack->pushValue(BinaryConversion::reinterpretF32AsI32($v)); } private function execInstrNumericI32ReinterpretF64(Instrs\Numeric\I32ReinterpretF64 $instr): void { $v = $this->stack->popFloat(); - $result = unpack('q', pack('d', $v)); - assert($result !== false); - $vAsI64 = $result[1]; - $this->stack->pushValue(self::phpIntToWasmI32($vAsI64 & 0xFFFFFFFF)); + $this->stack->pushValue(BinaryConversion::reinterpretF64AsI32($v)); } private function execInstrNumericI32RemS(Instrs\Numeric\I32RemS $instr): void @@ -1523,19 +1506,13 @@ final class Runtime private function execInstrNumericI64ReinterpretF32(Instrs\Numeric\I64ReinterpretF32 $instr): void { $v = $this->stack->popFloat(); - $result = unpack('q', pack('f', $v)); - assert($result !== false); - $vAsI64 = $result[1]; - $this->stack->pushValue($vAsI64); + $this->stack->pushValue(BinaryConversion::reinterpretF32AsI64($v)); } private function execInstrNumericI64ReinterpretF64(Instrs\Numeric\I64ReinterpretF64 $instr): void { $v = $this->stack->popFloat(); - $result = unpack('q', pack('d', $v)); - assert($result !== false); - $vAsI64 = $result[1]; - $this->stack->pushValue($vAsI64); + $this->stack->pushValue(BinaryConversion::reinterpretF64AsI64($v)); } private function execInstrNumericI64RemS(Instrs\Numeric\I64RemS $instr): void diff --git a/src/Utility/BinaryConversion.php b/src/Utility/BinaryConversion.php new file mode 100644 index 0000000..0ca6927 --- /dev/null +++ b/src/Utility/BinaryConversion.php @@ -0,0 +1,108 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Utility; + +use InvalidArgumentException; +use function pack; +use function unpack; + +final readonly class BinaryConversion +{ + public static function reinterpretI32AsF32(int $x): float + { + return self::deserializeF32FromBytes(self::serializeI32ToBytes($x)); + } + + public static function reinterpretI64AsF32(int $x): float + { + return self::deserializeF32FromBytes(self::serializeI64ToBytes($x)); + } + + public static function reinterpretI32AsF64(int $x): float + { + return self::deserializeF64FromBytes(self::serializeI32ToBytes($x)); + } + + public static function reinterpretI64AsF64(int $x): float + { + return self::deserializeF64FromBytes(self::serializeI64ToBytes($x)); + } + + public static function reinterpretF32AsI32(float $x): int + { + return self::deserializeI32FromBytes(self::serializeF32ToBytes($x)); + } + + public static function reinterpretF64AsI32(float $x): int + { + return self::deserializeI32FromBytes(self::serializeF64ToBytes($x)); + } + + public static function reinterpretF32AsI64(float $x): int + { + return self::deserializeI64FromBytes(self::serializeF32ToBytes($x)); + } + + public static function reinterpretF64AsI64(float $x): int + { + return self::deserializeI64FromBytes(self::serializeF64ToBytes($x)); + } + + public static function serializeI32ToBytes(int $x): string + { + return pack('l', $x); + } + + public static function deserializeI32FromBytes(string $s): int + { + $result = unpack('l', $s); + if ($result === false) { + throw new InvalidArgumentException("Failed to unpack i32: $s"); + } + return $result[1]; + } + + public static function serializeI64ToBytes(int $x): string + { + return pack('q', $x); + } + + public static function deserializeI64FromBytes(string $s): int + { + $result = unpack('q', $s); + if ($result === false) { + throw new InvalidArgumentException("Failed to unpack i64: $s"); + } + return $result[1]; + } + + public static function serializeF32ToBytes(float $x): string + { + return pack('f', $x); + } + + public static function deserializeF32FromBytes(string $s): float + { + $result = unpack('f', $s); + if ($result === false) { + throw new InvalidArgumentException("Failed to unpack f32: $s"); + } + return $result[1]; + } + + public static function serializeF64ToBytes(float $x): string + { + return pack('d', $x); + } + + public static function deserializeF64FromBytes(string $s): float + { + $result = unpack('d', $s); + if ($result === false) { + throw new InvalidArgumentException("Failed to unpack f64: $s"); + } + return $result[1]; + } +} |
