]>
Commit | Line | Data |
---|---|---|
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 | ||
9df8453f | 28 | #include "config.h" |
1b1ec774 | 29 | #include "logicsnapshot.h" |
f556bc6a | 30 | |
3b68d03d JH |
31 | using std::lock_guard; |
32 | using std::recursive_mutex; | |
819f4c25 JH |
33 | using std::max; |
34 | using std::min; | |
35 | using std::pair; | |
28a4c9c5 | 36 | |
51e77110 | 37 | namespace pv { |
1b1ec774 | 38 | namespace data { |
51e77110 | 39 | |
1b1ec774 JH |
40 | const int LogicSnapshot::MipMapScalePower = 4; |
41 | const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower; | |
42 | const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor); | |
43 | const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes | |
4ceab49a | 44 | |
27d7c96b DK |
45 | LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic, |
46 | const uint64_t expected_num_samples) : | |
1b1ec774 | 47 | Snapshot(logic.unitsize), |
4ceab49a | 48 | _last_append_sample(0) |
f556bc6a | 49 | { |
27d7c96b DK |
50 | set_capacity(expected_num_samples); |
51 | ||
7d29656f | 52 | lock_guard<recursive_mutex> lock(_mutex); |
4ceab49a | 53 | memset(_mip_map, 0, sizeof(_mip_map)); |
f556bc6a JH |
54 | append_payload(logic); |
55 | } | |
56 | ||
1b1ec774 | 57 | LogicSnapshot::~LogicSnapshot() |
4ceab49a | 58 | { |
7d29656f | 59 | lock_guard<recursive_mutex> lock(_mutex); |
d9aecf1f | 60 | for (MipMapLevel &l : _mip_map) |
4ceab49a JH |
61 | free(l.data); |
62 | } | |
63 | ||
8cb71705 MC |
64 | uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const |
65 | { | |
9df8453f MC |
66 | #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS |
67 | return *(uint64_t*)ptr; | |
68 | #else | |
8cb71705 MC |
69 | uint64_t value = 0; |
70 | switch(_unit_size) { | |
71 | default: | |
72 | value |= ((uint64_t)ptr[7]) << 56; | |
73 | /* FALLTHRU */ | |
74 | case 7: | |
75 | value |= ((uint64_t)ptr[6]) << 48; | |
76 | /* FALLTHRU */ | |
77 | case 6: | |
78 | value |= ((uint64_t)ptr[5]) << 40; | |
79 | /* FALLTHRU */ | |
80 | case 5: | |
81 | value |= ((uint64_t)ptr[4]) << 32; | |
82 | /* FALLTHRU */ | |
83 | case 4: | |
84 | value |= ((uint32_t)ptr[3]) << 24; | |
85 | /* FALLTHRU */ | |
86 | case 3: | |
87 | value |= ((uint32_t)ptr[2]) << 16; | |
88 | /* FALLTHRU */ | |
89 | case 2: | |
90 | value |= ptr[1] << 8; | |
91 | /* FALLTHRU */ | |
92 | case 1: | |
93 | value |= ptr[0]; | |
94 | /* FALLTHRU */ | |
95 | case 0: | |
96 | break; | |
97 | } | |
98 | return value; | |
9df8453f | 99 | #endif |
8cb71705 MC |
100 | } |
101 | ||
102 | void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value) | |
103 | { | |
9df8453f MC |
104 | #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS |
105 | *(uint64_t*)ptr = value; | |
106 | #else | |
8cb71705 MC |
107 | switch(_unit_size) { |
108 | default: | |
109 | ptr[7] = value >> 56; | |
110 | /* FALLTHRU */ | |
111 | case 7: | |
112 | ptr[6] = value >> 48; | |
113 | /* FALLTHRU */ | |
114 | case 6: | |
115 | ptr[5] = value >> 40; | |
116 | /* FALLTHRU */ | |
117 | case 5: | |
118 | ptr[4] = value >> 32; | |
119 | /* FALLTHRU */ | |
120 | case 4: | |
121 | ptr[3] = value >> 24; | |
122 | /* FALLTHRU */ | |
123 | case 3: | |
124 | ptr[2] = value >> 16; | |
125 | /* FALLTHRU */ | |
126 | case 2: | |
127 | ptr[1] = value >> 8; | |
128 | /* FALLTHRU */ | |
129 | case 1: | |
130 | ptr[0] = value; | |
131 | /* FALLTHRU */ | |
132 | case 0: | |
133 | break; | |
134 | } | |
9df8453f | 135 | #endif |
8cb71705 MC |
136 | } |
137 | ||
1b1ec774 | 138 | void LogicSnapshot::append_payload( |
28a4c9c5 JH |
139 | const sr_datafeed_logic &logic) |
140 | { | |
f556bc6a | 141 | assert(_unit_size == logic.unitsize); |
5cef1ea3 | 142 | assert((logic.length % _unit_size) == 0); |
f556bc6a | 143 | |
7d29656f JH |
144 | lock_guard<recursive_mutex> lock(_mutex); |
145 | ||
5cef1ea3 | 146 | append_data(logic.data, logic.length / _unit_size); |
4ceab49a JH |
147 | |
148 | // Generate the first mip-map from the data | |
149 | append_payload_to_mipmap(); | |
150 | } | |
151 | ||
ed990f11 JH |
152 | void LogicSnapshot::get_samples(uint8_t *const data, |
153 | int64_t start_sample, int64_t end_sample) const | |
154 | { | |
155 | assert(data); | |
156 | assert(start_sample >= 0); | |
0fbda3c2 | 157 | assert(start_sample <= (int64_t)_sample_count); |
ed990f11 | 158 | assert(end_sample >= 0); |
0fbda3c2 | 159 | assert(end_sample <= (int64_t)_sample_count); |
ed990f11 JH |
160 | assert(start_sample <= end_sample); |
161 | ||
162 | lock_guard<recursive_mutex> lock(_mutex); | |
163 | ||
164 | const size_t size = (end_sample - start_sample) * _unit_size; | |
c2d4e9df | 165 | memcpy(data, (const uint8_t*)_data + start_sample * _unit_size, size); |
ed990f11 JH |
166 | } |
167 | ||
023887ca | 168 | void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m) |
4ceab49a JH |
169 | { |
170 | const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) / | |
171 | MipMapDataUnit) * MipMapDataUnit; | |
333d5bbc | 172 | if (new_data_length > m.data_length) |
4ceab49a JH |
173 | { |
174 | m.data_length = new_data_length; | |
8fe60279 JH |
175 | |
176 | // Padding is added to allow for the uint64_t write word | |
177 | m.data = realloc(m.data, new_data_length * _unit_size + | |
178 | sizeof(uint64_t)); | |
4ceab49a JH |
179 | } |
180 | } | |
181 | ||
1b1ec774 | 182 | void LogicSnapshot::append_payload_to_mipmap() |
4ceab49a JH |
183 | { |
184 | MipMapLevel &m0 = _mip_map[0]; | |
185 | uint64_t prev_length; | |
186 | const uint8_t *src_ptr; | |
187 | uint8_t *dest_ptr; | |
188 | uint64_t accumulator; | |
189 | unsigned int diff_counter; | |
190 | ||
191 | // Expand the data buffer to fit the new samples | |
192 | prev_length = m0.length; | |
77bff0b1 | 193 | m0.length = _sample_count / MipMapScaleFactor; |
4ceab49a JH |
194 | |
195 | // Break off if there are no new samples to compute | |
333d5bbc | 196 | if (m0.length == prev_length) |
4ceab49a JH |
197 | return; |
198 | ||
023887ca | 199 | reallocate_mipmap_level(m0); |
4ceab49a JH |
200 | |
201 | dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size; | |
202 | ||
203 | // Iterate through the samples to populate the first level mipmap | |
023887ca | 204 | const uint8_t *const end_src_ptr = (uint8_t*)_data + |
4ceab49a | 205 | m0.length * _unit_size * MipMapScaleFactor; |
333d5bbc | 206 | for (src_ptr = (uint8_t*)_data + |
4ceab49a JH |
207 | prev_length * _unit_size * MipMapScaleFactor; |
208 | src_ptr < end_src_ptr;) | |
209 | { | |
210 | // Accumulate transitions which have occurred in this sample | |
211 | accumulator = 0; | |
212 | diff_counter = MipMapScaleFactor; | |
333d5bbc | 213 | while (diff_counter-- > 0) |
4ceab49a | 214 | { |
8cb71705 | 215 | const uint64_t sample = unpack_sample(src_ptr); |
4ceab49a JH |
216 | accumulator |= _last_append_sample ^ sample; |
217 | _last_append_sample = sample; | |
218 | src_ptr += _unit_size; | |
219 | } | |
220 | ||
8cb71705 | 221 | pack_sample(dest_ptr, accumulator); |
4ceab49a JH |
222 | dest_ptr += _unit_size; |
223 | } | |
224 | ||
225 | // Compute higher level mipmaps | |
333d5bbc | 226 | for (unsigned int level = 1; level < ScaleStepCount; level++) |
4ceab49a JH |
227 | { |
228 | MipMapLevel &m = _mip_map[level]; | |
229 | const MipMapLevel &ml = _mip_map[level-1]; | |
230 | ||
231 | // Expand the data buffer to fit the new samples | |
232 | prev_length = m.length; | |
233 | m.length = ml.length / MipMapScaleFactor; | |
234 | ||
235 | // Break off if there are no more samples to computed | |
333d5bbc | 236 | if (m.length == prev_length) |
4ceab49a JH |
237 | break; |
238 | ||
023887ca | 239 | reallocate_mipmap_level(m); |
4ceab49a JH |
240 | |
241 | // Subsample the level lower level | |
242 | src_ptr = (uint8_t*)ml.data + | |
243 | _unit_size * prev_length * MipMapScaleFactor; | |
023887ca | 244 | const uint8_t *const end_dest_ptr = |
4ceab49a | 245 | (uint8_t*)m.data + _unit_size * m.length; |
333d5bbc | 246 | for (dest_ptr = (uint8_t*)m.data + |
4ceab49a JH |
247 | _unit_size * prev_length; |
248 | dest_ptr < end_dest_ptr; | |
249 | dest_ptr += _unit_size) | |
250 | { | |
251 | accumulator = 0; | |
252 | diff_counter = MipMapScaleFactor; | |
333d5bbc | 253 | while (diff_counter-- > 0) |
4ceab49a | 254 | { |
8cb71705 | 255 | accumulator |= unpack_sample(src_ptr); |
4ceab49a JH |
256 | src_ptr += _unit_size; |
257 | } | |
258 | ||
8cb71705 | 259 | pack_sample(dest_ptr, accumulator); |
4ceab49a JH |
260 | } |
261 | } | |
28a4c9c5 | 262 | } |
2858b391 | 263 | |
1b1ec774 | 264 | uint64_t LogicSnapshot::get_sample(uint64_t index) const |
2858b391 JH |
265 | { |
266 | assert(_data); | |
3f96c34a | 267 | assert(index < _sample_count); |
2858b391 | 268 | |
8cb71705 | 269 | return unpack_sample((uint8_t*)_data + index * _unit_size); |
2858b391 JH |
270 | } |
271 | ||
1b1ec774 | 272 | void LogicSnapshot::get_subsampled_edges( |
2858b391 | 273 | std::vector<EdgePair> &edges, |
60b0c2da | 274 | uint64_t start, uint64_t end, |
0b02e057 | 275 | float min_length, int sig_index) |
2858b391 | 276 | { |
60b0c2da JH |
277 | uint64_t index = start; |
278 | unsigned int level; | |
7d0d64f9 JH |
279 | bool last_sample; |
280 | bool fast_forward; | |
0b02e057 | 281 | |
0b02e057 | 282 | assert(end <= get_sample_count()); |
2858b391 | 283 | assert(start <= end); |
0b02e057 | 284 | assert(min_length > 0); |
2858b391 | 285 | assert(sig_index >= 0); |
80d50141 | 286 | assert(sig_index < 64); |
2858b391 | 287 | |
7d29656f JH |
288 | lock_guard<recursive_mutex> lock(_mutex); |
289 | ||
60b0c2da JH |
290 | const uint64_t block_length = (uint64_t)max(min_length, 1.0f); |
291 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
0b02e057 | 292 | LogMipMapScaleFactor) - 1, 0); |
7d0d64f9 | 293 | const uint64_t sig_mask = 1ULL << sig_index; |
2858b391 | 294 | |
7d0d64f9 JH |
295 | // Store the initial state |
296 | last_sample = (get_sample(start) & sig_mask) != 0; | |
297 | edges.push_back(pair<int64_t, bool>(index++, last_sample)); | |
2858b391 | 298 | |
333d5bbc | 299 | while (index + block_length <= end) |
2858b391 | 300 | { |
7d0d64f9 | 301 | //----- Continue to search -----// |
0b02e057 | 302 | level = min_level; |
f06ab143 JH |
303 | |
304 | // We cannot fast-forward if there is no mip-map data at | |
305 | // at the minimum level. | |
306 | fast_forward = (_mip_map[level].data != NULL); | |
2858b391 | 307 | |
333d5bbc | 308 | if (min_length < MipMapScaleFactor) |
0b02e057 JH |
309 | { |
310 | // Search individual samples up to the beginning of | |
311 | // the next first level mip map block | |
7d0d64f9 | 312 | const uint64_t final_index = min(end, |
0b02e057 JH |
313 | pow2_ceil(index, MipMapScalePower)); |
314 | ||
333d5bbc | 315 | for (; index < final_index && |
0b02e057 JH |
316 | (index & ~(~0 << MipMapScalePower)) != 0; |
317 | index++) | |
318 | { | |
319 | const bool sample = | |
320 | (get_sample(index) & sig_mask) != 0; | |
7d0d64f9 JH |
321 | |
322 | // If there was a change we cannot fast forward | |
333d5bbc | 323 | if (sample != last_sample) { |
7d0d64f9 | 324 | fast_forward = false; |
0b02e057 | 325 | break; |
7d0d64f9 | 326 | } |
0b02e057 JH |
327 | } |
328 | } | |
329 | else | |
330 | { | |
331 | // If resolution is less than a mip map block, | |
332 | // round up to the beginning of the mip-map block | |
333 | // for this level of detail | |
334 | const int min_level_scale_power = | |
335 | (level + 1) * MipMapScalePower; | |
336 | index = pow2_ceil(index, min_level_scale_power); | |
333d5bbc | 337 | if (index >= end) |
0b02e057 JH |
338 | break; |
339 | ||
7d0d64f9 JH |
340 | // We can fast forward only if there was no change |
341 | const bool sample = | |
342 | (get_sample(index) & sig_mask) != 0; | |
f06ab143 JH |
343 | if (last_sample != sample) |
344 | fast_forward = false; | |
0b02e057 JH |
345 | } |
346 | ||
333d5bbc | 347 | if (fast_forward) { |
7d0d64f9 JH |
348 | |
349 | // Fast forward: This involves zooming out to higher | |
350 | // levels of the mip map searching for changes, then | |
351 | // zooming in on them to find the point where the edge | |
352 | // begins. | |
353 | ||
354 | // Slide right and zoom out at the beginnings of mip-map | |
355 | // blocks until we encounter a change | |
333d5bbc | 356 | while (1) { |
7d0d64f9 JH |
357 | const int level_scale_power = |
358 | (level + 1) * MipMapScalePower; | |
359 | const uint64_t offset = | |
360 | index >> level_scale_power; | |
7d0d64f9 JH |
361 | |
362 | // Check if we reached the last block at this | |
363 | // level, or if there was a change in this block | |
333d5bbc | 364 | if (offset >= _mip_map[level].length || |
7d0d64f9 JH |
365 | (get_subsample(level, offset) & |
366 | sig_mask)) | |
0b02e057 JH |
367 | break; |
368 | ||
333d5bbc | 369 | if ((offset & ~(~0 << MipMapScalePower)) == 0) { |
7d0d64f9 JH |
370 | // If we are now at the beginning of a |
371 | // higher level mip-map block ascend one | |
372 | // level | |
333d5bbc | 373 | if (level + 1 >= ScaleStepCount || |
7d0d64f9 JH |
374 | !_mip_map[level + 1].data) |
375 | break; | |
376 | ||
377 | level++; | |
378 | } else { | |
379 | // Slide right to the beginning of the | |
380 | // next mip map block | |
381 | index = pow2_ceil(index + 1, | |
382 | level_scale_power); | |
383 | } | |
0b02e057 | 384 | } |
7d0d64f9 JH |
385 | |
386 | // Zoom in, and slide right until we encounter a change, | |
387 | // and repeat until we reach min_level | |
333d5bbc | 388 | while (1) { |
7d0d64f9 JH |
389 | assert(_mip_map[level].data); |
390 | ||
391 | const int level_scale_power = | |
392 | (level + 1) * MipMapScalePower; | |
393 | const uint64_t offset = | |
394 | index >> level_scale_power; | |
7d0d64f9 JH |
395 | |
396 | // Check if we reached the last block at this | |
397 | // level, or if there was a change in this block | |
333d5bbc | 398 | if (offset >= _mip_map[level].length || |
7d0d64f9 JH |
399 | (get_subsample(level, offset) & |
400 | sig_mask)) { | |
401 | // Zoom in unless we reached the minimum | |
402 | // zoom | |
333d5bbc | 403 | if (level == min_level) |
7d0d64f9 JH |
404 | break; |
405 | ||
406 | level--; | |
407 | } else { | |
408 | // Slide right to the beginning of the | |
409 | // next mip map block | |
410 | index = pow2_ceil(index + 1, | |
411 | level_scale_power); | |
412 | } | |
0b02e057 | 413 | } |
0b02e057 | 414 | |
7d0d64f9 JH |
415 | // If individual samples within the limit of resolution, |
416 | // do a linear search for the next transition within the | |
417 | // block | |
333d5bbc UH |
418 | if (min_length < MipMapScaleFactor) { |
419 | for (; index < end; index++) { | |
7d0d64f9 JH |
420 | const bool sample = (get_sample(index) & |
421 | sig_mask) != 0; | |
333d5bbc | 422 | if (sample != last_sample) |
7d0d64f9 JH |
423 | break; |
424 | } | |
0b02e057 JH |
425 | } |
426 | } | |
427 | ||
7d0d64f9 JH |
428 | //----- Store the edge -----// |
429 | ||
430 | // Take the last sample of the quanization block | |
431 | const int64_t final_index = index + block_length; | |
333d5bbc | 432 | if (index + block_length > end) |
7d0d64f9 JH |
433 | break; |
434 | ||
435 | // Store the final state | |
436 | const bool final_sample = | |
437 | (get_sample(final_index - 1) & sig_mask) != 0; | |
438 | edges.push_back(pair<int64_t, bool>(index, final_sample)); | |
439 | ||
440 | index = final_index; | |
441 | last_sample = final_sample; | |
2858b391 JH |
442 | } |
443 | ||
444 | // Add the final state | |
175d6573 JH |
445 | const bool end_sample = get_sample(end) & sig_mask; |
446 | if (last_sample != end_sample) | |
447 | edges.push_back(pair<int64_t, bool>(end, end_sample)); | |
448 | edges.push_back(pair<int64_t, bool>(end + 1, end_sample)); | |
2858b391 | 449 | } |
0b02e057 | 450 | |
1b1ec774 | 451 | uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const |
b2bcbe51 JH |
452 | { |
453 | assert(level >= 0); | |
454 | assert(_mip_map[level].data); | |
8cb71705 | 455 | return unpack_sample((uint8_t*)_mip_map[level].data + |
b2bcbe51 JH |
456 | _unit_size * offset); |
457 | } | |
458 | ||
1b1ec774 | 459 | uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power) |
0b02e057 | 460 | { |
60b0c2da JH |
461 | const uint64_t p = 1 << power; |
462 | return (x + p - 1) / p * p; | |
0b02e057 | 463 | } |
51e77110 | 464 | |
1b1ec774 | 465 | } // namespace data |
51e77110 | 466 | } // namespace pv |