|
|
|
@ -15,36 +15,47 @@
|
|
|
|
|
*
|
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
|
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
|
|
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
|
*
|
|
|
|
|
* @}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include "bitarithm.h"
|
|
|
|
|
|
|
|
|
|
unsigned bitarithm_msb(unsigned v)
|
|
|
|
|
{
|
|
|
|
|
if ((signed) v >= 0) {
|
|
|
|
|
v &= -v;
|
|
|
|
|
return bitarithm_lsb(v);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return sizeof (v) * 8 - 1;
|
|
|
|
|
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;
|
|
|
|
|
while (v >>= 1) { // unroll for more speed...
|
|
|
|
|
r++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned bitarithm_lsb(unsigned v)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
unsigned bitarithm_lsb(register unsigned v)
|
|
|
|
|
{
|
|
|
|
|
unsigned r = 0;
|
|
|
|
|
while ((v & 1) == 0) {
|
|
|
|
|
register unsigned r = 0;
|
|
|
|
|
|
|
|
|
|
while ((v & 0x01) == 0) {
|
|
|
|
|
v >>= 1;
|
|
|
|
|
r++;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
unsigned bitarithm_bits_set(unsigned v)
|
|
|
|
|
{
|
|
|
|
|
unsigned c; // c accumulates the total bits set in v
|
|
|
|
|