From ed1f1d5f03356dec8091d4b53fe3f771d38e5bae Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 4 Nov 2021 14:23:50 +0000 Subject: [PATCH] C API: Start `next` shim MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for rngxx's current commit: Small blessing − 小吉 --- include/rngxx.h | 20 ++++++++++++++++++++ src/capi-bridge.cpp | 34 ++++++++++++++++++++++++++++++++++ src/capi-bridge.h | 1 + src/capi.c | 5 ++++- 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/include/rngxx.h b/include/rngxx.h index 0933d8a..f3ee4dd 100644 --- a/include/rngxx.h +++ b/include/rngxx.h @@ -43,6 +43,26 @@ enum rng_next_type { RNG_TY_F64, }; // 4 bits +struct rng_next_opt { + union { + // Bounded types + struct { + const void* restrict pmin; + const void* restrict pmax; + } range; + // Blob types + size_t len; + } bound; + + struct { + enum rng_next_type type : 4; + enum rng_next_flag mod : 2; + uint8_t _unused : 2; + } ty; // 8 bits +}; + +typedef __typeof( ((struct rng_next_opt*)NULL)->ty ) rng_next_tyq; // Qualified type + // -- // -- #ifndef _RNGXX_IMPL_ONLY_TYPES // -- C API functions -- (C++ NO compat) diff --git a/src/capi-bridge.cpp b/src/capi-bridge.cpp index 1484ead..523a97d 100644 --- a/src/capi-bridge.cpp +++ b/src/capi-bridge.cpp @@ -5,6 +5,25 @@ #include "capi-bridge.h" + +namespace bridge _export(internal) +{ + void nblob(Random& restrict engine, unsigned char* restrict output, size_t es, size_t l) + { + size_t len = es * l; // full length (bytes) + + } + inline void nblob(Random& restrict engine, unsigned char* restrict output, size_t len) { nblob(engine, output, 1, len); } + + constexpr inline size_t ty_sizeof(rng_next_type ty) + { + //TODO: + } + + constexpr inline bool ty_signed(rng_next_flag f) { return ! (f & RNG_TYQ_UNSIGNED); } + constexpr inline bool ty_signed(const rng_next_tyq& qt) { return ty_signed(qt.mod); } +} + extern "C" { // Internal bridge members _export(internal) @@ -17,7 +36,22 @@ extern "C" { default: return NULL; } } + _export(internal) + int RNG_IMPL(mnext)(rng_t* restrict engine, void* restrict output, const rng_next_opt* restrict opt) + { + if(opt->ty.mod & RNG_TYQ_CHAOS) { + bridge::nblob(*engine, reinterpret_cast(output), bridge::ty_sizeof(opt->ty.type), opt->bound.len); + return 1; + } + switch(opt->ty.type) + { + case RNG_TY_BLOB: bridge::nblob(*engine, reinterpret_cast(output), opt->bound.len); break; + //TODO: Rest of types + default: return 0; + } + return 1; + } // Direct C interface members void rng_free(rng_t* rng) diff --git a/src/capi-bridge.h b/src/capi-bridge.h index b2977b2..c4de1cc 100644 --- a/src/capi-bridge.h +++ b/src/capi-bridge.h @@ -17,6 +17,7 @@ extern "C" { #endif rng_t* RNG_IMPL(mkdriver)(enum rng_kind kind, u64* restrict seed) _export(internal); +int RNG_IMPL(mnext)(rng_t* restrict engine, void* restrict output, const struct rng_next_opt* restrict opt) _export(internal); #ifdef __cplusplus } diff --git a/src/capi.c b/src/capi.c index 5bc4b3b..2903697 100644 --- a/src/capi.c +++ b/src/capi.c @@ -19,4 +19,7 @@ rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1]) } // void rng_free() - direct bridge - +int rng_next(rng_t* restrict engine, void* restrict output, struct rng_next_opt opt) +{ + return RNG_IMPL(mnext)(engine, output, &opt); +}