summaryrefslogtreecommitdiff
path: root/modules/core
diff options
context:
space:
mode:
Diffstat (limited to 'modules/core')
-rw-r--r--modules/core/c++/abstract/encode.hpp19
-rw-r--r--modules/core/c++/abstract/forst.hpp615
-rw-r--r--modules/core/c++/chunk.hpp10
-rw-r--r--modules/core/c++/forst_chunk.hpp73
-rw-r--r--modules/core/c++/fplbm.hpp83
-rw-r--r--modules/core/c++/write_vtk.hpp3
-rw-r--r--modules/core/tests/forst.cpp361
7 files changed, 1110 insertions, 54 deletions
diff --git a/modules/core/c++/abstract/encode.hpp b/modules/core/c++/abstract/encode.hpp
index 571722a..1593656 100644
--- a/modules/core/c++/abstract/encode.hpp
+++ b/modules/core/c++/abstract/encode.hpp
@@ -25,8 +25,27 @@ struct NativeRaw {
struct Json {
static constexpr string_literal name = "enc::Json";
};
+
+/**
+ * A packed little endian binary layout. See abstract/forst.hpp.
+ *
+ * Every value sits at a computable byte offset, so reading one member costs no
+ * decoding of the members before it. Fixed size schemas are inline; variable
+ * length ones spend their slot on dimensions plus an offset and put their bytes
+ * in a single dynamic layer after the static section.
+ */
+struct Forst {
+ static constexpr string_literal name = "enc::Forst";
+};
} // namespace enc
+/**
+ * Declared here rather than in any one codec header so that an encoding does
+ * not have to include an unrelated one to specialise it. json.hpp and
+ * forst.hpp are siblings; neither should need the other.
+ */
+template <typename Schema, typename Encoding> class codec;
+
#ifndef KEL_LBM_DEFAULT_DATA_ENCODING
#define KEL_LBM_DEFAULT_DATA_ENCODING ::kel::lbm::enc::Native
#endif
diff --git a/modules/core/c++/abstract/forst.hpp b/modules/core/c++/abstract/forst.hpp
new file mode 100644
index 0000000..878ad58
--- /dev/null
+++ b/modules/core/c++/abstract/forst.hpp
@@ -0,0 +1,615 @@
+#pragma once
+
+#include "data.hpp"
+#include "encode.hpp"
+#include "schema.hpp"
+#include "templates.hpp"
+
+#include <cstddef>
+#include <string_view>
+#include <cstdint>
+#include <bit>
+#include <cstring>
+#include <string>
+#include <type_traits>
+#include <vector>
+
+/**
+ * A packed little endian binary encoding for the schema system.
+ *
+ * Every value sits at a computable byte offset, so reading one member costs no
+ * decoding of the members before it. A value is a static section followed by a
+ * single dynamic layer. Little endian, byte packed, no alignment and no
+ * padding. Fixed size members sit inline; a variable length member spends its
+ * static slot on its dimensions as UInt64 each and then an offset, and its
+ * bytes live in the dynamic layer.
+ *
+ * An offset is measured from the byte just past the offset field that holds
+ * it, so resolving one is a single add with no layer base to track.
+ *
+ * This is forstio's Forst format reimplemented against lbm's own schema types
+ * (lbm's abstract/ is a private reimplementation of forstio's data/codec, so
+ * the two schema vocabularies are distinct C++ types and nothing could be
+ * shared), and deliberately cut down to **one dynamic layer**. That makes
+ * `String` and `Array<Fixed>` expressible while `Array<String>` and
+ * `Array<Array<...>>` are not -- a static_assert says so rather than letting a
+ * nested dynamic value mis-encode.
+ *
+ * Chunk is not handled here: abstract/ sits below chunk.hpp and must not reach
+ * up into it. The Chunk layout lives in forst_chunk.hpp, which is what a result
+ * writer includes.
+ *
+ * One limitation shared with enc::Json: a bare `data<Chunk<...>, enc::Forst>`
+ * is ambiguous, because chunk.hpp already specialises
+ * `data<Chunk<...>, Encode>` for any encoding and neither specialisation is
+ * more specialised than the other. Wrap the chunk in a Struct, which is what a
+ * field always is in practice.
+ *
+ * Why it is worth having: a `Struct<Member<Chunk<...>,Key>...>` is entirely
+ * fixed size, so its encoding *is* the packed field data, byte for byte, with
+ * no framing overhead and no serialisation pass. Results are written as ASCII
+ * VTK today, which measures 4-5x larger.
+ */
+namespace kel {
+namespace lbm {
+
+namespace err {
+/// The buffer ended before a value that the layout said was there.
+struct forst_truncated {
+ static constexpr std::string_view description = "Truncated Forst buffer";
+ static constexpr bool is_critical = false;
+};
+
+/// A length or offset in the buffer does not agree with the buffer itself.
+struct forst_malformed {
+ static constexpr std::string_view description = "Malformed Forst buffer";
+ static constexpr bool is_critical = false;
+};
+} // namespace err
+
+namespace impl {
+
+/**
+ * The layout of a schema.
+ *
+ * `layers` is dynamicity depth: 0 for something wholly fixed size, and one
+ * more for each level of variable length nesting. `static_size` is the bytes
+ * the schema occupies in a static section -- for a variable length schema that
+ * is the size of its dimensions plus its offset, not of its contents.
+ */
+template <typename Schema> struct forst_info {
+ static_assert(always_false<Schema>, "This schema has no Forst layout");
+};
+
+template <> struct forst_info<sch::Void> {
+ static constexpr uint64_t layers = 0u;
+ static constexpr uint64_t static_size = 0u;
+};
+
+template <> struct forst_info<sch::Bool> {
+ static constexpr uint64_t layers = 0u;
+ static constexpr uint64_t static_size = 1u;
+};
+
+template <typename Kind, uint64_t N> struct forst_info<sch::Primitive<Kind, N>> {
+ static constexpr uint64_t layers = 0u;
+ static constexpr uint64_t static_size = N;
+};
+
+/**
+ * Stored as its storage type, not its interface type. The whole point of a
+ * mixed precision member is that the narrow form is what occupies space.
+ */
+template <typename Storage, typename Interface>
+struct forst_info<sch::MixedPrecision<Storage, Interface>> {
+ static constexpr uint64_t layers = forst_info<Storage>::layers;
+ static constexpr uint64_t static_size = forst_info<Storage>::static_size;
+};
+
+/// Length, then offset.
+template <> struct forst_info<sch::String> {
+ static constexpr uint64_t layers = 1u;
+ static constexpr uint64_t static_size = 16u;
+};
+
+template <typename T, uint64_t... D> struct forst_info<sch::FixedArray<T, D...>> {
+ static constexpr uint64_t layers = forst_info<T>::layers;
+ static constexpr uint64_t static_size =
+ ct_multiply<uint64_t, D...>::value * forst_info<T>::static_size;
+};
+
+template <typename T, uint64_t... D> struct forst_info<sch::Tensor<T, D...>> {
+ static constexpr uint64_t layers = forst_info<T>::layers;
+ static constexpr uint64_t static_size =
+ ct_multiply<uint64_t, D...>::value * forst_info<T>::static_size;
+};
+
+/// One UInt64 per dimension, then the offset.
+template <typename T, uint64_t Dim> struct forst_info<sch::Array<T, Dim>> {
+ static constexpr uint64_t layers = 1u + forst_info<T>::layers;
+ static constexpr uint64_t static_size = 8u * Dim + 8u;
+};
+
+template <typename... T> struct forst_info<sch::Tuple<T...>> {
+ template <uint64_t i> static constexpr uint64_t max_layers(){
+ if constexpr (i < sizeof...(T)){
+ using Ti = typename parameter_pack_type<i, T...>::type;
+ const uint64_t here = forst_info<Ti>::layers;
+ const uint64_t rest = max_layers<i + 1u>();
+ return (here > rest) ? here : rest;
+ }
+ return 0u;
+ }
+
+ template <uint64_t i> static constexpr uint64_t sum_static(){
+ if constexpr (i < sizeof...(T)){
+ using Ti = typename parameter_pack_type<i, T...>::type;
+ return forst_info<Ti>::static_size + sum_static<i + 1u>();
+ }
+ return 0u;
+ }
+
+ static constexpr uint64_t layers = max_layers<0u>();
+ static constexpr uint64_t static_size = sum_static<0u>();
+
+ /// Byte offset of member i within the static section.
+ template <uint64_t i> static constexpr uint64_t offset_of(){
+ if constexpr (i == 0u){
+ return 0u;
+ } else {
+ using Prev = typename parameter_pack_type<i - 1u, T...>::type;
+ return offset_of<i - 1u>() + forst_info<Prev>::static_size;
+ }
+ }
+};
+
+template <typename... T, string_literal... Keys>
+struct forst_info<sch::Struct<sch::Member<T, Keys>...>> {
+ using AsTuple = sch::Tuple<T...>;
+
+ static constexpr uint64_t layers = forst_info<AsTuple>::layers;
+ static constexpr uint64_t static_size = forst_info<AsTuple>::static_size;
+
+ template <uint64_t i> static constexpr uint64_t offset_of(){
+ return forst_info<AsTuple>::template offset_of<i>();
+ }
+};
+
+} // namespace impl
+
+/**
+ * The encoded bytes.
+ *
+ * Holds its own buffer and copies deeply, like enc::Native. Binary rather than
+ * text, so unlike data<Schema, enc::Json> this is a byte vector.
+ */
+template <typename Schema> class data<Schema, enc::Forst> final {
+public:
+ using SchemaType = Schema;
+
+private:
+ std::vector<uint8_t> value_;
+
+public:
+ data() = default;
+
+ explicit data(std::vector<uint8_t> value__) : value_{std::move(value__)} {}
+
+ void set(std::vector<uint8_t> value__) { value_ = std::move(value__); }
+
+ const std::vector<uint8_t> &bytes() const { return value_; }
+ std::vector<uint8_t> &bytes() { return value_; }
+
+ const uint8_t *raw() const { return value_.empty() ? nullptr : value_.data(); }
+ uint8_t *raw() { return value_.empty() ? nullptr : value_.data(); }
+
+ data<sch::UInt64, enc::Native> size() const { return {value_.size()}; }
+};
+
+namespace impl {
+
+/**
+ * Little endian is assumed, never converted.
+ *
+ * wasm is little endian by specification and every host this runs on is too.
+ * A big endian target should fail to build rather than silently produce
+ * byte-swapped field data that looks almost plausible.
+ */
+static_assert(std::endian::native == std::endian::little,
+ "Forst assumes a little endian host");
+
+/**
+ * Bytes a value needs in the dynamic layer. Zero for anything fixed size.
+ *
+ * With one layer the contents of a dynamic member are themselves always fixed
+ * size, so this never has to recurse into a dynamic child's own dynamic part.
+ */
+template <typename Schema> struct forst_dynamic_size {
+ static uint64_t apply(const data<Schema, enc::Native> &){ return 0u; }
+};
+
+template <> struct forst_dynamic_size<sch::String> {
+ static uint64_t apply(const data<sch::String, enc::Native> &from){
+ return from.size().get();
+ }
+};
+
+template <typename T, uint64_t Dim> struct forst_dynamic_size<sch::Array<T, Dim>> {
+ static uint64_t apply(const data<sch::Array<T, Dim>, enc::Native> &from){
+ uint64_t count = 1u;
+ for(uint64_t i = 0u; i < Dim; ++i){
+ count *= from.meta().flat_at(i).get();
+ }
+ return count * forst_info<T>::static_size;
+ }
+};
+
+template <typename T, uint64_t... D>
+struct forst_dynamic_size<sch::FixedArray<T, D...>> {
+ static uint64_t apply(const data<sch::FixedArray<T, D...>, enc::Native> &from){
+ if constexpr (forst_info<T>::layers == 0u){
+ return 0u;
+ } else {
+ uint64_t total = 0u;
+ constexpr uint64_t count = ct_multiply<uint64_t, D...>::value;
+ for(uint64_t i = 0u; i < count; ++i){
+ total += forst_dynamic_size<T>::apply(from.flat_at(i));
+ }
+ return total;
+ }
+ }
+};
+
+template <typename... T, string_literal... Keys>
+struct forst_dynamic_size<sch::Struct<sch::Member<T, Keys>...>> {
+ using Schema = sch::Struct<sch::Member<T, Keys>...>;
+
+ template <uint64_t i>
+ static uint64_t step(const data<Schema, enc::Native> &from){
+ if constexpr (i < sizeof...(T)){
+ using Ti = typename parameter_pack_type<i, T...>::type;
+ return forst_dynamic_size<Ti>::apply(from.template get_at_index<i>())
+ + step<i + 1u>(from);
+ }
+ return 0u;
+ }
+
+ static uint64_t apply(const data<Schema, enc::Native> &from){
+ return step<0u>(from);
+ }
+};
+
+/// Writes a value's static bytes at `at`, appending any contents at `dyn`.
+template <typename Schema> struct forst_write;
+
+template <typename Kind, uint64_t N> struct forst_write<sch::Primitive<Kind, N>> {
+ static void apply(const data<sch::Primitive<Kind, N>, enc::Native> &from,
+ uint8_t *base, uint64_t at, uint64_t &){
+ auto value = from.get();
+ static_assert(sizeof(value) == N, "Primitive storage must be its width");
+ std::memcpy(base + at, &value, N);
+ }
+};
+
+template <> struct forst_write<sch::Bool> {
+ static void apply(const data<sch::Bool, enc::Native> &from, uint8_t *base,
+ uint64_t at, uint64_t &){
+ base[at] = from.get() ? 1u : 0u;
+ }
+};
+
+template <> struct forst_write<sch::Void> {
+ static void apply(const data<sch::Void, enc::Native> &, uint8_t *, uint64_t,
+ uint64_t &){}
+};
+
+template <> struct forst_write<sch::String> {
+ static void apply(const data<sch::String, enc::Native> &from, uint8_t *base,
+ uint64_t at, uint64_t &dyn){
+ const uint64_t length = from.size().get();
+ std::memcpy(base + at, &length, 8u);
+
+ // Measured from just past the offset field, so resolving it is one add.
+ const uint64_t offset = dyn - (at + 16u);
+ std::memcpy(base + at + 8u, &offset, 8u);
+
+ if(length > 0u){
+ std::memcpy(base + dyn, from.stl_view().data(), length);
+ }
+ dyn += length;
+ }
+};
+
+template <typename T, uint64_t Dim> struct forst_write<sch::Array<T, Dim>> {
+ static void apply(const data<sch::Array<T, Dim>, enc::Native> &from,
+ uint8_t *base, uint64_t at, uint64_t &dyn){
+ static_assert(forst_info<T>::layers == 0u,
+ "One dynamic layer only: an Array element must be fixed size. "
+ "Array<String> and Array<Array<...>> need a second layer.");
+
+ uint64_t count = 1u;
+ for(uint64_t i = 0u; i < Dim; ++i){
+ const uint64_t extent = from.meta().flat_at(i).get();
+ std::memcpy(base + at + 8u * i, &extent, 8u);
+ count *= extent;
+ }
+
+ const uint64_t offset = dyn - (at + forst_info<sch::Array<T, Dim>>::static_size);
+ std::memcpy(base + at + 8u * Dim, &offset, 8u);
+
+ // Elements are fixed size, so they go straight into the layer with no
+ // dynamic part of their own.
+ uint64_t unused = 0u;
+ const uint64_t stride = forst_info<T>::static_size;
+ for(uint64_t i = 0u; i < count; ++i){
+ forst_write<T>::apply(from.flat_at(i), base, dyn + i * stride, unused);
+ }
+ dyn += count * stride;
+ }
+};
+
+template <typename T, uint64_t... D> struct forst_write<sch::FixedArray<T, D...>> {
+ static void apply(const data<sch::FixedArray<T, D...>, enc::Native> &from,
+ uint8_t *base, uint64_t at, uint64_t &dyn){
+ constexpr uint64_t count = ct_multiply<uint64_t, D...>::value;
+ constexpr uint64_t stride = forst_info<T>::static_size;
+ for(uint64_t i = 0u; i < count; ++i){
+ forst_write<T>::apply(from.flat_at(i), base, at + i * stride, dyn);
+ }
+ }
+};
+
+template <typename T, uint64_t... D> struct forst_write<sch::Tensor<T, D...>> {
+ static void apply(const data<sch::Tensor<T, D...>, enc::Native> &from,
+ uint8_t *base, uint64_t at, uint64_t &dyn){
+ constexpr uint64_t count = ct_multiply<uint64_t, D...>::value;
+ constexpr uint64_t stride = forst_info<T>::static_size;
+ for(uint64_t i = 0u; i < count; ++i){
+ forst_write<T>::apply(from.flat_at(i), base, at + i * stride, dyn);
+ }
+ }
+};
+template <typename... T, string_literal... Keys>
+struct forst_write<sch::Struct<sch::Member<T, Keys>...>> {
+ using Schema = sch::Struct<sch::Member<T, Keys>...>;
+ using Info = forst_info<Schema>;
+
+ template <uint64_t i>
+ static void step(const data<Schema, enc::Native> &from, uint8_t *base,
+ uint64_t at, uint64_t &dyn){
+ if constexpr (i < sizeof...(T)){
+ using Ti = typename parameter_pack_type<i, T...>::type;
+ forst_write<Ti>::apply(from.template get_at_index<i>(), base,
+ at + Info::template offset_of<i>(), dyn);
+ step<i + 1u>(from, base, at, dyn);
+ }
+ }
+
+ static void apply(const data<Schema, enc::Native> &from, uint8_t *base,
+ uint64_t at, uint64_t &dyn){
+ step<0u>(from, base, at, dyn);
+ }
+};
+
+
+/**
+ * Reads a value's static bytes at `at`.
+ *
+ * Every read is bounds checked against the buffer rather than trusted: the
+ * bytes may have come off a network from a peer that is older, newer, or
+ * hostile, and a bad offset would otherwise be an out of bounds read.
+ */
+template <typename Schema> struct forst_read;
+
+template <typename Kind, uint64_t N> struct forst_read<sch::Primitive<Kind, N>> {
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<sch::Primitive<Kind, N>, enc::Native> &to){
+ if(at + N > size){
+ return make_error<err::forst_truncated>();
+ }
+ typename native_data_type<sch::Primitive<Kind, N>>::type value{};
+ std::memcpy(&value, base + at, N);
+ to.set(value);
+ return make_void();
+ }
+};
+
+template <> struct forst_read<sch::Bool> {
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<sch::Bool, enc::Native> &to){
+ if(at + 1u > size){
+ return make_error<err::forst_truncated>();
+ }
+ to.set(base[at] != 0u);
+ return make_void();
+ }
+};
+
+template <> struct forst_read<sch::Void> {
+ static error_or<void> apply(const uint8_t *, uint64_t, uint64_t,
+ data<sch::Void, enc::Native> &){
+ return make_void();
+ }
+};
+
+template <> struct forst_read<sch::String> {
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<sch::String, enc::Native> &to){
+ if(at + 16u > size){
+ return make_error<err::forst_truncated>();
+ }
+
+ uint64_t length = 0u;
+ uint64_t offset = 0u;
+ std::memcpy(&length, base + at, 8u);
+ std::memcpy(&offset, base + at + 8u, 8u);
+
+ const uint64_t start = at + 16u + offset;
+ // Checked as a sum that cannot wrap, so a hostile length plus a
+ // hostile offset cannot alias back into the static section.
+ if(start > size or length > size - start){
+ return make_error<err::forst_malformed>();
+ }
+
+ to.set(std::string{reinterpret_cast<const char *>(base + start),
+ static_cast<std::size_t>(length)});
+ return make_void();
+ }
+};
+
+template <typename T, uint64_t Dim> struct forst_read<sch::Array<T, Dim>> {
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<sch::Array<T, Dim>, enc::Native> &to){
+ static_assert(forst_info<T>::layers == 0u,
+ "One dynamic layer only: an Array element must be fixed size.");
+
+ constexpr uint64_t slot = forst_info<sch::Array<T, Dim>>::static_size;
+ if(at + slot > size){
+ return make_error<err::forst_truncated>();
+ }
+
+ index_data<Dim> extents;
+ uint64_t count = 1u;
+ for(uint64_t i = 0u; i < Dim; ++i){
+ uint64_t extent = 0u;
+ std::memcpy(&extent, base + at + 8u * i, 8u);
+ extents.flat_at(i).set(extent);
+
+ // Reject an extent product that would overflow before it is used
+ // to size an allocation.
+ if(extent != 0u and count > (~uint64_t{0u}) / extent){
+ return make_error<err::forst_malformed>();
+ }
+ count *= extent;
+ }
+
+ uint64_t offset = 0u;
+ std::memcpy(&offset, base + at + 8u * Dim, 8u);
+
+ constexpr uint64_t stride = forst_info<T>::static_size;
+ const uint64_t start = at + slot + offset;
+ if(start > size or (stride != 0u and count > (size - start) / stride)){
+ return make_error<err::forst_malformed>();
+ }
+
+ to = data<sch::Array<T, Dim>, enc::Native>{extents};
+ for(uint64_t i = 0u; i < count; ++i){
+ auto eov = forst_read<T>::apply(base, size, start + i * stride,
+ to.flat_at(i));
+ if(eov.is_error()){
+ return eov;
+ }
+ }
+ return make_void();
+ }
+};
+
+template <typename T, uint64_t... D> struct forst_read<sch::FixedArray<T, D...>> {
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<sch::FixedArray<T, D...>, enc::Native> &to){
+ constexpr uint64_t count = ct_multiply<uint64_t, D...>::value;
+ constexpr uint64_t stride = forst_info<T>::static_size;
+ for(uint64_t i = 0u; i < count; ++i){
+ auto eov = forst_read<T>::apply(base, size, at + i * stride,
+ to.flat_at(i));
+ if(eov.is_error()){
+ return eov;
+ }
+ }
+ return make_void();
+ }
+};
+
+template <typename T, uint64_t... D> struct forst_read<sch::Tensor<T, D...>> {
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<sch::Tensor<T, D...>, enc::Native> &to){
+ constexpr uint64_t count = ct_multiply<uint64_t, D...>::value;
+ constexpr uint64_t stride = forst_info<T>::static_size;
+ for(uint64_t i = 0u; i < count; ++i){
+ auto eov = forst_read<T>::apply(base, size, at + i * stride,
+ to.flat_at(i));
+ if(eov.is_error()){
+ return eov;
+ }
+ }
+ return make_void();
+ }
+};
+
+template <typename... T, string_literal... Keys>
+struct forst_read<sch::Struct<sch::Member<T, Keys>...>> {
+ using Schema = sch::Struct<sch::Member<T, Keys>...>;
+ using Info = forst_info<Schema>;
+
+ template <uint64_t i>
+ static error_or<void> step(const uint8_t *base, uint64_t size, uint64_t at,
+ data<Schema, enc::Native> &to){
+ if constexpr (i < sizeof...(T)){
+ using Ti = typename parameter_pack_type<i, T...>::type;
+ auto eov = forst_read<Ti>::apply(
+ base, size, at + Info::template offset_of<i>(),
+ to.template get_at_index<i>());
+ if(eov.is_error()){
+ return eov;
+ }
+ return step<i + 1u>(base, size, at, to);
+ }
+ return make_void();
+ }
+
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<Schema, enc::Native> &to){
+ return step<0u>(base, size, at, to);
+ }
+};
+
+} // namespace impl
+
+/**
+ * Translates between a native value and its Forst bytes.
+ */
+template <typename Schema> class codec<Schema, enc::Forst> final {
+public:
+ static_assert(impl::forst_info<Schema>::layers <= 1u,
+ "Forst here carries exactly one dynamic layer. A schema needing more "
+ "-- Array<String>, Array<Array<...>> -- has to be flattened.");
+
+ codec() = default;
+
+ KEL_FORBID_COPY(codec);
+ KEL_DEFAULT_MOVE(codec);
+
+ error_or<void> encode(const data<Schema, enc::Native> &from,
+ data<Schema, enc::Forst> &to){
+ constexpr uint64_t static_size = impl::forst_info<Schema>::static_size;
+ const uint64_t dynamic_size = impl::forst_dynamic_size<Schema>::apply(from);
+
+ std::vector<uint8_t> buffer;
+ try {
+ buffer.resize(static_cast<std::size_t>(static_size + dynamic_size));
+ } catch (const std::exception &e){
+ (void) e;
+ return make_error<err::out_of_memory>();
+ }
+
+ uint64_t dyn = static_size;
+ if(not buffer.empty()){
+ impl::forst_write<Schema>::apply(from, buffer.data(), 0u, dyn);
+ }
+
+ to.set(std::move(buffer));
+ return make_void();
+ }
+
+ error_or<void> decode(const data<Schema, enc::Forst> &from,
+ data<Schema, enc::Native> &to){
+ const uint64_t size = from.size().get();
+ if(size < impl::forst_info<Schema>::static_size){
+ return make_error<err::forst_truncated>();
+ }
+ return impl::forst_read<Schema>::apply(from.raw(), size, 0u, to);
+ }
+};
+
+} // namespace lbm
+} // namespace kel
diff --git a/modules/core/c++/chunk.hpp b/modules/core/c++/chunk.hpp
index 126add8..8edd6ff 100644
--- a/modules/core/c++/chunk.hpp
+++ b/modules/core/c++/chunk.hpp
@@ -97,6 +97,16 @@ public:
return values_.flat_data();
}
+ /**
+ * The const counterpart data<FixedArray> has always had. Without it a
+ * const-correct reader -- a Forst encode, or a writer taking
+ * `const data<Sch>&` the way write_vtk_file does -- cannot reach the
+ * bytes at all.
+ */
+ auto flat_data() const {
+ return values_.flat_data();
+ }
+
static constexpr auto flat_size() {
return data<InnerSchema,Encode>::flat_dims();
}
diff --git a/modules/core/c++/forst_chunk.hpp b/modules/core/c++/forst_chunk.hpp
new file mode 100644
index 0000000..7d4c105
--- /dev/null
+++ b/modules/core/c++/forst_chunk.hpp
@@ -0,0 +1,73 @@
+#pragma once
+
+#include "abstract/forst.hpp"
+#include "chunk.hpp"
+
+/**
+ * The Forst layout for Chunk.
+ *
+ * Separate from abstract/forst.hpp because abstract/ sits below chunk.hpp and
+ * must not reach up into it. Include this rather than the abstract header when
+ * encoding fields.
+ *
+ * A Chunk is its InnerSchema -- a FixedArray widened by the ghost ring -- so it
+ * is entirely fixed size and encodes to exactly the bytes already sitting in
+ * flat_data(). That is what makes sending a field snapshot free of any
+ * serialisation pass or second buffer, which matters most in a browser tab
+ * where a second 18 MiB allocation may simply not be available.
+ *
+ * The encoded extents are `Sides + 2*Ghost`, not the logical sides. A reader
+ * without Ghost cannot index the buffer, which is why the result manifest
+ * carries it.
+ */
+namespace kel {
+namespace lbm {
+namespace impl {
+
+template <typename T, uint64_t Ghost, uint64_t... Sides>
+struct forst_info<sch::Chunk<T, Ghost, Sides...>> {
+ using Inner = typename sch::Chunk<T, Ghost, Sides...>::InnerSchema;
+ static constexpr uint64_t layers = forst_info<Inner>::layers;
+ static constexpr uint64_t static_size = forst_info<Inner>::static_size;
+};
+
+template <typename T, uint64_t Ghost, uint64_t... Sides>
+struct forst_write<sch::Chunk<T, Ghost, Sides...>> {
+ using Schema = sch::Chunk<T, Ghost, Sides...>;
+ using Value = typename Schema::StoredValueSchema;
+
+ static void apply(const data<Schema, enc::Native> &from, uint8_t *base,
+ uint64_t at, uint64_t &dyn){
+ const uint64_t count = data<Schema, enc::Native>::flat_size().get();
+ constexpr uint64_t stride = forst_info<Value>::static_size;
+ const auto *elements = from.flat_data();
+ for(uint64_t i = 0u; i < count; ++i){
+ forst_write<Value>::apply(elements[i], base, at + i * stride, dyn);
+ }
+ }
+};
+
+template <typename T, uint64_t Ghost, uint64_t... Sides>
+struct forst_read<sch::Chunk<T, Ghost, Sides...>> {
+ using Schema = sch::Chunk<T, Ghost, Sides...>;
+ using Value = typename Schema::StoredValueSchema;
+
+ static error_or<void> apply(const uint8_t *base, uint64_t size, uint64_t at,
+ data<Schema, enc::Native> &to){
+ const uint64_t count = data<Schema, enc::Native>::flat_size().get();
+ constexpr uint64_t stride = forst_info<Value>::static_size;
+ auto *elements = to.flat_data();
+ for(uint64_t i = 0u; i < count; ++i){
+ auto eov = forst_read<Value>::apply(base, size, at + i * stride,
+ elements[i]);
+ if(eov.is_error()){
+ return eov;
+ }
+ }
+ return make_void();
+ }
+};
+
+} // namespace impl
+} // namespace lbm
+} // namespace kel
diff --git a/modules/core/c++/fplbm.hpp b/modules/core/c++/fplbm.hpp
index bc99da5..780572b 100644
--- a/modules/core/c++/fplbm.hpp
+++ b/modules/core/c++/fplbm.hpp
@@ -2,7 +2,6 @@
#include "common.hpp"
#include "particle/particle.hpp"
-#include "particle/integrate.hpp"
#include "iterator.hpp"
namespace kel {
@@ -13,7 +12,6 @@ struct FpLbm {};
struct FpLbmOneParticle {};
struct FpLbmMomentumOneParticle {};
struct FpLbmOneParticleNoVelocity{};
-struct FpLbmOneParticleGather {};
}
namespace method {
struct FpLbm {};
@@ -101,7 +99,10 @@ public:
// We already computed this in reset
// compute_rho_u<T,Descriptor>(dfs,rho,vel);
- mom = mom + force * half;
+
+ // -------------------------
+ // mom = mom + force * half;
+ // -------------------------
auto vel = mom / rho;
auto eq = equilibrium<T,Descriptor>(rho,vel);
@@ -140,11 +141,10 @@ public:
};
template<typename T, typename Descriptor, typename Encode>
-class component<T, Descriptor, cmpt::FpLbmOneParticleGather, Encode> final {
+class component<T, Descriptor, cmpt::FpLbmOneParticle, Encode> final {
public:
- using Component = cmpt::FpLbmOneParticleGather;
+ using Component = cmpt::FpLbmOneParticle;
private:
- // An acceleration, as the comment above says -- now typed as one.
kel::lbm::data<sch::LbmAcceleration<sch::Vector<T,Descriptor::D>>> body_acceleration_;
public:
component():
@@ -175,28 +175,31 @@ public:
auto parts = pg.template get<"particles">();
auto parts_size = parts.meta().at({0u});
- auto& pi = parts.at(index);
+ auto& pi = parts.at(index);
auto& pirb = pi.template get<"rigid_body">();
+ // Unwrapped once: the sweep works in raw lattice offsets.
auto& pirb_pos = pirb.template get<"position">().handle();
auto& pirb_pos_old = pirb.template get<"position_old">().handle();
- auto& p_rad = pi.template get<"collision">().template get<"radius">().handle();
+ auto& p_coll = pg.template get<"collision">().at({});
+ auto& p_rad = p_coll.template get<"radius">().handle();
- kel::lbm::data<typename particle_aabb<typename ParticleSchema::ValueType>::AABB> aabb;
- if(not particle_aabb<typename ParticleSchema::ValueType>::try_calculate(pg,index,mom_f.meta(),aabb)){
+ auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(pg,{{0u}},mom_f.meta());
+ if(eo_aabb.is_error()){
return;
}
+ auto& aabb = eo_aabb.get_value();
kel::lbm::data<sch::Scalar<T>> three;
three.at({}).set(3.0f);
- kel::lbm::data<sch::Scalar<T>> two;
- two.at({}).set(2.0f);
-
kel::lbm::data<sch::Scalar<T>> one;
one.at({}) = 1.0f;
+
+ kel::lbm::data<sch::Scalar<T>> k;
+ k.at({}) = 1.0f;
kel::lbm::data<sch::Scalar<T>> eps;
eps.at({}) = 1.5f;
@@ -222,51 +225,27 @@ public:
// vel_s is technically time the density of the particle?
// force = body_acceleration_ * por + ( vel_s - mom) * two * flip_por;
- force = ( vel_s - mom) * two * flip_por;
+ // force = ( vel_s - mom) * eps * flip_por;
+ force = ( vel_s - mom) * k * flip_por;
// force = (vel_s - mom) * flip_por / (flip_por / two + one);
force_p = force_p - force;
}, aabb.template get<"a">(), aabb.template get<"b">());
- /**
- * Accumulate rather than assign, so a contact pass can add to this
- * before cmpt::ParticleIntegrate turns the total into motion.
- */
- auto& pirb_force = pirb.template get<"force">();
- pirb_force = pirb_force + force_p;
- }
-};
-
-/**
- * The original fused one-particle step, kept so existing call sites keep
- * working. The body acceleration rides straight through to the integrator;
- * it used to be called global_force_, which it never was -- the integrator
- * takes an acceleration, not a force.
- */
-template<typename T, typename Descriptor, typename Encode>
-class component<T, Descriptor, cmpt::FpLbmOneParticle, Encode> final {
-public:
- using Component = cmpt::FpLbmOneParticle;
-private:
- // An acceleration, as the comment above says -- now typed as one.
- kel::lbm::data<sch::LbmAcceleration<sch::Vector<T,Descriptor::D>>> body_acceleration_;
-public:
- component():
- body_acceleration_{}
- {}
- component(kel::lbm::data<sch::LbmAcceleration<sch::Vector<T,Descriptor::D>>> body_acceleration__):
- body_acceleration_{body_acceleration__}
- {}
-
- template<typename CellFieldSchema, typename MacroFieldSchema, typename ParticleSchema>
- void apply(const kel::lbm::data<CellFieldSchema, Encode>& field, const kel::lbm::data<MacroFieldSchema,Encode>& macros, const kel::lbm::data<ParticleSchema,Encode>& pg, kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index, kel::lbm::data<sch::UInt64> time_step, kel::lbm::data<sch::UInt64> sub_steps) const {
- component<T,Descriptor,cmpt::ParticleForceReset,Encode> reset;
- component<T,Descriptor,cmpt::FpLbmOneParticleGather,Encode> gather;
- component<T,Descriptor,cmpt::ParticleIntegrate,Encode> integrate{body_acceleration_};
+ auto& pirb_acc = pirb.template get<"acceleration">();
+ // Mass moved onto the particle; the quotient is untyped because force
+ // and mass have no units, so the acceleration unit is attached here.
+ pirb_acc = kel::lbm::data<sch::LbmAcceleration<sch::Vector<T,Descriptor::D>>>{
+ force_p / (pirb.template get<"mass">().at({}))}
+ + body_acceleration_;
- reset.apply(pg,index,time_step);
- gather.apply(field,macros,pg,index,time_step,sub_steps);
- integrate.apply(pg,index,time_step,sub_steps);
+ if(sub_steps.get() > 0u){
+ // The sub step length in lattice time units: one full step split sss ways.
+ kel::lbm::data<sch::LbmSecond<sch::Scalar<T>>> sss_inv{one / sss};
+ for(kel::lbm::data<sch::UInt64> i{0u}; i < sub_steps; ++i){
+ verlet_step_lambda<T,Descriptor::D>(pi,sss_inv);
+ }
+ }
}
};
}
diff --git a/modules/core/c++/write_vtk.hpp b/modules/core/c++/write_vtk.hpp
index 028a26f..4c67b74 100644
--- a/modules/core/c++/write_vtk.hpp
+++ b/modules/core/c++/write_vtk.hpp
@@ -1,7 +1,6 @@
#pragma once
#include "abstract/error.hpp"
-
#include "abstract/data.hpp"
#include "abstract/data_math.hpp"
@@ -47,7 +46,7 @@ struct lbm_vtk_writer<sch::FixedArray<T,D>> {
static kel::lbm::error_or<void> apply(std::ostream& vtk_file, const kel::lbm::data<sch::FixedArray<T,D>>& field){
kel::lbm::data<sch::FixedArray<sch::UInt64,D>> index;
for(kel::lbm::data<sch::UInt64> it{0}; it.get() < D; ++it){
- index.at({0u}).set(0u);
+ index.at(it).set(0u);
}
// vtk_file<<"VECTORS "<<name<<" float\n";
diff --git a/modules/core/tests/forst.cpp b/modules/core/tests/forst.cpp
new file mode 100644
index 0000000..2384126
--- /dev/null
+++ b/modules/core/tests/forst.cpp
@@ -0,0 +1,361 @@
+#include <forstio/test/suite.hpp>
+
+#include "../c++/abstract/forst.hpp"
+#include "../c++/forst_chunk.hpp"
+
+#include <cstring>
+#include <string>
+
+namespace {
+namespace sch {
+using namespace kel::lbm::sch;
+}
+using namespace kel::lbm;
+
+template <typename Schema>
+error_or<void> round_trip(const data<Schema> &from, data<Schema> &to){
+ codec<Schema, enc::Forst> c;
+
+ data<Schema, enc::Forst> bytes;
+ auto eov = c.encode(from, bytes);
+ if(eov.is_error()){
+ return eov;
+ }
+ return c.decode(bytes, to);
+}
+
+template <typename Schema>
+uint64_t encoded_size(const data<Schema> &from){
+ codec<Schema, enc::Forst> c;
+ data<Schema, enc::Forst> bytes;
+ auto eov = c.encode(from, bytes);
+ (void) eov;
+ return bytes.size().get();
+}
+
+SAW_TEST("Forst / layout traits match the declared table"){
+ using namespace kel::lbm::impl;
+
+ static_assert(forst_info<sch::UInt8>::static_size == 1u, "u8");
+ static_assert(forst_info<sch::Float32>::static_size == 4u, "f32");
+ static_assert(forst_info<sch::Float64>::static_size == 8u, "f64");
+ static_assert(forst_info<sch::UInt8>::layers == 0u, "primitives are fixed");
+
+ static_assert(forst_info<sch::String>::static_size == 16u, "length + offset");
+ static_assert(forst_info<sch::String>::layers == 1u, "a string is dynamic");
+
+ static_assert(forst_info<sch::Array<sch::UInt64, 1u>>::static_size == 16u,
+ "one extent + offset");
+ static_assert(forst_info<sch::Array<sch::UInt64, 2u>>::static_size == 24u,
+ "two extents + offset");
+
+ static_assert(forst_info<sch::FixedArray<sch::Float32, 9u>>::static_size == 36u,
+ "a fixed array is its elements inline");
+ static_assert(forst_info<sch::FixedArray<sch::Float32, 9u>>::layers == 0u,
+ "and stays fixed");
+
+ SAW_EXPECT(true, "static asserts carry this one");
+}
+
+/**
+ * The property the whole design rests on: an array of a dynamic element needs
+ * a second layer, and must therefore be rejected rather than mis-encoded.
+ */
+SAW_TEST("Forst / nesting depth is counted, so two layers are detectable"){
+ using namespace kel::lbm::impl;
+
+ static_assert(forst_info<sch::Array<sch::String, 1u>>::layers == 2u,
+ "Array<String> is two layers and must not pass the codec's assert");
+ static_assert(
+ forst_info<sch::Array<sch::Array<sch::UInt64,1u>, 1u>>::layers == 2u,
+ "Array<Array<...>> likewise");
+
+ using Flat = sch::Struct<
+ sch::Member<sch::String, "a">,
+ sch::Member<sch::Array<sch::UInt64, 1u>, "b">
+ >;
+ static_assert(forst_info<Flat>::layers == 1u,
+ "Several dynamic members side by side are still one layer");
+
+ // And the thing the result manifest depends on: an array of a *fixed*
+ // struct stays at one layer.
+ using FixedElem = sch::Struct<
+ sch::Member<sch::FixedArray<sch::UInt8, 32u>, "name">,
+ sch::Member<sch::UInt64, "bytes">
+ >;
+ static_assert(forst_info<FixedElem>::layers == 0u, "fixed struct");
+ static_assert(forst_info<sch::Array<FixedElem, 1u>>::layers == 1u,
+ "an array of fixed structs is what keeps the manifest flat");
+
+ SAW_EXPECT(true, "static asserts carry this one");
+}
+
+SAW_TEST("Forst / primitives round trip exactly"){
+ {
+ data<sch::Float32> from; from.set(-1.5f);
+ data<sch::Float32> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "f32");
+ SAW_EXPECT(to.get() == -1.5f, "f32 survives bit for bit");
+ }
+ {
+ data<sch::Float64> from; from.set(1.0 / 3.0);
+ data<sch::Float64> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "f64");
+ SAW_EXPECT(to.get() == 1.0 / 3.0, "f64 survives bit for bit");
+ }
+ {
+ // The edges, where a width or sign mistake would show.
+ data<sch::UInt64> from; from.set(~uint64_t{0u});
+ data<sch::UInt64> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "u64");
+ SAW_EXPECT(to.get() == ~uint64_t{0u}, "u64 max survives");
+ }
+ {
+ data<sch::Int32> from; from.set(-2147483647 - 1);
+ data<sch::Int32> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "i32");
+ SAW_EXPECT(to.get() == -2147483647 - 1, "i32 min survives");
+ }
+ {
+ data<sch::Bool> from; from.set(true);
+ data<sch::Bool> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "bool");
+ SAW_EXPECT(to.get(), "bool survives");
+ SAW_EXPECT(encoded_size(from) == 1u, "and costs one byte");
+ }
+}
+
+SAW_TEST("Forst / strings round trip, including the empty one"){
+ {
+ data<sch::String> from{std::string{"velocity"}};
+ data<sch::String> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "string");
+ SAW_EXPECT(to.stl_view() == "velocity", "contents survive");
+ SAW_EXPECT(encoded_size(from) == 16u + 8u, "slot plus contents");
+ }
+ {
+ data<sch::String> from{std::string{""}};
+ data<sch::String> to{std::string{"not empty"}};
+ SAW_EXPECT_EOV(round_trip(from, to), "empty string");
+ SAW_EXPECT(to.stl_view().empty(), "an empty string stays empty");
+ SAW_EXPECT(encoded_size(from) == 16u, "and costs only its slot");
+ }
+ {
+ // Embedded NULs and high bytes: this is a byte string, not C text.
+ std::string raw{"a\0b\xff", 4u};
+ data<sch::String> from{raw};
+ data<sch::String> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "binary string");
+ SAW_EXPECT(to.stl_string() == raw, "NULs and high bytes survive");
+ }
+}
+
+SAW_TEST("Forst / several dynamic members share the one layer"){
+ using Schema = sch::Struct<
+ sch::Member<sch::String, "first">,
+ sch::Member<sch::UInt32, "middle">,
+ sch::Member<sch::String, "last">
+ >;
+
+ data<Schema> from;
+ from.get<"first">().set(std::string{"alpha"});
+ from.get<"middle">().set(42u);
+ from.get<"last">().set(std::string{"omega"});
+
+ data<Schema> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "struct with two strings");
+
+ SAW_EXPECT(to.get<"first">().stl_view() == "alpha", "first survives");
+ SAW_EXPECT(to.get<"middle">().get() == 42u, "the fixed member between them survives");
+ SAW_EXPECT(to.get<"last">().stl_view() == "omega", "and so does the second string");
+
+ // 16 + 4 + 16 static, then 5 + 5 of contents.
+ SAW_EXPECT(encoded_size(from) == 36u + 10u, "no padding anywhere");
+}
+
+SAW_TEST("Forst / an array of fixed structs round trips"){
+ using Elem = sch::Struct<
+ sch::Member<sch::UInt32, "id">,
+ sch::Member<sch::Float32, "value">
+ >;
+ using Schema = sch::Struct<sch::Member<sch::Array<Elem, 1u>, "items">>;
+
+ data<Schema> from;
+ for(uint32_t i = 0u; i < 4u; ++i){
+ data<Elem> elem;
+ elem.get<"id">().set(i);
+ elem.get<"value">().set(static_cast<float>(i) * 0.5f);
+ auto eov = from.get<"items">().add(std::move(elem));
+ SAW_EXPECT_EOV(eov, "array grows");
+ }
+
+ data<Schema> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "array of structs");
+
+ SAW_EXPECT(to.get<"items">().meta().flat_at(0u).get() == 4u,
+ "the extent survives");
+ for(uint64_t i = 0u; i < 4u; ++i){
+ SAW_EXPECT(to.get<"items">().at({{i}}).get<"id">().get() == i,
+ "element ids survive");
+ SAW_EXPECT(to.get<"items">().at({{i}}).get<"value">().get()
+ == static_cast<float>(i) * 0.5f, "element values survive");
+ }
+
+ // 16 static for the slot, 8 per element.
+ SAW_EXPECT(encoded_size(from) == 16u + 4u * 8u, "exact, unpadded");
+}
+
+/**
+ * The claim the whole encoding choice rests on: a struct of chunks is entirely
+ * fixed size, so its Forst form is the packed field data itself. If this ever
+ * stops holding, sending results costs a serialisation pass and a second
+ * buffer -- in a browser tab, possibly one that does not exist.
+ */
+SAW_TEST("Forst / a struct of chunks is exactly its raw bytes"){
+ constexpr uint64_t dim_x = 16u;
+ constexpr uint64_t dim_y = 8u;
+
+ using Velocity = sch::Chunk<sch::Vector<sch::Float32, 2u>, 0u, dim_x, dim_y>;
+ using Density = sch::Chunk<sch::Scalar<sch::Float32>, 0u, dim_x, dim_y>;
+ using Macro = sch::Struct<
+ sch::Member<Velocity, "velocity">,
+ sch::Member<Density, "density">
+ >;
+
+ // The standard-layout assumption the zero-copy path depends on.
+ static_assert(sizeof(data<sch::Vector<sch::Float32, 2u>>) == 2u * sizeof(float),
+ "A Vector must be its components with nothing around them");
+ static_assert(sizeof(data<sch::Scalar<sch::Float32>>) == sizeof(float),
+ "A Scalar must be its component with nothing around it");
+
+ static_assert(kel::lbm::impl::forst_info<Macro>::layers == 0u,
+ "A struct of chunks has no dynamic part at all");
+
+ constexpr uint64_t expected = dim_x * dim_y * (2u + 1u) * 4u;
+ static_assert(kel::lbm::impl::forst_info<Macro>::static_size == expected,
+ "and occupies exactly the field data");
+
+ data<Macro> from;
+ for(uint64_t y = 0u; y < dim_y; ++y){
+ for(uint64_t x = 0u; x < dim_x; ++x){
+ auto &vel = from.get<"velocity">().at({{x, y}});
+ vel.at({{0u}}).set(static_cast<float>(x));
+ vel.at({{1u}}).set(static_cast<float>(y));
+ from.get<"density">().at({{x, y}}).at({}).set(
+ static_cast<float>(x * dim_y + y));
+ }
+ }
+
+ codec<Macro, enc::Forst> c;
+ data<Macro, enc::Forst> bytes;
+ SAW_EXPECT_EOV(c.encode(from, bytes), "encode");
+
+ SAW_EXPECT(bytes.size().get() == expected,
+ "The encoding adds not one byte of framing");
+
+ // And the bytes are the buffer, not merely the same size as it.
+ const auto *velocity_raw = reinterpret_cast<const uint8_t *>(
+ from.get<"velocity">().flat_data());
+ SAW_EXPECT(std::memcmp(bytes.raw(), velocity_raw, dim_x * dim_y * 2u * 4u) == 0,
+ "The velocity chunk encodes to its own memory, byte for byte");
+
+ const auto *density_raw = reinterpret_cast<const uint8_t *>(
+ from.get<"density">().flat_data());
+ SAW_EXPECT(std::memcmp(bytes.raw() + dim_x * dim_y * 2u * 4u, density_raw,
+ dim_x * dim_y * 4u) == 0,
+ "and the density chunk follows it with no gap");
+
+ data<Macro> to;
+ SAW_EXPECT_EOV(c.decode(bytes, to), "decode");
+ SAW_EXPECT(to.get<"velocity">().at({{3u, 5u}}).at({{1u}}).get() == 5.0f,
+ "values come back where they went in");
+ SAW_EXPECT(to.get<"density">().at({{3u, 5u}}).at({}).get()
+ == static_cast<float>(3u * dim_y + 5u), "for every member");
+}
+
+SAW_TEST("Forst / a chunk's ghost ring is part of the encoding"){
+ // Ghost=1 widens each side by two, and flat_data covers the ring. A reader
+ // that assumed the logical extents would be short by the whole border.
+ using Df = sch::Chunk<sch::FixedArray<sch::Float32, 9u>, 1u, 8u, 4u>;
+
+ constexpr uint64_t stored = (8u + 2u) * (4u + 2u);
+ static_assert(kel::lbm::impl::forst_info<Df>::static_size == stored * 9u * 4u,
+ "The encoded size is the padded extents, not the logical ones");
+
+ // Wrapped in a Struct rather than encoded bare: chunk.hpp already
+ // specialises data<Chunk<...>, Encode> for any encoding, so a top level
+ // data<Chunk, enc::Forst> is ambiguous against it. enc::Json has the same
+ // clash. A field is always a member of a struct in practice.
+ using Field = sch::Struct<sch::Member<Df, "dfs">>;
+
+ data<Field> from;
+ from.get<"dfs">().at({{0u, 0u}}).at({3u}).set(7.0f);
+
+ data<Field> to;
+ SAW_EXPECT_EOV(round_trip(from, to), "df chunk");
+ SAW_EXPECT(to.get<"dfs">().at({{0u, 0u}}).at({3u}).get() == 7.0f,
+ "interior survives");
+}
+
+SAW_TEST("Forst / truncated input is rejected rather than read past"){
+ using Schema = sch::Struct<
+ sch::Member<sch::String, "name">,
+ sch::Member<sch::UInt64, "count">
+ >;
+
+ data<Schema> from;
+ from.get<"name">().set(std::string{"something"});
+ from.get<"count">().set(5u);
+
+ codec<Schema, enc::Forst> c;
+ data<Schema, enc::Forst> bytes;
+ SAW_EXPECT_EOV(c.encode(from, bytes), "encode");
+
+ // Cut the contents off and the string's length no longer fits the buffer.
+ auto truncated = bytes.bytes();
+ truncated.resize(truncated.size() - 4u);
+ data<Schema, enc::Forst> damaged{std::move(truncated)};
+
+ data<Schema> to;
+ SAW_EXPECT(c.decode(damaged, to).is_error(),
+ "A string reaching past the end must be an error, not a read");
+
+ // Cut below the static section and it must fail before indexing anything.
+ data<Schema, enc::Forst> stub{std::vector<uint8_t>(4u, 0u)};
+ SAW_EXPECT(c.decode(stub, to).is_error(),
+ "A buffer shorter than the static section must be rejected up front");
+ SAW_EXPECT(c.decode(data<Schema, enc::Forst>{}, to).is_error(),
+ "and so must an empty one");
+}
+
+SAW_TEST("Forst / a hostile offset cannot escape the buffer"){
+ using Schema = sch::Struct<sch::Member<sch::String, "name">>;
+
+ data<Schema> from;
+ from.get<"name">().set(std::string{"abc"});
+
+ codec<Schema, enc::Forst> c;
+ data<Schema, enc::Forst> bytes;
+ SAW_EXPECT_EOV(c.encode(from, bytes), "encode");
+
+ // A length near the top of the range must not wrap when added to a start
+ // that is itself inside the buffer.
+ {
+ auto tampered = bytes.bytes();
+ const uint64_t huge = ~uint64_t{0u} - 8u;
+ std::memcpy(tampered.data(), &huge, 8u);
+ data<Schema, enc::Forst> evil{std::move(tampered)};
+ data<Schema> to;
+ SAW_EXPECT(c.decode(evil, to).is_error(), "An absurd length is rejected");
+ }
+ // An offset pointing far outside likewise.
+ {
+ auto tampered = bytes.bytes();
+ const uint64_t far = ~uint64_t{0u} / 2u;
+ std::memcpy(tampered.data() + 8u, &far, 8u);
+ data<Schema, enc::Forst> evil{std::move(tampered)};
+ data<Schema> to;
+ SAW_EXPECT(c.decode(evil, to).is_error(), "An absurd offset is rejected");
+ }
+}
+}