BigInt *divide(BigInt *bigger, BigInt *smaller) { if (eq(smaller, fromString("0"))) { fprintf(stderr, "division by 0\n"); return NULL; } int sign = bigger->sign * smaller->sign; bigger = bigabs(bigger); smaller = bigabs(smaller); BigInt *ten = fromString("10"); BigInt *divisor = smaller; if (eq(smaller, bigger)) { return fromString("1"); } if (geq(smaller, bigger)) { return fromString("0"); } BigInt *tens = fromString("1"); while(geq(mult(divisor, ten), bigger)) { divisor = mult(divisor, ten); tens = mult(tens, ten); } BigInt *result = add(tens,divide(subtract(bigger, divisor), smaller)); result->sign = sign; return result; }
int eq(BigInt *a, BigInt *b) { if (geq(a,b) && geq(b,a)) { return 1; } else { return 0; } }
// returns true if there is an intersection with the face, else returns false int testLinePlaneIntersection(GLfloat* la, GLfloat* lb, GLfloat* p0, GLfloat* p1, GLfloat* p2) { // returns true for indices where intersection occurs GLfloat a1=la[0]-lb[0], b1=la[1]-lb[1], c1=la[2]-lb[2]; GLfloat a2=p1[0]-p0[0], b2=p1[1]-p0[1], c2=p1[2]-p0[2]; GLfloat a3=p2[0]-p0[0], b3=p2[1]-p0[1], c3=p2[2]-p0[2]; GLfloat l1=la[0]-p0[0], l2=la[1]-p0[1], l3=la[2]-p0[2]; GLfloat denom=(a1*b2*c3 - a1*b3*c2 - a2*b1*c3 + a2*b3*c1 + a3*b1*c2 - a3*b2*c1); if (fabs(denom)<1e-6) return 0; // if you can't find an intersection because there is no solution, return notFacesSeen=0 ==> you are never blocked GLfloat num1= (a2*b3*l3 - a3*b2*l3 - a2*c3*l2 + a3*c2*l2 + b2*c3*l1 - b3*c2*l1); GLfloat num2=-(a1*b3*l3 - a3*b1*l3 - a1*c3*l2 + a3*c1*l2 + b1*c3*l1 - b3*c1*l1); GLfloat num3= (a1*b2*l3 - a2*b1*l3 - a1*c2*l2 + a2*c1*l2 + b1*c2*l1 - b2*c1*l1); GLfloat t1=num1/denom, t2=num2/denom, t3=num3/denom; return geq(t1,0) & leq(t1,1) & geq(t2,0) & geq(t3,0) & leq(t3,1) & leq(t2,1) & leq(t2+t3,1); }
static bool between(int a, int b, int c) { return and(geq(a, b), lt(b, c)); };
void opr() { switch ( ir.m) { //rtn case 0: ret(); break; //neg case 1: neg(); break; //add case 2: add(); break; //sub case 3: sub(); break; //mul case 4: mul(); break; //div case 5: divid(); break; //odd case 6: odd(); break; //mod case 7: mod(); break; //eql case 8: eql(); break; //neq case 9: neq(); break; //lss case 10: lss(); break; //leq case 11: leq(); break; //gtr case 12: gtr(); break; //geq case 13: geq(); break; default: fprintf(output, "OP code input was invalid. "); sio3(); } }
void executeInstruction() { switch(ir.op) { case 1: lit(); break; case 2: opr(); break; case 3: lod(); break; case 4: sto(); break; case 5: cal(); break; case 6: inc(); break; case 7: jmp(); break; case 8: jpc(); break; case 9: sio(); default: break; } void opr() { switch ( ir.m) { //rtn case 0: ret(); break; //neg case 1: neg(); break; //add case 2: add(); break; //sub case 3: sub(); break; //mul case 4: mul(); break; //div case 5: divid(); break; //odd case 6: odd(); break; //mod case 7: mod(); break; //eql case 8: eql(); break; //neq case 9: neq(); break; //lss case 10: lss(); break; //leq case 11: leq(); break; //gtr case 12: gtr(); break; //geq case 13: geq(); break; default: fprintf(output, "OP code input was invalid. "); halt = 1; break; } } }
CompileOutput *Compiler::Compile(AMXRef amx) { Prepare(amx); Disassembler disasm(amx); Instruction instr; bool error = false; while (!error && disasm.Decode(instr, error)) { if (!Process(instr)) { error = true; break; } switch (instr.opcode().GetId()) { case OP_LOAD_PRI: load_pri(instr.operand()); break; case OP_LOAD_ALT: load_alt(instr.operand()); break; case OP_LOAD_S_PRI: load_s_pri(instr.operand()); break; case OP_LOAD_S_ALT: load_s_alt(instr.operand()); break; case OP_LREF_PRI: lref_pri(instr.operand()); break; case OP_LREF_ALT: lref_alt(instr.operand()); break; case OP_LREF_S_PRI: lref_s_pri(instr.operand()); break; case OP_LREF_S_ALT: lref_s_alt(instr.operand()); break; case OP_LOAD_I: load_i(); break; case OP_LODB_I: lodb_i(instr.operand()); break; case OP_CONST_PRI: const_pri(instr.operand()); break; case OP_CONST_ALT: const_alt(instr.operand()); break; case OP_ADDR_PRI: addr_pri(instr.operand()); break; case OP_ADDR_ALT: addr_alt(instr.operand()); break; case OP_STOR_PRI: stor_pri(instr.operand()); break; case OP_STOR_ALT: stor_alt(instr.operand()); break; case OP_STOR_S_PRI: stor_s_pri(instr.operand()); break; case OP_STOR_S_ALT: stor_s_alt(instr.operand()); break; case OP_SREF_PRI: sref_pri(instr.operand()); break; case OP_SREF_ALT: sref_alt(instr.operand()); break; case OP_SREF_S_PRI: sref_s_pri(instr.operand()); break; case OP_SREF_S_ALT: sref_s_alt(instr.operand()); break; case OP_STOR_I: stor_i(); break; case OP_STRB_I: strb_i(instr.operand()); break; case OP_LIDX: lidx(); break; case OP_LIDX_B: lidx_b(instr.operand()); break; case OP_IDXADDR: idxaddr(); break; case OP_IDXADDR_B: idxaddr_b(instr.operand()); break; case OP_ALIGN_PRI: align_pri(instr.operand()); break; case OP_ALIGN_ALT: align_alt(instr.operand()); break; case OP_LCTRL: lctrl(instr.operand(), instr.address() + instr.size()); break; case OP_SCTRL: sctrl(instr.operand()); break; case OP_MOVE_PRI: move_pri(); break; case OP_MOVE_ALT: move_alt(); break; case OP_XCHG: xchg(); break; case OP_PUSH_PRI: push_pri(); break; case OP_PUSH_ALT: push_alt(); break; case OP_PUSH_C: push_c(instr.operand()); break; case OP_PUSH: push(instr.operand()); break; case OP_PUSH_S: push_s(instr.operand()); break; case OP_POP_PRI: pop_pri(); break; case OP_POP_ALT: pop_alt(); break; case OP_STACK: // value stack(instr.operand()); break; case OP_HEAP: heap(instr.operand()); break; case OP_PROC: proc(); break; case OP_RET: ret(); break; case OP_RETN: retn(); break; case OP_JUMP_PRI: jump_pri(); break; case OP_CALL: case OP_JUMP: case OP_JZER: case OP_JNZ: case OP_JEQ: case OP_JNEQ: case OP_JLESS: case OP_JLEQ: case OP_JGRTR: case OP_JGEQ: case OP_JSLESS: case OP_JSLEQ: case OP_JSGRTR: case OP_JSGEQ: { cell dest = instr.operand() - reinterpret_cast<cell>(amx.code()); switch (instr.opcode().GetId()) { case OP_CALL: call(dest); break; case OP_JUMP: jump(dest); break; case OP_JZER: jzer(dest); break; case OP_JNZ: jnz(dest); break; case OP_JEQ: jeq(dest); break; case OP_JNEQ: jneq(dest); break; case OP_JLESS: jless(dest); break; case OP_JLEQ: jleq(dest); break; case OP_JGRTR: jgrtr(dest); break; case OP_JGEQ: jgeq(dest); break; case OP_JSLESS: jsless(dest); break; case OP_JSLEQ: jsleq(dest); break; case OP_JSGRTR: jsgrtr(dest); break; case OP_JSGEQ: jsgeq(dest); break; } break; } case OP_SHL: shl(); break; case OP_SHR: shr(); break; case OP_SSHR: sshr(); break; case OP_SHL_C_PRI: shl_c_pri(instr.operand()); break; case OP_SHL_C_ALT: shl_c_alt(instr.operand()); break; case OP_SHR_C_PRI: shr_c_pri(instr.operand()); break; case OP_SHR_C_ALT: shr_c_alt(instr.operand()); break; case OP_SMUL: smul(); break; case OP_SDIV: sdiv(); break; case OP_SDIV_ALT: sdiv_alt(); break; case OP_UMUL: umul(); break; case OP_UDIV: udiv(); break; case OP_UDIV_ALT: udiv_alt(); break; case OP_ADD: add(); break; case OP_SUB: sub(); break; case OP_SUB_ALT: sub_alt(); break; case OP_AND: and_(); break; case OP_OR: or_(); break; case OP_XOR: xor_(); break; case OP_NOT: not_(); break; case OP_NEG: neg(); break; case OP_INVERT: invert(); break; case OP_ADD_C: add_c(instr.operand()); break; case OP_SMUL_C: smul_c(instr.operand()); break; case OP_ZERO_PRI: zero_pri(); break; case OP_ZERO_ALT: zero_alt(); break; case OP_ZERO: zero(instr.operand()); break; case OP_ZERO_S: zero_s(instr.operand()); break; case OP_SIGN_PRI: sign_pri(); break; case OP_SIGN_ALT: sign_alt(); break; case OP_EQ: eq(); break; case OP_NEQ: neq(); break; case OP_LESS: less(); break; case OP_LEQ: leq(); break; case OP_GRTR: grtr(); break; case OP_GEQ: geq(); break; case OP_SLESS: sless(); break; case OP_SLEQ: sleq(); break; case OP_SGRTR: sgrtr(); break; case OP_SGEQ: sgeq(); break; case OP_EQ_C_PRI: eq_c_pri(instr.operand()); break; case OP_EQ_C_ALT: eq_c_alt(instr.operand()); break; case OP_INC_PRI: inc_pri(); break; case OP_INC_ALT: inc_alt(); break; case OP_INC: inc(instr.operand()); break; case OP_INC_S: inc_s(instr.operand()); break; case OP_INC_I: inc_i(); break; case OP_DEC_PRI: dec_pri(); break; case OP_DEC_ALT: dec_alt(); break; case OP_DEC: dec(instr.operand()); break; case OP_DEC_S: dec_s(instr.operand()); break; case OP_DEC_I: dec_i(); break; case OP_MOVS: movs(instr.operand()); break; case OP_CMPS: cmps(instr.operand()); break; case OP_FILL: fill(instr.operand()); break; case OP_HALT: halt(instr.operand()); break; case OP_BOUNDS: bounds(instr.operand()); break; case OP_SYSREQ_PRI: sysreq_pri(); break; case OP_SYSREQ_C: { const char *name = amx.GetNativeName(instr.operand()); if (name == 0) { error = true; } else { sysreq_c(instr.operand(), name); } break; } case OP_SYSREQ_D: { const char *name = amx.GetNativeName(amx.FindNative(instr.operand())); if (name == 0) { error = true; } else { sysreq_d(instr.operand(), name); } break; } case OP_SWITCH: switch_(CaseTable(amx, instr.operand())); break; case OP_CASETBL: casetbl(); break; case OP_SWAP_PRI: swap_pri(); break; case OP_SWAP_ALT: swap_alt(); break; case OP_PUSH_ADR: push_adr(instr.operand()); break; case OP_NOP: nop(); break; case OP_BREAK: break_(); break; default: error = true; } } if (error && error_handler_ != 0) { error_handler_->Execute(instr); } return Finish(error); }