bvwx 0.1.0__tar.gz

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.
bvwx-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.2
2
+ Name: bvwx
3
+ Version: 0.1.0
4
+ Summary: Bit Vectors With Xes
5
+ Project-URL: Repository, https://github.com/cjdrake/bvwx.git
6
+ Requires-Python: >=3.12
7
+ Description-Content-Type: text/markdown
8
+
9
+ # Bit Vectors With Xes
bvwx-0.1.0/README.md ADDED
@@ -0,0 +1 @@
1
+ # Bit Vectors With Xes
@@ -0,0 +1,14 @@
1
+ [project]
2
+ name = "bvwx"
3
+ version = "0.1.0"
4
+ description = "Bit Vectors With Xes"
5
+ readme = "README.md"
6
+
7
+ requires-python = ">=3.12"
8
+ dependencies = []
9
+
10
+ [project.urls]
11
+ Repository = "https://github.com/cjdrake/bvwx.git"
12
+
13
+ [tool.black]
14
+ line-length = 100
bvwx-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,86 @@
1
+ """Bit Vectors With Xes"""
2
+
3
+ from ._arithmetic import adc, add, div, lsh, mod, mul, neg, ngc, rsh, sbc, srsh, sub
4
+ from ._bits import Array, Bits, Empty, Scalar, Vector, bits, i2bv, stack, u2bv
5
+ from ._bitwise import and_, impl, ite, mux, nand, nor, not_, or_, xnor, xor
6
+ from ._code import decode, encode_onehot, encode_priority
7
+ from ._enum import Enum
8
+ from ._predicate import eq, ge, gt, le, lt, match, ne, sge, sgt, sle, slt
9
+ from ._struct import Struct
10
+ from ._unary import uand, uor, uxnor, uxor
11
+ from ._union import Union
12
+ from ._word import cat, lrot, pack, rep, rrot, sxt, xt
13
+
14
+ # Alias Vector to Vec for brevity
15
+ Vec = Vector
16
+
17
+ __all__ = [
18
+ # bits
19
+ "Bits",
20
+ "Empty",
21
+ "Scalar",
22
+ "Vector",
23
+ "Vec",
24
+ "Array",
25
+ "Enum",
26
+ "Struct",
27
+ "Union",
28
+ # bitwise
29
+ "not_",
30
+ "nor",
31
+ "or_",
32
+ "nand",
33
+ "and_",
34
+ "xnor",
35
+ "xor",
36
+ "impl",
37
+ "ite",
38
+ "mux",
39
+ # unary
40
+ "uor",
41
+ "uand",
42
+ "uxnor",
43
+ "uxor",
44
+ # encode/decode
45
+ "encode_onehot",
46
+ "encode_priority",
47
+ "decode",
48
+ # arithmetic
49
+ "add",
50
+ "adc",
51
+ "sub",
52
+ "sbc",
53
+ "neg",
54
+ "ngc",
55
+ "mul",
56
+ "div",
57
+ "mod",
58
+ "lsh",
59
+ "rsh",
60
+ "srsh",
61
+ # word
62
+ "xt",
63
+ "sxt",
64
+ "lrot",
65
+ "rrot",
66
+ "cat",
67
+ "rep",
68
+ "pack",
69
+ # predicate
70
+ "match",
71
+ "eq",
72
+ "ne",
73
+ "lt",
74
+ "le",
75
+ "gt",
76
+ "ge",
77
+ "slt",
78
+ "sle",
79
+ "sgt",
80
+ "sge",
81
+ # factory
82
+ "bits",
83
+ "stack",
84
+ "u2bv",
85
+ "i2bv",
86
+ ]
@@ -0,0 +1,320 @@
1
+ """Arithmetic Operators"""
2
+
3
+ from ._bits import (
4
+ Bits,
5
+ Scalar,
6
+ Vector,
7
+ _add,
8
+ _cat,
9
+ _div,
10
+ _expect_shift,
11
+ _expect_size,
12
+ _expect_type,
13
+ _lsh,
14
+ _mod,
15
+ _mul,
16
+ _neg,
17
+ _rsh,
18
+ _Scalar0,
19
+ _srsh,
20
+ _sub,
21
+ )
22
+
23
+
24
+ def add(a: Bits | str, b: Bits | str, ci: Scalar | str | None = None) -> Bits:
25
+ """Addition with carry-in, but NO carry-out.
26
+
27
+ For example:
28
+
29
+ >>> add("4d2", "4d2")
30
+ bits("4b0100")
31
+
32
+ >>> add("2d2", "2d2")
33
+ bits("2b00")
34
+
35
+ Args:
36
+ a: ``Bits`` or string literal
37
+ b: ``Bits`` or string literal
38
+ ci: ``Scalar`` carry-in, or ``None``.
39
+ ``None`` defaults to carry-in ``0``.
40
+
41
+ Returns:
42
+ ``Bits`` sum w/ size equal to ``max(a.size, b.size)``.
43
+
44
+ Raises:
45
+ TypeError: ``a``, ``b``, or ``ci`` are not valid ``Bits`` objects.
46
+ ValueError: Error parsing string literal.
47
+ """
48
+ a = _expect_type(a, Bits)
49
+ b = _expect_type(b, Bits)
50
+ ci = _Scalar0 if ci is None else _expect_type(ci, Scalar)
51
+ s, _ = _add(a, b, ci)
52
+ return s
53
+
54
+
55
+ def adc(a: Bits | str, b: Bits | str, ci: Scalar | str | None = None) -> Vector:
56
+ """Addition with carry-in, and carry-out.
57
+
58
+ For example:
59
+
60
+ >>> adc("4d2", "4d2")
61
+ bits("5b0_0100")
62
+
63
+ >>> adc("2d2", "2d2")
64
+ bits("3b100")
65
+
66
+ Args:
67
+ a: ``Bits`` or string literal
68
+ b: ``Bits`` or string literal
69
+ ci: ``Scalar`` carry-in, or ``None``.
70
+ ``None`` defaults to carry-in ``0``.
71
+
72
+ Returns:
73
+ ``Vector`` sum w/ size equal to ``max(a.size, b.size) + 1``.
74
+ The most significant bit is the carry-out.
75
+
76
+ Raises:
77
+ TypeError: ``a``, ``b``, or ``ci`` are not valid ``Bits`` objects.
78
+ ValueError: Error parsing string literal.
79
+ """
80
+ a = _expect_type(a, Bits)
81
+ b = _expect_type(b, Bits)
82
+ ci = _Scalar0 if ci is None else _expect_type(ci, Scalar)
83
+ s, co = _add(a, b, ci)
84
+ return _cat(s, co)
85
+
86
+
87
+ def sub(a: Bits | str, b: Bits | str) -> Bits:
88
+ """Twos complement subtraction, with NO carry-out.
89
+
90
+ Args:
91
+ a: ``Bits`` or string literal
92
+ b: ``Bits`` or string literal equal size to ``a``.
93
+
94
+ Returns:
95
+ ``Bits`` sum equal size to ``a`` and ``b``.
96
+
97
+ Raises:
98
+ TypeError: ``a``, or ``b`` are not valid ``Bits`` objects,
99
+ or ``a`` not equal size to ``b``.
100
+ ValueError: Error parsing string literal.
101
+ """
102
+ a = _expect_type(a, Bits)
103
+ b = _expect_size(b, a.size)
104
+ s, _ = _sub(a, b)
105
+ return s
106
+
107
+
108
+ def sbc(a: Bits | str, b: Bits | str) -> Vector:
109
+ """Twos complement subtraction, with carry-out.
110
+
111
+ Args:
112
+ a: ``Bits`` or string literal
113
+ b: ``Bits`` or string literal equal size to ``a``.
114
+
115
+ Returns:
116
+ ``Bits`` sum w/ size one larger than ``a`` and ``b``.
117
+ The most significant bit is the carry-out.
118
+
119
+ Raises:
120
+ TypeError: ``a``, or ``b`` are not valid ``Bits`` objects,
121
+ or ``a`` not equal size to ``b``.
122
+ ValueError: Error parsing string literal.
123
+ """
124
+ a = _expect_type(a, Bits)
125
+ b = _expect_size(b, a.size)
126
+ s, co = _sub(a, b)
127
+ return _cat(s, co)
128
+
129
+
130
+ def neg(x: Bits | str) -> Bits:
131
+ """Twos complement negation, with NO carry-out.
132
+
133
+ Args:
134
+ x: ``Bits`` or string literal
135
+
136
+ Returns:
137
+ ``Bits`` equal size to ``x``.
138
+
139
+ Raises:
140
+ TypeError: ``x`` is not a valid ``Bits`` object.
141
+ ValueError: Error parsing string literal.
142
+ """
143
+ x = _expect_type(x, Bits)
144
+ s, _ = _neg(x)
145
+ return s
146
+
147
+
148
+ def ngc(x: Bits | str) -> Vector:
149
+ """Twos complement negation, with carry-out.
150
+
151
+ Args:
152
+ x: ``Bits`` or string literal
153
+
154
+ Returns:
155
+ ``Bits`` w/ size one larger than ``x``.
156
+ The most significant bit is the carry-out.
157
+
158
+ Raises:
159
+ TypeError: ``x`` is not a valid ``Bits`` object.
160
+ ValueError: Error parsing string literal.
161
+ """
162
+ x = _expect_type(x, Bits)
163
+ s, co = _neg(x)
164
+ return _cat(s, co)
165
+
166
+
167
+ def mul(a: Bits | str, b: Bits | str) -> Vector:
168
+ """Unsigned multiply.
169
+
170
+ For example:
171
+
172
+ >>> mul("4d2", "4d2")
173
+ bits("8b0000_0100")
174
+
175
+ >>> add("2d2", "2d2")
176
+ bits("2b00")
177
+
178
+ Args:
179
+ a: ``Bits`` or string literal
180
+ b: ``Bits`` or string literal
181
+
182
+ Returns:
183
+ ``Vector`` product w/ size ``a.size + b.size``
184
+
185
+ Raises:
186
+ TypeError: ``a`` or ``b`` are not valid ``Bits`` objects.
187
+ ValueError: Error parsing string literal.
188
+ """
189
+ a = _expect_type(a, Bits)
190
+ b = _expect_type(b, Bits)
191
+ return _mul(a, b)
192
+
193
+
194
+ def div(a: Bits | str, b: Bits | str) -> Bits:
195
+ """Unsigned divide.
196
+
197
+ Args:
198
+ a: ``Bits`` or string literal
199
+ b: ``Bits`` or string literal
200
+
201
+ Returns:
202
+ ``Vector`` quotient w/ size ``a.size``
203
+
204
+ Raises:
205
+ TypeError: ``a`` or ``b`` are not valid ``Bits`` objects.
206
+ ValueError: Error parsing string literal.
207
+ """
208
+ a = _expect_type(a, Bits)
209
+ b = _expect_type(b, Bits)
210
+ if not a.size >= b.size > 0:
211
+ raise ValueError("Expected a.size ≥ b.size > 0")
212
+ return _div(a, b)
213
+
214
+
215
+ def mod(a: Bits | str, b: Bits | str) -> Bits:
216
+ """Unsigned modulo.
217
+
218
+ Args:
219
+ a: ``Bits`` or string literal
220
+ b: ``Bits`` or string literal
221
+
222
+ Returns:
223
+ ``Vector`` remainder w/ size ``b.size``
224
+
225
+ Raises:
226
+ TypeError: ``a`` or ``b`` are not valid ``Bits`` objects.
227
+ ValueError: Error parsing string literal.
228
+ """
229
+ a = _expect_type(a, Bits)
230
+ b = _expect_type(b, Bits)
231
+ if not a.size >= b.size > 0:
232
+ raise ValueError("Expected a.size ≥ b.size > 0")
233
+ return _mod(a, b)
234
+
235
+
236
+ def lsh(x: Bits | str, n: Bits | str | int) -> Bits:
237
+ """Logical left shift by n bits.
238
+
239
+ Fill bits with zeros.
240
+
241
+ For example:
242
+
243
+ >>> lsh("4b1011", 2)
244
+ bits("4b1100")
245
+
246
+ Args:
247
+ x: ``Bits`` or string literal.
248
+ n: ``Bits``, string literal, or ``int``
249
+ Non-negative bit shift count.
250
+
251
+ Returns:
252
+ ``Bits`` left-shifted by n bits.
253
+
254
+ Raises:
255
+ TypeError: ``x`` is not a valid ``Bits`` object,
256
+ or ``n`` is not a valid bit shift count.
257
+ ValueError: Error parsing string literal,
258
+ or negative shift amount.
259
+ """
260
+ x = _expect_type(x, Bits)
261
+ n = _expect_shift(n, x.size)
262
+ return _lsh(x, n)
263
+
264
+
265
+ def rsh(x: Bits | str, n: Bits | str | int) -> Bits:
266
+ """Logical right shift by n bits.
267
+
268
+ Fill bits with zeros.
269
+
270
+ For example:
271
+
272
+ >>> rsh("4b1101", 2)
273
+ bits("4b0011")
274
+
275
+ Args:
276
+ x: ``Bits`` or string literal.
277
+ n: ``Bits``, string literal, or ``int``
278
+ Non-negative bit shift count.
279
+
280
+ Returns:
281
+ ``Bits`` right-shifted by n bits.
282
+
283
+ Raises:
284
+ TypeError: ``x`` is not a valid ``Bits`` object,
285
+ or ``n`` is not a valid bit shift count.
286
+ ValueError: Error parsing string literal,
287
+ or negative shift amount.
288
+ """
289
+ x = _expect_type(x, Bits)
290
+ n = _expect_shift(n, x.size)
291
+ return _rsh(x, n)
292
+
293
+
294
+ def srsh(x: Bits | str, n: Bits | str | int) -> Bits:
295
+ """Arithmetic (signed) right shift by n bits.
296
+
297
+ Fill bits with most significant bit (sign).
298
+
299
+ For example:
300
+
301
+ >>> srsh("4b1101", 2)
302
+ bits("4b1111")
303
+
304
+ Args:
305
+ x: ``Bits`` or string literal.
306
+ n: ``Bits``, string literal, or ``int``
307
+ Non-negative bit shift count.
308
+
309
+ Returns:
310
+ ``Bits`` right-shifted by n bits.
311
+
312
+ Raises:
313
+ TypeError: ``x`` is not a valid ``Bits`` object,
314
+ or ``n`` is not a valid bit shift count.
315
+ ValueError: Error parsing string literal,
316
+ or negative shift amount.
317
+ """
318
+ x = _expect_type(x, Bits)
319
+ n = _expect_shift(n, x.size)
320
+ return _srsh(x, n)