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