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