diff options
| author | Tyge Løvset <[email protected]> | 2023-05-24 19:47:16 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-05-24 19:47:16 +0200 |
| commit | c3e01470f45f19215fac339302d87b5d73a323e8 (patch) | |
| tree | a573d484fafc4bee8503c4af7eee1b61ab098010 | |
| parent | 276b8110033aa275f58ce60d096f220ca050738c (diff) | |
| download | STC-modified-c3e01470f45f19215fac339302d87b5d73a323e8.tar.gz STC-modified-c3e01470f45f19215fac339302d87b5d73a323e8.zip | |
More coro adjustments.
| -rw-r--r-- | include/stc/algo/coroutine.h | 17 | ||||
| -rw-r--r-- | include/stc/csview.h | 31 | ||||
| -rwxr-xr-x | misc/examples/make.sh | 17 | ||||
| -rw-r--r-- | misc/examples/prime.c | 4 |
4 files changed, 36 insertions, 33 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index ebfed613..fdce2629 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -141,12 +141,17 @@ typedef struct { */ #include <time.h> -#include <sys/time.h> - -static inline void csleep_us(int64_t usec) { - struct timeval tv = {.tv_sec=(int)(usec/1000000), .tv_usec=usec % 1000000}; - select(0, NULL, NULL, NULL, &tv); -} +#if defined _WIN32 && !defined __GNUC__ + static inline void csleep_ms(long msec) { + extern void Sleep(unsigned long); + Sleep((unsigned long)msec); + } +#else + static inline void csleep_ms(long msec) { + struct timespec ts = {msec/1000, 1000000*(msec % 1000)}; + nanosleep(&ts, NULL); + } +#endif typedef struct { clock_t start; diff --git a/include/stc/csview.h b/include/stc/csview.h index 571bb278..e8a3ad9b 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -33,7 +33,12 @@ #define csview_lit(literal) c_sv_1(literal) #define csview_from_n(str, n) c_sv_2(str, n) -STC_API intptr_t csview_find_sv(csview sv, csview search); +STC_API csview_iter csview_advance(csview_iter it, intptr_t pos); +STC_API intptr_t csview_find_sv(csview sv, csview search); +STC_API uint64_t csview_hash(const csview *self); +STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); +STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); +STC_API csview csview_token(csview sv, const char* sep, intptr_t* start); STC_INLINE csview csview_from(const char* str) { return c_LITERAL(csview){str, c_strlen(str)}; } @@ -87,15 +92,6 @@ STC_INLINE void csview_next(csview_iter* it) { it->u8.chr.size = utf8_chr_size(it->ref); if (it->ref == it->u8.end) it->ref = NULL; } -STC_INLINE csview_iter csview_advance(csview_iter it, intptr_t pos) { - int inc = -1; - if (pos > 0) pos = -pos, inc = 1; - while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80; - it.u8.chr.size = utf8_chr_size(it.ref); - if (it.ref == it.u8.end) it.ref = NULL; - return it; -} - /* utf8 */ STC_INLINE intptr_t csview_u8_size(csview sv) @@ -110,10 +106,6 @@ STC_INLINE csview csview_u8_substr(csview sv, intptr_t bytepos, intptr_t u8len) STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } -STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); -STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); -STC_API csview csview_token(csview sv, const char* sep, intptr_t* start); - #define c_fortoken_sv(it, inputsv, sep) \ for (struct { csview _inp, token, *ref; const char *_sep; intptr_t pos; } \ it = {._inp=inputsv, .token=it._inp, .ref=&it.token, ._sep=sep} \ @@ -155,11 +147,18 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) STC_INLINE bool csview_eq(const csview* x, const csview* y) { return x->size == y->size && !c_memcmp(x->str, y->str, x->size); } -STC_API uint64_t csview_hash(const csview *self); - /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) +STC_DEF csview_iter csview_advance(csview_iter it, intptr_t pos) { + int inc = -1; + if (pos > 0) pos = -pos, inc = 1; + while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80; + it.u8.chr.size = utf8_chr_size(it.ref); + if (it.ref == it.u8.end) it.ref = NULL; + return it; +} + STC_DEF intptr_t csview_find_sv(csview sv, csview search) { char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); return res ? (res - sv.str) : c_NPOS; diff --git a/misc/examples/make.sh b/misc/examples/make.sh index 5c81c4d3..d58ed0cb 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -6,15 +6,14 @@ if [ "$(uname)" = 'Linux' ]; then oflag='-o ' fi -cc=gcc; cflags="-DSTC_STATIC -s -O3 -std=c99 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-maybe-uninitialized -Wno-missing-field-initializers" -#cc=gcc; cflags="-DSTC_STATIC -g -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" -#cc=tcc; cflags="-DSTC_STATIC -Wall -std=c99" -#cc=clang; cflags="-DSTC_STATIC -s -O3 -std=c99 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers" -#cc=gcc; cflags="-DSTC_STATIC -x c++ -s -O2 -Wall -std=c++20" -#cc=g++; cflags="-DSTC_STATIC -x c++ -s -O2 -Wall" -#cc=cl; cflags="-DSTC_STATIC -O2 -nologo -W3 -MD" -#cc=cl; cflags="-DSTC_STATIC -nologo -TP" -#cc=cl; cflags="-DSTC_STATIC -nologo -std:c11" +cc=gcc; cflags="-DSTC_STATIC -std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-implicit-fallthrough -Wno-missing-field-initializers" +#cc=gcc; cflags="-DSTC_STATIC -std=c99 -g -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" +#cc=tcc; cflags="-DSTC_STATIC -std=c99 -Wall" +#cc=clang; cflags="-DSTC_STATIC -std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers" +#cc=gcc; cflags="-DSTC_STATIC -x c++ -std=c++20 -O2 -s -Wall" +#cc=cl; cflags="-DSTC_STATIC -nologo -O2 -MD -W3 -wd4003" +#cc=cl; cflags="-DSTC_STATIC -nologo -TP -wd4003" +#cc=cl; cflags="-DSTC_STATIC -nologo -std:c11 -wd4003" if [ "$cc" = "cl" ]; then oflag='/Fe:' diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 7efa26ff..cb0f8926 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -32,9 +32,8 @@ int main(void) clock_t t = clock(); cbits primes = sieveOfEratosthenes(n + 1); int np = (int)cbits_count(&primes); - t = t - clock(); + t = clock() - t; - printf("Number of primes: %d, time: %f\n\n", np, (double)t/CLOCKS_PER_SEC); puts("Show all the primes in the range [2, 1000):"); printf("2"); c_forrange (i, 3, 1000, 2) @@ -49,6 +48,7 @@ int main(void) printf("%lld ", *i.ref); if (c_flt_getcount(i) % 10 == 0) puts(""); } + printf("Number of primes: %d, time: %.2f\n\n", np, (double)t/CLOCKS_PER_SEC); cbits_drop(&primes); } |
