]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/logicsegment.cpp
decodetrace.cpp: Drop unused "using std::all_of".
[pulseview.git] / pv / data / logicsegment.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "config.h" // For HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
21
22#include <extdef.h>
23
24#include <cassert>
25#include <cmath>
26#include <cstdlib>
27#include <cstring>
28
29#include "logic.hpp"
30#include "logicsegment.hpp"
31
32#include <libsigrokcxx/libsigrokcxx.hpp>
33
34using std::lock_guard;
35using std::recursive_mutex;
36using std::max;
37using std::min;
38using std::shared_ptr;
39using std::vector;
40
41using sigrok::Logic;
42
43namespace pv {
44namespace data {
45
46const int LogicSegment::MipMapScalePower = 4;
47const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
48const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
49const uint64_t LogicSegment::MipMapDataUnit = 64 * 1024; // bytes
50
51LogicSegment::LogicSegment(pv::data::Logic& owner, uint32_t segment_id,
52 unsigned int unit_size, uint64_t samplerate) :
53 Segment(segment_id, samplerate, unit_size),
54 owner_(owner),
55 last_append_sample_(0)
56{
57 memset(mip_map_, 0, sizeof(mip_map_));
58}
59
60LogicSegment::~LogicSegment()
61{
62 lock_guard<recursive_mutex> lock(mutex_);
63 for (MipMapLevel &l : mip_map_)
64 free(l.data);
65}
66
67inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
68{
69#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
70 return *(uint64_t*)ptr;
71#else
72 uint64_t value = 0;
73 switch (unit_size_) {
74 default:
75 value |= ((uint64_t)ptr[7]) << 56;
76 /* FALLTHRU */
77 case 7:
78 value |= ((uint64_t)ptr[6]) << 48;
79 /* FALLTHRU */
80 case 6:
81 value |= ((uint64_t)ptr[5]) << 40;
82 /* FALLTHRU */
83 case 5:
84 value |= ((uint64_t)ptr[4]) << 32;
85 /* FALLTHRU */
86 case 4:
87 value |= ((uint32_t)ptr[3]) << 24;
88 /* FALLTHRU */
89 case 3:
90 value |= ((uint32_t)ptr[2]) << 16;
91 /* FALLTHRU */
92 case 2:
93 value |= ptr[1] << 8;
94 /* FALLTHRU */
95 case 1:
96 value |= ptr[0];
97 /* FALLTHRU */
98 case 0:
99 break;
100 }
101 return value;
102#endif
103}
104
105inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
106{
107#ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
108 *(uint64_t*)ptr = value;
109#else
110 switch (unit_size_) {
111 default:
112 ptr[7] = value >> 56;
113 /* FALLTHRU */
114 case 7:
115 ptr[6] = value >> 48;
116 /* FALLTHRU */
117 case 6:
118 ptr[5] = value >> 40;
119 /* FALLTHRU */
120 case 5:
121 ptr[4] = value >> 32;
122 /* FALLTHRU */
123 case 4:
124 ptr[3] = value >> 24;
125 /* FALLTHRU */
126 case 3:
127 ptr[2] = value >> 16;
128 /* FALLTHRU */
129 case 2:
130 ptr[1] = value >> 8;
131 /* FALLTHRU */
132 case 1:
133 ptr[0] = value;
134 /* FALLTHRU */
135 case 0:
136 break;
137 }
138#endif
139}
140
141void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
142{
143 assert(unit_size_ == logic->unit_size());
144 assert((logic->data_length() % unit_size_) == 0);
145
146 append_payload(logic->data_pointer(), logic->data_length());
147}
148
149void LogicSegment::append_payload(void *data, uint64_t data_size)
150{
151 assert((data_size % unit_size_) == 0);
152
153 lock_guard<recursive_mutex> lock(mutex_);
154
155 uint64_t prev_sample_count = sample_count_;
156 uint64_t sample_count = data_size / unit_size_;
157
158 append_samples(data, sample_count);
159
160 // Generate the first mip-map from the data
161 append_payload_to_mipmap();
162
163 if (sample_count > 1)
164 owner_.notify_samples_added(this, prev_sample_count + 1,
165 prev_sample_count + 1 + sample_count);
166 else
167 owner_.notify_samples_added(this, prev_sample_count + 1,
168 prev_sample_count + 1);
169}
170
171void LogicSegment::get_samples(int64_t start_sample,
172 int64_t end_sample, uint8_t* dest) const
173{
174 assert(start_sample >= 0);
175 assert(start_sample <= (int64_t)sample_count_);
176 assert(end_sample >= 0);
177 assert(end_sample <= (int64_t)sample_count_);
178 assert(start_sample <= end_sample);
179 assert(dest != nullptr);
180
181 lock_guard<recursive_mutex> lock(mutex_);
182
183 get_raw_samples(start_sample, (end_sample - start_sample), dest);
184}
185
186SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start)
187{
188 return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start);
189}
190
191void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase)
192{
193 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
194}
195
196void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it)
197{
198 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
199}
200
201void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
202{
203 lock_guard<recursive_mutex> lock(mutex_);
204
205 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
206 MipMapDataUnit) * MipMapDataUnit;
207
208 if (new_data_length > m.data_length) {
209 m.data_length = new_data_length;
210
211 // Padding is added to allow for the uint64_t write word
212 m.data = realloc(m.data, new_data_length * unit_size_ +
213 sizeof(uint64_t));
214 }
215}
216
217void LogicSegment::append_payload_to_mipmap()
218{
219 MipMapLevel &m0 = mip_map_[0];
220 uint64_t prev_length;
221 uint8_t *dest_ptr;
222 SegmentRawDataIterator* it;
223 uint64_t accumulator;
224 unsigned int diff_counter;
225
226 // Expand the data buffer to fit the new samples
227 prev_length = m0.length;
228 m0.length = sample_count_ / MipMapScaleFactor;
229
230 // Break off if there are no new samples to compute
231 if (m0.length == prev_length)
232 return;
233
234 reallocate_mipmap_level(m0);
235
236 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
237
238 // Iterate through the samples to populate the first level mipmap
239 uint64_t start_sample = prev_length * MipMapScaleFactor;
240 uint64_t end_sample = m0.length * MipMapScaleFactor;
241
242 it = begin_raw_sample_iteration(start_sample);
243 for (uint64_t i = start_sample; i < end_sample;) {
244 // Accumulate transitions which have occurred in this sample
245 accumulator = 0;
246 diff_counter = MipMapScaleFactor;
247 while (diff_counter-- > 0) {
248 const uint64_t sample = unpack_sample(it->value);
249 accumulator |= last_append_sample_ ^ sample;
250 last_append_sample_ = sample;
251 continue_raw_sample_iteration(it, 1);
252 i++;
253 }
254
255 pack_sample(dest_ptr, accumulator);
256 dest_ptr += unit_size_;
257 }
258 end_raw_sample_iteration(it);
259
260 // Compute higher level mipmaps
261 for (unsigned int level = 1; level < ScaleStepCount; level++) {
262 MipMapLevel &m = mip_map_[level];
263 const MipMapLevel &ml = mip_map_[level - 1];
264
265 // Expand the data buffer to fit the new samples
266 prev_length = m.length;
267 m.length = ml.length / MipMapScaleFactor;
268
269 // Break off if there are no more samples to be computed
270 if (m.length == prev_length)
271 break;
272
273 reallocate_mipmap_level(m);
274
275 // Subsample the lower level
276 const uint8_t* src_ptr = (uint8_t*)ml.data +
277 unit_size_ * prev_length * MipMapScaleFactor;
278 const uint8_t *const end_dest_ptr =
279 (uint8_t*)m.data + unit_size_ * m.length;
280
281 for (dest_ptr = (uint8_t*)m.data +
282 unit_size_ * prev_length;
283 dest_ptr < end_dest_ptr;
284 dest_ptr += unit_size_) {
285 accumulator = 0;
286 diff_counter = MipMapScaleFactor;
287 while (diff_counter-- > 0) {
288 accumulator |= unpack_sample(src_ptr);
289 src_ptr += unit_size_;
290 }
291
292 pack_sample(dest_ptr, accumulator);
293 }
294 }
295}
296
297uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
298{
299 assert(index < sample_count_);
300
301 assert(unit_size_ <= 8); // 8 * 8 = 64 channels
302 uint8_t data[8];
303
304 get_raw_samples(index, 1, data);
305 uint64_t sample = unpack_sample(data);
306
307 return sample;
308}
309
310void LogicSegment::get_subsampled_edges(
311 vector<EdgePair> &edges,
312 uint64_t start, uint64_t end,
313 float min_length, int sig_index)
314{
315 uint64_t index = start;
316 unsigned int level;
317 bool last_sample;
318 bool fast_forward;
319
320 assert(start <= end);
321 assert(min_length > 0);
322 assert(sig_index >= 0);
323 assert(sig_index < 64);
324
325 lock_guard<recursive_mutex> lock(mutex_);
326
327 // Make sure we only process as many samples as we have
328 if (end > get_sample_count())
329 end = get_sample_count();
330
331 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
332 const unsigned int min_level = max((int)floorf(logf(min_length) /
333 LogMipMapScaleFactor) - 1, 0);
334 const uint64_t sig_mask = 1ULL << sig_index;
335
336 // Store the initial state
337 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
338 edges.emplace_back(index++, last_sample);
339
340 while (index + block_length <= end) {
341 //----- Continue to search -----//
342 level = min_level;
343
344 // We cannot fast-forward if there is no mip-map data at
345 // the minimum level.
346 fast_forward = (mip_map_[level].data != nullptr);
347
348 if (min_length < MipMapScaleFactor) {
349 // Search individual samples up to the beginning of
350 // the next first level mip map block
351 const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
352
353 for (; index < final_index &&
354 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
355 index++) {
356
357 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
358
359 // If there was a change we cannot fast forward
360 if (sample != last_sample) {
361 fast_forward = false;
362 break;
363 }
364 }
365 } else {
366 // If resolution is less than a mip map block,
367 // round up to the beginning of the mip-map block
368 // for this level of detail
369 const int min_level_scale_power = (level + 1) * MipMapScalePower;
370 index = pow2_ceil(index, min_level_scale_power);
371 if (index >= end)
372 break;
373
374 // We can fast forward only if there was no change
375 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
376 if (last_sample != sample)
377 fast_forward = false;
378 }
379
380 if (fast_forward) {
381
382 // Fast forward: This involves zooming out to higher
383 // levels of the mip map searching for changes, then
384 // zooming in on them to find the point where the edge
385 // begins.
386
387 // Slide right and zoom out at the beginnings of mip-map
388 // blocks until we encounter a change
389 while (true) {
390 const int level_scale_power = (level + 1) * MipMapScalePower;
391 const uint64_t offset = index >> level_scale_power;
392
393 // Check if we reached the last block at this
394 // level, or if there was a change in this block
395 if (offset >= mip_map_[level].length ||
396 (get_subsample(level, offset) & sig_mask))
397 break;
398
399 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
400 // If we are now at the beginning of a
401 // higher level mip-map block ascend one
402 // level
403 if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
404 break;
405
406 level++;
407 } else {
408 // Slide right to the beginning of the
409 // next mip map block
410 index = pow2_ceil(index + 1, level_scale_power);
411 }
412 }
413
414 // Zoom in, and slide right until we encounter a change,
415 // and repeat until we reach min_level
416 while (true) {
417 assert(mip_map_[level].data);
418
419 const int level_scale_power = (level + 1) * MipMapScalePower;
420 const uint64_t offset = index >> level_scale_power;
421
422 // Check if we reached the last block at this
423 // level, or if there was a change in this block
424 if (offset >= mip_map_[level].length ||
425 (get_subsample(level, offset) & sig_mask)) {
426 // Zoom in unless we reached the minimum
427 // zoom
428 if (level == min_level)
429 break;
430
431 level--;
432 } else {
433 // Slide right to the beginning of the
434 // next mip map block
435 index = pow2_ceil(index + 1, level_scale_power);
436 }
437 }
438
439 // If individual samples within the limit of resolution,
440 // do a linear search for the next transition within the
441 // block
442 if (min_length < MipMapScaleFactor) {
443 for (; index < end; index++) {
444 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
445 if (sample != last_sample)
446 break;
447 }
448 }
449 }
450
451 //----- Store the edge -----//
452
453 // Take the last sample of the quanization block
454 const int64_t final_index = index + block_length;
455 if (index + block_length > end)
456 break;
457
458 // Store the final state
459 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
460 edges.emplace_back(index, final_sample);
461
462 index = final_index;
463 last_sample = final_sample;
464 }
465
466 // Add the final state
467 const bool end_sample = get_unpacked_sample(end) & sig_mask;
468 if (last_sample != end_sample)
469 edges.emplace_back(end, end_sample);
470 edges.emplace_back(end + 1, end_sample);
471}
472
473uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
474{
475 assert(level >= 0);
476 assert(mip_map_[level].data);
477 return unpack_sample((uint8_t*)mip_map_[level].data +
478 unit_size_ * offset);
479}
480
481uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
482{
483 const uint64_t p = 1 << power;
484 return (x + p - 1) / p * p;
485}
486
487} // namespace data
488} // namespace pv