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