3 * $Id: unihash.c,v 1.1 2003/10/12 14:43:24 mdw Exp $
5 * Simple and efficient universal hashing for hashtables
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2003/10/12 14:43:24 mdw
38 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @unihash_setkey@ --- *
49 * Arguments: @unihash_info *i@ = where to store the precomputed tables
50 * @uint32 k@ = the key to set, randomly chosen
54 * Use: Calculates the tables required for efficient hashing.
57 static uint32 mul(uint32 x, uint32 y)
63 x = U32(x << 1) ^ UNIHASH_POLY;
71 void unihash_setkey(unihash_info *i, uint32 k)
77 for (a = 0; a < UNIHASH_NBATCH; a++) {
79 for (b = 0; b < 256; b++) {
80 i->s[a][0][b] = mul(x, b << 0);
81 i->s[a][1][b] = mul(x, b << 8);
82 i->s[a][2][b] = mul(x, b << 16);
83 i->s[a][3][b] = mul(x, b << 24);
88 /* --- @unihash_hash@ --- *
90 * Arguments: @const unihash_info *i@ = pointer to precomputed table
91 * @uint32 a@ = @i->[0][0][1]@ or value from previous call
92 * @const void *p@ = pointer to data to hash
93 * @size_t sz@ = size of the data
97 * Use: Hashes data. Call this as many times as needed.
100 uint32 unihash_hash(const unihash_info *i, uint32 a,
101 const void *p, size_t sz)
105 assert(UNIHASH_NBATCH == 4);
107 #define FULLMULT(u, x) \
108 (i->s[u][0][U8((x) >> 0)] ^ i->s[u][1][U8((x) >> 8)] ^ \
109 i->s[u][2][U8((x) >> 16)] ^ i->s[u][3][U8((x) >> 24)]);
111 #define BYTEMULT(u, x) i->s[u][0][x]
113 /* --- Do the main bulk in batches of %$n$% bytes --- *
115 * We have %$a$% and %$m_{n-1}, \ldots, m_1, m_0$%; we want
117 * %$a' = (a + m_{n-1}) k^n + m_{n-2} k^{n-1} + \cdots + m_1 k^2 + m_0 k$%
120 while (sz >= UNIHASH_NBATCH) {
123 a ^= BYTEMULT(2, *pp++);
124 a ^= BYTEMULT(1, *pp++);
125 a ^= BYTEMULT(0, *pp++);
128 /* --- The tail end is a smaller batch --- */
131 case 3: a ^= *pp++; a = FULLMULT(2, a); goto batch_2;
132 case 2: a ^= *pp++; a = FULLMULT(1, a); goto batch_1;
133 case 1: a ^= *pp++; a = FULLMULT(0, a); goto batch_0;
134 batch_2: a ^= BYTEMULT(1, *pp++);
135 batch_1: a ^= BYTEMULT(0, *pp++);
142 /* --- @unihash@ --- *
144 * Arguments: @const unihash_info *i@ = precomputed tables
145 * @const void *p@ = pointer to data to hash
146 * @size_t sz@ = size of the data
148 * Returns: The hash value computed.
150 * Use: All-in-one hashing function. No faster than using the
151 * separate calls, but more convenient.
154 uint32 unihash(const unihash_info *i, const void *p, size_t sz)
156 return (UNIHASH(i, p, sz));
159 /*----- That's all, folks -------------------------------------------------*/