int main() { /* Brents example program */ flash x,pi; miracl *mip=mirsys(-35,0); x=mirvar(0); pi=mirvar(0); mip->RPOINT=ON; printf("Calculating pi..\n"); fpi(pi); cotnum(pi,stdout); /* output pi */ printf("Calculating exp(pi*(163/9)^0.5)\n"); fconv(163,9,x); froot(x,2,x); fmul(x,pi,x); fexp(x,x); cotnum(x,stdout); printf("Calculating exp(pi*(163)^0.5)\n"); fpower(x,3,x); cotnum(x,stdout); return 0; }
static BOOL act(int p,int q) { /* act on selected key */ int k,n,c; aprint(PRESSED,4+5*p,6+3*q,keys[q][p]); switch(p+7*q) { case 0: if (degrees) fmul(x,radeg,x); if (hyp) fsinh(x,x); else fsin(x,x); newx=TRUE; break; case 1: if (degrees) fmul(x,radeg,x); if (hyp) fcosh(x,x); else fcos(x,x); newx=TRUE; break; case 2: if (degrees) fmul(x,radeg,x); if (hyp) ftanh(x,x); else ftan(x,x); newx=TRUE; break; case 3: if (lgbase>0) { n=size(x); if (abs(n)<MR_TOOBIG) { convert(lgbase,x); if (n<0) frecip(x,x); fpower(x,abs(n),x); newx=TRUE; break; } if (lgbase==2) fmul(x,loge2,x); if (lgbase==10) fmul(x,loge10,x); } fexp(x,x); newx=TRUE; break; case 4: mip->RPOINT=!mip->RPOINT; newx=TRUE; break; case 5: clrall(); newx=TRUE; break; case 6: return TRUE; case 7: if (hyp) fasinh(x,x); else fasin(x,x); if (degrees) fdiv(x,radeg,x); newx=TRUE; break; case 8: if (hyp) facosh(x,x); else facos(x,x); if (degrees) fdiv(x,radeg,x); newx=TRUE; break; case 9: if (hyp) fatanh(x,x); else fatan(x,x); if (degrees) fdiv(x,radeg,x); newx=TRUE; break; case 10: flog(x,x); if (lgbase==2) fdiv(x,loge2,x); if (lgbase==10) fdiv(x,loge10,x); newx=TRUE; break; case 11: newx=TRUE; k=3; forever { aprint(INVER,2+stptr[k],2,settings[k][option[k]]); curser(2+stptr[k],2); c=arrow(gethit()); if (c==1) { if (option[k]==nops[k]) option[k]=0; else option[k]+=1; continue; } aprint(STATCOL,2+stptr[k],2,settings[k][option[k]]); if (c==0 || c==2) break; if (c==4 && k>0) k--; if (c==3 && k<3) k++; } setopts(); break; case 12: chekit(7); break; case 13: result=FALSE; if (ipt==0) break; ipt--; mybuff[ipt]='\0'; if (ipt==0) clr(); just(mybuff); cinstr(x,mybuff); newx=TRUE; break; case 14: if (!next('7')) putchar(BELL); break; case 15: if (!next('8')) putchar(BELL); break; case 16: if (!next('9')) putchar(BELL); break; case 17: chekit(6); break; case 18: chekit(5); break; case 19: chekit(4); break; case 20: copy(m,x); newx=TRUE; break; case 21: if (!next('4')) putchar(BELL); break; case 22: if (!next('5')) putchar(BELL); break; case 23: if (!next('6')) putchar(BELL); break; case 24: fmul(x,x,x); newx=TRUE; break; case 25: froot(x,2,x); newx=TRUE; break; case 26: chekit(3); break; case 27: brkt=0; chekit(0); flag=OFF; fadd(m,x,m); newx=TRUE; break; case 28: if (!next('1')) putchar(BELL); break; case 29: if (!next('2')) putchar(BELL); break; case 30: if (!next('3')) putchar(BELL); break; case 31: frecip(x,x); newx=TRUE; break; case 32: fpi(x); newx=TRUE; break; case 33: chekit(2); break; case 34: negify(x,x); newx=TRUE; break; case 35: if (!next('0')) putchar(BELL); break; case 36: if (!next('/')) putchar(BELL); break; case 37: if (!next('.')) putchar(BELL); break; case 38: if (ipt>0) { putchar(BELL); result=FALSE; } else { zero(x); brkt+=1; newx=TRUE; } break; case 39: if (brkt>0) { chekit(0); brkt-=1; } else { putchar(BELL); result=FALSE; } break; case 40: chekit(1); break; case 41: brkt=0; equals(0); flag=OFF; break; } return FALSE; }
/* pop exponent into rcx (loop counter), pop base into rax, cp rax to r8, loop til counter == 0 and push result (rax) MODIFIES lhs->dubval and rhs->dubval (note for future planning) ^^^ would have to save value before calling fdivide() */ Type *power(Type *lhs, Type *rhs){ Type *tmp;//points to lhs if lhs->attr == NULL, o.w. points to malloc type* Type *tempHolder; //UNUSED BUT HERE WHEN WE DECIDE IT'S NECESSARY int negFlag = 0; //set to 1 when lhs is negative typeCheck(lhs, rhs); if( lhs->type == 301) return fpower(lhs, rhs); /* USING special trick because the pow funct is messy */ if( lhs->attr != NULL){ //we cannot assign the result to a known variable here! //so we need to return a temp value tmp = makeTempStruct(lhs->type); tmp->longval = lhs->longval; } else{ tmp = lhs; } if( lhs->longval < 0 ){ tmp->longval = -tmp->longval; negFlag = 1; //set negative flag fprintf( textSec, "\tneg\tQWORD [rsp + 8]\n"); } if( rhs->longval < 0 ){ /* Do something special because assembly code fails */ // (1) complement the complement rhs->longval = -rhs->longval; //x^(-p) <==> (HERE) 1/(x^(p)) ==> fdivide( 1, (x^(p) ) tmp->longval = pow(tmp->longval, rhs->longval); //restore rhs value rhs->longval = -rhs->longval; fprintf( textSec, "\tneg\tQWORD [rsp]\n"); fprintf( textSec, "\tpop\trcx\n\tpop rdi\n\tcall\tloopPow\n"); // may need to have storage in .data for these two below fprintf( textSec, "\tpush QWORD 1\n\tpush\trax\n"); //x^(-p) <==> 1/(x^(p)) (HERE) ==> fdivide( 1, (x^(p) ) rhs->dubval = 1.0; // lhs = (double) lhs->longval tmp->dubval = tmp->longval; //x^(-p) <==> 1/(x^(p)) ==> fdivide( 1, (x^(p) ) (HERE) //===================================================== rhs->dubval /= tmp->dubval; /* asm write */ fprintf( textSec, "\tfld\tQWORD [rsp + 8]\n"); //push value into st(0) fprintf( textSec, "\tfld\tQWORD [rsp]\n"); fprintf( textSec, "\tadd\trsp, 8\n"); fprintf( textSec, "\t;div the previous st0 w/ top and discard top\n"); fprintf( textSec, "\tfdivp\tst1, st0\n"); //pop FPU stack and store INTEGER in rsp fprintf( textSec, "\tfistp\tQWORD [rsp] ;converts float to int!!!\n"); //===================================================== //trucate the result back to an integer tmp->longval = (int) rhs->dubval; //restore values of lhs->dubval and rhs->dubval /* lhs->dubval = savedlhsDUBVAL rhs->dubval = savedrhsDUBVAL */ // if lhs is negative and exponent is odd... if( negFlag && rhs->longval % 2 ){ tmp->longval = -tmp->longval; /* asm write */ fprintf( textSec, "\tneg\tQWORD [rsp]\n"); } /* pffffeww... */ return tmp; } /* update value in symbol table */ tmp->longval = pow(lhs->longval, rhs->longval); /* asm write */ fprintf( textSec, "\tpop\trcx\n\tpop rdi\n\tcall\tloopPow\n"); fprintf( textSec, "\tpush\trax\n"); // if lhs is negative and exponent is odd... if( negFlag && rhs->longval % 2 ){ tmp->longval = -tmp->longval; /* asm write */ fprintf( textSec, "\tneg\tQWORD [rsp]\n"); } //check name: if NULL, not id, malloc, o.w. struct remains in symtbl //if(rhs->attr == NULL) //rhs is no longer needed, free the memory. /*free(rhs);*/ //rhs = NULL; return tmp; }