diff options
| author | Tyge Løvset <[email protected]> | 2023-02-03 22:37:59 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-03 22:37:59 +0100 |
| commit | 2f370b34484e63359f9fd2850b72d3d1d8dfc32a (patch) | |
| tree | 609fc076aca1f1d218cdbd95379cf795349dce92 | |
| parent | cd806510c7c77c0618af58bd804e36f2bad6d219 (diff) | |
| download | STC-modified-2f370b34484e63359f9fd2850b72d3d1d8dfc32a.tar.gz STC-modified-2f370b34484e63359f9fd2850b72d3d1d8dfc32a.zip | |
Renamed dim to shape in cspan. Not 100% sure it will survive.
| -rw-r--r-- | docs/cspan_api.md | 12 | ||||
| -rw-r--r-- | include/stc/cspan.h | 94 | ||||
| -rw-r--r-- | misc/examples/multidim.c | 16 | ||||
| -rw-r--r-- | misc/tests/cspan_test.c | 26 |
4 files changed, 74 insertions, 74 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 8321b1e5..66a7b01b 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -58,7 +58,7 @@ void SpanType_next(SpanTypeN_iter* it); | Type name | Type definition | Used to represent... | |:------------------|:-----------------------------------------------|:---------------------| -| SpanTypeN | `struct { ValueType *data; uint32_t dim[N]; }` | SpanType with rank N | +| SpanTypeN | `struct { ValueType *data; uint32_t shape[N]; }` | SpanType with rank N | | SpanTypeN`_value` | `ValueType` | The ValueType | | `c_ALL` | `0,-1` | Full extent | | `c_END` | `-1` | End of extent | @@ -100,8 +100,8 @@ int main() { myspan3 ss3 = cspan_slice(myspan3, &ms3, {c_ALL}, {1,3}, {2,c_END}); myspan2 ss2 = cspan_submd3(&ss3, 1); - c_FORRANGE (i, ss2.dim[0]) - c_FORRANGE (j, ss2.dim[1]) + c_FORRANGE (i, ss2.shape[0]) + c_FORRANGE (j, ss2.shape[1]) fmt_print(" {}", *cspan_at(&ss2, i, j)); puts(""); @@ -163,9 +163,9 @@ int main() fmt_print("{} ", *i.ref); puts("\niterate span3 by dimensions:"); - c_FORRANGE (i, span3.dim[0]) { - c_FORRANGE (j, span3.dim[1]) { - c_FORRANGE (k, span3.dim[2]) + c_FORRANGE (i, span3.shape[0]) { + c_FORRANGE (j, span3.shape[1]) { + c_FORRANGE (k, span3.shape[2]) fmt_printf(" {:2}", *cspan_at(&span3, i, j, k)); printf(" |"); } diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 315465db..3ddc9742 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -32,8 +32,8 @@ int demo1() { float raw[4*5]; Span2f ms = cspan_md(raw, 4, 5); - for (int i=0; i<ms.dim[0]; i++) - for (int j=0; j<ms.dim[1]; j++) + for (int i=0; i<ms.shape[0]; i++) + for (int j=0; j<ms.shape[1]; j++) *cspan_at(&ms, i, j) = i*1000 + j; printf("%f\n", *cspan_at(&ms, 3, 4)); @@ -66,18 +66,18 @@ int demo2() { typedef T Self##_value; typedef T Self##_raw; \ typedef struct { \ Self##_value *data; \ - int32_t dim[RANK]; \ + int32_t shape[RANK]; \ cspan_idx##RANK stride; \ } Self; \ typedef struct { Self##_value *ref; int32_t pos[RANK]; const Self *_s; } Self##_iter; \ \ STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ - return (Self){.data=raw, .dim={(int32_t)n}}; \ + return (Self){.data=raw, .shape={(int32_t)n}}; \ } \ - STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t dim[], const int32_t stri[], \ + 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; \ - s.data += _cspan_slice(s.dim, s.stride.d, &outrank, dim, stri, rank, a); \ + s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \ c_ASSERT(outrank == RANK); \ return s; \ } \ @@ -90,8 +90,8 @@ int demo2() { return it; \ } \ STC_INLINE void Self##_next(Self##_iter* it) { \ - it->ref += _cspan_next##RANK(RANK, it->pos, it->_s->dim, it->_s->stride.d); \ - if (it->pos[0] == it->_s->dim[0]) it->ref = NULL; \ + it->ref += _cspan_next##RANK(RANK, it->pos, it->_s->shape, it->_s->stride.d); \ + if (it->pos[0] == it->_s->shape[0]) it->ref = NULL; \ } \ struct stc_nostruct @@ -108,35 +108,35 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define c_ALL 0,c_END #define cspan_md(array, ...) \ - {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} + {.data=array, .shape={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} /* For static initialization, use cspan_make(). c_make() for non-static only. */ #define cspan_make(SpanType, ...) \ - {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} + {.data=(SpanType##_value[])__VA_ARGS__, .shape={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} #define cspan_slice(OutSpan, parent, ...) \ - OutSpan##_slice_((parent)->data, (parent)->dim, (parent)->stride.d, cspan_rank(parent) + \ + OutSpan##_slice_((parent)->data, (parent)->shape, (parent)->stride.d, cspan_rank(parent) + \ c_static_assert(cspan_rank(parent) == sizeof((int32_t[][2]){__VA_ARGS__})/sizeof(int32_t[2])), \ (const int32_t[][2]){__VA_ARGS__}) /* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ #define cspan_from(container) \ - {.data=(container)->data, .dim={(int32_t)(container)->_len}} + {.data=(container)->data, .shape={(int32_t)(container)->_len}} #define cspan_from_array(array) \ - {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}} + {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .shape={c_ARRAYLEN(array)}} -#define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) -#define cspan_rank(self) c_ARRAYLEN((self)->dim) +#define cspan_size(self) _cspan_size((self)->shape, cspan_rank(self)) +#define cspan_rank(self) c_ARRAYLEN((self)->shape) #define cspan_index(self, ...) c_PASTE(cspan_idx_, c_NUMARGS(__VA_ARGS__))(self, __VA_ARGS__) #define cspan_idx_1 cspan_idx_4 #define cspan_idx_2 cspan_idx_4 #define cspan_idx_3 cspan_idx_4 #define cspan_idx_4(self, ...) \ - c_PASTE(_cspan_idx, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__) + c_PASTE(_cspan_idx, c_NUMARGS(__VA_ARGS__))((self)->shape, (self)->stride, __VA_ARGS__) #define cspan_idx_5(self, ...) \ - (_cspan_idxN(c_NUMARGS(__VA_ARGS__), (self)->dim, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ + (_cspan_idxN(c_NUMARGS(__VA_ARGS__), (self)->shape, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ c_static_assert(cspan_rank(self) == c_NUMARGS(__VA_ARGS__))) #define cspan_idx_6 cspan_idx_5 @@ -146,65 +146,65 @@ typedef struct { int32_t d[6]; } cspan_idx6; // cspan_subspanN. (N<5) Optimized, but same as e.g. cspan_slice(Span2, &ms4, {offset, offset + count}, {c_ALL}, {c_ALL}); #define cspan_subspan(self, offset, count) \ - {.data=cspan_at(self, offset), .dim={count}} + {.data=cspan_at(self, offset), .shape={count}} #define cspan_subspan2(self, offset, count) \ - {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[1]}, .stride={(self)->stride}} + {.data=cspan_at(self, offset, 0), .shape={count, (self)->shape[1]}, .stride={(self)->stride}} #define cspan_subspan3(self, offset, count) \ - {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}, .stride={(self)->stride}} + {.data=cspan_at(self, offset, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2]}, .stride={(self)->stride}} #define cspan_subspan4(self, offset, count) \ - {.data=cspan_at(self, offset, 0, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ + {.data=cspan_at(self, offset, 0, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2], (self)->shape[3]}, \ .stride={(self)->stride}} // cspan_submdN: return reduced rank #define cspan_submd4(...) c_MACRO_OVERLOAD(cspan_submd4, __VA_ARGS__) #define cspan_submd3(...) c_MACRO_OVERLOAD(cspan_submd3, __VA_ARGS__) #define cspan_submd2(self, x) \ - {.data=cspan_at(self, x, 0), .dim={(self)->dim[1]}} + {.data=cspan_at(self, x, 0), .shape={(self)->shape[1]}} #define cspan_submd3_2(self, x) \ - {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}, \ + {.data=cspan_at(self, x, 0, 0), .shape={(self)->shape[1], (self)->shape[2]}, \ .stride={.d={0, (self)->stride.d[2]}}} #define cspan_submd3_3(self, x, y) \ - {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} + {.data=cspan_at(self, x, y, 0), .shape={(self)->shape[2]}} #define cspan_submd4_2(self, x) \ - {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ + {.data=cspan_at(self, x, 0, 0, 0), .shape={(self)->shape[1], (self)->shape[2], (self)->shape[3]}, \ .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3]}}} #define cspan_submd4_3(self, x, y) \ - {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}, .stride={.d={0, (self)->stride.d[3]}}} + {.data=cspan_at(self, x, y, 0, 0), .shape={(self)->shape[2], (self)->shape[3]}, .stride={.d={0, (self)->stride.d[3]}}} #define cspan_submd4_4(self, x, y, z) \ - {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} + {.data=cspan_at(self, x, y, z, 0), .shape={(self)->shape[3]}} // FUNCTIONS -STC_INLINE intptr_t _cspan_idx1(const int32_t dim[1], const cspan_idx1 stri, int32_t x) - { c_ASSERT(c_LTu(x, dim[0])); return x; } +STC_INLINE intptr_t _cspan_idx1(const int32_t shape[1], const cspan_idx1 stri, int32_t x) + { c_ASSERT(c_LTu(x, shape[0])); return x; } -STC_INLINE intptr_t _cspan_idx2(const int32_t dim[2], const cspan_idx2 stri, int32_t x, int32_t y) - { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1])); return (intptr_t)stri.d[1]*x + y; } +STC_INLINE intptr_t _cspan_idx2(const int32_t shape[2], const cspan_idx2 stri, int32_t x, int32_t y) + { c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1])); return (intptr_t)stri.d[1]*x + y; } -STC_INLINE intptr_t _cspan_idx3(const int32_t dim[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { - c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2])); +STC_INLINE intptr_t _cspan_idx3(const int32_t shape[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { + c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1]) && c_LTu(z, shape[2])); return (intptr_t)stri.d[2]*(stri.d[1]*x + y) + z; } -STC_INLINE intptr_t _cspan_idx4(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, +STC_INLINE intptr_t _cspan_idx4(const int32_t shape[4], const cspan_idx4 stri, int32_t x, int32_t y, int32_t z, int32_t w) { - c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3])); + c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1]) && c_LTu(z, shape[2]) && c_LTu(w, shape[3])); return (intptr_t)stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w; } -STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t dim[], const int32_t stri[], const int32_t a[]) { +STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t shape[], const int32_t stri[], const int32_t a[]) { intptr_t off = a[0]; - bool ok = c_LTu(a[0], dim[0]); + bool ok = c_LTu(a[0], shape[0]); for (int i = 1; i < rank; ++i) { off *= stri[i]; off += a[i]; - ok &= c_LTu(a[i], dim[i]); + ok &= c_LTu(a[i], shape[i]); } c_ASSERT(ok); return off; } -STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { - intptr_t sz = dim[0]; - while (rank-- > 1) sz *= dim[rank]; +STC_INLINE intptr_t _cspan_size(const int32_t shape[], int rank) { + intptr_t sz = shape[0]; + while (rank-- > 1) sz *= shape[rank]; return sz; } @@ -214,12 +214,12 @@ STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { #define _cspan_next5 _cspan_next2 #define _cspan_next6 _cspan_next2 -STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t dim[], const int32_t stride[]) { +STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t shape[], const int32_t stride[]) { intptr_t off = 1, rs = 1; ++pos[rank - 1]; - while (--rank && pos[rank] == dim[rank]) { + while (--rank && pos[rank] == shape[rank]) { pos[rank] = 0, ++pos[rank - 1]; - const intptr_t ds = rs*dim[rank]; + const intptr_t ds = rs*shape[rank]; rs *= stride[rank]; off += rs - ds; } @@ -227,7 +227,7 @@ STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t dim[], c } STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, - const int32_t dim[], const int32_t stri[], + const int32_t shape[], const int32_t stri[], int rank, const int32_t a[][2]) { intptr_t off = 0, s = 1; int i = 0, j = 0, ok = true; @@ -237,12 +237,12 @@ STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, int32_t t; switch (a[i][1]) { case 0: s *= stri[i]; continue; - case -1: t = dim[i]; break; + case -1: t = shape[i]; break; default: t = a[i][1]; break; } odim[j] = t - a[i][0]; ostri[j] = s*stri[i]; - ok &= c_LTu(0, odim[j]) & !c_LTu(dim[i], t); + ok &= c_LTu(0, odim[j]) & !c_LTu(shape[i], t); s = 1; ++j; } *orank = j; diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 49d7fbc4..e61959ca 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -19,9 +19,9 @@ int main() ispan3 ms3 = cspan_md(v.data, 2, 3, 4); puts("ms3:"); - for (int i=0; i != ms3.dim[0]; i++) { - for (int j=0; j != ms3.dim[1]; j++) { - for (int k=0; k != ms3.dim[2]; k++) { + 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(""); @@ -33,9 +33,9 @@ int main() //cspan_slice(&ss3, {c_ALL}, {1,3}, {1,3}); ss3 = cspan_slice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3}); - for (int i=0; i != ss3.dim[0]; i++) { - for (int j=0; j != ss3.dim[1]; j++) { - for (int k=0; k != ss3.dim[2]; k++) { + 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(""); @@ -51,8 +51,8 @@ int main() ispan2 ms2 = cspan_submd3(&ms3, 0); // write data using 2D view - for (int i=0; i != ms2.dim[0]; i++) - for (int j=0; j != ms2.dim[1]; j++) + 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; puts("\nview data as 1D view:"); diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 044ce629..9e33467c 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -9,11 +9,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.dim[0]; ++i) { + for (size_t i = 0; i < m.shape[0]; ++i) { intspan2 sub_i = cspan_submd3(&m, i); - for (size_t j = 0; j < m.dim[1]; ++j) { + for (size_t j = 0; j < m.shape[1]; ++j) { intspan sub_i_j = cspan_submd2(&sub_i, j); - for (size_t k = 0; k < m.dim[2]; ++k) { + for (size_t k = 0; k < m.shape[2]; ++k) { ASSERT_EQ(*cspan_at(&sub_i_j, k), *cspan_at(&m, i, j, k)); } } @@ -25,8 +25,8 @@ CTEST(cspan, slice) { intspan2 m1 = cspan_md(array, 3, 4); size_t sum1 = 0; - for (size_t i = 0; i < m1.dim[0]; ++i) { - for (size_t j = 0; j < m1.dim[1]; ++j) { + for (size_t i = 0; i < m1.shape[0]; ++i) { + for (size_t j = 0; j < m1.shape[1]; ++j) { sum1 += *cspan_at(&m1, i, j); } } @@ -34,8 +34,8 @@ CTEST(cspan, slice) { intspan2 m2 = cspan_slice(intspan2, &m1, {c_ALL}, {2,4}); size_t sum2 = 0; - for (size_t i = 0; i < m2.dim[0]; ++i) { - for (size_t j = 0; j < m2.dim[1]; ++j) { + for (size_t i = 0; i < m2.shape[0]; ++i) { + for (size_t j = 0; j < m2.shape[1]; ++j) { sum2 += *cspan_at(&m2, i, j); } } @@ -56,9 +56,9 @@ CTEST(cspan, slice2) { ms3 = cspan_slice(intspan3, &ms3, {1,4}, {3,7}, {20,24}); size_t sum = 0; - for (size_t i = 0; i < ms3.dim[0]; ++i) { - for (size_t j = 0; j < ms3.dim[1]; ++j) { - for (size_t k = 0; k < ms3.dim[2]; ++k) { + 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) { sum += *cspan_at(&ms3, i, j, k); } } @@ -94,9 +94,9 @@ CTEST_SETUP(cspan_cube) { intspan3 ms3 = cspan_md(_self->stack.data, CUBE, CUBE, CUBE); - c_FORRANGE (i, 0, ms3.dim[0], TSIZE) { - c_FORRANGE (j, 0, ms3.dim[1], TSIZE) { - c_FORRANGE (k, 0, ms3.dim[2], TSIZE) { + c_FORRANGE (i, 0, ms3.shape[0], TSIZE) { + c_FORRANGE (j, 0, ms3.shape[1], TSIZE) { + c_FORRANGE (k, 0, ms3.shape[2], TSIZE) { intspan3 tile = cspan_slice(intspan3, &ms3, {i, i + TSIZE}, {j, j + TSIZE}, {k, k + TSIZE}); Tiles_push(&_self->tiles, tile); } |
