| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*---------------------------------------------------------------------------+ | 
 | 2 |  |  reg_mul.c                                                                | | 
 | 3 |  |                                                                           | | 
 | 4 |  | Multiply one FPU_REG by another, put the result in a destination FPU_REG. | | 
 | 5 |  |                                                                           | | 
 | 6 |  | Copyright (C) 1992,1993,1997                                              | | 
 | 7 |  |                  W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia | | 
 | 8 |  |                  E-mail   billm@suburbia.net                              | | 
 | 9 |  |                                                                           | | 
 | 10 |  | Returns the tag of the result if no exceptions or errors occurred.        | | 
 | 11 |  |                                                                           | | 
 | 12 |  +---------------------------------------------------------------------------*/ | 
 | 13 |  | 
 | 14 | /*---------------------------------------------------------------------------+ | 
 | 15 |  | The destination may be any FPU_REG, including one of the source FPU_REGs. | | 
 | 16 |  +---------------------------------------------------------------------------*/ | 
 | 17 |  | 
 | 18 | #include "fpu_emu.h" | 
 | 19 | #include "exception.h" | 
 | 20 | #include "reg_constant.h" | 
 | 21 | #include "fpu_system.h" | 
 | 22 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /* | 
 | 24 |   Multiply two registers to give a register result. | 
 | 25 |   The sources are st(deststnr) and (b,tagb,signb). | 
 | 26 |   The destination is st(deststnr). | 
 | 27 |   */ | 
 | 28 | /* This routine must be called with non-empty source registers */ | 
 | 29 | int FPU_mul(FPU_REG const *b, u_char tagb, int deststnr, int control_w) | 
 | 30 | { | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 31 | 	FPU_REG *a = &st(deststnr); | 
 | 32 | 	FPU_REG *dest = a; | 
 | 33 | 	u_char taga = FPU_gettagi(deststnr); | 
 | 34 | 	u_char saved_sign = getsign(dest); | 
 | 35 | 	u_char sign = (getsign(a) ^ getsign(b)); | 
 | 36 | 	int tag; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 38 | 	if (!(taga | tagb)) { | 
 | 39 | 		/* Both regs Valid, this should be the most common case. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 41 | 		tag = | 
 | 42 | 		    FPU_u_mul(a, b, dest, control_w, sign, | 
 | 43 | 			      exponent(a) + exponent(b)); | 
 | 44 | 		if (tag < 0) { | 
 | 45 | 			setsign(dest, saved_sign); | 
 | 46 | 			return tag; | 
 | 47 | 		} | 
 | 48 | 		FPU_settagi(deststnr, tag); | 
 | 49 | 		return tag; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 52 | 	if (taga == TAG_Special) | 
 | 53 | 		taga = FPU_Special(a); | 
 | 54 | 	if (tagb == TAG_Special) | 
 | 55 | 		tagb = FPU_Special(b); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 57 | 	if (((taga == TAG_Valid) && (tagb == TW_Denormal)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | 	    || ((taga == TW_Denormal) && (tagb == TAG_Valid)) | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 59 | 	    || ((taga == TW_Denormal) && (tagb == TW_Denormal))) { | 
 | 60 | 		FPU_REG x, y; | 
 | 61 | 		if (denormal_operand() < 0) | 
 | 62 | 			return FPU_Exception; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 64 | 		FPU_to_exp16(a, &x); | 
 | 65 | 		FPU_to_exp16(b, &y); | 
 | 66 | 		tag = FPU_u_mul(&x, &y, dest, control_w, sign, | 
 | 67 | 				exponent16(&x) + exponent16(&y)); | 
 | 68 | 		if (tag < 0) { | 
 | 69 | 			setsign(dest, saved_sign); | 
 | 70 | 			return tag; | 
 | 71 | 		} | 
 | 72 | 		FPU_settagi(deststnr, tag); | 
 | 73 | 		return tag; | 
 | 74 | 	} else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) { | 
 | 75 | 		if (((tagb == TW_Denormal) || (taga == TW_Denormal)) | 
 | 76 | 		    && (denormal_operand() < 0)) | 
 | 77 | 			return FPU_Exception; | 
 | 78 |  | 
 | 79 | 		/* Must have either both arguments == zero, or | 
 | 80 | 		   one valid and the other zero. | 
 | 81 | 		   The result is therefore zero. */ | 
 | 82 | 		FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr); | 
 | 83 | 		/* The 80486 book says that the answer is +0, but a real | 
 | 84 | 		   80486 behaves this way. | 
 | 85 | 		   IEEE-754 apparently says it should be this way. */ | 
 | 86 | 		setsign(dest, sign); | 
 | 87 | 		return TAG_Zero; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | 	} | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 89 | 	/* Must have infinities, NaNs, etc */ | 
 | 90 | 	else if ((taga == TW_NaN) || (tagb == TW_NaN)) { | 
 | 91 | 		return real_2op_NaN(b, tagb, deststnr, &st(0)); | 
 | 92 | 	} else if (((taga == TW_Infinity) && (tagb == TAG_Zero)) | 
 | 93 | 		   || ((tagb == TW_Infinity) && (taga == TAG_Zero))) { | 
 | 94 | 		return arith_invalid(deststnr);	/* Zero*Infinity is invalid */ | 
 | 95 | 	} else if (((taga == TW_Denormal) || (tagb == TW_Denormal)) | 
 | 96 | 		   && (denormal_operand() < 0)) { | 
 | 97 | 		return FPU_Exception; | 
 | 98 | 	} else if (taga == TW_Infinity) { | 
 | 99 | 		FPU_copy_to_regi(a, TAG_Special, deststnr); | 
 | 100 | 		setsign(dest, sign); | 
 | 101 | 		return TAG_Special; | 
 | 102 | 	} else if (tagb == TW_Infinity) { | 
 | 103 | 		FPU_copy_to_regi(b, TAG_Special, deststnr); | 
 | 104 | 		setsign(dest, sign); | 
 | 105 | 		return TAG_Special; | 
 | 106 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #ifdef PARANOID | 
| Ingo Molnar | 3d0d14f | 2008-01-30 13:30:11 +0100 | [diff] [blame] | 108 | 	else { | 
 | 109 | 		EXCEPTION(EX_INTERNAL | 0x102); | 
 | 110 | 		return FPU_Exception; | 
 | 111 | 	} | 
 | 112 | #endif /* PARANOID */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 |  | 
 | 114 | 	return 0; | 
 | 115 | } |