aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/string')
-rw-r--r--lib/libc/string/ffs.c12
-rw-r--r--lib/libc/string/ffsl.c12
-rw-r--r--lib/libc/string/ffsll.c12
-rw-r--r--lib/libc/string/fls.c13
-rw-r--r--lib/libc/string/flsl.c14
-rw-r--r--lib/libc/string/flsll.c13
6 files changed, 34 insertions, 42 deletions
diff --git a/lib/libc/string/ffs.c b/lib/libc/string/ffs.c
index 738ef90ce091..34140e3e4d85 100644
--- a/lib/libc/string/ffs.c
+++ b/lib/libc/string/ffs.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
@@ -43,11 +47,5 @@ __FBSDID("$FreeBSD$");
int
ffs(int mask)
{
- int bit;
-
- if (mask == 0)
- return(0);
- for (bit = 1; !(mask & 1); bit++)
- mask = (unsigned int)mask >> 1;
- return (bit);
+ return (mask == 0 ? 0 : __builtin_ctz(mask) + 1);
}
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);
}
diff --git a/lib/libc/string/ffsll.c b/lib/libc/string/ffsll.c
index 91886de2f127..e94fb518eb03 100644
--- a/lib/libc/string/ffsll.c
+++ b/lib/libc/string/ffsll.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
ffsll(long long mask)
{
- int bit;
-
- if (mask == 0)
- return (0);
- for (bit = 1; !(mask & 1); bit++)
- mask = (unsigned long long)mask >> 1;
- return (bit);
+ return (mask == 0 ? 0 : __builtin_ctzll(mask) + 1);
}
diff --git a/lib/libc/string/fls.c b/lib/libc/string/fls.c
index d9edc41f9599..3c4719776778 100644
--- a/lib/libc/string/fls.c
+++ b/lib/libc/string/fls.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
@@ -32,6 +36,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <limits.h>
#include <strings.h>
/*
@@ -40,11 +45,5 @@ __FBSDID("$FreeBSD$");
int
fls(int mask)
{
- int bit;
-
- if (mask == 0)
- return (0);
- for (bit = 1; mask != 1; bit++)
- mask = (unsigned int)mask >> 1;
- return (bit);
+ return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clz(mask));
}
diff --git a/lib/libc/string/flsl.c b/lib/libc/string/flsl.c
index 60370cf7d832..f5280b77a4e2 100644
--- a/lib/libc/string/flsl.c
+++ b/lib/libc/string/flsl.c
@@ -3,6 +3,11 @@
*
* 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
@@ -32,6 +37,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <limits.h>
#include <strings.h>
/*
@@ -40,11 +46,5 @@ __FBSDID("$FreeBSD$");
int
flsl(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 : CHAR_BIT * sizeof(mask) - __builtin_clzl(mask));
}
diff --git a/lib/libc/string/flsll.c b/lib/libc/string/flsll.c
index 275aaa0e2e15..ab504b8e592f 100644
--- a/lib/libc/string/flsll.c
+++ b/lib/libc/string/flsll.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
@@ -32,6 +36,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <limits.h>
#include <strings.h>
/*
@@ -40,11 +45,5 @@ __FBSDID("$FreeBSD$");
int
flsll(long long mask)
{
- int bit;
-
- if (mask == 0)
- return (0);
- for (bit = 1; mask != 1; bit++)
- mask = (unsigned long long)mask >> 1;
- return (bit);
+ return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clzll(mask));
}