Imported Upstream version 3.2.2
[debian/gnuradio] / gr-pager / src / pageri_util.cc
1 /*
2  * Copyright 2006 Free Software Foundation, Inc.
3  * 
4  * This file is part of GNU Radio
5  * 
6  * GNU Radio 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 3, or (at your option)
9  * any later version.
10  * 
11  * GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <pageri_util.h>
27
28 unsigned char pageri_reverse_bits8(unsigned char val)
29 {
30     // This method was attributed to Rich Schroeppel in the Programming 
31     // Hacks section of Beeler, M., Gosper, R. W., and Schroeppel, R. 
32     // HAKMEM. MIT AI Memo 239, Feb. 29, 1972.
33     //
34     // Reverses 8 bits in 5 machine operations with 64 bit arch
35     return (val * 0x0202020202ULL & 0x010884422010ULL) % 1023;
36 }
37
38 gr_int32 pageri_reverse_bits32(gr_int32 val)
39 {
40     gr_int32 out = 0x00000000;
41     out |= (pageri_reverse_bits8((val >> 24) & 0x000000FF)      );
42     out |= (pageri_reverse_bits8((val >> 16) & 0x000000FF) <<  8);
43     out |= (pageri_reverse_bits8((val >>  8) & 0x000000FF) << 16);
44     out |= (pageri_reverse_bits8((val      ) & 0x000000FF) << 24);
45     return out;
46 }