GNU Radio Manual and C++ API Reference  3.7.7
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
volk_32u_popcnt.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012, 2014 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 /*!
24  * \page volk_32u_popcnt
25  *
26  * \b Overview
27  *
28  * Computes the population count (popcnt), or Hamming distance of a
29  * binary string. This kernel takes in a single unsigned 32-bit value
30  * and returns the count of 1's that the value contains.
31  *
32  * <b>Dispatcher Prototype</b>
33  * \code
34  * void volk_32u_popcnt(uint32_t* ret, const uint32_t value)
35  * \endcode
36  *
37  * \b Inputs
38  * \li value: The input value.
39  *
40  * \b Outputs
41  * \li ret: The return value containing the popcnt.
42  *
43  * \b Example
44  * \code
45  int N = 10;
46  unsigned int alignment = volk_get_alignment();
47 
48  uint32_t bitstring = 0x55555555;
49  uint32_t hamming_distance = 0;
50 
51  volk_32u_popcnt(&hamming_distance, bitstring);
52  printf("hamming distance of %x = %i\n", bitstring, hamming_distance);
53  * \endcode
54  */
55 
56 #ifndef INCLUDED_VOLK_32u_POPCNT_A16_H
57 #define INCLUDED_VOLK_32u_POPCNT_A16_H
58 
59 #include <stdio.h>
60 #include <inttypes.h>
61 
62 #ifdef LV_HAVE_GENERIC
63 
64 static inline void
65 volk_32u_popcnt_generic(uint32_t* ret, const uint32_t value)
66 {
67  // This is faster than a lookup table
68  uint32_t retVal = value;
69 
70  retVal = (retVal & 0x55555555) + (retVal >> 1 & 0x55555555);
71  retVal = (retVal & 0x33333333) + (retVal >> 2 & 0x33333333);
72  retVal = (retVal + (retVal >> 4)) & 0x0F0F0F0F;
73  retVal = (retVal + (retVal >> 8));
74  retVal = (retVal + (retVal >> 16)) & 0x0000003F;
75 
76  *ret = retVal;
77 }
78 
79 #endif /*LV_HAVE_GENERIC*/
80 
81 
82 #ifdef LV_HAVE_SSE4_2
83 
84 #include <nmmintrin.h>
85 
86 static inline void
87 volk_32u_popcnt_a_sse4_2(uint32_t* ret, const uint32_t value)
88 {
89  *ret = _mm_popcnt_u32(value);
90 }
91 
92 #endif /*LV_HAVE_SSE4_2*/
93 
94 #endif /*INCLUDED_VOLK_32u_POPCNT_A16_H*/
unsigned int uint32_t
Definition: stdint.h:80