6#include "dawn/prog/adjust.hxx"
8#include "dawn/debug.hxx"
9#include "dawn/io/common.hxx"
10#include "dawn/io/ddata.hxx"
14template<
typename SRC,
typename DST,
typename PARAM>
15static void applyAdjust(
const void *src,
20 PARAM (*toCfg)(uint32_t))
22 PARAM scale = toCfg(rawScale);
23 PARAM offset = toCfg(rawOffset);
24 const SRC *in =
static_cast<const SRC *
>(src);
25 DST *out =
static_cast<DST *
>(dst);
27 for (
size_t i = 0; i < n; i++)
29 out[i] =
static_cast<DST
>(in[i]) * scale + offset;
34static void applyAdjustB16(
const void *src,
42 const SRC *in =
static_cast<const SRC *
>(src);
43 b16_t *out =
static_cast<b16_t *
>(dst);
45 for (
size_t i = 0; i < n; i++)
47 b16_t tmp1 = itob16(in[i]);
48 b16_t tmp2 = b16mulb16(tmp1, scale);
49 out[i] = tmp2 + offset;
53static void applyAdjustFloatToB16(
const void *src,
61 const float *in =
static_cast<const float *
>(src);
62 b16_t *out =
static_cast<b16_t *
>(dst);
64 for (
size_t i = 0; i < n; i++)
66 b16_t tmp1 = ftob16(in[i]);
67 b16_t tmp2 = b16mulb16(tmp1, scale);
68 out[i] = tmp2 + offset;
72static double cfgToDouble(uint32_t raw)
78static void applyAdjustRealToB16(
const void *src,
86 const SRC *in =
static_cast<const SRC *
>(src);
87 b16_t *out =
static_cast<b16_t *
>(dst);
89 for (
size_t i = 0; i < n; i++)
91 b16_t tmp1 = ftob16(
static_cast<float>(in[i]));
92 b16_t tmp2 = b16mulb16(tmp1, scale);
93 out[i] = tmp2 + offset;
98static bool applyAdjustToDst(
int dstType,
107#ifdef CONFIG_DAWN_DTYPE_INT8
112#ifdef CONFIG_DAWN_DTYPE_UINT8
117#ifdef CONFIG_DAWN_DTYPE_INT16
122#ifdef CONFIG_DAWN_DTYPE_UINT16
124 applyAdjust<SRC, uint16_t, uint32_t>(
128#ifdef CONFIG_DAWN_DTYPE_INT32
133#ifdef CONFIG_DAWN_DTYPE_UINT32
135 applyAdjust<SRC, uint32_t, uint32_t>(
139#ifdef CONFIG_DAWN_DTYPE_INT64
144#ifdef CONFIG_DAWN_DTYPE_UINT64
146 applyAdjust<SRC, uint64_t, uint32_t>(
150#ifdef CONFIG_DAWN_DTYPE_FLOAT
155#ifdef CONFIG_DAWN_DTYPE_DOUBLE
157 applyAdjust<SRC, double, double>(src, dst, n, rawScale, rawOffset, cfgToDouble);
160#ifdef CONFIG_DAWN_DTYPE_B16
162 applyAdjustB16<SRC>(src, dst, n, rawScale, rawOffset);
171bool applyAdjustToDst<float>(
int dstType,
180#ifdef CONFIG_DAWN_DTYPE_B16
181 applyAdjustFloatToB16(src, dst, n, rawScale, rawOffset);
188 return applyAdjustToDst<const float>(dstType, src, dst, n, rawScale, rawOffset);
192bool applyAdjustToDst<double>(
int dstType,
201#ifdef CONFIG_DAWN_DTYPE_B16
202 applyAdjustRealToB16<double>(src, dst, n, rawScale, rawOffset);
209 return applyAdjustToDst<const double>(dstType, src, dst, n, rawScale, rawOffset);
212static bool applyAdjustByType(
int srcType,
222#ifdef CONFIG_DAWN_DTYPE_INT8
224 return applyAdjustToDst<int8_t>(dstType, src, dst, n, rawScale, rawOffset);
226#ifdef CONFIG_DAWN_DTYPE_UINT8
228 return applyAdjustToDst<uint8_t>(dstType, src, dst, n, rawScale, rawOffset);
230#ifdef CONFIG_DAWN_DTYPE_INT16
232 return applyAdjustToDst<int16_t>(dstType, src, dst, n, rawScale, rawOffset);
234#ifdef CONFIG_DAWN_DTYPE_UINT16
236 return applyAdjustToDst<uint16_t>(dstType, src, dst, n, rawScale, rawOffset);
238#ifdef CONFIG_DAWN_DTYPE_INT32
240 return applyAdjustToDst<int32_t>(dstType, src, dst, n, rawScale, rawOffset);
242#ifdef CONFIG_DAWN_DTYPE_UINT32
244 return applyAdjustToDst<uint32_t>(dstType, src, dst, n, rawScale, rawOffset);
246#ifdef CONFIG_DAWN_DTYPE_INT64
248 return applyAdjustToDst<int64_t>(dstType, src, dst, n, rawScale, rawOffset);
250#ifdef CONFIG_DAWN_DTYPE_UINT64
252 return applyAdjustToDst<uint64_t>(dstType, src, dst, n, rawScale, rawOffset);
254#ifdef CONFIG_DAWN_DTYPE_FLOAT
256 return applyAdjustToDst<float>(dstType, src, dst, n, rawScale, rawOffset);
258#ifdef CONFIG_DAWN_DTYPE_DOUBLE
260 return applyAdjustToDst<double>(dstType, src, dst, n, rawScale, rawOffset);
267int CProgAdjust::ioNotifierCb(
void *priv,
io_ddata_t *data)
277int CProgAdjust::configureDesc(
const CDescObject &desc)
282 for (
size_t i = 0; i < desc.
getSize(); i++)
288 DAWNERR(
"Unsupported adjust config 0x%08" PRIx32
"\n", item->
cfgid.
v);
298 if (item->
cfgid.
s.
size !=
sizeof(SProgAdjustIOBind) / 4)
300 DAWNERR(
"Invalid IOBIND config size %d, expected %zu\n",
302 sizeof(SProgAdjustIOBind) / 4);
306 SProgAdjustIOBind *iobind =
310 offset +=
sizeof(SProgAdjustIOBind) / 4;
318 if (item->
cfgid.
s.
size !=
sizeof(SProgAdjustParams) / 4)
320 DAWNERR(
"Invalid PARAMS config size %d, expected %zu\n",
322 sizeof(SProgAdjustParams) / 4);
326 const SProgAdjustParams *params =
329 ioscale = params->scale;
330 iooffset = params->offset;
331 offset +=
sizeof(SProgAdjustParams) / 4;
337 DAWNERR(
"Unsupported adjust config 0x%08" PRIx32
"\n", item->
cfgid.
v);
350 DAWNERR(
"Null data pointer in handle()\n");
358 if (!applyAdjustByType(srcType, outputType, data_ptr, dst, n, ioscale, iooffset))
360 DAWNERR(
"Invalid conversion from type %d to type %d\n", srcType, outputType);
367bool CProgAdjust::isSupportedConvType(
int sourceType,
int targetType)
369 return applyAdjustByType(sourceType, targetType,
nullptr,
nullptr, 0, 0, 0);
372int CProgAdjust::allocObject(SProgAdjustIOBind *alloc)
374 DAWNINFO(
"allocate prog 0x%" PRIx32
" -> 0x%" PRIx32
"\n", alloc->objid.v, alloc->output.v);
388int CProgAdjust::bindPrepare()
395 DAWNERR(
"Source IO 0x%" PRIx32
" is not readable\n", src->getIdV());
399#ifdef CONFIG_DAWN_IO_NOTIFY
404 ret = src->setNotifier(ioNotifierCb, 0,
this);
407 DAWNERR(
"Set notifier failed for objId = 0x%" PRIx32
": %d\n", src->getIdV(), ret);
415 dim = src->getDataDim();
419 ret = prepareWritableTarget(output, dim, src->isNotify());
422 DAWNERR(
"Output IO initialize failed: %d\n", ret);
428 DAWNERR(
"Adjust output 0x%" PRIx32
" shape mismatch\n", output->
getIdV());
435CProgAdjust::~CProgAdjust()
446 ret = configureDesc(
getDesc());
449 DAWNERR(
"Adjust configure failed (error %d)\n", ret);
458 if (!isSupportedConvType(srcType, outputType))
460 DAWNERR(
"Unsupported conversion from type %d to type %d\n", srcType, outputType);
466 if (ioscale == 0 && iooffset == 0)
470#if defined(CONFIG_DAWN_DTYPE_INT8) || defined(CONFIG_DAWN_DTYPE_INT16) || \
471 defined(CONFIG_DAWN_DTYPE_INT32) || defined(CONFIG_DAWN_DTYPE_INT64)
481#if defined(CONFIG_DAWN_DTYPE_UINT8) || defined(CONFIG_DAWN_DTYPE_UINT16) || \
482 defined(CONFIG_DAWN_DTYPE_UINT32) || defined(CONFIG_DAWN_DTYPE_UINT64)
492#ifdef CONFIG_DAWN_DTYPE_FLOAT
499#ifdef CONFIG_DAWN_DTYPE_DOUBLE
506#ifdef CONFIG_DAWN_DTYPE_B16
514 DAWNERR(
"Unsupported output IO type %d for default scale/offset\n", outputType);
531 outputData =
nullptr;
544 src =
getIO(cfg->objid.v);
550 output =
getIO(cfg->output.v);
566 ioData = src->ddata_alloc(1);
583int CProgAdjust::refresh()
587 ret = src->getData(*ioData, 1);
590 DAWNERR(
"Failed to get data from source IO (error %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 * objectCfgItemAtOffset(size_t offset) const
Get configuration item at specified offset.
int setData(IODataCmn &data, size_t offset=0)
Set data for I/O (public interface with stats tracking).
virtual size_t getDataDim() const =0
Get data vector dimension.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
SObjectId::ObjectId getIdV() const
Get object identifier as raw 32-bit value.
CDescObject & getDesc()
Get descriptor object for this object.
Data scaling and offset transformation PROG.
int init()
One-time initialize object after bindings are resolved.
@ PROG_ADJUST_CFG_PARAMS
Adjustment parameters (scale, offset)
@ PROG_ADJUST_CFG_IOBIND
I/O binding configuration.
int doStart()
Start implementation hook.
int configure()
Configure object from descriptor data.
int deinit()
De-initialize object.
@ PROG_CLASS_ADJUST
Scale/offset adjustment.
static float cfgToF(ObjectCfgData_t x)
Convert ObjectCfgData_t to float.
static ObjectCfgData_t i32ToCfg(int32_t x)
Convert int32_t to ObjectCfgData_t.
static ObjectCfgData_t fToCfg(float x)
Convert float to ObjectCfgData_t.
static uint32_t cfgToU32(ObjectCfgData_t x)
Convert ObjectCfgData_t to uint32_t.
static ObjectCfgData_t u32ToCfg(uint32_t x)
Helper conversion functions: data types to cfg data type.
static b16_t cfgToB16(ObjectCfgData_t x)
Convert ObjectCfgData_t to b16_t.
static int32_t cfgtoi32(ObjectCfgData_t x)
Convert ObjectCfgData_t to int32_t.
static ObjectCfgData_t fToB16ToCfg(float x)
Convert float to b16_t and then to ObjectCfgData_t.
Out-of-tree user-extension hooks for Dawn.
Single configuration item within object.
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
@ 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_UINT64
Unsigned 64-bit integer.
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
@ DTYPE_DOUBLE
IEEE 754 double-precision floating point (64-bit).
@ DTYPE_INT64
Signed 64-bit integer.
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
@ DTYPE_B16
Signed 16.16 fixed-point (32-bit).
static uint8_t objectIdGetDtype(const ObjectId objid)
Extract data type field from ObjectID.
Heap-allocated dynamic I/O data buffer.
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).
size_t getItems()
Get number of items per 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).
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.