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