Skip to content

Commit ef248b6

Browse files
authored
add bitwise operators (#616)
* add bitwise operators * add build to requirements
1 parent 38a4976 commit ef248b6

17 files changed

+659
-3
lines changed

code/micropython.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SRC_USERMOD += $(USERMODULES_DIR)/ndarray.c
1212
SRC_USERMOD += $(USERMODULES_DIR)/numpy/ndarray/ndarray_iter.c
1313
SRC_USERMOD += $(USERMODULES_DIR)/ndarray_properties.c
1414
SRC_USERMOD += $(USERMODULES_DIR)/numpy/approx.c
15+
SRC_USERMOD += $(USERMODULES_DIR)/numpy/bitwise.c
1516
SRC_USERMOD += $(USERMODULES_DIR)/numpy/compare.c
1617
SRC_USERMOD += $(USERMODULES_DIR)/numpy/carray/carray.c
1718
SRC_USERMOD += $(USERMODULES_DIR)/numpy/carray/carray_tools.c

code/numpy/bitwise.c

Lines changed: 431 additions & 0 deletions
Large diffs are not rendered by default.

code/numpy/bitwise.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/*
3+
* This file is part of the micropython-ulab project,
4+
*
5+
* https://github.com/v923z/micropython-ulab
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Copyright (c) 2023 Zoltán Vörös
10+
*/
11+
12+
#ifndef _BITWISE_
13+
#define _BITWISE_
14+
15+
#include "../ulab.h"
16+
#include "../ndarray.h"
17+
18+
enum BITWISE_FUNCTION_TYPE {
19+
BITWISE_AND,
20+
BITWISE_OR,
21+
BITWISE_XOR,
22+
BITWISE_LEFT_SHIFT,
23+
BITWISE_RIGHT_SHIFT,
24+
};
25+
26+
MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_and_obj);
27+
MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_or_obj);
28+
MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_xor_obj);
29+
MP_DECLARE_CONST_FUN_OBJ_2(left_shift_obj);
30+
MP_DECLARE_CONST_FUN_OBJ_2(right_shift_obj);
31+
32+
#endif /* _BITWISE_ */

code/numpy/numpy.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "numpy.h"
2020
#include "approx.h"
21+
#include "bitwise.h"
2122
#include "carray/carray.h"
2223
#include "compare.h"
2324
#include "create.h"
@@ -164,7 +165,6 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
164165
#if ULAB_NUMPY_HAS_ZEROS
165166
{ MP_ROM_QSTR(MP_QSTR_zeros), MP_ROM_PTR(&create_zeros_obj) },
166167
#endif
167-
// functions of the compare sub-module
168168
#if ULAB_NUMPY_HAS_CLIP
169169
{ MP_ROM_QSTR(MP_QSTR_clip), MP_ROM_PTR(&compare_clip_obj) },
170170
#endif
@@ -189,10 +189,25 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
189189
#if ULAB_NUMPY_HAS_NONZERO
190190
{ MP_ROM_QSTR(MP_QSTR_nonzero), MP_ROM_PTR(&compare_nonzero_obj) },
191191
#endif
192-
193192
#if ULAB_NUMPY_HAS_WHERE
194193
{ MP_ROM_QSTR(MP_QSTR_where), MP_ROM_PTR(&compare_where_obj) },
195194
#endif
195+
// bitwise operators
196+
#if ULAB_NUMPY_HAS_BITWISE_AND
197+
{ MP_ROM_QSTR(MP_QSTR_bitwise_and), MP_ROM_PTR(&bitwise_bitwise_and_obj) },
198+
#endif
199+
#if ULAB_NUMPY_HAS_BITWISE_OR
200+
{ MP_ROM_QSTR(MP_QSTR_bitwise_or), MP_ROM_PTR(&bitwise_bitwise_or_obj) },
201+
#endif
202+
#if ULAB_NUMPY_HAS_BITWISE_XOR
203+
{ MP_ROM_QSTR(MP_QSTR_bitwise_xor), MP_ROM_PTR(&bitwise_bitwise_xor_obj) },
204+
#endif
205+
#if ULAB_NUMPY_HAS_LEFT_SHIFT
206+
{ MP_ROM_QSTR(MP_QSTR_left_shift), MP_ROM_PTR(&left_shift_obj) },
207+
#endif
208+
#if ULAB_NUMPY_HAS_RIGHT_SHIFT
209+
{ MP_ROM_QSTR(MP_QSTR_right_shift), MP_ROM_PTR(&right_shift_obj) },
210+
#endif
196211
// functions of the filter sub-module
197212
#if ULAB_NUMPY_HAS_CONVOLVE
198213
{ MP_ROM_QSTR(MP_QSTR_convolve), MP_ROM_PTR(&filter_convolve_obj) },

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "user/user.h"
3434
#include "utils/utils.h"
3535

36-
#define ULAB_VERSION 6.2.0
36+
#define ULAB_VERSION 6.3.0
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

code/ulab.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@
165165
#define NDARRAY_HAS_INPLACE_TRUE_DIVIDE (1)
166166
#endif
167167

168+
// bitwise operators
169+
#ifndef ULAB_NUMPY_HAS_BITWISE_AND
170+
#define ULAB_NUMPY_HAS_BITWISE_AND (1)
171+
#endif
172+
173+
#ifndef ULAB_NUMPY_HAS_BITWISE_OR
174+
#define ULAB_NUMPY_HAS_BITWISE_OR (1)
175+
#endif
176+
177+
#ifndef ULAB_NUMPY_HAS_BITWISE_XOR
178+
#define ULAB_NUMPY_HAS_BITWISE_XOR (1)
179+
#endif
180+
181+
#ifndef ULAB_NUMPY_HAS_LEFT_SHIFT
182+
#define ULAB_NUMPY_HAS_LEFT_SHIFT (1)
183+
#endif
184+
185+
#ifndef ULAB_NUMPY_HAS_RIGHT_SHIFT
186+
#define ULAB_NUMPY_HAS_RIGHT_SHIFT (1)
187+
#endif
188+
168189
// the ndarray unary operators
169190
#ifndef NDARRAY_HAS_UNARY_OPS
170191
#define NDARRAY_HAS_UNARY_OPS (1)

docs/ulab-change-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Fri, 12 May 2023
2+
3+
version 6.3.0
4+
5+
add bitwise operators
6+
17
Wed, 17 May 2023
28

39
version 6.1.1

tests/2d/numpy/bitwise_and.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
try:
2+
from ulab import numpy as np
3+
except:
4+
import numpy as np
5+
6+
7+
dtypes = (np.uint8, np.int8, np.uint16, np.int16)
8+
9+
for dtype1 in dtypes:
10+
x1 = np.array(range(5), dtype=dtype1)
11+
for dtype2 in dtypes:
12+
x2 = np.array(range(5, 0, -1), dtype=dtype2)
13+
14+
print(np.bitwise_and(x1, x2))

tests/2d/numpy/bitwise_and.py.exp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
array([0, 0, 2, 2, 0], dtype=uint8)
2+
array([0, 0, 2, 2, 0], dtype=int16)
3+
array([0, 0, 2, 2, 0], dtype=uint16)
4+
array([0, 0, 2, 2, 0], dtype=int16)
5+
array([0, 0, 2, 2, 0], dtype=int16)
6+
array([0, 0, 2, 2, 0], dtype=int8)
7+
array([0, 0, 2, 2, 0], dtype=uint16)
8+
array([0, 0, 2, 2, 0], dtype=int16)
9+
array([0, 0, 2, 2, 0], dtype=uint16)
10+
array([0, 0, 2, 2, 0], dtype=uint16)
11+
array([0, 0, 2, 2, 0], dtype=uint16)
12+
array([0, 0, 2, 2, 0], dtype=int16)
13+
array([0, 0, 2, 2, 0], dtype=int16)
14+
array([0, 0, 2, 2, 0], dtype=int16)
15+
array([0, 0, 2, 2, 0], dtype=int16)
16+
array([0, 0, 2, 2, 0], dtype=int16)

tests/2d/numpy/bitwise_or.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
try:
2+
from ulab import numpy as np
3+
except:
4+
import numpy as np
5+
6+
7+
dtypes = (np.uint8, np.int8, np.uint16, np.int16)
8+
9+
for dtype1 in dtypes:
10+
x1 = np.array(range(5), dtype=dtype1)
11+
for dtype2 in dtypes:
12+
x2 = np.array(range(5, 0, -1), dtype=dtype2)
13+
14+
print(np.bitwise_or(x1, x2))

0 commit comments

Comments
 (0)