xref: /titanic_54/usr/src/cmd/spell/hashlook.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include "hash.h"
35*7c478bd9Sstevel@tonic-gate #include "huff.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate unsigned *table;
38*7c478bd9Sstevel@tonic-gate int hindex[NI];
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #define	B (BYTE * sizeof (unsigned))
41*7c478bd9Sstevel@tonic-gate #define	L (BYTE * sizeof (long)-1)
42*7c478bd9Sstevel@tonic-gate #define	MASK (~((unsigned long)1L<<L))
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #ifdef pdp11	/* sizeof (unsigned)==sizeof(long)/2 */
45*7c478bd9Sstevel@tonic-gate #define	fetch(wp, bp)\
46*7c478bd9Sstevel@tonic-gate 	(((((long)wp[0]<<B)|wp[1])<<(B-bp))|(wp[2]>>bp))
47*7c478bd9Sstevel@tonic-gate #else 		/* sizeof (unsigned)==sizeof(long) */
48*7c478bd9Sstevel@tonic-gate #define	fetch(wp, bp) ((wp[0] << (B - bp)) | (wp[1] >> bp))
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate int
52*7c478bd9Sstevel@tonic-gate hashlook(char *s)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	unsigned long h;
55*7c478bd9Sstevel@tonic-gate 	unsigned long t;
56*7c478bd9Sstevel@tonic-gate 	register bp;
57*7c478bd9Sstevel@tonic-gate 	register unsigned *wp;
58*7c478bd9Sstevel@tonic-gate 	long sum;
59*7c478bd9Sstevel@tonic-gate 	unsigned *tp;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	h = hash(s);
62*7c478bd9Sstevel@tonic-gate 	t = h>>(HASHWIDTH-INDEXWIDTH);
63*7c478bd9Sstevel@tonic-gate 	wp = &table[hindex[t]];
64*7c478bd9Sstevel@tonic-gate 	tp = &table[hindex[t+1]];
65*7c478bd9Sstevel@tonic-gate 	bp = B;
66*7c478bd9Sstevel@tonic-gate 	sum = (long)t<<(HASHWIDTH-INDEXWIDTH);
67*7c478bd9Sstevel@tonic-gate 	for (;;) {
68*7c478bd9Sstevel@tonic-gate 		{
69*7c478bd9Sstevel@tonic-gate 			/*
70*7c478bd9Sstevel@tonic-gate 			 * this block is equivalent to:
71*7c478bd9Sstevel@tonic-gate 			 * bp -= decode((fetch(wp, bp) >> 1) & MASK, &t);
72*7c478bd9Sstevel@tonic-gate 			 */
73*7c478bd9Sstevel@tonic-gate 			long y;
74*7c478bd9Sstevel@tonic-gate 			long v;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 			/*
77*7c478bd9Sstevel@tonic-gate 			 * shift 32 on those machines leaves destination
78*7c478bd9Sstevel@tonic-gate 			 * unchanged
79*7c478bd9Sstevel@tonic-gate 			 */
80*7c478bd9Sstevel@tonic-gate 			if (bp == 0)
81*7c478bd9Sstevel@tonic-gate 				y = 0;
82*7c478bd9Sstevel@tonic-gate 			else
83*7c478bd9Sstevel@tonic-gate 				y = wp[0] << (B - bp);
84*7c478bd9Sstevel@tonic-gate 			if (bp < 32)
85*7c478bd9Sstevel@tonic-gate 				y |= (wp[1] >> bp);
86*7c478bd9Sstevel@tonic-gate 			y = (y >> 1) & MASK;
87*7c478bd9Sstevel@tonic-gate 			if (y < cs) {
88*7c478bd9Sstevel@tonic-gate 				t = y >> (long) (L+1-w);
89*7c478bd9Sstevel@tonic-gate 				bp -= w-1;
90*7c478bd9Sstevel@tonic-gate 			} else {
91*7c478bd9Sstevel@tonic-gate 				for (bp -= w, v = v0; y >= qcs;
92*7c478bd9Sstevel@tonic-gate 				    y = (y << 1) & MASK, v += n)
93*7c478bd9Sstevel@tonic-gate 					bp -= 1;
94*7c478bd9Sstevel@tonic-gate 				t = v + (y>> (long)(L-w));
95*7c478bd9Sstevel@tonic-gate 			}
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 		while (bp <= 0) {
98*7c478bd9Sstevel@tonic-gate 			bp += B;
99*7c478bd9Sstevel@tonic-gate 			wp++;
100*7c478bd9Sstevel@tonic-gate 		}
101*7c478bd9Sstevel@tonic-gate 		if (wp >= tp && (wp > tp||bp < B))
102*7c478bd9Sstevel@tonic-gate 			return (0);
103*7c478bd9Sstevel@tonic-gate 		sum += t;
104*7c478bd9Sstevel@tonic-gate 		if (sum < h)
105*7c478bd9Sstevel@tonic-gate 			continue;
106*7c478bd9Sstevel@tonic-gate 		return (sum == h);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate int
112*7c478bd9Sstevel@tonic-gate prime(char *file)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	register FILE *f;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate #ifdef pdp11	/* because of insufficient address space for buffers */
117*7c478bd9Sstevel@tonic-gate 	fd = dup(0);
118*7c478bd9Sstevel@tonic-gate 	close(0);
119*7c478bd9Sstevel@tonic-gate 	if (open(file, 0) != 0)
120*7c478bd9Sstevel@tonic-gate 		return (0);
121*7c478bd9Sstevel@tonic-gate 	f = stdin;
122*7c478bd9Sstevel@tonic-gate 	if (rhuff(f) == 0 || read(fileno(f), (char *)hindex,
123*7c478bd9Sstevel@tonic-gate 	    NI * sizeof (*hindex)) != NI * sizeof (*hindex) ||
124*7c478bd9Sstevel@tonic-gate 	    (table = (unsigned *)malloc(hindex[NI-1] * sizeof (*table))) == 0 ||
125*7c478bd9Sstevel@tonic-gate 	    read(fileno(f), (char *)table, sizeof (*table) * hindex[NI-1]) !=
126*7c478bd9Sstevel@tonic-gate 	    hindex[NI-1] * sizeof (*table))
127*7c478bd9Sstevel@tonic-gate 		return (0);
128*7c478bd9Sstevel@tonic-gate 	close(0);
129*7c478bd9Sstevel@tonic-gate 	if (dup(fd) != 0)
130*7c478bd9Sstevel@tonic-gate 		return (0);
131*7c478bd9Sstevel@tonic-gate 	close(fd);
132*7c478bd9Sstevel@tonic-gate #else
133*7c478bd9Sstevel@tonic-gate 	if ((f = fopen(file, "r")) == NULL)
134*7c478bd9Sstevel@tonic-gate 		return (0);
135*7c478bd9Sstevel@tonic-gate 	if (rhuff(f) == 0 ||
136*7c478bd9Sstevel@tonic-gate 	    fread((char *)hindex, sizeof (*hindex),  NI, f) != NI ||
137*7c478bd9Sstevel@tonic-gate 	    (table = (unsigned *)malloc(hindex[NI-1] * sizeof (*table))) == 0 ||
138*7c478bd9Sstevel@tonic-gate 	    fread((char *)table, sizeof (*table), hindex[NI-1], f) !=
139*7c478bd9Sstevel@tonic-gate 	    hindex[NI-1])
140*7c478bd9Sstevel@tonic-gate 		return (0);
141*7c478bd9Sstevel@tonic-gate 	(void) fclose(f);
142*7c478bd9Sstevel@tonic-gate #endif
143*7c478bd9Sstevel@tonic-gate 	hashinit();
144*7c478bd9Sstevel@tonic-gate 	return (1);
145*7c478bd9Sstevel@tonic-gate }
146