aboutsummaryrefslogtreecommitdiff
path: root/common/bits.c
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2022-10-29 20:10:09 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2022-10-29 20:10:09 +0300
commit6303dd9b55a1871387122525e456747eeb860932 (patch)
tree5ade67069f4b7f1e3d85e31c295da4f0b17c4141 /common/bits.c
parente2631ad54db87b71943040dcb5a2fc0f4b850716 (diff)
downloadkmi-6303dd9b55a1871387122525e456747eeb860932.tar.gz
kmi-6303dd9b55a1871387122525e456747eeb860932.zip
rewrite pmem
Diffstat (limited to 'common/bits.c')
-rw-r--r--common/bits.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/common/bits.c b/common/bits.c
index 2efbe2f..5691617 100644
--- a/common/bits.c
+++ b/common/bits.c
@@ -36,3 +36,23 @@ __weak uint64_t __bswap64(const uint64_t u)
(u & 0x000000000000ff00ULL) << 40 |
(u & 0x00000000000000ffULL) << 56;
}
+
+#undef ffs
+__weak int ffs(int v)
+{
+ /* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightParallel */
+ if (v == 0)
+ return 0;
+
+ int c = 32;
+ v &= -v;
+
+ if (v) c--;
+ if (v & 0x0000FFFF) c -= 16;
+ if (v & 0x00FF00FF) c -= 8;
+ if (v & 0x0F0F0F0F) c -= 4;
+ if (v & 0x33333333) c -= 2;
+ if (v & 0x55555555) c -= 1;
+
+ return c + 1;
+}