Skip to content

Commit

Permalink
add bitwise operators (#616)
Browse files Browse the repository at this point in the history
* add bitwise operators

* add build to requirements
  • Loading branch information
v923z committed Jun 20, 2023
1 parent 38a4976 commit ef248b6
Show file tree
Hide file tree
Showing 17 changed files with 659 additions and 3 deletions.
1 change: 1 addition & 0 deletions code/micropython.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SRC_USERMOD += $(USERMODULES_DIR)/ndarray.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/ndarray/ndarray_iter.c
SRC_USERMOD += $(USERMODULES_DIR)/ndarray_properties.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/approx.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/bitwise.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/compare.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/carray/carray.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/carray/carray_tools.c
Expand Down
431 changes: 431 additions & 0 deletions code/numpy/bitwise.c

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions code/numpy/bitwise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

/*
* This file is part of the micropython-ulab project,
*
* https://github.com/v923z/micropython-ulab
*
* The MIT License (MIT)
*
* Copyright (c) 2023 Zoltán Vörös
*/

#ifndef _BITWISE_
#define _BITWISE_

#include "../ulab.h"
#include "../ndarray.h"

enum BITWISE_FUNCTION_TYPE {
BITWISE_AND,
BITWISE_OR,
BITWISE_XOR,
BITWISE_LEFT_SHIFT,
BITWISE_RIGHT_SHIFT,
};

MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_and_obj);
MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_or_obj);
MP_DECLARE_CONST_FUN_OBJ_2(bitwise_bitwise_xor_obj);
MP_DECLARE_CONST_FUN_OBJ_2(left_shift_obj);
MP_DECLARE_CONST_FUN_OBJ_2(right_shift_obj);

#endif /* _BITWISE_ */
19 changes: 17 additions & 2 deletions code/numpy/numpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "numpy.h"
#include "approx.h"
#include "bitwise.h"
#include "carray/carray.h"
#include "compare.h"
#include "create.h"
Expand Down Expand Up @@ -164,7 +165,6 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
#if ULAB_NUMPY_HAS_ZEROS
{ MP_ROM_QSTR(MP_QSTR_zeros), MP_ROM_PTR(&create_zeros_obj) },
#endif
// functions of the compare sub-module
#if ULAB_NUMPY_HAS_CLIP
{ MP_ROM_QSTR(MP_QSTR_clip), MP_ROM_PTR(&compare_clip_obj) },
#endif
Expand All @@ -189,10 +189,25 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
#if ULAB_NUMPY_HAS_NONZERO
{ MP_ROM_QSTR(MP_QSTR_nonzero), MP_ROM_PTR(&compare_nonzero_obj) },
#endif

#if ULAB_NUMPY_HAS_WHERE
{ MP_ROM_QSTR(MP_QSTR_where), MP_ROM_PTR(&compare_where_obj) },
#endif
// bitwise operators
#if ULAB_NUMPY_HAS_BITWISE_AND
{ MP_ROM_QSTR(MP_QSTR_bitwise_and), MP_ROM_PTR(&bitwise_bitwise_and_obj) },
#endif
#if ULAB_NUMPY_HAS_BITWISE_OR
{ MP_ROM_QSTR(MP_QSTR_bitwise_or), MP_ROM_PTR(&bitwise_bitwise_or_obj) },
#endif
#if ULAB_NUMPY_HAS_BITWISE_XOR
{ MP_ROM_QSTR(MP_QSTR_bitwise_xor), MP_ROM_PTR(&bitwise_bitwise_xor_obj) },
#endif
#if ULAB_NUMPY_HAS_LEFT_SHIFT
{ MP_ROM_QSTR(MP_QSTR_left_shift), MP_ROM_PTR(&left_shift_obj) },
#endif
#if ULAB_NUMPY_HAS_RIGHT_SHIFT
{ MP_ROM_QSTR(MP_QSTR_right_shift), MP_ROM_PTR(&right_shift_obj) },
#endif
// functions of the filter sub-module
#if ULAB_NUMPY_HAS_CONVOLVE
{ MP_ROM_QSTR(MP_QSTR_convolve), MP_ROM_PTR(&filter_convolve_obj) },
Expand Down
2 changes: 1 addition & 1 deletion code/ulab.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "user/user.h"
#include "utils/utils.h"

#define ULAB_VERSION 6.2.0
#define ULAB_VERSION 6.3.0
#define xstr(s) str(s)
#define str(s) #s

Expand Down
21 changes: 21 additions & 0 deletions code/ulab.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@
#define NDARRAY_HAS_INPLACE_TRUE_DIVIDE (1)
#endif

// bitwise operators
#ifndef ULAB_NUMPY_HAS_BITWISE_AND
#define ULAB_NUMPY_HAS_BITWISE_AND (1)
#endif

#ifndef ULAB_NUMPY_HAS_BITWISE_OR
#define ULAB_NUMPY_HAS_BITWISE_OR (1)
#endif

#ifndef ULAB_NUMPY_HAS_BITWISE_XOR
#define ULAB_NUMPY_HAS_BITWISE_XOR (1)
#endif

#ifndef ULAB_NUMPY_HAS_LEFT_SHIFT
#define ULAB_NUMPY_HAS_LEFT_SHIFT (1)
#endif

#ifndef ULAB_NUMPY_HAS_RIGHT_SHIFT
#define ULAB_NUMPY_HAS_RIGHT_SHIFT (1)
#endif

// the ndarray unary operators
#ifndef NDARRAY_HAS_UNARY_OPS
#define NDARRAY_HAS_UNARY_OPS (1)
Expand Down
6 changes: 6 additions & 0 deletions docs/ulab-change-log.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Fri, 12 May 2023

version 6.3.0

add bitwise operators

Wed, 17 May 2023

version 6.1.1
Expand Down
14 changes: 14 additions & 0 deletions tests/2d/numpy/bitwise_and.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try:
from ulab import numpy as np
except:
import numpy as np


dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.bitwise_and(x1, x2))
16 changes: 16 additions & 0 deletions tests/2d/numpy/bitwise_and.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
array([0, 0, 2, 2, 0], dtype=uint8)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int8)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=uint16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
array([0, 0, 2, 2, 0], dtype=int16)
14 changes: 14 additions & 0 deletions tests/2d/numpy/bitwise_or.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try:
from ulab import numpy as np
except:
import numpy as np


dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.bitwise_or(x1, x2))
16 changes: 16 additions & 0 deletions tests/2d/numpy/bitwise_or.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
array([5, 5, 3, 3, 5], dtype=uint8)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int8)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=uint16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
array([5, 5, 3, 3, 5], dtype=int16)
14 changes: 14 additions & 0 deletions tests/2d/numpy/bitwise_xor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try:
from ulab import numpy as np
except:
import numpy as np


dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.bitwise_xor(x1, x2))
16 changes: 16 additions & 0 deletions tests/2d/numpy/bitwise_xor.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
array([5, 5, 1, 1, 5], dtype=uint8)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=uint16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=int8)
array([5, 5, 1, 1, 5], dtype=uint16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=uint16)
array([5, 5, 1, 1, 5], dtype=uint16)
array([5, 5, 1, 1, 5], dtype=uint16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=int16)
array([5, 5, 1, 1, 5], dtype=int16)
14 changes: 14 additions & 0 deletions tests/2d/numpy/left_shift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try:
from ulab import numpy as np
except:
import numpy as np


dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.left_shift(x1, x2))
16 changes: 16 additions & 0 deletions tests/2d/numpy/left_shift.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
array([0, 16, 16, 12, 8], dtype=uint8)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int8)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int8)
array([0, 16, 16, 12, 8], dtype=uint16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
array([0, 16, 16, 12, 8], dtype=int16)
14 changes: 14 additions & 0 deletions tests/2d/numpy/right_shift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
try:
from ulab import numpy as np
except:
import numpy as np


dtypes = (np.uint8, np.int8, np.uint16, np.int16)

for dtype1 in dtypes:
x1 = np.array(range(5), dtype=dtype1)
for dtype2 in dtypes:
x2 = np.array(range(5, 0, -1), dtype=dtype2)

print(np.right_shift(x1, x2))
16 changes: 16 additions & 0 deletions tests/2d/numpy/right_shift.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
array([0, 0, 0, 0, 2], dtype=uint8)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int8)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int8)
array([0, 0, 0, 0, 2], dtype=uint16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)
array([0, 0, 0, 0, 2], dtype=int16)

0 comments on commit ef248b6

Please sign in to comment.