From 07da7b2d454833089c2c9fd8a657a2698de3c73a Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Mon, 9 Dec 2013 22:44:53 +0100 Subject: [PATCH] reverted 18e97f6dd5457538c7bf39c6abcf346f5a080ca5 --- core/bitarithm.c | 27 ++++++++++++++++++++++++++- core/include/bitarithm.h | 14 +++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/core/bitarithm.c b/core/bitarithm.c index 40eefd7bb1..cda4b6e280 100644 --- a/core/bitarithm.c +++ b/core/bitarithm.c @@ -11,7 +11,6 @@ * \{ * \file * \author Kaspar Schleiser - * \author Martin Lenders * \} */ @@ -40,3 +39,29 @@ number_of_highest_bit(unsigned v) 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; +} + diff --git a/core/include/bitarithm.h b/core/include/bitarithm.h index 1df68464c1..6f38b28397 100644 --- a/core/include/bitarithm.h +++ b/core/include/bitarithm.h @@ -13,7 +13,6 @@ * @file * @author Freie Universität Berlin, Computer Systems & Telematics * @author Kaspar Schleiser - * @author Martin Lenders */ #ifndef BITARITHM_H_ @@ -79,17 +78,22 @@ unsigned number_of_highest_bit(unsigned v); /** * @brief Returns the number of the lowest '1' bit in a value - * @param[in] v Input value - * @return Bit Number, 0 for least significant bit, -1 if input is 0 + * @param[in] v Input value - must be unequal to '0', otherwise the + * function will produce an infinite loop + * @return Bit Number + * + * Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */ -#define number_of_lowest_bit(v) (__builtin_ffs(v)-1) +unsigned number_of_lowest_bit(register unsigned v); /** * @brief Returns the number of bits set in a value * @param[in] v Input value * @return Number of set bits + * + * Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */ -#define number_of_bits_set(v) (__builtin_popcount(v)) +unsigned number_of_bits_set(unsigned v); /** * @}