xo-reader2 stack: use NumericDispatch for *,/,+,- ops

This commit is contained in:
Roland Conybeare 2026-02-18 22:40:37 -08:00
commit 9dca9a8c46
12 changed files with 167 additions and 17 deletions

View file

@ -23,6 +23,10 @@ namespace xo {
DFloat * x, DInteger * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
static obj<AGCObject> add(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
static obj<AGCObject> subtract(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y);
};
class IntegerFloatOps {
@ -34,6 +38,10 @@ namespace xo {
DInteger * x, DFloat * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
static obj<AGCObject> add(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
static obj<AGCObject> subtract(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y);
};
}

View file

@ -22,6 +22,10 @@ namespace xo {
DFloat * x, DFloat * y);
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
static obj<AGCObject> add(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
static obj<AGCObject> subtract(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y);
};
}

View file

@ -23,6 +23,12 @@ namespace xo {
static obj<AGCObject> divide(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
static obj<AGCObject> add(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
static obj<AGCObject> subtract(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y);
};
}

View file

@ -70,6 +70,18 @@ namespace xo {
obj<AGCObject> x,
obj<AGCObject> y);
/** add w/ runtime polymorphism (double-dispatch)
**/
static obj<AGCObject> add(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y);
/** subtract w/ runtime polymorphism (double-dispatch)
**/
static obj<AGCObject> subtract(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y);
/** report memory use for owned arenas to @p visitor **/
void visit_pools(const MemorySizeVisitor & visitor);
@ -80,13 +92,19 @@ namespace xo {
**/
template <typename DRepr1, typename DRepr2>
void register_impl(typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl mul_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl div_fn) {
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl div_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl add_fn,
typename NumericOps<DRepr1, DRepr2>::BinaryOp_Impl sub_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);
this->dispatch_[key]
= NumericOps<DRepr1, DRepr2>::make(mul_fn,
div_fn,
add_fn,
sub_fn);
}
private:

View file

@ -23,11 +23,15 @@ namespace xo {
AnonymizedNumericOps() = default;
/** @p multiply to multiply (x,y); allocate from mm **/
explicit AnonymizedNumericOps(BinaryOp multiply,
BinaryOp divide)
: multiply_{multiply}, divide_{divide} {}
BinaryOp divide,
BinaryOp add,
BinaryOp subtract)
: multiply_{multiply}, divide_{divide}, add_{add}, subtract_{subtract} {}
BinaryOp multiply_ = nullptr;
BinaryOp divide_ = nullptr;
BinaryOp add_ = nullptr;
BinaryOp subtract_ = nullptr;
};
template <typename DRepr1, typename DRepr2>
@ -40,9 +44,13 @@ namespace xo {
public:
static AnonymizedNumericOps make(BinaryOp_Impl multiply,
BinaryOp_Impl divide) {
BinaryOp_Impl divide,
BinaryOp_Impl add,
BinaryOp_Impl subtract) {
return AnonymizedNumericOps(reinterpret_cast<BinaryOp_Anon>(multiply),
reinterpret_cast<BinaryOp_Anon>(divide));
reinterpret_cast<BinaryOp_Anon>(divide),
reinterpret_cast<BinaryOp_Anon>(add),
reinterpret_cast<BinaryOp_Anon>(subtract));
}
};

View file

@ -18,6 +18,10 @@ namespace xo {
static DPrimitive_gco_2_gco_gco s_mul_gco_gco_pm;
/** polymorphic (in both arguments) divide **/
static DPrimitive_gco_2_gco_gco s_div_gco_gco_pm;
/** polymorphic (in both arguments) add **/
static DPrimitive_gco_2_gco_gco s_add_gco_gco_pm;
/** polymorphic (in both arguments) subtract **/
static DPrimitive_gco_2_gco_gco s_sub_gco_gco_pm;
};
}
}

View file

@ -27,6 +27,20 @@ namespace xo {
return DFloat::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
obj<AGCObject>
FloatIntegerOps::add(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() + y->value());
}
obj<AGCObject>
FloatIntegerOps::subtract(obj<ARuntimeContext> rcx,
DFloat * x, DInteger * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() - y->value());
}
// ----- Integer op Float -----
obj<AGCObject>
@ -42,6 +56,21 @@ namespace xo {
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
obj<AGCObject>
IntegerFloatOps::add(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() + y->value());
}
obj<AGCObject>
IntegerFloatOps::subtract(obj<ARuntimeContext> rcx,
DInteger * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() - y->value());
}
}
}

View file

@ -25,6 +25,21 @@ namespace xo {
return DFloat::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
obj<AGCObject>
FloatOps::add(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() + y->value());
}
obj<AGCObject>
FloatOps::subtract(obj<ARuntimeContext> rcx,
DFloat * x, DFloat * y)
{
return DFloat::box<AGCObject>(rcx.allocator(), x->value() - y->value());
}
}
}

View file

@ -25,6 +25,20 @@ namespace xo {
return DInteger::box<AGCObject>(rcx.allocator(), x->value() / y->value());
}
obj<AGCObject>
IntegerOps::add(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y)
{
return DInteger::box<AGCObject>(rcx.allocator(), x->value() + y->value());
}
obj<AGCObject>
IntegerOps::subtract(obj<ARuntimeContext> rcx,
DInteger * x, DInteger * y)
{
return DInteger::box<AGCObject>(rcx.allocator(), x->value() - y->value());
}
}
}

View file

@ -27,6 +27,9 @@ namespace xo {
auto target_fn
= NumericDispatch::instance().dispatch_[key].multiply_;
if (!target_fn)
assert(false);
return (*target_fn)(rcx, x.data(), y.data());
}
@ -35,23 +38,48 @@ namespace xo {
obj<AGCObject> x,
obj<AGCObject> y)
{
scope log(XO_DEBUG(true));
KeyType key(x._typeseq(), y._typeseq());
log && log(xtag("x.tseq", x._typeseq().seqno()),
xtag("y.tseq", y._typeseq().seqno()));
auto target_fn
= NumericDispatch::instance().dispatch_[key].divide_;
log && log(xtag("target_fn", target_fn));
assert(target_fn);
if (!target_fn)
assert(false);
return (*target_fn)(rcx, x.data(), y.data());
}
obj<AGCObject>
NumericDispatch::add(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y)
{
KeyType key(x._typeseq(), y._typeseq());
auto target_fn
= NumericDispatch::instance().dispatch_[key].add_;
if (!target_fn)
assert(false);
return (*target_fn)(rcx, x.data(), y.data());
}
obj<AGCObject>
NumericDispatch::subtract(obj<ARuntimeContext> rcx,
obj<AGCObject> x,
obj<AGCObject> y)
{
KeyType key(x._typeseq(), y._typeseq());
auto target_fn
= NumericDispatch::instance().dispatch_[key].subtract_;
if (!target_fn)
assert(false);
return (*target_fn)(rcx, x.data(), y.data());
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -19,6 +19,14 @@ namespace xo {
NumericPrimitives::s_div_gco_gco_pm("_div",
&NumericDispatch::divide);
DPrimitive_gco_2_gco_gco
NumericPrimitives::s_add_gco_gco_pm("_add",
&NumericDispatch::add);
DPrimitive_gco_2_gco_gco
NumericPrimitives::s_sub_gco_gco_pm("_sub",
&NumericDispatch::subtract);
} /*namespace scm*/
} /*namespace xo*/

View file

@ -36,19 +36,27 @@ namespace xo {
NumericDispatch::instance().register_impl<DFloat, DFloat>
(&FloatOps::multiply,
&FloatOps::divide);
&FloatOps::divide,
&FloatOps::add,
&FloatOps::subtract);
NumericDispatch::instance().register_impl<DFloat, DInteger>
(&FloatIntegerOps::multiply,
&FloatIntegerOps::divide);
&FloatIntegerOps::divide,
&FloatIntegerOps::add,
&FloatIntegerOps::subtract);
NumericDispatch::instance().register_impl<DInteger, DFloat>
(&IntegerFloatOps::multiply,
&IntegerFloatOps::divide);
&IntegerFloatOps::divide,
&IntegerFloatOps::add,
&IntegerFloatOps::subtract);
NumericDispatch::instance().register_impl<DInteger, DInteger>
(&IntegerOps::multiply,
&IntegerOps::divide);
&IntegerOps::divide,
&IntegerOps::add,
&IntegerOps::subtract);
log && log(xtag("ANumeric.tseq", typeseq::id<ANumeric>()));