diff options
| author | Tyge Løvset <[email protected]> | 2020-07-17 10:55:16 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-17 10:55:16 +0200 |
| commit | 7cda97c127076acbbaa7b472cf5a89357c1d9a84 (patch) | |
| tree | bbb33fad91ae03bd9cfca86095c675105eceff56 | |
| parent | 2d04857e42a08d7d5428c78463b668f15d4513f5 (diff) | |
| download | STC-modified-7cda97c127076acbbaa7b472cf5a89357c1d9a84.tar.gz STC-modified-7cda97c127076acbbaa7b472cf5a89357c1d9a84.zip | |
Added macro overload allowing zero args (not utilized). Renamed ..._floatRandom() to ..._randomFloat()
| -rw-r--r-- | examples/rngtest.c | 76 | ||||
| -rw-r--r-- | stc/cdefs.h | 18 | ||||
| -rw-r--r-- | stc/chash.h | 12 | ||||
| -rw-r--r-- | stc/crandom.h | 6 |
4 files changed, 57 insertions, 55 deletions
diff --git a/examples/rngtest.c b/examples/rngtest.c index d88d1fab..ed3bed08 100644 --- a/examples/rngtest.c +++ b/examples/rngtest.c @@ -1,39 +1,39 @@ -#include <stdio.h> -#include <time.h> -#include "../stc/crandom.h" -#ifdef __cplusplus -#include <random> -#endif - - -#define NN 1000000000 - -int main(void) -{ - clock_t difference, before; - uint64_t v; - printf("start\n"); - - pcg32_random_t pcg = pcg32_seed(time(NULL), 1); - before = clock(); \ - v = 0; - for (size_t i=0; i<NN; i++) { - v += pcg32_random(&pcg) & 0xffffffff; - } - difference = clock() - before; - printf("pcg32: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v); - - sfc64_random_t sfc = sfc64_seed(time(NULL)); - before = clock(); \ - v = 0; - for (size_t i=0; i<NN; i++) { - v += sfc64_random(&sfc) & 0xffffffff; - } - difference = clock() - before; - printf("sfc64: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v); - - for (int i=0; i<8; ++i) printf("%f ", sfc64_fRandom(&sfc)); - puts(""); - for (int i=0; i<8; ++i) printf("%f ", pcg32_fRandom(&pcg)); - puts(""); +#include <stdio.h>
+#include <time.h>
+#include "../stc/crandom.h"
+#ifdef __cplusplus
+#include <random>
+#endif
+
+
+#define NN 1000000000
+
+int main(void)
+{
+ clock_t difference, before;
+ uint64_t v;
+ printf("start\n");
+
+ pcg32_random_t pcg = pcg32_seed(time(NULL), 1);
+ before = clock(); \
+ v = 0;
+ for (size_t i=0; i<NN; i++) {
+ v += pcg32_random(&pcg) & 0xffffffff;
+ }
+ difference = clock() - before;
+ printf("pcg32: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
+
+ sfc64_random_t sfc = sfc64_seed(time(NULL));
+ before = clock(); \
+ v = 0;
+ for (size_t i=0; i<NN; i++) {
+ v += sfc64_random(&sfc) & 0xffffffff;
+ }
+ difference = clock() - before;
+ printf("sfc64: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
+
+ for (int i=0; i<8; ++i) printf("%f ", sfc64_randomFloat(&sfc));
+ puts("");
+ for (int i=0; i<8; ++i) printf("%f ", pcg32_randomFloat(&pcg));
+ puts("");
}
\ No newline at end of file diff --git a/stc/cdefs.h b/stc/cdefs.h index 7f141a77..0b4d7550 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -43,16 +43,18 @@ #endif
/* Macro overloading feature support: https://rextester.com/ONP80107 */
-#define c_CAT( A, B ) A ## B
+#define c_CAT(A, B) A ## B
#define c_EXPAND(...) __VA_ARGS__
-#define c_VA_ARG_SIZE(...) c_EXPAND(c_APPLY_ARG_N((__VA_ARGS__, c_RSEQ_N)))
-#define c_APPLY_ARG_N(ARGS) c_EXPAND(c_ARG_N ARGS)
-#define c_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N,...) N
-#define c_RSEQ_N 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
-#define c_OVERLOAD_SELECT(NAME, NUM) c_CAT( NAME ## _, NUM)
+#define _c_RSEQ_N 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
+#define _c_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N,...) N
+#define _c_ZERO_ARGS_PREFIX__ZERO_ARGS_SUFFIX ,,,,,,,,,,,,,0 /* 13 commas */
+#define _c_ZERO_ARGS(...) c_EXPAND(_c_ZERO_ARGS_PREFIX_ ## __VA_ARGS__ ## _ZERO_ARGS_SUFFIX)
+#define _c_VA_ARG_SIZE(...) c_EXPAND(_c_APPLY_ARG_N((_c_ZERO_ARGS(__VA_ARGS__), _c_RSEQ_N)))
+#define _c_APPLY_ARG_N(ARGS) c_EXPAND(_c_ARG_N ARGS)
+#define _c_OVERLOAD_SELECT(NAME, NUM) c_CAT(NAME ## _, NUM)
-#define c_MACRO_OVERLOAD(NAME, ...) c_OVERLOAD_SELECT(NAME, c_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__)
-//#define test(...) c_MACRO_OVERLOAD(test, __VA_ARGS__) //#define test_1(x) a(x) //#define test_2(x,y) b(x,y)
+#define c_MACRO_OVERLOAD(NAME, ...) _c_OVERLOAD_SELECT(NAME, _c_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__)
+/*#define FOO(...) c_MACRO_OVERLOAD(FOO, __VA_ARGS__) #define FOO_0() "0" #define FOO_1(x) "1" #define FOO_2(x,y) "2"*/
#define c_new(T) ((T *) malloc(sizeof(T)))
#define c_new_N(T, n) ((T *) malloc(sizeof(T) * (n)))
diff --git a/stc/chash.h b/stc/chash.h index 050907d3..dc18d77d 100644 --- a/stc/chash.h +++ b/stc/chash.h @@ -23,7 +23,7 @@ /* // Example:
#include <stdio.h>
-#include <stc/chash.h>
+#include <stc/cmap.h>
declare_CSet(sx, int); // Set of int
declare_CMap(mx, int, char); // Map of int -> char
@@ -101,16 +101,16 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80}; #define declare_CSet_5(tag, Key, keyDestroy, keyHash, keyEquals) \
declare_CSet_8(tag, Key, keyDestroy, keyHash, keyEquals, \
- Key, c_defaultGetRaw, c_defaultInitRaw)
+ Key, c_defaultGetRaw, c_defaultInitRaw)
#define declare_CSet_8(tag, Key, keyDestroy, keyHashRaw, keyEqualsRaw, \
RawKey, keyGetRaw, keyInitRaw) \
- declare_CHASH(tag, CSet, cset, Key, void, void, keyHashRaw, keyEqualsRaw, \
+ declare_CHASH(tag, CSet, cset, Key, void, void, keyHashRaw, keyEqualsRaw, \
keyDestroy, RawKey, keyGetRaw, keyInitRaw)
/* CSet_str, CMap_str: */
-#define declare_CSet_str(tag) \
- declare_CMAPSTR(tag, CSet, cset, void, void)
+#define declare_CSet_str() \
+ declare_CMAPSTR(str, CSet, cset, void, void)
#define declare_CMap_str(...) \
c_MACRO_OVERLOAD(declare_CMap_str, __VA_ARGS__)
@@ -123,7 +123,7 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80}; #define declare_CMAPSTR(tag, CType, ctype, Value, valueDestroy) \
declare_CHASH(tag, CType, ctype, CStr, Value, valueDestroy, cstr_hashRaw, \
- cstr_equalsRaw, cstr_destroy, const char*, cstr_getRaw, cstr_make)
+ cstr_equalsRaw, cstr_destroy, const char*, cstr_getRaw, cstr_make)
#define OPT_1_cset(x)
#define OPT_2_cset(x, y) x
diff --git a/stc/crandom.h b/stc/crandom.h index 06f1c26a..ff2e03d3 100644 --- a/stc/crandom.h +++ b/stc/crandom.h @@ -17,13 +17,13 @@ STC_INLINE uint32_t pcg32_random(pcg32_random_t* rng) }
/* float random int number in range [0, 1). NB: 23 bit resolution. */
-STC_INLINE float pcg32_floatRandom(pcg32_random_t* rng) {
+STC_INLINE float pcg32_randomFloat(pcg32_random_t* rng) {
union {uint32_t i; float f;} u = {0x3F800000u | (pcg32_random(rng) >> 9)};
return u.f - 1.0f;
}
/* Uniform random number in range [0, bound) */
-STC_INLINE uint32_t pcg32_boundedRandom(pcg32_random_t* rng, uint32_t bound) {
+STC_INLINE uint32_t pcg32_randomBounded(pcg32_random_t* rng, uint32_t bound) {
return (uint32_t) (((uint64_t) pcg32_random(rng) * bound) >> 32);
}
@@ -59,7 +59,7 @@ STC_API uint64_t sfc64_random(sfc64_random_t* rng) { }
/* double random int number in range [0, 1). */
-STC_INLINE double sfc64_fRandom(sfc64_random_t* rng) {
+STC_INLINE double sfc64_randomFloat(sfc64_random_t* rng) {
union {uint64_t i; double f;} u = {0x3FF0000000000000ull | (sfc64_random(rng) >> 12)};
return u.f - 1.0;
}
|
