Skip to content

Commit 1cd4e45

Browse files
authored
Merge pull request #4219 from jepler/bit_transpose
Add Bit transpose function to support piopixl8
2 parents 0ecb24c + b854c06 commit 1cd4e45

File tree

9 files changed

+343
-0
lines changed

9 files changed

+343
-0
lines changed

locale/circuitpython.pot

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

1095+
#: shared-bindings/bitops/__init__.c
1096+
#, c-format
1097+
msgid "Input buffer length (%d) must be a multiple of the strand count (%d)"
1098+
msgstr ""
1099+
10951100
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
10961101
msgid "Input taking too long"
10971102
msgstr ""
@@ -1659,6 +1664,11 @@ msgstr ""
16591664
msgid "Out of sockets"
16601665
msgstr ""
16611666

1667+
#: shared-bindings/bitops/__init__.c
1668+
#, c-format
1669+
msgid "Output buffer must be at least %d bytes"
1670+
msgstr ""
1671+
16621672
#: shared-bindings/audiobusio/PDMIn.c
16631673
msgid "Oversample must be multiple of 8."
16641674
msgstr ""
@@ -4101,6 +4111,11 @@ msgstr ""
41014111
msgid "watchdog timeout must be greater than 0"
41024112
msgstr ""
41034113

4114+
#: shared-bindings/bitops/__init__.c
4115+
#, c-format
4116+
msgid "width must be from 2 to 8 (inclusive), not %d"
4117+
msgstr ""
4118+
41044119
#: shared-bindings/rgbmatrix/RGBMatrix.c
41054120
msgid "width must be greater than zero"
41064121
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_BITOPS = 1
2728
CIRCUITPY_PWMIO = 1
2829

2930
# Things that need to be implemented.

py/circuitpy_defns.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ endif
132132
ifeq ($(CIRCUITPY_AUDIOMP3),1)
133133
SRC_PATTERNS += audiomp3/%
134134
endif
135+
ifeq ($(CIRCUITPY_BITOPS),1)
136+
SRC_PATTERNS += bitops/%
137+
endif
135138
ifeq ($(CIRCUITPY_BITBANGIO),1)
136139
SRC_PATTERNS += bitbangio/%
137140
endif
@@ -466,6 +469,7 @@ SRC_SHARED_MODULE_ALL = \
466469
bitbangio/OneWire.c \
467470
bitbangio/SPI.c \
468471
bitbangio/__init__.c \
472+
bitops/__init__.c \
469473
board/__init__.c \
470474
adafruit_bus_device/__init__.c \
471475
adafruit_bus_device/I2CDevice.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_BITOPS
303+
extern const struct _mp_obj_module_t bitops_module;
304+
#define BITOPS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitops),(mp_obj_t)&bitops_module },
305+
#else
306+
#define BITOPS_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+
BITOPS_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_BITOPS ?= 0
93+
CFLAGS += -DCIRCUITPY_BITOPS=$(CIRCUITPY_BITOPS)
94+
9295
CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD)
9396
CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO)
9497

shared-bindings/bitops/__init__.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 for Adafruit Industries
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/bitops/__init__.h"
31+
32+
//| """Routines for low-level manipulation of binary data"""
33+
//|
34+
//|
35+
36+
//| def bit_transpose(input: ReadableBuffer, output: WriteableBuffer, width:int = 8) -> WriteableBuffer:
37+
//| """"Transpose" a buffer by assembling each output byte with bits taken from each of ``width`` different input bytes.
38+
//|
39+
//| This can be useful to convert a sequence of pixel values into a single
40+
//| stream of bytes suitable for sending via a parallel conversion method.
41+
//|
42+
//| The number of bytes in the input buffer must be a multiple of the width,
43+
//| and the width can be any value from 2 to 8. If the width is fewer than 8,
44+
//| then the remaining (less significant) bits of the output are set to zero.
45+
//|
46+
//| Let ``stride = len(input)//width``. Then the first byte is made out of the
47+
//| most significant bits of ``[input[0], input[stride], input[2*stride], ...]``.
48+
//| The second byte is made out of the second bits, and so on until the 8th output
49+
//| byte which is made of the first bits of ``input[1], input[1+stride,
50+
//| input[2*stride], ...]``.
51+
//|
52+
//| The required output buffer size is ``len(input) * 8 // width``.
53+
//|
54+
//| Returns the output buffer."""
55+
//| ...
56+
57+
STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
58+
enum { ARG_input, ARG_output, ARG_width };
59+
static const mp_arg_t allowed_args[] = {
60+
{ MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED },
61+
{ MP_QSTR_output, MP_ARG_OBJ | MP_ARG_REQUIRED },
62+
{ MP_QSTR_width, MP_ARG_INT, { .u_int = 8 } },
63+
};
64+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
65+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
66+
67+
int width = args[ARG_width].u_int;
68+
if (width < 2 || width > 8) {
69+
mp_raise_ValueError_varg(translate("width must be from 2 to 8 (inclusive), not %d"), width);
70+
}
71+
72+
mp_buffer_info_t input_bufinfo;
73+
mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ);
74+
int inlen = input_bufinfo.len;
75+
if (inlen % width != 0) {
76+
mp_raise_ValueError_varg(translate("Input buffer length (%d) must be a multiple of the strand count (%d)"), inlen, width);
77+
}
78+
79+
mp_buffer_info_t output_bufinfo;
80+
mp_get_buffer_raise(args[ARG_output].u_obj, &output_bufinfo, MP_BUFFER_WRITE);
81+
int avail = output_bufinfo.len;
82+
int outlen = 8 * (inlen / width);
83+
if (avail < outlen) {
84+
mp_raise_ValueError_varg(translate("Output buffer must be at least %d bytes"), outlen);
85+
}
86+
common_hal_bitops_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, inlen, width);
87+
return args[ARG_output].u_obj;
88+
}
89+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bitops_bit_transpose_obj, 1, bit_transpose);
90+
91+
STATIC const mp_rom_map_elem_t bitops_module_globals_table[] = {
92+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitops) },
93+
{ MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bitops_bit_transpose_obj) },
94+
};
95+
96+
STATIC MP_DEFINE_CONST_DICT(bitops_module_globals, bitops_module_globals_table);
97+
98+
const mp_obj_module_t bitops_module = {
99+
.base = { &mp_type_module },
100+
.globals = (mp_obj_dict_t*)&bitops_module_globals,
101+
};

shared-bindings/bitops/__init__.h

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_bitops_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands);

shared-module/bitops/__init__.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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 for Adafruit Industries
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/bitops/__init__.h"
28+
29+
#include <stdint.h>
30+
#include <stdlib.h>
31+
#include <string.h>
32+
33+
#ifdef __GNUC__
34+
#define FALLTHROUGH __attribute__((fallthrough))
35+
#else
36+
#define FALLTHROUGH ((void)0) /* FALLTHROUGH */
37+
#endif
38+
39+
// adapted from "Hacker's Delight" - Figure 7-2 Transposing an 8x8-bit matrix
40+
// basic idea is:
41+
// > First, treat the 8x8-bit matrix as 16 2x2-bit matrices, and transpose each
42+
// > of the 16 2x2-bit matrices. Second, treat the matrix as four 2x2 submatrices
43+
// > whose elements are 2x2-bit matrices and transpose each of the four 2x2
44+
// > submatrices. Finally, treat the matrix as a 2x2 matrix whose elements are
45+
// > 4x4-bit matrices, and transpose the 2x2 matrix. These transformations are
46+
// > illustrated below.
47+
// We want a different definition of bit/byte order, deal with strides differently, etc.
48+
// so the code is heavily re-worked compared to the original.
49+
static void transpose_var(uint32_t *result, const uint8_t *src, int src_stride, int num_strands) {
50+
uint32_t x = 0, y = 0, t;
51+
52+
src += (num_strands-1) * src_stride;
53+
54+
switch(num_strands) {
55+
case 7:
56+
x |= *src << 16;
57+
src -= src_stride;
58+
FALLTHROUGH;
59+
case 6:
60+
x |= *src << 8;
61+
src -= src_stride;
62+
FALLTHROUGH;
63+
case 5:
64+
x |= *src;
65+
src -= src_stride;
66+
FALLTHROUGH;
67+
case 4:
68+
y |= *src << 24;
69+
src -= src_stride;
70+
FALLTHROUGH;
71+
case 3:
72+
y |= *src << 16;
73+
src -= src_stride;
74+
FALLTHROUGH;
75+
case 2:
76+
y |= *src << 8;
77+
src -= src_stride;
78+
y |= *src;
79+
}
80+
81+
t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
82+
t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
83+
84+
t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
85+
t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
86+
87+
t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
88+
y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
89+
x = t;
90+
91+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
92+
x = __builtin_bswap32(x);
93+
y = __builtin_bswap32(y);
94+
#endif
95+
result[0] = x;
96+
result[1] = y;
97+
}
98+
99+
static void transpose_8(uint32_t *result, const uint8_t *src, int src_stride) {
100+
uint32_t x, y, t;
101+
102+
y = *src; src += src_stride;
103+
y |= (*src << 8); src += src_stride;
104+
y |= (*src << 16); src += src_stride;
105+
y |= (*src << 24); src += src_stride;
106+
x = *src; src += src_stride;
107+
x |= (*src << 8); src += src_stride;
108+
x |= (*src << 16); src += src_stride;
109+
x |= (*src << 24); src += src_stride;
110+
111+
t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
112+
t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
113+
114+
t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
115+
t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
116+
117+
t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
118+
y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
119+
x = t;
120+
121+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
122+
x = __builtin_bswap32(x);
123+
y = __builtin_bswap32(y);
124+
#endif
125+
result[0] = x;
126+
result[1] = y;
127+
}
128+
129+
static void bit_transpose_8(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n) {
130+
for(size_t i=0; i<n; i++) {
131+
transpose_8(result, src, src_stride);
132+
result += 2;
133+
src += 1;
134+
}
135+
}
136+
137+
static void bit_transpose_var(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n, int num_strands) {
138+
for(size_t i=0; i<n; i++) {
139+
transpose_var(result, src, src_stride, num_strands);
140+
result += 2;
141+
src += 1;
142+
}
143+
}
144+
145+
void common_hal_bitops_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands) {
146+
if(num_strands == 8) {
147+
bit_transpose_8((uint32_t*)(void*)result, src, inlen/8, inlen/8);
148+
} else {
149+
bit_transpose_var((uint32_t*)(void*)result, src, inlen/num_strands, inlen/num_strands, num_strands);
150+
}
151+
}

0 commit comments

Comments
 (0)