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),
57 last_append_accumulator_(0),
60 memset(mip_map_, 0, sizeof(mip_map_));
63 LogicSegment::~LogicSegment()
65 lock_guard<recursive_mutex> lock(mutex_);
66 for (MipMapLevel &l : mip_map_)
71 void LogicSegment::downsampleTmain(const T*&in, T &acc, T &prev)
73 // Accumulate one sample at a time
74 for (uint64_t i = 0; i < MipMapScaleFactor; i++) {
82 void LogicSegment::downsampleTmain<uint8_t>(const uint8_t*&in, uint8_t &acc, uint8_t &prev)
84 // Handle 8 bit samples in 32 bit steps
85 uint32_t prev32 = prev | prev << 8 | prev << 16 | prev << 24;
87 const uint32_t *in32 = (const uint32_t*)in;
88 for (uint64_t i = 0; i < MipMapScaleFactor; i += 4) {
89 uint32_t sample32 = *in32++;
90 acc32 |= prev32 ^ sample32;
93 // Reduce result back to uint8_t
94 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
95 prev = (prev32 >> 24) & 0xff; // MSB is last
96 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
97 prev = prev32 & 0xff; // LSB is last
99 #error Endianness unknown
102 acc |= (acc32 >> 8) & 0xff;
103 acc |= (acc32 >> 16) & 0xff;
104 acc |= (acc32 >> 24) & 0xff;
105 in = (const uint8_t*)in32;
109 void LogicSegment::downsampleTmain<uint16_t>(const uint16_t*&in, uint16_t &acc, uint16_t &prev)
111 // Handle 16 bit samples in 32 bit steps
112 uint32_t prev32 = prev | prev << 16;
113 uint32_t acc32 = acc;
114 const uint32_t *in32 = (const uint32_t*)in;
115 for (uint64_t i = 0; i < MipMapScaleFactor; i += 2) {
116 uint32_t sample32 = *in32++;
117 acc32 |= prev32 ^ sample32;
120 // Reduce result back to uint16_t
121 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
122 prev = (prev32 >> 16) & 0xffff; // MSB is last
123 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
124 prev = prev32 & 0xffff; // LSB is last
126 #error Endian unknown
128 acc |= acc32 & 0xffff;
129 acc |= (acc32 >> 16) & 0xffff;
130 in = (const uint16_t*)in32;
134 void LogicSegment::downsampleT(const uint8_t *in_, uint8_t *&out_, uint64_t len)
136 const T *in = (const T*)in_;
138 T prev = last_append_sample_;
139 T acc = last_append_accumulator_;
141 // Try to complete the previous downsample
142 if (last_append_extra_) {
143 while (last_append_extra_ < MipMapScaleFactor && len > 0) {
145 acc |= prev ^ sample;
147 last_append_extra_++;
151 // Not enough samples available to complete downsample
152 last_append_sample_ = prev;
153 last_append_accumulator_ = acc;
156 // We have a complete downsample
159 last_append_extra_ = 0;
162 // Handle complete blocks of MipMapScaleFactor samples
163 while (len >= MipMapScaleFactor) {
164 downsampleTmain<T>(in, acc, prev);
165 len -= MipMapScaleFactor;
171 // Process remainder, not enough for a complete sample
174 acc |= prev ^ sample;
176 last_append_extra_++;
181 last_append_sample_ = prev;
182 last_append_accumulator_ = acc;
183 out_ = (uint8_t *)out;
186 void LogicSegment::downsampleGeneric(const uint8_t *in, uint8_t *&out, uint64_t len)
188 // Downsample using the generic unpack_sample()
189 // which can handle any width between 1 and 8 bytes
190 uint64_t prev = last_append_sample_;
191 uint64_t acc = last_append_accumulator_;
193 // Try to complete the previous downsample
194 if (last_append_extra_) {
195 while (last_append_extra_ < MipMapScaleFactor && len > 0) {
196 const uint64_t sample = unpack_sample(in);
198 acc |= prev ^ sample;
200 last_append_extra_++;
204 // Not enough samples available to complete downsample
205 last_append_sample_ = prev;
206 last_append_accumulator_ = acc;
209 // We have a complete downsample
210 pack_sample(out, acc);
213 last_append_extra_ = 0;
216 // Handle complete blocks of MipMapScaleFactor samples
217 while (len >= MipMapScaleFactor) {
218 // Accumulate one sample at a time
219 for (uint64_t i = 0; i < MipMapScaleFactor; i++) {
220 const uint64_t sample = unpack_sample(in);
222 acc |= prev ^ sample;
225 len -= MipMapScaleFactor;
227 pack_sample(out, acc);
232 // Process remainder, not enough for a complete sample
234 const uint64_t sample = unpack_sample(in);
236 acc |= prev ^ sample;
238 last_append_extra_++;
243 last_append_sample_ = prev;
244 last_append_accumulator_ = acc;
247 inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
249 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
250 return *(uint64_t*)ptr;
253 switch (unit_size_) {
255 value |= ((uint64_t)ptr[7]) << 56;
258 value |= ((uint64_t)ptr[6]) << 48;
261 value |= ((uint64_t)ptr[5]) << 40;
264 value |= ((uint64_t)ptr[4]) << 32;
267 value |= ((uint32_t)ptr[3]) << 24;
270 value |= ((uint32_t)ptr[2]) << 16;
273 value |= ptr[1] << 8;
285 inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
287 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
288 *(uint64_t*)ptr = value;
290 switch (unit_size_) {
292 ptr[7] = value >> 56;
295 ptr[6] = value >> 48;
298 ptr[5] = value >> 40;
301 ptr[4] = value >> 32;
304 ptr[3] = value >> 24;
307 ptr[2] = value >> 16;
321 void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
323 assert(unit_size_ == logic->unit_size());
324 assert((logic->data_length() % unit_size_) == 0);
326 append_payload(logic->data_pointer(), logic->data_length());
329 void LogicSegment::append_payload(void *data, uint64_t data_size)
331 assert((data_size % unit_size_) == 0);
333 lock_guard<recursive_mutex> lock(mutex_);
335 const uint64_t prev_sample_count = sample_count_;
336 const uint64_t sample_count = data_size / unit_size_;
338 append_samples(data, sample_count);
340 // Generate the first mip-map from the data
341 append_payload_to_mipmap();
343 if (sample_count > 1)
344 owner_.notify_samples_added(this, prev_sample_count + 1,
345 prev_sample_count + 1 + sample_count);
347 owner_.notify_samples_added(this, prev_sample_count + 1,
348 prev_sample_count + 1);
351 void LogicSegment::get_samples(int64_t start_sample,
352 int64_t end_sample, uint8_t* dest) const
354 assert(start_sample >= 0);
355 assert(start_sample <= (int64_t)sample_count_);
356 assert(end_sample >= 0);
357 assert(end_sample <= (int64_t)sample_count_);
358 assert(start_sample <= end_sample);
359 assert(dest != nullptr);
361 lock_guard<recursive_mutex> lock(mutex_);
363 get_raw_samples(start_sample, (end_sample - start_sample), dest);
366 void LogicSegment::get_subsampled_edges(
367 vector<EdgePair> &edges,
368 uint64_t start, uint64_t end,
369 float min_length, int sig_index, bool first_change_only)
371 uint64_t index = start;
376 assert(start <= end);
377 assert(min_length > 0);
378 assert(sig_index >= 0);
379 assert(sig_index < 64);
381 lock_guard<recursive_mutex> lock(mutex_);
383 // Make sure we only process as many samples as we have
384 if (end > get_sample_count())
385 end = get_sample_count();
387 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
388 const unsigned int min_level = max((int)floorf(logf(min_length) /
389 LogMipMapScaleFactor) - 1, 0);
390 const uint64_t sig_mask = 1ULL << sig_index;
392 // Store the initial state
393 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
394 if (!first_change_only)
395 edges.emplace_back(index++, last_sample);
397 while (index + block_length <= end) {
398 //----- Continue to search -----//
401 // We cannot fast-forward if there is no mip-map data at
402 // the minimum level.
403 fast_forward = (mip_map_[level].data != nullptr);
405 if (min_length < MipMapScaleFactor) {
406 // Search individual samples up to the beginning of
407 // the next first level mip map block
408 const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
410 for (; index < final_index &&
411 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
414 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
416 // If there was a change we cannot fast forward
417 if (sample != last_sample) {
418 fast_forward = false;
423 // If resolution is less than a mip map block,
424 // round up to the beginning of the mip-map block
425 // for this level of detail
426 const int min_level_scale_power = (level + 1) * MipMapScalePower;
427 index = pow2_ceil(index, min_level_scale_power);
431 // We can fast forward only if there was no change
432 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
433 if (last_sample != sample)
434 fast_forward = false;
439 // Fast forward: This involves zooming out to higher
440 // levels of the mip map searching for changes, then
441 // zooming in on them to find the point where the edge
444 // Slide right and zoom out at the beginnings of mip-map
445 // blocks until we encounter a change
447 const int level_scale_power = (level + 1) * MipMapScalePower;
448 const uint64_t offset = index >> level_scale_power;
450 // Check if we reached the last block at this
451 // level, or if there was a change in this block
452 if (offset >= mip_map_[level].length ||
453 (get_subsample(level, offset) & sig_mask))
456 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
457 // If we are now at the beginning of a
458 // higher level mip-map block ascend one
460 if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
465 // Slide right to the beginning of the
466 // next mip map block
467 index = pow2_ceil(index + 1, level_scale_power);
471 // Zoom in, and slide right until we encounter a change,
472 // and repeat until we reach min_level
474 assert(mip_map_[level].data);
476 const int level_scale_power = (level + 1) * MipMapScalePower;
477 const uint64_t offset = index >> level_scale_power;
479 // Check if we reached the last block at this
480 // level, or if there was a change in this block
481 if (offset >= mip_map_[level].length ||
482 (get_subsample(level, offset) & sig_mask)) {
483 // Zoom in unless we reached the minimum
485 if (level == min_level)
490 // Slide right to the beginning of the
491 // next mip map block
492 index = pow2_ceil(index + 1, level_scale_power);
496 // If individual samples within the limit of resolution,
497 // do a linear search for the next transition within the
499 if (min_length < MipMapScaleFactor) {
500 for (; index < end; index++) {
501 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
502 if (sample != last_sample)
508 //----- Store the edge -----//
510 // Take the last sample of the quanization block
511 const int64_t final_index = index + block_length;
512 if (index + block_length > end)
515 // Store the final state
516 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
517 edges.emplace_back(index, final_sample);
520 last_sample = final_sample;
522 if (first_change_only)
526 // Add the final state
527 if (!first_change_only) {
528 const bool end_sample = get_unpacked_sample(end) & sig_mask;
529 if (last_sample != end_sample)
530 edges.emplace_back(end, end_sample);
531 edges.emplace_back(end + 1, end_sample);
535 void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
536 uint64_t origin_sample, float min_length, int sig_index)
538 if (origin_sample >= sample_count_)
541 // Put the edges vector on the heap, it can become quite big until we can
542 // use a get_subsampled_edges() implementation that searches backwards
543 vector<EdgePair>* edges = new vector<EdgePair>;
545 // Get all edges to the left of origin_sample
546 get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
548 // If we don't specify "first only", the first and last edge are the states
549 // at samples 0 and origin_sample. If only those exist, there are no edges
550 if (edges->size() == 2) {
555 // Dismiss the entry for origin_sample so that back() gives us the
558 dest.push_back(edges->back());
561 // Get first edge to the right of origin_sample
562 get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
564 // "first only" is specified, so nothing needs to be dismissed
565 if (edges->size() == 0) {
570 dest.push_back(edges->front());
575 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
577 lock_guard<recursive_mutex> lock(mutex_);
579 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
580 MipMapDataUnit) * MipMapDataUnit;
582 if (new_data_length > m.data_length) {
583 m.data_length = new_data_length;
585 // Padding is added to allow for the uint64_t write word
586 m.data = realloc(m.data, new_data_length * unit_size_ +
591 void LogicSegment::append_payload_to_mipmap()
593 MipMapLevel &m0 = mip_map_[0];
594 uint64_t prev_length;
596 SegmentDataIterator* it;
597 uint64_t accumulator;
598 unsigned int diff_counter;
600 // Expand the data buffer to fit the new samples
601 prev_length = m0.length;
602 m0.length = sample_count_ / MipMapScaleFactor;
604 // Break off if there are no new samples to compute
605 if (m0.length == prev_length)
608 reallocate_mipmap_level(m0);
610 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
612 // Iterate through the samples to populate the first level mipmap
613 const uint64_t start_sample = prev_length * MipMapScaleFactor;
614 const uint64_t end_sample = m0.length * MipMapScaleFactor;
615 uint64_t len_sample = end_sample - start_sample;
616 it = begin_sample_iteration(start_sample);
617 while (len_sample > 0) {
618 // Number of samples available in this chunk
619 uint64_t count = get_iterator_valid_length(it);
620 // Reduce if less than asked for
621 count = std::min(count, len_sample);
622 uint8_t *src_ptr = get_iterator_value(it);
623 // Submit these contiguous samples to downsampling in bulk
625 downsampleT<uint8_t>(src_ptr, dest_ptr, count);
626 else if (unit_size_ == 2)
627 downsampleT<uint16_t>(src_ptr, dest_ptr, count);
628 else if (unit_size_ == 4)
629 downsampleT<uint32_t>(src_ptr, dest_ptr, count);
630 else if (unit_size_ == 8)
631 downsampleT<uint8_t>(src_ptr, dest_ptr, count);
633 downsampleGeneric(src_ptr, dest_ptr, count);
635 // Advance iterator, should move to start of next chunk
636 continue_sample_iteration(it, count);
638 end_sample_iteration(it);
640 // Compute higher level mipmaps
641 for (unsigned int level = 1; level < ScaleStepCount; level++) {
642 MipMapLevel &m = mip_map_[level];
643 const MipMapLevel &ml = mip_map_[level - 1];
645 // Expand the data buffer to fit the new samples
646 prev_length = m.length;
647 m.length = ml.length / MipMapScaleFactor;
649 // Break off if there are no more samples to be computed
650 if (m.length == prev_length)
653 reallocate_mipmap_level(m);
655 // Subsample the lower level
656 const uint8_t* src_ptr = (uint8_t*)ml.data +
657 unit_size_ * prev_length * MipMapScaleFactor;
658 const uint8_t *const end_dest_ptr =
659 (uint8_t*)m.data + unit_size_ * m.length;
661 for (dest_ptr = (uint8_t*)m.data +
662 unit_size_ * prev_length;
663 dest_ptr < end_dest_ptr;
664 dest_ptr += unit_size_) {
666 diff_counter = MipMapScaleFactor;
667 while (diff_counter-- > 0) {
668 accumulator |= unpack_sample(src_ptr);
669 src_ptr += unit_size_;
672 pack_sample(dest_ptr, accumulator);
677 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
679 assert(index < sample_count_);
681 assert(unit_size_ <= 8); // 8 * 8 = 64 channels
684 get_raw_samples(index, 1, data);
686 return unpack_sample(data);
689 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
692 assert(mip_map_[level].data);
693 return unpack_sample((uint8_t*)mip_map_[level].data +
694 unit_size_ * offset);
697 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
699 const uint64_t p = UINT64_C(1) << power;
700 return (x + p - 1) / p * p;