6
6
*
7
7
* The MIT License (MIT)
8
8
*
9
- * Copyright (c) 2019-2022 Zoltán Vörös
9
+ * Copyright (c) 2019-2024 Zoltán Vörös
10
10
* 2020 Jeff Epler for Adafruit Industries
11
11
* 2020 Taku Fukada
12
12
*/
@@ -509,8 +509,9 @@ static size_t multiply_size(size_t a, size_t b) {
509
509
return result ;
510
510
}
511
511
512
- ndarray_obj_t * ndarray_new_ndarray (uint8_t ndim , size_t * shape , int32_t * strides , uint8_t dtype ) {
512
+ ndarray_obj_t * ndarray_new_ndarray (uint8_t ndim , size_t * shape , int32_t * strides , uint8_t dtype , uint8_t * buffer ) {
513
513
// Creates the base ndarray with shape, and initialises the values to straight 0s
514
+ // optionally, values can be supplied via the last argument
514
515
ndarray_obj_t * ndarray = m_new_obj (ndarray_obj_t );
515
516
ndarray -> base .type = & ulab_ndarray_type ;
516
517
ndarray -> dtype = dtype == NDARRAY_BOOL ? NDARRAY_UINT8 : dtype ;
@@ -536,9 +537,13 @@ ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides
536
537
537
538
// if the length is 0, still allocate a single item, so that contractions can be handled
538
539
size_t len = multiply_size (ndarray -> itemsize , MAX (1 , ndarray -> len ));
539
- uint8_t * array = m_new0 (byte , len );
540
- // this should set all elements to 0, irrespective of the of the dtype (all bits are zero)
541
- // we could, perhaps, leave this step out, and initialise the array only, when needed
540
+ uint8_t * array ;
541
+ array = buffer ;
542
+ if (array == NULL ) {
543
+ // this should set all elements to 0, irrespective of the of the dtype (all bits are zero)
544
+ // we could, perhaps, leave this step out, and initialise the array only, when needed
545
+ array = m_new0 (byte , len );
546
+ }
542
547
ndarray -> array = array ;
543
548
ndarray -> origin = array ;
544
549
return ndarray ;
@@ -547,12 +552,12 @@ ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides
547
552
ndarray_obj_t * ndarray_new_dense_ndarray (uint8_t ndim , size_t * shape , uint8_t dtype ) {
548
553
// creates a dense array, i.e., one, where the strides are derived directly from the shapes
549
554
// the function should work in the general n-dimensional case
550
- int32_t * strides = m_new (int32_t , ULAB_MAX_DIMS );
551
- strides [ULAB_MAX_DIMS - 1 ] = (int32_t )ulab_binary_get_size (dtype );
552
- for (size_t i = ULAB_MAX_DIMS ; i > 1 ; i -- ) {
553
- strides [i - 2 ] = strides [i - 1 ] * MAX (1 , shape [i - 1 ]);
554
- }
555
- return ndarray_new_ndarray (ndim , shape , strides , dtype );
555
+ // int32_t *strides = m_new(int32_t, ULAB_MAX_DIMS);
556
+ // strides[ULAB_MAX_DIMS - 1] = (int32_t)ulab_binary_get_size(dtype);
557
+ // for(size_t i = ULAB_MAX_DIMS; i > 1; i--) {
558
+ // strides[i-2] = strides[i-1] * MAX(1, shape[i-1]);
559
+ // }
560
+ return ndarray_new_ndarray (ndim , shape , NULL , dtype , NULL );
556
561
}
557
562
558
563
ndarray_obj_t * ndarray_new_ndarray_from_tuple (mp_obj_tuple_t * _shape , uint8_t dtype ) {
@@ -650,7 +655,7 @@ ndarray_obj_t *ndarray_copy_view(ndarray_obj_t *source) {
650
655
if (source -> boolean ) {
651
656
dtype = NDARRAY_BOOL ;
652
657
}
653
- ndarray_obj_t * ndarray = ndarray_new_ndarray (source -> ndim , source -> shape , strides , dtype );
658
+ ndarray_obj_t * ndarray = ndarray_new_ndarray (source -> ndim , source -> shape , strides , dtype , NULL );
654
659
ndarray_copy_array (source , ndarray , 0 );
655
660
return ndarray ;
656
661
}
@@ -1884,7 +1889,7 @@ mp_obj_t ndarray_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
1884
1889
#if ULAB_SUPPORTS_COMPLEX
1885
1890
if (self -> dtype == NDARRAY_COMPLEX ) {
1886
1891
int32_t * strides = strides_from_shape (self -> shape , NDARRAY_FLOAT );
1887
- ndarray_obj_t * target = ndarray_new_ndarray (self -> ndim , self -> shape , strides , NDARRAY_FLOAT );
1892
+ ndarray_obj_t * target = ndarray_new_ndarray (self -> ndim , self -> shape , strides , NDARRAY_FLOAT , NULL );
1888
1893
ndarray = MP_OBJ_TO_PTR (carray_abs (self , target ));
1889
1894
} else {
1890
1895
#endif
0 commit comments