aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/string/ffsl.c
diff options
context:
space:
mode:
authorRobert Clausecker <fuz@FreeBSD.org>2023-06-23 16:22:54 +0000
committerRobert Clausecker <fuz@FreeBSD.org>2023-07-21 08:52:37 +0000
commit2c03221cce0b7446cb681de0179349d2ee72c252 (patch)
tree8c977ea64a0f305d1609c35df210dfd7f3143867 /lib/libc/string/ffsl.c
parent65b618794f68aadbebbca11de99278c9ec1b2992 (diff)
downloadsrc-2c03221cce0b7446cb681de0179349d2ee72c252.tar.gz
src-2c03221cce0b7446cb681de0179349d2ee72c252.zip
lib/libc/string: replace ffs/fls implementations with clang builtins
Most architectures we support (except for riscv64) have instructions to compute these functions very quickly. Replace old code with the ftz and clz builtin functions, allowing clang to generate good code for all architectures. We cannot use the ffs builtins as gcc uses ffs() to implement these on some platforms, potentially causing an infinite loop. As a consequence, toss out arm and i386 ffs() implementations. Sponsored by: The FreeBSD Foundation Reported by: jlduran@gmail.com, jhb Approved by: mhorne, jhb MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D40730 Differential Revision: https://reviews.freebsd.org/D40966 (cherry picked from commit ee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16) (cherry picked from commit 3f5788e0ed8e85567f651ad360596b8c330af5a9)
Diffstat (limited to 'lib/libc/string/ffsl.c')
-rw-r--r--lib/libc/string/ffsl.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/lib/libc/string/ffsl.c b/lib/libc/string/ffsl.c
index dbd894b9655b..701e23cdf8f4 100644
--- a/lib/libc/string/ffsl.c
+++ b/lib/libc/string/ffsl.c
@@ -3,6 +3,10 @@
*
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
+ * Copyright (c) 2023 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by Robert Clausecker
+ * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -40,11 +44,5 @@ __FBSDID("$FreeBSD$");
int
ffsl(long mask)
{
- int bit;
-
- if (mask == 0)
- return (0);
- for (bit = 1; !(mask & 1); bit++)
- mask = (unsigned long)mask >> 1;
- return (bit);
+ return (mask == 0 ? 0 : __builtin_ctzl(mask) + 1);
}