summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-15 18:06:22 +0200
committertylov <[email protected]>2023-07-15 18:06:22 +0200
commit23aebb77554bc43c929704e9f1c46dc4520024df (patch)
treed72d24c02a46acba838ba812bc6bc2746abea37b
parent0073f0a2d67239f019041f07d9a322df03fc7ae4 (diff)
downloadSTC-modified-23aebb77554bc43c929704e9f1c46dc4520024df.tar.gz
STC-modified-23aebb77554bc43c929704e9f1c46dc4520024df.zip
Reverted to cspan_submdX() without output span type as first argument. Type/argument safety is still present.
-rw-r--r--docs/cspan_api.md20
-rw-r--r--include/stc/cspan.h43
-rw-r--r--misc/examples/multidim.c4
-rw-r--r--misc/tests/cspan_test.c30
4 files changed, 45 insertions, 52 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md
index 1d3177da..1089e48d 100644
--- a/docs/cspan_api.md
+++ b/docs/cspan_api.md
@@ -30,11 +30,6 @@ SpanType cspan_init(T SpanType, {v1, v2, ...}); // make a 1-
SpanType cspan_from(STCContainer* cnt); // make a 1-d cspan from compatible STC container
SpanType cspan_from_array(ValueType array[]); // make a 1-d cspan from C array
- // make a subspan of input span rank. Like e.g. cspan_slice(Span3, &ms3, {off,off+count}, {c_ALL}, {c_ALL});
-SpanType cspan_subspan(const SpanType* span, intptr_t offset, intptr_t count);
-SpanType2 cspan_subspan2(const SpanType2* span, intptr_t offset, intptr_t count);
-SpanType3 cspan_subspan3(const SpanType3* span, intptr_t offset, intptr_t count);
-
intptr_t cspan_size(const SpanTypeN* self); // return number of elements
intptr_t cspan_rank(const SpanTypeN* self); // dimensions; compile time constant
intptr_t cspan_index(const SpanTypeN* self, intptr_t x, ..); // index of element
@@ -54,10 +49,15 @@ SpanTypeN cspan_md_order(char order, ValueType* data, d1, d2, ...); // ord
void cspan_transpose(const SpanTypeN* self);
bool cspan_is_order_F(const SpanTypeN* self);
+ // create a subspan of input span rank. Like e.g. cspan_slice(Span3, &ms3, {off,off+count}, {c_ALL}, {c_ALL});
+SpanType cspan_subspan(const SpanType* span, intptr_t offset, intptr_t count);
+SpanType2 cspan_subspan2(const SpanType2* span, intptr_t offset, intptr_t count);
+SpanType3 cspan_subspan3(const SpanType3* span, intptr_t offset, intptr_t count);
+
// create a sub md span of lower rank. Like e.g. cspan_slice(Span2, &ms4, {x}, {y}, {c_ALL}, {c_ALL});
-OutSpan1 cspan_submd2(TYPE OutSpan1, const SpanType2* parent, intptr_t x); // return a 1d subspan from a 2d span.
-OutSpanN cspan_submd3(TYPE OutSpanN, const SpanType3* parent, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span.
-OutSpanN cspan_submd4(TYPE OutSpanN, const SpanType4* parent, intptr_t x, ...); // number of args decides rank of output span.
+OutSpan1 cspan_submd2(const SpanType2* parent, intptr_t x); // return a 1d subspan from a 2d span.
+OutSpanN cspan_submd3(const SpanType3* parent, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span.
+OutSpanN cspan_submd4(const SpanType4* parent, intptr_t x, ...); // number of args decides rank of output span.
// general slicing of an md span.
// {i}: reduce rank. {i,c_END}: slice to end. {c_ALL}: use full extent.
@@ -106,7 +106,7 @@ int main() {
myspan3 ms3 = cspan_md(arr, 2, 3, 4); // C-order, i.e. row-major.
myspan3 ss3 = cspan_slice(myspan3, &ms3, {c_ALL}, {1,3}, {2,c_END});
- myspan2 ss2 = cspan_submd3(myspan2, &ss3, 1);
+ myspan2 ss2 = cspan_submd3(&ss3, 1);
c_forrange (i, ss2.shape[0])
c_forrange (j, ss2.shape[1])
@@ -156,7 +156,7 @@ int main()
Span3 span3 = cspan_md(span.data, 2, 4, 3);
// reduce rank: (i.e. span3[1])
- Span2 span2 = cspan_submd3(Span2, &span3, 1);
+ Span2 span2 = cspan_submd3(&span3, 1);
puts("\niterate span2 flat:");
c_foreach (i, Span2, span2)
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index 358d5bf0..582e1004 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -82,7 +82,7 @@ int demo2() {
} \
STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t shape[], const int32_t stri[], \
const int rank, const int32_t a[][2]) { \
- Self s = {.data=v}; int outrank; \
+ Self s; s.data = v; int outrank; \
s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \
c_assert(outrank == RANK); \
return s; \
@@ -144,30 +144,23 @@ using_cspan_tuple(7); using_cspan_tuple(8);
{.data=cspan_at(self, offset, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2]}, .stride=(self)->stride}
// cspan_submd(): Reduce rank (N <= 4) Optimized, same as e.g. cspan_slice(Span2, &ms4, {x}, {y}, {c_ALL}, {c_ALL});
-#define cspan_submd2(OutSpan, self, ...) _cspan_submdN(OutSpan, 2, self, __VA_ARGS__)
-#define cspan_submd3(OutSpan, self, ...) _cspan_submdN(OutSpan, 3, self, __VA_ARGS__)
-#define cspan_submd4(OutSpan, self, ...) _cspan_submdN(OutSpan, 4, self, __VA_ARGS__)
-
-#define _cspan_submdN(OutSpan, N, self, ...) \
- (OutSpan)_cspan_submd##N(c_static_assert(cspan_rank((OutSpan*)0) == N - c_NUMARGS(__VA_ARGS__)), self, __VA_ARGS__)
-
-#define _cspan_submd2(ok, self, x) \
- {.data=cspan_at(self, x, 0) + ok, .shape={(self)->shape[1]}, .stride={.d={(self)->stride.d[1]}}}
-#define _cspan_submd3(...) c_MACRO_OVERLOAD(_cspan_submd3, __VA_ARGS__)
-#define _cspan_submd3_3(ok, self, x) \
- {.data=cspan_at(self, x, 0, 0) + ok, .shape={(self)->shape[1], (self)->shape[2]}, \
- .stride={.d={(self)->stride.d[1], (self)->stride.d[2]}}}
-#define _cspan_submd3_4(ok, self, x, y) \
- {.data=cspan_at(self, x, y, 0) + ok, .shape={(self)->shape[2]}, .stride={.d={(self)->stride.d[2]}}}
-#define _cspan_submd4(...) c_MACRO_OVERLOAD(_cspan_submd4, __VA_ARGS__)
-#define _cspan_submd4_3(ok, self, x) \
- {.data=cspan_at(self, x, 0, 0, 0) + ok, .shape={(self)->shape[1], (self)->shape[2], (self)->shape[3]}, \
- .stride={.d={(self)->stride.d[1], (self)->stride.d[2], (self)->stride.d[3]}}}
-#define _cspan_submd4_4(ok, self, x, y) \
- {.data=cspan_at(self, x, y, 0, 0) + ok, .shape={(self)->shape[2], (self)->shape[3]}, \
- .stride={.d={(self)->stride.d[2], (self)->stride.d[3]}}}
-#define _cspan_submd4_5(ok, self, x, y, z) \
- {.data=cspan_at(self, x, y, z, 0) + ok, .shape={(self)->shape[3]}, .stride={.d={(self)->stride.d[3]}}}
+#define cspan_submd2(self, x) \
+ {.data=cspan_at(self, x, 0), .shape={(self)->shape[1]}, .stride=(cspan_tuple1){.d={(self)->stride.d[1]}}}
+#define cspan_submd3(...) c_MACRO_OVERLOAD(cspan_submd3, __VA_ARGS__)
+#define cspan_submd3_2(self, x) \
+ {.data=cspan_at(self, x, 0, 0), .shape={(self)->shape[1], (self)->shape[2]}, \
+ .stride=(cspan_tuple2){.d={(self)->stride.d[1], (self)->stride.d[2]}}}
+#define cspan_submd3_3(self, x, y) \
+ {.data=cspan_at(self, x, y, 0), .shape={(self)->shape[2]}, .stride=(cspan_tuple1){.d={(self)->stride.d[2]}}}
+#define cspan_submd4(...) c_MACRO_OVERLOAD(cspan_submd4, __VA_ARGS__)
+#define cspan_submd4_2(self, x) \
+ {.data=cspan_at(self, x, 0, 0, 0), .shape={(self)->shape[1], (self)->shape[2], (self)->shape[3]}, \
+ .stride=(cspan_tuple3){.d={(self)->stride.d[1], (self)->stride.d[2], (self)->stride.d[3]}}}
+#define cspan_submd4_3(self, x, y) \
+ {.data=cspan_at(self, x, y, 0, 0), .shape={(self)->shape[2], (self)->shape[3]}, \
+ .stride=(cspan_tuple2){.d={(self)->stride.d[2], (self)->stride.d[3]}}}
+#define cspan_submd4_4(self, x, y, z) \
+ {.data=cspan_at(self, x, y, z, 0), .shape={(self)->shape[3]}, .stride=(cspan_tuple1){.d={(self)->stride.d[3]}}}
#define cspan_md(array, ...) cspan_md_order('C', array, __VA_ARGS__)
#define cspan_md_order(order, array, ...) /* order='C' or 'F' */ \
diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c
index dbea9699..45b97378 100644
--- a/misc/examples/multidim.c
+++ b/misc/examples/multidim.c
@@ -45,7 +45,7 @@ int main()
printf(" %d", *i.ref);
puts("");
- ispan2 ms2 = cspan_submd3(ispan2, &ms3, 0);
+ ispan2 ms2 = cspan_submd3(&ms3, 0);
// write data using 2D view
for (int i=0; i != ms2.shape[0]; i++)
@@ -58,7 +58,7 @@ int main()
puts("");
puts("iterate subspan ms3[1]:");
- ispan2 sub = cspan_submd3(ispan2, &ms3, 1);
+ ispan2 sub = cspan_submd3(&ms3, 1);
c_foreach (i, ispan2, sub)
printf(" %d", *i.ref);
puts("");
diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c
index aa055ea6..d7ca9b64 100644
--- a/misc/tests/cspan_test.c
+++ b/misc/tests/cspan_test.c
@@ -10,11 +10,11 @@ CTEST(cspan, subdim) {
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
intspan3 m = cspan_md(array, 2, 2, 3);
- for (size_t i = 0; i < m.shape[0]; ++i) {
- intspan2 sub_i = cspan_submd3(intspan2, &m, i);
- for (size_t j = 0; j < m.shape[1]; ++j) {
- intspan sub_i_j = cspan_submd2(intspan, &sub_i, j);
- for (size_t k = 0; k < m.shape[2]; ++k) {
+ for (int i = 0; i < m.shape[0]; ++i) {
+ intspan2 sub_i = cspan_submd3(&m, i);
+ for (int j = 0; j < m.shape[1]; ++j) {
+ intspan sub_i_j = cspan_submd2(&sub_i, j);
+ for (int k = 0; k < m.shape[2]; ++k) {
ASSERT_EQ(*cspan_at(&sub_i_j, k), *cspan_at(&m, i, j, k));
}
}
@@ -25,18 +25,18 @@ CTEST(cspan, slice) {
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
intspan2 m1 = cspan_md(array, 3, 4);
- size_t sum1 = 0;
- for (size_t i = 0; i < m1.shape[0]; ++i) {
- for (size_t j = 0; j < m1.shape[1]; ++j) {
+ int sum1 = 0;
+ for (int i = 0; i < m1.shape[0]; ++i) {
+ for (int j = 0; j < m1.shape[1]; ++j) {
sum1 += *cspan_at(&m1, i, j);
}
}
intspan2 m2 = cspan_slice(intspan2, &m1, {c_ALL}, {2,4});
- size_t sum2 = 0;
- for (size_t i = 0; i < m2.shape[0]; ++i) {
- for (size_t j = 0; j < m2.shape[1]; ++j) {
+ int sum2 = 0;
+ for (int i = 0; i < m2.shape[0]; ++i) {
+ for (int j = 0; j < m2.shape[1]; ++j) {
sum2 += *cspan_at(&m2, i, j);
}
}
@@ -56,10 +56,10 @@ CTEST(cspan, slice2) {
intspan3 ms3 = cspan_md(stack.data, 10, 20, 30);
ms3 = cspan_slice(intspan3, &ms3, {1,4}, {3,7}, {20,24});
- size_t sum = 0;
- for (size_t i = 0; i < ms3.shape[0]; ++i) {
- for (size_t j = 0; j < ms3.shape[1]; ++j) {
- for (size_t k = 0; k < ms3.shape[2]; ++k) {
+ int sum = 0;
+ 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) {
sum += *cspan_at(&ms3, i, j, k);
}
}