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