OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uC-libc/] [misc/] [bsearch.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
 
2
/*
3
 * This file lifted in toto from 'Dlibs' on the atari ST  (RdeBath)
4
 *
5
 *
6
 *    Dale Schumacher                         399 Beacon Ave.
7
 *    (alias: Dalnefre')                      St. Paul, MN  55104
8
 *    dal@syntel.UUCP                         United States of America
9
 *  "It's not reality that's important, but how you perceive things."
10
 */
11
#include <stdio.h>
12
 
13
static int _bsearch;            /* index of element found, or where to
14
                                 * insert */
15
 
16
char *
17
bsearch(key, base, num, size, cmp)
18
register char *key;             /* item to search for */
19
register char *base;            /* base address */
20
int   num;                      /* number of elements */
21
register int size;              /* element size in bytes */
22
register int (*cmp) ();         /* comparison function */
23
{
24
   register int a, b, c, dir;
25
 
26
   a = 0;
27
   b = num - 1;
28
   while (a <= b)
29
   {
30
      c = (a + b) >> 1;         /* == ((a + b) / 2) */
31
      if (dir = (*cmp) ((base + (c * size)), key))
32
      {
33
         if (dir > 0)
34
            b = c - 1;
35
         else                   /* (dir < 0) */
36
            a = c + 1;
37
      }
38
      else
39
      {
40
         _bsearch = c;
41
         return (base + (c * size));
42
      }
43
   }
44
   _bsearch = b;
45
   return (NULL);
46
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.