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