xref: /openbmc/linux/arch/arm/nwfpe/fpa11_cpdo.c (revision 87c2ce3b)
1 /*
2     NetWinder Floating Point Emulator
3     (c) Rebel.COM, 1998,1999
4     (c) Philip Blundell, 2001
5 
6     Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 
23 #include <linux/config.h>
24 #include "fpa11.h"
25 #include "fpopcode.h"
26 
27 unsigned int SingleCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd);
28 unsigned int DoubleCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd);
29 unsigned int ExtendedCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd);
30 
31 unsigned int EmulateCPDO(const unsigned int opcode)
32 {
33 	FPA11 *fpa11 = GET_FPA11();
34 	FPREG *rFd;
35 	unsigned int nType, nDest, nRc;
36 	struct roundingData roundData;
37 
38 	/* Get the destination size.  If not valid let Linux perform
39 	   an invalid instruction trap. */
40 	nDest = getDestinationSize(opcode);
41 	if (typeNone == nDest)
42 		return 0;
43 
44 	roundData.mode = SetRoundingMode(opcode);
45 	roundData.precision = SetRoundingPrecision(opcode);
46 	roundData.exception = 0;
47 
48 	/* Compare the size of the operands in Fn and Fm.
49 	   Choose the largest size and perform operations in that size,
50 	   in order to make use of all the precision of the operands.
51 	   If Fm is a constant, we just grab a constant of a size
52 	   matching the size of the operand in Fn. */
53 	if (MONADIC_INSTRUCTION(opcode))
54 		nType = nDest;
55 	else
56 		nType = fpa11->fType[getFn(opcode)];
57 
58 	if (!CONSTANT_FM(opcode)) {
59 		register unsigned int Fm = getFm(opcode);
60 		if (nType < fpa11->fType[Fm]) {
61 			nType = fpa11->fType[Fm];
62 		}
63 	}
64 
65 	rFd = &fpa11->fpreg[getFd(opcode)];
66 
67 	switch (nType) {
68 	case typeSingle:
69 		nRc = SingleCPDO(&roundData, opcode, rFd);
70 		break;
71 	case typeDouble:
72 		nRc = DoubleCPDO(&roundData, opcode, rFd);
73 		break;
74 #ifdef CONFIG_FPE_NWFPE_XP
75 	case typeExtended:
76 		nRc = ExtendedCPDO(&roundData, opcode, rFd);
77 		break;
78 #endif
79 	default:
80 		nRc = 0;
81 	}
82 
83 	/* The CPDO functions used to always set the destination type
84 	   to be the same as their working size. */
85 
86 	if (nRc != 0) {
87 		/* If the operation succeeded, check to see if the result in the
88 		   destination register is the correct size.  If not force it
89 		   to be. */
90 
91 		fpa11->fType[getFd(opcode)] = nDest;
92 
93 #ifdef CONFIG_FPE_NWFPE_XP
94 		if (nDest != nType) {
95 			switch (nDest) {
96 			case typeSingle:
97 				{
98 					if (typeDouble == nType)
99 						rFd->fSingle = float64_to_float32(&roundData, rFd->fDouble);
100 					else
101 						rFd->fSingle = floatx80_to_float32(&roundData, rFd->fExtended);
102 				}
103 				break;
104 
105 			case typeDouble:
106 				{
107 					if (typeSingle == nType)
108 						rFd->fDouble = float32_to_float64(rFd->fSingle);
109 					else
110 						rFd->fDouble = floatx80_to_float64(&roundData, rFd->fExtended);
111 				}
112 				break;
113 
114 			case typeExtended:
115 				{
116 					if (typeSingle == nType)
117 						rFd->fExtended = float32_to_floatx80(rFd->fSingle);
118 					else
119 						rFd->fExtended = float64_to_floatx80(rFd->fDouble);
120 				}
121 				break;
122 			}
123 		}
124 #else
125 		if (nDest != nType) {
126 			if (nDest == typeSingle)
127 				rFd->fSingle = float64_to_float32(&roundData, rFd->fDouble);
128 			else
129 				rFd->fDouble = float32_to_float64(rFd->fSingle);
130 		}
131 #endif
132 	}
133 
134 	if (roundData.exception)
135 		float_raise(roundData.exception);
136 
137 	return nRc;
138 }
139