]> sigrok.org Git - pulseview.git/blame - pv/data/logicsegment.cpp
Fix clang warning: shifting a negative signed value is undefined
[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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
cef18fc6 21#include <extdef.h>
28a4c9c5 22
f556bc6a 23#include <assert.h>
4ceab49a 24#include <string.h>
b36d8550 25#include <stdlib.h>
d9e71737 26#include <cmath>
4ceab49a 27
f3d66e52 28#include "logicsegment.hpp"
f556bc6a 29
fe3a1c21 30#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 31
3b68d03d
JH
32using std::lock_guard;
33using std::recursive_mutex;
819f4c25
JH
34using std::max;
35using std::min;
36using std::pair;
e8d00928
ML
37using std::shared_ptr;
38
39using sigrok::Logic;
28a4c9c5 40
51e77110 41namespace pv {
1b1ec774 42namespace data {
51e77110 43
f3d66e52
JH
44const int LogicSegment::MipMapScalePower = 4;
45const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
46const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
47const uint64_t LogicSegment::MipMapDataUnit = 64*1024; // bytes
4ceab49a 48
f3d66e52 49LogicSegment::LogicSegment(shared_ptr<Logic> logic, uint64_t samplerate,
3d79f521 50 const uint64_t expected_num_samples) :
f3d66e52 51 Segment(samplerate, logic->unit_size()),
8dbbc7f0 52 last_append_sample_(0)
f556bc6a 53{
27d7c96b
DK
54 set_capacity(expected_num_samples);
55
8dbbc7f0
JH
56 lock_guard<recursive_mutex> lock(mutex_);
57 memset(mip_map_, 0, sizeof(mip_map_));
f556bc6a
JH
58 append_payload(logic);
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
f3d66e52 68uint64_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
f3d66e52 106void 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
f3d66e52 142void LogicSegment::append_payload(shared_ptr<Logic> logic)
28a4c9c5 143{
8dbbc7f0
JH
144 assert(unit_size_ == logic->unit_size());
145 assert((logic->data_length() % unit_size_) == 0);
f556bc6a 146
8dbbc7f0 147 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 148
e8d00928 149 append_data(logic->data_pointer(),
8dbbc7f0 150 logic->data_length() / unit_size_);
4ceab49a
JH
151
152 // Generate the first mip-map from the data
153 append_payload_to_mipmap();
154}
155
f3d66e52 156void LogicSegment::get_samples(uint8_t *const data,
ed990f11
JH
157 int64_t start_sample, int64_t end_sample) const
158{
159 assert(data);
160 assert(start_sample >= 0);
8dbbc7f0 161 assert(start_sample <= (int64_t)sample_count_);
ed990f11 162 assert(end_sample >= 0);
8dbbc7f0 163 assert(end_sample <= (int64_t)sample_count_);
ed990f11
JH
164 assert(start_sample <= end_sample);
165
8dbbc7f0 166 lock_guard<recursive_mutex> lock(mutex_);
ed990f11 167
8dbbc7f0
JH
168 const size_t size = (end_sample - start_sample) * unit_size_;
169 memcpy(data, (const uint8_t*)data_.data() + start_sample * unit_size_, size);
ed990f11
JH
170}
171
f3d66e52 172void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
4ceab49a
JH
173{
174 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
175 MipMapDataUnit) * MipMapDataUnit;
2ad82c2e 176 if (new_data_length > m.data_length) {
4ceab49a 177 m.data_length = new_data_length;
8fe60279
JH
178
179 // Padding is added to allow for the uint64_t write word
8dbbc7f0 180 m.data = realloc(m.data, new_data_length * unit_size_ +
8fe60279 181 sizeof(uint64_t));
4ceab49a
JH
182 }
183}
184
f3d66e52 185void LogicSegment::append_payload_to_mipmap()
4ceab49a 186{
8dbbc7f0 187 MipMapLevel &m0 = mip_map_[0];
4ceab49a
JH
188 uint64_t prev_length;
189 const uint8_t *src_ptr;
190 uint8_t *dest_ptr;
191 uint64_t accumulator;
192 unsigned int diff_counter;
193
194 // Expand the data buffer to fit the new samples
195 prev_length = m0.length;
8dbbc7f0 196 m0.length = sample_count_ / MipMapScaleFactor;
4ceab49a
JH
197
198 // Break off if there are no new samples to compute
333d5bbc 199 if (m0.length == prev_length)
4ceab49a
JH
200 return;
201
023887ca 202 reallocate_mipmap_level(m0);
4ceab49a 203
8dbbc7f0 204 dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
4ceab49a
JH
205
206 // Iterate through the samples to populate the first level mipmap
8dbbc7f0
JH
207 const uint8_t *const end_src_ptr = (uint8_t*)data_.data() +
208 m0.length * unit_size_ * MipMapScaleFactor;
209 for (src_ptr = (uint8_t*)data_.data() +
2ad82c2e
UH
210 prev_length * unit_size_ * MipMapScaleFactor;
211 src_ptr < end_src_ptr;) {
4ceab49a
JH
212 // Accumulate transitions which have occurred in this sample
213 accumulator = 0;
214 diff_counter = MipMapScaleFactor;
2ad82c2e 215 while (diff_counter-- > 0) {
8cb71705 216 const uint64_t sample = unpack_sample(src_ptr);
8dbbc7f0
JH
217 accumulator |= last_append_sample_ ^ sample;
218 last_append_sample_ = sample;
219 src_ptr += unit_size_;
4ceab49a
JH
220 }
221
8cb71705 222 pack_sample(dest_ptr, accumulator);
8dbbc7f0 223 dest_ptr += unit_size_;
4ceab49a
JH
224 }
225
226 // Compute higher level mipmaps
2ad82c2e 227 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0
JH
228 MipMapLevel &m = mip_map_[level];
229 const MipMapLevel &ml = mip_map_[level-1];
4ceab49a
JH
230
231 // Expand the data buffer to fit the new samples
232 prev_length = m.length;
233 m.length = ml.length / MipMapScaleFactor;
234
235 // Break off if there are no more samples to computed
333d5bbc 236 if (m.length == prev_length)
4ceab49a
JH
237 break;
238
023887ca 239 reallocate_mipmap_level(m);
4ceab49a
JH
240
241 // Subsample the level lower level
242 src_ptr = (uint8_t*)ml.data +
8dbbc7f0 243 unit_size_ * prev_length * MipMapScaleFactor;
023887ca 244 const uint8_t *const end_dest_ptr =
8dbbc7f0 245 (uint8_t*)m.data + unit_size_ * m.length;
333d5bbc 246 for (dest_ptr = (uint8_t*)m.data +
2ad82c2e
UH
247 unit_size_ * prev_length;
248 dest_ptr < end_dest_ptr;
249 dest_ptr += unit_size_) {
4ceab49a
JH
250 accumulator = 0;
251 diff_counter = MipMapScaleFactor;
2ad82c2e 252 while (diff_counter-- > 0) {
8cb71705 253 accumulator |= unpack_sample(src_ptr);
8dbbc7f0 254 src_ptr += unit_size_;
4ceab49a
JH
255 }
256
8cb71705 257 pack_sample(dest_ptr, accumulator);
4ceab49a
JH
258 }
259 }
28a4c9c5 260}
2858b391 261
f3d66e52 262uint64_t LogicSegment::get_sample(uint64_t index) const
2858b391 263{
8dbbc7f0 264 assert(index < sample_count_);
2858b391 265
8dbbc7f0 266 return unpack_sample((uint8_t*)data_.data() + index * unit_size_);
2858b391
JH
267}
268
f3d66e52 269void LogicSegment::get_subsampled_edges(
2858b391 270 std::vector<EdgePair> &edges,
60b0c2da 271 uint64_t start, uint64_t end,
0b02e057 272 float min_length, int sig_index)
2858b391 273{
60b0c2da
JH
274 uint64_t index = start;
275 unsigned int level;
7d0d64f9
JH
276 bool last_sample;
277 bool fast_forward;
0b02e057 278
0b02e057 279 assert(end <= get_sample_count());
2858b391 280 assert(start <= end);
0b02e057 281 assert(min_length > 0);
2858b391 282 assert(sig_index >= 0);
80d50141 283 assert(sig_index < 64);
2858b391 284
8dbbc7f0 285 lock_guard<recursive_mutex> lock(mutex_);
7d29656f 286
60b0c2da
JH
287 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
288 const unsigned int min_level = max((int)floorf(logf(min_length) /
0b02e057 289 LogMipMapScaleFactor) - 1, 0);
7d0d64f9 290 const uint64_t sig_mask = 1ULL << sig_index;
2858b391 291
7d0d64f9
JH
292 // Store the initial state
293 last_sample = (get_sample(start) & sig_mask) != 0;
294 edges.push_back(pair<int64_t, bool>(index++, last_sample));
2858b391 295
2ad82c2e 296 while (index + block_length <= end) {
7d0d64f9 297 //----- Continue to search -----//
0b02e057 298 level = min_level;
f06ab143
JH
299
300 // We cannot fast-forward if there is no mip-map data at
301 // at the minimum level.
4c60462b 302 fast_forward = (mip_map_[level].data != nullptr);
2858b391 303
2ad82c2e 304 if (min_length < MipMapScaleFactor) {
0b02e057
JH
305 // Search individual samples up to the beginning of
306 // the next first level mip map block
7d0d64f9 307 const uint64_t final_index = min(end,
0b02e057
JH
308 pow2_ceil(index, MipMapScalePower));
309
333d5bbc 310 for (; index < final_index &&
c28fa62b 311 (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
2ad82c2e 312 index++) {
0b02e057
JH
313 const bool sample =
314 (get_sample(index) & sig_mask) != 0;
7d0d64f9
JH
315
316 // If there was a change we cannot fast forward
333d5bbc 317 if (sample != last_sample) {
7d0d64f9 318 fast_forward = false;
0b02e057 319 break;
7d0d64f9 320 }
0b02e057 321 }
2ad82c2e 322 } else {
0b02e057
JH
323 // If resolution is less than a mip map block,
324 // round up to the beginning of the mip-map block
325 // for this level of detail
326 const int min_level_scale_power =
327 (level + 1) * MipMapScalePower;
328 index = pow2_ceil(index, min_level_scale_power);
333d5bbc 329 if (index >= end)
0b02e057
JH
330 break;
331
7d0d64f9
JH
332 // We can fast forward only if there was no change
333 const bool sample =
334 (get_sample(index) & sig_mask) != 0;
f06ab143
JH
335 if (last_sample != sample)
336 fast_forward = false;
0b02e057
JH
337 }
338
333d5bbc 339 if (fast_forward) {
7d0d64f9
JH
340
341 // Fast forward: This involves zooming out to higher
342 // levels of the mip map searching for changes, then
343 // zooming in on them to find the point where the edge
344 // begins.
345
346 // Slide right and zoom out at the beginnings of mip-map
347 // blocks until we encounter a change
333d5bbc 348 while (1) {
7d0d64f9
JH
349 const int level_scale_power =
350 (level + 1) * MipMapScalePower;
351 const uint64_t offset =
352 index >> level_scale_power;
7d0d64f9
JH
353
354 // Check if we reached the last block at this
355 // level, or if there was a change in this block
8dbbc7f0 356 if (offset >= mip_map_[level].length ||
7d0d64f9
JH
357 (get_subsample(level, offset) &
358 sig_mask))
0b02e057
JH
359 break;
360
c28fa62b 361 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
7d0d64f9
JH
362 // If we are now at the beginning of a
363 // higher level mip-map block ascend one
364 // level
333d5bbc 365 if (level + 1 >= ScaleStepCount ||
8dbbc7f0 366 !mip_map_[level + 1].data)
7d0d64f9
JH
367 break;
368
369 level++;
370 } else {
371 // Slide right to the beginning of the
372 // next mip map block
373 index = pow2_ceil(index + 1,
374 level_scale_power);
375 }
0b02e057 376 }
7d0d64f9
JH
377
378 // Zoom in, and slide right until we encounter a change,
379 // and repeat until we reach min_level
333d5bbc 380 while (1) {
8dbbc7f0 381 assert(mip_map_[level].data);
7d0d64f9
JH
382
383 const int level_scale_power =
384 (level + 1) * MipMapScalePower;
385 const uint64_t offset =
386 index >> level_scale_power;
7d0d64f9
JH
387
388 // Check if we reached the last block at this
389 // level, or if there was a change in this block
8dbbc7f0 390 if (offset >= mip_map_[level].length ||
2ad82c2e 391 (get_subsample(level, offset) &
7d0d64f9
JH
392 sig_mask)) {
393 // Zoom in unless we reached the minimum
394 // zoom
333d5bbc 395 if (level == min_level)
7d0d64f9
JH
396 break;
397
398 level--;
399 } else {
400 // Slide right to the beginning of the
401 // next mip map block
402 index = pow2_ceil(index + 1,
403 level_scale_power);
404 }
0b02e057 405 }
0b02e057 406
7d0d64f9
JH
407 // If individual samples within the limit of resolution,
408 // do a linear search for the next transition within the
409 // block
333d5bbc
UH
410 if (min_length < MipMapScaleFactor) {
411 for (; index < end; index++) {
7d0d64f9
JH
412 const bool sample = (get_sample(index) &
413 sig_mask) != 0;
333d5bbc 414 if (sample != last_sample)
7d0d64f9
JH
415 break;
416 }
0b02e057
JH
417 }
418 }
419
7d0d64f9
JH
420 //----- Store the edge -----//
421
422 // Take the last sample of the quanization block
423 const int64_t final_index = index + block_length;
333d5bbc 424 if (index + block_length > end)
7d0d64f9
JH
425 break;
426
427 // Store the final state
428 const bool final_sample =
429 (get_sample(final_index - 1) & sig_mask) != 0;
430 edges.push_back(pair<int64_t, bool>(index, final_sample));
431
432 index = final_index;
433 last_sample = final_sample;
2858b391
JH
434 }
435
436 // Add the final state
175d6573
JH
437 const bool end_sample = get_sample(end) & sig_mask;
438 if (last_sample != end_sample)
439 edges.push_back(pair<int64_t, bool>(end, end_sample));
440 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
2858b391 441}
0b02e057 442
f3d66e52 443uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
b2bcbe51
JH
444{
445 assert(level >= 0);
8dbbc7f0
JH
446 assert(mip_map_[level].data);
447 return unpack_sample((uint8_t*)mip_map_[level].data +
448 unit_size_ * offset);
b2bcbe51
JH
449}
450
f3d66e52 451uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
0b02e057 452{
60b0c2da
JH
453 const uint64_t p = 1 << power;
454 return (x + p - 1) / p * p;
0b02e057 455}
51e77110 456
1b1ec774 457} // namespace data
51e77110 458} // namespace pv