Dawn Framework 1.0
Universal data acquisition framework for embedded systems
saturate.cxx
1// dawn/src/prog/saturate.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/prog/saturate.hxx"
7
8#include <algorithm>
9#include <limits>
10
11#include "dawn/debug.hxx"
12#include "dawn/io/common.hxx"
13#include "dawn/io/ddata.hxx"
14
15using namespace dawn;
16
17// Integer bound word, or the type limit when the bound is not set
18
19template<typename T>
20static T bound(uint32_t raw, bool set, T limit)
21{
22 return set ? static_cast<T>(static_cast<int32_t>(raw)) : limit;
23}
24
25// Real bounds travel as one float word
26
27static float bound(uint32_t raw, bool set, float limit)
28{
29 return set ? SObjectCfg::cfgToF(raw) : limit;
30}
31
32static double bound(uint32_t raw, bool set, double limit)
33{
34 return set ? static_cast<double>(SObjectCfg::cfgToF(raw)) : limit;
35}
36
39 size_t &offset)
40{
41 (void)desc;
42 (void)offset;
43
44 if (item->cfgid.s.size != 1)
45 {
46 DAWNERR("saturate: invalid config size %u\n", item->cfgid.s.size);
47 return -EINVAL;
48 }
49
50 switch (item->cfgid.s.id)
51 {
53 {
54 minRaw = item->data[0];
55 hasMin = true;
56 return OK;
57 }
58
60 {
61 maxRaw = item->data[0];
62 hasMax = true;
63 return OK;
64 }
65
66 default:
67 {
68 return -ENOTSUP;
69 }
70 }
71}
72
73int CProgSaturate::onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
74{
75 if (data == nullptr || len != 1)
76 {
77 return -EINVAL;
78 }
79
80 switch (SObjectCfg::objectCfgGetId(objcfg))
81 {
83 {
84 minRaw = data[0];
85 return OK;
86 }
87
89 {
90 maxRaw = data[0];
91 return OK;
92 }
93
94 default:
95 {
96 return OK;
97 }
98 }
99}
100
101template<typename T>
102void CProgSaturate::clamp(io_ddata_t *data, io_ddata_t *outputData, T lo, T hi)
103{
104 const size_t items = data->getItems();
105 const size_t batches = std::min(data->getBatch(), outputData->getBatch());
106 const size_t first = data->getBatch() - batches;
107
108 // An output holding fewer batches gets the latest samples
109
110 for (size_t b = 0; b < batches; b++)
111 {
112 for (size_t i = 0; i < items; i++)
113 {
114 outputData->get<T>(i, b) = std::min(std::max(data->get<T>(i, first + b), lo), hi);
115 }
116 }
117}
118
120 io_ddata_t *data,
121 io_ddata_t *ioData,
122 io_ddata_t *outputData,
123 bool &initsample)
124{
125 const uint32_t lo = minRaw;
126 const uint32_t hi = maxRaw;
127
128 (void)ioData;
129 (void)initsample;
130
131 if (outputData->getDtype() != data->getDtype())
132 {
133 DAWNERR("saturate: output dtype %d differs from input dtype %d\n",
134 outputData->getDtype(),
135 data->getDtype());
136 return;
137 }
138
139 switch (data->getDtype())
140 {
141#ifdef CONFIG_DAWN_DTYPE_BOOL
143 {
144 clamp<uint8_t>(
145 data, outputData, bound<uint8_t>(lo, hasMin, 0), bound<uint8_t>(hi, hasMax, 1));
146 break;
147 }
148#endif
149
150#ifdef CONFIG_DAWN_DTYPE_INT8
152 {
153 clamp<int8_t>(data,
154 outputData,
155 bound<int8_t>(lo, hasMin, INT8_MIN),
156 bound<int8_t>(hi, hasMax, INT8_MAX));
157 break;
158 }
159#endif
160
161#ifdef CONFIG_DAWN_DTYPE_UINT8
163 {
164 clamp<uint8_t>(
165 data, outputData, bound<uint8_t>(lo, hasMin, 0), bound<uint8_t>(hi, hasMax, UINT8_MAX));
166 break;
167 }
168#endif
169
170#ifdef CONFIG_DAWN_DTYPE_INT16
172 {
173 clamp<int16_t>(data,
174 outputData,
175 bound<int16_t>(lo, hasMin, INT16_MIN),
176 bound<int16_t>(hi, hasMax, INT16_MAX));
177 break;
178 }
179#endif
180
181#ifdef CONFIG_DAWN_DTYPE_UINT16
183 {
184 clamp<uint16_t>(data,
185 outputData,
186 bound<uint16_t>(lo, hasMin, 0),
187 bound<uint16_t>(hi, hasMax, UINT16_MAX));
188 break;
189 }
190#endif
191
192#ifdef CONFIG_DAWN_DTYPE_INT32
194 {
195 clamp<int32_t>(data,
196 outputData,
197 bound<int32_t>(lo, hasMin, INT32_MIN),
198 bound<int32_t>(hi, hasMax, INT32_MAX));
199 break;
200 }
201#endif
202
203#ifdef CONFIG_DAWN_DTYPE_UINT32
205 {
206 clamp<uint32_t>(data,
207 outputData,
208 bound<uint32_t>(lo, hasMin, 0),
209 bound<uint32_t>(hi, hasMax, UINT32_MAX));
210 break;
211 }
212#endif
213
214#ifdef CONFIG_DAWN_DTYPE_B16
216 {
217 // 16.16 fixed point is clamped as its raw int32 word
218
219 clamp<int32_t>(data,
220 outputData,
221 bound<int32_t>(lo, hasMin, INT32_MIN),
222 bound<int32_t>(hi, hasMax, INT32_MAX));
223 break;
224 }
225#endif
226
227#ifdef CONFIG_DAWN_DTYPE_FLOAT
229 {
230 clamp<float>(data,
231 outputData,
232 bound(lo, hasMin, std::numeric_limits<float>::lowest()),
233 bound(hi, hasMax, std::numeric_limits<float>::max()));
234 break;
235 }
236#endif
237
238#ifdef CONFIG_DAWN_DTYPE_DOUBLE
240 {
241 clamp<double>(data,
242 outputData,
243 bound(lo, hasMin, std::numeric_limits<double>::lowest()),
244 bound(hi, hasMax, std::numeric_limits<double>::max()));
245 break;
246 }
247#endif
248
249 default:
250 {
251 DAWNERR("saturate: unsupported dtype %d\n", data->getDtype());
252 return;
253 }
254 }
255
256 output->setData(*outputData);
257}
Descriptor wrapper for individual object configuration.
Base class for all I/O objects.
Definition common.hxx:30
int setData(IODataCmn &data, size_t offset=0)
Set data for I/O (public interface with stats tracking).
Definition common.hxx:421
@ PROG_SATURATE_CFG_MAX
Upper bound.
Definition saturate.hxx:30
@ PROG_SATURATE_CFG_MIN
Lower bound.
Definition saturate.hxx:29
void handle(CIOCommon *output, io_ddata_t *data, io_ddata_t *ioData, io_ddata_t *outputData, bool &initsample)
Process incoming sample.
Definition saturate.cxx:119
int onSetObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Pre-update hook for runtime configuration writes.
Definition saturate.cxx:73
int configureExtraCfgItem(const CDescObject &desc, const SObjectCfg::SObjectCfgItem *item, size_t &offset)
Parse program-specific config item (optional extension hook).
Definition saturate.cxx:37
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
static float cfgToF(ObjectCfgData_t x)
Convert ObjectCfgData_t to float.
static uint8_t objectCfgGetId(const ObjectCfgId objcfg)
Extract configuration identifier from ConfigID.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
Definition objectid.hxx:112
@ DTYPE_INT32
Signed 32-bit integer (-2147483648 to 2147483647).
Definition objectid.hxx:92
@ DTYPE_UINT8
Unsigned 8-bit integer (0 to 255).
Definition objectid.hxx:80
@ DTYPE_INT16
Signed 16-bit integer (-32768 to 32767).
Definition objectid.hxx:84
@ DTYPE_INT8
Signed 8-bit integer (-128 to 127).
Definition objectid.hxx:76
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
Definition objectid.hxx:88
@ DTYPE_DOUBLE
IEEE 754 double-precision floating point (64-bit).
Definition objectid.hxx:120
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
@ DTYPE_BOOL
Boolean data type (stored in 32-bit container).
Definition objectid.hxx:72
@ DTYPE_B16
Signed 16.16 fixed-point (32-bit).
Definition objectid.hxx:128
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
uint8_t getDtype() const
Get data type enum for introspection.
Definition ddata.hxx:217
size_t getItems()
Get number of items per batch.
Definition ddata.hxx:128
T & get(size_t index, size_t batch=0)
Get data element by index and batch (type-safe).
Definition ddata.hxx:93
size_t getBatch()
Get number of batches.
Definition ddata.hxx:135
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
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).