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

declare(strict_types=1);

namespace Nsfisis\Waddiwasi\BitOps;

final readonly class FloatTraits
{
    public const int F32_EXPONENT_BITS = 8;
    public const int F32_MANTISSA_BITS = 23;

    public const int F32_SIGN_MASK = 0b10000000_00000000_00000000_00000000;
    public const int F32_EXPONENT_MASK = 0b01111111_10000000_00000000_00000000;
    public const int F32_MANTISSA_MASK = 0b00000000_01111111_11111111_11111111;

    public const int F32_SIGN_UNSIGNED = 0;
    public const int F32_SIGN_SIGNED = 0b10000000_00000000_00000000_00000000;
    public const int F32_EXPONENT_NAN = 0b01111111_10000000_00000000_00000000;

    public const int F32_INFINITY_BITS = 0b01111111_10000000_00000000_00000000;

    public const int F64_EXPONENT_BITS = 11;
    public const int F64_MANTISSA_BITS = 52;

    public const int F64_SIGN_MASK = PHP_INT_MIN;
    public const int F64_EXPONENT_MASK = 0b01111111_11110000_00000000_00000000_00000000_00000000_00000000_00000000;
    public const int F64_MANTISSA_MASK = 0b00000000_00001111_11111111_11111111_11111111_11111111_11111111_11111111;

    public const int F64_SIGN_UNSIGNED = 0;
    public const int F64_SIGN_SIGNED = PHP_INT_MIN;
    public const int F64_EXPONENT_NAN = 0b01111111_11110000_00000000_00000000_00000000_00000000_00000000_00000000;

    private function __construct()
    {
    }

    public static function getF32SignBit(Signedness $sign): int
    {
        return match ($sign) {
            Signedness::Unsigned => self::F32_SIGN_UNSIGNED,
            Signedness::Signed => self::F32_SIGN_SIGNED,
        };
    }

    public static function getF64SignBit(Signedness $sign): int
    {
        return match ($sign) {
            Signedness::Unsigned => self::F64_SIGN_UNSIGNED,
            Signedness::Signed => self::F64_SIGN_SIGNED,
        };
    }
}