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