int FPU_sub(int flags, int rm, int control_w)
{
	FPU_REG const *a, *b;
	FPU_REG *dest;
	u_char taga, tagb, signa, signb, saved_sign, sign;
	int diff, tag = 0, expa, expb, deststnr;

	a = &st(0);
	taga = FPU_gettag0();

	deststnr = 0;
	if (flags & LOADED) {
		b = (FPU_REG *) rm;
		tagb = flags & 0x0f;
	} else {
		b = &st(rm);
		tagb = FPU_gettagi(rm);

		if (flags & DEST_RM)
			deststnr = rm;
	}

	signa = getsign(a);
	signb = getsign(b);

	if (flags & REV) {
		signa ^= SIGN_NEG;
		signb ^= SIGN_NEG;
	}

	dest = &st(deststnr);
	saved_sign = getsign(dest);

	if (!(taga | tagb)) {
		expa = exponent(a);
		expb = exponent(b);

	      valid_subtract:
		

		diff = expa - expb;

		if (!diff) {
			diff = a->sigh - b->sigh;	
			if (!diff) {
				diff = a->sigl > b->sigl;
				if (!diff)
					diff = -(a->sigl < b->sigl);
			}
		}

		switch ((((int)signa) * 2 + signb) / SIGN_NEG) {
		case 0:	
		case 3:	
			if (diff > 0) {
				
				tag =
				    FPU_u_sub(a, b, dest, control_w, signa,
					      expa, expb);
			} else if (diff == 0) {
				FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);

				
				setsign(dest, ((control_w & CW_RC) != RC_DOWN)
					? SIGN_POS : SIGN_NEG);
				return TAG_Zero;
			} else {
				sign = signa ^ SIGN_NEG;
				tag =
				    FPU_u_sub(b, a, dest, control_w, sign, expb,
					      expa);
			}
			break;
		case 1:	
			tag =
			    FPU_u_add(a, b, dest, control_w, SIGN_POS, expa,
				      expb);
			break;
		case 2:	
			tag =
			    FPU_u_add(a, b, dest, control_w, SIGN_NEG, expa,
				      expb);
			break;
#ifdef PARANOID
		default:
			EXCEPTION(EX_INTERNAL | 0x111);
			return -1;
#endif
		}
		if (tag < 0) {
			setsign(dest, saved_sign);
			return tag;
		}
		FPU_settagi(deststnr, tag);
		return tag;
	}

	if (taga == TAG_Special)
		taga = FPU_Special(a);
	if (tagb == TAG_Special)
		tagb = FPU_Special(b);

	if (((taga == TAG_Valid) && (tagb == TW_Denormal))
	    || ((taga == TW_Denormal) && (tagb == TAG_Valid))
	    || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
		FPU_REG x, y;

		if (denormal_operand() < 0)
			return FPU_Exception;

		FPU_to_exp16(a, &x);
		FPU_to_exp16(b, &y);
		a = &x;
		b = &y;
		expa = exponent16(a);
		expb = exponent16(b);

		goto valid_subtract;
	}

	if ((taga == TW_NaN) || (tagb == TW_NaN)) {
		FPU_REG const *d1, *d2;
		if (flags & REV) {
			d1 = b;
			d2 = a;
		} else {
			d1 = a;
			d2 = b;
		}
		if (flags & LOADED)
			return real_2op_NaN(b, tagb, deststnr, d1);
		if (flags & DEST_RM)
			return real_2op_NaN(a, taga, deststnr, d2);
		else
			return real_2op_NaN(b, tagb, deststnr, d2);
	}

	return add_sub_specials(a, taga, signa, b, tagb, signb ^ SIGN_NEG,
				dest, deststnr, control_w);
}
int FPU_add(FPU_REG const *b, u_char tagb, int deststnr, int control_w)
{
	FPU_REG *a = &st(0);
	FPU_REG *dest = &st(deststnr);
	u_char signb = getsign(b);
	u_char taga = FPU_gettag0();
	u_char signa = getsign(a);
	u_char saved_sign = getsign(dest);
	int diff, tag, expa, expb;

	if (!(taga | tagb)) {
		expa = exponent(a);
		expb = exponent(b);

	      valid_add:
		
		if (!(signa ^ signb)) {
			
			tag =
			    FPU_u_add(a, b, dest, control_w, signa, expa, expb);
		} else {
			
			diff = expa - expb;
			if (!diff) {
				diff = a->sigh - b->sigh;	
				if (!diff) {
					diff = a->sigl > b->sigl;
					if (!diff)
						diff = -(a->sigl < b->sigl);
				}
			}

			if (diff > 0) {
				tag =
				    FPU_u_sub(a, b, dest, control_w, signa,
					      expa, expb);
			} else if (diff < 0) {
				tag =
				    FPU_u_sub(b, a, dest, control_w, signb,
					      expb, expa);
			} else {
				FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
				
				setsign(dest, ((control_w & CW_RC) != RC_DOWN)
					? SIGN_POS : SIGN_NEG);
				return TAG_Zero;
			}
		}

		if (tag < 0) {
			setsign(dest, saved_sign);
			return tag;
		}
		FPU_settagi(deststnr, tag);
		return tag;
	}

	if (taga == TAG_Special)
		taga = FPU_Special(a);
	if (tagb == TAG_Special)
		tagb = FPU_Special(b);

	if (((taga == TAG_Valid) && (tagb == TW_Denormal))
	    || ((taga == TW_Denormal) && (tagb == TAG_Valid))
	    || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
		FPU_REG x, y;

		if (denormal_operand() < 0)
			return FPU_Exception;

		FPU_to_exp16(a, &x);
		FPU_to_exp16(b, &y);
		a = &x;
		b = &y;
		expa = exponent16(a);
		expb = exponent16(b);
		goto valid_add;
	}

	if ((taga == TW_NaN) || (tagb == TW_NaN)) {
		if (deststnr == 0)
			return real_2op_NaN(b, tagb, deststnr, a);
		else
			return real_2op_NaN(a, taga, deststnr, a);
	}

	return add_sub_specials(a, taga, signa, b, tagb, signb,
				dest, deststnr, control_w);
}
Exemplo n.º 3
0
/*
  Divide one register by another and put the result into a third register.
  */
int FPU_div(int flags, int rm, int control_w)
{
	FPU_REG x, y;
	FPU_REG const *a, *b, *st0_ptr, *st_ptr;
	FPU_REG *dest;
	u_char taga, tagb, signa, signb, sign, saved_sign;
	int tag, deststnr;

	if (flags & DEST_RM)
		deststnr = rm;
	else
		deststnr = 0;

	if (flags & REV) {
		b = &st(0);
		st0_ptr = b;
		tagb = FPU_gettag0();
		if (flags & LOADED) {
			a = (FPU_REG *) rm;
			taga = flags & 0x0f;
		} else {
			a = &st(rm);
			st_ptr = a;
			taga = FPU_gettagi(rm);
		}
	} else {
		a = &st(0);
		st0_ptr = a;
		taga = FPU_gettag0();
		if (flags & LOADED) {
			b = (FPU_REG *) rm;
			tagb = flags & 0x0f;
		} else {
			b = &st(rm);
			st_ptr = b;
			tagb = FPU_gettagi(rm);
		}
	}

	signa = getsign(a);
	signb = getsign(b);

	sign = signa ^ signb;

	dest = &st(deststnr);
	saved_sign = getsign(dest);

	if (!(taga | tagb)) {
		/* Both regs Valid, this should be the most common case. */
		reg_copy(a, &x);
		reg_copy(b, &y);
		setpositive(&x);
		setpositive(&y);
		tag = FPU_u_div(&x, &y, dest, control_w, sign);

		if (tag < 0)
			return tag;

		FPU_settagi(deststnr, tag);
		return tag;
	}

	if (taga == TAG_Special)
		taga = FPU_Special(a);
	if (tagb == TAG_Special)
		tagb = FPU_Special(b);

	if (((taga == TAG_Valid) && (tagb == TW_Denormal))
	    || ((taga == TW_Denormal) && (tagb == TAG_Valid))
	    || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
		if (denormal_operand() < 0)
			return FPU_Exception;

		FPU_to_exp16(a, &x);
		FPU_to_exp16(b, &y);
		tag = FPU_u_div(&x, &y, dest, control_w, sign);
		if (tag < 0)
			return tag;

		FPU_settagi(deststnr, tag);
		return tag;
	} else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) {
		if (tagb != TAG_Zero) {
			/* Want to find Zero/Valid */
			if (tagb == TW_Denormal) {
				if (denormal_operand() < 0)
					return FPU_Exception;
			}

			/* The result is zero. */
			FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
			setsign(dest, sign);
			return TAG_Zero;
		}
		/* We have an exception condition, either 0/0 or Valid/Zero. */
		if (taga == TAG_Zero) {
			/* 0/0 */
			return arith_invalid(deststnr);
		}
		/* Valid/Zero */
		return FPU_divide_by_zero(deststnr, sign);
	}
	/* Must have infinities, NaNs, etc */
	else if ((taga == TW_NaN) || (tagb == TW_NaN)) {
		if (flags & LOADED)
			return real_2op_NaN((FPU_REG *) rm, flags & 0x0f, 0,
					    st0_ptr);

		if (flags & DEST_RM) {
			int tag;
			tag = FPU_gettag0();
			if (tag == TAG_Special)
				tag = FPU_Special(st0_ptr);
			return real_2op_NaN(st0_ptr, tag, rm,
					    (flags & REV) ? st0_ptr : &st(rm));
		} else {
			int tag;
			tag = FPU_gettagi(rm);
			if (tag == TAG_Special)
				tag = FPU_Special(&st(rm));
			return real_2op_NaN(&st(rm), tag, 0,
					    (flags & REV) ? st0_ptr : &st(rm));
		}
	} else if (taga == TW_Infinity) {
		if (tagb == TW_Infinity) {
			/* infinity/infinity */
			return arith_invalid(deststnr);
		} else {
			/* tagb must be Valid or Zero */
			if ((tagb == TW_Denormal) && (denormal_operand() < 0))
				return FPU_Exception;

			/* Infinity divided by Zero or Valid does
			   not raise and exception, but returns Infinity */
			FPU_copy_to_regi(a, TAG_Special, deststnr);
			setsign(dest, sign);
			return taga;
		}
	} else if (tagb == TW_Infinity) {
		if ((taga == TW_Denormal) && (denormal_operand() < 0))
			return FPU_Exception;

		/* The result is zero. */
		FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
		setsign(dest, sign);
		return TAG_Zero;
	}
#ifdef PARANOID
	else {
		EXCEPTION(EX_INTERNAL | 0x102);
		return FPU_Exception;
	}
#endif /* PARANOID */

	return 0;
}
Exemplo n.º 4
0
static int
math_emulate(struct trapframe * tframe)
{

	unsigned char FPU_modrm;
	unsigned short code;
#ifdef LOOKAHEAD_LIMIT
	int lookahead_limit = LOOKAHEAD_LIMIT;
#endif
#ifdef PARANOID
	if (emulating) {
		printf("ERROR: wm-FPU-emu is not RE-ENTRANT!\n");
	}
	REENTRANT_CHECK(ON);
#endif				/* PARANOID */

	if ((((struct pcb *) curproc->p_addr)->pcb_flags & FP_SOFTFP) == 0) {
		finit();
		control_word = __INITIAL_NPXCW__;
		((struct pcb *) curproc->p_addr)->pcb_flags |= FP_SOFTFP;
	}
	FPU_info = tframe;
	FPU_ORIG_EIP = FPU_EIP;	/* --pink-- */

	if (FPU_CS != 0x001f) {
		printf("math_emulate: %x : %x\n", FPU_CS, FPU_EIP);
		panic("FPU emulation in kernel");
	}
#ifdef notyet
	/* We cannot handle emulation in v86-mode */
	if (FPU_EFLAGS & 0x00020000) {
		FPU_ORIG_EIP = FPU_EIP;
		math_abort(FPU_info, SIGILL);
	}
#endif

	FPU_lookahead = FPU_LOOKAHEAD;
	if (curproc->p_flag & P_TRACED)
		FPU_lookahead = 0;

do_another_FPU_instruction:

	REENTRANT_CHECK(OFF);
	code = fuword((u_int *) FPU_EIP);
	REENTRANT_CHECK(ON);
	if ((code & 0xff) == 0x9b) {	/* fwait */
		if (status_word & SW_Summary)
			goto do_the_FPU_interrupt;
		else {
			FPU_EIP++;
			goto FPU_instruction_done;
		}
	}
	if (status_word & SW_Summary) {
		/* Ignore the error for now if the current instruction is a
		 * no-wait control instruction */
		/* The 80486 manual contradicts itself on this topic, so I use
		 * the following list of such instructions until I can check
		 * on a real 80486: fninit, fnstenv, fnsave, fnstsw, fnstenv,
		 * fnclex. */
		if (!((((code & 0xf803) == 0xe003) ||	/* fnclex, fninit,
							 * fnstsw */
			    (((code & 0x3003) == 0x3001) &&	/* fnsave, fnstcw,
								 * fnstenv, fnstsw */
				((code & 0xc000) != 0xc000))))) {
			/* This is a guess about what a real FPU might do to
			 * this bit: */
/*	  status_word &= ~SW_Summary; ****/

			/* We need to simulate the action of the kernel to FPU
			 * interrupts here. Currently, the "real FPU" part of
			 * the kernel (0.99.10) clears the exception flags,
			 * sets the registers to empty, and passes information
			 * back to the interrupted process via the cs selector
			 * and operand selector, so we do the same. */
	do_the_FPU_interrupt:
			cs_selector &= 0xffff0000;
			cs_selector |= (status_word & ~SW_Top) | ((top & 7) << SW_Top_Shift);
			operand_selector = tag_word();
			status_word = 0;
			top = 0;
			{
				int     r;
				for (r = 0; r < 8; r++) {
					regs[r].tag = TW_Empty;
				}
			}
			REENTRANT_CHECK(OFF);
			math_abort(SIGFPE);
		}
	}
	FPU_entry_eip = FPU_ORIG_EIP = FPU_EIP;

	if ((code & 0xff) == 0x66) {	/* size prefix */
		FPU_EIP++;
		REENTRANT_CHECK(OFF);
		code = fuword((u_int *) FPU_EIP);
		REENTRANT_CHECK(ON);
	}
	FPU_EIP += 2;

	FPU_modrm = code >> 8;
	FPU_rm = FPU_modrm & 7;

	if (FPU_modrm < 0300) {
		/* All of these instructions use the mod/rm byte to get a data
		 * address */
		get_address(FPU_modrm);
		if (!(code & 1)) {
			unsigned short status1 = status_word;
			FPU_st0_ptr = &st(0);
			FPU_st0_tag = FPU_st0_ptr->tag;

			/* Stack underflow has priority */
			if (NOT_EMPTY_0) {
				switch ((code >> 1) & 3) {
				case 0:
					reg_load_single();
					break;
				case 1:
					reg_load_int32();
					break;
				case 2:
					reg_load_double();
					break;
				case 3:
					reg_load_int16();
					break;
				}

				/* No more access to user memory, it is safe
				 * to use static data now */
				FPU_st0_ptr = &st(0);
				FPU_st0_tag = FPU_st0_ptr->tag;

				/* NaN operands have the next priority. */
				/* We have to delay looking at st(0) until
				 * after loading the data, because that data
				 * might contain an SNaN */
				if ((FPU_st0_tag == TW_NaN) ||
				    (FPU_loaded_data.tag == TW_NaN)) {
					/* Restore the status word; we might
					 * have loaded a denormal. */
					status_word = status1;
					if ((FPU_modrm & 0x30) == 0x10) {
						/* fcom or fcomp */
						EXCEPTION(EX_Invalid);
						setcc(SW_C3 | SW_C2 | SW_C0);
						if (FPU_modrm & 0x08)
							pop();	/* fcomp, so we pop. */
					} else
						real_2op_NaN(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr);
					goto reg_mem_instr_done;
				}
				switch ((FPU_modrm >> 3) & 7) {
				case 0:	/* fadd */
					reg_add(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
					break;
				case 1:	/* fmul */
					reg_mul(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
					break;
				case 2:	/* fcom */
					compare_st_data();
					break;
				case 3:	/* fcomp */
					compare_st_data();
					pop();
					break;
				case 4:	/* fsub */
					reg_sub(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
					break;
				case 5:	/* fsubr */
					reg_sub(&FPU_loaded_data, FPU_st0_ptr, FPU_st0_ptr, control_word);
					break;
				case 6:	/* fdiv */
					reg_div(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word);
					break;
				case 7:	/* fdivr */
					if (FPU_st0_tag == TW_Zero)
						status_word = status1;	/* Undo any denorm tag,
									 * zero-divide has
									 * priority. */
					reg_div(&FPU_loaded_data, FPU_st0_ptr, FPU_st0_ptr, control_word);
					break;
				}
			} else {
				if ((FPU_modrm & 0x30) == 0x10) {
Exemplo n.º 5
0
int FPU_mul(FPU_REG const *b, u_char tagb, int deststnr, int control_w)
{
	FPU_REG *a = &st(deststnr);
	FPU_REG *dest = a;
	u_char taga = FPU_gettagi(deststnr);
	u_char saved_sign = getsign(dest);
	u_char sign = (getsign(a) ^ getsign(b));
	int tag;

	if (!(taga | tagb)) {
		

		tag =
		    FPU_u_mul(a, b, dest, control_w, sign,
			      exponent(a) + exponent(b));
		if (tag < 0) {
			setsign(dest, saved_sign);
			return tag;
		}
		FPU_settagi(deststnr, tag);
		return tag;
	}

	if (taga == TAG_Special)
		taga = FPU_Special(a);
	if (tagb == TAG_Special)
		tagb = FPU_Special(b);

	if (((taga == TAG_Valid) && (tagb == TW_Denormal))
	    || ((taga == TW_Denormal) && (tagb == TAG_Valid))
	    || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
		FPU_REG x, y;
		if (denormal_operand() < 0)
			return FPU_Exception;

		FPU_to_exp16(a, &x);
		FPU_to_exp16(b, &y);
		tag = FPU_u_mul(&x, &y, dest, control_w, sign,
				exponent16(&x) + exponent16(&y));
		if (tag < 0) {
			setsign(dest, saved_sign);
			return tag;
		}
		FPU_settagi(deststnr, tag);
		return tag;
	} else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) {
		if (((tagb == TW_Denormal) || (taga == TW_Denormal))
		    && (denormal_operand() < 0))
			return FPU_Exception;

		FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
		setsign(dest, sign);
		return TAG_Zero;
	}
	
	else if ((taga == TW_NaN) || (tagb == TW_NaN)) {
		return real_2op_NaN(b, tagb, deststnr, &st(0));
	} else if (((taga == TW_Infinity) && (tagb == TAG_Zero))
		   || ((tagb == TW_Infinity) && (taga == TAG_Zero))) {
		return arith_invalid(deststnr);	
	} else if (((taga == TW_Denormal) || (tagb == TW_Denormal))
		   && (denormal_operand() < 0)) {
		return FPU_Exception;
	} else if (taga == TW_Infinity) {
		FPU_copy_to_regi(a, TAG_Special, deststnr);
		setsign(dest, sign);
		return TAG_Special;
	} else if (tagb == TW_Infinity) {
		FPU_copy_to_regi(b, TAG_Special, deststnr);
		setsign(dest, sign);
		return TAG_Special;
	}
#ifdef PARANOID
	else {
		EXCEPTION(EX_INTERNAL | 0x102);
		return FPU_Exception;
	}
#endif 

	return 0;
}
Exemplo n.º 6
0
void reg_add(FPU_REG *a, FPU_REG *b, FPU_REG *dest, int control_w)
{
    int diff;

    if ( !(a->tag | b->tag) )
    {
        /* Both registers are valid */
        if (!(a->sign ^ b->sign))
        {
            /* signs are the same */
            reg_u_add(a, b, dest, control_w);
            dest->sign = a->sign;
            return;
        }

        /* The signs are different, so do a subtraction */
        diff = a->exp - b->exp;
        if (!diff)
        {
            diff = a->sigh - b->sigh;  /* Works only if ms bits are identical */
            if (!diff)
            {
                diff = a->sigl > b->sigl;
                if (!diff)
                    diff = -(a->sigl < b->sigl);
            }
        }

        if (diff > 0)
        {
            reg_u_sub(a, b, dest, control_w);
            dest->sign = a->sign;
        }
        else if ( diff == 0 )
        {
            reg_move(&CONST_Z, dest);
            /* sign depends upon rounding mode */
            dest->sign = ((control_w & CW_RC) != RC_DOWN)
                         ? SIGN_POS : SIGN_NEG;
        }
        else
        {
            reg_u_sub(b, a, dest, control_w);
            dest->sign = b->sign;
        }
        return;
    }
    else
    {
        if ( (a->tag == TW_NaN) || (b->tag == TW_NaN) )
        {
            real_2op_NaN(a, b, dest);
            return;
        }
        else if (a->tag == TW_Zero)
        {
            if (b->tag == TW_Zero)
            {
                char different_signs = a->sign ^ b->sign;
                /* Both are zero, result will be zero. */
                reg_move(a, dest);
                if (different_signs)
                {
                    /* Signs are different. */
                    /* Sign of answer depends upon rounding mode. */
                    dest->sign = ((control_w & CW_RC) != RC_DOWN)
                                 ? SIGN_POS : SIGN_NEG;
                }
            }
            else
                reg_move(b, dest);
            return;
        }
        else if (b->tag == TW_Zero)
        {
            reg_move(a, dest);
            return;
        }
        else if (a->tag == TW_Infinity)
        {
            if (b->tag != TW_Infinity)
            {
                reg_move(a, dest);
                return;
            }
            /* They are both + or - infinity */
            if (a->sign == b->sign)
            {
                reg_move(a, dest);
                return;
            }
            reg_move(&CONST_QNaN, dest);	/* inf - inf is undefined. */
            return;
        }
        else if (b->tag == TW_Infinity)
        {
            reg_move(b, dest);
            return;
        }
    }
#ifdef PARANOID
    EXCEPTION(EX_INTERNAL|0x101);
#endif
}
Exemplo n.º 7
0
/* Subtract b from a.  (a-b) -> dest */
void reg_sub(FPU_REG *a, FPU_REG *b, FPU_REG *dest, int control_w)
{
    int diff;

    if ( !(a->tag | b->tag) )
    {
        /* Both registers are valid */
        diff = a->exp - b->exp;
        if (!diff)
        {
            diff = a->sigh - b->sigh;  /* Works only if ms bits are identical */
            if (!diff)
            {
                diff = a->sigl > b->sigl;
                if (!diff)
                    diff = -(a->sigl < b->sigl);
            }
        }

        switch (a->sign*2 + b->sign)
        {
        case 0: /* P - P */
        case 3: /* N - N */
            if (diff > 0)
            {
                reg_u_sub(a, b, dest, control_w);
                dest->sign = a->sign;
            }
            else if ( diff == 0 )
            {
                reg_move(&CONST_Z, dest);
                /* sign depends upon rounding mode */
                dest->sign = ((control_w & CW_RC) != RC_DOWN)
                             ? SIGN_POS : SIGN_NEG;
            }
            else
            {
                reg_u_sub(b, a, dest, control_w);
                dest->sign = a->sign ^ SIGN_POS^SIGN_NEG;
            }
            return;
        case 1: /* P - N */
            reg_u_add(a, b, dest, control_w);
            dest->sign = SIGN_POS;
            return;
        case 2: /* N - P */
            reg_u_add(a, b, dest, control_w);
            dest->sign = SIGN_NEG;
            return;
        }
    }
    else
    {
        if ( (a->tag == TW_NaN) || (b->tag == TW_NaN) )
        {
            real_2op_NaN(a, b, dest);
            return;
        }
        else if (b->tag == TW_Zero)
        {
            if (a->tag == TW_Zero)
            {
                char same_signs = !(a->sign ^ b->sign);
                /* Both are zero, result will be zero. */
                reg_move(a, dest); /* Answer for different signs. */
                if (same_signs)
                {
                    /* Sign depends upon rounding mode */
                    dest->sign = ((control_w & CW_RC) != RC_DOWN)
                                 ? SIGN_POS : SIGN_NEG;
                }
            }
            else
                reg_move(a, dest);
            return;
        }
        else if (a->tag == TW_Zero)
        {
            reg_move(b, dest);
            dest->sign ^= SIGN_POS^SIGN_NEG;
            return;
        }
        else if (a->tag == TW_Infinity)
        {
            if (b->tag != TW_Infinity)
            {
                reg_move(a, dest);
                return;
            }
            if (a->sign == b->sign)
            {
                reg_move(&CONST_QNaN, dest);
                return;
            }
            reg_move(a, dest);
            return;
        }
        else if (b->tag == TW_Infinity)
        {
            reg_move(b, dest);
            dest->sign ^= SIGN_POS^SIGN_NEG;
            return;
        }
    }
#ifdef PARANOID
    EXCEPTION(EX_INTERNAL|0x110);
#endif
}
Exemplo n.º 8
0
/*
  Operates on st(0) and st(n), or on st(0) and temporary data.
  The destination must be one of the source st(x).
  */
int FPU_add(FPU_REG const *b, u_char tagb, int deststnr, u16 control_w)
{
  FPU_REG *a = &st(0);
  FPU_REG *dest = &st(deststnr);
  u_char signb = getsign(b);
  u_char taga = FPU_gettag0();
  u_char signa = getsign(a);
  u_char saved_sign = getsign(dest);
  int diff, tag, expa, expb;
  
  if ( !(taga | tagb) )
    {
      expa = exponent(a);
      expb = exponent(b);

    valid_add:
      /* Both registers are valid */
      if (!(signa ^ signb))
	{
	  /* signs are the same */
	  tag = FPU_u_add(a, b, dest, control_w, signa, expa, expb);
	}
      else
	{
	  /* The signs are different, so do a subtraction */
	  diff = expa - expb;
	  if (!diff)
	    {
	      diff = a->sigh - b->sigh;  /* This works only if the ms bits
					    are identical. */
	      if (!diff)
		{
		  diff = a->sigl > b->sigl;
		  if (!diff)
		    diff = -(a->sigl < b->sigl);
		}
	    }
      
	  if (diff > 0)
	    {
	      tag = FPU_u_sub(a, b, dest, control_w, signa, expa, expb);
	    }
	  else if ( diff < 0 )
	    {
	      tag = FPU_u_sub(b, a, dest, control_w, signb, expb, expa);
	    }
	  else
	    {
	      FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
	      /* sign depends upon rounding mode */
	      setsign(dest, ((control_w & CW_RC) != RC_DOWN)
		      ? SIGN_POS : SIGN_NEG);
	      return TAG_Zero;
	    }
	}

      if ( tag < 0 )
	{
	  setsign(dest, saved_sign);
	  return tag;
	}
      FPU_settagi(deststnr, tag);
      return tag;
    }

  if ( taga == TAG_Special )
    taga = FPU_Special(a);
  if ( tagb == TAG_Special )
    tagb = FPU_Special(b);

  if ( ((taga == TAG_Valid) && (tagb == TW_Denormal))
	    || ((taga == TW_Denormal) && (tagb == TAG_Valid))
	    || ((taga == TW_Denormal) && (tagb == TW_Denormal)) )
    {
      FPU_REG x, y;

      if ( denormal_operand() < 0 )
	return FPU_Exception;

      FPU_to_exp16(a, &x);
      FPU_to_exp16(b, &y);
      a = &x;
      b = &y;
      expa = exponent16(a);
      expb = exponent16(b);
      goto valid_add;
    }

  if ( (taga == TW_NaN) || (tagb == TW_NaN) )
    {
      if ( deststnr == 0 )
	return real_2op_NaN(b, tagb, deststnr, a);
      else
	return real_2op_NaN(a, taga, deststnr, a);
    }

  return add_sub_specials(a, taga, signa, b, tagb, signb,
			  dest, deststnr, control_w);
}
Exemplo n.º 9
0
int FPU_div(int flags, int rm, int control_w)
{
	FPU_REG x, y;
	FPU_REG const *a, *b, *st0_ptr, *st_ptr;
	FPU_REG *dest;
	u_char taga, tagb, signa, signb, sign, saved_sign;
	int tag, deststnr;

	if (flags & DEST_RM)
		deststnr = rm;
	else
		deststnr = 0;

	if (flags & REV) {
		b = &st(0);
		st0_ptr = b;
		tagb = FPU_gettag0();
		if (flags & LOADED) {
			a = (FPU_REG *) rm;
			taga = flags & 0x0f;
		} else {
			a = &st(rm);
			st_ptr = a;
			taga = FPU_gettagi(rm);
		}
	} else {
		a = &st(0);
		st0_ptr = a;
		taga = FPU_gettag0();
		if (flags & LOADED) {
			b = (FPU_REG *) rm;
			tagb = flags & 0x0f;
		} else {
			b = &st(rm);
			st_ptr = b;
			tagb = FPU_gettagi(rm);
		}
	}

	signa = getsign(a);
	signb = getsign(b);

	sign = signa ^ signb;

	dest = &st(deststnr);
	saved_sign = getsign(dest);

	if (!(taga | tagb)) {
		
		reg_copy(a, &x);
		reg_copy(b, &y);
		setpositive(&x);
		setpositive(&y);
		tag = FPU_u_div(&x, &y, dest, control_w, sign);

		if (tag < 0)
			return tag;

		FPU_settagi(deststnr, tag);
		return tag;
	}

	if (taga == TAG_Special)
		taga = FPU_Special(a);
	if (tagb == TAG_Special)
		tagb = FPU_Special(b);

	if (((taga == TAG_Valid) && (tagb == TW_Denormal))
	    || ((taga == TW_Denormal) && (tagb == TAG_Valid))
	    || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
		if (denormal_operand() < 0)
			return FPU_Exception;

		FPU_to_exp16(a, &x);
		FPU_to_exp16(b, &y);
		tag = FPU_u_div(&x, &y, dest, control_w, sign);
		if (tag < 0)
			return tag;

		FPU_settagi(deststnr, tag);
		return tag;
	} else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) {
		if (tagb != TAG_Zero) {
			
			if (tagb == TW_Denormal) {
				if (denormal_operand() < 0)
					return FPU_Exception;
			}

			
			FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
			setsign(dest, sign);
			return TAG_Zero;
		}
		
		if (taga == TAG_Zero) {
			
			return arith_invalid(deststnr);
		}
		
		return FPU_divide_by_zero(deststnr, sign);
	}
	
	else if ((taga == TW_NaN) || (tagb == TW_NaN)) {
		if (flags & LOADED)
			return real_2op_NaN((FPU_REG *) rm, flags & 0x0f, 0,
					    st0_ptr);

		if (flags & DEST_RM) {
			int tag;
			tag = FPU_gettag0();
			if (tag == TAG_Special)
				tag = FPU_Special(st0_ptr);
			return real_2op_NaN(st0_ptr, tag, rm,
					    (flags & REV) ? st0_ptr : &st(rm));
		} else {
			int tag;
			tag = FPU_gettagi(rm);
			if (tag == TAG_Special)
				tag = FPU_Special(&st(rm));
			return real_2op_NaN(&st(rm), tag, 0,
					    (flags & REV) ? st0_ptr : &st(rm));
		}
	} else if (taga == TW_Infinity) {
		if (tagb == TW_Infinity) {
			
			return arith_invalid(deststnr);
		} else {
			
			if ((tagb == TW_Denormal) && (denormal_operand() < 0))
				return FPU_Exception;

			
			FPU_copy_to_regi(a, TAG_Special, deststnr);
			setsign(dest, sign);
			return taga;
		}
	} else if (tagb == TW_Infinity) {
		if ((taga == TW_Denormal) && (denormal_operand() < 0))
			return FPU_Exception;

		
		FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
		setsign(dest, sign);
		return TAG_Zero;
	}
#ifdef PARANOID
	else {
		EXCEPTION(EX_INTERNAL | 0x102);
		return FPU_Exception;
	}
#endif 

	return 0;
}