From ede1079e13ac0cbdcc63738f8a3414fefaa0c48d Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 12 Oct 2019 00:53:30 -0700 Subject: hash: use ucnum for hash values everywhere. One consequence of this is that we no longer pass negative values to vecref; that functon was protecting us from the consequences of this signed/unsigned mixup, at the cost of cyles. * hash.c (struct hash_ops): hash parameter in assoc_fun and acons_new_c_fun pointers changes from cnum to ucnum. (hash_assoc, hash_assql, hash_assq): hash parameter changes to ucnum. (hash_acons_new_c, hash_aconsql_new_c, hash_aconsq_new_c): Likewise. (gethash_c, gethash_e, remhash): Variable which captures the output of the hashing function is now ucnum instead of cnum. * lib.h (struct cons_hash_entry): Member hash changes from cnum to ucnum. --- hash.c | 22 +++++++++++----------- lib.h | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/hash.c b/hash.c index 83e73e35..57b029de 100644 --- a/hash.c +++ b/hash.c @@ -64,8 +64,8 @@ typedef enum hash_type { struct hash_ops { ucnum (*hash_fun)(val, int *, ucnum); val (*equal_fun)(val, val); - val (*assoc_fun)(val key, cnum hash, val list); - val (*acons_new_c_fun)(val key, cnum hash, loc new_p, loc list); + val (*assoc_fun)(val key, ucnum hash, val list); + val (*acons_new_c_fun)(val key, ucnum hash, loc new_p, loc list); }; #define hash_ops_init(hash, equal, assoc, acons) \ @@ -665,7 +665,7 @@ static void hash_grow(struct hash *h, val hash) setcheck(hash, new_table); } -static val hash_assoc(val key, cnum hash, val list) +static val hash_assoc(val key, ucnum hash, val list) { while (list) { val elem = us_car(list); @@ -677,7 +677,7 @@ static val hash_assoc(val key, cnum hash, val list) return nil; } -static val hash_assql(val key, cnum hash, val list) +static val hash_assql(val key, ucnum hash, val list) { while (list) { val elem = us_car(list); @@ -689,7 +689,7 @@ static val hash_assql(val key, cnum hash, val list) return nil; } -static val hash_assq(val key, cnum hash, val list) +static val hash_assq(val key, ucnum hash, val list) { while (list) { val elem = us_car(list); @@ -702,7 +702,7 @@ static val hash_assq(val key, cnum hash, val list) } -static val hash_acons_new_c(val key, cnum hash, loc new_p, loc list) +static val hash_acons_new_c(val key, ucnum hash, loc new_p, loc list) { val existing = hash_assoc(key, hash, deref(list)); @@ -720,7 +720,7 @@ static val hash_acons_new_c(val key, cnum hash, loc new_p, loc list) } } -static val hash_aconsql_new_c(val key, cnum hash, loc new_p, loc list) +static val hash_aconsql_new_c(val key, ucnum hash, loc new_p, loc list) { val existing = hash_assql(key, hash, deref(list)); @@ -738,7 +738,7 @@ static val hash_aconsql_new_c(val key, cnum hash, loc new_p, loc list) } } -static val hash_aconsq_new_c(val key, cnum hash, loc new_p, loc list) +static val hash_aconsq_new_c(val key, ucnum hash, loc new_p, loc list) { val existing = hash_assq(key, hash, deref(list)); @@ -893,7 +893,7 @@ val gethash_c(val self, val hash, val key, loc new_p) { struct hash *h = coerce(struct hash *, cobj_handle(self, hash, hash_s)); int lim = hash_rec_limit; - cnum hv = h->hops->hash_fun(key, &lim, h->seed); + ucnum hv = h->hops->hash_fun(key, &lim, h->seed); loc pchain = vecref_l(h->table, num_fast(hv % h->modulus)); val old = deref(pchain); val cell = h->hops->acons_new_c_fun(key, hv, new_p, pchain); @@ -906,7 +906,7 @@ val gethash_e(val self, val hash, val key) { struct hash *h = coerce(struct hash *, cobj_handle(self, hash, hash_s)); int lim = hash_rec_limit; - cnum hv = h->hops->hash_fun(key, &lim, h->seed); + ucnum hv = h->hops->hash_fun(key, &lim, h->seed); val chain = vecref(h->table, num_fast(hv % h->modulus)); return h->hops->assoc_fun(key, hv, chain); } @@ -960,7 +960,7 @@ val remhash(val hash, val key) val self = lit("remhash"); struct hash *h = coerce(struct hash *, cobj_handle(self, hash, hash_s)); int lim = hash_rec_limit; - cnum hv = h->hops->hash_fun(key, &lim, h->seed); + ucnum hv = h->hops->hash_fun(key, &lim, h->seed); val *pchain = valptr(vecref_l(h->table, num_fast(hv % h->modulus))); val existing = h->hops->assoc_fun(key, hv, *pchain); diff --git a/lib.h b/lib.h index 7c8a582b..a57442a4 100644 --- a/lib.h +++ b/lib.h @@ -114,7 +114,7 @@ struct cons { struct cons_hash_entry { obj_common; val car, cdr; - cnum hash; + ucnum hash; }; struct string { -- cgit v1.2.3