Back to index

glibc  2.9
nis_hash.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 1999, 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <rpcsvc/nis.h>
00021 
00022 /* This is from libc/db/hash/hash_func.c, hash3 is static there */
00023 /*
00024  * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
00025  * units.  On the first time through the loop we get the "leftover bytes"
00026  * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
00027  * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
00028  * this routine is heavily used enough, it's worth the ugly coding.
00029  *
00030  * OZ's original sdbm hash
00031  */
00032 uint32_t
00033 __nis_hash (const void *keyarg, register size_t len)
00034 {
00035   register const u_char *key;
00036   register size_t loop;
00037   register uint32_t h;
00038 
00039 #define HASHC   h = *key++ + 65599 * h
00040 
00041   h = 0;
00042   key = keyarg;
00043   if (len > 0)
00044     {
00045       loop = (len + 8 - 1) >> 3;
00046       switch (len & (8 - 1))
00047        {
00048        case 0:
00049          do {
00050            HASHC;
00051            /* FALLTHROUGH */
00052          case 7:
00053            HASHC;
00054            /* FALLTHROUGH */
00055          case 6:
00056            HASHC;
00057            /* FALLTHROUGH */
00058          case 5:
00059            HASHC;
00060            /* FALLTHROUGH */
00061          case 4:
00062            HASHC;
00063            /* FALLTHROUGH */
00064          case 3:
00065            HASHC;
00066            /* FALLTHROUGH */
00067          case 2:
00068            HASHC;
00069            /* FALLTHROUGH */
00070          case 1:
00071            HASHC;
00072          } while (--loop);
00073        }
00074     }
00075   return h;
00076 }