bitops: all macros (hmmm, mistake ?), more tests

This commit is contained in:
2024-01-06 18:23:29 +01:00
parent 459f18a019
commit 2c95124020
6 changed files with 504 additions and 412 deletions

View File

@@ -0,0 +1,45 @@
/* generic-clz.h - generic clz 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_CLZ_H_
#define _GENERIC_CLZ_H_
#include "br.h"
/* Adapted from: http://www-graphics.stanford.edu/%7Eseander/bithacks.html
*/
static __always_inline int __clz32_emulated(u32 n)
{
uint r, q;
r = (n > 0xFFFF) << 4; n >>= r;
q = (n > 0xFF ) << 3; n >>= q; r |= q;
q = (n > 0xF ) << 2; n >>= q; r |= q;
q = (n > 0x3 ) << 1; n >>= q; r |= q;
r |= (n >> 1);
return 31 - r;
}
static __always_inline int __clz64_emulated(u64 n)
{
uint r, q;
r = (n > 0xFFFFFFFF) << 5; n >>= r;
q = (n > 0xFFFF) << 4; n >>= q; r |= q;
q = (n > 0xFF ) << 3; n >>= q; r |= q;
q = (n > 0xF ) << 2; n >>= q; r |= q;
q = (n > 0x3 ) << 1; n >>= q; r |= q;
r |= (n >> 1);
return 63 - r;
}
#endif /* _GENERIC_CLZ_H_ */

View File

@@ -0,0 +1,130 @@
/* generic-ctz.h - generic ctz 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_CTZ_H_
#define _GENERIC_CTZ_H_
#include "br.h"
/* Adapted from: http://www-graphics.stanford.edu/%7Eseander/bithacks.html
*/
static __always_inline int __ctz32_emulated(u32 n)
{
int r = 0;
if (!(n & 0xffff)) {
n >>= 16;
r += 16;
}
if (!(n & 0xff)) {
n >>= 8;
r += 8;
}
if (!(n & 0xf)) {
n >>= 4;
r += 4;
}
if (!(n & 3)) {
n >>= 2;
r += 2;
}
if (!(n & 1)) {
n >>= 1;
r += 1;
}
return r;
}
#define __ctz32_emulated2(n) ({ \
int r = 0; \
if (!(n & 0xffff)) { \
n >>= 16; \
r += 16; \
} \
if (!(n & 0xff)) { \
n >>= 8; \
r += 8; \
} \
if (!(n & 0xf)) { \
n >>= 4; \
r += 4; \
} \
if (!(n & 3)) { \
n >>= 2; \
r += 2; \
} \
if (!(n & 1)) { \
n >>= 1; \
r += 1; \
} \
r; \
})
static __always_inline int __ctz64_emulated(u64 n)
{
int r = 0; /* !!!!!!!!!!!!!!!!!!!!!!!! */
if (!(n & 0xffffffff)) {
n >>= 32;
r += 32;
}
if (!(n & 0xffff)) {
n >>= 16;
r += 16;
}
if (!(n & 0xff)) {
n >>= 8;
r += 8;
}
if (!(n & 0xf)) {
n >>= 4;
r += 4;
}
if (!(n & 3)) {
n >>= 2;
r += 2;
}
if (!(n & 1)) {
n >>= 1;
r += 1;
}
return r;
}
#define __ctz64_emulated2(n) ({ \
int r = 0; \
if (!(n & 0xffffffff)) { \
n >>= 32; \
r += 32; \
} \
if (!(n & 0xffff)) { \
n >>= 16; \
r += 16; \
} \
if (!(n & 0xff)) { \
n >>= 8; \
r += 8; \
} \
if (!(n & 0xf)) { \
n >>= 4; \
r += 4; \
} \
if (!(n & 3)) { \
n >>= 2; \
r += 2; \
} \
if (!(n & 1)) { \
n >>= 1; \
r += 1; \
} \
r; \
})
#endif /* _GENERIC_CTZ_H_ */