@@ -586,43 +586,10 @@ void ndarray_copy_array(ndarray_obj_t *source, ndarray_obj_t *target, uint8_t sh
586
586
}
587
587
#endif
588
588
589
- #if ULAB_MAX_DIMS > 3
590
- size_t i = 0 ;
591
- do {
592
- #endif
593
- #if ULAB_MAX_DIMS > 2
594
- size_t j = 0 ;
595
- do {
596
- #endif
597
- #if ULAB_MAX_DIMS > 1
598
- size_t k = 0 ;
599
- do {
600
- #endif
601
- size_t l = 0 ;
602
- do {
603
- memcpy (tarray , sarray , target -> itemsize );
604
- tarray += target -> itemsize ;
605
- sarray += source -> strides [ULAB_MAX_DIMS - 1 ];
606
- l ++ ;
607
- } while (l < source -> shape [ULAB_MAX_DIMS - 1 ]);
608
- #if ULAB_MAX_DIMS > 1
609
- sarray -= source -> strides [ULAB_MAX_DIMS - 1 ] * source -> shape [ULAB_MAX_DIMS - 1 ];
610
- sarray += source -> strides [ULAB_MAX_DIMS - 2 ];
611
- k ++ ;
612
- } while (k < source -> shape [ULAB_MAX_DIMS - 2 ]);
613
- #endif
614
- #if ULAB_MAX_DIMS > 2
615
- sarray -= source -> strides [ULAB_MAX_DIMS - 2 ] * source -> shape [ULAB_MAX_DIMS - 2 ];
616
- sarray += source -> strides [ULAB_MAX_DIMS - 3 ];
617
- j ++ ;
618
- } while (j < source -> shape [ULAB_MAX_DIMS - 3 ]);
619
- #endif
620
- #if ULAB_MAX_DIMS > 3
621
- sarray -= source -> strides [ULAB_MAX_DIMS - 3 ] * source -> shape [ULAB_MAX_DIMS - 3 ];
622
- sarray += source -> strides [ULAB_MAX_DIMS - 4 ];
623
- i ++ ;
624
- } while (i < source -> shape [ULAB_MAX_DIMS - 4 ]);
625
- #endif
589
+ ITERATOR_HEAD ();
590
+ memcpy (tarray , sarray , target -> itemsize );
591
+ tarray += target -> itemsize ;
592
+ ITERATOR_TAIL (source , sarray );
626
593
}
627
594
628
595
ndarray_obj_t * ndarray_new_view (ndarray_obj_t * source , uint8_t ndim , size_t * shape , int32_t * strides , int32_t offset ) {
@@ -676,69 +643,36 @@ ndarray_obj_t *ndarray_copy_view_convert_type(ndarray_obj_t *source, uint8_t dty
676
643
uint8_t complex_size = 2 * sizeof (mp_float_t );
677
644
#endif
678
645
679
- #if ULAB_MAX_DIMS > 3
680
- size_t i = 0 ;
681
- do {
682
- #endif
683
- #if ULAB_MAX_DIMS > 2
684
- size_t j = 0 ;
685
- do {
646
+ ITERATOR_HEAD ()
647
+ mp_obj_t item ;
648
+ #if ULAB_SUPPORTS_COMPLEX
649
+ if (source -> dtype == NDARRAY_COMPLEX ) {
650
+ if (dtype != NDARRAY_COMPLEX ) {
651
+ mp_raise_TypeError (MP_ERROR_TEXT ("cannot convert complex type" ));
652
+ } else {
653
+ memcpy (array , sarray , complex_size );
654
+ }
655
+ } else {
686
656
#endif
687
- #if ULAB_MAX_DIMS > 1
688
- size_t k = 0 ;
689
- do {
690
- #endif
691
- size_t l = 0 ;
692
- do {
693
- mp_obj_t item ;
694
- #if ULAB_SUPPORTS_COMPLEX
695
- if (source -> dtype == NDARRAY_COMPLEX ) {
696
- if (dtype != NDARRAY_COMPLEX ) {
697
- mp_raise_TypeError (MP_ERROR_TEXT ("cannot convert complex type" ));
698
- } else {
699
- memcpy (array , sarray , complex_size );
700
- }
701
- } else {
702
- #endif
703
- if ((source -> dtype == NDARRAY_FLOAT ) && (dtype != NDARRAY_FLOAT )) {
704
- // floats must be treated separately, because they can't directly be converted to integer types
705
- mp_float_t f = ndarray_get_float_value (sarray , source -> dtype );
706
- item = mp_obj_new_int ((int32_t )MICROPY_FLOAT_C_FUN (round )(f ));
707
- } else {
708
- item = mp_binary_get_val_array (source -> dtype , sarray , 0 );
709
- }
710
- #if ULAB_SUPPORTS_COMPLEX
711
- if (dtype == NDARRAY_COMPLEX ) {
712
- ndarray_set_value (NDARRAY_FLOAT , array , 0 , item );
713
- } else {
714
- ndarray_set_value (dtype , array , 0 , item );
715
- }
716
- }
717
- #else
718
- ndarray_set_value (dtype , array , 0 , item );
719
- #endif
720
- array += ndarray -> itemsize ;
721
- sarray += source -> strides [ULAB_MAX_DIMS - 1 ];
722
- l ++ ;
723
- } while (l < source -> shape [ULAB_MAX_DIMS - 1 ]);
724
- #if ULAB_MAX_DIMS > 1
725
- sarray -= source -> strides [ULAB_MAX_DIMS - 1 ] * source -> shape [ULAB_MAX_DIMS - 1 ];
726
- sarray += source -> strides [ULAB_MAX_DIMS - 2 ];
727
- k ++ ;
728
- } while (k < source -> shape [ULAB_MAX_DIMS - 2 ]);
729
- #endif
730
- #if ULAB_MAX_DIMS > 2
731
- sarray -= source -> strides [ULAB_MAX_DIMS - 2 ] * source -> shape [ULAB_MAX_DIMS - 2 ];
732
- sarray += source -> strides [ULAB_MAX_DIMS - 3 ];
733
- j ++ ;
734
- } while (j < source -> shape [ULAB_MAX_DIMS - 3 ]);
657
+ if ((source -> dtype == NDARRAY_FLOAT ) && (dtype != NDARRAY_FLOAT )) {
658
+ // floats must be treated separately, because they can't directly be converted to integer types
659
+ mp_float_t f = ndarray_get_float_value (sarray , source -> dtype );
660
+ item = mp_obj_new_int ((int32_t )MICROPY_FLOAT_C_FUN (round )(f ));
661
+ } else {
662
+ item = mp_binary_get_val_array (source -> dtype , sarray , 0 );
663
+ }
664
+ #if ULAB_SUPPORTS_COMPLEX
665
+ if (dtype == NDARRAY_COMPLEX ) {
666
+ ndarray_set_value (NDARRAY_FLOAT , array , 0 , item );
667
+ } else {
668
+ ndarray_set_value (dtype , array , 0 , item );
669
+ }
670
+ }
671
+ #else
672
+ ndarray_set_value (dtype , array , 0 , item );
735
673
#endif
736
- #if ULAB_MAX_DIMS > 3
737
- sarray -= source -> strides [ULAB_MAX_DIMS - 3 ] * source -> shape [ULAB_MAX_DIMS - 3 ];
738
- sarray += source -> strides [ULAB_MAX_DIMS - 4 ];
739
- i ++ ;
740
- } while (i < source -> shape [ULAB_MAX_DIMS - 4 ]);
741
- #endif
674
+ array += ndarray -> itemsize ;
675
+ ITERATOR_TAIL (source , sarray );
742
676
return ndarray ;
743
677
}
744
678
@@ -765,54 +699,21 @@ mp_obj_t ndarray_byteswap(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
765
699
return MP_OBJ_FROM_PTR (ndarray );
766
700
} else {
767
701
uint8_t * array = (uint8_t * )ndarray -> array ;
768
- #if ULAB_MAX_DIMS > 3
769
- size_t i = 0 ;
770
- do {
771
- #endif
772
- #if ULAB_MAX_DIMS > 2
773
- size_t j = 0 ;
774
- do {
775
- #endif
776
- #if ULAB_MAX_DIMS > 1
777
- size_t k = 0 ;
778
- do {
779
- #endif
780
- size_t l = 0 ;
781
- do {
782
- if (self -> dtype == NDARRAY_FLOAT ) {
783
- #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
784
- SWAP (uint8_t , array [0 ], array [3 ]);
785
- SWAP (uint8_t , array [1 ], array [2 ]);
786
- #else
787
- SWAP (uint8_t , array [0 ], array [7 ]);
788
- SWAP (uint8_t , array [1 ], array [6 ]);
789
- SWAP (uint8_t , array [2 ], array [5 ]);
790
- SWAP (uint8_t , array [3 ], array [4 ]);
791
- #endif
792
- } else {
793
- SWAP (uint8_t , array [0 ], array [1 ]);
794
- }
795
- array += ndarray -> strides [ULAB_MAX_DIMS - 1 ];
796
- l ++ ;
797
- } while (l < ndarray -> shape [ULAB_MAX_DIMS - 1 ]);
798
- #if ULAB_MAX_DIMS > 1
799
- array -= ndarray -> strides [ULAB_MAX_DIMS - 1 ] * ndarray -> shape [ULAB_MAX_DIMS - 1 ];
800
- array += ndarray -> strides [ULAB_MAX_DIMS - 2 ];
801
- k ++ ;
802
- } while (k < ndarray -> shape [ULAB_MAX_DIMS - 2 ]);
702
+ ITERATOR_HEAD ();
703
+ if (self -> dtype == NDARRAY_FLOAT ) {
704
+ #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
705
+ SWAP (uint8_t , array [0 ], array [3 ]);
706
+ SWAP (uint8_t , array [1 ], array [2 ]);
707
+ #else
708
+ SWAP (uint8_t , array [0 ], array [7 ]);
709
+ SWAP (uint8_t , array [1 ], array [6 ]);
710
+ SWAP (uint8_t , array [2 ], array [5 ]);
711
+ SWAP (uint8_t , array [3 ], array [4 ]);
803
712
#endif
804
- #if ULAB_MAX_DIMS > 2
805
- array -= ndarray -> strides [ULAB_MAX_DIMS - 2 ] * ndarray -> shape [ULAB_MAX_DIMS - 2 ];
806
- array += ndarray -> strides [ULAB_MAX_DIMS - 3 ];
807
- j ++ ;
808
- } while (j < ndarray -> shape [ULAB_MAX_DIMS - 3 ]);
809
- #endif
810
- #if ULAB_MAX_DIMS > 3
811
- array -= ndarray -> strides [ULAB_MAX_DIMS - 3 ] * ndarray -> shape [ULAB_MAX_DIMS - 3 ];
812
- array += ndarray -> strides [ULAB_MAX_DIMS - 4 ];
813
- i ++ ;
814
- } while (i < ndarray -> shape [ULAB_MAX_DIMS - 4 ]);
815
- #endif
713
+ } else {
714
+ SWAP (uint8_t , array [0 ], array [1 ]);
715
+ }
716
+ ITERATOR_TAIL (ndarray , array );
816
717
}
817
718
return MP_OBJ_FROM_PTR (ndarray );
818
719
}
@@ -1441,43 +1342,10 @@ mp_obj_t ndarray_flatten(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
1441
1342
uint8_t * array = (uint8_t * )ndarray -> array ;
1442
1343
1443
1344
if (memcmp (order , "C" , 1 ) == 0 ) { // C-type ordering
1444
- #if ULAB_MAX_DIMS > 3
1445
- size_t i = 0 ;
1446
- do {
1447
- #endif
1448
- #if ULAB_MAX_DIMS > 2
1449
- size_t j = 0 ;
1450
- do {
1451
- #endif
1452
- #if ULAB_MAX_DIMS > 1
1453
- size_t k = 0 ;
1454
- do {
1455
- #endif
1456
- size_t l = 0 ;
1457
- do {
1458
- memcpy (array , sarray , self -> itemsize );
1459
- array += ndarray -> strides [ULAB_MAX_DIMS - 1 ];
1460
- sarray += self -> strides [ULAB_MAX_DIMS - 1 ];
1461
- l ++ ;
1462
- } while (l < self -> shape [ULAB_MAX_DIMS - 1 ]);
1463
- #if ULAB_MAX_DIMS > 1
1464
- sarray -= self -> strides [ULAB_MAX_DIMS - 1 ] * self -> shape [ULAB_MAX_DIMS - 1 ];
1465
- sarray += self -> strides [ULAB_MAX_DIMS - 2 ];
1466
- k ++ ;
1467
- } while (k < self -> shape [ULAB_MAX_DIMS - 2 ]);
1468
- #endif
1469
- #if ULAB_MAX_DIMS > 2
1470
- sarray -= self -> strides [ULAB_MAX_DIMS - 2 ] * self -> shape [ULAB_MAX_DIMS - 2 ];
1471
- sarray += self -> strides [ULAB_MAX_DIMS - 3 ];
1472
- j ++ ;
1473
- } while (j < self -> shape [ULAB_MAX_DIMS - 3 ]);
1474
- #endif
1475
- #if ULAB_MAX_DIMS > 3
1476
- sarray -= self -> strides [ULAB_MAX_DIMS - 3 ] * self -> shape [ULAB_MAX_DIMS - 3 ];
1477
- sarray += self -> strides [ULAB_MAX_DIMS - 4 ];
1478
- i ++ ;
1479
- } while (i < self -> shape [ULAB_MAX_DIMS - 4 ]);
1480
- #endif
1345
+ ITERATOR_HEAD ();
1346
+ memcpy (array , sarray , self -> itemsize );
1347
+ array += ndarray -> strides [ULAB_MAX_DIMS - 1 ];
1348
+ ITERATOR_TAIL (self , sarray );
1481
1349
} else { // 'F', Fortran-type ordering
1482
1350
#if ULAB_MAX_DIMS > 3
1483
1351
size_t i = 0 ;
0 commit comments