8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / msgfmt / gnu_hash.c
blobd3976c0f040685c3128f3e2d7b0a6bc25087faa2
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "gnu_msgfmt.h"
30 #include "gnu_prime.h"
34 * hashpjw
36 * Calculates the hash value from the specified string.
37 * Actual hashid will be mod(hash value, PRIME_NUMBER).
39 * Ref: Compilers - Principles, Techniques, and Tools
40 * Aho, Sethi, and Ullman
42 unsigned int
43 hashpjw(const char *str)
45 const char *p;
46 unsigned int h = 0, g;
48 for (p = str; *p; p++) {
49 h = (h << 4) + *p;
50 g = h & 0xf0000000;
51 if (g) {
52 h = h ^ (g >> 24);
53 h = h ^ g;
57 return (h);
60 static unsigned int
61 find_prime_big(unsigned int n)
63 int t;
64 unsigned int max_tbl_prime, prd;
65 max_tbl_prime = prime[MAX_PRIME_INDEX] + 2;
67 for (; ; ) {
68 for (t = 1; t <= MAX_PRIME_INDEX; t++) {
69 if (n % prime[t] == 0) {
70 /* n is not a prime number */
71 break;
74 if (t <= MAX_PRIME_INDEX) {
75 n += 2;
76 continue;
79 prd = max_tbl_prime;
80 while ((prd * prd < n) && (n % prd != 0)) {
81 prd += 2;
83 if (n % prd == 0) {
84 n += 2;
85 continue;
87 return (n);
89 /* NOTREACHED */
92 unsigned int
93 find_prime(unsigned int tbl_size)
95 int t, d;
96 unsigned int n, prd;
98 /* for compatibility with GNU msgfmt */
99 if (tbl_size == 1)
100 return (1);
101 else if (tbl_size == 2)
102 return (5);
104 n = 4 * tbl_size / 3;
106 /* make n an odd number */
107 n |= 1;
109 prd = n / 100;
110 if (prd <= MAX_INDEX_INDEX) {
111 /* first, search the table */
112 for (t = index[prd] + 1; t <= MAX_PRIME_INDEX; t++) {
113 if (prime[t] >= n)
114 return (prime[t]);
116 error(ERR_PRIME, n);
117 /* NOTREACHED */
119 t = START_SEARCH_INDEX;
120 for (; ; ) {
121 while (prime[t] * prime[t] < n) {
122 if (t == MAX_PRIME_INDEX) {
123 return (find_prime_big(n));
125 t++;
127 for (d = 1; d <= t; d++) {
128 if (n % prime[d] == 0) {
129 /* n is not a prime number */
130 break;
133 if (d > t) {
134 /* n is a prime number */
135 return (n);
137 n += 2;
139 /* NOTREACHED */
142 unsigned int
143 get_hash_index(unsigned int *hash_tbl, unsigned int hash_value,
144 unsigned int hash_size)
146 unsigned int idx, inc;
148 idx = hash_value % hash_size;
149 inc = 1 + (hash_value % (hash_size - 2));
151 for (; ; ) {
152 if (!hash_tbl[idx])
153 return (idx);
154 idx = (idx + inc) % hash_size;
156 /* NOTREACHED */