C API: Type spec and flags (start)

Use bitfields for ty + tyflag in struct for `rng_next()` options.

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

@ -24,10 +24,30 @@ enum rng_kind {
RNG_KIND_CRAND,
};
enum rng_next_flag {
RNG_TYQ_SIGNED = 0, // Binary with `unsigned`.
RNG_TYQ_UNSIGNED = 1 << 0,
RNG_TYQ_CHAOS = 1 << 1,
}; // 2 bits
enum rng_next_type {
RNG_TY_BLOB = 0,
RNG_TY_INT8,
RNG_TY_INT16,
RNG_TY_INT32,
RNG_TY_INT64,
RNG_TY_F32,
RNG_TY_F64,
}; // 4 bits
// -- // --
#ifndef _RNGXX_IMPL_ONLY_TYPES
// -- C API functions -- (C++ NO compat)
rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1]);
void rng_free(rng_t* rng);
// -- // --
#endif

@ -71,7 +71,7 @@ struct Random
inline virtual ~iterator(){}
protected:
virtual T _sample() { if (rng) return rng->next<T>(); else throw ObjectMoved(); }
virtual T _sample() { if (LIKELY(rng)) return rng->next<T>(); else throw ObjectMoved(); }
virtual inline void _init() {}
private:
inline explicit iterator(Random& rng) : rng(&rng){ _init(); }

@ -6,6 +6,7 @@
#include "capi-bridge.h"
extern "C" {
// Internal bridge members
_export(internal)
rng_t* RNG_IMPL(mkdriver)(rng_kind kind, u64* restrict seed)
{
@ -16,4 +17,11 @@ extern "C" {
default: return NULL;
}
}
// Direct C interface members
void rng_free(rng_t* rng)
{
delete rng;
}
}

@ -1,11 +1,22 @@
// Contains the library exported C API functions.
// Actual work may be done in `capi-bridge.cpp`, `capi-bridge.h` bridges this shim TU with that working one
#include <stdio.h>
#include <stdlib.h>
#include <common.h>
#include <rngxx.h>
#include "capi-bridge.h"
#define assert_not_null(expr, ...) ({ __auto_type _nn__expr = (expr); \
if(UNLIKELY(_nn__expr == NULL)) { fprintf(stderr, "fatal (unexpected null pointer): " __VA_ARGS__); abort(); } \
_nn__expr; })
rng_t* rng_new(enum rng_kind kind, u64 seed[static restrict 1])
{
return RNG_IMPL(mkdriver) (kind, &seed[0]);
return assert_not_null(RNG_IMPL(mkdriver) (kind, &seed[0]), "invalid kind %d", (int)kind);
}
// void rng_free() - direct bridge

Loading…
Cancel
Save