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::reallocate_mipmap_level(MipMapLevel &m)
204 lock_guard<recursive_mutex> lock(mutex_);
206 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
207 MipMapDataUnit) * MipMapDataUnit;
209 if (new_data_length > m.data_length) {
210 m.data_length = new_data_length;
212 // Padding is added to allow for the uint64_t write word
213 m.data = realloc(m.data, new_data_length * unit_size_ +
218 void LogicSegment::append_payload_to_mipmap()
220 MipMapLevel &m0 = mip_map_[0];
221 uint64_t prev_length;
223 SegmentRawDataIterator* it;
224 uint64_t accumulator;
225 unsigned int diff_counter;
227 // Expand the data buffer to fit the new samples
228 prev_length = m0.length;
229 m0.length = sample_count_ / MipMapScaleFactor;
231 // Break off if there are no new samples to compute
232 if (m0.length == prev_length)
235 reallocate_mipmap_level(m0);
237 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
239 // Iterate through the samples to populate the first level mipmap
240 const uint64_t start_sample = prev_length * MipMapScaleFactor;
241 const uint64_t end_sample = m0.length * MipMapScaleFactor;
243 it = begin_raw_sample_iteration(start_sample);
244 for (uint64_t i = start_sample; i < end_sample;) {
245 // Accumulate transitions which have occurred in this sample
247 diff_counter = MipMapScaleFactor;
248 while (diff_counter-- > 0) {
249 const uint64_t sample = unpack_sample(it->value);
250 accumulator |= last_append_sample_ ^ sample;
251 last_append_sample_ = sample;
252 continue_raw_sample_iteration(it, 1);
256 pack_sample(dest_ptr, accumulator);
257 dest_ptr += unit_size_;
259 end_raw_sample_iteration(it);
261 // Compute higher level mipmaps
262 for (unsigned int level = 1; level < ScaleStepCount; level++) {
263 MipMapLevel &m = mip_map_[level];
264 const MipMapLevel &ml = mip_map_[level - 1];
266 // Expand the data buffer to fit the new samples
267 prev_length = m.length;
268 m.length = ml.length / MipMapScaleFactor;
270 // Break off if there are no more samples to be computed
271 if (m.length == prev_length)
274 reallocate_mipmap_level(m);
276 // Subsample the lower level
277 const uint8_t* src_ptr = (uint8_t*)ml.data +
278 unit_size_ * prev_length * MipMapScaleFactor;
279 const uint8_t *const end_dest_ptr =
280 (uint8_t*)m.data + unit_size_ * m.length;
282 for (dest_ptr = (uint8_t*)m.data +
283 unit_size_ * prev_length;
284 dest_ptr < end_dest_ptr;
285 dest_ptr += unit_size_) {
287 diff_counter = MipMapScaleFactor;
288 while (diff_counter-- > 0) {
289 accumulator |= unpack_sample(src_ptr);
290 src_ptr += unit_size_;
293 pack_sample(dest_ptr, accumulator);
298 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
300 assert(index < sample_count_);
302 assert(unit_size_ <= 8); // 8 * 8 = 64 channels
305 get_raw_samples(index, 1, data);
307 return unpack_sample(data);
310 void LogicSegment::get_subsampled_edges(
311 vector<EdgePair> &edges,
312 uint64_t start, uint64_t end,
313 float min_length, int sig_index)
315 uint64_t index = start;
320 assert(start <= end);
321 assert(min_length > 0);
322 assert(sig_index >= 0);
323 assert(sig_index < 64);
325 lock_guard<recursive_mutex> lock(mutex_);
327 // Make sure we only process as many samples as we have
328 if (end > get_sample_count())
329 end = get_sample_count();
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;
336 // Store the initial state
337 last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
338 edges.emplace_back(index++, last_sample);
340 while (index + block_length <= end) {
341 //----- Continue to search -----//
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);
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));
353 for (; index < final_index &&
354 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
357 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
359 // If there was a change we cannot fast forward
360 if (sample != last_sample) {
361 fast_forward = false;
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);
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;
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
387 // Slide right and zoom out at the beginnings of mip-map
388 // blocks until we encounter a change
390 const int level_scale_power = (level + 1) * MipMapScalePower;
391 const uint64_t offset = index >> level_scale_power;
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))
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
403 if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
408 // Slide right to the beginning of the
409 // next mip map block
410 index = pow2_ceil(index + 1, level_scale_power);
414 // Zoom in, and slide right until we encounter a change,
415 // and repeat until we reach min_level
417 assert(mip_map_[level].data);
419 const int level_scale_power = (level + 1) * MipMapScalePower;
420 const uint64_t offset = index >> level_scale_power;
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
428 if (level == min_level)
433 // Slide right to the beginning of the
434 // next mip map block
435 index = pow2_ceil(index + 1, level_scale_power);
439 // If individual samples within the limit of resolution,
440 // do a linear search for the next transition within the
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)
451 //----- Store the edge -----//
453 // Take the last sample of the quanization block
454 const int64_t final_index = index + block_length;
455 if (index + block_length > end)
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);
463 last_sample = final_sample;
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);
473 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
476 assert(mip_map_[level].data);
477 return unpack_sample((uint8_t*)mip_map_[level].data +
478 unit_size_ * offset);
481 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
483 const uint64_t p = UINT64_C(1) << power;
484 return (x + p - 1) / p * p;