• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

bit_count.h

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2010 iptelorg GmbH
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  *
00018  * History
00019  * -------
00020  *  2010-04-26  Initial version (Miklos)
00021  */
00022 
00023 /* Implements the bit counting function:
00024  *   int bit_count(unsigned int u)
00025  *   Returns the number of bits in u.
00026  */
00027 
00028 #ifndef _BIT_COUNT_H
00029 #define _BIT_COUNT_H
00030 
00031 /* fix __CPU_i386 -> __CPU_x86 */
00032 #if defined __CPU_i386 && ! defined __CPU_x86
00033 #define __CPU_x86
00034 #endif
00035  
00036 #ifdef CC_GCC_LIKE_ASM
00037 #if defined __CPU_x86 || defined __CPU_x86_64
00038 #ifdef __SSE4_2__
00039 /* popcnt requires SSE4.2 support,
00040  * see http://en.wikipedia.org/wiki/SSE4 */
00041 #define BIT_COUNT_ASM
00042 #endif
00043 #endif
00044 #endif
00045 
00046 #ifdef BIT_COUNT_ASM
00047 
00048 /* Returns the number of 1 bits in u. */
00049 static inline int bit_count(unsigned int u)
00050 {
00051         int     v;
00052 
00053         asm volatile(" popcnt %1, %0 " : "=r" (v) : "rm" (u));
00054         return v;
00055 }
00056 
00057 #else /* BIT_COUNT_ASM */
00058 
00059 /* Returns the number of 1 bits in u.
00060  * source: http://en.wikipedia.org/wiki/Hamming_weight
00061  */
00062 #if 0
00063 static inline int bit_count(unsigned int u)
00064 {
00065         int     count;
00066 
00067         /* It is likely to have only few
00068          * bits set to 1, so there will be only
00069          * few iterations */
00070         for (count=0; u; count++)
00071                 u &= u-1;
00072         return count;
00073 }
00074 #endif
00075 
00076 static inline int bit_count(unsigned int i)
00077 {
00078         i = i - ((i >> 1) & 0x55555555);
00079         i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
00080         return (((i + (i >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
00081 }
00082 
00083 #if 0
00084 /* number of bits in a byte.
00085  * (Only slightly faster then the above version,
00086  * It is not worth the extra memory usage.)
00087  */
00088 extern int      bits_in_char[256];
00089 
00090 static inline int bit_count(unsigned int u)
00091 {
00092         return bits_in_char [u & 0xffu]
00093                 +  bits_in_char [(u >>  8 ) & 0xffu]
00094                 +  bits_in_char [(u >> 16) & 0xffu]
00095                 +  bits_in_char [(u >> 24) & 0xffu];
00096 }
00097 #endif
00098 
00099 #endif /* BIT_COUNT_ASM */
00100 
00101 #endif /* _BIT_COUNT_H */

Generated on Tue May 22 2012 13:10:03 for SIP Router by  doxygen 1.7.1