61 lines
1.6 KiB
C
61 lines
1.6 KiB
C
/* generic-ilog2.h - generic ilog2 implementations.
|
|
*
|
|
* Copyright (C) 2024 Bruno Raoult ("br")
|
|
* Licensed under the GNU General Public License v3.0 or later.
|
|
* Some rights reserved. See COPYING.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with this
|
|
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
|
*
|
|
*/
|
|
#ifndef _GENERIC_ILOG2_H_
|
|
#define _GENERIC_ILOG2_H_
|
|
|
|
#include "brlib.h"
|
|
|
|
/*
|
|
* See https://stackoverflow.com/a/11398748/3079831
|
|
*/
|
|
static const int __ilog2_32_table[32] = {
|
|
0, 9, 1, 10, 13, 21, 2, 29,
|
|
11, 14, 16, 18, 22, 25, 3, 30,
|
|
8, 12, 20, 28, 15, 17, 24, 7,
|
|
19, 27, 23, 6, 26, 5, 4, 31
|
|
};
|
|
|
|
static const int __ilog2_64_table[64] = {
|
|
63, 0, 58, 1, 59, 47, 53, 2,
|
|
60, 39, 48, 27, 54, 33, 42, 3,
|
|
61, 51, 37, 40, 49, 18, 28, 20,
|
|
55, 30, 34, 11, 43, 14, 22, 4,
|
|
62, 57, 46, 52, 38, 26, 32, 41,
|
|
50, 36, 17, 19, 29, 10, 13, 21,
|
|
56, 45, 25, 31, 35, 16, 9, 12,
|
|
44, 24, 15, 8, 23, 7, 6, 5
|
|
};
|
|
|
|
static __always_inline __const int __ilog2_32_emulated (u32 n)
|
|
{
|
|
n |= n >> 1;
|
|
n |= n >> 2;
|
|
n |= n >> 4;
|
|
n |= n >> 8;
|
|
n |= n >> 16;
|
|
return __ilog2_32_table[(n*0x07C4ACDD) >> 27];
|
|
}
|
|
|
|
static __always_inline __const int __ilog2_64_emulated (u64 n)
|
|
{
|
|
n |= n >> 1;
|
|
n |= n >> 2;
|
|
n |= n >> 4;
|
|
n |= n >> 8;
|
|
n |= n >> 16;
|
|
n |= n >> 32;
|
|
return __ilog2_64_table[((n - (n >> 1))*0x07EDD5E59A4E28C2LL) >> 58];
|
|
}
|
|
|
|
#endif /* _GENERIC_ILOG2_H_ */
|