xref: /freebsd/sys/libkern/bsearch.c (revision fdafd315)
1d6ea0262SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro 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 
320b1ae809SJulian Elischer #include <sys/param.h>
330b1ae809SJulian Elischer #include <sys/libkern.h>
343393f8daSKenneth D. Merry 
353393f8daSKenneth D. Merry /*
363393f8daSKenneth D. Merry  * Perform a binary search.
373393f8daSKenneth D. Merry  *
383393f8daSKenneth D. Merry  * The code below is a bit sneaky.  After a comparison fails, we
393393f8daSKenneth D. Merry  * divide the work in half by moving either left or right. If lim
403393f8daSKenneth D. Merry  * is odd, moving left simply involves halving lim: e.g., when lim
413393f8daSKenneth D. Merry  * is 5 we look at item 2, so we change lim to 2 so that we will
423393f8daSKenneth D. Merry  * look at items 0 & 1.  If lim is even, the same applies.  If lim
435cacfa70SGordon Bergling  * is odd, moving right again involves halving lim, this time moving
443393f8daSKenneth D. Merry  * the base up one item past p: e.g., when lim is 5 we change base
453393f8daSKenneth D. Merry  * to item 3 and make lim 2 so that we will look at items 3 and 4.
463393f8daSKenneth D. Merry  * If lim is even, however, we have to shrink it by one before
473393f8daSKenneth D. Merry  * halving: e.g., when lim is 4, we still looked at item 2, so we
483393f8daSKenneth D. Merry  * have to make lim 3, then halve, obtaining 1, so that we will only
493393f8daSKenneth D. Merry  * look at item 3.
503393f8daSKenneth D. Merry  */
513393f8daSKenneth D. Merry void *
bsearch(const void * key,const void * base0,size_t nmemb,size_t size,int (* compar)(const void *,const void *))524a8dea8cSEd Maste bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
534a8dea8cSEd Maste     int (*compar)(const void *, const void *))
543393f8daSKenneth D. Merry {
55484820d4SConrad Meyer 	const char *base = base0;
56484820d4SConrad Meyer 	size_t lim;
57484820d4SConrad Meyer 	int cmp;
58484820d4SConrad Meyer 	const void *p;
593393f8daSKenneth D. Merry 
603393f8daSKenneth D. Merry 	for (lim = nmemb; lim != 0; lim >>= 1) {
613393f8daSKenneth D. Merry 		p = base + (lim >> 1) * size;
623393f8daSKenneth D. Merry 		cmp = (*compar)(key, p);
633393f8daSKenneth D. Merry 		if (cmp == 0)
64531c9dd5SPeter Wemm 			return ((void *)(uintptr_t)p);
653393f8daSKenneth D. Merry 		if (cmp > 0) {	/* key > p: move right */
663393f8daSKenneth D. Merry 			base = (const char *)p + size;
673393f8daSKenneth D. Merry 			lim--;
683393f8daSKenneth D. Merry 		}		/* else move left */
693393f8daSKenneth D. Merry 	}
703393f8daSKenneth D. Merry 	return (NULL);
713393f8daSKenneth D. Merry }
72