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>
31 #include "logicsnapshot.h"
33 using boost::lock_guard;
34 using boost::recursive_mutex;
42 const int LogicSnapshot::MipMapScalePower = 4;
43 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
44 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
45 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
47 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic,
48 const uint64_t expected_num_samples) :
49 Snapshot(logic.unitsize),
50 _last_append_sample(0)
52 set_capacity(expected_num_samples);
54 lock_guard<recursive_mutex> lock(_mutex);
55 memset(_mip_map, 0, sizeof(_mip_map));
56 append_payload(logic);
59 LogicSnapshot::~LogicSnapshot()
61 lock_guard<recursive_mutex> lock(_mutex);
62 BOOST_FOREACH(MipMapLevel &l, _mip_map)
66 uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
68 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
69 return *(uint64_t*)ptr;
74 value |= ((uint64_t)ptr[7]) << 56;
77 value |= ((uint64_t)ptr[6]) << 48;
80 value |= ((uint64_t)ptr[5]) << 40;
83 value |= ((uint64_t)ptr[4]) << 32;
86 value |= ((uint32_t)ptr[3]) << 24;
89 value |= ((uint32_t)ptr[2]) << 16;
104 void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
106 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
107 *(uint64_t*)ptr = value;
111 ptr[7] = value >> 56;
114 ptr[6] = value >> 48;
117 ptr[5] = value >> 40;
120 ptr[4] = value >> 32;
123 ptr[3] = value >> 24;
126 ptr[2] = value >> 16;
140 void LogicSnapshot::append_payload(
141 const sr_datafeed_logic &logic)
143 assert(_unit_size == logic.unitsize);
144 assert((logic.length % _unit_size) == 0);
146 lock_guard<recursive_mutex> lock(_mutex);
148 append_data(logic.data, logic.length / _unit_size);
150 // Generate the first mip-map from the data
151 append_payload_to_mipmap();
154 void LogicSnapshot::get_samples(uint8_t *const data,
155 int64_t start_sample, int64_t end_sample) const
158 assert(start_sample >= 0);
159 assert(start_sample <= (int64_t)_sample_count);
160 assert(end_sample >= 0);
161 assert(end_sample <= (int64_t)_sample_count);
162 assert(start_sample <= end_sample);
164 lock_guard<recursive_mutex> lock(_mutex);
166 const size_t size = (end_sample - start_sample) * _unit_size;
167 memcpy(data, (const uint8_t*)_data + start_sample * _unit_size, size);
170 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
172 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
173 MipMapDataUnit) * MipMapDataUnit;
174 if (new_data_length > m.data_length)
176 m.data_length = new_data_length;
178 // Padding is added to allow for the uint64_t write word
179 m.data = realloc(m.data, new_data_length * _unit_size +
184 void LogicSnapshot::append_payload_to_mipmap()
186 MipMapLevel &m0 = _mip_map[0];
187 uint64_t prev_length;
188 const uint8_t *src_ptr;
190 uint64_t accumulator;
191 unsigned int diff_counter;
193 // Expand the data buffer to fit the new samples
194 prev_length = m0.length;
195 m0.length = _sample_count / MipMapScaleFactor;
197 // Break off if there are no new samples to compute
198 if (m0.length == prev_length)
201 reallocate_mipmap_level(m0);
203 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
205 // Iterate through the samples to populate the first level mipmap
206 const uint8_t *const end_src_ptr = (uint8_t*)_data +
207 m0.length * _unit_size * MipMapScaleFactor;
208 for (src_ptr = (uint8_t*)_data +
209 prev_length * _unit_size * MipMapScaleFactor;
210 src_ptr < end_src_ptr;)
212 // Accumulate transitions which have occurred in this sample
214 diff_counter = MipMapScaleFactor;
215 while (diff_counter-- > 0)
217 const uint64_t sample = unpack_sample(src_ptr);
218 accumulator |= _last_append_sample ^ sample;
219 _last_append_sample = sample;
220 src_ptr += _unit_size;
223 pack_sample(dest_ptr, accumulator);
224 dest_ptr += _unit_size;
227 // Compute higher level mipmaps
228 for (unsigned int level = 1; level < ScaleStepCount; level++)
230 MipMapLevel &m = _mip_map[level];
231 const MipMapLevel &ml = _mip_map[level-1];
233 // Expand the data buffer to fit the new samples
234 prev_length = m.length;
235 m.length = ml.length / MipMapScaleFactor;
237 // Break off if there are no more samples to computed
238 if (m.length == prev_length)
241 reallocate_mipmap_level(m);
243 // Subsample the level lower level
244 src_ptr = (uint8_t*)ml.data +
245 _unit_size * prev_length * MipMapScaleFactor;
246 const uint8_t *const end_dest_ptr =
247 (uint8_t*)m.data + _unit_size * m.length;
248 for (dest_ptr = (uint8_t*)m.data +
249 _unit_size * prev_length;
250 dest_ptr < end_dest_ptr;
251 dest_ptr += _unit_size)
254 diff_counter = MipMapScaleFactor;
255 while (diff_counter-- > 0)
257 accumulator |= unpack_sample(src_ptr);
258 src_ptr += _unit_size;
261 pack_sample(dest_ptr, accumulator);
266 uint64_t LogicSnapshot::get_sample(uint64_t index) const
269 assert(index < _sample_count);
271 return unpack_sample((uint8_t*)_data + index * _unit_size);
274 void LogicSnapshot::get_subsampled_edges(
275 std::vector<EdgePair> &edges,
276 uint64_t start, uint64_t end,
277 float min_length, int sig_index)
279 uint64_t index = start;
284 assert(end <= get_sample_count());
285 assert(start <= end);
286 assert(min_length > 0);
287 assert(sig_index >= 0);
288 assert(sig_index < 64);
290 lock_guard<recursive_mutex> lock(_mutex);
292 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
293 const unsigned int min_level = max((int)floorf(logf(min_length) /
294 LogMipMapScaleFactor) - 1, 0);
295 const uint64_t sig_mask = 1ULL << sig_index;
297 // Store the initial state
298 last_sample = (get_sample(start) & sig_mask) != 0;
299 edges.push_back(pair<int64_t, bool>(index++, last_sample));
301 while (index + block_length <= end)
303 //----- Continue to search -----//
306 // We cannot fast-forward if there is no mip-map data at
307 // at the minimum level.
308 fast_forward = (_mip_map[level].data != NULL);
310 if (min_length < MipMapScaleFactor)
312 // Search individual samples up to the beginning of
313 // the next first level mip map block
314 const uint64_t final_index = min(end,
315 pow2_ceil(index, MipMapScalePower));
317 for (; index < final_index &&
318 (index & ~(~0 << MipMapScalePower)) != 0;
322 (get_sample(index) & sig_mask) != 0;
324 // If there was a change we cannot fast forward
325 if (sample != last_sample) {
326 fast_forward = false;
333 // If resolution is less than a mip map block,
334 // round up to the beginning of the mip-map block
335 // for this level of detail
336 const int min_level_scale_power =
337 (level + 1) * MipMapScalePower;
338 index = pow2_ceil(index, min_level_scale_power);
342 // We can fast forward only if there was no change
344 (get_sample(index) & sig_mask) != 0;
345 if (last_sample != sample)
346 fast_forward = false;
351 // Fast forward: This involves zooming out to higher
352 // levels of the mip map searching for changes, then
353 // zooming in on them to find the point where the edge
356 // Slide right and zoom out at the beginnings of mip-map
357 // blocks until we encounter a change
359 const int level_scale_power =
360 (level + 1) * MipMapScalePower;
361 const uint64_t offset =
362 index >> level_scale_power;
364 // Check if we reached the last block at this
365 // level, or if there was a change in this block
366 if (offset >= _mip_map[level].length ||
367 (get_subsample(level, offset) &
371 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
372 // If we are now at the beginning of a
373 // higher level mip-map block ascend one
375 if (level + 1 >= ScaleStepCount ||
376 !_mip_map[level + 1].data)
381 // Slide right to the beginning of the
382 // next mip map block
383 index = pow2_ceil(index + 1,
388 // Zoom in, and slide right until we encounter a change,
389 // and repeat until we reach min_level
391 assert(_mip_map[level].data);
393 const int level_scale_power =
394 (level + 1) * MipMapScalePower;
395 const uint64_t offset =
396 index >> level_scale_power;
398 // Check if we reached the last block at this
399 // level, or if there was a change in this block
400 if (offset >= _mip_map[level].length ||
401 (get_subsample(level, offset) &
403 // Zoom in unless we reached the minimum
405 if (level == min_level)
410 // Slide right to the beginning of the
411 // next mip map block
412 index = pow2_ceil(index + 1,
417 // If individual samples within the limit of resolution,
418 // do a linear search for the next transition within the
420 if (min_length < MipMapScaleFactor) {
421 for (; index < end; index++) {
422 const bool sample = (get_sample(index) &
424 if (sample != last_sample)
430 //----- Store the edge -----//
432 // Take the last sample of the quanization block
433 const int64_t final_index = index + block_length;
434 if (index + block_length > end)
437 // Store the final state
438 const bool final_sample =
439 (get_sample(final_index - 1) & sig_mask) != 0;
440 edges.push_back(pair<int64_t, bool>(index, final_sample));
443 last_sample = final_sample;
446 // Add the final state
447 const bool end_sample = get_sample(end) & sig_mask;
448 if (last_sample != end_sample)
449 edges.push_back(pair<int64_t, bool>(end, end_sample));
450 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
453 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
456 assert(_mip_map[level].data);
457 return unpack_sample((uint8_t*)_mip_map[level].data +
458 _unit_size * offset);
461 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
463 const uint64_t p = 1 << power;
464 return (x + p - 1) / p * p;