diff --git a/Makefile b/Makefile index e6131c7..ce0f814 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROJECT=rngxx AUTHOR=Avril (Flanchan) VERSION_MAJOR=0 -VERSION_MINOR=1 +VERSION_MINOR=2 VERSION=$(VERSION_MAJOR).$(VERSION_MINOR) ifeq ($(PREFIX),) diff --git a/include/rngxx.h b/include/rngxx.h index 6b51739..33c0826 100644 --- a/include/rngxx.h +++ b/include/rngxx.h @@ -80,6 +80,46 @@ void* rng_next_tyb(rng_t* engine, void* restrict output, enum rng_next_type ty, rng_t* rng_new(enum rng_kind kind, const u64 seed[static restrict 1]); void rng_free(rng_t* rng); +#ifdef _RNGXX_INLINE_ONLY +#define _rng__always_inline __attribute__((gnu_inline)) inline extern +#else +#define _rng__always_inline __attribute__((gnu_inline)) inline static +#endif + +#define _NEXT_TYPED_(name, T, N, S) \ +_rng__always_inline T rng_next_ ## name (rng_t* engine, const T* min, const T* max) \ +{ \ + T output; \ + rng_next_tyb(engine, &output, RNG_TY_ ## N, RNG_TYQ_ ## S, min, max); \ + return output; \ +} + +#define _NEXT_TYPED(T, N, S) _NEXT_TYPED_(T, T, N, S) + +#define NEXT_TYPED_INT(n) _NEXT_TYPED(i ## n, INT ## n, SIGNED) \ + _NEXT_TYPED(u ## n, INT ## n, UNSIGNED) + +NEXT_TYPED_INT(8) +NEXT_TYPED_INT(16) +NEXT_TYPED_INT(32) +NEXT_TYPED_INT(64) + +_NEXT_TYPED(f32, F32, SIGNED) +_NEXT_TYPED(f64, F64, SIGNED) + +_rng__always_inline rng_bool_t rng_next_bool(rng_t* engine) +{ + rng_bool_t output; + rng_next_tyb(engine, &output, RNG_TY_BOOL, 0, NULL, NULL); + return output & 1; +} + +#undef NEXT_TYPED_INT +#undef _NEXT_TYPED +#undef _NEXT_TYPED_ + +#undef _rng__always_inline + // -- // -- #endif #ifdef __cplusplus diff --git a/include/rngxx.hpp b/include/rngxx.hpp index e4a852a..6c39f5e 100644 --- a/include/rngxx.hpp +++ b/include/rngxx.hpp @@ -228,8 +228,10 @@ DEF template<> inline T Random::next< T >(T min, T max) { return next_ ## T(min, max); } \ template<> inline T Random::next< T >(T max) { return next_ ## T(max); } template<> inline bool Random::next() { return next_bool(); } -template<> inline f64 Random::next() { return next_f64(); } -template<> inline f32 Random::next() { return next_f32(); } +//template<> inline f64 Random::next() { return next_f64(); } +//template<> inline f32 Random::next() { return next_f32(); } +DEFT(f32) +DEFT(f64) DEF #undef DEF #undef DEFTT diff --git a/src/capi-bridge.cpp b/src/capi-bridge.cpp index f0a30d7..1667cc2 100644 --- a/src/capi-bridge.cpp +++ b/src/capi-bridge.cpp @@ -15,10 +15,13 @@ namespace bridge _export(internal) template inline T next_typed(Random& engine, const void* minp = nullptr, const void* maxp = nullptr) { - if constexpr(!is_same_any()) { + if constexpr(!is_same_any()) { if(minp && maxp) return engine.next(*reinterpret_cast(minp), *reinterpret_cast(maxp)); else if(maxp) return engine.next(*reinterpret_cast(maxp)); - else if(minp) return engine.next(*reinterpret_cast(minp), engine.max_for()); + else { + if constexpr(!is_same_any()) + if(minp)return engine.next(*reinterpret_cast(minp), engine.max_for()); + } } return engine.next(); } @@ -115,10 +118,10 @@ extern "C" { INTTY(64); #undef INTTY case RNG_TY_F32: - bridge::next_typed_into(*engine, output); + bridge::next_typed_into(*engine, output, opt->bound.range.pmin, opt->bound.range.pmax); break; case RNG_TY_F64: - bridge::next_typed_into(*engine, output); + bridge::next_typed_into(*engine, output, opt->bound.range.pmin, opt->bound.range.pmax); break; case RNG_TY_BOOL: *reinterpret_cast(output) = bridge::next_typed(*engine, output) ? 1 : 0; diff --git a/src/test/main.c b/src/test/main.c index 4184cca..20aebe8 100644 --- a/src/test/main.c +++ b/src/test/main.c @@ -1,6 +1,9 @@ #include #include +#include + +#define _RNGXX_INLINE_ONLY #include static int next(rng_t* rng, const int* min, const int* max) @@ -15,8 +18,11 @@ static int next(rng_t* rng, const int* min, const int* max) #define TREF(x) ( (const __typeof(x)[]){ (x) } ) int main() { - rng_t* engine = rng_new(RNG_KIND_CRAND, (const u64[]){ 1000 }); + rng_t* engine = rng_new(RNG_KIND_CRAND, (const u64[]){ time(NULL) }); printf("%d %d %d\n", next(engine, NULL, TREF(100)), next(engine, TREF(10), TREF(20)), next(engine, NULL, NULL)); + printf("%ld\n", rng_next_i64(engine, NULL, NULL)); + printf("%lf\n", rng_next_f64(engine, TREF(1.0), TREF(100.0))); + printf("%d\n", rng_next_bool(engine)); rng_free(engine); return 0; }