aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2010-07-21 21:42:40 +0200
committerMike Pall <mike>2010-07-21 22:06:38 +0200
commitd05873ee0ae63ee47710a2c9843d032010cc296f (patch)
tree6b104afc208df5cdbf54a2865380df2bebf2615d /src
parent6667ab0f266b12909cf0eedd025525b24d987592 (diff)
downloadluajit-d05873ee0ae63ee47710a2c9843d032010cc296f.tar.gz
luajit-d05873ee0ae63ee47710a2c9843d032010cc296f.tar.bz2
luajit-d05873ee0ae63ee47710a2c9843d032010cc296f.zip
Abstract out pointer hash to hashrot(). Tune hash constants.
Diffstat (limited to 'src')
-rw-r--r--src/lj_asm.c17
-rw-r--r--src/lj_record.c11
-rw-r--r--src/lj_tab.c21
-rw-r--r--src/lj_tab.h15
4 files changed, 32 insertions, 32 deletions
diff --git a/src/lj_asm.c b/src/lj_asm.c
index 4070ccb3..56b39e32 100644
--- a/src/lj_asm.c
+++ b/src/lj_asm.c
@@ -1572,7 +1572,7 @@ static void asm_aref(ASMState *as, IRIns *ir)
1572 emit_rr(as, XO_MOV, dest, as->mrm.base); 1572 emit_rr(as, XO_MOV, dest, as->mrm.base);
1573} 1573}
1574 1574
1575/* Must match with hashkey() and hashrot() in lj_tab.c. */ 1575/* Must match with hash*() in lj_tab.c. */
1576static uint32_t ir_khash(IRIns *ir) 1576static uint32_t ir_khash(IRIns *ir)
1577{ 1577{
1578 uint32_t lo, hi; 1578 uint32_t lo, hi;
@@ -1587,12 +1587,9 @@ static uint32_t ir_khash(IRIns *ir)
1587 } else { 1587 } else {
1588 lua_assert(irt_isgcv(ir->t)); 1588 lua_assert(irt_isgcv(ir->t));
1589 lo = u32ptr(ir_kgc(ir)); 1589 lo = u32ptr(ir_kgc(ir));
1590 hi = lo - 0x04c11db7; 1590 hi = lo + HASH_BIAS;
1591 } 1591 }
1592 lo ^= hi; hi = lj_rol(hi, 14); 1592 return hashrot(lo, hi);
1593 lo -= hi; hi = lj_rol(hi, 5);
1594 hi ^= lo; hi -= lj_rol(lo, 27);
1595 return hi;
1596} 1593}
1597 1594
1598/* Merge NE(HREF, niltv) check. */ 1595/* Merge NE(HREF, niltv) check. */
@@ -1717,11 +1714,11 @@ static void asm_href(ASMState *as, IRIns *ir)
1717 } else { /* Must match with hashrot() in lj_tab.c. */ 1714 } else { /* Must match with hashrot() in lj_tab.c. */
1718 emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask)); 1715 emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask));
1719 emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp); 1716 emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp);
1720 emit_shifti(as, XOg_ROL, tmp, 27); 1717 emit_shifti(as, XOg_ROL, tmp, HASH_ROT3);
1721 emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp); 1718 emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp);
1722 emit_shifti(as, XOg_ROL, dest, 5); 1719 emit_shifti(as, XOg_ROL, dest, HASH_ROT2);
1723 emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest); 1720 emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest);
1724 emit_shifti(as, XOg_ROL, dest, 14); 1721 emit_shifti(as, XOg_ROL, dest, HASH_ROT1);
1725 emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest); 1722 emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest);
1726 if (irt_isnum(kt)) { 1723 if (irt_isnum(kt)) {
1727 emit_rr(as, XO_ARITH(XOg_ADD), dest, dest); 1724 emit_rr(as, XO_ARITH(XOg_ADD), dest, dest);
@@ -1735,7 +1732,7 @@ static void asm_href(ASMState *as, IRIns *ir)
1735#endif 1732#endif
1736 } else { 1733 } else {
1737 emit_rr(as, XO_MOV, tmp, key); 1734 emit_rr(as, XO_MOV, tmp, key);
1738 emit_rmro(as, XO_LEA, dest, key, -0x04c11db7); 1735 emit_rmro(as, XO_LEA, dest, key, HASH_BIAS);
1739 } 1736 }
1740 } 1737 }
1741 } 1738 }
diff --git a/src/lj_record.c b/src/lj_record.c
index 55daaae6..9355cb38 100644
--- a/src/lj_record.c
+++ b/src/lj_record.c
@@ -1051,15 +1051,6 @@ static TRef rec_idx(jit_State *J, RecordIndex *ix)
1051 1051
1052/* -- Upvalue access ------------------------------------------------------ */ 1052/* -- Upvalue access ------------------------------------------------------ */
1053 1053
1054/* Shrink disambiguation hash into an 8 bit value. */
1055static uint32_t shrink_dhash(uint32_t lo, uint32_t hi)
1056{
1057 lo ^= hi; hi = lj_rol(hi, 14);
1058 lo -= hi; hi = lj_rol(hi, 5);
1059 hi ^= lo; hi -= lj_rol(lo, 27);
1060 return (hi & 0xff);
1061}
1062
1063/* Record upvalue load/store. */ 1054/* Record upvalue load/store. */
1064static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val) 1055static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val)
1065{ 1056{
@@ -1068,7 +1059,7 @@ static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val)
1068 IRRef uref; 1059 IRRef uref;
1069 int needbarrier = 0; 1060 int needbarrier = 0;
1070 /* Note: this effectively limits LJ_MAX_UPVAL to 127. */ 1061 /* Note: this effectively limits LJ_MAX_UPVAL to 127. */
1071 uv = (uv << 8) | shrink_dhash(uvp->dhash, uvp->dhash-0x04c11db7); 1062 uv = (uv << 8) | (hashrot(uvp->dhash, uvp->dhash + HASH_BIAS) & 0xff);
1072 if (!uvp->closed) { 1063 if (!uvp->closed) {
1073 /* In current stack? */ 1064 /* In current stack? */
1074 if (uvval(uvp) >= J->L->stack && uvval(uvp) < J->L->maxstack) { 1065 if (uvval(uvp) >= J->L->stack && uvval(uvp) < J->L->maxstack) {
diff --git a/src/lj_tab.c b/src/lj_tab.c
index 006a87ff..d6175bbd 100644
--- a/src/lj_tab.c
+++ b/src/lj_tab.c
@@ -17,22 +17,19 @@
17/* -- Object hashing ------------------------------------------------------ */ 17/* -- Object hashing ------------------------------------------------------ */
18 18
19/* Hash values are masked with the table hash mask and used as an index. */ 19/* Hash values are masked with the table hash mask and used as an index. */
20#define hashmask(t, x) (&noderef(t->node)[(x) & t->hmask]) 20static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash)
21{
22 Node *n = noderef(t->node);
23 return &n[hash & t->hmask];
24}
21 25
22/* String hashes are precomputed when they are interned. */ 26/* String hashes are precomputed when they are interned. */
23#define hashstr(t, s) hashmask(t, (s)->hash) 27#define hashstr(t, s) hashmask(t, (s)->hash)
24 28
25#define hashnum(t, o) hashrot(t, (o)->u32.lo, ((o)->u32.hi << 1)) 29#define hashlohi(t, lo, hi) hashmask((t), hashrot((lo), (hi)))
26#define hashgcref(t, r) hashrot(t, gcrefu(r), gcrefu(r)-0x04c11db7) 30#define hashnum(t, o) hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1))
27 31#define hashptr(t, p) hashlohi((t), u32ptr(p), u32ptr(p) + HASH_BIAS)
28/* Scramble the bits of numbers and pointers. */ 32#define hashgcref(t, r) hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS)
29static LJ_AINLINE Node *hashrot(const GCtab *t, uint32_t lo, uint32_t hi)
30{
31 lo ^= hi; hi = lj_rol(hi, 14);
32 lo -= hi; hi = lj_rol(hi, 5);
33 hi ^= lo; hi -= lj_rol(lo, 27);
34 return hashmask(t, hi);
35}
36 33
37/* Hash an arbitrary key and return its anchor position in the hash table. */ 34/* Hash an arbitrary key and return its anchor position in the hash table. */
38static Node *hashkey(const GCtab *t, cTValue *key) 35static Node *hashkey(const GCtab *t, cTValue *key)
diff --git a/src/lj_tab.h b/src/lj_tab.h
index f8ace6c1..d18c604e 100644
--- a/src/lj_tab.h
+++ b/src/lj_tab.h
@@ -8,6 +8,21 @@
8 8
9#include "lj_obj.h" 9#include "lj_obj.h"
10 10
11/* Hash constants. Tuned using a brute force search. */
12#define HASH_BIAS (-0x04c11db7)
13#define HASH_ROT1 14
14#define HASH_ROT2 5
15#define HASH_ROT3 13
16
17/* Scramble the bits of numbers and pointers. */
18static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi)
19{
20 lo ^= hi; hi = lj_rol(hi, HASH_ROT1);
21 lo -= hi; hi = lj_rol(hi, HASH_ROT2);
22 hi ^= lo; hi -= lj_rol(lo, HASH_ROT3);
23 return hi;
24}
25
11#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0) 26#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0)
12 27
13LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits); 28LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits);