|
22 | 22 | from typing import NoReturn, Tuple, Union
|
23 | 23 |
|
24 | 24 |
|
| 25 | +class _UInt32: |
| 26 | + def __init__(self, value: int) -> None: |
| 27 | + self._value: int = value & 0xFFFFFFFF |
| 28 | + |
| 29 | + @property |
| 30 | + def value(self) -> int: |
| 31 | + return self._value |
| 32 | + |
| 33 | + @value.setter |
| 34 | + def value(self, new_value: int) -> None: |
| 35 | + self._value = new_value & 0xFFFFFFFF |
| 36 | + |
| 37 | + def __add__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 38 | + result = self.value + (other.value if isinstance(other, _UInt32) else other) |
| 39 | + return _UInt32(result & 0xFFFFFFFF) |
| 40 | + |
| 41 | + def __sub__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 42 | + result = self.value - (other.value if isinstance(other, _UInt32) else other) |
| 43 | + return _UInt32(result & 0xFFFFFFFF) |
| 44 | + |
| 45 | + def __mul__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 46 | + result = self.value * (other.value if isinstance(other, _UInt32) else other) |
| 47 | + return _UInt32(result & 0xFFFFFFFF) |
| 48 | + |
| 49 | + def __xor__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 50 | + result = self.value ^ (other.value if isinstance(other, _UInt32) else other) |
| 51 | + return _UInt32(result & 0xFFFFFFFF) |
| 52 | + |
| 53 | + def __lshift__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 54 | + result = self.value << (other.value if isinstance(other, _UInt32) else other) |
| 55 | + return _UInt32(result & 0xFFFFFFFF) |
| 56 | + |
| 57 | + def __ilshift__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 58 | + self._value = (self.value << (other.value if isinstance(other, _UInt32) else other)) & 0xFFFFFFFF |
| 59 | + return self |
| 60 | + |
| 61 | + def __rshift__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 62 | + result = self.value >> (other.value if isinstance(other, _UInt32) else other) |
| 63 | + return _UInt32(result & 0xFFFFFFFF) |
| 64 | + |
| 65 | + def __irshift__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 66 | + self._value = (self.value >> (other.value if isinstance(other, _UInt32) else other)) & 0xFFFFFFFF |
| 67 | + return self |
| 68 | + |
| 69 | + def __and__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 70 | + result = self.value & (other.value if isinstance(other, _UInt32) else other) |
| 71 | + return _UInt32(result & 0xFFFFFFFF) |
| 72 | + |
| 73 | + def __or__(self, other: Union[int, '_UInt32']) -> '_UInt32': |
| 74 | + if isinstance(other, _UInt32): |
| 75 | + return _UInt32(self.value | other.value) |
| 76 | + if isinstance(other, int): |
| 77 | + return _UInt32(self.value | other) |
| 78 | + raise TypeError("Unsupported type for OR operation") |
| 79 | + |
| 80 | + def __invert__(self) -> '_UInt32': |
| 81 | + return _UInt32(~self.value & 0xFFFFFFFF) |
| 82 | + |
| 83 | + def __eq__(self, other: Union[int, '_UInt32', object]) -> bool: |
| 84 | + return self.value == (other.value if isinstance(other, _UInt32) else other) |
| 85 | + |
| 86 | + def __ne__(self, other: Union[int, '_UInt32', object]) -> bool: |
| 87 | + return not self.__eq__(other) |
| 88 | + |
| 89 | + def __lt__(self, other: Union[int, '_UInt32']) -> bool: |
| 90 | + return self.value < (other.value if isinstance(other, _UInt32) else other) |
| 91 | + |
| 92 | + def __gt__(self, other: Union[int, '_UInt32']) -> bool: |
| 93 | + return self.value > (other.value if isinstance(other, _UInt32) else other) |
| 94 | + |
| 95 | + def __le__(self, other: Union[int, '_UInt32']) -> bool: |
| 96 | + return self.value <= (other.value if isinstance(other, _UInt32) else other) |
| 97 | + |
| 98 | + def __ge__(self, other: Union[int, '_UInt32']) -> bool: |
| 99 | + return self.value >= (other.value if isinstance(other, _UInt32) else other) |
| 100 | + |
| 101 | + @staticmethod |
| 102 | + def encode_double_as_uint32(value: float) -> int: |
| 103 | + value_in_uint32 = struct.unpack('<I', struct.pack('<f', value))[0] |
| 104 | + mask = 0x80000000 |
| 105 | + return (value_in_uint32 ^ mask) if value_in_uint32 < mask else (~value_in_uint32) + 1 |
| 106 | + |
| 107 | + @staticmethod |
| 108 | + def decode_double_from_uint32(value: int) -> int: |
| 109 | + mask = 0x80000000 |
| 110 | + value = ~(value - 1) if value < mask else value ^ mask |
| 111 | + return struct.unpack('<f', struct.pack('<I', value))[0] |
| 112 | + |
| 113 | + def __int__(self) -> int: |
| 114 | + return self.value |
| 115 | + |
25 | 116 | class _UInt64:
|
26 | 117 | def __init__(self, value: int) -> None:
|
27 | 118 | self._value: int = value & 0xFFFFFFFFFFFFFFFF
|
@@ -72,6 +163,32 @@ def __or__(self, other: Union[int, '_UInt64']) -> '_UInt64':
|
72 | 163 | def __invert__(self) -> '_UInt64':
|
73 | 164 | return _UInt64(~self.value & 0xFFFFFFFFFFFFFFFF)
|
74 | 165 |
|
| 166 | + def __eq__(self, other: Union[int, '_UInt64', object]) -> bool: |
| 167 | + return self.value == (other.value if isinstance(other, _UInt64) else other) |
| 168 | + |
| 169 | + def __ne__(self, other: Union[int, '_UInt64', object]) -> bool: |
| 170 | + return not self.__eq__(other) |
| 171 | + |
| 172 | + def __irshift__(self, other: Union[int, '_UInt64']) -> '_UInt64': |
| 173 | + self._value = (self.value >> (other.value if isinstance(other, _UInt64) else other)) & 0xFFFFFFFFFFFFFFFF |
| 174 | + return self |
| 175 | + |
| 176 | + def __ilshift__(self, other: Union[int, '_UInt64']) -> '_UInt64': |
| 177 | + self._value = (self.value << (other.value if isinstance(other, _UInt64) else other)) & 0xFFFFFFFFFFFFFFFF |
| 178 | + return self |
| 179 | + |
| 180 | + def __lt__(self, other: Union[int, '_UInt64']) -> bool: |
| 181 | + return self.value < (other.value if isinstance(other, _UInt64) else other) |
| 182 | + |
| 183 | + def __gt__(self, other: Union[int, '_UInt64']) -> bool: |
| 184 | + return self.value > (other.value if isinstance(other, _UInt64) else other) |
| 185 | + |
| 186 | + def __le__(self, other: Union[int, '_UInt64']) -> bool: |
| 187 | + return self.value <= (other.value if isinstance(other, _UInt64) else other) |
| 188 | + |
| 189 | + def __ge__(self, other: Union[int, '_UInt64']) -> bool: |
| 190 | + return self.value >= (other.value if isinstance(other, _UInt64) else other) |
| 191 | + |
75 | 192 | @staticmethod
|
76 | 193 | def encode_double_as_uint64(value: float) -> int:
|
77 | 194 | value_in_uint64 = struct.unpack('<Q', struct.pack('<d', value))[0]
|
|
0 commit comments