bitstring 4.2.2__py3-none-any.whl → 4.2.3__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 +12 -6
- bitstring/bitarray_.py +3 -4
- bitstring/bits.py +20 -17
- bitstring/bitstore.py +3 -3
- 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 +8 -3
- {bitstring-4.2.2.dist-info → bitstring-4.2.3.dist-info}/METADATA +54 -68
- bitstring-4.2.3.dist-info/RECORD +22 -0
- bitstring-4.2.2.dist-info/RECORD +0 -22
- {bitstring-4.2.2.dist-info → bitstring-4.2.3.dist-info}/LICENSE +0 -0
- {bitstring-4.2.2.dist-info → bitstring-4.2.3.dist-info}/WHEEL +0 -0
- {bitstring-4.2.2.dist-info → bitstring-4.2.3.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.2.
|
|
58
|
+
__version__ = "4.2.3"
|
|
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:
|
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
|
@@ -1699,34 +1699,37 @@ class Bits:
|
|
|
1699
1699
|
|
|
1700
1700
|
@staticmethod
|
|
1701
1701
|
def _process_pp_tokens(token_list, fmt):
|
|
1702
|
+
if len(token_list) not in [1, 2]:
|
|
1703
|
+
raise ValueError(
|
|
1704
|
+
f"Only one or two tokens can be used in an pp() format - '{fmt}' has {len(token_list)} tokens.")
|
|
1702
1705
|
has_length_in_fmt = True
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1706
|
+
name1, length1 = utils.parse_name_length_token(token_list[0])
|
|
1707
|
+
dtype1 = Dtype(name1, length1)
|
|
1708
|
+
bits_per_group = dtype1.bitlength
|
|
1709
|
+
dtype2 = None
|
|
1710
|
+
|
|
1711
|
+
if len(token_list) == 2:
|
|
1712
|
+
name2, length2 = utils.parse_name_length_token(token_list[1])
|
|
1713
|
+
dtype2 = Dtype(name2, length2)
|
|
1714
|
+
if None not in {dtype1.bitlength, dtype2.bitlength} and dtype1.bitlength != dtype2.bitlength:
|
|
1715
|
+
raise ValueError(
|
|
1716
|
+
f"Differing bit lengths of {dtype1.bitlength} and {dtype2.bitlength} in format string '{fmt}'.")
|
|
1707
1717
|
if bits_per_group is None:
|
|
1708
|
-
|
|
1718
|
+
bits_per_group = dtype2.bitlength
|
|
1719
|
+
|
|
1720
|
+
if bits_per_group is None:
|
|
1721
|
+
has_length_in_fmt = False
|
|
1722
|
+
if len(token_list) == 1:
|
|
1709
1723
|
bits_per_group = {'bin': 8, 'hex': 8, 'oct': 12, 'bytes': 32}.get(dtype1.name)
|
|
1710
1724
|
if bits_per_group is None:
|
|
1711
1725
|
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
|
|
1726
|
+
else:
|
|
1721
1727
|
try:
|
|
1722
1728
|
bits_per_group = 2 * Bits._bits_per_char(dtype1.name) * Bits._bits_per_char(dtype2.name)
|
|
1723
1729
|
except ValueError:
|
|
1724
1730
|
raise ValueError(f"Can't find a default bitlength to use for pp() format '{fmt}'.")
|
|
1725
1731
|
if bits_per_group >= 24:
|
|
1726
1732
|
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
1733
|
return dtype1, dtype2, bits_per_group, has_length_in_fmt
|
|
1731
1734
|
|
|
1732
1735
|
def pp(self, fmt: Optional[str] = None, width: int = 120, sep: str = ' ',
|
bitstring/bitstore.py
CHANGED
|
@@ -14,7 +14,8 @@ def offset_slice_indices_lsb0(key: slice, length: int) -> slice:
|
|
|
14
14
|
# For negative step we sometimes get a negative stop, which can't be used correctly in a new slice
|
|
15
15
|
return slice(new_start, None if new_stop < 0 else new_stop, step)
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
def offset_start_stop_lsb0(start: Optional[int], stop: Optional[int], length: int) -> tuple[int, int]:
|
|
18
19
|
# First convert slice to all integers
|
|
19
20
|
# Length already should take account of the offset
|
|
20
21
|
start, stop, _ = slice(start, stop, None).indices(length)
|
|
@@ -218,7 +219,6 @@ class BitStore:
|
|
|
218
219
|
def getindex_lsb0(self, index: int, /) -> bool:
|
|
219
220
|
return bool(self._bitarray.__getitem__(-index - 1))
|
|
220
221
|
|
|
221
|
-
|
|
222
222
|
@overload
|
|
223
223
|
def setitem_lsb0(self, key: int, value: int, /) -> None:
|
|
224
224
|
...
|
|
@@ -269,4 +269,4 @@ class BitStore:
|
|
|
269
269
|
self._bitarray.__setitem__(key, value)
|
|
270
270
|
|
|
271
271
|
def delitem_msb0(self, key, /):
|
|
272
|
-
self._bitarray.__delitem__(key)
|
|
272
|
+
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
bitstring/luts.py
CHANGED
|
@@ -5,56 +5,57 @@
|
|
|
5
5
|
mxfp_luts_compressed = \
|
|
6
6
|
{(2, 1, 1, 'saturate'): (b'x\x01\x1d\xc9\xc1\r\xc00\x00\xc2@o\xd6\x8c\xc6f\xf5h\xb1\x828\xf1\x00^>X\x0c\xa7f1,\x7f'
|
|
7
7
|
b'\x13\x83\xfdY\xf4\x027\xf1\x0c\xfb',
|
|
8
|
-
b
|
|
9
|
-
b'\
|
|
10
|
-
b'@\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @'
|
|
11
|
-
b'\x80@[\x81A\x04\x08\x10 \x90(0\x8a@\xb2\xc0\xa4d\x81Y\xd9\x02\x8b\x92\x05Ve\x0bl"@\x80'
|
|
8
|
+
b"x\x01\xed\xdd\t\r\xc00\x10\x03\xc1\xf4\xff[\xfehS\x1a'y\x96\x81\x87\x80[\x13\x01\x02"
|
|
9
|
+
b'\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80'
|
|
12
10
|
b'\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10'
|
|
11
|
+
b' @\x80\x00\x01\x02e\x05\x06\x11 @\x80@\xa2\xc0(\x02\xc9\x02\x93\x92\x05fe\x0b,J\x16X\x95-'
|
|
12
|
+
b'\xb0\x89\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04'
|
|
13
|
+
b'\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00'
|
|
14
|
+
b'\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x14\x14\xe8"@ U`\x17\x01\x02\x04\x08\x10 '
|
|
15
|
+
b'@\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @'
|
|
16
|
+
b'\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02u\x05\x0e\x11 @'
|
|
17
|
+
b'\x80@\xa2\xc0)\x02\xc9\x02\x97\x92\x05ne\x0b<J\x16x\x95-\xf0\x89\x00\x01\x02\x04\x08\x10'
|
|
13
18
|
b' @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 '
|
|
14
|
-
b'@\x80\x00\x01\x02\r\x05>\x11 \x90*\xd0\xf6.\x99\xc1\x08\x10 @\x80\x00\x01\x02\x04\x08\x10'
|
|
15
|
-
b' @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 '
|
|
16
|
-
b'@\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10\xa8\xdaE\x80\x00\x01\x02'
|
|
17
|
-
b'\x89\x02\x87\x08$\x0b\x9cJ\x16\xb8\x94-p+Y\xe0Q\xb6\xc0+\x02\x04\x08\x10 @\x80\x00'
|
|
18
|
-
b'\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 '
|
|
19
19
|
b'@\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04'
|
|
20
|
-
b'\x08\
|
|
20
|
+
b'\x08\x14\x14H=>\xb7\x9b\x00\x81\xfe\x03\\M8\xf2'),
|
|
21
21
|
(2, 3, 1, 'saturate'): (b'x\x01\x1d\xcdQ\r\xc30\x10\x03\xd0\x830\x08\x85P\x04[ \x14B \x0cB \x04B \x14\xc1v\x10'
|
|
22
22
|
b'\x02!\x10\x02aO\xb3\xf4\xe4?;\xe2\x9fgD#\x89W\xc4A\xa1\xd2\xe8\x0cn\x92\xc9b\x13%\xe2\xc1'
|
|
23
23
|
b'\xc1I\xe1\xa2\xf2\xa6\xd1\x19\xdc$\x93\xc5&\x1a\x1fE\x12_[\x14*\x8d\xce\xe0&\x99,6\x91\xfe98'
|
|
24
24
|
b')\\T\xde4:\x83\x9bd\xb2\xd8\xf9\x03~S=\xdd',
|
|
25
|
-
b'x\x01\xed\xdd\
|
|
26
|
-
b'\
|
|
27
|
-
b'\
|
|
28
|
-
b'\
|
|
29
|
-
b'\
|
|
30
|
-
b'\
|
|
31
|
-
b'
|
|
32
|
-
b'
|
|
33
|
-
b'\x08\x10
|
|
34
|
-
b'\
|
|
35
|
-
b'
|
|
36
|
-
b'\
|
|
37
|
-
b'\
|
|
38
|
-
b'\
|
|
39
|
-
b'
|
|
25
|
+
b'x\x01\xed\xdd\x85qB\x01\x14DQ\x12\x88\x11#\xeeF\x84\xb8\xbb\xf6_\x15\xa9\x82\xd9\xf9\xf3'
|
|
26
|
+
b'\xce\xed`O\x03\xdbj\x89\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01'
|
|
27
|
+
b'\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @'
|
|
28
|
+
b'\x80\x00\x81\xc6\x08\x8c\x89\x00\x01\x02\x04*\n\x8c\x8b@e\x81\xb6*\x0bt\x94\x15\x98\xa8\xddd'
|
|
29
|
+
b'\xf5\xa6j7\xdd\xf4f\xb2u\xd3\xcdf\x9bK7\x9fm!\xddb\xb6^\xba\xa5l\xcb\xe9V\xb2\xad\xa6['
|
|
30
|
+
b'\xcb\xb6\x9en#\xdbf\xba\xadl\xdb\xe9v\xb2\xed\xa6\xdb\xcb\xb6\x9f\xee@\x04\x08\x10 @\x80'
|
|
31
|
+
b'\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10'
|
|
32
|
+
b' @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02'
|
|
33
|
+
b'\x04\x08\x10\x18\x91\xc0P\x04\x08T\x158\x14\x01\x02\x04\x08\x10 @\x80\x00\x01\x02'
|
|
34
|
+
b'\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80'
|
|
35
|
+
b'\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\xcd\x118\x12\x01\x02\x04\x08T\x14\xe8\x8b@e\x81c'
|
|
36
|
+
b'U\x168QV\xe0\xb4vg\xd5\x1b\xd4\xee\xbc\xe9]d\xbbLw\x95\xed:\xddM\xb6\xdbtw\xd9\xee\xd3=d{L'
|
|
37
|
+
b'\xf7\x94\xed9\xddK\xb6\xd7to\xd9\xde\xd3}d\xfbL\xf7\x95\xed;\xddO\xb6\xdft\x7f"@\x80\x00\x01'
|
|
38
|
+
b'\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @'
|
|
39
|
+
b'\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08'
|
|
40
|
+
b'\x10 @\x80\xc0\x88\x04\xaa\x1e\x9f\xdbM\x80\xc0\xf0\x1f\x9f\x0cK\xfb'),
|
|
40
41
|
(3, 2, 3, 'saturate'): (b'x\x01\x15\xcbA\x15BQ\x08\x05@\xa2XD\xc5&/\nQ(\xa2\xf2\x9b\x10\xc5\x91\xc3\xc0\xe2B\xc4\xbf'
|
|
41
42
|
b'\xean<"\x92\xa2\x19\x96xF\xdcH\x0eE3,\x91r\x92C\xd1\x0cK\xbc\xe4$\x87\xa2\x19\x96(\xfd'
|
|
42
43
|
b'\xb6?n(\x9aa\x89\xaf\x7f\x92C\xd1\x0cK\x8c\x9c\xe4P4\xc3\x12\x97\x9c\xe4P4\xc3^?\xc7\x8a;c',
|
|
43
|
-
b
|
|
44
|
-
b'
|
|
45
|
-
b'
|
|
46
|
-
b'\
|
|
47
|
-
b'\
|
|
48
|
-
b'\
|
|
44
|
+
b'x\x01\xed\xdd\xd7U\x15\x00\x00\x04Q$\x89HV@r\x90 Q\xb2\x92\xfb\xaf\xea\xd1\x02\x7f'
|
|
45
|
+
b'\x1c\xce\xdc\xe9`o\x03;4$\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01'
|
|
46
|
+
b'\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\xe0\xd3\t|'
|
|
47
|
+
b'\x11\x01\x02\x04\x08\x14\x05\x86E\xa0,0\xa2\xb2\xc0\xa8>V`\xac\xddx\xbd\xaf\xed&\xea}k7Y\xef'
|
|
48
|
+
b'{\xbb\xa9z\xd3\xedf\xea\xcd\xb6\x9b\xab7\xdfn\xa1\xde\x8fv?\xeb-\xb6[\xaa\xb7\xdc\xee'
|
|
49
|
+
b'W\xbd\x95v\xab\xf5\xd6\xda\xad\xd7\xdbh\xb7YoK\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10'
|
|
49
50
|
b' @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 '
|
|
50
|
-
b'@\x80\x00\x01\x02\x04\x08\x10
|
|
51
|
-
b'\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10
|
|
52
|
-
b'\x00\x01\x02\x04\x08\x10
|
|
53
|
-
b'\
|
|
54
|
-
b'\
|
|
55
|
-
b'\
|
|
56
|
-
b'\
|
|
57
|
-
b'\
|
|
51
|
+
b'@\x80\x00\x01\x02\x04\x08\x10 \xf0.\x81\x81\x08\x10\xa8\nl\x8b\x00\x01\x02\x04\x08\x10 @\x80'
|
|
52
|
+
b'\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10'
|
|
53
|
+
b' @\x80\x00\x01\x02\x04\x08\x10\xf8|\x02;"@\x80\x00\x81\xa2\xc0\xae\x08\x94\x05~\xab,\xb0'
|
|
54
|
+
b'\xa7\x8f\x15\xd8owP\xef\xb0\xdd\x9fzG\xed\x8e\xeb\x9d\xb4;\xadw\xd6\xee\xbc\xde\xdfv\x17'
|
|
55
|
+
b'\xf5.\xdb]\xd5\xbbnwS\xef\xb6\xdd]\xbd\x7f\xed\xfe\xd7\xbbo\xf7P\xef\xb1\xddS\xbd\xe7v/\xf5^'
|
|
56
|
+
b'E\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @'
|
|
57
|
+
b'\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\x04\x08\x10 @\x80\x00\x01\x02\xef\x12'
|
|
58
|
+
b'\xa8\x1e\x9f\xdbM\x80\xc0\xe0\r\xd9\xf2;{'),
|
|
58
59
|
(4, 3, 7, 'saturate'): (b'x\x01\x1d\xcd[\xb5\x90!\x10\x80Q"\x18\x81\x08\xbc{C-@\x04"\xd0\xe0\x10\x81\x08D\xe0\xdd\x1bj'
|
|
59
60
|
b'\x01"\x10\xe1o\xa0\xfb\xc8Z{\xcd\xbc|C\x08\xff\xdf\xdb\x10:\x9b\xf0.\x84H\xa6\xd2\x19'
|
|
60
61
|
b'L\x16\x9b\xc3\xe5!\xbc\x0f\xe1\r\x91D\xa6Pit\x06\x93\xc5\xe6py\x08\x1f\xf4D\x12\x99'
|
|
@@ -65,33 +66,32 @@ mxfp_luts_compressed = \
|
|
|
65
66
|
b"\x87\xcbC\xf8\xa9'\x92\xc8\x14*\x8d\xce`\xb2\xd8\x1c.\x0fa\xeb\x89$2\x85J\xa33\x98,6\x87\xcb"
|
|
66
67
|
b'C\xf8\xa5\'\x92\xc8\x14*\x8d\xce`\xb2\xd8\x1c.\x0f\xe1\xb7\x9eH"S\xa84:\x83\xc9bs\xb8<\x84'
|
|
67
68
|
b'?z"\x89L\xa1\xd2\xe8\x0c&\x8b\xcd\xe1\xbe\xee\x7f\xff\x01!\xba\xf7\x9b',
|
|
68
|
-
b'x\x01\xed\
|
|
69
|
-
b'
|
|
70
|
-
b'
|
|
71
|
-
b'
|
|
72
|
-
b'\x8b\
|
|
73
|
-
b'\
|
|
74
|
-
b'
|
|
75
|
-
b'\
|
|
76
|
-
b'
|
|
77
|
-
b'\
|
|
78
|
-
b'\
|
|
79
|
-
b'\
|
|
80
|
-
b'
|
|
81
|
-
b'
|
|
82
|
-
b'\
|
|
83
|
-
b'\
|
|
84
|
-
b'
|
|
85
|
-
b'\
|
|
86
|
-
b'\
|
|
87
|
-
b'\
|
|
88
|
-
b'\
|
|
89
|
-
b'
|
|
90
|
-
b'\
|
|
91
|
-
b'\
|
|
92
|
-
b'\
|
|
93
|
-
b'\x02\x16\xb0\x80\x05
|
|
94
|
-
b' \xec\x1d\xdd\xb0\xc7z'),
|
|
69
|
+
b'x\x01\xed\xdd\xd5\x96\x90\x05\x00E\xe1\x01\xe9\x90n\x90\x96F\xbaE\xa4S@\xba\x15\xa4\xbbKX H*'
|
|
70
|
+
b'\xdd)\xa9\x12Jw*H\x08Jww\x87\xd25\\\xccC\xec5\xf3\xef\xef\r\xce~\x81\x13\x12"\x0bX'
|
|
71
|
+
b'\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX \xc2\x16\x88$\x0b'
|
|
72
|
+
b'X\xc0\x02\x16\x08b\x81\xc8\xb2@\x90\x0b|\xa0 \x17\x88"\xb6@\xd4`\x8b\x16t\xd1\x83-Fx\x17\x93'
|
|
73
|
+
b'\x15\x8b\x16\x9b\x15\x87\x16\x97\xf5!-\x1e+>-\x01+!-\x11+1-\t+)-\x19+9-\x05+%-\x15+5-\r'
|
|
74
|
+
b'\xeb#ZZV:ZzV\x06ZFV&Zf\xd6\xc7\xb4,\xac\xac\xb4l\xac\xec\xb4\x1c\xac\x9c\xb4\\\xac\xdc\xb4O'
|
|
75
|
+
b'XyhyY\xf9h\xf9Y\x05h\x05Y\x85h\x85YEhEY\xc5h\xc5Y%h\x9f\xb2J\xd2>c\x95\xa2}\xce*M+\xc3*K+'
|
|
76
|
+
b'\xc7*O\xab\xc0\xaaH\xab\xc4\xaaL\xab\xc2\xaaJ\xab\xc6\xfa\x82V\x9dU\x83V\x93\xf5%\xad'
|
|
77
|
+
b'\x16\xab6\xad\x0e\xab.\xad\x1e\xab>\xad\x01\xab!\xad\x11\xab1\xad\t\xab)\xad\x19\xeb+\xda'
|
|
78
|
+
b'\xd7\xac\xe6\xb4\x16\xacoh-Y\xadh\xadYmhmY\xedh\xedY\x1dh\x1dY\x9dh\x9dY]h]Y\xddh\xddY=h'
|
|
79
|
+
b'=Y\xbdh\xbdY}h}Y\xfdh\xfdY\xdf\xd2\x06\xb0\x06\xca\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02'
|
|
80
|
+
b'\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05\xc2\n'
|
|
81
|
+
b'\x84\xca\x02\x16\x08j\x81A\xb2\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0'
|
|
82
|
+
b'\x02\x16\xb0\x80\x05"n\x81\xc1\xb2\x80\x05,`\x81 \x16\xf8N\x16\x08r\x81!\nr\x81\xa1'
|
|
83
|
+
b'b\x0b|\x1fl\xc3\x82nx\xb0\x8d\x08\xefF\xb2F\xd1F\xb3~\xa0\xfd\xc8\x1aC\x1b\xcb\x1a'
|
|
84
|
+
b'G\x1b\xcf\x9a@\x9b\xc8\x9aD\x9b\xcc\x9aB\x9b\xca\x9aF\x9b\xce\x9aA\x9b\xc9\x9aE\x9b\xcd\x9a'
|
|
85
|
+
b'C\x9b\xcb\xfa\x896\x8f5\x9f\xb6\x80\xb5\x90\xb6\x88\xb5\x98\xf63\xeb\x17\xda\xaf\xac'
|
|
86
|
+
b'%\xb4\xa5\xace\xb4\xe5\xac\xdfh\xbf\xb3V\xd0V\xb2V\xd1V\xb3\xd6\xd0\xd6\xb2\xd6\xd1\xd6\xb3'
|
|
87
|
+
b'6\xd06\xb26\xd16\xb3\xb6\xd0\xb6\xb2\xb6\xd1\xb6\xb3v\xd0v\xb2\xfe\xa0\xfd\xc9\xdaE\xdb\xcd'
|
|
88
|
+
b'\xfa\x8b\xb6\x87\xb5\x97\xb6\x8f\xb5\x9f\xf67\xeb\x00\xed \xeb\x1f\xda\xbf\xacC\xb4\xc3'
|
|
89
|
+
b'\xac#\xb4\xa3\xacc\xb4\xe3\xac\x13\xb4\x93\xacS\xb4\xd3\xac3\xb4\xb3\xacs\xb4\xf3'
|
|
90
|
+
b'\xac\x0b\xb4\x8b\xacK\xb4\xcb\xac+\xb4\xab\xack\xb4\xeb\xac\x1b\xb4\x9b\xac[\xb4\xdb'
|
|
91
|
+
b'\xac;\xb4\xbb\xac{\xb4\xfb\xac\x07\xb4\x87\xacG\xb4\xc7\xac\xffh\xff\xb3\x9e\xd0\x9e'
|
|
92
|
+
b'\xb2\x9e\xd1\x9e\xb3^\xd0^\xb2^\xd1^\xb3\xde\xd0\xde\xb2\xde\xc9\x02\x16\xb0\x80\x05'
|
|
93
|
+
b',`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0'
|
|
94
|
+
b'\x02\x16\xb0\x80\x05\xc2\n\x04\xf5\xf8\xdc\xdd\x16\xb0@\xe8{\t?\xc8\x90'),
|
|
95
95
|
(5, 2, 15, 'saturate'): (b'x\x01M\xcb\xd9\x11\x10T\x0c@\xd1\x94B\x17 \x08\x06Dh#\x9d\x90R\xd2\x05\xca\x1a\x17'
|
|
96
96
|
b'\xa0\x8dt\x02\xc7\x1f\xc7\xcc\x9c\xf7\xf1n\x12\xf1\xef\xf4C\xcf\xa3\x88\xa4\x19\x96#~\x8ax@'
|
|
97
97
|
b"R4\xc3r\xc4c\x9d\xa4h\x86\xe5\x88':I\xd1\x0c\xcb\x11?\xeb$E3,G<\xd5I\x8afX\x8ex\xa6\x93"
|
|
@@ -102,27 +102,22 @@ mxfp_luts_compressed = \
|
|
|
102
102
|
b"')\x9aa9bu\x92\xa2\x19\x96#\xfe\xd4I\x8afX\x8e\xf8K')\x9aa9\xe2o\x9d\xa4h\x86\xe5\x88\x7ft"
|
|
103
103
|
b'\x92\xa2\x19\x96#\xbe\xe8$E3,G|\xd5I\x8afX\x8e\xf8\xa6\x93\x14\xfd]\xfb\xcf\x0f'
|
|
104
104
|
b'\xd2\x15\xf0\xcf',
|
|
105
|
-
b
|
|
106
|
-
b'
|
|
107
|
-
b'\
|
|
108
|
-
b'\
|
|
109
|
-
b'\
|
|
110
|
-
b'\
|
|
111
|
-
b'\
|
|
112
|
-
b'\
|
|
113
|
-
b'\
|
|
114
|
-
b'\
|
|
115
|
-
b'\
|
|
116
|
-
b'\
|
|
117
|
-
b
|
|
118
|
-
b'\
|
|
119
|
-
b'\
|
|
120
|
-
b'\
|
|
121
|
-
b'\xb6S\xba\xd3\xb63\xba\xb3\xb6s\xba\xf3\xb6\x0b\xba\x8b\xb6K\xba\xcb\xb6+\xba\xab'
|
|
122
|
-
b'\xb6k\xba\xeb\xb6\x1b\xba\x9b\xb6[\xba\xdb\xb6;\xba\xbb\xb6{\xba\xfb\xb6\x07\xba\x87'
|
|
123
|
-
b"\xb6G\xba\xc7\xb6'\xba\xa7\xb6g\xba\xe7\xb6\x17\xba\x97\xb6W\xba\xd7\xb67\xba\xb7"
|
|
124
|
-
b'\xb6w\xba\xf7\xb6\x0f\xba\x8f\xb6O\xba\xcf\xb6/\xba\xaf\xb6o\xba\xef\xb6\x1f\xba\x9f'
|
|
125
|
-
b'\xb6_\xba\xdf\xb6?\xba\xbf\x01\xc1Z@=>\x0f\xec\x0e\x14\x08\x14\xf8\xf7\x1f$\x1d\xcd\x0c'),
|
|
105
|
+
b'x\x01\xed\xddC\xa2\x1c\x00\x00D\xc1\xd8\xb6m\xdb\xb6m\xdb66\xb1m\xdb\xb6m\xdb\xb6m'
|
|
106
|
+
b',\x92c\xfcEM\xdd\xa0\xdf\x05:X\xb0 \x16\xdc\x16B\x17\xd2\x16J\x17\xda\x16F\x17\xd6'
|
|
107
|
+
b'\x16N\x17\xde\x16A\x17\xd1\x16I\x17\xd9\x16E\x17\xd5\x16M\x17\xdd\x16C\x17\xd3\x16K\x17\xdb'
|
|
108
|
+
b'\x16G\x17\xd7\x16O\x17\xdf\x96@\x97\xd0\x96H\x97\xd8\x96D\x97\xd4\x96L\x97\xdc\x96B\x97\xd2'
|
|
109
|
+
b'\x96J\x97\xda\x96F\x97\xd6\x96N\x97\xde\x96A\x97\xd1\x96I\x97\xd9\x96E\x97\xd5\x96M\x97\xdd'
|
|
110
|
+
b'\x96C\x97\xd3\x96K\x97\xdb\x96G\x97\xd7\x96O\x97\xdfV@W\xd0VHW\xd8VDW\xd4VLW\xdcVBW\xd2'
|
|
111
|
+
b'VJW\xdaVFW\xd6VNW\xdeVAW\xd1VIW\xd9VEW\xd5VMW\xddVCW\xd3VKW\xdbVGW\xd7VOW\xdf\xd6@\xd7\xd0'
|
|
112
|
+
b'\xd6H\xd7\xd8\xd6D\xd7\xd4\xd6L\xd7\xdc\xd6B\xd7\xd2\xd6J\xd7\xda\xd6F\xd7\xd6\xd6N\xd7\xde'
|
|
113
|
+
b'\xd6A\xd7\xd1\xd6I\xd7\xd9\xd6E\xd7\xd5\xd6M\xd7\xdd\xd6C\xd7\xd3\xd6K\xd7\xdb\xd6G\xd7\xd7'
|
|
114
|
+
b'\xd6O\xd7\xdf6@70 H\x0b\xfc\x0b\x08\x14\x08\x14P\x0b\x0c\nj\x83mCtCm\xc3t\xc3m#t#m'
|
|
115
|
+
b'\xa3t\xa3mctcm\xe3t\xe3m\x13t\x13m\x93t\x93mStSm\xd3t\xd3m3t3m\xb3t\xb3mstsm\xf3t\xf3m'
|
|
116
|
+
b'\x0bt\x0bm\x8bt\x8bmKtKm\xcbt\xcbm+t+m\xabt\xabmktkm\xebt\xebm\x1bt\x1bm\x9bt\x9bm[t[m'
|
|
117
|
+
b"\xdbt\xdbm;t;m\xbbt\xbbm{t{m\xfbt\xfbm\x07t\x07m\x87t\x87mGtGm\xc7t\xc7m't'm\xa7t\xa7mgtgm"
|
|
118
|
+
b'\xe7t\xe7m\x17t\x17m\x97t\x97mWtWm\xd7t\xd7m7t7m\xb7t\xb7mwtwm\xf7t\xf7m\x0ft\x0fm'
|
|
119
|
+
b'\x8ft\x8fmOtOm\xcft\xcfm/t/m\xaft\xafmotom\xeft\xefm\x1ft\x1fm\x9ft\x9fm_t_m\xdft\xdfm?t?m'
|
|
120
|
+
b'\xbft\xbfm\x7ft\x7f\x03\x82\xb4\x80z|\x1e\xd8\x1d(\x10(\xf0\xef?\xe6\xfc\r\x9b'),
|
|
126
121
|
(4, 3, 7, 'overflow'): (b'x\x01\x1d\xcd[\xb5\x90!\x10\x80Q"\x18\x81\x08\xbc{C-@\x04"\xd0\xe0\x10\x81\x08D\xe0\xdd\x1bj'
|
|
127
122
|
b'\x01"\x10\xe1o\xa0\xfb\xc8Z{\xcd\xbc|C\x08\xff\xdf\xdb\x10:\x9b\xf0.\x84H\xa6\xd2\x19'
|
|
128
123
|
b'L\x16\x9b\xc3\xe5!\xbc\x0f\xe1\r\x91D\xa6Pit\x06\x93\xc5\xe6py\x08\x1f\xf4D\x12\x99'
|
|
@@ -133,32 +128,31 @@ mxfp_luts_compressed = \
|
|
|
133
128
|
b"\x87\xcbC\xf8\xa9'\x92\xc8\x14*\x8d\xce`\xb2\xd8\x1c.\x0fa\xeb\x89$2\x85J\xa33\x98,6\x87\xcb"
|
|
134
129
|
b'C\xf8\xa5\'\x92\xc8\x14*\x8d\xce`\xb2\xd8\x1c.\x0f\xe1\xb7\x9eH"S\xa84:\x83\xc9bs\xb8<\x84'
|
|
135
130
|
b'?z"\x89L\xa1\xd2\xe8\x0c&\x8b\xcd\xe1\xbe\xee\x7f\xff\x01!\xba\xf7\x9b',
|
|
136
|
-
b'x\x01\xed\
|
|
137
|
-
b'\
|
|
138
|
-
b
|
|
139
|
-
b'\
|
|
140
|
-
b'\
|
|
141
|
-
b'\
|
|
142
|
-
b'
|
|
143
|
-
b'
|
|
144
|
-
b'\
|
|
145
|
-
b'\
|
|
146
|
-
b'\
|
|
147
|
-
b
|
|
148
|
-
b'\
|
|
149
|
-
b'\x02\x16\
|
|
150
|
-
b
|
|
151
|
-
b'\xb0\
|
|
152
|
-
b
|
|
153
|
-
b'
|
|
154
|
-
b
|
|
155
|
-
b
|
|
156
|
-
b'\
|
|
157
|
-
b'\
|
|
158
|
-
b'
|
|
159
|
-
b'
|
|
160
|
-
b'
|
|
161
|
-
b'\x16\xb0\x80\x05,`\x01\x0b\x84~\x02\x87va\xdb'),
|
|
131
|
+
b'x\x01\xed\xdc\x05\xb2\x90\x05\x00E\xe1\x87\x80\xa4t\x83tw\x97Jw\x0bHKw\x83\x80\x92Cww7'
|
|
132
|
+
b'\x92\xd2\x1dRJww\x97\xa4\x92J\xba\x8c3\x8f\xff|\x1b\xb83g\x017$D\x16\xb0\x80\x05,`\x01'
|
|
133
|
+
b'\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\xc0g[ \x8c,`\x01\x0bX '
|
|
134
|
+
b'\x88\x05\xbe\x90\x05\x82\\ \xac\x82\\ \x9c\xd8\x02\xe1\x83\xed\xcb\xa0\x8b\x10l\x11'
|
|
135
|
+
b'C\xbbH\xac\xc8\xb4(\xac\xa8\xb4\xafX\xd1h\xd1Y1h1Y\xb1h\xb1YqhqY\xf1h\xf1Y\th\tY\x89h\x89Y'
|
|
136
|
+
b'Ih_\xb3\x92\xd2\x92\xb1\x92\xd3R\xb0R\xd2R\xb1R\xd3\xd2\xb0\xd2\xd2\xd2\xb1\xd2\xd32\xb0'
|
|
137
|
+
b'2\xd22\xb12\xd3\xb2\xb0\xb2\xd2\xb2\xb1\xb2\xd3r\xb0r\xd2r\xb1r\xd3\xf2\xb0\xf2\xd2\xf2\xb1'
|
|
138
|
+
b'\xf2\xd3\n\xb0\n\xd2\xbea}K\xfb\x8eU\x88V\x98U\x84V\x94U\x8cV\x9cU\x82V\x92U\x8aV\x9a'
|
|
139
|
+
b'U\x86V\x96U\x8eV\x9eU\x81V\x91U\x89V\x99U\x85\xf6=\xab*\xad\x1a\xab:\xed\x07V\rZMV-ZmV\x1dZ]'
|
|
140
|
+
b'V=\xda\x8f\xac\xfa\xb4\x06\xac\x86\xb4F\xac\xc6\xb4&\xac\xa6\xb4f\xac\xe6\xb4\x16'
|
|
141
|
+
b'\xac\x96\xb4V\xac\xd6\xb46\xac\xb6\xb4v\xac\xf6\xb4\x0e\xac\x8e\xb4N\xac\xce\xb4\x9fX]h]'
|
|
142
|
+
b'Y\xddh?\xb3~\xa1ug\xf5\xa0\xf5d\xf5\xa2\xf5f\xf5\xa1}\x92\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80'
|
|
143
|
+
b'\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX'
|
|
144
|
+
b'\xc0\x02\x16`\x0b\xf4\x95\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0'
|
|
145
|
+
b"\x80\x05,\xf0\xf9\x16\xe8'\x0bX\xc0\x02\x16\x08b\x81\xfe\xb2@\x90\x0b\x0cP\x90\x0b\x0c\x14["
|
|
146
|
+
b'`P\xb0\r\x0e\xba!\xc164\xb4\x1b\xc6\x1aN\x1b\xc1\x1aI\x1b\xc5\x1aM\x1b\xc3\x1aK\x1b'
|
|
147
|
+
b'\xc7\x1aO\x9b\xc0\x9aH\x9b\xc4\x9aL\x9b\xc2\x9aJ\x9b\xc6\x9aN\x9b\xc1\x9aI\x9b\xc5\x9aM\x9b'
|
|
148
|
+
b'\xc3\x9aK\x9b\xc7\x9aO[\xc0ZH[\xc4\xfa\x95\xb6\x98\xb5\x84\xb6\x94\xb5\x8c\xb6'
|
|
149
|
+
b'\x9c\xb5\x82\xf6\x1bk%m\x15k5m\rk-m\x1dk=m\x03k#m\x13k3m\x0bk+m\x1bk;m\x07\xebw\xda'
|
|
150
|
+
b"N\xd6.\xdan\xd6\x1e\xda^\xd6\x1f\xb4?Y\xfbh\xfbY\x07h\x07Y\x87h\x87YGhGY\xc7h\xc7Y'h'Y\xa7h"
|
|
151
|
+
b'\xa7YghgY\xe7h\xe7Y\x17h\x17Y\x97h\x97YWhWY\xd7h\xd7Y7h7Y\xb7h\xb7YwhwY\xf7h\xf7Y\x0fh'
|
|
152
|
+
b'\x7f\xb1\x1e\xd2\x1e\xb1\x1e\xd3\x9e\xb0\x9e\xd2\x9e\xb1\xfe\xa6\xfd\xc3zN{\xc1zI{\xc5zM'
|
|
153
|
+
b'{\xc3\xfa\x97\xf6\x1f\xeb-\xed\x1d\xeb=\xed\x03\xeb#\x8d\xbd\xefw\xdd\x02\x16\xb0\x80\x05,`'
|
|
154
|
+
b'\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16\xb0\x80\x05,`\x01\x0bX\xc0\x02\x16'
|
|
155
|
+
b'\xb0\x80\x05,`\x01\x0b|\xfa\x1f\xb2\xf6b\xf1'),
|
|
162
156
|
(5, 2, 15, 'overflow'): (b'x\x01M\xcb\xd9\x11\x10T\x0c@\xd1\x94B\x17 \x08\x06Dh#\x9d\x90R\xd2\x05\xca\x1a\x17'
|
|
163
157
|
b'\xa0\x8dt\x02\xc7\x1f\xc7\xcc\x9c\xf7\xf1n\x12\xf1\xef\xf4C\xcf\xa3\x88\xa4\x19\x96#~\x8ax@'
|
|
164
158
|
b"R4\xc3r\xc4c\x9d\xa4h\x86\xe5\x88':I\xd1\x0c\xcb\x11?\xeb$E3,G<\xd5I\x8afX\x8ex\xa6\x93"
|
|
@@ -169,24 +163,27 @@ mxfp_luts_compressed = \
|
|
|
169
163
|
b"')\x9aa9bu\x92\xa2\x19\x96#\xfe\xd4I\x8afX\x8e\xf8K')\x9aa9\xe2o\x9d\xa4h\x86\xe5\x88\x7ft"
|
|
170
164
|
b'\x92\xa2\x19\x96#\xbe\xe8$E3,G|\xd5I\x8afX\x8e\xf8\xa6\x93\x14\xfd]\xfb\xcf\x0f'
|
|
171
165
|
b'\xd2\x15\xf0\xcf',
|
|
172
|
-
b'x\x01\xed\xddS\
|
|
173
|
-
b
|
|
174
|
-
b'\x85\xd5\x85\xb3\x85\xd7E\xb0E\xd4E\xb2E\xd6E\xb1E\xd5E\xb3E\xd7\xc5\xb0
|
|
175
|
-
b'\xc5\xd6\xc5\xb1\xc5\xd5\xc5\xb3\xc5\xd7%\xb0%\xd4%\xb2%\xd6%\xb1%\xd5%\xb3
|
|
176
|
-
b'\xa5\xd4\xa5\xb2\xa5\xd6\xa5\xb1\xa5\xd5\xa5\xb3\
|
|
177
|
-
b'
|
|
178
|
-
b'
|
|
179
|
-
b'
|
|
180
|
-
b
|
|
181
|
-
b'\
|
|
182
|
-
b'\
|
|
183
|
-
b'\
|
|
184
|
-
b'\
|
|
185
|
-
b'\
|
|
186
|
-
b'\
|
|
187
|
-
b'\
|
|
188
|
-
b'
|
|
189
|
-
b'\
|
|
166
|
+
b'x\x01\xed\xddS\xb6\x1e\x06\x00\x85\xd1\x1b\xdb\xb6\x1b\xdb\xb6\x9b\x06\x8dm\xdbz\x88'
|
|
167
|
+
b"m\xabI\x1b\xa3\xb1m\xdb\xb6mg\xad\x9ba\xdc\x87\xfd\xef\x19\x9co\x02'((\x84\x85\xb2"
|
|
168
|
+
b'\x85\xd6\x85\xb1\x85\xd5\x85\xb3\x85\xd7E\xb0E\xd4E\xb2E\xd6E\xb1E\xd5E\xb3E\xd7\xc5\xb0'
|
|
169
|
+
b'\xc5\xd4\xc5\xb2\xc5\xd6\xc5\xb1\xc5\xd5\xc5\xb3\xc5\xd7%\xb0%\xd4%\xb2%\xd6%\xb1%\xd5%\xb3'
|
|
170
|
+
b'%\xd7\xa5\xb0\xa5\xd4\xa5\xb2\xa5\xd6\xa5\xb1\xa5\xd5\xa5\xb3\xfd\xa1Ko\xcb\xa0\xcbh'
|
|
171
|
+
b'\xcb\xa4\xcbl\xcb\xa2\xcbj\xcb\xa6\xcbn\xcb\xa1\xcbi\xcb\xa5\xcbm\xcb\xa3\xcbk\xcb\xa7\xcbo'
|
|
172
|
+
b'+\xa0+h+\xa4+l+\xa2+j+\xa6+n+\xa1+i+\xa5+m+\xa3+k+\xa7+o\xab\xa0\xabh\xab\xa4\xabl'
|
|
173
|
+
b'\xfbSW\xc5\xf6\x97\xae\xaa\xad\x9a\xae\xba\xad\x86\xeeo[M]-[m]\x1d[]]=[}]\x03[C]#[c]\x13'
|
|
174
|
+
b"[S]3[s]\x0b[K]+[k]\x1b[[];[{]\x07[G]'[g]\x17[W]7[w]\x0f[O]/[o]\x1f[_]?[\x7f\xdd\x00"
|
|
175
|
+
b'\xdb\xc0\x90\x16\x1c\x10(\x10(\xa0\x16\x18\x14\xd2\x06\xdb\x86\xe8\x86\xda\x86\xe9\x86\xdb'
|
|
176
|
+
b'F\xe8F\xdaF\xe9F\xdb\xc6\xe8\xc6\xda\xc6\xe9\xc6\xdb&\xe8&\xda&\xe9&\xdb\xa6\xe8\xa6\xda'
|
|
177
|
+
b'\xa6\xe9\xa6\xdbf\xe8f\xdaf\xe9f\xdb\xe6\xe8\xe6\xda\xe6\xe9\xfe\xb1\xcd\xd7-\xb0'
|
|
178
|
+
b'\xfd\xab\xfb\xcf\xb6P\xb7\xc8\xb6X\xb7\xc4\xb6T\xb7\xcc\xb6\\\xb7\xc2\xb6R\xb7\xca'
|
|
179
|
+
b'\xf6\xbfn\xb5m\x8dn\xadm\x9dn\xbdm\x83n\xa3m\x93n\xb3m\x8bn\xabm\x9bn\xbbm\x87n\xa7'
|
|
180
|
+
b'm\x97n\xb7m\x8fn\xafm\x9fn\xbf\xed\x80\xee\xa0\xed\x90\xee\xb0\xed\x88\xee\xa8'
|
|
181
|
+
b'\xed\x98\xee\xb8\xed\x84\xee\xa4\xed\x94\xee\xb4\xed\x8c\xee\xac\xed\x9c\xee\xbc'
|
|
182
|
+
b'\xed\x82\xee\xa2\xed\x92\xee\xb2\xed\x8a\xee\xaa\xed\x9a\xee\xba\xed\x86\xee\xa6'
|
|
183
|
+
b'\xed\x96\xee\xb6\xed\x8e\xee\xae\xed\x9e\xee\xbe\xed\x81\xee\xa1\xed\x91\xee\xb1'
|
|
184
|
+
b'\xed\x89\xee\xa9\xed\x99\xee\xb9\xed\x85\xee\xa5\xed\x95\xee\xb5\xed\x8d\xee\xad'
|
|
185
|
+
b'\xed\x9d\xee\xbd\xed\x83\xee\xa3\xed\x93\xee\xb3\xed\x8b\xee\xab\xed\x9b\xee\xbb'
|
|
186
|
+
b'\xed\x87\xee\xa7\xedWHS\x8f\xcf\x03\xbb\x03\x05\x02\x05\x82\x7f\x03\xb3\x87\x0e\x9d')}
|
|
190
187
|
|
|
191
188
|
|
|
192
189
|
binary8_luts_compressed = \
|
bitstring/mxfp.py
CHANGED
|
@@ -6,6 +6,7 @@ from bitstring.luts import mxfp_luts_compressed
|
|
|
6
6
|
import zlib
|
|
7
7
|
from typing import Optional
|
|
8
8
|
|
|
9
|
+
|
|
9
10
|
def round_to_nearest_ties_to_even(lut_int_to_float, lower: int, f: float) -> Optional[int]:
|
|
10
11
|
upper = lower + 1
|
|
11
12
|
# Special case for LUTs without a negative zero.
|
|
@@ -56,7 +57,7 @@ class MXFPFormat:
|
|
|
56
57
|
self.pos_clamp_value = 0b01111100 # +inf
|
|
57
58
|
self.neg_clamp_value = 0b11111100 # -inf
|
|
58
59
|
|
|
59
|
-
# If we calculate these LUTs now
|
|
60
|
+
# If we calculate these LUTs now it creates a bootstrap problem in generate_luts.py.
|
|
60
61
|
self.lut_float16_to_mxfp = None
|
|
61
62
|
self.lut_int_to_float = None
|
|
62
63
|
|
|
@@ -91,7 +92,15 @@ class MXFPFormat:
|
|
|
91
92
|
# The output int has the binary sequence needed for the float.
|
|
92
93
|
length = 1 + self.exp_bits + self.mantissa_bits
|
|
93
94
|
values = 1 << length
|
|
94
|
-
|
|
95
|
+
# First get the NaN case out of the way
|
|
96
|
+
if math.isnan(f):
|
|
97
|
+
if length == 8:
|
|
98
|
+
return 0xff # Works for both e5m2 and e4m3
|
|
99
|
+
# For smaller lengths, NaN isn't supported so we instead return an invalid value to detect later
|
|
100
|
+
return 0xff
|
|
101
|
+
# This is so we can distinguish between 0.0 and -0.0
|
|
102
|
+
is_positive = math.copysign(1.0, f) == 1.0
|
|
103
|
+
if is_positive:
|
|
95
104
|
# Positive, so top bit is not set
|
|
96
105
|
for i in range(values // 2 - 1):
|
|
97
106
|
upper = self.lut_int_to_float[i + 1]
|
|
@@ -101,7 +110,7 @@ class MXFPFormat:
|
|
|
101
110
|
if x is not None:
|
|
102
111
|
return x
|
|
103
112
|
return self.pos_clamp_value
|
|
104
|
-
|
|
113
|
+
else:
|
|
105
114
|
# Negative, so top bit is set
|
|
106
115
|
for i in range(values // 2, values - 1):
|
|
107
116
|
lower = self.lut_int_to_float[i + 1]
|
|
@@ -112,11 +121,6 @@ class MXFPFormat:
|
|
|
112
121
|
return x
|
|
113
122
|
# Clip to negative max
|
|
114
123
|
return self.neg_clamp_value
|
|
115
|
-
assert math.isnan(f)
|
|
116
|
-
if length == 8:
|
|
117
|
-
return 0xff # Works for both e5m2 and e4m3
|
|
118
|
-
# For smaller lengths, NaN isn't supported so we instead return an invalid value to detect later
|
|
119
|
-
return 0xff
|
|
120
124
|
|
|
121
125
|
def createLUT_for_int_to_float(self) -> array.array:
|
|
122
126
|
"""Create a LUT to convert an int in representing a MXFP float into a Python float"""
|
|
@@ -154,7 +158,8 @@ class MXFPFormat:
|
|
|
154
158
|
length = 1 + self.exp_bits + self.mantissa_bits
|
|
155
159
|
if length == 8:
|
|
156
160
|
import gfloat
|
|
157
|
-
|
|
161
|
+
from gfloat.formats import format_info_ocp_e5m2, format_info_ocp_e4m3
|
|
162
|
+
fi = format_info_ocp_e5m2 if self.exp_bits == 5 else format_info_ocp_e4m3
|
|
158
163
|
|
|
159
164
|
fp16_to_fp8 = bytearray(1 << 16)
|
|
160
165
|
for i in range(1 << 16):
|
|
@@ -164,7 +169,11 @@ class MXFPFormat:
|
|
|
164
169
|
if math.isnan(fp):
|
|
165
170
|
fp8_i = 0b11111111
|
|
166
171
|
else:
|
|
167
|
-
|
|
172
|
+
# Special case for negative zero
|
|
173
|
+
if fp == 0.0 and math.copysign(1.0, fp) == -1.0:
|
|
174
|
+
fp8_i = 0b10000000
|
|
175
|
+
else:
|
|
176
|
+
fp8_i = self.lut_int_to_float.index(fp)
|
|
168
177
|
fp16_to_fp8[i] = fp8_i
|
|
169
178
|
return bytes(fp16_to_fp8)
|
|
170
179
|
else:
|
|
@@ -174,9 +183,6 @@ class MXFPFormat:
|
|
|
174
183
|
b = struct.pack('>H', i)
|
|
175
184
|
f, = struct.unpack('>e', b)
|
|
176
185
|
fp8_i = self.slow_float_to_int(f)
|
|
177
|
-
if fp8_i == 1 << (self.exp_bits + self.mantissa_bits):
|
|
178
|
-
# Got back int representing binary digits for negative zero. Just convert to positive zero instead.
|
|
179
|
-
fp8_i = 0
|
|
180
186
|
fp16_to_fp8[i] = fp8_i
|
|
181
187
|
return bytes(fp16_to_fp8)
|
|
182
188
|
|
bitstring/utils.py
CHANGED
|
@@ -21,11 +21,11 @@ MULTIPLICATIVE_RE: Pattern[str] = re.compile(r'^(?P<factor>.*)\*(?P<token>.+)')
|
|
|
21
21
|
LITERAL_RE: Pattern[str] = re.compile(r'^(?P<name>0([xob]))(?P<value>.+)', re.IGNORECASE)
|
|
22
22
|
|
|
23
23
|
# An endianness indicator followed by one or more struct.pack codes
|
|
24
|
-
STRUCT_PACK_RE: Pattern[str] = re.compile(r'^(?P<endian>[<>@=])
|
|
24
|
+
STRUCT_PACK_RE: Pattern[str] = re.compile(r'^(?P<endian>[<>@=])(?P<fmt>(?:\d*[bBhHlLqQefd])+)$')
|
|
25
25
|
# The same as above, but it doesn't insist on an endianness as it's byteswapping anyway.
|
|
26
26
|
BYTESWAP_STRUCT_PACK_RE: Pattern[str] = re.compile(r'^(?P<endian>[<>@=])?(?P<fmt>(?:\d*[bBhHlLqQefd])+)$')
|
|
27
27
|
# An endianness indicator followed by exactly one struct.pack codes
|
|
28
|
-
SINGLE_STRUCT_PACK_RE: Pattern[str] = re.compile(r'^(?P<endian>[<>@=])
|
|
28
|
+
SINGLE_STRUCT_PACK_RE: Pattern[str] = re.compile(r'^(?P<endian>[<>@=])(?P<fmt>[bBhHlLqQefd])$')
|
|
29
29
|
|
|
30
30
|
# A number followed by a single character struct.pack code
|
|
31
31
|
STRUCT_SPLIT_RE: Pattern[str] = re.compile(r'\d*[bBhHlLqQefd]')
|
|
@@ -74,6 +74,7 @@ def structparser(m: Match[str]) -> List[str]:
|
|
|
74
74
|
tokens = [REPLACEMENTS_BE[c] for c in fmt]
|
|
75
75
|
return tokens
|
|
76
76
|
|
|
77
|
+
|
|
77
78
|
@functools.lru_cache(CACHE_SIZE)
|
|
78
79
|
def parse_name_length_token(fmt: str, **kwargs) -> Tuple[str, Optional[int]]:
|
|
79
80
|
# Any single token with just a name and length
|
|
@@ -94,6 +95,7 @@ def parse_name_length_token(fmt: str, **kwargs) -> Tuple[str, Optional[int]]:
|
|
|
94
95
|
raise ValueError(f"Can't parse 'name[:]length' token '{fmt}'.")
|
|
95
96
|
return name, length
|
|
96
97
|
|
|
98
|
+
|
|
97
99
|
@functools.lru_cache(CACHE_SIZE)
|
|
98
100
|
def parse_single_struct_token(fmt: str) -> Optional[Tuple[str, Optional[int]]]:
|
|
99
101
|
if m := SINGLE_STRUCT_PACK_RE.match(fmt):
|
|
@@ -110,6 +112,7 @@ def parse_single_struct_token(fmt: str) -> Optional[Tuple[str, Optional[int]]]:
|
|
|
110
112
|
else:
|
|
111
113
|
return None
|
|
112
114
|
|
|
115
|
+
|
|
113
116
|
@functools.lru_cache(CACHE_SIZE)
|
|
114
117
|
def parse_single_token(token: str) -> Tuple[str, str, Optional[str]]:
|
|
115
118
|
if (equals_pos := token.find('=')) == -1:
|
|
@@ -132,6 +135,7 @@ def parse_single_token(token: str) -> Tuple[str, str, Optional[str]]:
|
|
|
132
135
|
length = token
|
|
133
136
|
return name, length, value
|
|
134
137
|
|
|
138
|
+
|
|
135
139
|
@functools.lru_cache(CACHE_SIZE)
|
|
136
140
|
def preprocess_tokens(fmt: str) -> List[str]:
|
|
137
141
|
# Remove whitespace and expand brackets
|
|
@@ -143,7 +147,7 @@ def preprocess_tokens(fmt: str) -> List[str]:
|
|
|
143
147
|
final_tokens = []
|
|
144
148
|
|
|
145
149
|
for meta_token in meta_tokens:
|
|
146
|
-
if
|
|
150
|
+
if meta_token == '':
|
|
147
151
|
continue
|
|
148
152
|
# Extract factor and actual token if a multiplicative factor exists
|
|
149
153
|
factor = 1
|
|
@@ -204,6 +208,7 @@ def tokenparser(fmt: str, keys: Tuple[str, ...] = ()) -> \
|
|
|
204
208
|
|
|
205
209
|
BRACKET_RE = re.compile(r'(?P<factor>\d+)\*\(')
|
|
206
210
|
|
|
211
|
+
|
|
207
212
|
def expand_brackets(s: str) -> str:
|
|
208
213
|
"""Expand all brackets."""
|
|
209
214
|
while True:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: bitstring
|
|
3
|
-
Version: 4.2.
|
|
3
|
+
Version: 4.2.3
|
|
4
4
|
Summary: Simple construction, analysis and modification of binary data.
|
|
5
5
|
Author-email: Scott Griffiths <dr.scottgriffiths@gmail.com>
|
|
6
6
|
Project-URL: homepage, https://github.com/scott-griffiths/bitstring
|
|
@@ -40,26 +40,13 @@ It has been actively maintained since 2006.
|
|
|
40
40
|
[](https://www.pepy.tech/projects/bitstring)
|
|
41
41
|
[](https://pypistats.org/packages/bitstring)
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
News
|
|
45
43
|
----
|
|
46
|
-
**May 2024**: bitstring 4.2.2 released.
|
|
47
|
-
|
|
48
|
-
New in version 4.2:
|
|
49
|
-
|
|
50
|
-
* Dropped support for Python 3.7. Minimum version is now 3.8.
|
|
51
|
-
* A new `Dtype` class can be optionally used to specify types.
|
|
52
|
-
* The `bitstring.options` object is now the preferred method for changing module options.
|
|
53
|
-
* New `fromstring` method as another way to create bitstrings from formatted strings.
|
|
54
|
-
* More types can now be pretty printed.
|
|
55
|
-
* A range of 8-bit, 6-bit and even 4-bit floating point formats added (beta):
|
|
56
|
-
* Performance improvements.
|
|
57
44
|
|
|
58
|
-
|
|
45
|
+
> [!NOTE]
|
|
46
|
+
> To see what been added, improved or fixed, and also to see what's coming in the next version, see the [release notes](https://github.com/scott-griffiths/bitstring/blob/main/release_notes.md).
|
|
59
47
|
|
|
60
48
|
|
|
61
|
-
Overview
|
|
62
|
-
--------
|
|
49
|
+
# Overview
|
|
63
50
|
|
|
64
51
|
* Efficiently store and manipulate binary data in idiomatic Python.
|
|
65
52
|
* Create bitstrings from hex, octal, binary, files, formatted strings, bytes, integers and floats of different endiannesses.
|
|
@@ -70,8 +57,8 @@ Overview
|
|
|
70
57
|
* Rich API - chances are that whatever you want to do there's a simple and elegant way of doing it.
|
|
71
58
|
* Open source software, released under the MIT licence.
|
|
72
59
|
|
|
73
|
-
Documentation
|
|
74
|
-
|
|
60
|
+
# Documentation
|
|
61
|
+
|
|
75
62
|
Extensive documentation for the bitstring module is available.
|
|
76
63
|
Some starting points are given below:
|
|
77
64
|
|
|
@@ -81,67 +68,66 @@ Some starting points are given below:
|
|
|
81
68
|
|
|
82
69
|
There is also an introductory walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
|
|
83
70
|
|
|
84
|
-
|
|
85
|
-
-------------
|
|
86
|
-
|
|
87
|
-
To see what been added, improved or fixed, and also to see what's coming in the next version, see the [release notes](https://github.com/scott-griffiths/bitstring/blob/main/release_notes.txt).
|
|
88
|
-
|
|
89
|
-
Examples
|
|
90
|
-
--------
|
|
71
|
+
# Examples
|
|
91
72
|
|
|
92
73
|
### Installation
|
|
93
|
-
|
|
94
|
-
|
|
74
|
+
```
|
|
75
|
+
$ pip install bitstring
|
|
76
|
+
```
|
|
95
77
|
|
|
96
78
|
### Creation
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
79
|
+
```pycon
|
|
80
|
+
>>> from bitstring import Bits, BitArray, BitStream, pack
|
|
81
|
+
>>> a = BitArray(bin='00101')
|
|
82
|
+
>>> b = Bits(a_file_object)
|
|
83
|
+
>>> c = BitArray('0xff, 0b101, 0o65, uint6=22')
|
|
84
|
+
>>> d = pack('intle16, hex=a, 0b1', 100, a='0x34f')
|
|
85
|
+
>>> e = pack('<16h', *range(16))
|
|
86
|
+
```
|
|
104
87
|
|
|
105
88
|
### Different interpretations, slicing and concatenation
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
89
|
+
```pycon
|
|
90
|
+
>>> a = BitArray('0x3348')
|
|
91
|
+
>>> a.hex, a.bin, a.uint, a.float, a.bytes
|
|
92
|
+
('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
|
|
93
|
+
>>> a[10:3:-1].bin
|
|
94
|
+
'0101100'
|
|
95
|
+
>>> '0b100' + 3*a
|
|
96
|
+
BitArray('0x866906690669, 0b000')
|
|
97
|
+
```
|
|
114
98
|
|
|
115
99
|
### Reading data sequentially
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
100
|
+
```pycon
|
|
101
|
+
>>> b = BitStream('0x160120f')
|
|
102
|
+
>>> b.read(12).hex
|
|
103
|
+
'160'
|
|
104
|
+
>>> b.pos = 0
|
|
105
|
+
>>> b.read('uint12')
|
|
106
|
+
352
|
|
107
|
+
>>> b.readlist('uint12, bin3')
|
|
108
|
+
[288, '111']
|
|
109
|
+
```
|
|
125
110
|
|
|
126
111
|
### Searching, inserting and deleting
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
112
|
+
```pycon
|
|
113
|
+
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
|
|
114
|
+
>>> c.find('0x48')
|
|
115
|
+
(8,)
|
|
116
|
+
>>> c.replace('0b001', '0xabc')
|
|
117
|
+
>>> c.insert('0b0000', pos=3)
|
|
118
|
+
>>> del c[12:16]
|
|
119
|
+
```
|
|
134
120
|
|
|
135
121
|
### Arrays of fixed-length formats
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
122
|
+
```pycon
|
|
123
|
+
>>> from bitstring import Array
|
|
124
|
+
>>> a = Array('uint7', [9, 100, 3, 1])
|
|
125
|
+
>>> a.data
|
|
126
|
+
BitArray('0x1390181')
|
|
127
|
+
>>> a[::2] *= 5
|
|
128
|
+
>>> a
|
|
129
|
+
Array('uint7', [45, 100, 15, 1])
|
|
130
|
+
```
|
|
145
131
|
|
|
146
132
|
|
|
147
133
|
<sub>Copyright (c) 2006 - 2024 Scott Griffiths</sub>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
bitstring/__init__.py,sha256=GjT0V01IeR3DBanzVa5utd4csONZx-PUSr6H0fuXnNU,13899
|
|
2
|
+
bitstring/__main__.py,sha256=Lg6ARiAqKssp_uj1msYpiTGDybhJRshQmeftQ-yTpc0,1658
|
|
3
|
+
bitstring/array_.py,sha256=d4vuZAc0ecJC95RD5_WPApehyH-ZGms4pcxl2I0R90I,36201
|
|
4
|
+
bitstring/bitarray_.py,sha256=kZTVYBo32vQuhQ4TZQldQkqRE5OMPVoKCoxEfKOTg8U,22319
|
|
5
|
+
bitstring/bits.py,sha256=N2bszuYM_WrTfqx-5-m56Vu_XgPZLeTta4xysBqCSYs,76754
|
|
6
|
+
bitstring/bitstore.py,sha256=jHk1cwBPqCJoYq2TEgIE37u_7K8HZq5K0L__X8edMgc,10337
|
|
7
|
+
bitstring/bitstore_helpers.py,sha256=6UHBomeOZlfQmY_Wa17U1zi7FBo73n2D9GPPmPztIrk,9107
|
|
8
|
+
bitstring/bitstream.py,sha256=Hu1ZpcCeP6lfpJiKAwKoJI22A-vsKATN1F2RvnS3rFY,29017
|
|
9
|
+
bitstring/bitstring_options.py,sha256=54Io4xLDylwXdRQpb12eNkSpSXQhRTW9Aidx7G42h7w,3560
|
|
10
|
+
bitstring/dtypes.py,sha256=oBrcavnz15vaBZ_GOVm0tOaW6Ml0oXoz8RyZSyqG6Zo,16897
|
|
11
|
+
bitstring/exceptions.py,sha256=7O3oVJJgvUOadqIzg6U-8fUGyOTu_lmjbMtX3yPc18o,553
|
|
12
|
+
bitstring/fp8.py,sha256=8CIiGlijn9OmBwdpJOl-1oVPUY6qVL2jGl1N8u8LjAg,3768
|
|
13
|
+
bitstring/luts.py,sha256=KPuXC9MvTOQSOlb0bFBKbnuxXuVBYlhaFEdIgSrwtzs,27072
|
|
14
|
+
bitstring/methods.py,sha256=3JM4jo8B6r3xQYY2nvbibmf7byQYTHTZye0F8D2lF10,4351
|
|
15
|
+
bitstring/mxfp.py,sha256=IxXK0SZy5RTD883YwATg6tlqVfl1uCFQplp5BVBwx9Q,9058
|
|
16
|
+
bitstring/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
+
bitstring/utils.py,sha256=tuWHppQUcCVLbtrZoO7eUkpNk6pJLtsOZT7QdWADW78,9221
|
|
18
|
+
bitstring-4.2.3.dist-info/LICENSE,sha256=NwXu1akj812b-sofEOkTbMhNbldlcK7GYb2mmZHxKeo,1106
|
|
19
|
+
bitstring-4.2.3.dist-info/METADATA,sha256=aUUR8ljmKXchDLSPxTGzHot_7Wi659yUYUWS5Zi-3Ek,5017
|
|
20
|
+
bitstring-4.2.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
21
|
+
bitstring-4.2.3.dist-info/top_level.txt,sha256=9Xh4qfKH0fMhwxzzkSBk3uzTRGiYPPM1FuYFAgCB-ks,10
|
|
22
|
+
bitstring-4.2.3.dist-info/RECORD,,
|
bitstring-4.2.2.dist-info/RECORD
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
bitstring/__init__.py,sha256=cSFzwMWpgUBbvhQGZpW9bl10oPVTVROMa9Au8I75FOY,13972
|
|
2
|
-
bitstring/__main__.py,sha256=p0n1Dy1TvWtIYQPuUiTwZT70PxpdnML7h4pxJP423dU,1657
|
|
3
|
-
bitstring/array_.py,sha256=dlknDIuqz1urgkHMMQnv5HaoZ8gPNBAMyGw2QF-UHvs,35860
|
|
4
|
-
bitstring/bitarray_.py,sha256=rtrkJwIODWNA1ca06x3p2BV1lka-BJDSOHOFsw8WbNg,22285
|
|
5
|
-
bitstring/bits.py,sha256=IG7zXU3rF0iq5_pWJuvahja8Yc-y5G3j-rNivHFr0i0,76821
|
|
6
|
-
bitstring/bitstore.py,sha256=0o2-PUixhRToCpH43MNsjJE1THVQib3DcbMICuNZnZ0,10326
|
|
7
|
-
bitstring/bitstore_helpers.py,sha256=ZaMU9WRWnE4FRP2tDveii25yKFnOcMrdHBCQdBFcf8A,9068
|
|
8
|
-
bitstring/bitstream.py,sha256=w52K1yvRTA0Trl6-tq0oVwTUYtz_5O3mvsQEVEpp1V8,29016
|
|
9
|
-
bitstring/bitstring_options.py,sha256=lUQu_3mFf4y58nPwrwap_mLiu7ydGWLp8x_dAEW2W4Y,3558
|
|
10
|
-
bitstring/dtypes.py,sha256=cW8yLYCbrKWsMOGYUNJ-a8bszg-cJJsHrVzajsocPCw,16892
|
|
11
|
-
bitstring/exceptions.py,sha256=xuatJ43eWemtEnfxKNNtK3_7RNhnzqxVodTlHhvKDbg,554
|
|
12
|
-
bitstring/fp8.py,sha256=8CIiGlijn9OmBwdpJOl-1oVPUY6qVL2jGl1N8u8LjAg,3768
|
|
13
|
-
bitstring/luts.py,sha256=M-Qwr_SKr8GId0QKWavq4edHTv7qnVUFr0Vc1m9O7pY,27340
|
|
14
|
-
bitstring/methods.py,sha256=3JM4jo8B6r3xQYY2nvbibmf7byQYTHTZye0F8D2lF10,4351
|
|
15
|
-
bitstring/mxfp.py,sha256=MSliuiofx8uM0HpqoBnYpAx7ih9V0Knx8KcFy9_zSho,8861
|
|
16
|
-
bitstring/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
-
bitstring/utils.py,sha256=yCf0G3l54YXNPMSBS9bMgt2NQBWwbNqzDAcGRz_Ymn4,9223
|
|
18
|
-
bitstring-4.2.2.dist-info/LICENSE,sha256=NwXu1akj812b-sofEOkTbMhNbldlcK7GYb2mmZHxKeo,1106
|
|
19
|
-
bitstring-4.2.2.dist-info/METADATA,sha256=IwJ1xLsqB-Tn65n1RPTGOLND-rLDEhSGPt5h-KglpoE,5846
|
|
20
|
-
bitstring-4.2.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
21
|
-
bitstring-4.2.2.dist-info/top_level.txt,sha256=9Xh4qfKH0fMhwxzzkSBk3uzTRGiYPPM1FuYFAgCB-ks,10
|
|
22
|
-
bitstring-4.2.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|