From a1632ff3e723a47d71ab179ac699aa6ca881695b Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sat, 22 Jun 2013 20:09:20 -0500 Subject: [PATCH] fix 32 bit offsets --- C/absmi.c | 8 ++++---- C/exo.c | 19 ++++++++++--------- C/exo_udi.c | 34 ++++++++++++++++++---------------- H/clause.h | 6 +++--- H/dlocals.h | 2 ++ H/hlocals.h | 3 ++- H/ilocals.h | 1 + H/rlocals.h | 1 + misc/LOCALS | 5 +++-- 9 files changed, 44 insertions(+), 35 deletions(-) diff --git a/C/absmi.c b/C/absmi.c index cbb277225..b9616b55a 100755 --- a/C/absmi.c +++ b/C/absmi.c @@ -968,7 +968,7 @@ Yap_absmi(int inp) CACHE_Y(YREG); { struct index_t *i = (struct index_t *)(PREG->u.lp.l); - S_YREG[-1] = (CELL)EXO_OFFSET_TO_ADDRESS(i,i->links[(CELL)(SREG-i->cls)/i->arity]); + S_YREG[-1] = (CELL)LINK_TO_ADDRESS(i,i->links[EXO_ADDRESS_TO_OFFSET(i, SREG)]); } S_YREG--; /* store arguments for procedure */ @@ -1091,10 +1091,10 @@ Yap_absmi(int inp) CACHE_Y(B); { struct index_t *it = (struct index_t *)(PREG->u.lp.l); - CELL offset = EXO_ADDRESS_TO_OFFSET(it,(CELL *)((CELL *)(B+1))[it->arity]); + BITS32 offset = ADDRESS_TO_LINK(it,(BITS32 *)((CELL *)(B+1))[it->arity]); d0 = it->links[offset]; - ((CELL *)(B+1))[it->arity] = (CELL)EXO_OFFSET_TO_ADDRESS(it, d0); - SREG = it->cls+it->arity*offset; + ((CELL *)(B+1))[it->arity] = (CELL)LINK_TO_ADDRESS(it, d0); + SREG = EXO_OFFSET_TO_ADDRESS(it, offset); } if (d0) { /* After retry, cut should be pointing at the parent diff --git a/C/exo.c b/C/exo.c index e432c52ab..989c5f5dc 100644 --- a/C/exo.c +++ b/C/exo.c @@ -243,8 +243,8 @@ MATCH(CELL *clp, CELL *kvp, UInt arity, UInt bnds[]) static void ADD_TO_TRY_CHAIN(CELL *kvp, CELL *cl, struct index_t *it) { - BITS32 old = (kvp-it->cls)/it->arity; - BITS32 new = (cl-it->cls)/it->arity; + BITS32 old = EXO_ADDRESS_TO_OFFSET(it, kvp); + BITS32 new = EXO_ADDRESS_TO_OFFSET(it, cl); BITS32 *links = it->links; BITS32 tmp = links[old]; /* points to the end of the chain */ @@ -324,7 +324,7 @@ LOOKUP(struct index_t *it, UInt arity, UInt j, UInt bnds[]) return FAILCODE; } else if (MATCH(kvp, XREGS+1, arity, bnds)) { S = kvp; - if (!it->is_key && it->links[(S-it->cls)/arity]) + if (!it->is_key && it->links[EXO_ADDRESS_TO_OFFSET(it, S)]) return it->code; else return NEXTOP(NEXTOP(it->code,lp),lp); @@ -349,10 +349,10 @@ fill_hash(UInt bmap, struct index_t *it, UInt bnds[]) } for (i=0; i < it->hsize; i++) { if (it->key[i]) { - BITS32 offset = it->key[i]/arity; + BITS32 offset = it->key[i]; BITS32 last = it->links[offset]; if (last) { - /* the chain used to point straight to the last, and the last back to the origibal first */ + /* the chain used to point straight to the last, and the last back to the original first */ it->links[offset] = it->links[last]; it->links[last] = 0; } @@ -371,7 +371,7 @@ add_index(struct index_t **ip, UInt bmap, PredEntry *ap, UInt count) size_t sz, dsz; yamop *ptr; UInt *bnds = LOCAL_ibnds; - + sz = (CELL)NEXTOP(NEXTOP((yamop*)NULL,lp),lp)+ap->ArityOfPE*(CELL)NEXTOP((yamop *)NULL,x) +(CELL)NEXTOP(NEXTOP((yamop *)NULL,p),l); if (!(i = (struct index_t *)Yap_AllocCodeSpace(sizeof(struct index_t)+sz))) { CACHE_REGS @@ -390,7 +390,7 @@ add_index(struct index_t **ip, UInt bmap, PredEntry *ap, UInt count) i->bmap = bmap; i->is_key = FALSE; i->hsize = 2*ncls; - dsz = sizeof(BITS32)*(ncls+i->hsize); + dsz = sizeof(BITS32)*(ncls+1+i->hsize); if (count) { if (!(base = (CELL *)Yap_AllocCodeSpace(dsz))) { CACHE_REGS @@ -408,6 +408,7 @@ add_index(struct index_t **ip, UInt bmap, PredEntry *ap, UInt count) i->links = (BITS32 *)base+i->hsize; i->ncollisions = i->nentries = i->ntrys = 0; i->cls = (CELL *)((ADDR)ap->cs.p_code.FirstClause+2*sizeof(struct index_t *)); + i->bcls= i->cls-i->arity; *ip = i; while (count) { if (!fill_hash(bmap, i, bnds)) { @@ -416,7 +417,7 @@ add_index(struct index_t **ip, UInt bmap, PredEntry *ap, UInt count) if (i->is_key) { sz = i->hsize*sizeof(BITS32); } else { - sz = (ncls+i->hsize)*sizeof(BITS32); + sz = (ncls+1+i->hsize)*sizeof(BITS32); } if (base != (CELL *)Yap_ReallocCodeSpace((char *)base, sz)) return FALSE; @@ -441,7 +442,7 @@ add_index(struct index_t **ip, UInt bmap, PredEntry *ap, UInt count) if (i->is_key) { sz = i->hsize*sizeof(BITS32); } else { - sz = (ncls+i->hsize)*sizeof(BITS32); + sz = (ncls+1+i->hsize)*sizeof(BITS32); } if (base != (CELL *)Yap_ReallocCodeSpace((char *)base, sz)) return FALSE; diff --git a/C/exo_udi.c b/C/exo_udi.c index 7aa59588b..544ac7bc4 100644 --- a/C/exo_udi.c +++ b/C/exo_udi.c @@ -41,16 +41,15 @@ static int compar(const void *ip0, const void *jp0) { CACHE_REGS BITS32 *ip = (BITS32 *)ip0, *jp = (BITS32 *)jp0; - BITS32 *bs = LOCAL_exo_base; - Int i = bs[LOCAL_exo_arity*(*ip)+LOCAL_exo_arg]; - Int j = bs[LOCAL_exo_arity*(*jp)+LOCAL_exo_arg]; + Term i = EXO_OFFSET_TO_ADDRESS(LOCAL_exo_it, *ip)[LOCAL_exo_arg]; + Term j = EXO_OFFSET_TO_ADDRESS(LOCAL_exo_it, *jp)[LOCAL_exo_arg]; + //fprintf(stderr, "%ld-%ld\n", IntOfTerm(i), IntOfTerm(j)); return IntOfTerm(i)-IntOfTerm(j); } static int compare(const BITS32 *ip, Int j USES_REGS) { - BITS32 *bs = LOCAL_exo_base; - Int i = bs[LOCAL_exo_arity*(*ip)+LOCAL_exo_arg]; + Term i = EXO_OFFSET_TO_ADDRESS(LOCAL_exo_it, *ip)[LOCAL_exo_arg]; //fprintf(stderr, "%ld-%ld\n", IntOfTerm(i), j); return IntOfTerm(i)-j; } @@ -61,7 +60,6 @@ compare(const BITS32 *ip, Int j USES_REGS) { { size_t sz; struct index_t *it = *ip; - UInt arity = it->arity; yamop *code; /* hard-wired implementation for the Interval case */ @@ -70,7 +68,8 @@ compare(const BITS32 *ip, Int j USES_REGS) { if (it->bmap & b[i]) return; /* no constraints, nothing to gain */ if (!IsAttVar(VarOfTerm(XREGS[i+1]))) return; - LOCAL_exo_base = it->cls; + LOCAL_exo_it = it; + LOCAL_exo_base = it->bcls; LOCAL_exo_arity = it->arity; LOCAL_exo_arg = i; if (!it->key) { @@ -99,14 +98,14 @@ compare(const BITS32 *ip, Int j USES_REGS) { for (i=0; i < it->hsize; i++) { if (it->key[i]) { BITS32 *s0 = sorted; - BITS32 offset = it->key[i]/arity, offset0 = offset; + BITS32 offset = it->key[i], offset0 = offset; *sorted++ = 0; do { *sorted++ = offset; offset = it->links[offset]; } while (offset); - // S = it->cls+it->arity*offset0; Yap_DebugPlWrite(S[1]); + // S = EXO_OFFSET_TO_ADDRESS(it, offset0); Yap_DebugPlWrite(S[0]); // fprintf(stderr, " key[i]=%d offset=%d %d\n", it->key[i], offset0, (sorted-s0)-1); if (sorted-s0 == 2) { it->links[offset0] = 0; @@ -116,6 +115,7 @@ compare(const BITS32 *ip, Int j USES_REGS) { *s0 = sorted - (s0+1); qsort(s0+1, (size_t)*s0, sizeof(BITS32), compar); it->links[offset0] = s0-sorted0; + // fprintf(stderr," %d links %d=%d \n", offset0, s0-sorted0, s0[0]); } } } @@ -156,7 +156,8 @@ Interval(struct index_t *it, Term min, Term max, Term op, BITS32 off USES_REGS) BITS32 *end; Atom at; - LOCAL_exo_base = it->cls; + LOCAL_exo_it = it; + LOCAL_exo_base = it->bcls; LOCAL_exo_arity = it->arity; LOCAL_exo_arg = it->udi_arg; if (!it->links) { @@ -169,6 +170,7 @@ Interval(struct index_t *it, Term min, Term max, Term op, BITS32 off USES_REGS) n = c[it->links[off]]; pt = c+(it->links[off]+1); end = c+(it->links[off]+n); + // fprintf(stderr," %d links %d=%d \n", off, it->links[off], n); } else { if (!IsVarTerm(min)) { Int x; @@ -257,7 +259,7 @@ Interval(struct index_t *it, Term min, Term max, Term op, BITS32 off USES_REGS) end = pt1; } if (IsVarTerm(op)) { - S = it->cls+it->arity*pt[0]; + S = EXO_OFFSET_TO_ADDRESS(it, pt[0]); if (pt < end ) { YENV[-2] = (CELL)( pt+1 ); YENV[-1] = (CELL)( end ); @@ -268,13 +270,13 @@ Interval(struct index_t *it, Term min, Term max, Term op, BITS32 off USES_REGS) } at = AtomOfTerm(op); if (at == AtomAny || at == AtomMin) { - S = it->cls+it->arity*pt[0]; + S = EXO_OFFSET_TO_ADDRESS(it, pt[0]); } else if (at == AtomMax) { - S = it->cls+it->arity*end[0]; + S = EXO_OFFSET_TO_ADDRESS(it, end[0]); } else if (at == AtomUnique) { if (end-2 > pt) return FAILCODE; - S = it->cls+it->arity*pt[0]; + S = EXO_OFFSET_TO_ADDRESS(it, pt[0]); } return NEXTOP(NEXTOP(it->code,lp),lp); } @@ -284,7 +286,7 @@ IntervalEnterUDIIndex(struct index_t *it USES_REGS) { Int i = it->udi_arg; Term t = XREGS[i+1], a1; - BITS32 off = EXO_ADDRESS_TO_OFFSET(it, S)/it->arity; + BITS32 off = EXO_ADDRESS_TO_OFFSET(it, S); // printf("off=%d it=%p %p---%p\n", off, it, it->cls, S); attvar_record *attv; @@ -315,7 +317,7 @@ IntervalRetryUDIIndex(struct index_t *it USES_REGS) *pt = (BITS32 *) w[it->arity+1]; BITS32 f = *pt; - S = it->cls+it->arity*f; + S = EXO_OFFSET_TO_ADDRESS(it, f); if (pt++ == end) return FALSE; w[it->arity+1] = (CELL)pt; return TRUE; diff --git a/H/clause.h b/H/clause.h index 2cc42b3aa..760ffa6fc 100644 --- a/H/clause.h +++ b/H/clause.h @@ -172,7 +172,7 @@ typedef struct index_t { UInt nentries; UInt hsize; BITS32 *key; - CELL *cls; + CELL *cls, *bcls; BITS32 *links; size_t size; yamop *code; @@ -186,7 +186,7 @@ INLINE_ONLY EXTERN inline BITS32 EXO_ADDRESS_TO_OFFSET(struct index_t *it, CELL INLINE_ONLY EXTERN inline BITS32 EXO_ADDRESS_TO_OFFSET(struct index_t *it, CELL* ptr) { - return 1+(ptr-it->cls); + return (ptr-it->cls)/it->arity+1; } INLINE_ONLY EXTERN inline CELL *EXO_OFFSET_TO_ADDRESS(struct index_t *it, BITS32 off); @@ -196,7 +196,7 @@ EXO_OFFSET_TO_ADDRESS(struct index_t *it, BITS32 off) { if (off == 0L) return NULL; - return (it->cls-1)+off; + return (it->cls)+(off-1)*it->arity; } INLINE_ONLY EXTERN inline BITS32 ADDRESS_TO_LINK(struct index_t *it, BITS32 *ptr); diff --git a/H/dlocals.h b/H/dlocals.h index b1bea94f5..a52e07df2 100644 --- a/H/dlocals.h +++ b/H/dlocals.h @@ -403,6 +403,8 @@ #define LOCAL_ibnds LOCAL->ibnds_ #define REMOTE_ibnds(wid) REMOTE(wid)->ibnds_ +#define LOCAL_exo_it LOCAL->exo_it_ +#define REMOTE_exo_it(wid) REMOTE(wid)->exo_it_ #define LOCAL_exo_base LOCAL->exo_base_ #define REMOTE_exo_base(wid) REMOTE(wid)->exo_base_ #define LOCAL_exo_arity LOCAL->exo_arity_ diff --git a/H/hlocals.h b/H/hlocals.h index 561bc7253..e21d94bb3 100644 --- a/H/hlocals.h +++ b/H/hlocals.h @@ -227,7 +227,8 @@ typedef struct worker_local { Functor FunctorVar_; UInt ibnds_[256]; - void* exo_base_; + struct index_t* exo_it_; + CELL* exo_base_; UInt exo_arity_; UInt exo_arg_; diff --git a/H/ilocals.h b/H/ilocals.h index 537d0464c..eb3f452c8 100644 --- a/H/ilocals.h +++ b/H/ilocals.h @@ -227,6 +227,7 @@ static void InitWorker(int wid) { REMOTE_FunctorVar(wid) = FunctorVar; + REMOTE_exo_it(wid) = NULL; REMOTE_exo_base(wid) = NULL; REMOTE_exo_arity(wid) = 0; REMOTE_exo_arg(wid) = 0; diff --git a/H/rlocals.h b/H/rlocals.h index b2ad1e01e..a4fdfc287 100644 --- a/H/rlocals.h +++ b/H/rlocals.h @@ -230,6 +230,7 @@ static void RestoreWorker(int wid USES_REGS) { + } diff --git a/misc/LOCALS b/misc/LOCALS index 7a1f259eb..c94103639 100644 --- a/misc/LOCALS +++ b/misc/LOCALS @@ -255,10 +255,11 @@ yamop *ImportFAILCODE =NULL Functor FunctorVar =FunctorVar -// exo indexingxb +// exo indexing UInt ibnds[256] void -BITS32* exo_base =NULL +struct index_t* exo_it =NULL +CELL* exo_base =NULL UInt exo_arity =0 UInt exo_arg =0