Example #1
0
void
fmulp_()
{
	/* fmulp st(i),st */
	reg_mul(&st(FPU_rm), FPU_st0_ptr, &st(FPU_rm), control_word);
	pop();
}
Example #2
0
File: fpu_trig.c Project: kame/kame
static void
f2xm1(void)
{
    switch (FPU_st0_tag) {
    case TW_Valid:
    {
        FPU_REG rv, tmp;

#ifdef DENORM_OPERAND
        if ((FPU_st0_ptr->exp <= EXP_UNDER) && (denormal_operand()))
            return;
#endif				/* DENORM_OPERAND */

        if (FPU_st0_ptr->sign == SIGN_POS) {
            /* poly_2xm1(x) requires 0 < x < 1. */
            if (poly_2xm1(FPU_st0_ptr, &rv))
                return;	/* error */
            reg_mul(&rv, FPU_st0_ptr, FPU_st0_ptr, FULL_PRECISION);
        } else {
            /* **** Should change poly_2xm1() to at least handle numbers near 0 */
            /* poly_2xm1(x) doesn't handle negative
             * numbers. */
            /* So we compute (poly_2xm1(x+1)-1)/2, for -1
             * < x < 0 */
            reg_add(FPU_st0_ptr, &CONST_1, &tmp, FULL_PRECISION);
            poly_2xm1(&tmp, &rv);
            reg_mul(&rv, &tmp, &tmp, FULL_PRECISION);
            reg_sub(&tmp, &CONST_1, FPU_st0_ptr, FULL_PRECISION);
            FPU_st0_ptr->exp--;
            if (FPU_st0_ptr->exp <= EXP_UNDER)
                arith_underflow(FPU_st0_ptr);
        }
        return;
    }
    case TW_Zero:
        return;
    case TW_Infinity:
        if (FPU_st0_ptr->sign == SIGN_NEG) {
            /* -infinity gives -1 (p16-10) */
            reg_move(&CONST_1, FPU_st0_ptr);
            FPU_st0_ptr->sign = SIGN_NEG;
        }
        return;
    default:
        single_arg_error();
    }
}
Example #3
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) {
Example #4
0
/*--- poly_sine() -----------------------------------------------------------+
 |                                                                           |
 +---------------------------------------------------------------------------*/
void	poly_sine(FPU_REG const *arg, FPU_REG *result)
{
  short	exponent;
  FPU_REG	fixed_arg, arg_sqrd, arg_to_4, accum, negaccum;
  
  
  exponent = arg->exp - EXP_BIAS;
  
  if ( arg->tag == TW_Zero )
    {
      /* Return 0.0 */
      reg_move(&CONST_Z, result);
      return;
    }
  
#ifdef PARANOID
  if ( arg->sign != 0 )	/* Can't hack a number < 0.0 */
    {
      EXCEPTION(EX_Invalid);
      reg_move(&CONST_QNaN, result);
      return;
    }
  
  if ( exponent >= 0 )	/* Can't hack a number > 1.0 */
    {
      if ( (exponent == 0) && (arg->sigl == 0) && (arg->sigh == 0x80000000) )
	{
	  reg_move(&CONST_1, result);
	  return;
	}
      EXCEPTION(EX_Invalid);
      reg_move(&CONST_QNaN, result);
      return;
    }
#endif PARANOID
  
  fixed_arg.sigl = arg->sigl;
  fixed_arg.sigh = arg->sigh;
  if ( exponent < -1 )
    {
      /* shift the argument right by the required places */
      if ( shrx(&(fixed_arg.sigl), -1-exponent) >= 0x80000000U )
	significand(&fixed_arg)++;	/* round up */
    }
  
  mul64(&significand(&fixed_arg), &significand(&fixed_arg),
	&significand(&arg_sqrd));
  mul64(&significand(&arg_sqrd), &significand(&arg_sqrd),
	&significand(&arg_to_4));
  
  /* will be a valid positive nr with expon = 0 */
  *(short *)&(accum.sign) = 0;
  accum.exp = 0;

  /* Do the basic fixed point polynomial evaluation */
  polynomial(&(accum.sigl), &(arg_to_4.sigl), lterms, HIPOWER-1);
  
  /* will be a valid positive nr with expon = 0 */
  *(short *)&(negaccum.sign) = 0;
  negaccum.exp = 0;
  
  /* Do the basic fixed point polynomial evaluation */
  polynomial(&(negaccum.sigl), &(arg_to_4.sigl), negterms, HIPOWER-1);
  mul64(&significand(&arg_sqrd), &significand(&negaccum),
	&significand(&negaccum));

  /* Subtract the mantissas */
  significand(&accum) -= significand(&negaccum);
  
  /* Convert to 64 bit signed-compatible */
  accum.exp = EXP_BIAS - 1 + accum.exp;

  reg_move(&accum, result);

  normalize(result);

  reg_mul(result, arg, result, FULL_PRECISION);
  reg_u_add(result, arg, result, FULL_PRECISION);
  
  if ( result->exp >= EXP_BIAS )
    {
      /* A small overflow may be possible... but an illegal result. */
      if (    (result->exp > EXP_BIAS) /* Larger or equal 2.0 */
	  || (result->sigl > 1)	  /* Larger than 1.0+msb */
	  ||	(result->sigh != 0x80000000) /* Much > 1.0 */
	  )
	{
#ifdef DEBUGGING
	  RE_ENTRANT_CHECK_OFF;
	  printk("\nEXP=%d, MS=%08x, LS=%08x\n", result->exp,
		 result->sigh, result->sigl);
	  RE_ENTRANT_CHECK_ON;
#endif DEBUGGING
	  EXCEPTION(EX_INTERNAL|0x103);
	}
      
#ifdef DEBUGGING
      RE_ENTRANT_CHECK_OFF;
      printk("\n***CORRECTING ILLEGAL RESULT*** in poly_sin() computation\n");
      printk("EXP=%d, MS=%08x, LS=%08x\n", result->exp,
	     result->sigh, result->sigl);
      RE_ENTRANT_CHECK_ON;
#endif DEBUGGING

      result->sigl = 0;	/* Truncate the result to 1.00 */
    }

}
Example #5
0
void
fmul__()
{
	/* fmul st,st(i) */
	reg_mul(FPU_st0_ptr, &st(FPU_rm), FPU_st0_ptr, control_word);
}
Example #6
0
void
fmul_i()
{
	/* fmul st(i),st */
	reg_mul(&st(FPU_rm), FPU_st0_ptr, &st(FPU_rm), control_word);
}
Example #7
0
/*--- poly_tan() ------------------------------------------------------------+
 |                                                                           |
 +---------------------------------------------------------------------------*/
void
poly_tan(FPU_REG * arg, FPU_REG * y_reg)
{
	char    invert = 0;
	short   exponent;
	FPU_REG odd_poly, even_poly, pos_poly, neg_poly;
	FPU_REG argSq;
	long long arg_signif, argSqSq;


	exponent = arg->exp - EXP_BIAS;

	if (arg->tag == TW_Zero) {
		/* Return 0.0 */
		reg_move(&CONST_Z, y_reg);
		return;
	}
	if (exponent >= -1) {
		/* argument is in the range  [0.5 .. 1.0] */
		if (exponent >= 0) {
#ifdef PARANOID
			if ((exponent == 0) &&
			    (arg->sigl == 0) && (arg->sigh == 0x80000000))
#endif				/* PARANOID */
			{
				arith_overflow(y_reg);
				return;
			}
#ifdef PARANOID
			EXCEPTION(EX_INTERNAL | 0x104);	/* There must be a logic
							 * error */
			return;
#endif				/* PARANOID */
		}
		/* The argument is in the range  [0.5 .. 1.0) */
		/* Convert the argument to a number in the range  (0.0 .. 0.5] */
		*((long long *) (&arg->sigl)) = -*((long long *) (&arg->sigl));
		normalize(arg);	/* Needed later */
		exponent = arg->exp - EXP_BIAS;
		invert = 1;
	}
#ifdef PARANOID
	if (arg->sign != 0) {	/* Can't hack a number < 0.0 */
		arith_invalid(y_reg);
		return;
	}			/* Need a positive number */
#endif				/* PARANOID */

	*(long long *) &arg_signif = *(long long *) &(arg->sigl);
	if (exponent < -1) {
		/* shift the argument right by the required places */
		if (shrx(&arg_signif, -1 - exponent) >= (unsigned)0x80000000)
			arg_signif++;	/* round up */
	}
	mul64(&arg_signif, &arg_signif, (long long *) (&argSq.sigl));
	mul64((long long *) (&argSq.sigl), (long long *) (&argSq.sigl), &argSqSq);

	/* will be a valid positive nr with expon = 0 */
	*(short *) &(pos_poly.sign) = 0;
	pos_poly.exp = EXP_BIAS;

	/* Do the basic fixed point polynomial evaluation */
	polynomial((u_int *) &pos_poly.sigl, (unsigned *) &argSqSq, oddplterms, HIPOWERop - 1);

	/* will be a valid positive nr with expon = 0 */
	*(short *) &(neg_poly.sign) = 0;
	neg_poly.exp = EXP_BIAS;

	/* Do the basic fixed point polynomial evaluation */
	polynomial((u_int *) &neg_poly.sigl, (unsigned *) &argSqSq, oddnegterms, HIPOWERon - 1);
	mul64((long long *) (&argSq.sigl), (long long *) (&neg_poly.sigl),
	    (long long *) (&neg_poly.sigl));

	/* Subtract the mantissas */
	*((long long *) (&pos_poly.sigl)) -= *((long long *) (&neg_poly.sigl));

	/* Convert to 64 bit signed-compatible */
	pos_poly.exp -= 1;

	reg_move(&pos_poly, &odd_poly);
	normalize(&odd_poly);

	reg_mul(&odd_poly, arg, &odd_poly, FULL_PRECISION);
	reg_u_add(&odd_poly, arg, &odd_poly, FULL_PRECISION);	/* This is just the odd
								 * polynomial */


	/* will be a valid positive nr with expon = 0 */
	*(short *) &(pos_poly.sign) = 0;
	pos_poly.exp = EXP_BIAS;

	/* Do the basic fixed point polynomial evaluation */
	polynomial((u_int *) &pos_poly.sigl, (unsigned *) &argSqSq, evenplterms, HIPOWERep - 1);
	mul64((long long *) (&argSq.sigl),
	    (long long *) (&pos_poly.sigl), (long long *) (&pos_poly.sigl));

	/* will be a valid positive nr with expon = 0 */
	*(short *) &(neg_poly.sign) = 0;
	neg_poly.exp = EXP_BIAS;

	/* Do the basic fixed point polynomial evaluation */
	polynomial((u_int *) &neg_poly.sigl, (unsigned *) &argSqSq, evennegterms, HIPOWERen - 1);

	/* Subtract the mantissas */
	*((long long *) (&neg_poly.sigl)) -= *((long long *) (&pos_poly.sigl));
	/* and multiply by argSq */

	/* Convert argSq to a valid reg number */
	*(short *) &(argSq.sign) = 0;
	argSq.exp = EXP_BIAS - 1;
	normalize(&argSq);

	/* Convert to 64 bit signed-compatible */
	neg_poly.exp -= 1;

	reg_move(&neg_poly, &even_poly);
	normalize(&even_poly);

	reg_mul(&even_poly, &argSq, &even_poly, FULL_PRECISION);
	reg_add(&even_poly, &argSq, &even_poly, FULL_PRECISION);
	reg_sub(&CONST_1, &even_poly, &even_poly, FULL_PRECISION);	/* This is just the even
									 * polynomial */

	/* Now ready to copy the results */
	if (invert) {
		reg_div(&even_poly, &odd_poly, y_reg, FULL_PRECISION);
	} else {
		reg_div(&odd_poly, &even_poly, y_reg, FULL_PRECISION);
	}

}