2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
28 #include <boost/foreach.hpp>
30 #include "logicdatasnapshot.h"
36 const int LogicDataSnapshot::MipMapScalePower = 4;
37 const int LogicDataSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
38 const float LogicDataSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
39 const uint64_t LogicDataSnapshot::MipMapDataUnit = 64*1024; // bytes
41 LogicDataSnapshot::LogicDataSnapshot(
42 const sr_datafeed_logic &logic) :
43 DataSnapshot(logic.unitsize),
44 _last_append_sample(0)
46 memset(_mip_map, 0, sizeof(_mip_map));
47 append_payload(logic);
50 LogicDataSnapshot::~LogicDataSnapshot()
52 BOOST_FOREACH(MipMapLevel &l, _mip_map)
56 void LogicDataSnapshot::append_payload(
57 const sr_datafeed_logic &logic)
59 assert(_unit_size == logic.unitsize);
61 append_data(logic.data, logic.length);
63 // Generate the first mip-map from the data
64 append_payload_to_mipmap();
67 void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m)
69 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
70 MipMapDataUnit) * MipMapDataUnit;
71 if(new_data_length > m.data_length)
73 m.data_length = new_data_length;
74 m.data = realloc(m.data, new_data_length * _unit_size);
78 void LogicDataSnapshot::append_payload_to_mipmap()
80 MipMapLevel &m0 = _mip_map[0];
82 const uint8_t *src_ptr;
85 unsigned int diff_counter;
87 // Expand the data buffer to fit the new samples
88 prev_length = m0.length;
89 m0.length = _sample_count / MipMapScaleFactor;
91 // Break off if there are no new samples to compute
92 if(m0.length == prev_length)
95 reallocate_mip_map(m0);
97 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
99 // Iterate through the samples to populate the first level mipmap
101 diff_counter = MipMapScaleFactor;
102 const uint8_t *end_src_ptr = (uint8_t*)_data +
103 m0.length * _unit_size * MipMapScaleFactor;
104 for(src_ptr = (uint8_t*)_data +
105 prev_length * _unit_size * MipMapScaleFactor;
106 src_ptr < end_src_ptr;)
108 // Accumulate transitions which have occurred in this sample
110 diff_counter = MipMapScaleFactor;
111 while(diff_counter-- > 0)
113 const uint64_t sample = *(uint64_t*)src_ptr;
114 accumulator |= _last_append_sample ^ sample;
115 _last_append_sample = sample;
116 src_ptr += _unit_size;
119 *(uint64_t*)dest_ptr = accumulator;
120 dest_ptr += _unit_size;
123 // Compute higher level mipmaps
124 for(int level = 1; level < ScaleStepCount; level++)
126 MipMapLevel &m = _mip_map[level];
127 const MipMapLevel &ml = _mip_map[level-1];
129 // Expand the data buffer to fit the new samples
130 prev_length = m.length;
131 m.length = ml.length / MipMapScaleFactor;
133 // Break off if there are no more samples to computed
134 if(m.length == prev_length)
137 reallocate_mip_map(m);
139 // Subsample the level lower level
140 src_ptr = (uint8_t*)ml.data +
141 _unit_size * prev_length * MipMapScaleFactor;
142 const uint8_t *end_dest_ptr =
143 (uint8_t*)m.data + _unit_size * m.length;
144 for(dest_ptr = (uint8_t*)m.data +
145 _unit_size * prev_length;
146 dest_ptr < end_dest_ptr;
147 dest_ptr += _unit_size)
150 diff_counter = MipMapScaleFactor;
151 while(diff_counter-- > 0)
153 accumulator |= *(uint64_t*)src_ptr;
154 src_ptr += _unit_size;
157 *(uint64_t*)dest_ptr = accumulator;
162 uint64_t LogicDataSnapshot::get_sample(uint64_t index) const
165 assert(index >= 0 && index < _sample_count);
167 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
170 void LogicDataSnapshot::get_subsampled_edges(
171 std::vector<EdgePair> &edges,
172 int64_t start, int64_t end,
173 float min_length, int sig_index)
175 int64_t index = start;
181 assert(end <= get_sample_count());
182 assert(start <= end);
183 assert(min_length > 0);
184 assert(sig_index >= 0);
185 assert(sig_index < SR_MAX_NUM_PROBES);
187 const int64_t block_length = (int64_t)max(min_length, 1.0f);
188 const int min_level = max((int)floorf(logf(min_length) /
189 LogMipMapScaleFactor) - 1, 0);
190 const uint64_t sig_mask = 1ULL << sig_index;
192 // Store the initial state
193 last_sample = (get_sample(start) & sig_mask) != 0;
194 edges.push_back(pair<int64_t, bool>(index++, last_sample));
196 while(index + block_length <= end)
198 //----- Continue to search -----//
202 if(min_length < MipMapScaleFactor)
204 // Search individual samples up to the beginning of
205 // the next first level mip map block
206 const uint64_t final_index = min(end,
207 pow2_ceil(index, MipMapScalePower));
210 index < final_index &&
211 (index & ~(~0 << MipMapScalePower)) != 0;
215 (get_sample(index) & sig_mask) != 0;
217 // If there was a change we cannot fast forward
218 if(sample != last_sample) {
219 fast_forward = false;
226 // If resolution is less than a mip map block,
227 // round up to the beginning of the mip-map block
228 // for this level of detail
229 const int min_level_scale_power =
230 (level + 1) * MipMapScalePower;
231 index = pow2_ceil(index, min_level_scale_power);
235 // We can fast forward only if there was no change
237 (get_sample(index) & sig_mask) != 0;
238 fast_forward = last_sample == sample;
243 // Fast forward: This involves zooming out to higher
244 // levels of the mip map searching for changes, then
245 // zooming in on them to find the point where the edge
248 // Slide right and zoom out at the beginnings of mip-map
249 // blocks until we encounter a change
251 const int level_scale_power =
252 (level + 1) * MipMapScalePower;
253 const uint64_t offset =
254 index >> level_scale_power;
257 // Check if we reached the last block at this
258 // level, or if there was a change in this block
259 if(offset >= _mip_map[level].length ||
260 (get_subsample(level, offset) &
264 if((offset & ~(~0 << MipMapScalePower)) == 0) {
265 // If we are now at the beginning of a
266 // higher level mip-map block ascend one
268 if(level + 1 >= ScaleStepCount ||
269 !_mip_map[level + 1].data)
274 // Slide right to the beginning of the
275 // next mip map block
276 index = pow2_ceil(index + 1,
281 // Zoom in, and slide right until we encounter a change,
282 // and repeat until we reach min_level
284 assert(_mip_map[level].data);
286 const int level_scale_power =
287 (level + 1) * MipMapScalePower;
288 const uint64_t offset =
289 index >> level_scale_power;
292 // Check if we reached the last block at this
293 // level, or if there was a change in this block
294 if(offset >= _mip_map[level].length ||
295 (get_subsample(level, offset) &
297 // Zoom in unless we reached the minimum
299 if(level == min_level)
304 // Slide right to the beginning of the
305 // next mip map block
306 index = pow2_ceil(index + 1,
311 // If individual samples within the limit of resolution,
312 // do a linear search for the next transition within the
314 if(min_length < MipMapScaleFactor) {
315 for(index; index < end; index++) {
316 const bool sample = (get_sample(index) &
318 if(sample != last_sample)
324 //----- Store the edge -----//
326 // Take the last sample of the quanization block
327 const int64_t final_index = index + block_length;
328 if(index + block_length > end)
331 // Store the final state
332 const bool final_sample =
333 (get_sample(final_index - 1) & sig_mask) != 0;
334 edges.push_back(pair<int64_t, bool>(index, final_sample));
337 last_sample = final_sample;
340 // Add the final state
341 edges.push_back(pair<int64_t, bool>(end,
342 get_sample(end) & sig_mask));
345 uint64_t LogicDataSnapshot::get_subsample(int level, uint64_t offset) const
348 assert(_mip_map[level].data);
349 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
350 _unit_size * offset);
353 int64_t LogicDataSnapshot::pow2_ceil(int64_t x, unsigned int power)
355 const int64_t p = 1 << power;
356 return ((x < 0) ? x : (x + p - 1)) / p * p;