Skip to content

Commit d072086

Browse files
authored
allow function iterator in math functions (#633)
* allow function iterator in math functions * increment version number
1 parent 38caf84 commit d072086

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

code/numpy/vector.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,48 @@ static mp_obj_t vector_generic_vector(size_t n_args, const mp_obj_t *pos_args, m
8787

8888
uint8_t *sarray = (uint8_t *)source->array;
8989

90+
#if ULAB_VECTORISE_USES_FUN_POINTER
91+
92+
mp_float_t (*func)(void *) = ndarray_get_float_function(source->dtype);
93+
94+
#if ULAB_MAX_DIMS > 3
95+
size_t i = 0;
96+
do {
97+
#endif
98+
#if ULAB_MAX_DIMS > 2
99+
size_t j = 0;
100+
do {
101+
#endif
102+
#if ULAB_MAX_DIMS > 1
103+
size_t k = 0;
104+
do {
105+
#endif
106+
size_t l = 0;
107+
do {
108+
mp_float_t value = func(sarray);
109+
*tarray++ = f(value);
110+
sarray += source->strides[ULAB_MAX_DIMS - 1];
111+
l++;
112+
} while(l < source->shape[ULAB_MAX_DIMS - 1]);
113+
#if ULAB_MAX_DIMS > 1
114+
sarray -= source->strides[ULAB_MAX_DIMS - 1] * source->shape[ULAB_MAX_DIMS-1];
115+
sarray += source->strides[ULAB_MAX_DIMS - 2];
116+
k++;
117+
} while(k < source->shape[ULAB_MAX_DIMS - 2]);
118+
#endif /* ULAB_MAX_DIMS > 1 */
119+
#if ULAB_MAX_DIMS > 2
120+
sarray -= source->strides[ULAB_MAX_DIMS - 2] * source->shape[ULAB_MAX_DIMS-2];
121+
sarray += source->strides[ULAB_MAX_DIMS - 3];
122+
j++;
123+
} while(j < source->shape[ULAB_MAX_DIMS - 3]);
124+
#endif /* ULAB_MAX_DIMS > 2 */
125+
#if ULAB_MAX_DIMS > 3
126+
sarray -= source->strides[ULAB_MAX_DIMS - 3] * source->shape[ULAB_MAX_DIMS-3];
127+
sarray += source->strides[ULAB_MAX_DIMS - 4];
128+
i++;
129+
} while(i < source->shape[ULAB_MAX_DIMS - 4]);
130+
#endif /* ULAB_MAX_DIMS > 3 */
131+
#else
90132
if(source->dtype == NDARRAY_UINT8) {
91133
ITERATE_VECTOR(uint8_t, target, tarray, tstrides, source, sarray);
92134
} else if(source->dtype == NDARRAY_INT8) {
@@ -98,6 +140,7 @@ static mp_obj_t vector_generic_vector(size_t n_args, const mp_obj_t *pos_args, m
98140
} else {
99141
ITERATE_VECTOR(mp_float_t, target, tarray, tstrides, source, sarray);
100142
}
143+
#endif /* ULAB_VECTORISE_USES_FUN_POINTER */
101144
} else {
102145
target = ndarray_from_mp_obj(o_in, 0);
103146
mp_float_t *tarray = (mp_float_t *)target->array;

code/numpy/vector.h

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,24 @@ typedef struct _vectorized_function_obj_t {
9090
const mp_obj_type_t *type;
9191
} vectorized_function_obj_t;
9292

93+
94+
#if ULAB_MATH_FUNCTIONS_OUT_KEYWORD
95+
9396
#if ULAB_HAS_FUNCTION_ITERATOR
94-
#define ITERATE_VECTOR(type, array, source, sarray, shift)\
97+
#define ITERATE_VECTOR(type, target, tarray, tstrides, source, sarray)\
9598
({\
9699
size_t *scoords = ndarray_new_coords((source)->ndim);\
97-
for(size_t i=0; i < (source)->len/(source)->shape[ULAB_MAX_DIMS -1]; i++) {\
98-
for(size_t l=0; l < (source)->shape[ULAB_MAX_DIMS - 1]; l++) {\
99-
*(array) = f(*((type *)(sarray)));\
100-
(array) += (shift);\
100+
for(size_t i = 0; i < (source)->len / (source)->shape[ULAB_MAX_DIMS - 1]; i++) {\
101+
for(size_t l = 0; l < (source)->shape[ULAB_MAX_DIMS - 1]; l++) {\
102+
*(tarray) = f(*((type *)(sarray)));\
103+
(tarray) += (tstrides)[ULAB_MAX_DIMS - 1];\
101104
(sarray) += (source)->strides[ULAB_MAX_DIMS - 1];\
102105
}\
103106
ndarray_rewind_array((source)->ndim, sarray, (source)->shape, (source)->strides, scoords);\
104107
}\
105108
})
106-
#endif /* ULAB_HAS_FUNCTION_ITERATOR */
107109

108-
#if ULAB_MATH_FUNCTIONS_OUT_KEYWORD
110+
#else
109111

110112
#if ULAB_MAX_DIMS == 1
111113
#define ITERATE_VECTOR(type, target, tarray, tstrides, source, sarray) do {\
@@ -202,6 +204,7 @@ typedef struct _vectorized_function_obj_t {
202204
} while(i < (source)->shape[ULAB_MAX_DIMS - 4]);\
203205
} while(0)
204206
#endif /* ULAB_MAX_DIMS == 4 */
207+
#endif /* ULAB_HAS_FUNCTION_ITERATOR */
205208

206209
#define MATH_FUN_1(py_name, c_name) \
207210
static mp_obj_t vector_ ## py_name(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { \
@@ -210,6 +213,22 @@ typedef struct _vectorized_function_obj_t {
210213

211214
#else /* ULAB_MATH_FUNCTIONS_OUT_KEYWORD */
212215

216+
#if ULAB_HAS_FUNCTION_ITERATOR
217+
#define ITERATE_VECTOR(type, array, source, sarray, shift)\
218+
({\
219+
size_t *scoords = ndarray_new_coords((source)->ndim);\
220+
for(size_t i=0; i < (source)->len / (source)->shape[ULAB_MAX_DIMS - 1]; i++) {\
221+
for(size_t l = 0; l < (source)->shape[ULAB_MAX_DIMS - 1]; l++) {\
222+
*(array) = f(*((type *)(sarray)));\
223+
(array)++;\
224+
(sarray) += (source)->strides[ULAB_MAX_DIMS - 1];\
225+
}\
226+
ndarray_rewind_array((source)->ndim, sarray, (source)->shape, (source)->strides, scoords);\
227+
}\
228+
})
229+
230+
#else
231+
213232
#if ULAB_MAX_DIMS == 1
214233
#define ITERATE_VECTOR(type, array, source, sarray) do {\
215234
size_t l = 0;\
@@ -290,6 +309,8 @@ typedef struct _vectorized_function_obj_t {
290309
} while(0)
291310
#endif /* ULAB_MAX_DIMS == 4 */
292311

312+
#endif /* ULAB_HAS_FUNCTION_ITERATOR */
313+
293314
#define MATH_FUN_1(py_name, c_name) \
294315
static mp_obj_t vector_ ## py_name(mp_obj_t x_obj) { \
295316
return vector_generic_vector(x_obj, MICROPY_FLOAT_C_FUN(c_name)); \

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.3.4
36+
#define ULAB_VERSION 6.3.5
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

docs/ulab-change-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Sat, 1 Jul 2023
2+
3+
version 6.3.5
4+
5+
allow function itertor in math functions with the out keyword
6+
17
Fri, 12 May 2023
28

39
version 6.3.4

0 commit comments

Comments
 (0)