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