From b3d9e68fa2c2ec9ebe7e3f08045d01db5b720eea Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 30 Jan 2021 23:05:36 +0100 Subject: Internal changes: removed use of cmap_inits and set_inits. Use cmap_x_init() and cset_X_init(). Minor changes in cvec, csmap, cstr, crandom. --- stc/cmap.h | 11 +++++------ stc/crandom.h | 12 ++++++++---- stc/csmap.h | 16 +++++++++++----- stc/cstr.h | 2 +- stc/cvec.h | 17 ++++++++--------- 5 files changed, 33 insertions(+), 25 deletions(-) (limited to 'stc') diff --git a/stc/cmap.h b/stc/cmap.h index a36cd70a..79277405 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -31,14 +31,14 @@ using_cset(sx, int); // Set of int using_cmap(mx, int, char); // Map of int -> char int main(void) { - cset_sx s = cset_inits; + cset_sx s = cset_sx_init(); cset_sx_insert(&s, 5); cset_sx_insert(&s, 8); c_foreach (i, cset_sx, s) printf("set %d\n", i.ref->second); cset_sx_del(&s); - cmap_mx m = cmap_inits; + cmap_mx m = cmap_mx_init(); cmap_mx_put(&m, 5, 'a'); cmap_mx_put(&m, 8, 'b'); cmap_mx_put(&m, 12, 'c'); @@ -55,8 +55,7 @@ int main(void) { #include #include -#define cmap_inits {NULL, NULL, 0, 0, 0.15f, 0.85f} -#define cset_inits cmap_inits +#define _cmap_inits {NULL, NULL, 0, 0, 0.15f, 0.85f} /* https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction */ #define chash_reduce(x, N) ((uint32_t) (((uint64_t) (x) * (N)) >> 32)) @@ -185,7 +184,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; } C##_##X##_iter_t; \ \ STC_INLINE C##_##X \ - C##_##X##_init(void) {C##_##X m = cmap_inits; return m;} \ + C##_##X##_init(void) {C##_##X m = _cmap_inits; return m;} \ STC_INLINE bool \ C##_##X##_empty(C##_##X m) {return m.size == 0;} \ STC_INLINE size_t \ @@ -318,7 +317,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \ STC_DEF C##_##X \ C##_##X##_with_capacity(size_t cap) { \ - C##_##X h = C##_inits; \ + C##_##X h = _cmap_inits; \ C##_##X##_reserve(&h, cap); \ return h; \ } \ diff --git a/stc/crandom.h b/stc/crandom.h index c92a7de6..b43fdd46 100644 --- a/stc/crandom.h +++ b/stc/crandom.h @@ -84,14 +84,14 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) { } #if defined(__SIZEOF_INT128__) - #define cmul128(a, b, lo, hi) \ + #define cumul128(a, b, lo, hi) \ do { __uint128_t _z = (__uint128_t)(a) * (b); \ *(lo) = (uint64_t)_z, *(hi) = _z >> 64; } while(0) #elif defined(_MSC_VER) && defined(_WIN64) #include - #define cmul128(a, b, lo, hi) (*(lo) = _umul128(a, b, hi), (void)0) + #define cumul128(a, b, lo, hi) (*(lo) = _umul128(a, b, hi), (void)0) #elif defined(__x86_64__) - #define cmul128(a, b, lo, hi) \ + #define cumul128(a, b, lo, hi) \ asm("mulq %[rhs]" : "=a" (*(lo)), "=d" (*(hi)) \ : [lhs] "0" (a), [rhs] "rm" (b)) #endif @@ -99,7 +99,11 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) { /* Unbiased bounded uniform distribution. */ STC_INLINE int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) { uint64_t lo, hi; - do { cmul128(stc64_rand(rng), d->range, &lo, &hi); } while (lo < d->threshold); +#ifdef cumul128 + do { cumul128(stc64_rand(rng), d->range, &lo, &hi); } while (lo < d->threshold); +#else + hi = stc64_rand(rng) % d->range; +#endif return d->lower + hi; } diff --git a/stc/csmap.h b/stc/csmap.h index 97183abf..2e45f1a4 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -167,8 +167,8 @@ int main(void) { bool second; \ } C##_##X##_result_t; \ \ - STC_INLINE C##_##X \ - C##_##X##_init(void) {C##_##X m = {(C##_##X##_node_t *) &cbst_nil, 0}; return m;} \ + STC_API C##_##X \ + C##_##X##_init(void); \ STC_INLINE bool \ C##_##X##_empty(C##_##X m) {return m.size == 0;} \ STC_INLINE size_t \ @@ -305,14 +305,16 @@ int main(void) { keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \ typedef C##_##X C##_##X##_t -_using_CBST_types(_, csmap, int, int); -static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0}; - /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) #define _implement_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \ + STC_DEF C##_##X \ + C##_##X##_init(void) { \ + C##_##X m = {(C##_##X##_node_t *) &cbst_nil, 0}; \ + return m; \ + } \ \ STC_DEF C##_##X##_value_t* \ C##_##X##_find_it(const C##_##X* self, C##_##X##_rawkey_t rkey, C##_##X##_iter_t* out) { \ @@ -447,6 +449,10 @@ static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0}; } \ } + +_using_CBST_types(_, csmap, int, int); +static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0}; + #else #define _implement_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) diff --git a/stc/cstr.h b/stc/cstr.h index 9c3d4f1c..8c29c9b3 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -260,7 +260,7 @@ uint32_t cstr_hash_raw(const char* const* p, size_t none) { STC_DEF size_t cstr_reserve(cstr_t* self, size_t cap) { - size_t len = cstr_size(*self), oldcap = cstr_capacity(*self); + size_t oldcap = cstr_capacity(*self); if (cap > oldcap) { size_t* rep = (size_t *) c_realloc(oldcap ? _cstr_rep(self) : NULL, _cstr_mem(cap)); self->str = (char *) &rep[2]; diff --git a/stc/cvec.h b/stc/cvec.h index e8f24ac7..d17876f1 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -216,18 +216,17 @@ STC_DEF void \ cvec_##X##_del(cvec_##X* self) { \ cvec_##X##_clear(self); \ - if (_cvec_alloced(self) != _cvec_inits) \ - c_free(_cvec_alloced(self)); \ + if (_cvec_rep(self) != _cvec_inits) \ + c_free(_cvec_rep(self)); \ } \ \ STC_DEF void \ cvec_##X##_reserve(cvec_##X* self, size_t cap) { \ - size_t* rep; \ - if (cap > cvec_##X##_capacity(*self)) { \ - size_t len = _cvec_size(self); \ - rep = (size_t *) c_realloc(_cvec_alloced(self) != _cvec_inits ? _cvec_alloced(self) : NULL, \ - 2*sizeof(size_t) + cap*sizeof(Value)); \ - self->data = (Value *) (rep + 2); \ + size_t len = _cvec_size(self); \ + if (cap > _cvec_cap(self)) { \ + size_t* rep = (size_t *) c_realloc(_cvec_rep(self) != _cvec_inits ? _cvec_rep(self) : NULL, \ + 2*sizeof(size_t) + cap*sizeof(Value)); \ + self->data = (cvec_##X##_value_t*) (rep + 2); \ rep[0] = len; \ rep[1] = cap; \ } \ @@ -305,7 +304,7 @@ } static size_t _cvec_inits[2] = {0, 0}; -#define _cvec_alloced(self) (((size_t *) (self)->data) - 2) +#define _cvec_rep(self) (((size_t *) (self)->data) - 2) #else #define _c_implement_cvec_7(X, Value, valueCompareRaw, valueDestroy, valueFromRaw, valueToRaw, RawValue) -- cgit v1.2.3