38#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
39 const char *getClassNameStr()
const override
71 SBindState **state)
override
78#ifdef CONFIG_DAWN_DTYPE_INT8
80 return allocStateInt<int8_t>(ioData->
getItems(), state);
82#ifdef CONFIG_DAWN_DTYPE_UINT8
84 return allocStateInt<uint8_t>(ioData->
getItems(), state);
86#ifdef CONFIG_DAWN_DTYPE_INT16
88 return allocStateInt<int16_t>(ioData->
getItems(), state);
90#ifdef CONFIG_DAWN_DTYPE_UINT16
92 return allocStateInt<uint16_t>(ioData->
getItems(), state);
94#ifdef CONFIG_DAWN_DTYPE_INT32
96 return allocStateInt<int32_t>(ioData->
getItems(), state);
98#ifdef CONFIG_DAWN_DTYPE_UINT32
100 return allocStateInt<uint32_t>(ioData->
getItems(), state);
102#ifdef CONFIG_DAWN_DTYPE_INT64
104 return allocStateInt<int64_t>(ioData->
getItems(), state);
106#ifdef CONFIG_DAWN_DTYPE_UINT64
108 return allocStateInt<uint64_t>(ioData->
getItems(), state);
110#ifdef CONFIG_DAWN_DTYPE_FLOAT
112 return allocStateFloat<float>(ioData->
getItems(), state);
114#ifdef CONFIG_DAWN_DTYPE_DOUBLE
116 return allocStateFloat<double>(ioData->
getItems(), state);
120 DAWNERR(
"stats rms: unsupported dtype %d\n", src->
getDtype());
130 bool &initsample)
override
132 handleWithState(output, data, ioData, outputData, initsample,
nullptr);
140 void *state)
override
142 if (state ==
nullptr)
144 DAWNERR(
"stats rms: missing state\n");
150 static_cast<SBindState *
>(state)->reset();
156#ifdef CONFIG_DAWN_DTYPE_INT8
158 handleInt<int8_t>(output, data, ioData, outputData, state);
161#ifdef CONFIG_DAWN_DTYPE_UINT8
163 handleInt<uint8_t>(output, data, ioData, outputData, state);
166#ifdef CONFIG_DAWN_DTYPE_INT16
168 handleInt<int16_t>(output, data, ioData, outputData, state);
171#ifdef CONFIG_DAWN_DTYPE_UINT16
173 handleInt<uint16_t>(output, data, ioData, outputData, state);
176#ifdef CONFIG_DAWN_DTYPE_INT32
178 handleInt<int32_t>(output, data, ioData, outputData, state);
181#ifdef CONFIG_DAWN_DTYPE_UINT32
183 handleInt<uint32_t>(output, data, ioData, outputData, state);
186#ifdef CONFIG_DAWN_DTYPE_INT64
188 handleInt<int64_t>(output, data, ioData, outputData, state);
191#ifdef CONFIG_DAWN_DTYPE_UINT64
193 handleInt<uint64_t>(output, data, ioData, outputData, state);
196#ifdef CONFIG_DAWN_DTYPE_FLOAT
198 handleFloat<float>(output, data, ioData, outputData, state);
201#ifdef CONFIG_DAWN_DTYPE_DOUBLE
203 handleFloat<double>(output, data, ioData, outputData, state);
208 DAWNERR(
"stats rms: unsupported dtype %d\n", data->
getDtype());
215 struct SStateInt final :
public SBindState
219 std::vector<uint64_t> sumsq;
221 void reset()
override
224 std::fill(sumsq.begin(), sumsq.end(), 0);
229 struct SStateFloat final :
public SBindState
233 std::vector<T> sumsq;
235 void reset()
override
238 std::fill(sumsq.begin(), sumsq.end(),
static_cast<T
>(0));
243 int allocStateInt(
size_t items, SBindState **state)
247 SStateInt *st =
new (std::nothrow) SStateInt();
254 st->sumsq.assign(items, 0);
255 if (st->sumsq.size() != items)
266 int allocStateFloat(
size_t items, SBindState **state)
268 SStateFloat<T> *st =
new (std::nothrow) SStateFloat<T>();
275 st->sumsq.assign(items,
static_cast<T
>(0));
276 if (st->sumsq.size() != items)
286 static uint64_t saturatedAdd(uint64_t a, uint64_t b)
288 if (UINT64_MAX - a < b)
297 static uint64_t intMagnitude(T v)
299 if constexpr (std::numeric_limits<T>::is_signed)
303 return static_cast<uint64_t
>(0) -
static_cast<uint64_t
>(v);
307 return static_cast<uint64_t
>(v);
310 static uint64_t squareSat(uint64_t v)
320 static uint64_t isqrt64(uint64_t x)
323 uint64_t bit =
static_cast<uint64_t
>(1) << 62;
335 root = (root >> 1) + bit;
349 static T castIntRms(uint64_t v)
351 const uint64_t vmax =
static_cast<uint64_t
>(std::numeric_limits<T>::max());
355 return std::numeric_limits<T>::max();
358 return static_cast<T
>(v);
362 void handleInt(CIOCommon *output,
365 io_ddata_t *outputData,
369 SStateInt *st =
static_cast<SStateInt *
>(state);
371 std::memcpy(ioData->getDataPtr(), data->getDataPtr(), ioData->getDataSize());
373 if (st->items != ioData->getItems())
375 DAWNERR(
"stats rms: item count changed from %u to %zu\n", st->items, ioData->getItems());
381 for (
size_t i = 0; i < ioData->getItems(); i++)
383 const uint64_t sq = squareSat(intMagnitude(ioData->get<T>(i)));
384 const uint64_t sumsq = saturatedAdd(st->sumsq[i], sq);
385 const uint64_t meanSq = sumsq / st->count;
387 st->sumsq[i] = sumsq;
388 outputData->get<T>(i) = castIntRms<T>(isqrt64(meanSq));
391 ret = output->setData(*outputData);
394 DAWNERR(
"failed to set rms value %d\n", ret);
399 void handleFloat(CIOCommon *output,
402 io_ddata_t *outputData,
406 SStateFloat<T> *st =
static_cast<SStateFloat<T> *
>(state);
408 std::memcpy(ioData->getDataPtr(), data->getDataPtr(), ioData->getDataSize());
410 if (st->items != ioData->getItems())
412 DAWNERR(
"stats rms: item count changed from %u to %zu\n", st->items, ioData->getItems());
418 for (
size_t i = 0; i < ioData->getItems(); i++)
420 const T x = ioData->get<T>(i);
421 const T sumsq = st->sumsq[i] + x * x;
423 st->sumsq[i] = sumsq;
424 outputData->get<T>(i) = std::sqrt(sumsq /
static_cast<T
>(st->count));
427 ret = output->setData(*outputData);
430 DAWNERR(
"failed to set rms value %d\n", ret);
int bindStateAlloc(CIOCommon *src, CIOCommon *output, io_ddata_t *ioData, io_ddata_t *outputData, SBindState **state)
Allocate optional per-binding derived state.
void handleWithState(CIOCommon *output, io_ddata_t *data, io_ddata_t *ioData, io_ddata_t *outputData, bool &initsample, void *state)
Process incoming sample with optional per-binding state.
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.