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