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