2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-18 17:21:38 +02:00
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_util
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file bitarithm.c
|
|
|
|
* @brief Bit arithmetic helper functions implementation
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
|
|
|
|
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2013-11-27 16:28:31 +01:00
|
|
|
* @}
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
number_of_highest_bit(unsigned v)
|
|
|
|
{
|
|
|
|
register unsigned r; // result of log2(v) will go here
|
|
|
|
|
|
|
|
#if ARCH_32_BIT
|
|
|
|
register unsigned shift;
|
|
|
|
|
|
|
|
r = (v > 0xFFFF) << 4; v >>= r;
|
|
|
|
shift = (v > 0xFF ) << 3; v >>= shift; r |= shift;
|
|
|
|
shift = (v > 0xF ) << 2; v >>= shift; r |= shift;
|
|
|
|
shift = (v > 0x3 ) << 1; v >>= shift; r |= shift;
|
|
|
|
r |= (v >> 1);
|
|
|
|
#else
|
|
|
|
r = 0;
|
2013-06-24 22:37:35 +02:00
|
|
|
while (v >>= 1) { // unroll for more speed...
|
2010-09-22 15:10:42 +02:00
|
|
|
r++;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2013-12-09 22:44:53 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|