xo-reader2 stack: + xo-numeric + setup multi dispatch for *,/
This commit is contained in:
parent
b8fd087319
commit
ac1395ce98
28 changed files with 912 additions and 70 deletions
42
include/xo/numeric/FloatIntegerOps.hpp
Normal file
42
include/xo/numeric/FloatIntegerOps.hpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/** @file FloatIntegerOps.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Numeric.hpp"
|
||||
#include <xo/procedure2/RuntimeContext.hpp>
|
||||
#include <xo/object2/Float.hpp>
|
||||
#include <xo/object2/Integer.hpp>
|
||||
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
class FloatIntegerOps {
|
||||
public:
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
|
||||
public:
|
||||
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
|
||||
DFloat * x, DInteger * y);
|
||||
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
|
||||
DFloat * x, DInteger * y);
|
||||
};
|
||||
|
||||
class IntegerFloatOps {
|
||||
public:
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
|
||||
public:
|
||||
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
|
||||
DInteger * x, DFloat * y);
|
||||
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
|
||||
DInteger * x, DFloat * y);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* end FloatIntegerOps.hpp */
|
||||
30
include/xo/numeric/FloatOps.hpp
Normal file
30
include/xo/numeric/FloatOps.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/** @file FloatOps.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Numeric.hpp"
|
||||
#include <xo/procedure2/RuntimeContext.hpp>
|
||||
#include <xo/object2/Float.hpp>
|
||||
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
class FloatOps {
|
||||
public:
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
|
||||
public:
|
||||
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
|
||||
DFloat * x, DFloat * y);
|
||||
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
|
||||
DFloat * x, DFloat * y);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* end FloatOps.hpp */
|
||||
31
include/xo/numeric/IntegerOps.hpp
Normal file
31
include/xo/numeric/IntegerOps.hpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/** @file IntegerOps.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Numeric.hpp"
|
||||
#include <xo/procedure2/RuntimeContext.hpp>
|
||||
#include <xo/object2/Integer.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
class IntegerOps {
|
||||
public:
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
|
||||
public:
|
||||
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
|
||||
DInteger * x, DInteger * y);
|
||||
|
||||
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
|
||||
DInteger * x, DInteger * y);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* end IntegerOps.hpp */
|
||||
|
|
@ -5,13 +5,93 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "NumericOps.hpp"
|
||||
#include <xo/procedure2/RuntimeContext.hpp>
|
||||
#include <xo/gc/GCObject.hpp>
|
||||
#include <xo/arena/DArenaHashMap.hpp>
|
||||
#include <xo/reflectutil/typeseq.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** Runtime polymoprhic multimethod dispatch.
|
||||
* Hash on argument types to get function table
|
||||
**/
|
||||
class NumericDispatch {
|
||||
public:
|
||||
//using AAllocator = xo::mm::AAllocator;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using MemorySizeVisitor = xo::mm::MemorySizeVisitor;
|
||||
using typeseq = xo::reflect::typeseq;
|
||||
using KeyType = std::pair<typeseq, typeseq>;
|
||||
using MappedType = AnonymizedNumericOps;
|
||||
|
||||
|
||||
/** hash function for key_type **/
|
||||
struct KeyHash {
|
||||
|
||||
std::size_t operator()(const KeyType & k) const noexcept {
|
||||
// combine the two seqno values
|
||||
std::size_t h1 = std::hash<int32_t>{}(k.first.seqno());
|
||||
std::size_t h2 = std::hash<int32_t>{}(k.second.seqno());
|
||||
|
||||
return h1 ^ (h2 << 7);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
using MapType = xo::map::DArenaHashMap<KeyType,
|
||||
MappedType,
|
||||
KeyHash,
|
||||
std::equal_to<void>>;
|
||||
|
||||
public:
|
||||
NumericDispatch(uint32_t hint_max_capacity)
|
||||
: dispatch_{"numeric-dispatch",
|
||||
hint_max_capacity,
|
||||
false /*!debug_flag*/} {}
|
||||
|
||||
/** @p hint_max_capacity only honored the first time instance()
|
||||
* is called.
|
||||
**/
|
||||
static NumericDispatch & instance(uint32_t hint_max_capacity = 64) {
|
||||
static NumericDispatch s_instance(hint_max_capacity);
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
/** multiply w/ runtime polymorphism (double-dispatch)
|
||||
**/
|
||||
static obj<AGCObject> multiply(obj<ARuntimeContext> rcx,
|
||||
obj<AGCObject> x,
|
||||
obj<AGCObject> y);
|
||||
|
||||
/** divide w/ runtime polymorphism (double-dispatch)
|
||||
**/
|
||||
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
|
||||
obj<AGCObject> x,
|
||||
obj<AGCObject> y);
|
||||
|
||||
/** report memory use for owned arenas to @p visitor **/
|
||||
void visit_pools(const MemorySizeVisitor & visitor);
|
||||
|
||||
/** Use:
|
||||
* Need to have overload
|
||||
* multiply(obj<AAllocator>, DRepr1, DRepr2) -> obj<ANumeric>
|
||||
* available
|
||||
**/
|
||||
template <typename DRepr1, typename DRepr2>
|
||||
void register_impl(typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl mul_fn,
|
||||
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl div_fn) {
|
||||
|
||||
KeyType key(typeseq::id<DRepr1>().seqno(),
|
||||
typeseq::id<DRepr2>().seqno());
|
||||
|
||||
// note: copying op table so they're in proximity
|
||||
this->dispatch_[key] = NumericOps<DRepr1, DRepr2>::make(mul_fn, div_fn);
|
||||
}
|
||||
|
||||
private:
|
||||
/** 2d dispatch for arithmetic **/
|
||||
MapType dispatch_;
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
|
|
|
|||
|
|
@ -1,44 +1,52 @@
|
|||
/** @file NumericOps.hpp
|
||||
*
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Numeric.hpp"
|
||||
#include <xo/numeric/Numeric.hpp>
|
||||
#include <xo/procedure2/RuntimeContext.hpp>
|
||||
#include <xo/gc/GCObject.hpp>
|
||||
#include <xo/facet/obj.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
class INumericOps {
|
||||
class AnonymizedNumericOps {
|
||||
public:
|
||||
using BinaryOp1 = obj<ANumeric> (*)(obj<AAllocator> mm, void * x, void * y);
|
||||
using ARuntimeContext = xo::scm::ARuntimeContext;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using BinaryOp = obj<AGCObject> (*)(obj<ARuntimeContext> mm, void * x, void * y);
|
||||
|
||||
public:
|
||||
explicit INumericOp9s(BinaryOp1 multiply) : multiply_{multiply} {}
|
||||
/** note: null ctor load-bearing for membership in DArenaHashTable **/
|
||||
AnonymizedNumericOps() = default;
|
||||
/** @p multiply to multiply (x,y); allocate from mm **/
|
||||
explicit AnonymizedNumericOps(BinaryOp multiply,
|
||||
BinaryOp divide)
|
||||
: multiply_{multiply}, divide_{divide} {}
|
||||
|
||||
/** multiply (x,y); allocate from mm **/
|
||||
BinaryOp1 multiply_;
|
||||
BinaryOp multiply_ = nullptr;
|
||||
BinaryOp divide_ = nullptr;
|
||||
};
|
||||
|
||||
/** Convenience template. To use, provide implementation
|
||||
* for
|
||||
* _multiply() ...
|
||||
*
|
||||
**/
|
||||
template <typename DRepr1, typename DRepr2>
|
||||
class NumericOps : public INumericOps {
|
||||
class NumericOps {
|
||||
public:
|
||||
using BinaryOp1_Impl = obj<ANumeric> (*)(obj<AAllocator> mm, DRepr1 * x, DRepr2 * y);
|
||||
using ARuntimeContext = xo::scm::ARuntimeContext;
|
||||
using AGCObject = xo::mm::AGCObject;
|
||||
using BinaryOp_Impl = obj<AGCObject> (*)(obj<ARuntimeContext> rcx, DRepr1 * x, DRepr2 * y);
|
||||
using BinaryOp_Anon = AnonymizedNumericOps::BinaryOp;
|
||||
|
||||
public:
|
||||
explicit NumericOps(BinaryOp1_Impl multiply)
|
||||
: INumericOps(reinterpret_cast<INumericOps::BinaryOp1>(multiply))
|
||||
{}
|
||||
static AnonymizedNumericOps make(BinaryOp_Impl multiply,
|
||||
BinaryOp_Impl divide) {
|
||||
return AnonymizedNumericOps(reinterpret_cast<BinaryOp_Anon>(multiply),
|
||||
reinterpret_cast<BinaryOp_Anon>(divide));
|
||||
}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end NumericOps.hpp */
|
||||
|
||||
|
|
|
|||
23
include/xo/numeric/NumericPrimitives.hpp
Normal file
23
include/xo/numeric/NumericPrimitives.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/** @file NumericDispatch.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/procedure2/DPrimitive_gco_2_gco_gco.hpp>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
|
||||
/** @brief primitives using multidispatch
|
||||
**/
|
||||
class NumericPrimitives {
|
||||
public:
|
||||
/** poly divide **/
|
||||
static DPrimitive_gco_2_gco_gco s_div_gco_gco_pm;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/* end NumericDispatch.hpp */
|
||||
56
include/xo/numeric/float/INumeric_DFloat.hpp
Normal file
56
include/xo/numeric/float/INumeric_DFloat.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/** @file INumeric_DFloat.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/INumeric_DFloat.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/INumeric_DFloat.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Numeric.hpp"
|
||||
#include "DFloat.hpp"
|
||||
|
||||
namespace xo { namespace scm { class INumeric_DFloat; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::ANumeric,
|
||||
xo::scm::DFloat>
|
||||
{
|
||||
using ImplType = xo::scm::INumeric_Xfer
|
||||
<xo::scm::DFloat,
|
||||
xo::scm::INumeric_DFloat>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class INumeric_DFloat
|
||||
**/
|
||||
class INumeric_DFloat {
|
||||
public:
|
||||
/** @defgroup scm-numeric-dfloat-type-traits **/
|
||||
///@{
|
||||
using Copaque = xo::scm::ANumeric::Copaque;
|
||||
using Opaque = xo::scm::ANumeric::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-numeric-dfloat-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
21
include/xo/numeric/init_numeric.hpp
Normal file
21
include/xo/numeric/init_numeric.hpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/** @file init_numeric.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <xo/subsys/Subsystem.hpp>
|
||||
|
||||
namespace xo {
|
||||
/* tag to represent the xo-numeric/ subsystem within ordered initialization */
|
||||
enum S_numeric_tag {};
|
||||
|
||||
template <>
|
||||
struct InitSubsys<S_numeric_tag> {
|
||||
static void init();
|
||||
static InitEvidence require();
|
||||
};
|
||||
}
|
||||
|
||||
/* end init_numeric.hpp */
|
||||
56
include/xo/numeric/integer/INumeric_DInteger.hpp
Normal file
56
include/xo/numeric/integer/INumeric_DInteger.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/** @file INumeric_DInteger.hpp
|
||||
*
|
||||
* Generated automagically from ingredients:
|
||||
* 1. code generator:
|
||||
* [xo-facet/codegen/genfacet]
|
||||
* arguments:
|
||||
* --input [idl/INumeric_DInteger.json5]
|
||||
* 2. jinja2 template for abstract facet .hpp file:
|
||||
* [iface_facet_repr.hpp.j2]
|
||||
* 3. idl for facet methods
|
||||
* [idl/INumeric_DInteger.json5]
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Numeric.hpp"
|
||||
#include "DInteger.hpp"
|
||||
|
||||
namespace xo { namespace scm { class INumeric_DInteger; } }
|
||||
|
||||
namespace xo {
|
||||
namespace facet {
|
||||
template <>
|
||||
struct FacetImplementation<xo::scm::ANumeric,
|
||||
xo::scm::DInteger>
|
||||
{
|
||||
using ImplType = xo::scm::INumeric_Xfer
|
||||
<xo::scm::DInteger,
|
||||
xo::scm::INumeric_DInteger>;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class INumeric_DInteger
|
||||
**/
|
||||
class INumeric_DInteger {
|
||||
public:
|
||||
/** @defgroup scm-numeric-dinteger-type-traits **/
|
||||
///@{
|
||||
using Copaque = xo::scm::ANumeric::Copaque;
|
||||
using Opaque = xo::scm::ANumeric::Opaque;
|
||||
///@}
|
||||
/** @defgroup scm-numeric-dinteger-methods **/
|
||||
///@{
|
||||
// const methods
|
||||
|
||||
// non-const methods
|
||||
///@}
|
||||
};
|
||||
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end */
|
||||
16
include/xo/numeric/numeric_register_facets.hpp
Normal file
16
include/xo/numeric/numeric_register_facets.hpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/** @file numeric_register_facets.hpp
|
||||
*
|
||||
* @author Roland Conybeare, Feb 2026
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** Setup numeric facet dispatch **/
|
||||
bool numeric_register_facets();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* end numeric_register_facets.hpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue