diff --git a/Makefile b/Makefile index 78146f1..ac788c9 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ INCLUDE=include INCLUDE_INTERNAL=src/internal COMMON_FLAGS= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing "-D_VERSION=$(VERSION)" $(addprefix -I,$(INCLUDE)) $(addprefix -I,$(INCLUDE_INTERNAL)) +COMMON_FLAGS+=-D_RNGXX_IMPL TARGET_CPU?=native OPT_FLAGS?= $(addprefix -march=,$(TARGET_CPU)) -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ diff --git a/include/rngxx.h b/include/rngxx.h index 478fb2d..df02715 100644 --- a/include/rngxx.h +++ b/include/rngxx.h @@ -6,7 +6,13 @@ #include +#ifndef _RNGXX_IMPL +#define _RNGXX_COMMON_MINIMAL +#endif #include "rngxx/common.h" +#ifndef _RNGXX_IMPL +#undef _RNGXX_COMMON_MINIMAL +#endif #define CTOR_COPY(name) name(const name& copy) #define CTOR_MOVE(name) name(name&& move) @@ -178,13 +184,9 @@ protected: // It is recommended to override `next_bytes()` too however. virtual f64 _sample() = 0; - inline f64 sample() - { - auto s = _sample(); - if (UNLIKELY(s < 0 || s > 1)) throw InvalidRandomSample{ s }; - return s; - } -private: + f64 sample(); + + private: template inline void _next_bytes(u8* a) { diff --git a/include/rngxx/common.h b/include/rngxx/common.h index cc49478..076db3c 100644 --- a/include/rngxx/common.h +++ b/include/rngxx/common.h @@ -7,6 +7,8 @@ extern "C" { #include #include + +#ifndef _RNGXX_COMMON_MINIMAL #include #define IFUNC_NAME(name, ver) _impl__ ## name ## __ ## ver @@ -32,6 +34,8 @@ extern "C" { #define _rng_internal v_internal +#endif + #define DEF(s, n) typedef s ## int ## n ## _t s ## n #define DEFINT(n) typedef uint ## n ## _t u ## n; \ typedef int ## n ## _t i ## n @@ -41,6 +45,7 @@ DEFINT(16); DEFINT(32); DEFINT(64); + #ifdef __cplusplus #else #include @@ -65,7 +70,9 @@ typedef uintptr_t ptr_t; #undef DEF #ifdef __cplusplus +#ifndef _RNGXX_COMMON_MINIMAL #define restrict __restrict__ +#endif } #endif diff --git a/src/rng.cpp b/src/rng.cpp index 333ede1..80b9d4d 100644 --- a/src/rng.cpp +++ b/src/rng.cpp @@ -30,6 +30,15 @@ N_INTS #undef NEXTT #undef NEXT +f64 Random::sample() +{ + auto s = _sample(); + if (UNLIKELY(s < 0 || s > 1)) throw InvalidRandomSample{ s }; + return s; +} + + + // Inefficient sample() based impl of `bytes()` void Random::next_bytes(u8* ptr, usize n) {