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