Skip to content

Commit 9cf7d73

Browse files
committed
core: add bit_transpose function
.. this version can only handle exactly 8 bits "across". The restriction may be relaxed in a future revision.
1 parent 261b077 commit 9cf7d73

File tree

9 files changed

+255
-0
lines changed

9 files changed

+255
-0
lines changed

locale/circuitpython.pot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,10 @@ msgstr ""
10921092
msgid "Initialization failed due to lack of memory"
10931093
msgstr ""
10941094

1095+
#: shared-bindings/_bit_transpose/__init__.c
1096+
msgid "Input buffer must be a multiple of 8 bytes"
1097+
msgstr ""
1098+
10951099
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
10961100
msgid "Input taking too long"
10971101
msgstr ""
@@ -1659,6 +1663,10 @@ msgstr ""
16591663
msgid "Out of sockets"
16601664
msgstr ""
16611665

1666+
#: shared-bindings/_bit_transpose/__init__.c
1667+
msgid "Output buffer must be at least as big as input buffer"
1668+
msgstr ""
1669+
16621670
#: shared-bindings/audiobusio/PDMIn.c
16631671
msgid "Oversample must be multiple of 8."
16641672
msgstr ""

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CIRCUITPY_NEOPIXEL_WRITE = 0
2424
endif
2525

2626
CIRCUITPY_FULL_BUILD = 1
27+
CIRCUITPY_BIT_TRANSPOSE = 1
2728
CIRCUITPY_PWMIO = 1
2829

2930
# Things that need to be implemented.

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ endif
132132
ifeq ($(CIRCUITPY_AUDIOMP3),1)
133133
SRC_PATTERNS += audiomp3/%
134134
endif
135+
ifeq ($(CIRCUITPY_BIT_TRANSPOSE),1)
136+
$(info BIT_TRANSPOSE enabled)
137+
SRC_PATTERNS += _bit_transpose/%
138+
endif
135139
ifeq ($(CIRCUITPY_BITBANGIO),1)
136140
SRC_PATTERNS += bitbangio/%
137141
endif
@@ -440,6 +444,7 @@ SRC_BINDINGS_ENUMS += \
440444
util.c
441445

442446
SRC_SHARED_MODULE_ALL = \
447+
_bit_transpose/__init__.c \
443448
_bleio/Address.c \
444449
_bleio/Attribute.c \
445450
_bleio/ScanEntry.c \

py/circuitpy_mpconfig.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ extern const struct _mp_obj_module_t audiopwmio_module;
299299
#define BINASCII_MODULE
300300
#endif
301301

302+
#if CIRCUITPY_BIT_TRANSPOSE
303+
extern const struct _mp_obj_module_t bit_transpose_module;
304+
#define BIT_TRANSPOSE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__bit_transpose),(mp_obj_t)&bit_transpose_module },
305+
#else
306+
#define BIT_TRANSPOSE_MODULE
307+
#endif
308+
309+
302310
#if CIRCUITPY_BITBANGIO
303311
#define BITBANGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module },
304312
extern const struct _mp_obj_module_t bitbangio_module;
@@ -819,6 +827,7 @@ extern const struct _mp_obj_module_t msgpack_module;
819827
AUDIOMP3_MODULE \
820828
AUDIOPWMIO_MODULE \
821829
BINASCII_MODULE \
830+
BIT_TRANSPOSE_MODULE \
822831
BITBANGIO_MODULE \
823832
BLEIO_MODULE \
824833
BOARD_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ CFLAGS += -DCIRCUITPY_AUDIOMP3=$(CIRCUITPY_AUDIOMP3)
8989
CIRCUITPY_BINASCII ?= $(CIRCUITPY_FULL_BUILD)
9090
CFLAGS += -DCIRCUITPY_BINASCII=$(CIRCUITPY_BINASCII)
9191

92+
CIRCUITPY_BIT_TRANSPOSE ?= 0
93+
CFLAGS += -DCIRCUITPY_BIT_TRANSPOSE=$(CIRCUITPY_BIT_TRANSPOSE)
94+
9295
CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD)
9396
CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO)
9497

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Roy Hooper
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
#include "py/runtime.h"
29+
30+
#include "shared-bindings/_bit_transpose/__init__.h"
31+
32+
//| """A fast bit transposition function for parallel NeoPixel strips
33+
//|
34+
//| When driving multiple NeoPixel strips from a shift register, the bits
35+
//| must be re-ordered in a specific way. This module offers a low-level
36+
//| routine for performing the transformation."""
37+
//|
38+
39+
//| def bit_transpose(input: _typing.ReadableBuffer, *, output: Optional[_typing.WritableBuffer]=None):
40+
//| """Convert a sequence of 8*N pixel values into a single stream of bytes suitable for sending via a parallel conversion method (PioPixl8)
41+
//|
42+
//| Returns the output buffer if specified (which must be big enough to hold the result), otherwise a freshly allocated buffer."""
43+
//| ...
44+
//|
45+
STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
46+
enum { ARG_input, ARG_output };
47+
static const mp_arg_t allowed_args[] = {
48+
{ MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED, {} },
49+
{ MP_QSTR_output, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
50+
};
51+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
52+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
53+
54+
mp_buffer_info_t input_bufinfo;
55+
mp_buffer_info_t output_bufinfo;
56+
57+
mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ);
58+
int n = input_bufinfo.len;
59+
if (n % 8 != 0) {
60+
mp_raise_ValueError(translate("Input buffer must be a multiple of 8 bytes"));
61+
}
62+
mp_obj_t output = args[ARG_output].u_obj;
63+
64+
if (!output || output == mp_const_none) {
65+
output = mp_obj_new_bytearray_of_zeros(n);
66+
}
67+
mp_get_buffer_raise(output, &output_bufinfo, MP_BUFFER_WRITE);
68+
int m = output_bufinfo.len;
69+
if (m < n) {
70+
mp_raise_ValueError(translate("Output buffer must be at least as big as input buffer"));
71+
}
72+
common_hal_bit_transpose_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, input_bufinfo.len);
73+
return output;
74+
}
75+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bit_transpose_bit_transpose_obj, 1, bit_transpose);
76+
77+
STATIC const mp_rom_map_elem_t bit_transpose_module_globals_table[] = {
78+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bit_transpose) },
79+
{ MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bit_transpose_bit_transpose_obj) },
80+
};
81+
82+
STATIC MP_DEFINE_CONST_DICT(bit_transpose_module_globals, bit_transpose_module_globals_table);
83+
84+
const mp_obj_module_t bit_transpose_module = {
85+
.base = { &mp_type_module },
86+
.globals = (mp_obj_dict_t*)&bit_transpose_module_globals,
87+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jeff Epler
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once
28+
29+
#include <stdint.h>
30+
#include <stdlib.h>
31+
32+
void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t n);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jeff Epler
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/_bit_transpose/__init__.h"
28+
29+
#include <stdint.h>
30+
#include <stdlib.h>
31+
#include <string.h>
32+
33+
// adapted from "Hacker's Delight" - Figure 7-2 Transposing an 8x8-bit matrix
34+
// basic idea is:
35+
// > First, treat the 8x8-bit matrix as 16 2x2-bit matrices, and transpose each
36+
// > of the 16 2x2-bit matrices. Second, treat the matrix as four 2x2 submatrices
37+
// > whose elements are 2x2-bit matrices and transpose each of the four 2x2
38+
// > submatrices. Finally, treat the matrix as a 2x2 matrix whose elements are
39+
// > 4x4-bit matrices, and transpose the 2x2 matrix. These transformations are
40+
// > illustrated below.
41+
// We want a different definition of bit/byte order, deal with strides differently, etc.
42+
// so the code is heavily re-worked compared to the original.
43+
static void transpose8(uint32_t *result, const uint8_t *src, int src_stride) {
44+
uint32_t x, y, t;
45+
46+
y = *src; src += src_stride;
47+
y |= (*src << 8); src += src_stride;
48+
y |= (*src << 16); src += src_stride;
49+
y |= (*src << 24); src += src_stride;
50+
x = *src; src += src_stride;
51+
x |= (*src << 8); src += src_stride;
52+
x |= (*src << 16); src += src_stride;
53+
x |= (*src << 24); src += src_stride;
54+
55+
t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
56+
t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
57+
58+
t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
59+
t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
60+
61+
t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
62+
y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
63+
x = t;
64+
65+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
66+
x = __builtin_bswap32(x);
67+
y = __builtin_bswap32(y);
68+
#endif
69+
result[0] = x;
70+
result[1] = y;
71+
}
72+
73+
static void bit_transpose(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n) {
74+
for(size_t i=0; i<n; i++) {
75+
transpose8(result, src, src_stride);
76+
result += 2;
77+
src += 1;
78+
}
79+
}
80+
81+
void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t n) {
82+
bit_transpose((uint32_t*)(void*)result, src, n/8, n/8);
83+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jeff Epler
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once

0 commit comments

Comments
 (0)