2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 #include "config.h" // For HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
31 #include "logicsegment.hpp"
33 #include <libsigrokcxx/libsigrokcxx.hpp>
35 using std::lock_guard;
36 using std::recursive_mutex;
39 using std::shared_ptr;
47 const int LogicSegment::MipMapScalePower = 4;
48 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
49 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
50 const uint64_t LogicSegment::MipMapDataUnit = 64 * 1024; // bytes
52 LogicSegment::LogicSegment(pv::data::Logic& owner, uint32_t segment_id,
53 unsigned int unit_size, uint64_t samplerate) :
54 Segment(segment_id, samplerate, unit_size),
56 last_append_sample_(0)
58 memset(mip_map_, 0, sizeof(mip_map_));
61 LogicSegment::~LogicSegment()
63 lock_guard<recursive_mutex> lock(mutex_);
64 for (MipMapLevel &l : mip_map_)
68 inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
70 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
71 return *(uint64_t*)ptr;
76 value |= ((uint64_t)ptr[7]) << 56;
79 value |= ((uint64_t)ptr[6]) << 48;
82 value |= ((uint64_t)ptr[5]) << 40;
85 value |= ((uint64_t)ptr[4]) << 32;
88 value |= ((uint32_t)ptr[3]) << 24;
91 value |= ((uint32_t)ptr[2]) << 16;
106 inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
108 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
109 *(uint64_t*)ptr = value;
111 switch (unit_size_) {
113 ptr[7] = value >> 56;
116 ptr[6] = value >> 48;
119 ptr[5] = value >> 40;
122 ptr[4] = value >> 32;
125 ptr[3] = value >> 24;
128 ptr[2] = value >> 16;
142 void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
144 assert(unit_size_ == logic->unit_size());
145 assert((logic->data_length() % unit_size_) == 0);
147 append_payload(logic->data_pointer(), logic->data_length());
150 void LogicSegment::append_payload(void *data, uint64_t data_size)
152 assert((data_size % unit_size_) == 0);
154 lock_guard<recursive_mutex> lock(mutex_);
156 const uint64_t prev_sample_count = sample_count_;
157 const uint64_t sample_count = data_size / unit_size_;
159 append_samples(data, sample_count);
161 // Generate the first mip-map from the data
162 append_payload_to_mipmap();
164 if (sample_count > 1)
165 owner_.notify_samples_added(this, prev_sample_count + 1,
166 prev_sample_count + 1 + sample_count);
168 owner_.notify_samples_added(this, prev_sample_count + 1,
169 prev_sample_count + 1);
172 void LogicSegment::get_samples(int64_t start_sample,
173 int64_t end_sample, uint8_t* dest) const
175 assert(start_sample >= 0);
176 assert(start_sample <= (int64_t)sample_count_);
177 assert(end_sample >= 0);
178 assert(end_sample <= (int64_t)sample_count_);
179 assert(start_sample <= end_sample);
180 assert(dest != nullptr);
182 lock_guard<recursive_mutex> lock(mutex_);
184 get_raw_samples(start_sample, (end_sample - start_sample), dest);
187 SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start)
189 return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start);
192 void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase)
194 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
197 void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it)
199 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
202 void LogicSegment::get_subsampled_edges(
203 vector<EdgePair> &edges,
204 uint64_t start, uint64_t end,
205 float min_length, int sig_index, bool first_change_only)
207 uint64_t index = start;
212 assert(start <= end);
213 assert(min_length > 0);
214 assert(sig_index >= 0);
215 assert(sig_index < 64);
217 lock_guard<recursive_mutex> lock(mutex_);
219 // Make sure we only process as many samples as we have
220 if (end > get_sample_count())
221 end = get_sample_count();
223 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
224 const unsigned int min_level = max((int)floorf(logf(min_length) /
225 LogMipMapScaleFactor) - 1, 0);
226 const uint64_t sig_mask = 1ULL << sig_index;
228 // Store the initial state
229 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
230 if (!first_change_only)
231 edges.emplace_back(index++, last_sample);
233 while (index + block_length <= end) {
234 //----- Continue to search -----//
237 // We cannot fast-forward if there is no mip-map data at
238 // the minimum level.
239 fast_forward = (mip_map_[level].data != nullptr);
241 if (min_length < MipMapScaleFactor) {
242 // Search individual samples up to the beginning of
243 // the next first level mip map block
244 const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
246 for (; index < final_index &&
247 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
250 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
252 // If there was a change we cannot fast forward
253 if (sample != last_sample) {
254 fast_forward = false;
259 // If resolution is less than a mip map block,
260 // round up to the beginning of the mip-map block
261 // for this level of detail
262 const int min_level_scale_power = (level + 1) * MipMapScalePower;
263 index = pow2_ceil(index, min_level_scale_power);
267 // We can fast forward only if there was no change
268 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
269 if (last_sample != sample)
270 fast_forward = false;
275 // Fast forward: This involves zooming out to higher
276 // levels of the mip map searching for changes, then
277 // zooming in on them to find the point where the edge
280 // Slide right and zoom out at the beginnings of mip-map
281 // blocks until we encounter a change
283 const int level_scale_power = (level + 1) * MipMapScalePower;
284 const uint64_t offset = index >> level_scale_power;
286 // Check if we reached the last block at this
287 // level, or if there was a change in this block
288 if (offset >= mip_map_[level].length ||
289 (get_subsample(level, offset) & sig_mask))
292 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
293 // If we are now at the beginning of a
294 // higher level mip-map block ascend one
296 if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
301 // Slide right to the beginning of the
302 // next mip map block
303 index = pow2_ceil(index + 1, level_scale_power);
307 // Zoom in, and slide right until we encounter a change,
308 // and repeat until we reach min_level
310 assert(mip_map_[level].data);
312 const int level_scale_power = (level + 1) * MipMapScalePower;
313 const uint64_t offset = index >> level_scale_power;
315 // Check if we reached the last block at this
316 // level, or if there was a change in this block
317 if (offset >= mip_map_[level].length ||
318 (get_subsample(level, offset) & sig_mask)) {
319 // Zoom in unless we reached the minimum
321 if (level == min_level)
326 // Slide right to the beginning of the
327 // next mip map block
328 index = pow2_ceil(index + 1, level_scale_power);
332 // If individual samples within the limit of resolution,
333 // do a linear search for the next transition within the
335 if (min_length < MipMapScaleFactor) {
336 for (; index < end; index++) {
337 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
338 if (sample != last_sample)
344 //----- Store the edge -----//
346 // Take the last sample of the quanization block
347 const int64_t final_index = index + block_length;
348 if (index + block_length > end)
351 // Store the final state
352 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
353 edges.emplace_back(index, final_sample);
356 last_sample = final_sample;
358 if (first_change_only)
362 // Add the final state
363 if (!first_change_only) {
364 const bool end_sample = get_unpacked_sample(end) & sig_mask;
365 if (last_sample != end_sample)
366 edges.emplace_back(end, end_sample);
367 edges.emplace_back(end + 1, end_sample);
371 void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
372 uint64_t origin_sample, float min_length, int sig_index)
374 // Put the edges vector on the heap, it can become quite big until we can
375 // use a get_subsampled_edges() implementation that searches backwards
376 vector<EdgePair>* edges = new vector<EdgePair>;
378 get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
380 // If we don't specify "first only", the first and last edge are the states
381 // at samples 0 and origin_sample. If only those exist, there are no edges
382 if (edges->size() == 2) {
387 // Dismiss the entry for origin_sample so that back() gives us the
390 dest.push_back(edges->back());
393 get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
395 // "first only" is specified, so nothing needs to be dismissed
396 if (edges->size() == 0) {
401 dest.push_back(edges->front());
406 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
408 lock_guard<recursive_mutex> lock(mutex_);
410 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
411 MipMapDataUnit) * MipMapDataUnit;
413 if (new_data_length > m.data_length) {
414 m.data_length = new_data_length;
416 // Padding is added to allow for the uint64_t write word
417 m.data = realloc(m.data, new_data_length * unit_size_ +
422 void LogicSegment::append_payload_to_mipmap()
424 MipMapLevel &m0 = mip_map_[0];
425 uint64_t prev_length;
427 SegmentRawDataIterator* it;
428 uint64_t accumulator;
429 unsigned int diff_counter;
431 // Expand the data buffer to fit the new samples
432 prev_length = m0.length;
433 m0.length = sample_count_ / MipMapScaleFactor;
435 // Break off if there are no new samples to compute
436 if (m0.length == prev_length)
439 reallocate_mipmap_level(m0);
441 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
443 // Iterate through the samples to populate the first level mipmap
444 const uint64_t start_sample = prev_length * MipMapScaleFactor;
445 const uint64_t end_sample = m0.length * MipMapScaleFactor;
447 it = begin_raw_sample_iteration(start_sample);
448 for (uint64_t i = start_sample; i < end_sample;) {
449 // Accumulate transitions which have occurred in this sample
451 diff_counter = MipMapScaleFactor;
452 while (diff_counter-- > 0) {
453 const uint64_t sample = unpack_sample(it->value);
454 accumulator |= last_append_sample_ ^ sample;
455 last_append_sample_ = sample;
456 continue_raw_sample_iteration(it, 1);
460 pack_sample(dest_ptr, accumulator);
461 dest_ptr += unit_size_;
463 end_raw_sample_iteration(it);
465 // Compute higher level mipmaps
466 for (unsigned int level = 1; level < ScaleStepCount; level++) {
467 MipMapLevel &m = mip_map_[level];
468 const MipMapLevel &ml = mip_map_[level - 1];
470 // Expand the data buffer to fit the new samples
471 prev_length = m.length;
472 m.length = ml.length / MipMapScaleFactor;
474 // Break off if there are no more samples to be computed
475 if (m.length == prev_length)
478 reallocate_mipmap_level(m);
480 // Subsample the lower level
481 const uint8_t* src_ptr = (uint8_t*)ml.data +
482 unit_size_ * prev_length * MipMapScaleFactor;
483 const uint8_t *const end_dest_ptr =
484 (uint8_t*)m.data + unit_size_ * m.length;
486 for (dest_ptr = (uint8_t*)m.data +
487 unit_size_ * prev_length;
488 dest_ptr < end_dest_ptr;
489 dest_ptr += unit_size_) {
491 diff_counter = MipMapScaleFactor;
492 while (diff_counter-- > 0) {
493 accumulator |= unpack_sample(src_ptr);
494 src_ptr += unit_size_;
497 pack_sample(dest_ptr, accumulator);
502 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
504 assert(index < sample_count_);
506 assert(unit_size_ <= 8); // 8 * 8 = 64 channels
509 get_raw_samples(index, 1, data);
511 return unpack_sample(data);
514 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
517 assert(mip_map_[level].data);
518 return unpack_sample((uint8_t*)mip_map_[level].data +
519 unit_size_ * offset);
522 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
524 const uint64_t p = UINT64_C(1) << power;
525 return (x + p - 1) / p * p;