6#include "dawn/prog/bitmerge.hxx"
13#include "dawn/debug.hxx"
14#include "dawn/io/common.hxx"
15#include "dawn/io/ddata.hxx"
19static const size_t INPUT_WORDS = 3;
20static const uint32_t FIELD_BITS =
sizeof(uint32_t) * CHAR_BIT;
24#if defined(CONFIG_DAWN_DTYPE_BOOL) || defined(CONFIG_DAWN_DTYPE_INT8) || \
25 defined(CONFIG_DAWN_DTYPE_UINT8)
26# define BITMERGE_HAVE_8BIT
29#if defined(CONFIG_DAWN_DTYPE_INT16) || defined(CONFIG_DAWN_DTYPE_UINT16)
30# define BITMERGE_HAVE_16BIT
33#if defined(CONFIG_DAWN_DTYPE_INT32) || defined(CONFIG_DAWN_DTYPE_UINT32)
34# define BITMERGE_HAVE_32BIT
39static bool isMergeDtype(uint8_t dtype)
43#ifdef CONFIG_DAWN_DTYPE_BOOL
46#ifdef CONFIG_DAWN_DTYPE_INT8
49#ifdef CONFIG_DAWN_DTYPE_UINT8
52#ifdef CONFIG_DAWN_DTYPE_INT16
55#ifdef CONFIG_DAWN_DTYPE_UINT16
58#ifdef CONFIG_DAWN_DTYPE_INT32
61#ifdef CONFIG_DAWN_DTYPE_UINT32
77#ifdef BITMERGE_HAVE_8BIT
80 return *
static_cast<uint8_t *
>(data->
getDataPtr());
84#ifdef BITMERGE_HAVE_16BIT
87 return *
static_cast<uint16_t *
>(data->
getDataPtr());
91#ifdef BITMERGE_HAVE_32BIT
94 return *
static_cast<uint32_t *
>(data->
getDataPtr());
107static void writeElem(
io_ddata_t *data, uint32_t word)
111#ifdef BITMERGE_HAVE_8BIT
114 *
static_cast<uint8_t *
>(data->
getDataPtr()) =
static_cast<uint8_t
>(word);
119#ifdef BITMERGE_HAVE_16BIT
122 *
static_cast<uint16_t *
>(data->
getDataPtr()) =
static_cast<uint16_t
>(word);
127#ifdef BITMERGE_HAVE_32BIT
130 *
static_cast<uint32_t *
>(data->
getDataPtr()) = word;
145static void mergeSamplesT(
const void *in,
152 const T *src =
static_cast<const T *
>(in);
153 T *dst =
static_cast<T *
>(out);
156 for (i = 0; i < items; i++)
158 dst[i] =
static_cast<T
>(((
static_cast<uint32_t
>(src[i]) & mask) << shift) | orval);
166 , outputData(nullptr)
167 , streamIdx(NO_STREAM)
174CProgBitMerge::~CProgBitMerge()
188 inp.currentData =
nullptr;
190 inputs.push_back(inp);
195int CProgBitMerge::configureDesc(
const CDescObject &desc)
197 const SObjectCfg::SObjectCfgItem *item;
198 const SObjectId::UObjectId *ids;
199 const uint32_t *vals;
205 for (ii = 0; ii < desc.
getSize(); ii++)
209 if (item->
cfgid.
s.
cls != CProgCommon::PROG_CLASS_BITMERGE)
211 DAWNERR(
"bitmerge: unsupported cfg class 0x%" PRIx32
"\n", item->
cfgid.
v);
217 case PROG_BITMERGE_CFG_INPUTS:
220 if (n == 0 || n % INPUT_WORDS != 0)
222 DAWNERR(
"bitmerge: invalid INPUTS size %zu\n", n);
226 vals =
reinterpret_cast<const uint32_t *
>(item->
data);
227 for (
size_t j = 0; j < n; j += INPUT_WORDS)
229 ids =
reinterpret_cast<const SObjectId::UObjectId *
>(&vals[j]);
230 int ret = allocInput(ids[0].v, vals[j + 1], vals[j + 2]);
239 case PROG_BITMERGE_CFG_OUTPUT:
243 DAWNERR(
"bitmerge: OUTPUT must have 1 entry\n");
247 ids =
reinterpret_cast<const SObjectId::UObjectId *
>(item->
data);
255 DAWNERR(
"bitmerge: unsupported cfg id %u\n", item->
cfgid.
s.
id);
263 DAWNERR(
"bitmerge: at least one input required\n");
269 DAWNERR(
"bitmerge: output IO not configured\n");
278 return configureDesc(
getDesc());
281int CProgBitMerge::checkFieldLayout(uint32_t width)
const
286 for (i = 0; i < inputs.size(); i++)
290 if (inputs[i].mask == 0)
292 DAWNERR(
"bitmerge: input %zu has an empty mask\n", i);
296 if (inputs[i].shift >= width)
298 DAWNERR(
"bitmerge: input %zu shift %" PRIu32
" out of range\n", i, inputs[i].shift);
304 field = inputs[i].mask << inputs[i].shift;
305 if ((field >> inputs[i].shift) != inputs[i].mask ||
306 (width < FIELD_BITS && (field >> width) != 0))
308 DAWNERR(
"bitmerge: input %zu field does not fit the word\n", i);
312 if ((used & field) != 0)
314 DAWNERR(
"bitmerge: input %zu field overlaps an earlier one\n", i);
331 for (i = 0; i < inputs.size(); i++)
333 io =
getIO(inputs[i].ioId);
336 DAWNERR(
"bitmerge: input IO 0x%" PRIx32
" not found\n", inputs[i].ioId);
342 DAWNERR(
"bitmerge: input 0x%" PRIx32
" is not readable\n", inputs[i].ioId);
349 "bitmerge: input 0x%" PRIx32
" unsupported dtype %u\n", inputs[i].ioId, io->
getDtype());
360 if (streamIdx != NO_STREAM)
362 DAWNERR(
"bitmerge: only one batched input is supported\n");
370 DAWNERR(
"bitmerge: batched input 0x%" PRIx32
" is not notify-capable\n",
376 batch = io->getNotifyBatch();
383 DAWNWARN(
"bitmerge: input 0x%" PRIx32
" dim %zu, using element 0\n",
389 io =
getIO(outputId);
392 DAWNERR(
"bitmerge: output IO 0x%" PRIx32
" not found\n", outputId);
397 if (!isMergeDtype(output->getDtype()))
399 DAWNERR(
"bitmerge: output unsupported dtype %u\n", output->getDtype());
403 if (streamIdx != NO_STREAM)
409 DAWNERR(
"bitmerge: output element size must match the batched input\n");
416 ret = checkFieldLayout(output->getDtypeSize() * CHAR_BIT);
432 DAWNERR(
"bitmerge: output target prepare failed %d\n", ret);
436 for (i = 0; i < inputs.size(); i++)
438 inputs[i].currentData = inputs[i].io->ddata_alloc(1);
439 if (inputs[i].currentData ==
nullptr)
441 DAWNERR(
"bitmerge: currentData allocation failed for input %zu\n", i);
446 outputData = output->ddata_alloc(batch);
447 if (outputData ==
nullptr)
449 DAWNERR(
"bitmerge: outputData allocation failed\n");
460 for (
size_t i = 0; i < inputs.size(); i++)
462 delete inputs[i].currentData;
463 inputs[i].currentData =
nullptr;
469 outputData =
nullptr;
472 streamIdx = NO_STREAM;
477int CProgBitMerge::ioNotifierCb(
void *priv,
io_ddata_t *data)
479 SMergeInput *inp =
static_cast<SMergeInput *
>(priv);
481 if (inp ==
nullptr || inp->owner ==
nullptr || !inp->owner->active)
486 if (inp->owner->streamIdx != NO_STREAM && inp == &inp->owner->inputs[inp->owner->streamIdx])
488 inp->owner->updateStream(data);
494 if (inp->owner->streamIdx == NO_STREAM)
496 inp->owner->updateScalar();
511 for (i = 0; i < inputs.size(); i++)
513 if (!inputs[i].io->isNotify())
518 ret = inputs[i].io->setNotifier(ioNotifierCb, 0, &inputs[i]);
521 DAWNERR(
"bitmerge: setNotifier failed on input %zu: %d\n", i, ret);
527 if (inputs[i].io->isNotify())
529 inputs[i].io->setNotifier(
nullptr, 0,
nullptr);
541 if (streamIdx == NO_STREAM)
553 for (
size_t i = 0; i < inputs.size(); i++)
555 if (inputs[i].io && inputs[i].io->isNotify())
557 inputs[i].io->setNotifier(
nullptr, 0,
nullptr);
573int CProgBitMerge::mergeConstant(
size_t skip, uint32_t &acc)
580 for (i = 0; i < inputs.size(); i++)
582 if (i == skip || inputs[i].currentData ==
nullptr)
587 ret = inputs[i].io->getData(*inputs[i].currentData, 1);
590 DAWNERR(
"bitmerge: getData failed for input 0x%" PRIx32
": %d\n", inputs[i].ioId, ret);
594 acc |= (readElem(inputs[i].currentData) & inputs[i].mask) << inputs[i].shift;
600void CProgBitMerge::updateScalar()
605 if (output ==
nullptr || outputData ==
nullptr)
612 std::lock_guard<std::mutex> lock(mergeLock);
614 if (mergeConstant(NO_STREAM, word) != OK)
619 writeElem(outputData, word);
621 ret = output->setData(*outputData);
624 DAWNERR(
"bitmerge: setData on output failed %d\n", ret);
628void CProgBitMerge::mergeBatch(
const void *in,
631 const SMergeInput *src,
634 switch (outputData->getSize())
636#ifdef BITMERGE_HAVE_8BIT
639 mergeSamplesT<uint8_t>(in, out, items, src->mask, src->shift, orval);
644#ifdef BITMERGE_HAVE_16BIT
647 mergeSamplesT<uint16_t>(in, out, items, src->mask, src->shift, orval);
652#ifdef BITMERGE_HAVE_32BIT
655 mergeSamplesT<uint32_t>(in, out, items, src->mask, src->shift, orval);
667void CProgBitMerge::updateStream(
io_ddata_t *data)
669 const SMergeInput *src;
678 if (data ==
nullptr || output ==
nullptr || outputData ==
nullptr)
683 src = &inputs[streamIdx];
687 std::lock_guard<std::mutex> lock(mergeLock);
691 if (mergeConstant(streamIdx, orval) != OK)
698 nbatch = std::min(data->
getBatch(), outputData->getBatch());
700 nitems = std::min(data->
getItems(), outputData->getItems());
707 mergeBatch(data->
getDataPtr(0), outputData->getDataPtr(0), flat, src, orval);
713 for (b = 0; b < nbatch; b++)
715 mergeBatch(data->
getDataPtr(first + b), outputData->getDataPtr(b), nitems, src, orval);
721 for (b = 0; b < nbatch; b++)
723 outputData->getTs(b) = data->
getTs(first + b);
727 ret = output->setData(*outputData);
730 DAWNERR(
"bitmerge: setData on output failed %d\n", ret);
CIOCommon * getIO(SObjectId::ObjectId id)
Get an I/O object by ID.
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemNext(size_t &offset) const
Get config item at current offset and advance past it.
Base class for all I/O objects.
virtual bool isBatch() const =0
Check if IO supports batch operations.
virtual bool isNotify() const =0
Check if IO supports notifications.
virtual size_t getDataDim() const =0
Get data vector dimension.
virtual bool isRead() const =0
Check if IO supports read operations.
@ IO_CLASS_VIRT
Virtual I/O.
CDescObject & getDesc()
Get descriptor object for this object.
size_t getDtypeSize() const
Get size of this object's data type.
uint8_t getDtype() const
Get data type field.
int init()
One-time initialize object after bindings are resolved.
bool hasThread() const
Check if a background thread is active.
int doStart()
Start implementation hook.
int configure()
Configure object from descriptor data.
int deinit()
De-initialize object.
int doStop()
Stop implementation hook.
Base class for all PROG (processing) objects.
int prepareWritableTarget(CIOCommon *io, size_t dim, bool notify, size_t batch=1)
Check a writable output; shape a deferred virt output or check an already shaped one.
static size_t contiguousCount(io_ddata_t *d, size_t nbatch, size_t nitems)
Count elements of the first nbatch batches stored back to back.
Out-of-tree user-extension hooks for Dawn.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_INT32
Signed 32-bit integer (-2147483648 to 2147483647).
@ DTYPE_UINT8
Unsigned 8-bit integer (0 to 255).
@ DTYPE_INT16
Signed 16-bit integer (-32768 to 32767).
@ DTYPE_INT8
Signed 8-bit integer (-128 to 127).
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
@ DTYPE_BOOL
Boolean data type (stored in 32-bit container).
uint32_t ObjectId
ObjectID type - single 32-bit value.
Heap-allocated dynamic I/O data buffer.
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
bool hasTimestamp()
Check if this buffer has per-batch timestamp storage.
size_t getSize() const
Get element size in bytes.
size_t getItems()
Get number of items per batch.
size_t getBatch()
Get number of batches.
io_ts_t & getTs(size_t batch=0)
Get timestamp reference for batch.
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
struct dawn::SObjectCfg::UObjectCfgId::@334307326077356333363326153167141302007100111136 s
Bit-field structure for named member access.
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
ObjectId v
Raw 32-bit ObjectID value (for comparison, hashing, storage).