core: add bit_transpose function

.. this version can only handle exactly 8 bits "across".  The restriction
may be relaxed in a future revision.
This commit is contained in:
Jeff Epler 2021-02-16 18:37:11 -06:00
parent 261b077209
commit 9cf7d73c6c
9 changed files with 255 additions and 0 deletions

View File

@ -1092,6 +1092,10 @@ msgstr ""
msgid "Initialization failed due to lack of memory"
msgstr ""
#: shared-bindings/_bit_transpose/__init__.c
msgid "Input buffer must be a multiple of 8 bytes"
msgstr ""
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
msgid "Input taking too long"
msgstr ""
@ -1659,6 +1663,10 @@ msgstr ""
msgid "Out of sockets"
msgstr ""
#: shared-bindings/_bit_transpose/__init__.c
msgid "Output buffer must be at least as big as input buffer"
msgstr ""
#: shared-bindings/audiobusio/PDMIn.c
msgid "Oversample must be multiple of 8."
msgstr ""

View File

@ -24,6 +24,7 @@ CIRCUITPY_NEOPIXEL_WRITE = 0
endif
CIRCUITPY_FULL_BUILD = 1
CIRCUITPY_BIT_TRANSPOSE = 1
CIRCUITPY_PWMIO = 1
# Things that need to be implemented.

View File

@ -132,6 +132,10 @@ endif
ifeq ($(CIRCUITPY_AUDIOMP3),1)
SRC_PATTERNS += audiomp3/%
endif
ifeq ($(CIRCUITPY_BIT_TRANSPOSE),1)
$(info BIT_TRANSPOSE enabled)
SRC_PATTERNS += _bit_transpose/%
endif
ifeq ($(CIRCUITPY_BITBANGIO),1)
SRC_PATTERNS += bitbangio/%
endif
@ -440,6 +444,7 @@ SRC_BINDINGS_ENUMS += \
util.c
SRC_SHARED_MODULE_ALL = \
_bit_transpose/__init__.c \
_bleio/Address.c \
_bleio/Attribute.c \
_bleio/ScanEntry.c \

View File

@ -299,6 +299,14 @@ extern const struct _mp_obj_module_t audiopwmio_module;
#define BINASCII_MODULE
#endif
#if CIRCUITPY_BIT_TRANSPOSE
extern const struct _mp_obj_module_t bit_transpose_module;
#define BIT_TRANSPOSE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__bit_transpose),(mp_obj_t)&bit_transpose_module },
#else
#define BIT_TRANSPOSE_MODULE
#endif
#if CIRCUITPY_BITBANGIO
#define BITBANGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module },
extern const struct _mp_obj_module_t bitbangio_module;
@ -819,6 +827,7 @@ extern const struct _mp_obj_module_t msgpack_module;
AUDIOMP3_MODULE \
AUDIOPWMIO_MODULE \
BINASCII_MODULE \
BIT_TRANSPOSE_MODULE \
BITBANGIO_MODULE \
BLEIO_MODULE \
BOARD_MODULE \

View File

@ -89,6 +89,9 @@ CFLAGS += -DCIRCUITPY_AUDIOMP3=$(CIRCUITPY_AUDIOMP3)
CIRCUITPY_BINASCII ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BINASCII=$(CIRCUITPY_BINASCII)
CIRCUITPY_BIT_TRANSPOSE ?= 0
CFLAGS += -DCIRCUITPY_BIT_TRANSPOSE=$(CIRCUITPY_BIT_TRANSPOSE)
CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO)

View File

@ -0,0 +1,87 @@
/*
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Roy Hooper
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/_bit_transpose/__init__.h"
//| """A fast bit transposition function for parallel NeoPixel strips
//|
//| When driving multiple NeoPixel strips from a shift register, the bits
//| must be re-ordered in a specific way. This module offers a low-level
//| routine for performing the transformation."""
//|
//| def bit_transpose(input: _typing.ReadableBuffer, *, output: Optional[_typing.WritableBuffer]=None):
//| """Convert a sequence of 8*N pixel values into a single stream of bytes suitable for sending via a parallel conversion method (PioPixl8)
//|
//| Returns the output buffer if specified (which must be big enough to hold the result), otherwise a freshly allocated buffer."""
//| ...
//|
STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_input, ARG_output };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED, {} },
{ MP_QSTR_output, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_buffer_info_t input_bufinfo;
mp_buffer_info_t output_bufinfo;
mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ);
int n = input_bufinfo.len;
if (n % 8 != 0) {
mp_raise_ValueError(translate("Input buffer must be a multiple of 8 bytes"));
}
mp_obj_t output = args[ARG_output].u_obj;
if (!output || output == mp_const_none) {
output = mp_obj_new_bytearray_of_zeros(n);
}
mp_get_buffer_raise(output, &output_bufinfo, MP_BUFFER_WRITE);
int m = output_bufinfo.len;
if (m < n) {
mp_raise_ValueError(translate("Output buffer must be at least as big as input buffer"));
}
common_hal_bit_transpose_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, input_bufinfo.len);
return output;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bit_transpose_bit_transpose_obj, 1, bit_transpose);
STATIC const mp_rom_map_elem_t bit_transpose_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bit_transpose) },
{ MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bit_transpose_bit_transpose_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bit_transpose_module_globals, bit_transpose_module_globals_table);
const mp_obj_module_t bit_transpose_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&bit_transpose_module_globals,
};

View File

@ -0,0 +1,32 @@
/*
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Jeff Epler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#include <stdint.h>
#include <stdlib.h>
void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t n);

View File

@ -0,0 +1,83 @@
/*
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Jeff Epler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/_bit_transpose/__init__.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// adapted from "Hacker's Delight" - Figure 7-2 Transposing an 8x8-bit matrix
// basic idea is:
// > First, treat the 8x8-bit matrix as 16 2x2-bit matrices, and transpose each
// > of the 16 2x2-bit matrices. Second, treat the matrix as four 2x2 submatrices
// > whose elements are 2x2-bit matrices and transpose each of the four 2x2
// > submatrices. Finally, treat the matrix as a 2x2 matrix whose elements are
// > 4x4-bit matrices, and transpose the 2x2 matrix. These transformations are
// > illustrated below.
// We want a different definition of bit/byte order, deal with strides differently, etc.
// so the code is heavily re-worked compared to the original.
static void transpose8(uint32_t *result, const uint8_t *src, int src_stride) {
uint32_t x, y, t;
y = *src; src += src_stride;
y |= (*src << 8); src += src_stride;
y |= (*src << 16); src += src_stride;
y |= (*src << 24); src += src_stride;
x = *src; src += src_stride;
x |= (*src << 8); src += src_stride;
x |= (*src << 16); src += src_stride;
x |= (*src << 24); src += src_stride;
t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
x = t;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
x = __builtin_bswap32(x);
y = __builtin_bswap32(y);
#endif
result[0] = x;
result[1] = y;
}
static void bit_transpose(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n) {
for(size_t i=0; i<n; i++) {
transpose8(result, src, src_stride);
result += 2;
src += 1;
}
}
void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t n) {
bit_transpose((uint32_t*)(void*)result, src, n/8, n/8);
}

View File

@ -0,0 +1,27 @@
/*
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Jeff Epler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once