This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Further ANSI changes now builds and passes (most) tests
[perl5.git] / ext / SDBM_File / sdbm / hash.c
1 /*
2  * sdbm - ndbm work-alike hashed database library
3  * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
4  * author: oz@nexus.yorku.ca
5  * status: public domain. keep it that way.
6  *
7  * hashing routine
8  */
9
10 #include "config.h"
11 #include "sdbm.h"
12 /*
13  * polynomial conversion ignoring overflows
14  * [this seems to work remarkably well, in fact better
15  * then the ndbm hash function. Replace at your own risk]
16  * use: 65599   nice.
17  *      65587   even better. 
18  */
19 long
20 sdbm_hash(register char *str, register int len)
21 {
22         register unsigned long n = 0;
23
24 #ifdef DUFF
25
26 #define HASHC   n = *str++ + 65599 * n
27
28         if (len > 0) {
29                 register int loop = (len + 8 - 1) >> 3;
30
31                 switch(len & (8 - 1)) {
32                 case 0: do {
33                         HASHC;  case 7: HASHC;
34                 case 6: HASHC;  case 5: HASHC;
35                 case 4: HASHC;  case 3: HASHC;
36                 case 2: HASHC;  case 1: HASHC;
37                         } while (--loop);
38                 }
39
40         }
41 #else
42         while (len--)
43                 n = *str++ + 65599 * n;
44 #endif
45         return n;
46 }