Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gr-atsc / src / lib / atsci_sliding_correlator.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <atsci_sliding_correlator.h>
24 #include <atsci_pnXXX.h>
25
26 // #define TRY_BACKWARDS
27
28 /*
29  * Return the number of 1's in v.
30  * This magic code is explained wonderfully in the AMD Athlon
31  * optimization guide, pg 136.
32  */
33 static inline int popcount32 (unsigned long v)
34 {
35   unsigned long w = v - ((v >> 1) & 0x55555555);
36   unsigned long x = (w & 0x33333333) + ((w >> 2) & 0x33333333);
37   unsigned long y = (x + (x >> 4)) & 0x0f0f0f0f;
38   unsigned long z = (y * 0x01010101) >> 24;
39
40   return z;
41 }
42
43 atsci_sliding_correlator::atsci_sliding_correlator ()
44 {
45 #if defined(TRY_BACKWARDS)
46   for (int i = 511 - 1; i >= 0; i--)
47     mask.shift_in (atsc_pn511[i]);
48 #else
49   for (unsigned i = 0; i < 511; i++)
50     mask.shift_in (atsc_pn511[i]);
51 #endif
52
53   and_mask.shift_in (0);
54   for (int i = 0; i < 511; i++)
55     and_mask.shift_in (1);
56 }
57
58 /*
59  * we shift in from the top of the register towards the bottom
60  *
61  * +-- bits enter here...
62  * |
63  * v
64  * block 0 | block 1 | block 2 ... | block N-1
65  *                                           |
66  *                                           +--> ... and drop out here
67  *
68  */
69 inline void
70 atsci_sliding_correlator::shift_reg::shift_in (int bit)
71 {
72   for (int i = NSRBLOCKS - 1; i > 0; i--)
73     d[i] = ((d[i-1] & 0x1) << (srblock_bitsize - 1)) | (d[i] >> 1);
74
75   d[0] = (((srblock) bit) << (srblock_bitsize - 1)) | (d[0] >> 1);
76 }
77
78 int
79 atsci_sliding_correlator::input_bit (int bit)
80 {
81   input.shift_in (bit);
82
83   // can probably get a win by unrolling the loop, if we need to.
84   int   count = 0;
85   for (int i = 0; i < NSRBLOCKS; i++)
86     count += popcount32 ((input.d[i] ^ mask.d[i]) & and_mask.d[i]);
87
88   return count;
89 }
90