mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] bsearch: simplify and optimize
@ 2018-07-22  0:47 Fāng-ruì Sòng
  2018-07-22 18:51 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Fāng-ruì Sòng @ 2018-07-22  0:47 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 0 bytes --]



[-- Attachment #2: bsearch.patch --]
[-- Type: text/x-diff, Size: 906 bytes --]

From 64d86f96fa404dbaa40de8a1979ecadca6add4ca Mon Sep 17 00:00:00 2001
From: Fangrui Song <i@maskray.me>
Date: Sat, 21 Jul 2018 17:34:00 -0700
Subject: [PATCH] bsearch: simplify and optimize

---
 src/stdlib/bsearch.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/stdlib/bsearch.c b/src/stdlib/bsearch.c
index 61d89367..2a998cc6 100644
--- a/src/stdlib/bsearch.c
+++ b/src/stdlib/bsearch.c
@@ -7,14 +7,13 @@ void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (
 	while (nel > 0) {
 		try = (char *)base + width*(nel/2);
 		sign = cmp(key, try);
-		if (!sign) return try;
-		else if (nel == 1) break;
-		else if (sign < 0)
+		if (sign < 0)
 			nel /= 2;
-		else {
-			base = try;
-			nel -= nel/2;
-		}
+		else if (sign > 0) {
+			base = (char *)try + width;
+			nel -= nel/2+1;
+		} else
+			return try;
 	}
 	return NULL;
 }
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-07-22 23:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-22  0:47 [PATCH] bsearch: simplify and optimize Fāng-ruì Sòng
2018-07-22 18:51 ` Rich Felker
2018-07-22 23:03   ` Ray

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).