88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/access/hash/hashfunc.c,v 1.32 2002/03/06 20:49:38 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/access/hash/hashfunc.c,v 1.33 2002/03/09 17:35:35 tgl Exp $
1212 *
1313 * NOTES
1414 * These functions are stored in pg_amproc.For each operator class
@@ -58,23 +58,23 @@ hashfloat4(PG_FUNCTION_ARGS)
5858{
5959float4 key = PG_GETARG_FLOAT4 (0 );
6060
61- return hash_any ((char * )& key ,sizeof (key ));
61+ return hash_any ((unsigned char * )& key ,sizeof (key ));
6262}
6363
6464Datum
6565hashfloat8 (PG_FUNCTION_ARGS )
6666{
6767float8 key = PG_GETARG_FLOAT8 (0 );
6868
69- return hash_any ((char * )& key ,sizeof (key ));
69+ return hash_any ((unsigned char * )& key ,sizeof (key ));
7070}
7171
7272Datum
7373hashoidvector (PG_FUNCTION_ARGS )
7474{
7575Oid * key = (Oid * )PG_GETARG_POINTER (0 );
7676
77- return hash_any ((char * )key ,INDEX_MAX_KEYS * sizeof (Oid ));
77+ return hash_any ((unsigned char * )key ,INDEX_MAX_KEYS * sizeof (Oid ));
7878}
7979
8080/*
@@ -87,17 +87,18 @@ hashint2vector(PG_FUNCTION_ARGS)
8787{
8888int16 * key = (int16 * )PG_GETARG_POINTER (0 );
8989
90- return hash_any ((char * )key ,INDEX_MAX_KEYS * sizeof (int16 ));
90+ return hash_any ((unsigned char * )key ,INDEX_MAX_KEYS * sizeof (int16 ));
9191}
9292
9393Datum
9494hashname (PG_FUNCTION_ARGS )
9595{
9696char * key = NameStr (* PG_GETARG_NAME (0 ));
97+ int keylen = strlen (key );
9798
98- Assert (strlen ( key ) <= NAMEDATALEN );
99+ Assert (keylen < NAMEDATALEN );/* else it's not truncated correctly */
99100
100- return hash_any (key ,strlen ( key ) );
101+ return hash_any (( unsigned char * ) key ,keylen );
101102}
102103
103104/*
@@ -110,21 +111,24 @@ hashvarlena(PG_FUNCTION_ARGS)
110111struct varlena * key = PG_GETARG_VARLENA_P (0 );
111112Datum result ;
112113
113- result = hash_any (VARDATA (key ),VARSIZE (key )- VARHDRSZ );
114+ result = hash_any ((unsignedchar * )VARDATA (key ),
115+ VARSIZE (key )- VARHDRSZ );
114116
115117/* Avoid leaking memory for toasted inputs */
116118PG_FREE_IF_COPY (key ,0 );
117119
118120return result ;
119121}
120122
121- /* This hash function was written by Bob Jenkins
123+ /*
124+ * This hash function was written by Bob Jenkins
122125 * (bob_jenkins@burtleburtle.net), and superficially adapted
123126 * for PostgreSQL by Neil Conway. For more information on this
124- * hash function, see http://burtleburtle.net/bob/hash/doobs.html
127+ * hash function, see http://burtleburtle.net/bob/hash/doobs.html,
128+ * or Bob's article in Dr. Dobb's Journal, Sept. 1997.
125129 */
126130
127- /*
131+ /*----------
128132 * mix -- mix 3 32-bit values reversibly.
129133 * For every delta with one or two bits set, and the deltas of all three
130134 * high bits or all three low bits, whether the original value of a,b,c
@@ -133,6 +137,7 @@ hashvarlena(PG_FUNCTION_ARGS)
133137 * have at least 1/4 probability of changing.
134138 * - If mix() is run forward, every bit of c will change between 1/3 and
135139 * 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
140+ *----------
136141 */
137142#define mix (a ,b ,c ) \
138143{ \
@@ -151,56 +156,52 @@ hashvarlena(PG_FUNCTION_ARGS)
151156 * hash_any() -- hash a variable-length key into a 32-bit value
152157 * k : the key (the unaligned variable-length array of bytes)
153158 * len : the length of the key, counting by bytes
154- * Returns a 32-bit value. Every bit of the key affects every bit of
159+ *
160+ * Returns a uint32 value. Every bit of the key affects every bit of
155161 * the return value. Every 1-bit and 2-bit delta achieves avalanche.
156162 * About 6*len+35 instructions. The best hash table sizes are powers
157163 * of 2. There is no need to do mod a prime (mod is sooo slow!).
158164 * If you need less than 32 bits, use a bitmask.
159165 */
160166Datum
161- hash_any (registerconst char * k , registerint keylen )
167+ hash_any (registerconst unsigned char * k , registerint keylen )
162168{
163- registerDatum a ,b ,c ,len ;
164-
165- /* Set up the internal state */
166- len = keylen ;
167- a = b = 0x9e3779b9 ;/* the golden ratio; an arbitrary value */
168- /* Another arbitrary value. If the hash function is called
169- * multiple times, this could be the previously generated
170- * hash value; however, the interface currently doesn't allow
171- * this. AFAIK this isn't a big deal.
172- */
173- c = 3923095 ;
174-
175- /* handle most of the key */
176- while (len >=12 )
177- {
178- a += (k [0 ]+ ((Datum )k [1 ]<<8 )+ ((Datum )k [2 ]<<16 )+ ((Datum )k [3 ]<<24 ));
179- b += (k [4 ]+ ((Datum )k [5 ]<<8 )+ ((Datum )k [6 ]<<16 )+ ((Datum )k [7 ]<<24 ));
180- c += (k [8 ]+ ((Datum )k [9 ]<<8 )+ ((Datum )k [10 ]<<16 )+ ((Datum )k [11 ]<<24 ));
181- mix (a ,b ,c );
182- k += 12 ;len -= 12 ;
183- }
184-
185- /* handle the last 11 bytes */
186- c += keylen ;
187- switch (len )/* all the case statements fall through */
188- {
189- case 11 :c += ((Datum )k [10 ]<<24 );
190- case 10 :c += ((Datum )k [9 ]<<16 );
191- case 9 :c += ((Datum )k [8 ]<<8 );
192- /* the first byte of c is reserved for the length */
193- case 8 :b += ((Datum )k [7 ]<<24 );
194- case 7 :b += ((Datum )k [6 ]<<16 );
195- case 6 :b += ((Datum )k [5 ]<<8 );
196- case 5 :b += k [4 ];
197- case 4 :a += ((Datum )k [3 ]<<24 );
198- case 3 :a += ((Datum )k [2 ]<<16 );
199- case 2 :a += ((Datum )k [1 ]<<8 );
200- case 1 :a += k [0 ];
201- /* case 0: nothing left to add */
202- }
203- mix (a ,b ,c );
204- /* report the result */
205- return c ;
169+ registeruint32 a ,b ,c ,len ;
170+
171+ /* Set up the internal state */
172+ len = keylen ;
173+ a = b = 0x9e3779b9 ;/* the golden ratio; an arbitrary value */
174+ c = 3923095 ;/* initialize with an arbitrary value */
175+
176+ /* handle most of the key */
177+ while (len >=12 )
178+ {
179+ a += (k [0 ]+ ((uint32 )k [1 ]<<8 )+ ((uint32 )k [2 ]<<16 )+ ((uint32 )k [3 ]<<24 ));
180+ b += (k [4 ]+ ((uint32 )k [5 ]<<8 )+ ((uint32 )k [6 ]<<16 )+ ((uint32 )k [7 ]<<24 ));
181+ c += (k [8 ]+ ((uint32 )k [9 ]<<8 )+ ((uint32 )k [10 ]<<16 )+ ((uint32 )k [11 ]<<24 ));
182+ mix (a ,b ,c );
183+ k += 12 ;len -= 12 ;
184+ }
185+
186+ /* handle the last 11 bytes */
187+ c += keylen ;
188+ switch (len )/* all the case statements fall through */
189+ {
190+ case 11 :c += ((uint32 )k [10 ]<<24 );
191+ case 10 :c += ((uint32 )k [9 ]<<16 );
192+ case 9 :c += ((uint32 )k [8 ]<<8 );
193+ /* the first byte of c is reserved for the length */
194+ case 8 :b += ((uint32 )k [7 ]<<24 );
195+ case 7 :b += ((uint32 )k [6 ]<<16 );
196+ case 6 :b += ((uint32 )k [5 ]<<8 );
197+ case 5 :b += k [4 ];
198+ case 4 :a += ((uint32 )k [3 ]<<24 );
199+ case 3 :a += ((uint32 )k [2 ]<<16 );
200+ case 2 :a += ((uint32 )k [1 ]<<8 );
201+ case 1 :a += k [0 ];
202+ /* case 0: nothing left to add */
203+ }
204+ mix (a ,b ,c );
205+ /* report the result */
206+ return UInt32GetDatum (c );
206207}