Use GCC/Clang builtins for bit arithmetics

This commit is contained in:
authmillenon 2013-11-03 14:32:25 +01:00
parent 05ececa079
commit 18e97f6dd5
3 changed files with 29 additions and 36 deletions

View File

@ -11,6 +11,7 @@
* \{ * \{
* \file * \file
* \author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de> * \author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
* \author Martin Lenders <mlenders@inf.fu-berlin.de>
* \} * \}
*/ */
@ -39,29 +40,3 @@ number_of_highest_bit(unsigned v)
return r; return r;
} }
/*---------------------------------------------------------------------------*/
unsigned
number_of_lowest_bit(register unsigned v)
{
register unsigned r = 0;
while ((v & 0x01) == 0) {
v >>= 1;
r++;
};
return r;
}
/*---------------------------------------------------------------------------*/
unsigned
number_of_bits_set(unsigned v)
{
unsigned c; // c accumulates the total bits set in v
for (c = 0; v; c++) {
v &= v - 1; // clear the least significant bit set
}
return c;
}

View File

@ -13,6 +13,7 @@
* @file * @file
* @author Freie Universität Berlin, Computer Systems & Telematics * @author Freie Universität Berlin, Computer Systems & Telematics
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de> * @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
*/ */
#ifndef BITARITHM_H_ #ifndef BITARITHM_H_
@ -78,22 +79,17 @@ unsigned number_of_highest_bit(unsigned v);
/** /**
* @brief Returns the number of the lowest '1' bit in a value * @brief Returns the number of the lowest '1' bit in a value
* @param[in] v Input value - must be unequal to '0', otherwise the * @param[in] v Input value
* function will produce an infinite loop * @return Bit Number, 0 for least significant bit, -1 if input is 0
* @return Bit Number
*
* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
*/ */
unsigned number_of_lowest_bit(register unsigned v); #define number_of_lowest_bit(v) (__builtin_ffs(v)-1)
/** /**
* @brief Returns the number of bits set in a value * @brief Returns the number of bits set in a value
* @param[in] v Input value * @param[in] v Input value
* @return Number of set bits * @return Number of set bits
*
* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
*/ */
unsigned number_of_bits_set(unsigned v); #define number_of_bits_set(v) (__builtin_popcount(v))
/** /**
* @} * @}

View File

@ -1,3 +1,23 @@
/**
* Copyright (C) 2013 INRIA.
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @addtogroup posix
* @{
* @file
* @brief POSIX-like IO
*
* @author Freie Universität Berlin
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Stephan Zeisberg <zeisberg@mi.fu-berlin.de>
* @author Oliver Hahm <oleg@hobbykeller.org>
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef __READ_H #ifndef __READ_H
#define __READ_H #define __READ_H
@ -15,5 +35,7 @@ int posix_open(int pid, int flags);
int posix_close(int pid); int posix_close(int pid);
int posix_read(int pid, char *buffer, int bufsize); int posix_read(int pid, char *buffer, int bufsize);
int posix_write(int pid, char *buffer, int bufsize); int posix_write(int pid, char *buffer, int bufsize);
/**
* @}
*/
#endif /* __READ_H */ #endif /* __READ_H */