summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-03 17:48:43 +0100
committerTyge Løvset <[email protected]>2023-02-03 17:48:43 +0100
commit5f4076de684da4507943a86e26dcaa5479cdf69b (patch)
treed7d4673f295e743d80ccf40e45c5b6aa47976a76
parentfeef5067e0c3f1cf113b4fa8b302ac1bfa249e68 (diff)
downloadSTC-modified-5f4076de684da4507943a86e26dcaa5479cdf69b.tar.gz
STC-modified-5f4076de684da4507943a86e26dcaa5479cdf69b.zip
Temporary commit. Will cleanup cspan and usage.
-rw-r--r--CMakeLists.txt8
-rw-r--r--include/stc/cspan.h30
-rw-r--r--include/stc/forward.h28
-rw-r--r--misc/examples/multidim.c4
-rw-r--r--misc/tests/cspan_test.c2
5 files changed, 30 insertions, 42 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2f54a7f2..9369a070 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,10 +16,10 @@ if(BUILD_TESTING)
foreach(file IN LISTS misc/examples)
get_filename_component(name "${file}" NAME_WE)
add_executable(${name} ${file})
- target_compile_options(${name} PRIVATE "-pthread")
- if(CMAKE_THREAD_LIBS_INIT)
- target_link_libraries(${name} PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
- endif()
+ target_compile_options(${name} PRIVATE "-pthread")
+ if(CMAKE_THREAD_LIBS_INIT)
+ target_link_libraries(${name} PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
+ endif()
target_link_libraries(${name} PRIVATE stc m)
add_test(NAME ${name} COMMAND ${name})
endforeach()
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index 9e981009..78c92520 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -74,6 +74,13 @@ int demo2() {
STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \
return (Self){.data=raw, .dim={(int32_t)n}}; \
} \
+ STC_INLINE Self Self##_subslice_(Self##_value* v, const int32_t dim[], const int32_t stri[], \
+ const int rank, const int32_t a[][2]) { \
+ Self s = {.data=v}; int outrank; \
+ s.data += _cspan_subslice(s.dim, s.stride.d, &outrank, dim, stri, rank, a); \
+ c_ASSERT(outrank == RANK); \
+ return s; \
+ } \
STC_INLINE Self##_iter Self##_begin(const Self* self) { \
Self##_iter it = {.ref=self->data, .pos={0}, ._s=self}; \
return it; \
@@ -91,12 +98,14 @@ int demo2() {
#define using_cspan2(Self, T) using_cspan_3(Self, T, 1); using_cspan_3(Self##2, T, 2)
#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan_3(Self##3, T, 3)
#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan_3(Self##4, T, 4)
-#define using_cspan5(Self, T) using_cspan4(Self, T); using_cspan_4(Self##4, T, 5)
typedef struct { int32_t d[1]; } cspan_idx1;
typedef struct { int32_t d[2]; } cspan_idx2;
typedef struct { int32_t d[3]; } cspan_idx3;
typedef struct { int32_t d[4]; } cspan_idx4;
typedef struct { int32_t d[5]; } cspan_idx5;
+typedef struct { int32_t d[6]; } cspan_idx6;
+#define c_END -1
+#define c_ALL 0,c_END
#define cspan_md(array, ...) \
{.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}}
@@ -105,6 +114,11 @@ typedef struct { int32_t d[5]; } cspan_idx5;
#define cspan_make(SpanType, ...) \
{.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}}
+#define cspan_subslice(OutSpan, parent, ...) \
+ OutSpan##_subslice_((parent)->data, (parent)->dim, (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}}
@@ -170,10 +184,10 @@ typedef struct { int32_t d[5]; } cspan_idx5;
// e.g.: cspan_slice(&ms3, {1,3}, {0,-1}, {1,4});
#define cspan_slice(self, ...) \
- ((void)((self)->data += _cspan_slice(cspan_rank(self), (self)->dim, (self)->stride.d, \
- (const int32_t[][2]){__VA_ARGS__}) + \
+ ((void)((self)->data += _cspan_slice((self)->dim, (self)->stride.d, \
+ cspan_rank(self), (const int32_t[][2]){__VA_ARGS__}) + \
c_static_assert(cspan_rank(self) == \
- sizeof((const int32_t[][2]){__VA_ARGS__})/8)))
+ sizeof((const int32_t[][2]){__VA_ARGS__})/sizeof(int32_t[2]))))
// FUNCTIONS
@@ -233,7 +247,7 @@ STC_INLINE intptr_t _cspan_next_2(int rank, int32_t pos[], const int32_t dim[],
return off;
}
-STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[], const int32_t a[][2]) {
+STC_INLINE intptr_t _cspan_slice(int32_t dim[], const int32_t stri[], const int rank, const int32_t a[][2]) {
intptr_t off = 0;
bool ok = true;
for (int i = 0; i < rank; ++i) {
@@ -252,7 +266,7 @@ STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[],
return off;
}
-STC_INLINE intptr_t _cspan_subslice(int* orank, int32_t odim[], int32_t ostri[],
+STC_INLINE intptr_t _cspan_subslice(int32_t odim[], int32_t ostri[], int* orank,
const int32_t dim[], const int32_t stri[],
int rank, const int32_t a[][2]) {
intptr_t off = a[0][0];
@@ -270,10 +284,10 @@ STC_INLINE intptr_t _cspan_subslice(int* orank, int32_t odim[], int32_t ostri[],
odim[j] = t - a[i][0];
ostri[j] = s*stri[i];
s = 1; ++j;
- ok &= c_LTu(0, odim[0]);
+ ok &= c_LTu(0, odim[i]) & !c_LTu(dim[i], t);
}
*orank = j;
- c_ASSERT(ok);
+ c_ASSERT(ok); /* input indices ok */
return off;
}
#endif
diff --git a/include/stc/forward.h b/include/stc/forward.h
index 6103ba39..d08b71b4 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -120,33 +120,6 @@ typedef union {
SELF##_sizet size, bucket_count; \
} SELF
-#if defined STC_CSMAP_V1
-#define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \
- typedef KEY SELF##_key; \
- typedef VAL SELF##_mapped; \
- typedef SZ SELF##_sizet; \
- typedef struct SELF##_node SELF##_node; \
-\
- typedef SET_ONLY( SELF##_key ) \
- MAP_ONLY( struct SELF##_value ) \
- SELF##_value; \
-\
- typedef struct { \
- SELF##_value *ref; \
- bool inserted, nomem_error; \
- } SELF##_result; \
-\
- typedef struct { \
- SELF##_value *ref; \
- int _top; \
- SELF##_node *_tn, *_st[36]; \
- } SELF##_iter; \
-\
- typedef struct { \
- SELF##_node *root; \
- SELF##_sizet size; \
- } SELF
-#else
#define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \
typedef KEY SELF##_key; \
typedef VAL SELF##_mapped; \
@@ -173,7 +146,6 @@ typedef union {
SELF##_node *nodes; \
SELF##_sizet root, disp, head, size, cap; \
} SELF
-#endif
#define _c_cstack_fixed(SELF, VAL, CAP) \
typedef VAL SELF##_value; \
diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c
index d0ddf839..49b24172 100644
--- a/misc/examples/multidim.c
+++ b/misc/examples/multidim.c
@@ -5,6 +5,7 @@
#include <stdio.h>
using_cspan3(ispan, int);
+#define c_ALL 0,-1
int main()
{
@@ -30,7 +31,8 @@ int main()
}
puts("ss3 = ms3[:, 1:3, 1:3]");
ispan3 ss3 = ms3;
- cspan_slice(&ss3, {0,-1}, {1,3}, {1,3});
+ //cspan_slice(&ss3, {c_ALL}, {1,3}, {1,3});
+ ss3 = cspan_subslice(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++) {
diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c
index 43422f1c..cd6a1271 100644
--- a/misc/tests/cspan_test.c
+++ b/misc/tests/cspan_test.c
@@ -32,7 +32,7 @@ CTEST(cspan, slice) {
}
intspan2 m2 = m1;
- cspan_slice(&m2, {0,-1}, {2,4});
+ cspan_slice(&m2, {c_ALL}, {2,4});
size_t sum2 = 0;
for (size_t i = 0; i < m2.dim[0]; ++i) {