aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/BitOps/FloatOps.php
blob: cb6cbc45b164cdc3517d9dd887efcce26117c4e2 (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
<?php

declare(strict_types=1);

namespace Nsfisis\Waddiwasi\BitOps;

final readonly class FloatOps
{
    private function __construct()
    {
    }

    /**
     * @return array{int, int, int}
     */
    public static function destructF64Bits(float $x): array
    {
        $i = BinaryConversion::deserializeS64(BinaryConversion::serializeF64($x));
        return [
            $i & FloatTraits::F64_SIGN_MASK,
            $i & FloatTraits::F64_EXPONENT_MASK,
            $i & FloatTraits::F64_MANTISSA_MASK,
        ];
    }

    public static function constructNan(Signedness $sign, float $x): float
    {
        [, , $payload] = self::destructF64Bits($x);
        $i = 0 |
            FloatTraits::getF64SignBit($sign) |
            FloatTraits::F64_EXPONENT_NAN |
            $payload;
        return BinaryConversion::deserializeF64(BinaryConversion::serializeI64($i));
    }

    public static function getSignedness(float $x): Signedness
    {
        return self::destructF64Bits($x)[0] === 0 ? Signedness::Unsigned : Signedness::Signed;
    }
}