xref: /freebsd/sys/libkern/bsearch.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1d6ea0262SWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
43393f8daSKenneth D. Merry  * Copyright (c) 1990, 1993
53393f8daSKenneth D. Merry  *	The Regents of the University of California.  All rights reserved.
63393f8daSKenneth D. Merry  *
73393f8daSKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
83393f8daSKenneth D. Merry  * modification, are permitted provided that the following conditions
93393f8daSKenneth D. Merry  * are met:
103393f8daSKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
113393f8daSKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
123393f8daSKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
133393f8daSKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
143393f8daSKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
163393f8daSKenneth D. Merry  *    may be used to endorse or promote products derived from this software
173393f8daSKenneth D. Merry  *    without specific prior written permission.
183393f8daSKenneth D. Merry  *
193393f8daSKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
203393f8daSKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213393f8daSKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223393f8daSKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
233393f8daSKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243393f8daSKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253393f8daSKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263393f8daSKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273393f8daSKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283393f8daSKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293393f8daSKenneth D. Merry  * SUCH DAMAGE.
303393f8daSKenneth D. Merry  */
313393f8daSKenneth D. Merry 
323393f8daSKenneth D. Merry #if defined(LIBC_SCCS) && !defined(lint)
333393f8daSKenneth D. Merry static char sccsid[] = "@(#)bsearch.c	8.1 (Berkeley) 6/4/93";
343393f8daSKenneth D. Merry #endif /* LIBC_SCCS and not lint */
35ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
36ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
373393f8daSKenneth D. Merry 
380b1ae809SJulian Elischer #include <sys/param.h>
390b1ae809SJulian Elischer #include <sys/libkern.h>
403393f8daSKenneth D. Merry 
413393f8daSKenneth D. Merry /*
423393f8daSKenneth D. Merry  * Perform a binary search.
433393f8daSKenneth D. Merry  *
443393f8daSKenneth D. Merry  * The code below is a bit sneaky.  After a comparison fails, we
453393f8daSKenneth D. Merry  * divide the work in half by moving either left or right. If lim
463393f8daSKenneth D. Merry  * is odd, moving left simply involves halving lim: e.g., when lim
473393f8daSKenneth D. Merry  * is 5 we look at item 2, so we change lim to 2 so that we will
483393f8daSKenneth D. Merry  * look at items 0 & 1.  If lim is even, the same applies.  If lim
493393f8daSKenneth D. Merry  * is odd, moving right again involes halving lim, this time moving
503393f8daSKenneth D. Merry  * the base up one item past p: e.g., when lim is 5 we change base
513393f8daSKenneth D. Merry  * to item 3 and make lim 2 so that we will look at items 3 and 4.
523393f8daSKenneth D. Merry  * If lim is even, however, we have to shrink it by one before
533393f8daSKenneth D. Merry  * halving: e.g., when lim is 4, we still looked at item 2, so we
543393f8daSKenneth D. Merry  * have to make lim 3, then halve, obtaining 1, so that we will only
553393f8daSKenneth D. Merry  * look at item 3.
563393f8daSKenneth D. Merry  */
573393f8daSKenneth D. Merry void *
584a8dea8cSEd Maste bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
594a8dea8cSEd Maste     int (*compar)(const void *, const void *))
603393f8daSKenneth D. Merry {
61484820d4SConrad Meyer 	const char *base = base0;
62484820d4SConrad Meyer 	size_t lim;
63484820d4SConrad Meyer 	int cmp;
64484820d4SConrad Meyer 	const void *p;
653393f8daSKenneth D. Merry 
663393f8daSKenneth D. Merry 	for (lim = nmemb; lim != 0; lim >>= 1) {
673393f8daSKenneth D. Merry 		p = base + (lim >> 1) * size;
683393f8daSKenneth D. Merry 		cmp = (*compar)(key, p);
693393f8daSKenneth D. Merry 		if (cmp == 0)
70531c9dd5SPeter Wemm 			return ((void *)(uintptr_t)p);
713393f8daSKenneth D. Merry 		if (cmp > 0) {	/* key > p: move right */
723393f8daSKenneth D. Merry 			base = (const char *)p + size;
733393f8daSKenneth D. Merry 			lim--;
743393f8daSKenneth D. Merry 		}		/* else move left */
753393f8daSKenneth D. Merry 	}
763393f8daSKenneth D. Merry 	return (NULL);
773393f8daSKenneth D. Merry }
78