Skip to content

Commit dd1f493

Browse files
committed
add setup_table() to header - avoid duplicate code
1 parent f640265 commit dd1f493

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

bitarray/_bitarray.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,6 @@ buffers_overlap(bitarrayobject *self, bitarrayobject *other)
189189
#undef PIB
190190
}
191191

192-
/* setup translation table, which maps each byte to its reversed:
193-
reverse_trans = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ..., 0xff} */
194-
static void
195-
setup_reverse_trans(void)
196-
{
197-
int j, k;
198-
199-
for (k = 0; k < 256; k++) {
200-
reverse_trans[k] = 0x00;
201-
for (j = 0; j < 8; j++)
202-
if (k & 128 >> j)
203-
reverse_trans[k] |= 1 << j;
204-
}
205-
}
206-
207192
/* reverse bits in first n characters of p */
208193
static void
209194
bytereverse(char *p, Py_ssize_t n)
@@ -4291,7 +4276,9 @@ PyInit__bitarray(void)
42914276
{
42924277
PyObject *m;
42934278

4294-
setup_reverse_trans();
4279+
/* setup translation table, which maps each byte to its reversed:
4280+
reverse_trans = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ..., 0xff} */
4281+
setup_table(reverse_trans, 0, 'r');
42954282

42964283
if ((m = PyModule_Create(&moduledef)) == NULL)
42974284
return NULL;

bitarray/_util.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,6 @@ Return parity of bitarray `a`.\n\
265265
`parity(a)` is equivalent to `a.count() % 2` but more efficient.");
266266

267267

268-
static void
269-
setup_table(char *table, int le, char op)
270-
{
271-
int j, k;
272-
273-
memset(table, 0, 256);
274-
for (k = 0; k < 256; k++) {
275-
for (j = 1; j < 8; j++) {
276-
if (( le && k & 0x01 << j) || /* little endian */
277-
(!le && k & 0x80 >> j)) /* big endian */
278-
switch (op) {
279-
case 'a': table[k] += j; break;
280-
case 'x': table[k] ^= j; break;
281-
default: Py_UNREACHABLE();
282-
}
283-
}
284-
}
285-
}
286-
287268
static PyObject *
288269
add_uint64(PyObject *number, uint64_t i)
289270
{
@@ -876,6 +857,7 @@ digit_to_int(int m, char c)
876857
static int setup = 0;
877858
int i;
878859

860+
assert(1 <= m && m <= 6);
879861
if (m < 5) { /* base 2, 4, 8, 16 */
880862
i = hex_to_int(c);
881863
return i >> m ? -1 : i;

bitarray/bitarray.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ swap_bytes(char *p, Py_ssize_t n)
225225
}
226226
}
227227

228+
/* write 256 characters into table for given endianness, kernel operation */
229+
static inline void
230+
setup_table(char *table, int le, char kop)
231+
{
232+
int j, k;
233+
for (k = 0; k < 256; k++) {
234+
table[k] = 0;
235+
for (j = 0; j < 8; j++) {
236+
if (( le && k & 0x01 << j) || /* little endian */
237+
(!le && k & 0x80 >> j)) /* big endian */
238+
switch (kop) {
239+
case 'a': table[k] += j; break;
240+
case 'x': table[k] ^= j; break;
241+
case 'r': table[k] |= 1 << j; break;
242+
default: Py_UNREACHABLE();
243+
}
244+
}
245+
}
246+
}
247+
228248
/* Return distance [0..3] to next aligned pointer.
229249
While on modern compilers uint64_t pointers may be misaligned, it may
230250
cause problems on older ones. Moreover, it may lead to slowdown (even

0 commit comments

Comments
 (0)