JNIEXPORT jint JNICALL Java_edu_berkeley_bid_CUMAT_floatToLong (JNIEnv *env, jobject obj, jobject jA, jobject jB, jint N) { float *A = (float*)getPointer(env, jA); long long *B = (long long*)getPointer(env, jB); return floatToLong(A, B, N); }
thStatus thGethitPreamble(char *line, thGethitOpaque *opqptr,int *icoord){ char *equal; char *arg; /* printf("Processing preamble line %x %s\n",line,line);*/ if(!(equal=strchr(line,'='))) return(S_SUCCESS); arg = equal + 1; *equal = 0; arg = thSpaceStrip(arg); /* printf("line=%s,arg=%s\n",line,arg); */ if(strcasestr(line,"missing_value")) { opqptr->default_real = atof(arg); opqptr->default_int = floatToLong(opqptr->default_real); } else { daVarStruct *varp; /* printf("line=%s,arg=%x %s\n",line,arg,arg);*/ if(daVarLookupPWithClass(arg,hitsourceclasslist,&varp)!=S_SUCCESS){ fprintf(STDERR,"(thGethitPreamble )Variable %s not found\n",arg); return(S_FAILURE); /* Variable not found */ } /* printf("line=%s,arg=%x %s\n",line,arg,arg);*/ /* printf("Searching for keyword in %s\n",line);*/ if(strcasestr(line,"valuelist")) { opqptr->srcdefault = varp; } else { if(varp->type != DAVARINT) { return(S_FAILURE); /* Not an integer array */ } if(strcasestr(line,"hitcount")) { opqptr->vnhits = (DAINT *) varp->varptr; /* printf("Setting vnhits to %s\n",varp->name);*/ } else if(strcasestr(line,"matchlist")) { /* printf("Coordinate %d=%s\n",*icoord,varp->name);*/ opqptr->coord_array[(*icoord)++] = (DAINT *) varp->varptr; } else { return(S_FAILURE); } } } return(S_SUCCESS); }
daVarStatus daVarRegWatr(daVarStruct *varp, char *attribute , int index, any *setval) /* Regular Write Attribute routine. Returns failure if attribute is not of the standard set. */ { int i; if(*attribute == '\0' || strcasecmp(attribute,DAVAR_VALUE) == 0){ if(index == DAVAR_NOINDEX) index = 0; if(setval->valtype == DAVARINT_RPC) { if(varp->size >= setval->any_u.i.i_len+index){ if(varp->type == DAVARINT){ for(i=0;i<setval->any_u.i.i_len;i++){ ((DAINT *)varp->varptr)[i+index] = setval->any_u.i.i_val[i]; } } else if(varp->type == DAVARFLOAT){ for(i=0;i<setval->any_u.i.i_len;i++){ ((DAFLOAT *)varp->varptr)[i+index] = setval->any_u.i.i_val[i]; } } else if(varp->type == DAVARDOUBLE){ for(i=0;i<setval->any_u.i.i_len;i++){ ((DADOUBLE *)varp->varptr)[i+index] = setval->any_u.i.i_val[i]; } } else return(S_DAVAR_ILLCONV); } else return(S_DAVAR_TOOMANY); } else if(setval->valtype == DAVARFLOAT_RPC) { if(varp->size >= setval->any_u.r.r_len+index){ if(varp->type == DAVARINT){ for(i=0;i<setval->any_u.r.r_len;i++){ ((DAINT *)varp->varptr)[i+index] = floatToLong(setval->any_u.r.r_val[i]); } } else if(varp->type == DAVARFLOAT){ for(i=0;i<setval->any_u.r.r_len;i++){ ((DAFLOAT *)varp->varptr)[i+index] = setval->any_u.r.r_val[i]; } } else if(varp->type == DAVARDOUBLE){ for(i=0;i<setval->any_u.r.r_len;i++){ ((DADOUBLE *)varp->varptr)[i+index] = setval->any_u.r.r_val[i]; } } else return(S_DAVAR_TOOMANY); } else return(S_DAVAR_ILLCONV); } #ifdef DOUBLERPC else if(setval->valtype == DAVARDOUBLE_RPC) { if(varp->size >= setval->any_u.d.d_len+index){ if(varp->type == DAVARINT){ for(i=0;i<setval->any_u.d.d_len;i++){ ((DAINT *)varp->varptr)[i+index] = floatToLong(setval->any_u.d.d_val[i]); } } else if(varp->type == DAVARFLOAT){ for(i=0;i<setval->any_u.d.d_len;i++){ ((DAFLOAT *)varp->varptr)[i+index] = setval->any_u.d.d_val[i]; } } else if(varp->type == DAVARDOUBLE){ for(i=0;i<setval->any_u.d.d_len;i++){ ((DADOUBLE *)varp->varptr)[i+index] = setval->any_u.d.d_val[i]; } } else return(S_DAVAR_TOOMANY); } else return(S_DAVAR_ILLCONV); } #endif else if(setval->valtype == DAVARSTRING_RPC) { if(varp->type == DAVARSTRING) { if(varp->size < strlen(setval->any_u.s)+index) { return(S_DAVAR_TOOMANY); /* Maybe we should truncate strings??? */ } strcpy(((char *) varp->varptr)+index, setval->any_u.s); } else if(varp->type == DAVARFSTRING) { int in_len; int blanks; char *p; in_len = strlen(setval->any_u.s); /* printf("|%s|\n",setval->any_u.s);*/ if(varp->size < in_len+index) { return(S_DAVAR_TOOMANY); } strncpy(((char *) varp->varptr)+index, setval->any_u.s,in_len); blanks = varp->size - index - in_len; p = ((char *) varp->varptr) + index + in_len; while(blanks-- > 0) *p++ = ' '; /* Blank pad */ } else return(S_DAVAR_ILLCONV); } else return(S_FAILURE); } else if(strcasecmp(attribute,DAVAR_TITLE) == 0){ /* Index ignored for title attribute */ /* printf("setval->valtype = %d\n",setval->valtype);*/ if(setval->valtype == DAVARSTRING_RPC) { varp->title = (char *) realloc(varp->title,strlen(setval->any_u.s) + 1); /* printf("Writing string %s\n",setval->any_u.s);*/ strcpy(varp->title,setval->any_u.s); } else return(S_DAVAR_ILLCONV); } else { return(S_DAVAR_UNKATTR); } return(S_SUCCESS); }
thStatus thExecuteCode(char *blockname,CODEPTR code, CODEPTR codelimit) { #ifdef PHILDEBUG #ifdef NOTPOSIX #warning Phil says NOTPOSIX! #else #warning Phil says not NOTPOSIX! i.e. POSIX!! #endif #ifdef POINTER64 #warning Phil says POINTER64! #else #warning Phil says not POINTER64! #endif #ifdef USEMEMCPY #warning Phil says USEMEMCPY! #else #warning Phil says not USEMEMCPY! #endif #endif register CODEPTR pc; CODE rawopcode,opcode,ltype,rtype,lrtypes; DAINT nargs,result; register DAINT *sp; DAINT i,il,ir,*pi; DAFLOAT f,fl,fr,*pf; DADOUBLE d,dl,dr,*pd; DAINT index; sp = stack; pc = code; while(pc < codelimit){ /* printf("PC=%x, Op code %x, Stack=%x, SP=%x\n",pc,*pc,stack,sp);*/ rawopcode = *pc++; if(rawopcode >= OPLP){ /* New style */ ltype = (rawopcode & OPLEFTTYPEMASK) >> 8; rtype = (rawopcode & OPRIGHTTYPEMASK) >> 4; /* lrtypes = opcode & OPLRTYPEMASK;*/ opcode = rawopcode & OPCODEMASK; switch(opcode & OPGROUPMASK) { case OPPUSHGROUP: /* Pushes */ switch(opcode) { #ifdef USEMEMCPY void *tmpptr; #endif case OPPUSHINT: /* Float included in pushes */ if((rawopcode & OPRESTYPEMASK) == OPRDOUBLE){ /* printf("sp=%x, pc=%x\n",sp,pc);*/ #ifdef USEMEMCPY memcpy((void *)&d,((DADOUBLE *)pc)++,sizeof(DADOUBLE)); PUSHDOUBLE(d); #else #ifdef __sgi PUSHDOUBLE(*((DADOUBLE *)pc)); pc++; pc++; #else PUSHDOUBLE(*(DADOUBLE *)pc);/*phil*/ pc = (CODEPTR) (DADOUBLE *) ((DADOUBLE *)pc + 1); #endif #endif /* printf("sp=%x, pc=%x\n",sp,pc);*/ } else { PUSHINT(*pc++); } break; case OPPUSHPINT: /*Push a pointer*/ #ifdef USEMEMCPY PUSHPOINTER((memcpy(&tmpptr,(((DAINT **)pc)++),sizeof(void *)) ,tmpptr)); #else PUSHPOINTER(*(DAINT **)pc); /*phil*/ pc = (CODEPTR)(DAINT **) ((DAINT **)pc + 1); #endif break; case OPPUSHINTP: /*Push what a pointer points to */ if((rawopcode & OPRESTYPEMASK) == OPRDOUBLE){ #ifdef USEMEMCPY memcpy(&tmpptr,(((DAINT **)pc)++),sizeof(void *)); d = *(DADOUBLE *) tmpptr; #else d = **(DADOUBLE **)pc;/*phil*/ pc = (CODEPTR) (DADOUBLE **) ((DADOUBLE **)pc + 1); #endif PUSHDOUBLE(d);/*phil*/ } else { #ifdef USEMEMCPY memcpy(&tmpptr,(((DAINT **)pc)++),sizeof(void *)); PUSHINT(*(DAINT *) tmpptr); #else PUSHINT(**(DAINT **)pc);/*phil*/ pc = (CODEPTR) (DAINT **) ((DAINT **)pc + 1); #endif } break; case OPPUSHFUNCTION: /*Push a intrinsic function code */ PUSHINT(*pc++); break; } break; case OPEOLGROUP: sp--; /* Should empty the stack */ if(rtype == OPRDOUBLE) sp--; /* Double is two entries on stack */ break; case OPLINDEXGROUP: if(opcode==OPLFARG) { if(rtype==OPRINT) {POPINT(i);} else if(rtype==OPRFLOAT) {POPFLOAT(f);}/*phil*/ else {POPDOUBLE(d);}/*phil*/ POPINT(index); /* Pop the function code */ switch(index) { case 0: /* abs */ if(rtype==OPRINT) { if(i<0) i = -i; PUSHINT(i); } else if(rtype==OPRFLOAT) { if(f<0.0) f = -f; PUSHFLOAT(f);/*phil*/ } else { if(d<0.0) d = -d; PUSHDOUBLE(d);/*phil*/ } break; case 1: /* sqrt */ if(rtype==OPRINT) d = i; else if(rtype==OPRFLOAT) d = f; if(d>=0) d = sqrt(d); else { fprintf(STDERR,"Test block %s: sqrt(%f)\n",blockname,d); d = 0; } PUSHDOUBLE(d);/*phil*/ break; case 2: /* exp */ if(rtype==OPRINT) d = i; else if(rtype==OPRFLOAT) d = f; d = exp(d); PUSHDOUBLE(d);/*phil*/ break; case 3: /* sin */ if(rtype==OPRINT) d = i; else if(rtype==OPRFLOAT) d = f; d = sin(d); PUSHDOUBLE(d);/*phil*/ break; case 4: /* cos */ if(rtype==OPRINT) d = i; else if(rtype==OPRFLOAT) d = f; d = cos(d); PUSHDOUBLE(d);/*phil*/ break; case 5: /* tan */ if(rtype==OPRINT) d = i; else if(rtype==OPRFLOAT) d = f; d = tan(d); PUSHDOUBLE(d);/*phil*/ break; } break; } if(rtype==OPRFLOAT) { /* Floating point index */ POPFLOAT(f);/*phil*/ index = floatToLong(f); } else if(rtype==OPRDOUBLE) { /* Double */ POPDOUBLE(d);/*phil*/ index = floatToLong(d); } else { POPINT(index); } index -= ((opcode & 0xF000) == 0x1000 ? 0 : 1); /* ltype should always be == restype */ if(opcode == OPLINDEX || opcode == OPLINDEXB){ if(ltype == OPRDOUBLE) { FETCHDARRAY(d);/*phil*/ PUSHDOUBLE(d);/*phil*/ } else if (ltype == OPRFLOAT) { FETCHFARRAY(f);/*phil*/ PUSHFLOAT(f);/*phil*/ } else { FETCHIARRAY(i);/*phil*/ PUSHINT(i); } } else { /*pointer on stack*/ sp--; #ifdef POINTER64 sp--; #endif if(ltype == OPRDOUBLE) { /* *((DADOUBLE **)sp)++ = (*((DADOUBLE **)sp)+index);*/ /* The following works better on the alpha */ pd = *((DADOUBLE **)sp); pd += index; PUSHPOINTER(pd);/*phil*/ } else { /* Assume INT and FLOAT the same size */ /**((DAINT **)sp)++ = (*((DAINT **)sp)+index);*/ /* The following works better on the alpha */ pi = *((DAINT **)sp); pi += index; PUSHPOINTER(pi);/*phil*/ } } break; case OPEQUAL: /* Big ugly matrix of type conversions */ if(rtype==OPRINT) { POPINT(i); if(ltype==OPRINT) { SAVEINT(i); /* Save result in result variable *//*phil*/ PUSHINT(i); /* Put result back on stack */ } else if(ltype==OPRFLOAT) { f = i; /* Convert to floating */ SAVEFLOAT(f); /* Save variable *//*phil*/ PUSHFLOAT(f); /* Put back on stack *//*phil*/ } else { /* if(ltype==OPRDOUBLE) */ d = i; SAVEDOUBLE(d);/*phil*/ PUSHDOUBLE(d);/*phil*/ } } else if(rtype==OPRFLOAT) { POPFLOAT(f);/*phil*/ if(ltype==OPRINT) { i = floatToLong(f); SAVEINT(i); /* Save result in result variable *//*phil*/ *sp++ = i; } else if(ltype==OPRFLOAT) { SAVEFLOAT(f); /* Save variable *//*phil*/ *sp++ = *(DAINT *)&f; } else { /* if(ltype==OPRDOUBLE) */ d = f; SAVEDOUBLE(d);/*phil*/ PUSHDOUBLE(d);/*phil*/ } } else { /* if(rtype==OPRDOUBLE) */ POPDOUBLE(d);/*phil*/ if(ltype==OPRINT) { i = floatToLong(d); SAVEINT(i); /* Save result in result variable *//*phil*/ *sp++ = i; } else if(ltype==OPRFLOAT) { f = d; SAVEFLOAT(f); /* Save variable *//*phil*/ *sp++ = *(DAINT *)&f; } else { /* if(ltype==OPRDOUBLE) */ SAVEDOUBLE(d);/*phil*/ PUSHDOUBLE(d);/*phil*/ } } break; case OPLOGGROUP: /* Logic and Bit operations */ case OPSHIFTGROUP: /* Logic and Bit operations */ if(rtype==OPRINT) { POPINT(ir); } else if(rtype==OPRFLOAT) { POPFLOAT(f);/*phil*/ ir = floatToLong(f); } else { POPDOUBLE(d);/*phil*/ ir = floatToLong(d); } if(ltype==OPRINT) { POPINT(il); } else if(ltype==OPRFLOAT) { POPFLOAT(f);/*phil*/ il = floatToLong(f); } else { POPDOUBLE(d);/*phil*/ il = floatToLong(d); } switch(opcode) { case OPLOGOR: *sp++ = il || ir; break; case OPLOGXOR: *sp++ = (il != 0) ^ (ir != 0); break; case OPLOGAND: *sp++ = il && ir; break; case OPBITOR: *sp++ = il | ir; break; case OPBITXOR: *sp++ = il ^ ir; break; case OPBITAND: *sp++ = il & ir; break; case OPSHL: *sp++ = il << ir; break; case OPSHR: *sp++ = il >> ir; break; } break; case OPCOMPGROUP: /* Logic comparisons */ /* Result of Add amd MUL groups should now always be double */ case OPADDGROUP: /* Add and Subtract */ case OPMULGROUP: /* * / and % */ if(rtype==OPRINT) { POPINT(ir); dr = ir; } else if (rtype==OPRFLOAT) { POPFLOAT(fr);/*phil*/ dr = fr; } else { POPDOUBLE(dr);/*phil*/ } if(ltype==OPRINT) { POPINT(il); dl = il; } else if (ltype==OPRFLOAT) { POPFLOAT(fl);/*phil*/ dl = fl; } else { POPDOUBLE(dl);/*phil*/ } if(rtype!=OPRINT || ltype!=OPRINT){ switch(opcode) { case OPISEQUAL: *sp++ = dl == dr; break; case OPISNOTEQUAL: *sp++ = dl != dr; break; case OPISLT: *sp++ = dl < dr; break; case OPISGT: *sp++ = dl > dr; break; case OPISLE: *sp++ = dl <= dr; break; case OPISGE: *sp++ = dl >= dr; break; case OPADD: d = dl + dr; PUSHDOUBLE(d);/*phil*/ break; case OPSUB: d = dl - dr; PUSHDOUBLE(d);/*phil*/ break; case OPTIMES: d = dl * dr; /* Need to deal with overflow */ PUSHDOUBLE(d);/*phil*/ break; case OPIDIV: /* printf("OP=%x\n",rawopcode);*/ if(dr == 0.0) { fprintf(STDERR,"Test block %s: %f/0.0\n",blockname,dl); d = 0.0; } else { d = dl / dr; /* Need to deal with overflow and div 0 */ } *sp++ = floatToLong(d); break; case OPDIV: if(dr == 0.0) { fprintf(STDERR,"Test block %s: %f/0.0\n",blockname,dl); d = 0.0; } else { d = dl / dr; /* Need to deal with overflow and div 0 */ } PUSHDOUBLE(d);/*phil*/ break; case OPMOD: d = fmod(dl,dr); PUSHDOUBLE(d);/*phil*/ break; } } else { /* Both left and right are int */ switch(opcode) { case OPISEQUAL: *sp++ = il == ir; break; case OPISNOTEQUAL: *sp++ = il != ir; break; case OPISLT: *sp++ = il < ir; break; case OPISGT: *sp++ = il > ir; break; case OPISLE: *sp++ = il <= ir; break; case OPISGE: *sp++ = il >= ir; break; case OPADD: *sp++ = il + ir; break; case OPSUB: *sp++ = il - ir; break; case OPTIMES: *sp++ = il * ir; /* Need to deal with overflow */ break; case OPIDIV: /* printf("At OPIDIV all int branch\n");*/ if(ir == 0) { fprintf(STDERR,"Test block %s: %d/0.0\n",blockname,il); *sp++ = 0; } else { *sp++ = il / ir; } break; case OPDIV: if(ir == 0) { fprintf(STDERR,"Test block %s: %d/0.0\n",blockname,il); d = 0.0; } else d = dl / dr; /* Need to deal with overflow and div 0 */ PUSHDOUBLE(d);/*phil*/ break; case OPMOD: *sp++ = il % ir; /* Need to deal with overflow and div 0 */ break; } } break; case OPUNARY: /* Unary Operators */ switch(opcode) { case OPNEG: if(rtype==OPRINT) { i = -(*--sp); *sp++ = i; } else if (rtype==OPRFLOAT) { f = *(DAFLOAT *)(--sp); f = -f; *sp++ = *(DAINT *)&f; } else { POPDOUBLE(d);/*phil*/ d = -d; PUSHDOUBLE(d);/*phil*/ } break; case OPNOT: case OPCOMP: if(rtype==OPRINT) { POPINT(i); } else if(rtype==OPRFLOAT) { POPFLOAT(f);/*phil*/ i = floatToLong(f); } else { POPDOUBLE(d);/*phil*/ i = floatToLong(d); } i = (opcode == OPNOT ? !i : ~i); *sp++ = i; break; } break; default: fprintf(STDERR,"Test block %s: Operator %x not yet implimented\n", blockname,opcode); break; } /* Terminates switch */ } else { /* terminates if(rawopcode >=OPLP) *//* Old Style, May not work anymore */ switch(*pc++)
thStatus thBookaTest(char *line, CODEPTR *codeheadp, CODEPTR *codenextp, CODEPTR *codelimitp, CODEPTR *codelastop, daVarStructList **vlisthead) /* if expflag != 0, still treat as an expression even if there is no equal sign in the line. Return codes: S_SUCCESS = Line OK S_FAILURE = Line not executable */ { /* int type;*/ char *args[20]; int nargs; thTokenType toktyp; daVarStruct var, *varp; thTestType test_type; int forcefloat; int iarg; char *token; CODEPTR codenext; int index; /* Used for index into arrays */ thStatus status; int expflag; if(codelastop) expflag = 1; else expflag = 0; status = S_SUCCESS; if(*codenextp + 2*strlen(line) > *codelimitp) { CODEPTR src,dst,newhead; int newsize; /* printf("Increasing the size of the code stack from %d ", *codelimitp-*codeheadp);*/ src = *codeheadp; newsize = max((*codelimitp-*codeheadp)+CODEGROWSIZE ,(*codenextp-*codeheadp)+2*strlen(line)); newhead = dst = (CODEPTR) malloc(sizeof(CODE)*newsize); while(src < *codenextp) *dst++ = *src++; if(*codeheadp) free(*codeheadp); *codelimitp = newhead + newsize; *codeheadp = newhead; *codenextp = *codenextp + (dst - src); /*printf("to %d, using %d\n",*codelimitp-*codeheadp,*codenextp - *codeheadp);*/ } codenext = *codenextp; /* printf("Booking \"%s\"\n",line);*/ if(strchr(line,'=')||expflag) { char *linep; int TOKEN,TOKCOMP; char *tokstr; CODE tokval; void *tokptr; CODE *osp, *tsp, opcode; CODE rightoptype,leftoptype,resultoptype; osp = opstack; /* Stack of pending operators */ *osp = '\0'; tsp = typstack; /* Stack of Current result type */ /* Like the stack in the executor but only */ /* contains the data types */ linep = line; do { /* Get tokens until there are no more (last token will be OPEOL) */ linep = thGetTok(linep,&TOKEN, &tokstr, &tokval, &tokptr, expflag, vlisthead); if(tokstr) { /* Operand */ /* printf("Operand %s |",tokstr);*/ if(codelastop) *codelastop = codenext; /* HACK for thImmed: Save ptr to last operator */ if(TOKEN) { if(tokptr == 0) { /* Value operand - 4 bytes */ *codenext++ = TOKEN; /* String not put on stack at moment */ *codenext++ = tokval; } else { /* Pointer operand - maybe 8 bytes */ *codenext++ = TOKEN; #ifdef USEMEMCPY memcpy(((void **)codenext)++,&tokptr,sizeof(void *)); #else *(void **)codenext = tokptr;/*phil*/ codenext = (CODEPTR) (void **) ((void **)codenext +1); #endif } /* If TOKEN is push function, then tokval is an index into a list of functions. We put this index on tsp instead of the result type. */ if(TOKEN==OPPUSHFUNCTION) { *tsp++ = tokval; } else { *tsp++ = TOKEN & OPRESTYPEMASK; } } else { fprintf(STDERR,"thTest: Unregistered variable %s\n",tokstr); status = S_TH_UNREG; *codenext++ = OPPUSHINT; *codenext++ = 0; *tsp++ = OPPUSHINT & OPRESTYPEMASK; } } else { /* Operator */ switch(TOKEN) { case 0: fprintf(STDERR,"thTest: Bad token\n"); return(S_FAILURE); break; case OPLP: *++osp = TOKEN; break; default: /* printf("OSP:"); {CODE *sp; for(sp=opstack;sp<=osp;sp++) printf("%x:",*sp);} printf("\n"); */ /* Generate code for all operators of equal or higher precedence that are pending on the operator stack. */ if((TOKEN & OPGROUPMASK) == OPLINDEXGROUP) TOKCOMP = 0xFFFFFFF; /* Nothing higher in precedence */ else TOKCOMP = TOKEN & OPPRECMASK; while((*osp & OPPRECMASK) >= TOKCOMP){ /* if((*osp & OPPRECMASK) == OPLINDEX){*/ if((*osp & OPGROUPMASK) == OPLINDEXGROUP){ if(TOKEN == OPRP) { if(*osp == OPLFARG) TOKEN = OPRFARG; else TOKEN = OPRINDEX; /* Break from case */ } TOKCOMP = 0xFFFFFFF; /* Terminate osp rundown */ } rightoptype = *--tsp; leftoptype = ((*osp & OPPRECMASK) == OPUNARY) ? 0 : (*--tsp); /* If the Operator is "evaluate function", we need to find out what the function is so that we can get the correct result type. leftoptype should be an index into "intrinsic_functions". We can use that and rightoptype to look up the resulttype. */ if(*osp==OPLFARG) { resultoptype = intrinsic_functions[leftoptype].result[rightoptype]; } else { resultoptype = thGetResultType(*osp,leftoptype,rightoptype); } opcode = *osp--; opcode |= (leftoptype << 8) | (rightoptype << 4) | resultoptype; if(codelastop) if((opcode&&OPCODEMASK) !=OPEOL) *codelastop = codenext; /* HACK for thImmed: Save ptr to last operator */ *codenext++ = opcode; *tsp++ = resultoptype; /* Keep a rpn stack of the data type */ } if(TOKEN == OPRINDEX || TOKEN == OPRFARG) break; /* No clean up needed */ if(TOKEN == OPRP) { if(*osp == OPLP) osp--; /* ) removes matching ( */ else { fprintf(STDERR,"Right paren not matched by left\n"); return(S_FAILURE); } } else if(TOKEN == OPEOL || TOKEN == OPCOMMA) { if(codelastop) if(TOKEN==OPCOMMA) *codelastop = codenext; /* HACK for thImmed: Save ptr to last operator */ *codenext++ = TOKEN | (*--tsp) << 4; /* Leave type in Right type field */ } else { *++osp = TOKEN; } break; } } /* Token processed */ } while (linep); /* Check that stacks are OK. Need to add some clean up of allocated memory. */ if(tsp != typstack) { fprintf(STDERR,"%d items left on type stack\n",tsp-typstack); return(S_FAILURE); } if(osp != opstack) { fprintf(STDERR,"%d items left on operand stack\n",osp-opstack); return(S_FAILURE); } } else { /* Old style test lines */ int i; nargs = thCommas(line,args); for(i=0;i<nargs;i++){ args[i] = thSpaceStrip(args[i]); /* Remove all space from the argument */ } if(nargs <= 1) return(S_FAILURE); { /* Interpret the test type. */ for(test_type=0;test_type<tBAD;test_type++){ if(testCodes[test_type][0] == toupper(args[1][0]) && testCodes[test_type][1] == toupper(args[1][1])) break; } if(test_type == tBAD) return(S_FAILURE); /* printf("%s\n",testCodes[test_type]);*/ } if(test_type == tGATE || test_type == tEQ) { forcefloat = 1; } else forcefloat = 0; for(iarg=2;iarg<nargs;iarg++){ DAFLOAT f; /* Should do double here */ token = args[iarg]; toktyp = thIDToken(token); switch((toktyp = thIDToken(token))) { case TOKINT: *codenext++ = PUSHI; if(forcefloat) { f = atof(token); *codenext++ = *(DAINT *)&f; } else { DAINT i; /* Used to be %li and %ld, but that makes 8 byte result stuffed into 4 byte i */ if(token[0] == '0' && (token[1] == 'x' || token[1] == 'X')) { sscanf(token,"%i",&i); /* Treat as Hex */ } else { sscanf(token,"%d",&i); /* Treat as decimal */ } *codenext++ = i; } break; case TOKFLOAT: /* Should Do all floats as doubles */ *codenext++ = PUSHI; if(forcefloat) { f = atof(token); *codenext++ = *(DAINT *)&f; } else { *codenext++ = (DAINT) floatToLong(atof(token)); } break; case TOKARRAY: case TOKVAR: { char *p; int index; char leftp; if(toktyp == TOKARRAY) { p = thTokenArray(token,&index); leftp = *p; *p = 0; /* Save ( or [ then null terminate */ } else index = 0; if(daVarLookup(token,&var)!=S_SUCCESS) { fprintf(STDERR,"(thTest) %s not registered\n",token); *codenext++ = PUSHI; if(forcefloat) { f = 0.0; *codenext++ = *(DAINT *)&f; } else *codenext++ = 0; } else { if(forcefloat) if(var.type == DAVARINT) *codenext++ = PUSHITOFS; /* Push converting to float and skip */ else if(var.type == DAVARFLOAT) *codenext++ = PUSHS; else *codenext++ = PUSHI; /* Push immediate */ else if(var.type == DAVARINT) *codenext++ = PUSHS; /* Push and skip */ else if(var.type == DAVARFLOAT) *codenext++ = PUSHFTOIS; else *codenext++ = PUSHI; /* Push immediate */ if(toktyp == TOKARRAY) *p = leftp; if(var.type == DAVARINT || var.type == DAVARFLOAT) { *(void **)codenext = ((DAINT *) var.varptr+index);/*phil*/ codenext = (CODEPTR) (void **) ((void **)codenext + 1); *((void **)codenext) = (void *) malloc(sizeof(token)+1); strcpy((char *) *(void **)codenext,token); codenext = (CODEPTR) (void **) ((void **)codenext + 1); } else { if(forcefloat) { f = 0.0; *codenext++ = *(DAINT *)&f; } else *codenext++ = 0; } } } break; } } *codenext++ = test_type; /* Operation to do on pushed args */ *codenext++ = nargs-2; /* # of args pushed on stack for this op */ /* Now push test result on stack */ *codenext++ = POPS; token = args[0]; toktyp = thIDToken(token); index = 0; switch((toktyp = thIDToken(token))) { case TOKINT: case TOKFLOAT: fprintf(STDERR,"(thTest) Test result must be a variable name\n"); return(S_FAILURE); /* No test is added to program */ case TOKARRAY: /* First check if variable with index has been already registered perhaps from a previous booking of histograms */ if(daVarLookup(token,&var) != S_SUCCESS){ char *p; char leftp; p = thTokenArray(token,&index); leftp = *p; *p = 0; /* Save ( or [ then null terminate */ if(daVarLookup(token,&var) != S_SUCCESS){ fprintf(STDERR, "(thTest) Arrays %s must be registered\n",token); return(S_FAILURE); } *p = leftp; /* Restore the left ( or [ */ if(index >= var.size) { fprintf(STDERR, "(thTest) Array size for %s exceeded\n",token); return(S_FAILURE); } if(var.type != DAVARINT) { fprintf(STDERR, "(thTest) Array %s must be of integer*4\n",token); return(S_FAILURE); } var.varptr = (DAINT *) var.varptr + index; var.name = token; var.opaque = 0; } var.title = token; /* Eventually be the input line */ break; case TOKVAR: if(daVarLookup(token,&var)!=S_SUCCESS) { var.name = token; var.varptr = (void *) malloc(sizeof(DAINT)); var.opaque = 0; var.rhook = 0; var.whook = 0; var.type = DAVARINT; var.flag = DAVAR_READONLY | DAVAR_REPOINTOK; /* Do I need to set the size to 1 here??? */ } var.title = token; break; } daVarRegister((int) 0, &var); /* Create or replace variable */ *(void **)codenext = ((DAINT *) var.varptr);/*phil*/ codenext = (CODEPTR) (void **) ((void **)codenext + 1); /* Save the token string for future reference */ *((void **)codenext) = ((void *) malloc(strlen(token)+1)); strcpy((char *) *(void **)codenext,token); codenext = (CODEPTR) (void **) ((void **)codenext + 1); } *codenextp = codenext; return(status); }
thStatus thEvalImed(char *line, DADOUBLE *d, DAINT *i) /* ImmedOBiately evaluate the expression in line. Will internally evaluate to a float, and then pass back both the float and interized values. */ { CODEPTR codehead, codenext, codelimit, codelastop; int codesize; #define RDOUBLE #ifdef RDOUBLE double result; #else float result; /* Should change to double */ #endif thStatus retcode; /* printf("%s=",line);*/ codesize = 10+2*strlen(line); codehead = codenext = (CODEPTR) malloc(sizeof(CODE)*codesize); codelimit = codehead + codesize; #ifdef RDOUBLE *codenext++ = OPPUSHPDOUBLE; #ifdef USEMEMCPY { void *resultp; resultp = &result; memcpy(((void **)codenext)++, (void *) &resultp, sizeof(void *)); } #else *((void **) codenext) = (void *) &result; /*phil*/ codenext = (CODEPTR) (void **) ((void **)codenext +1); #endif /* printf("%x\n",codenext);*/ #else *codenext++ = OPPUSHPFLOAT; /* Should change to double */ *((void **) codenext)++ = (void *) &result; #endif retcode = S_SUCCESS; if(thBookaTest(line,&codehead,&codenext,&codelimit,&codelastop,0)!=S_SUCCESS) { fprintf(STDERR,"Failure interpreting expression |%s|\n",line); result = 0.0; retcode = S_FAILURE; } else { int exptype; CODE lastop; #if 0 printf("%x-%x=%d\n",codenext,codehead,codenext-codehead); { CODEPTR code; for(code=codehead;code < codenext; code++) if(code==codelastop) printf("* %x\n",*code); else printf(" %x\n",*code); } #endif codenext = codelastop; exptype = *codenext++ & OPRESTYPEMASK; lastop = *codelastop & OPCODEMASK; if(lastop == OPPUSHPINT || lastop == OPPUSHINTP) { codenext = (CODEPTR) (DAINT **) ((DAINT **)codenext + 1);/*phil*/ } else if(lastop == OPPUSHINT) { if(exptype == OPRDOUBLE) { codenext = (CODEPTR) (DADOUBLE **) ((DADOUBLE **)codenext + 1);/*phil*/ } else { /* Assume ints, floats have size */ codenext = (CODEPTR) (DAINT *) ((DAINT *)codenext + 1);/*phil*/ } } #ifdef RDOUBLE *codenext++ = OPEQUAL | 0x202 | (exptype<<4); #else *codenext++ = OPEQUAL | 0x101 | (exptype<<4); #endif #ifdef RDOUBLE *codenext++ = OPEOL | (OPRDOUBLE<<4); #else *codenext++ = OPEOL; #endif if(thExecuteCode("IMMED",codehead,codenext)!=S_SUCCESS){ fprintf(STDERR,"Failure evaluating expression |%s|\n",line); result = 0.0; retcode = S_FAILURE; } } /* printf("%f\n",result);*/ free(codehead); if(d) *d = result; if(i) { if(result>=INT_MAX || result <=-INT_MAX) { if(retcode==S_SUCCESS) retcode=S_INTOVF; } else { *i = floatToLong(result); } } return(retcode); }
thStatus thExecuteaGethitBlock(daVarStruct *var) /* Execute a gethit block */ { thGethitOpaque *opqptr; thGethitList *thisgethit; int nhits; int ihit; int icoord,ncoord; DAINT **coord_array; double dval; int ival; /* opqptr = block->var->opaque;*/ /* Structure that describes the gethits */ opqptr = var->opaque; /* printf("opqptr=%x\n",opqptr);*/ nhits = *(opqptr->vnhits); ncoord = opqptr->ncoord; coord_array = opqptr->coord_array; thisgethit = opqptr->hlisthead; /* printf("%d hits this event\n",nhits);*/ while(thisgethit){ /* Inefficient algorithm */ /* printf("Getting %s\n",thisgethit->dest->name);*/ if(thisgethit->test) { /* Assume that test is integer */ *((DAINT *) thisgethit->test->varptr + thisgethit->testindex) = FALSE; } for(ihit=0;ihit<nhits;ihit++){ for(icoord=0;icoord<ncoord;icoord++){ /* printf("Hit=%d, Coord=%d %d %d\n",ihit,icoord ,coord_array[icoord][ihit],thisgethit->coord[icoord]);*/ if((coord_array[icoord])[ihit] != thisgethit->coord[icoord]) break; } if(icoord >= ncoord){ /* All coordinates matched */ int srctype; /* printf("Matched at hit %d, %d %d\n",ihit,icoord,ncoord);*/ if(thisgethit->test) *((DAINT *) thisgethit->test->varptr + thisgethit->testindex) = TRUE; /* Need to grab the data value, stuff it into variable */ srctype = thisgethit->src->type; switch(srctype) { case DAVARINT: ival = ((DAINT *)thisgethit->src->varptr)[ihit]; switch(thisgethit->dest->type) { case DAVARINT: ((DAINT *)thisgethit->dest->varptr)[thisgethit->destindex] = ival; break; case DAVARFLOAT: ((DAFLOAT *)thisgethit->dest->varptr)[thisgethit->destindex] = ival; break; case DAVARDOUBLE: ((DADOUBLE *)thisgethit->dest->varptr)[thisgethit->destindex] = ival; break; } break; case DAVARFLOAT: case DAVARDOUBLE: if(srctype == DAVARFLOAT) dval = ((DAFLOAT *)thisgethit->src->varptr)[ihit]; else dval = ((DADOUBLE *)thisgethit->src->varptr)[ihit]; switch(thisgethit->dest->type) { case DAVARINT: ((DAINT *)thisgethit->dest->varptr)[thisgethit->destindex] = floatToLong(dval); break; case DAVARFLOAT: ((DAFLOAT *)thisgethit->dest->varptr)[thisgethit->destindex] = dval; break; case DAVARDOUBLE: ((DADOUBLE *)thisgethit->dest->varptr)[thisgethit->destindex] = dval; break; } break; } break; /* Only match one hit for now */ } } thisgethit = thisgethit->next; } return(S_SUCCESS); }