C API: Start `next` shim

Fortune for rngxx's current commit: Small blessing − 小吉
capi
Avril 3 years ago
parent fc70397536
commit ed1f1d5f03
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -43,6 +43,26 @@ enum rng_next_type {
RNG_TY_F64, RNG_TY_F64,
}; // 4 bits }; // 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 #ifndef _RNGXX_IMPL_ONLY_TYPES
// -- C API functions -- (C++ NO compat) // -- C API functions -- (C++ NO compat)

@ -5,6 +5,25 @@
#include "capi-bridge.h" #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" { extern "C" {
// Internal bridge members // Internal bridge members
_export(internal) _export(internal)
@ -17,7 +36,22 @@ extern "C" {
default: return NULL; 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<unsigned char*>(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<unsigned char*>(output), opt->bound.len); break;
//TODO: Rest of types
default: return 0;
}
return 1;
}
// Direct C interface members // Direct C interface members
void rng_free(rng_t* rng) void rng_free(rng_t* rng)

@ -17,6 +17,7 @@ extern "C" {
#endif #endif
rng_t* RNG_IMPL(mkdriver)(enum rng_kind kind, u64* restrict seed) _export(internal); 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 #ifdef __cplusplus
} }

@ -19,4 +19,7 @@ rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1])
} }
// void rng_free() - direct bridge // 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);
}

Loading…
Cancel
Save