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