Beispiel #1
0
int main()
{
    unsigned int numerator;

    unsigned int denominator;

    unsigned int error, i;

    div_t valid, test, test2;

    printf("Divide Test!\n");
    for (i = 0; i < 100; i++) {
        printf("i=%d\n", i);
        numerator = (random() % 100) + 1;       // (8192 * 4 * 1056)) + 1;       // Memory Size
        denominator = (random() % 10) + 1;      //(65536)) + 1;                  // Possible Page Size
        valid = div(numerator, denominator);
        test = udiv(numerator, denominator);
        if (valid.rem != test.rem) {
            error = 1;
        }
        if (valid.quot != test.quot) {
            error = 1;
        }
        if (error) {
/*
			printf("%4d:",i);
			printf("Numerator = %d,  Denominator = %d\n", numerator, denominator);	
			printf("\tREM:	%8d\t%08x,\t%8x\t%8x\n",valid.rem,	valid.rem,		test.rem,	test2.rem);
			printf("\tQUOT:	%8d\t%08x,\t%8x\t%8x\n",valid.quot,	valid.quot,	test.quot,	test2.rem);
*/
            test = udiv(numerator, denominator);
        }
    }
    return 0;
}
Beispiel #2
0
Datei: div.c Projekt: 8l/go-learn
void
udivgen(Node *l, Node *r, Node *ax, Node *dx)
{
	int a, s, t;
	uint32 m;
	Node nod;

	a = udiv(r->vconst, &m, &s, &t);
//print("a=%ud i=%ld p=%d s=%d m=%lux\n", a, (int32)r->vconst, t, s, m);
	if(t != 0) {
		gins(AMOVL, l, ax);
		gins(ASHRL, nodconst(t), ax);
		gins(AMOVL, nodconst(m), dx);
		gins(AMULL, dx, Z);
	}
	else if(a) {
		if(l->op != OREGISTER) {
			regalloc(&nod, l, Z);
			gins(AMOVL, l, &nod);
			l = &nod;
		}
		gins(AMOVL, nodconst(m), ax);
		gins(AMULL, l, Z);
		gins(AADDL, l, dx);
		gins(ARCRL, nodconst(1), dx);
		if(l == &nod)
			regfree(l);
	}
	else {
		gins(AMOVL, nodconst(m), ax);
		gins(AMULL, l, Z);
	}
	if(s != 0)
		gins(ASHRL, nodconst(s), dx);
}
Beispiel #3
0
/*
 * iinit is called once (from main)
 * very early in initialization.
 * It reads the root's super block
 * and initializes the current date
 * from the last modified date.
 *
 * panic: iinit -- cannot read the super
 * block. Usually because of an IO error.
 */
iinit()
{
	register struct buf *cp, *bp;
	register struct filsys *fp;
	register unsigned i , j ;

	(*bdevsw[major(rootdev)].d_open)(rootdev, 1);
	bp = bread(rootdev, SUPERB);
	cp = geteblk();
	if(u.u_error)
		panic("iinit");
	bcopy(bp->b_un.b_addr, cp->b_un.b_addr, sizeof(struct filsys));
	brelse(bp);
	mount[0].m_bufp = cp;
	mount[0].m_dev = rootdev;
	fp = cp->b_un.b_filsys;
	fp->s_flock = 0;
	fp->s_ilock = 0;
	fp->s_ronly = 0;
	/* on boot, read VAX TODR register (GMT 10 ms.
	*	clicks into current year) and set software time
	*	in 'int time' (GMT seconds since year YRREF)
	*/
	for (i = 0 , j = YRREF ; j < YRCURR ; j++)
		i += (SECYR + (j%4?0:SECDAY)) ;
	time = udiv(mfpr(TODR),100) + i ;
}
 small_bv operator()(const small_bv& v1, const small_bv& v2)
 {
     bv_neg <small_bv, small_bv> neg;
     bv_udiv <small_bv, small_bv> udiv;
     size_t choice = (v1.msb() ? 1 : 0) + (v2.msb() ? 2 : 0);
     switch (choice) {
         case 0: {
             return udiv(v1, v2);
         } case 1: {
             return neg(udiv(neg(v1), v2));
         } case 2: {
             return neg(udiv(v1, neg(v2)));
         } case 3: {
             return udiv(neg(v1), neg(v2));
         } default: {
             assert(false);
         }
     }
 }
Beispiel #5
0
void stats_time( unsigned int cpu_id, struct s_time *st )
{
	uint64_t temp = rdtsc();


	if ( st->last_rdtsc == 0 )
	{
		st->last_rdtsc = temp;
		return;
	}
	
	
	st->ops += temp - st->last_rdtsc;
	st->last_rdtsc = temp;
	
	// update time usage --------------------

		if ( st->ops >= cpu[ cpu_id].bogomips )
		{
			uint32_t rem;
			st->usage += udiv( st->ops, cpu[ cpu_id].bogomips, &rem );
			st->ops   = rem;
		}
}
Beispiel #6
0
static inline  void forth_vm_execute_instruction(forth_context_type *fc, char cmd)
{
//	printf("%c\n",cmd);
//	getchar();
	switch(cmd)
	{
		case '0': push(fc,0);				break;
		case '1': push(fc,1);				break;
		case '2': push(fc,2);				break;
		case '3': push(fc,3);				break;
		case '4': push(fc,4);				break;
		case '5': push(fc,5);				break;
		case '6': push(fc,6);				break;
		case '7': push(fc,7);				break;
		case '8': push(fc,8);				break;
		case '9': push(fc,9);				break;
		case '@': at(fc);					break; //@
		case '!': to(fc);					break; //!
		case 'd': fc->SP+=fc->cell;			break; //drop
		case 'D': dup(fc);					break; //dup
		case 's': swap_(fc);					break; //swap
		case 'l': push(fc,next_cell(fc));	break; //lit
		case '+': add(fc);  				break; //+
		case '-': sub(fc);  				break; //-
		case '*': mul(fc);					break; //*
		case '/': div_(fc);					break; // /
		case '%': mod(fc);					break; //mod
		case '&': and(fc);  				break; // and
		case '|': or(fc);   				break; // or
		case '^': xor(fc);   				break; // xor
		case '>': more(fc);					break; // >
		case '<': less(fc);					break;  // <
		case '=': eq(fc);					break; // =
		case 'b': branch(fc);				break; // branch
		case '?': cbranch(fc);				break; // ?branch
		case 'c': call(fc);					break; // call
		case 'r': ret(fc);					break; // ret
		case 't': to_r(fc);					break; // >R
		case 'f': from_r(fc);				break; // R>
		case 'i': in(fc);					break; // in
		case 'o': out(fc);					break; // out
		case '_': fc->stop=1;				break; // stop
		case 'A': adr0(fc);					break; // @0
		case 1:	  push(fc,fc->SP);			break; // SP@
		case 2:	  fc->SP=pop(fc);			break; // SP!
		case 3:	  push(fc,fc->RP);			break; // RP@
		case 4:	  fc->RP=pop(fc);			break; // RP!
		case 5:	 shl(fc);					break; // <<
		case 6:	 shr(fc);					break; // >>
		case 7:  push(fc,*(size_t *)(fc->mem+fc->RP)); break; // i
		case 8:  cat(fc);					break; // c@
		case 9:  cto(fc);					break; // c!
		case 10: set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1);				break; // nop
		case 11: in_ready(fc);				break; // ?in
		case 12: out_ready(fc);				break; // ?out
		case 16: umod(fc);					break; // umod
		case 17: udiv(fc);					break; // u/
// kernel
		case 'K': kalsym_lookup(fc);		break; // lookup kallsym address
		case 18:  kcall(fc);				break; // kcall
	}
}
Beispiel #7
0
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);
}