bitstring 4.2.2__py3-none-any.whl → 4.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bitstring/__init__.py +33 -14
- bitstring/__main__.py +1 -1
- bitstring/array_.py +14 -12
- bitstring/bitarray_.py +3 -4
- bitstring/bits.py +21 -23
- bitstring/bitstore.py +35 -21
- bitstring/bitstore_helpers.py +11 -1
- bitstring/bitstream.py +1 -1
- bitstring/bitstring_options.py +2 -1
- bitstring/dtypes.py +3 -4
- bitstring/exceptions.py +0 -1
- bitstring/luts.py +128 -131
- bitstring/mxfp.py +19 -13
- bitstring/utils.py +14 -6
- {bitstring-4.2.2.dist-info → bitstring-4.3.0.dist-info}/LICENSE +1 -1
- {bitstring-4.2.2.dist-info → bitstring-4.3.0.dist-info}/METADATA +63 -76
- bitstring-4.3.0.dist-info/RECORD +22 -0
- {bitstring-4.2.2.dist-info → bitstring-4.3.0.dist-info}/WHEEL +1 -1
- bitstring-4.2.2.dist-info/RECORD +0 -22
- {bitstring-4.2.2.dist-info → bitstring-4.3.0.dist-info}/top_level.txt +0 -0
bitstring/__init__.py
CHANGED
|
@@ -55,7 +55,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
55
55
|
THE SOFTWARE.
|
|
56
56
|
"""
|
|
57
57
|
|
|
58
|
-
__version__ = "4.
|
|
58
|
+
__version__ = "4.3.0"
|
|
59
59
|
|
|
60
60
|
__author__ = "Scott Griffiths"
|
|
61
61
|
|
|
@@ -111,31 +111,38 @@ class _MyModuleType(types.ModuleType):
|
|
|
111
111
|
|
|
112
112
|
sys.modules[__name__].__class__ = _MyModuleType
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
|
|
115
|
+
# These methods convert a bit length to the number of characters needed to print it for different interpretations.
|
|
115
116
|
def hex_bits2chars(bitlength: int):
|
|
116
117
|
# One character for every 4 bits
|
|
117
118
|
return bitlength // 4
|
|
118
119
|
|
|
120
|
+
|
|
119
121
|
def oct_bits2chars(bitlength: int):
|
|
120
122
|
# One character for every 3 bits
|
|
121
123
|
return bitlength // 3
|
|
122
124
|
|
|
125
|
+
|
|
123
126
|
def bin_bits2chars(bitlength: int):
|
|
124
127
|
# One character for each bit
|
|
125
128
|
return bitlength
|
|
126
129
|
|
|
130
|
+
|
|
127
131
|
def bytes_bits2chars(bitlength: int):
|
|
128
132
|
# One character for every 8 bits
|
|
129
133
|
return bitlength // 8
|
|
130
134
|
|
|
135
|
+
|
|
131
136
|
def uint_bits2chars(bitlength: int):
|
|
132
137
|
# How many characters is largest possible int of this length?
|
|
133
138
|
return len(str((1 << bitlength) - 1))
|
|
134
139
|
|
|
140
|
+
|
|
135
141
|
def int_bits2chars(bitlength: int):
|
|
136
142
|
# How many characters is largest negative int of this length? (To include minus sign).
|
|
137
143
|
return len(str((-1 << (bitlength - 1))))
|
|
138
144
|
|
|
145
|
+
|
|
139
146
|
def float_bits2chars(bitlength: Literal[16, 32, 64]):
|
|
140
147
|
# These bit lengths were found by looking at lots of possible values
|
|
141
148
|
if bitlength in [16, 32]:
|
|
@@ -143,53 +150,65 @@ def float_bits2chars(bitlength: Literal[16, 32, 64]):
|
|
|
143
150
|
else:
|
|
144
151
|
return 24 # Empirical value
|
|
145
152
|
|
|
146
|
-
|
|
153
|
+
|
|
154
|
+
def p3binary_bits2chars(_: Literal[8]):
|
|
147
155
|
return 19 # Empirical value
|
|
148
156
|
|
|
149
|
-
|
|
157
|
+
|
|
158
|
+
def p4binary_bits2chars(_: Literal[8]):
|
|
150
159
|
# Found by looking at all the possible values
|
|
151
160
|
return 13 # Empirical value
|
|
152
161
|
|
|
153
|
-
|
|
162
|
+
|
|
163
|
+
def e4m3mxfp_bits2chars(_: Literal[8]):
|
|
154
164
|
return 13
|
|
155
165
|
|
|
156
|
-
|
|
166
|
+
|
|
167
|
+
def e5m2mxfp_bits2chars(_: Literal[8]):
|
|
157
168
|
return 19
|
|
158
169
|
|
|
159
|
-
|
|
170
|
+
|
|
171
|
+
def e3m2mxfp_bits2chars(_: Literal[6]):
|
|
160
172
|
# Not sure what the best value is here. It's 7 without considering the scale that could be applied.
|
|
161
173
|
return 7
|
|
162
174
|
|
|
163
|
-
|
|
175
|
+
|
|
176
|
+
def e2m3mxfp_bits2chars(_: Literal[6]):
|
|
164
177
|
# Not sure what the best value is here.
|
|
165
178
|
return 7
|
|
166
179
|
|
|
167
|
-
|
|
180
|
+
|
|
181
|
+
def e2m1mxfp_bits2chars(_: Literal[4]):
|
|
168
182
|
# Not sure what the best value is here.
|
|
169
183
|
return 7
|
|
170
184
|
|
|
171
|
-
|
|
172
|
-
|
|
185
|
+
|
|
186
|
+
def e8m0mxfp_bits2chars(_: Literal[8]):
|
|
187
|
+
# Has same range as float32
|
|
173
188
|
return 23
|
|
174
189
|
|
|
175
|
-
|
|
190
|
+
|
|
191
|
+
def mxint_bits2chars(_: Literal[8]):
|
|
176
192
|
# Not sure what the best value is here.
|
|
177
193
|
return 10
|
|
178
194
|
|
|
179
195
|
|
|
180
|
-
def bfloat_bits2chars(
|
|
196
|
+
def bfloat_bits2chars(_: Literal[16]):
|
|
181
197
|
# Found by looking at all the possible values
|
|
182
198
|
return 23 # Empirical value
|
|
183
199
|
|
|
200
|
+
|
|
184
201
|
def bits_bits2chars(bitlength: int):
|
|
185
202
|
# For bits type we can see how long it needs to be printed by trying any value
|
|
186
203
|
temp = Bits(bitlength)
|
|
187
204
|
return len(str(temp))
|
|
188
205
|
|
|
189
|
-
|
|
206
|
+
|
|
207
|
+
def bool_bits2chars(_: Literal[1]):
|
|
190
208
|
# Bools are printed as 1 or 0, not True or False, so are one character each
|
|
191
209
|
return 1
|
|
192
210
|
|
|
211
|
+
|
|
193
212
|
dtype_definitions = [
|
|
194
213
|
# Integer types
|
|
195
214
|
DtypeDefinition('uint', Bits._setuint, Bits._getuint, int, False, uint_bits2chars,
|
bitstring/__main__.py
CHANGED
bitstring/array_.py
CHANGED
|
@@ -93,6 +93,7 @@ class Array:
|
|
|
93
93
|
self.data += BitArray._create_from_bitstype(trailing_bits)
|
|
94
94
|
|
|
95
95
|
_largest_values = None
|
|
96
|
+
|
|
96
97
|
@staticmethod
|
|
97
98
|
def _calculate_auto_scale(initializer, name: str, length: Optional[int]) -> float:
|
|
98
99
|
# Now need to find the largest power of 2 representable with this format.
|
|
@@ -112,12 +113,15 @@ class Array:
|
|
|
112
113
|
}
|
|
113
114
|
if f'{name}{length}' in Array._largest_values.keys():
|
|
114
115
|
float_values = Array('float64', initializer).tolist()
|
|
116
|
+
if not float_values:
|
|
117
|
+
raise ValueError("Can't calculate an 'auto' scale with an empty Array initializer.")
|
|
115
118
|
max_float_value = max(abs(x) for x in float_values)
|
|
116
119
|
if max_float_value == 0:
|
|
117
120
|
# This special case isn't covered in the standard. I'm choosing to return no scale.
|
|
118
121
|
return 1.0
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
# We need to find the largest power of 2 that is less than the max value
|
|
123
|
+
log2 = math.floor(math.log2(max_float_value))
|
|
124
|
+
lp2 = math.floor(math.log2(Array._largest_values[f'{name}{length}']))
|
|
121
125
|
lg_scale = log2 - lp2
|
|
122
126
|
# Saturate at values representable in E8M0 format.
|
|
123
127
|
if lg_scale > 127:
|
|
@@ -155,7 +159,7 @@ class Array:
|
|
|
155
159
|
except ValueError:
|
|
156
160
|
name_length = utils.parse_single_struct_token(new_dtype)
|
|
157
161
|
if name_length is not None:
|
|
158
|
-
dtype = Dtype(
|
|
162
|
+
dtype = Dtype(name_length[0], name_length[1])
|
|
159
163
|
else:
|
|
160
164
|
raise ValueError(f"Inappropriate Dtype for Array: '{new_dtype}'.")
|
|
161
165
|
if dtype.length is None:
|
|
@@ -270,7 +274,7 @@ class Array:
|
|
|
270
274
|
return new_array
|
|
271
275
|
|
|
272
276
|
def tolist(self) -> List[ElementType]:
|
|
273
|
-
return
|
|
277
|
+
return [self._dtype.read_fn(self.data, start=start)
|
|
274
278
|
for start in range(0, len(self.data) - self._dtype.length + 1, self._dtype.length)]
|
|
275
279
|
|
|
276
280
|
def append(self, x: ElementType) -> None:
|
|
@@ -408,9 +412,11 @@ class Array:
|
|
|
408
412
|
token_list = utils.preprocess_tokens(fmt)
|
|
409
413
|
if len(token_list) not in [1, 2]:
|
|
410
414
|
raise ValueError(f"Only one or two tokens can be used in an Array.pp() format - '{fmt}' has {len(token_list)} tokens.")
|
|
411
|
-
|
|
415
|
+
name1, length1 = utils.parse_name_length_token(token_list[0])
|
|
416
|
+
dtype1 = Dtype(name1, length1)
|
|
412
417
|
if len(token_list) == 2:
|
|
413
|
-
|
|
418
|
+
name2, length2 = utils.parse_name_length_token(token_list[1])
|
|
419
|
+
dtype2 = Dtype(name2, length2)
|
|
414
420
|
|
|
415
421
|
token_length = dtype1.bitlength
|
|
416
422
|
if dtype2 is not None:
|
|
@@ -754,12 +760,8 @@ class Array:
|
|
|
754
760
|
def _eq_ne(self, op, other: Any) -> Array:
|
|
755
761
|
if isinstance(other, (int, float, str, Bits)):
|
|
756
762
|
return self._apply_op_to_all_elements(op, other, is_comparison=True)
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
except:
|
|
760
|
-
return NotImplemented
|
|
761
|
-
finally:
|
|
762
|
-
return self._apply_op_between_arrays(op, other, is_comparison=True)
|
|
763
|
+
other = self.__class__(self.dtype, other)
|
|
764
|
+
return self._apply_op_between_arrays(op, other, is_comparison=True)
|
|
763
765
|
|
|
764
766
|
def __eq__(self, other: Any) -> Array:
|
|
765
767
|
return self._eq_ne(operator.eq, other)
|
bitstring/bitarray_.py
CHANGED
|
@@ -11,6 +11,7 @@ from bitstring.bits import Bits, BitsType, TBits
|
|
|
11
11
|
|
|
12
12
|
import bitstring.dtypes
|
|
13
13
|
|
|
14
|
+
|
|
14
15
|
class BitArray(Bits):
|
|
15
16
|
"""A container holding a mutable sequence of bits.
|
|
16
17
|
|
|
@@ -71,7 +72,6 @@ class BitArray(Bits):
|
|
|
71
72
|
# As BitArray objects are mutable, we shouldn't allow them to be hashed.
|
|
72
73
|
__hash__: None = None
|
|
73
74
|
|
|
74
|
-
|
|
75
75
|
def __init__(self, auto: Optional[Union[BitsType, int]] = None, /, length: Optional[int] = None,
|
|
76
76
|
offset: Optional[int] = None, **kwargs) -> None:
|
|
77
77
|
"""Either specify an 'auto' initialiser:
|
|
@@ -172,6 +172,7 @@ class BitArray(Bits):
|
|
|
172
172
|
|
|
173
173
|
def _setitem_slice(self, key: slice, value: BitsType) -> None:
|
|
174
174
|
if isinstance(value, numbers.Integral):
|
|
175
|
+
value = int(value)
|
|
175
176
|
if key.step not in [None, -1, 1]:
|
|
176
177
|
if value in [0, 1]:
|
|
177
178
|
self.set(value, range(*key.indices(len(self))))
|
|
@@ -195,7 +196,7 @@ class BitArray(Bits):
|
|
|
195
196
|
|
|
196
197
|
def __setitem__(self, key: Union[slice, int], value: BitsType) -> None:
|
|
197
198
|
if isinstance(key, numbers.Integral):
|
|
198
|
-
self._setitem_int(key, value)
|
|
199
|
+
self._setitem_int(int(key), value)
|
|
199
200
|
else:
|
|
200
201
|
self._setitem_slice(key, value)
|
|
201
202
|
|
|
@@ -573,5 +574,3 @@ class BitArray(Bits):
|
|
|
573
574
|
def clear(self) -> None:
|
|
574
575
|
"""Remove all bits, reset to zero length."""
|
|
575
576
|
self._clear()
|
|
576
|
-
|
|
577
|
-
|
bitstring/bits.py
CHANGED
|
@@ -231,13 +231,8 @@ class Bits:
|
|
|
231
231
|
def __getitem__(self: TBits, key: Union[slice, int], /) -> Union[TBits, bool]:
|
|
232
232
|
"""Return a new bitstring representing a slice of the current bitstring.
|
|
233
233
|
|
|
234
|
-
|
|
235
|
-
Stepping is used to specify the number of bits in each item.
|
|
236
|
-
|
|
237
|
-
>>> print(BitArray('0b00110')[1:4])
|
|
234
|
+
>>> print(Bits('0b00110')[1:4])
|
|
238
235
|
'0b011'
|
|
239
|
-
>>> print(BitArray('0x00112233')[1:3:8])
|
|
240
|
-
'0x1122'
|
|
241
236
|
|
|
242
237
|
"""
|
|
243
238
|
if isinstance(key, numbers.Integral):
|
|
@@ -1699,34 +1694,37 @@ class Bits:
|
|
|
1699
1694
|
|
|
1700
1695
|
@staticmethod
|
|
1701
1696
|
def _process_pp_tokens(token_list, fmt):
|
|
1697
|
+
if len(token_list) not in [1, 2]:
|
|
1698
|
+
raise ValueError(
|
|
1699
|
+
f"Only one or two tokens can be used in an pp() format - '{fmt}' has {len(token_list)} tokens.")
|
|
1702
1700
|
has_length_in_fmt = True
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1701
|
+
name1, length1 = utils.parse_name_length_token(token_list[0])
|
|
1702
|
+
dtype1 = Dtype(name1, length1)
|
|
1703
|
+
bits_per_group = dtype1.bitlength
|
|
1704
|
+
dtype2 = None
|
|
1705
|
+
|
|
1706
|
+
if len(token_list) == 2:
|
|
1707
|
+
name2, length2 = utils.parse_name_length_token(token_list[1])
|
|
1708
|
+
dtype2 = Dtype(name2, length2)
|
|
1709
|
+
if None not in {dtype1.bitlength, dtype2.bitlength} and dtype1.bitlength != dtype2.bitlength:
|
|
1710
|
+
raise ValueError(
|
|
1711
|
+
f"Differing bit lengths of {dtype1.bitlength} and {dtype2.bitlength} in format string '{fmt}'.")
|
|
1707
1712
|
if bits_per_group is None:
|
|
1708
|
-
|
|
1713
|
+
bits_per_group = dtype2.bitlength
|
|
1714
|
+
|
|
1715
|
+
if bits_per_group is None:
|
|
1716
|
+
has_length_in_fmt = False
|
|
1717
|
+
if len(token_list) == 1:
|
|
1709
1718
|
bits_per_group = {'bin': 8, 'hex': 8, 'oct': 12, 'bytes': 32}.get(dtype1.name)
|
|
1710
1719
|
if bits_per_group is None:
|
|
1711
1720
|
raise ValueError(f"No length or default length available for pp() format '{fmt}'.")
|
|
1712
|
-
|
|
1713
|
-
dtype1 = Dtype(*utils.parse_name_length_token(token_list[0]))
|
|
1714
|
-
dtype2 = Dtype(*utils.parse_name_length_token(token_list[1]))
|
|
1715
|
-
if dtype1.bitlength is not None and dtype2.bitlength is not None and dtype1.bitlength != dtype2.bitlength:
|
|
1716
|
-
raise ValueError(
|
|
1717
|
-
f"Differing bit lengths of {dtype1.bitlength} and {dtype2.bitlength} in format string '{fmt}'.")
|
|
1718
|
-
bits_per_group = dtype1.bitlength if dtype1.bitlength is not None else dtype2.bitlength
|
|
1719
|
-
if bits_per_group is None:
|
|
1720
|
-
has_length_in_fmt = False
|
|
1721
|
+
else:
|
|
1721
1722
|
try:
|
|
1722
1723
|
bits_per_group = 2 * Bits._bits_per_char(dtype1.name) * Bits._bits_per_char(dtype2.name)
|
|
1723
1724
|
except ValueError:
|
|
1724
1725
|
raise ValueError(f"Can't find a default bitlength to use for pp() format '{fmt}'.")
|
|
1725
1726
|
if bits_per_group >= 24:
|
|
1726
1727
|
bits_per_group //= 2
|
|
1727
|
-
else:
|
|
1728
|
-
raise ValueError(
|
|
1729
|
-
f"Only one or two tokens can be used in an pp() format - '{fmt}' has {len(token_list)} tokens.")
|
|
1730
1728
|
return dtype1, dtype2, bits_per_group, has_length_in_fmt
|
|
1731
1729
|
|
|
1732
1730
|
def pp(self, fmt: Optional[str] = None, width: int = 120, sep: str = ' ',
|
bitstring/bitstore.py
CHANGED
|
@@ -4,23 +4,38 @@ import bitarray
|
|
|
4
4
|
from bitstring.exceptions import CreationError
|
|
5
5
|
from typing import Union, Iterable, Optional, overload, Iterator, Any
|
|
6
6
|
|
|
7
|
+
if bitarray.__version__.startswith("2."):
|
|
8
|
+
raise ImportError(f"bitstring version 4.3 requires bitarray version 3 or higher. Found version {bitarray.__version__}.")
|
|
9
|
+
|
|
10
|
+
def indices(s: slice, length: int) -> tuple[int, int | None, int]:
|
|
11
|
+
"""A better implementation of slice.indices such that a
|
|
12
|
+
slice made from [start:stop:step] will actually equal the original slice."""
|
|
13
|
+
if s.step is None or s.step > 0:
|
|
14
|
+
return s.indices(length)
|
|
15
|
+
assert s.step < 0
|
|
16
|
+
start, stop, step = s.indices(length)
|
|
17
|
+
if stop < 0:
|
|
18
|
+
stop = None
|
|
19
|
+
return start, stop, step
|
|
7
20
|
|
|
8
21
|
def offset_slice_indices_lsb0(key: slice, length: int) -> slice:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
start, stop, step = indices(key, length)
|
|
23
|
+
if step is not None and step < 0:
|
|
24
|
+
if stop is None:
|
|
25
|
+
new_start = start + 1
|
|
26
|
+
new_stop = None
|
|
27
|
+
else:
|
|
28
|
+
first_element = start
|
|
29
|
+
last_element = start + ((stop + 1 - start) // step) * step
|
|
30
|
+
new_start = length - last_element
|
|
31
|
+
new_stop = length - first_element - 1
|
|
32
|
+
else:
|
|
33
|
+
first_element = start
|
|
34
|
+
# The last element will usually be stop - 1, but needs to be adjusted if step != 1.
|
|
35
|
+
last_element = start + ((stop - 1 - start) // step) * step
|
|
36
|
+
new_start = length - last_element - 1
|
|
37
|
+
new_stop = length - first_element
|
|
38
|
+
return slice(new_start, new_stop, key.step)
|
|
24
39
|
|
|
25
40
|
|
|
26
41
|
class BitStore:
|
|
@@ -149,7 +164,7 @@ class BitStore:
|
|
|
149
164
|
byte_pos = byte_pos + 1
|
|
150
165
|
return
|
|
151
166
|
# General case
|
|
152
|
-
i = self._bitarray.
|
|
167
|
+
i = self._bitarray.search(bs._bitarray, start, end)
|
|
153
168
|
if not bytealigned:
|
|
154
169
|
for p in i:
|
|
155
170
|
yield p
|
|
@@ -159,7 +174,7 @@ class BitStore:
|
|
|
159
174
|
yield p
|
|
160
175
|
|
|
161
176
|
def rfindall_msb0(self, bs: BitStore, start: int, end: int, bytealigned: bool = False) -> Iterator[int]:
|
|
162
|
-
i = self._bitarray.
|
|
177
|
+
i = self._bitarray.search(bs._bitarray, start, end, right=True)
|
|
163
178
|
if not bytealigned:
|
|
164
179
|
for p in i:
|
|
165
180
|
yield p
|
|
@@ -212,13 +227,12 @@ class BitStore:
|
|
|
212
227
|
return BitStore(self._bitarray[start:stop])
|
|
213
228
|
|
|
214
229
|
def getslice_lsb0(self, start: Optional[int], stop: Optional[int], /) -> BitStore:
|
|
215
|
-
|
|
216
|
-
return BitStore(self._bitarray[start:stop])
|
|
230
|
+
s = offset_slice_indices_lsb0(slice(start, stop, None), len(self))
|
|
231
|
+
return BitStore(self._bitarray[s.start:s.stop])
|
|
217
232
|
|
|
218
233
|
def getindex_lsb0(self, index: int, /) -> bool:
|
|
219
234
|
return bool(self._bitarray.__getitem__(-index - 1))
|
|
220
235
|
|
|
221
|
-
|
|
222
236
|
@overload
|
|
223
237
|
def setitem_lsb0(self, key: int, value: int, /) -> None:
|
|
224
238
|
...
|
|
@@ -269,4 +283,4 @@ class BitStore:
|
|
|
269
283
|
self._bitarray.__setitem__(key, value)
|
|
270
284
|
|
|
271
285
|
def delitem_msb0(self, key, /):
|
|
272
|
-
self._bitarray.__delitem__(key)
|
|
286
|
+
self._bitarray.__delitem__(key)
|
bitstring/bitstore_helpers.py
CHANGED
|
@@ -8,7 +8,8 @@ import bitarray
|
|
|
8
8
|
from bitstring.bitstore import BitStore
|
|
9
9
|
import bitstring
|
|
10
10
|
from bitstring.fp8 import p4binary_fmt, p3binary_fmt
|
|
11
|
-
from bitstring.mxfp import e3m2mxfp_fmt, e2m3mxfp_fmt, e2m1mxfp_fmt, e4m3mxfp_saturate_fmt,
|
|
11
|
+
from bitstring.mxfp import (e3m2mxfp_fmt, e2m3mxfp_fmt, e2m1mxfp_fmt, e4m3mxfp_saturate_fmt,
|
|
12
|
+
e5m2mxfp_saturate_fmt, e4m3mxfp_overflow_fmt, e5m2mxfp_overflow_fmt)
|
|
12
13
|
|
|
13
14
|
# The size of various caches used to improve performance
|
|
14
15
|
CACHE_SIZE = 256
|
|
@@ -121,11 +122,13 @@ def p4binary2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
121
122
|
u = p4binary_fmt.float_to_int8(f)
|
|
122
123
|
return int2bitstore(u, 8, False)
|
|
123
124
|
|
|
125
|
+
|
|
124
126
|
def p3binary2bitstore(f: Union[str, float]) -> BitStore:
|
|
125
127
|
f = float(f)
|
|
126
128
|
u = p3binary_fmt.float_to_int8(f)
|
|
127
129
|
return int2bitstore(u, 8, False)
|
|
128
130
|
|
|
131
|
+
|
|
129
132
|
def e4m3mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
130
133
|
f = float(f)
|
|
131
134
|
if bitstring.options.mxfp_overflow == 'saturate':
|
|
@@ -134,6 +137,7 @@ def e4m3mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
134
137
|
u = e4m3mxfp_overflow_fmt.float_to_int(f)
|
|
135
138
|
return int2bitstore(u, 8, False)
|
|
136
139
|
|
|
140
|
+
|
|
137
141
|
def e5m2mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
138
142
|
f = float(f)
|
|
139
143
|
if bitstring.options.mxfp_overflow == 'saturate':
|
|
@@ -142,6 +146,7 @@ def e5m2mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
142
146
|
u = e5m2mxfp_overflow_fmt.float_to_int(f)
|
|
143
147
|
return int2bitstore(u, 8, False)
|
|
144
148
|
|
|
149
|
+
|
|
145
150
|
def e3m2mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
146
151
|
f = float(f)
|
|
147
152
|
if math.isnan(f):
|
|
@@ -149,6 +154,7 @@ def e3m2mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
149
154
|
u = e3m2mxfp_fmt.float_to_int(f)
|
|
150
155
|
return int2bitstore(u, 6, False)
|
|
151
156
|
|
|
157
|
+
|
|
152
158
|
def e2m3mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
153
159
|
f = float(f)
|
|
154
160
|
if math.isnan(f):
|
|
@@ -156,6 +162,7 @@ def e2m3mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
156
162
|
u = e2m3mxfp_fmt.float_to_int(f)
|
|
157
163
|
return int2bitstore(u, 6, False)
|
|
158
164
|
|
|
165
|
+
|
|
159
166
|
def e2m1mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
160
167
|
f = float(f)
|
|
161
168
|
if math.isnan(f):
|
|
@@ -165,6 +172,8 @@ def e2m1mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
165
172
|
|
|
166
173
|
|
|
167
174
|
e8m0mxfp_allowed_values = [float(2 ** x) for x in range(-127, 128)]
|
|
175
|
+
|
|
176
|
+
|
|
168
177
|
def e8m0mxfp2bitstore(f: Union[str, float]) -> BitStore:
|
|
169
178
|
f = float(f)
|
|
170
179
|
if math.isnan(f):
|
|
@@ -199,6 +208,7 @@ def mxint2bitstore(f: Union[str, float]) -> BitStore:
|
|
|
199
208
|
i += 1
|
|
200
209
|
return int2bitstore(i, 8, True)
|
|
201
210
|
|
|
211
|
+
|
|
202
212
|
def int2bitstore(i: int, length: int, signed: bool) -> BitStore:
|
|
203
213
|
i = int(i)
|
|
204
214
|
try:
|
bitstring/bitstream.py
CHANGED
|
@@ -57,7 +57,7 @@ class ConstBitStream(Bits):
|
|
|
57
57
|
pos -- The current bit position in the bitstring.
|
|
58
58
|
"""
|
|
59
59
|
|
|
60
|
-
__slots__ = ('_pos')
|
|
60
|
+
__slots__ = ('_pos',)
|
|
61
61
|
|
|
62
62
|
def __init__(self, auto: Optional[Union[BitsType, int]] = None, /, length: Optional[int] = None,
|
|
63
63
|
offset: Optional[int] = None, pos: int = 0, **kwargs) -> None:
|
bitstring/bitstring_options.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import bitstring
|
|
4
4
|
import os
|
|
5
5
|
|
|
6
|
+
|
|
6
7
|
class Options:
|
|
7
8
|
"""Internal class to create singleton module options instance."""
|
|
8
9
|
|
|
@@ -91,4 +92,4 @@ class Colour:
|
|
|
91
92
|
cls.off = '\033[0m'
|
|
92
93
|
else:
|
|
93
94
|
cls.blue = cls.purple = cls.green = cls.off = ''
|
|
94
|
-
return x
|
|
95
|
+
return x
|
bitstring/dtypes.py
CHANGED
|
@@ -55,7 +55,6 @@ class Dtype:
|
|
|
55
55
|
_length: Optional[int]
|
|
56
56
|
_scale: Union[None, float, int]
|
|
57
57
|
|
|
58
|
-
|
|
59
58
|
def __new__(cls, token: Union[str, Dtype], /, length: Optional[int] = None, scale: Union[None, float, int] = None) -> Dtype:
|
|
60
59
|
if isinstance(token, cls):
|
|
61
60
|
return token
|
|
@@ -253,7 +252,7 @@ class DtypeDefinition:
|
|
|
253
252
|
"""Represents a class of dtypes, such as uint or float, rather than a concrete dtype such as uint8.
|
|
254
253
|
Not (yet) part of the public interface."""
|
|
255
254
|
|
|
256
|
-
def __init__(self, name: str, set_fn, get_fn, return_type: Any = Any, is_signed: bool = False, bitlength2chars_fn
|
|
255
|
+
def __init__(self, name: str, set_fn, get_fn, return_type: Any = Any, is_signed: bool = False, bitlength2chars_fn=None,
|
|
257
256
|
variable_length: bool = False, allowed_lengths: Tuple[int, ...] = tuple(), multiplier: int = 1, description: str = ''):
|
|
258
257
|
|
|
259
258
|
# Consistency checks
|
|
@@ -277,7 +276,7 @@ class DtypeDefinition:
|
|
|
277
276
|
self.set_fn_needs_length = set_fn is not None and 'length' in inspect.signature(set_fn).parameters
|
|
278
277
|
self.set_fn = set_fn
|
|
279
278
|
|
|
280
|
-
if self.allowed_lengths:
|
|
279
|
+
if self.allowed_lengths.values:
|
|
281
280
|
def allowed_length_checked_get_fn(bs):
|
|
282
281
|
if len(bs) not in self.allowed_lengths:
|
|
283
282
|
if self.allowed_lengths.only_one_value():
|
|
@@ -401,4 +400,4 @@ class Register:
|
|
|
401
400
|
|
|
402
401
|
|
|
403
402
|
# Create the Register singleton
|
|
404
|
-
dtype_register = Register()
|
|
403
|
+
dtype_register = Register()
|
bitstring/exceptions.py
CHANGED