1139815Simp/*- 274840Sken * Copyright (c) 1990, 1993 374840Sken * The Regents of the University of California. All rights reserved. 474840Sken * 574840Sken * Redistribution and use in source and binary forms, with or without 674840Sken * modification, are permitted provided that the following conditions 774840Sken * are met: 874840Sken * 1. Redistributions of source code must retain the above copyright 974840Sken * notice, this list of conditions and the following disclaimer. 1074840Sken * 2. Redistributions in binary form must reproduce the above copyright 1174840Sken * notice, this list of conditions and the following disclaimer in the 1274840Sken * documentation and/or other materials provided with the distribution. 1374840Sken * 4. Neither the name of the University nor the names of its contributors 1474840Sken * may be used to endorse or promote products derived from this software 1574840Sken * without specific prior written permission. 1674840Sken * 1774840Sken * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1874840Sken * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1974840Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2074840Sken * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2174840Sken * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2274840Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2374840Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2474840Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2574840Sken * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2674840Sken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2774840Sken * SUCH DAMAGE. 2874840Sken */ 2974840Sken 3074840Sken#if defined(LIBC_SCCS) && !defined(lint) 3174840Skenstatic char sccsid[] = "@(#)bsearch.c 8.1 (Berkeley) 6/4/93"; 3274840Sken#endif /* LIBC_SCCS and not lint */ 33116189Sobrien#include <sys/cdefs.h> 34116189Sobrien__FBSDID("$FreeBSD$"); 3574840Sken 3679418Sjulian#include <sys/param.h> 3779418Sjulian#include <sys/libkern.h> 3874840Sken 3974840Sken/* 4074840Sken * Perform a binary search. 4174840Sken * 4274840Sken * The code below is a bit sneaky. After a comparison fails, we 4374840Sken * divide the work in half by moving either left or right. If lim 4474840Sken * is odd, moving left simply involves halving lim: e.g., when lim 4574840Sken * is 5 we look at item 2, so we change lim to 2 so that we will 4674840Sken * look at items 0 & 1. If lim is even, the same applies. If lim 4774840Sken * is odd, moving right again involes halving lim, this time moving 4874840Sken * the base up one item past p: e.g., when lim is 5 we change base 4974840Sken * to item 3 and make lim 2 so that we will look at items 3 and 4. 5074840Sken * If lim is even, however, we have to shrink it by one before 5174840Sken * halving: e.g., when lim is 4, we still looked at item 2, so we 5274840Sken * have to make lim 3, then halve, obtaining 1, so that we will only 5374840Sken * look at item 3. 5474840Sken */ 5574840Skenvoid * 5674840Skenbsearch(key, base0, nmemb, size, compar) 5774840Sken register const void *key; 5874840Sken const void *base0; 5974840Sken size_t nmemb; 6074840Sken register size_t size; 6192741Salfred register int (*compar)(const void *, const void *); 6274840Sken{ 6374840Sken register const char *base = base0; 6474840Sken register size_t lim; 6574840Sken register int cmp; 6674840Sken register const void *p; 6774840Sken 6874840Sken for (lim = nmemb; lim != 0; lim >>= 1) { 6974840Sken p = base + (lim >> 1) * size; 7074840Sken cmp = (*compar)(key, p); 7174840Sken if (cmp == 0) 7283289Speter return ((void *)(uintptr_t)p); 7374840Sken if (cmp > 0) { /* key > p: move right */ 7474840Sken base = (const char *)p + size; 7574840Sken lim--; 7674840Sken } /* else move left */ 7774840Sken } 7874840Sken return (NULL); 7974840Sken} 80