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
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
2014-02-11 18:15:43 +01:00
|
|
|
|
2013-11-27 16:28:31 +01:00
|
|
|
/**
|
|
|
|
* @ingroup core_util
|
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Bit arithmetic helper functions implementation
|
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2015-02-08 18:51:25 +01:00
|
|
|
* @author Martine 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>
|
|
|
|
|
2017-10-06 23:45:25 +02:00
|
|
|
#include "bitarithm.h"
|
|
|
|
|
2020-08-04 15:06:07 +02:00
|
|
|
unsigned bitarith_msb_32bit_no_native_clz(unsigned v)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2018-02-05 11:39:34 +01:00
|
|
|
register unsigned r; /* result of log2(v) will go here */
|
2014-11-26 17:20:47 +01:00
|
|
|
register unsigned shift;
|
|
|
|
|
2019-10-29 17:51:21 +01:00
|
|
|
/* begin{code-style-ignore} */
|
2014-11-26 17:20:47 +01:00
|
|
|
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);
|
2019-10-29 17:51:21 +01:00
|
|
|
/* end{code-style-ignore} */
|
2014-11-26 17:20:47 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
unsigned bitarithm_bits_set(unsigned v)
|
2013-12-09 22:44:53 +01:00
|
|
|
{
|
2018-02-05 11:39:34 +01:00
|
|
|
unsigned c; /* c accumulates the total bits set in v */
|
2013-12-09 22:44:53 +01:00
|
|
|
|
|
|
|
for (c = 0; v; c++) {
|
2018-02-05 11:39:34 +01:00
|
|
|
v &= v - 1; /* clear the least significant bit set */
|
2013-12-09 22:44:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2017-10-06 23:45:25 +02:00
|
|
|
|
2018-10-05 15:14:43 +02:00
|
|
|
#if !ARCH_32_BIT
|
|
|
|
uint8_t bitarithm_bits_set_u32(uint32_t v)
|
|
|
|
{
|
|
|
|
uint8_t c;
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2018-10-05 15:14:43 +02:00
|
|
|
for (c = 0; v; c++) {
|
|
|
|
v &= v - 1; /* clear the least significant bit set */
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-29 13:39:03 +02:00
|
|
|
const uint8_t bitarithm_MultiplyDeBruijnBitPosition[32] =
|
2017-10-06 23:45:25 +02:00
|
|
|
{
|
|
|
|
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
|
|
|
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
|
|
|
|
};
|