Skip to content

Commit 7fd4567

Browse files
committed
bitops: rename from _bit_transpose, describe the algorithm
1 parent ac0ffc5 commit 7fd4567

File tree

10 files changed

+122
-121
lines changed

10 files changed

+122
-121
lines changed

locale/circuitpython.pot

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

1095-
#: shared-bindings/_bit_transpose/__init__.c
1095+
#: shared-bindings/bitops/__init__.c
10961096
#, c-format
10971097
msgid "Input buffer length (%d) must be a multiple of the strand count (%d)"
10981098
msgstr ""
@@ -1664,7 +1664,7 @@ msgstr ""
16641664
msgid "Out of sockets"
16651665
msgstr ""
16661666

1667-
#: shared-bindings/_bit_transpose/__init__.c
1667+
#: shared-bindings/bitops/__init__.c
16681668
#, c-format
16691669
msgid "Output buffer must be at least %d bytes"
16701670
msgstr ""
@@ -3475,10 +3475,6 @@ msgstr ""
34753475
msgid "not enough arguments for format string"
34763476
msgstr ""
34773477

3478-
#: shared-bindings/_bit_transpose/__init__.c
3479-
msgid "num_strands must be from 2 to 8 (inclusive)"
3480-
msgstr ""
3481-
34823478
#: extmod/ulab/code/ulab_create.c
34833479
msgid "number of points must be at least 2"
34843480
msgstr ""
@@ -4115,6 +4111,10 @@ msgstr ""
41154111
msgid "watchdog timeout must be greater than 0"
41164112
msgstr ""
41174113

4114+
#: shared-bindings/bitops/__init__.c
4115+
msgid "width must be from 2 to 8 (inclusive)"
4116+
msgstr ""
4117+
41184118
#: shared-bindings/rgbmatrix/RGBMatrix.c
41194119
msgid "width must be greater than zero"
41204120
msgstr ""

ports/raspberrypi/mpconfigport.mk

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

2626
CIRCUITPY_FULL_BUILD = 1
27-
CIRCUITPY_BIT_TRANSPOSE = 1
27+
CIRCUITPY_BITOPS = 1
2828
CIRCUITPY_PWMIO = 1
2929

3030
# Things that need to be implemented.

py/circuitpy_defns.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ 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/%
135+
ifeq ($(CIRCUITPY_BITOPS),1)
136+
$(info BITOPS enabled)
137+
SRC_PATTERNS += bitops/%
138138
endif
139139
ifeq ($(CIRCUITPY_BITBANGIO),1)
140140
SRC_PATTERNS += bitbangio/%
@@ -444,7 +444,6 @@ SRC_BINDINGS_ENUMS += \
444444
util.c
445445

446446
SRC_SHARED_MODULE_ALL = \
447-
_bit_transpose/__init__.c \
448447
_bleio/Address.c \
449448
_bleio/Attribute.c \
450449
_bleio/ScanEntry.c \
@@ -471,6 +470,7 @@ SRC_SHARED_MODULE_ALL = \
471470
bitbangio/OneWire.c \
472471
bitbangio/SPI.c \
473472
bitbangio/__init__.c \
473+
bitops/__init__.c \
474474
board/__init__.c \
475475
adafruit_bus_device/__init__.c \
476476
adafruit_bus_device/I2CDevice.c \

py/circuitpy_mpconfig.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ 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 },
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 },
305305
#else
306-
#define BIT_TRANSPOSE_MODULE
306+
#define BITOPS_MODULE
307307
#endif
308308

309309

@@ -827,7 +827,7 @@ extern const struct _mp_obj_module_t msgpack_module;
827827
AUDIOMP3_MODULE \
828828
AUDIOPWMIO_MODULE \
829829
BINASCII_MODULE \
830-
BIT_TRANSPOSE_MODULE \
830+
BITOPS_MODULE \
831831
BITBANGIO_MODULE \
832832
BLEIO_MODULE \
833833
BOARD_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ 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)
92+
CIRCUITPY_BITOPS ?= 0
93+
CFLAGS += -DCIRCUITPY_BITOPS=$(CIRCUITPY_BITOPS)
9494

9595
CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD)
9696
CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO)

shared-bindings/_bit_transpose/__init__.c

Lines changed: 0 additions & 100 deletions
This file was deleted.

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) 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/bitops/__init__.h"
31+
32+
//| """Routines for low-level manipulation of binary data"""
33+
//|
34+
//|
35+
36+
//| def bit_transpose(input: _typing.ReadableBuffer, *, width:int = 8, output: Optional[_typing.WriteableBuffer]=None) -> 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_width, ARG_output };
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 | MP_ARG_KW_ONLY, { .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(translate("width must be from 2 to 8 (inclusive)"));
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/_bit_transpose/__init__.h renamed to shared-bindings/bitops/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
#include <stdint.h>
3030
#include <stdlib.h>
3131

32-
void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands);
32+
void common_hal_bitops_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands);

shared-module/_bit_transpose/__init__.c renamed to shared-module/bitops/__init__.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "shared-bindings/_bit_transpose/__init__.h"
27+
#include "shared-bindings/bitops/__init__.h"
2828

2929
#include <stdint.h>
3030
#include <stdlib.h>
@@ -142,7 +142,7 @@ static void bit_transpose_var(uint32_t *result, const uint8_t *src, size_t src_s
142142
}
143143
}
144144

145-
void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands) {
145+
void common_hal_bitops_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands) {
146146
if(num_strands == 8) {
147147
bit_transpose_8((uint32_t*)(void*)result, src, inlen/8, inlen/8);
148148
} else {

0 commit comments

Comments
 (0)