summaryrefslogtreecommitdiff
path: root/lib/libc/string
diff options
context:
space:
mode:
authorRobert Clausecker <fuz@FreeBSD.org>2023-06-23 16:22:54 +0000
committerRobert Clausecker <fuz@FreeBSD.org>2023-07-03 20:18:27 +0000
commitee8b0c436d7221c25e8be3c3fe1f9da78b9d5b16 (patch)
tree21158245c9f8a5300a74a70882874cf1e0588bf6 /lib/libc/string
parent49390697b9265d08d3f831cf38cdc2f79e216c48 (diff)
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..c011b3390612 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 (__builtin_ffs(mask));
}
diff --git a/lib/libc/string/ffsl.c b/lib/libc/string/ffsl.c
index dbd894b9655b..6e1ac8ec45c1 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 (__builtin_ffsl(mask));
}
diff --git a/lib/libc/string/ffsll.c b/lib/libc/string/ffsll.c
index 91886de2f127..b945658b9008 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 (__builtin_ffsll(mask));
}
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));
}