static void vec_rebuild_lookup_table(struct plot *pl) { int cnt; /* count entries */ struct dvec *d; /* dynamic vector */ NGHASHPTR lookup_p; /* lookup table for speed */ SPICE_DSTRING dbuf; /* dynamic buffer */ char *lower_name; /* lower case name */ if (pl->pl_lookup_table) { nghash_empty(pl->pl_lookup_table, NULL, NULL); } else { cnt = 0; for (d = pl->pl_dvecs; d; d = d->v_next) cnt++; pl->pl_lookup_table = nghash_init(cnt); /* allow multiple entries */ nghash_unique(pl->pl_lookup_table, FALSE); } lookup_p = pl->pl_lookup_table; spice_dstring_init(&dbuf); for (d = pl->pl_dvecs; d; d = d->v_next) { spice_dstring_reinit(&dbuf); lower_name = spice_dstring_append_lower(&dbuf, d->v_name, -1); nghash_insert(lookup_p, lower_name, d); } spice_dstring_free(&dbuf); pl->pl_lookup_valid = TRUE; }
int CKTmodCrt(CKTcircuit *ckt, int type, GENmodel **modfast, IFuid name) { GENmodel *model = CKTfndMod(ckt, name); if (model) { *modfast = model; return E_EXISTS; } model = (GENmodel *) tmalloc((size_t) *(DEVices[type]->DEVmodSize)); if (!model) return E_NOMEM; model->GENmodType = type; model->GENmodName = name; model->GENnextModel = ckt->CKThead[type]; ckt->CKThead[type] = model; nghash_insert(ckt->MODnameHash, name, model); *modfast = model; return OK; }
void nghash_resize(NGHASHPTR hashtable, int num) { NGTABLEPTR *oldtable, hptr, zapptr ; NGTABLEPTR new_hptr ; /* new hash table entry */ int i, oldsize ; oldsize = hashtable->size ; oldtable = hashtable->hash_table; if( hashtable->power_of_two ) { hashtable->size = nghash_table_size2( num - 1 ) ; } else { hashtable->size = nghash_table_size( num ) ; } hashtable->num_entries = 0 ; hashtable->thread = NULL ; hashtable->last_entry = NULL ; /* end of list */ hashtable->need_resize = hashtable->size * hashtable->max_density ; hashtable->hash_table = NGMALLOC( hashtable->size, NGTABLEPTR); for( i = 0 ; i < oldsize ; i++ ) { for( hptr = oldtable[i]; hptr; ) { zapptr = hptr ; nghash_insert( hashtable, hptr->key, hptr->data ) ; if( hashtable->searchPtr && hashtable->searchPtr == hptr ) { new_hptr = _nghash_find_item(hashtable, hptr->key, hptr->data ) ; hashtable->searchPtr = new_hptr ; } if( hashtable->enumeratePtr && hashtable->enumeratePtr == hptr ) { new_hptr = _nghash_find_item(hashtable, hptr->key, hptr->data ) ; hashtable->enumeratePtr = new_hptr ; } /* Now safe to free */ if( hashtable->hash_func == NGHASH_DEF_HASH(NGHASH_FUNC_STR) ) { NGFREE( hptr->key); } hptr = hptr->next ; NGFREE( zapptr ) ; } } NGFREE( oldtable ); } /* end nghash_resize() */
void com_diff(wordlist *wl) { double vntol, abstol, reltol, tol, cmax, cm1, cm2; struct plot *p1, *p2 = NULL; struct dvec *v1, *v2; double d1, d2; ngcomplex_t c1, c2, c3; int i, j; char *v1_name; /* cannonical v1 name */ char *v2_name; /* cannonical v2 name */ NGHASHPTR crossref_p; /* cross reference hash table */ SPICE_DSTRING ibuf; /* used to build cannonical name */ wordlist *tw; char numbuf[BSIZE_SP], numbuf2[BSIZE_SP], numbuf3[BSIZE_SP], numbuf4[BSIZE_SP]; /* For printnum */ if (!cp_getvar("diff_vntol", CP_REAL, &vntol)) vntol = 1.0e-6; if (!cp_getvar("diff_abstol", CP_REAL, &abstol)) abstol = 1.0e-12; if (!cp_getvar("diff_reltol", CP_REAL, &reltol)) reltol = 0.001; /* Let's try to be clever about defaults. This code is ugly. */ if (!wl || !wl->wl_next) { if (plot_list && plot_list->pl_next && !plot_list->pl_next->pl_next) { p1 = plot_list; p2 = plot_list->pl_next; if (wl && !eq(wl->wl_word, p1->pl_typename) && !eq(wl->wl_word, p2->pl_typename)) { fprintf(cp_err, "Error: no such plot \"%s\"\n", wl->wl_word); return; } fprintf(cp_err, "Plots are \"%s\" and \"%s\"\n", plot_list->pl_typename, plot_list->pl_next->pl_typename); if (wl) wl = NULL; } else { fprintf(cp_err, "Error: plot names not given.\n"); return; } } else { for (p1 = plot_list; p1; p1 = p1->pl_next) if (eq(wl->wl_word, p1->pl_typename)) break; if (!p1) { fprintf(cp_err, "Error: no such plot %s\n", wl->wl_word); return; } wl = wl->wl_next; } if (!p2) { for (p2 = plot_list; p2; p2 = p2->pl_next) if (eq(wl->wl_word, p2->pl_typename)) break; if (!p2) { fprintf(cp_err, "Error: no such plot %s\n", wl->wl_word); return; } wl = wl->wl_next; } /* Now do some tests to make sure these plots are really the * same type, etc. */ if (!eq(p1->pl_name, p2->pl_name)) fprintf(cp_err, "Warning: plots %s and %s seem to be of different types\n", p1->pl_typename, p2->pl_typename); if (!eq(p1->pl_title, p2->pl_title)) fprintf(cp_err, "Warning: plots %s and %s seem to be from different circuits\n", p1->pl_typename, p2->pl_typename); /* This may not be the best way to do this. It wasn't :). The original * was O(n2) - not good. Now use a hash table to reduce it to O(n). */ for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) v1->v_link2 = NULL; spice_dstring_init(&ibuf); crossref_p = nghash_init(NGHASH_MIN_SIZE); nghash_unique(crossref_p, FALSE); for (v2 = p2->pl_dvecs; v2; v2 = v2->v_next) { v2->v_link2 = NULL; v2_name = cannonical_name(v2->v_name, &ibuf); nghash_insert(crossref_p, v2_name, v2); } for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) { v1_name = cannonical_name(v1->v_name, &ibuf); for (v2 = nghash_find(crossref_p, v1_name); v2; v2 = nghash_find_again(crossref_p, v1_name)) { if (!v2->v_link2 && ((v1->v_flags & (VF_REAL | VF_COMPLEX)) == (v2->v_flags & (VF_REAL | VF_COMPLEX))) && (v1->v_type == v2->v_type)) { v1->v_link2 = v2; v2->v_link2 = v1; break; } } } spice_dstring_free(&ibuf); nghash_free(crossref_p, NULL, NULL); for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) if (!v1->v_link2) fprintf(cp_err, ">>> %s vector %s in %s not in %s, or of wrong type\n", isreal(v1) ? "real" : "complex", v1->v_name, p1->pl_typename, p2->pl_typename); for (v2 = p2->pl_dvecs; v2; v2 = v2->v_next) if (!v2->v_link2) fprintf(cp_err, ">>> %s vector %s in %s not in %s, or of wrong type\n", isreal(v2) ? "real" : "complex", v2->v_name, p2->pl_typename, p1->pl_typename); /* Throw out the ones that aren't in the arg list */ if (wl && !eq(wl->wl_word, "all")) { /* Just in case */ for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) if (v1->v_link2) { for (tw = wl; tw; tw = tw->wl_next) if (nameeq(v1->v_name, tw->wl_word)) break; if (!tw) v1->v_link2 = NULL; } for (v2 = p2->pl_dvecs; v2; v2 = v2->v_next) if (v2->v_link2) { for (tw = wl; tw; tw = tw->wl_next) if (nameeq(v2->v_name, tw->wl_word)) break; if (!tw) v2->v_link2 = NULL; } } /* Now we have all the vectors linked to their twins. Travel * down each one and print values that differ enough. */ for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) { if (!v1->v_link2) continue; v2 = v1->v_link2; if (v1->v_type == SV_VOLTAGE) tol = vntol; else tol = abstol; j = MAX(v1->v_length, v2->v_length); for (i = 0; i < j; i++) { if (v1->v_length <= i) { fprintf(cp_out, ">>> %s is %d long in %s and %d long in %s\n", v1->v_name, v1->v_length, p1->pl_typename, v2->v_length, p2->pl_typename); break; } else if (v2->v_length <= i) { fprintf(cp_out, ">>> %s is %d long in %s and %d long in %s\n", v2->v_name, v2->v_length, p2->pl_typename, v1->v_length, p1->pl_typename); break; } else { if (isreal(v1)) { d1 = v1->v_realdata[i]; d2 = v2->v_realdata[i]; if (MAX(fabs(d1), fabs(d2)) * reltol + tol < fabs(d1 - d2)) { printnum(numbuf, d1); fprintf(cp_out, "%s.%s[%d] = %-15s ", p1->pl_typename, v1->v_name, i, numbuf); printnum(numbuf, d2); fprintf(cp_out, "%s.%s[%d] = %s\n", p2->pl_typename, v2->v_name, i, numbuf); } } else { c1 = v1->v_compdata[i]; c2 = v2->v_compdata[i]; realpart(c3) = realpart(c1) - realpart(c2); imagpart(c3) = imagpart(c1) - imagpart(c2); /* Stupid evil PC compilers */ cm1 = cmag(c1); cm2 = cmag(c2); cmax = MAX(cm1, cm2); if (cmax * reltol + tol < cmag(c3)) { printnum(numbuf, realpart(c1)); printnum(numbuf2, imagpart(c1)); printnum(numbuf3, realpart(c2)); printnum(numbuf4, imagpart(c2)); fprintf(cp_out, "%s.%s[%d] = %-10s, %-10s %s.%s[%d] = %-10s, %s\n", p1->pl_typename, v1->v_name, i, numbuf, numbuf2, p2->pl_typename, v2->v_name, i, numbuf3, numbuf4); } } } } } }