summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--docs/cstr_api.md6
-rw-r--r--include/stc/cspan.h28
-rw-r--r--misc/benchmarks/various/cspan_bench.c5
-rw-r--r--misc/examples/spans/multidim.c75
4 files changed, 60 insertions, 54 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index 5f6ce9e4..397634ec 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -120,14 +120,14 @@ cstr_iter cstr_advance(cstr_iter it, intptr_t n);
// utf8 functions requires linking with src/utf8code.c symbols:
bool cstr_valid_utf8(const cstr* self); // check if str is valid utf8
-cstr cstr_casefold_sv(csview sv); // returns new casefolded utf8 cstr
+cstr cstr_casefold_sv(csview sv); // returns new casefolded utf8 cstr
cstr cstr_tolower(const char* str); // returns new lowercase utf8 cstr
-cstr cstr_tolower_sv(csview sv); // returns new lowercase utf8 cstr
+cstr cstr_tolower_sv(csview sv); // returns new lowercase utf8 cstr
void cstr_lowercase(cstr* self); // transform cstr to lowercase utf8
cstr cstr_toupper(const char* str); // returns new uppercase utf8 cstr
-cstr cstr_toupper_sv(csview sv); // returns new uppercase utf8 cstr
+cstr cstr_toupper_sv(csview sv); // returns new uppercase utf8 cstr
void cstr_uppercase(cstr* self); // transform cstr to uppercase utf8
int cstr_icmp(const cstr* s1, const cstr* s2); // utf8 case-insensitive comparison
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index b8b191f1..6f8de8ec 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -97,8 +97,9 @@ int demo2() {
return it; \
} \
STC_INLINE void Self##_next(Self##_iter* it) { \
- int done; \
- it->ref += _cspan_next##RANK(it->pos, it->_s->shape, it->_s->stride.d, RANK, &done); \
+ int i, inc, done; \
+ if (it->_s->stride.d[0] < it->_s->stride.d[RANK - 1]) i=0, inc=1; else i=RANK-1, inc=-1; \
+ it->ref += _cspan_next##RANK(it->pos, it->_s->shape, it->_s->stride.d, RANK, i, inc, &done); \
if (done) it->ref = NULL; \
} \
struct stc_nostruct
@@ -223,8 +224,8 @@ STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t shape[], const int32_t s
return off;
}
-STC_API intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_t stride[], int rank, int* done);
-#define _cspan_next1(pos, shape, stride, rank, done) (*done = ++pos[0]==shape[0], stride[0])
+STC_API intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_t stride[], int rank, int i, int inc, int* done);
+#define _cspan_next1(pos, shape, stride, rank, i, inc, done) (*done = ++pos[0]==shape[0], stride[0])
#define _cspan_next3 _cspan_next2
#define _cspan_next4 _cspan_next2
#define _cspan_next5 _cspan_next2
@@ -242,26 +243,27 @@ STC_API int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank);
/* --------------------- IMPLEMENTATION --------------------- */
#if defined(i_implement) || defined(i_static)
-STC_DEF intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_t stride[], int rank, int* done) {
- int i, inc;
- if (stride[0] < stride[rank - 1]) i = rank - 1, inc = -1; else i = 0, inc = 1;
+STC_DEF intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_t stride[], int rank, int i, int inc, int* done) {
intptr_t off = stride[i];
++pos[i];
- for (; --rank && pos[i] == shape[i]; i += inc) {
+ while (--rank && pos[i] == shape[i]) {
pos[i] = 0; ++pos[i + inc];
off += stride[i + inc] - stride[i]*shape[i];
+ i += inc;
}
*done = pos[i] == shape[i];
return off;
}
STC_DEF int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank) {
- int32_t k = 1, i, j, inc, s1, s2;
- if (order == 'F') i = 0, j = rank, inc = 1;
- else /* 'C' */ i = rank - 1, j = -1, inc = -1;
- s1 = shape[i]; shape[i] = 1;
+ int i, inc;
+ if (order == 'F') i = 0, inc = 1;
+ else i = rank - 1, inc = -1;
+ int32_t k = 1, s1 = shape[i], s2;
- for (i += inc; i != j; i += inc) {
+ shape[i] = 1;
+ while (--rank) {
+ i += inc;
s2 = shape[i];
shape[i] = (k *= s1);
s1 = s2;
diff --git a/misc/benchmarks/various/cspan_bench.c b/misc/benchmarks/various/cspan_bench.c
index f4b067f8..b5caca83 100644
--- a/misc/benchmarks/various/cspan_bench.c
+++ b/misc/benchmarks/various/cspan_bench.c
@@ -49,9 +49,8 @@ static void TraditionalForLoop(intptr_t n)
for (int x = lx; x < hx; ++x) {
for (int y = ly; y < hy; ++y) {
for (int z = lz; z < hz; ++z) {
- int i = nz*(ny*x + y) + z;
- double d = Vin[i];
- Vout[i] += d;
+ double d = Vin[nz*(ny*x + y) + z];
+ Vout[nz*(ny*x + y) + z] += d;
sum += d;
}
}
diff --git a/misc/examples/spans/multidim.c b/misc/examples/spans/multidim.c
index 798a1126..ebc05a70 100644
--- a/misc/examples/spans/multidim.c
+++ b/misc/examples/spans/multidim.c
@@ -1,66 +1,71 @@
// Example based on https://en.cppreference.com/w/cpp/container/mdspan
#define i_val int
#include <stc/cstack.h>
+#define i_implement
#include <stc/cspan.h>
#include <stdio.h>
using_cspan3(ispan, int);
+void print2d(ispan2 ms2) {
+ for (int i=0; i < ms2.shape[0]; i++) {
+ for (int j=0; j < ms2.shape[1]; j++)
+ printf(" %3d", *cspan_at(&ms2, i, j));
+ puts("");
+ }
+}
+
+void print3d(ispan3 ms3) {
+ for (int i=0; i < ms3.shape[0]; i++) {
+ for (int j=0; j < ms3.shape[1]; j++) {
+ for (int k=0; k < ms3.shape[2]; k++)
+ printf(" %3d", *cspan_at(&ms3, i, j, k));
+ puts("");
+ }
+ puts("");
+ }
+}
+
int main(void)
{
cstack_int v = c_init(cstack_int, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24});
- // View data as contiguous memory representing 24 ints
+ // Create 1d span from a compatibel container
ispan ms1 = cspan_from(&v);
- // View the same data as a 3D array 2 x 3 x 4
+ // Create a 3D mdspan 2 x 3 x 4
ispan3 ms3 = cspan_md(v.data, 2, 3, 4);
puts("ms3:");
- for (int i=0; i != ms3.shape[0]; i++) {
- for (int j=0; j != ms3.shape[1]; j++) {
- for (int k=0; k != ms3.shape[2]; k++) {
- printf(" %2d", *cspan_at(&ms3, i, j, k));
- }
- puts("");
- }
- puts("");
- }
- puts("ss3 = ms3[:, 1:3, 1:3]");
- ispan3 ss3 = ms3;
- ss3 = cspan_slice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3});
+ print3d(ms3);
- for (int i=0; i != ss3.shape[0]; i++) {
- for (int j=0; j != ss3.shape[1]; j++) {
- for (int k=0; k != ss3.shape[2]; k++) {
- printf(" %2d", *cspan_at(&ss3, i, j, k));
- }
- puts("");
- }
- puts("");
- }
+ // Take a slice of md3
+ ispan3 ss3 = cspan_slice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3});
+ puts("ss3 = ms3[:, 1:3, 1:3]");
+ print3d(ss3);
puts("Iterate ss3 flat:");
- c_foreach (i, ispan3, ss3)
- printf(" %d", *i.ref);
+ c_foreach (i, ispan3, ss3) printf(" %d", *i.ref);
puts("");
- ispan2 ms2 = cspan_submd3(&ms3, 0);
+ // submd3 span reduces rank depending on number of arguments
+ ispan2 ms2 = cspan_submd3(&ms3, 1);
- // write data using 2D view
+ // Change data on the 2d subspan
for (int i=0; i != ms2.shape[0]; i++)
for (int j=0; j != ms2.shape[1]; j++)
- *cspan_at(&ms2, i, j) = i*1000 + j;
+ *cspan_at(&ms2, i, j) = (i + 1)*100 + j;
+
+ puts("\nms2 = ms3[1] with updated data:");
+ print2d(ms2);
+ puts("");
- puts("\nview data as 1D view:");
- for (int i=0; i != cspan_size(&ms1); i++)
- printf(" %d", *cspan_at(&ms1, i));
+ puts("\nOriginal s1 span with updated data:");
+ c_foreach (i, ispan, ms1) printf(" %d", *i.ref);
puts("");
- puts("iterate subspan ms3[1]:");
- ispan2 sub = cspan_submd3(&ms3, 1);
- c_foreach (i, ispan2, sub)
- printf(" %d", *i.ref);
+ puts("\nOriginal ms3 span with updated data:");
+ print3d(ms3);
puts("");
cstack_int_drop(&v);