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