aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Utility/BinaryConversion.php
blob: 0ca69271a15950deb80fd405279d72d9ea9c30fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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];
    }
}