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 /include/stc/cspan.h | |
| 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.
Diffstat (limited to 'include/stc/cspan.h')
| -rw-r--r-- | include/stc/cspan.h | 94 |
1 files changed, 47 insertions, 47 deletions
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; |
