xref: /openbmc/linux/arch/arm/nwfpe/fpa11_cpdo.c (revision 1da177e4)
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(const unsigned int opcode, FPREG * rFd);
28 unsigned int DoubleCPDO(const unsigned int opcode, FPREG * rFd);
29 unsigned int ExtendedCPDO(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 
37 	/* Get the destination size.  If not valid let Linux perform
38 	   an invalid instruction trap. */
39 	nDest = getDestinationSize(opcode);
40 	if (typeNone == nDest)
41 		return 0;
42 
43 	SetRoundingMode(opcode);
44 
45 	/* Compare the size of the operands in Fn and Fm.
46 	   Choose the largest size and perform operations in that size,
47 	   in order to make use of all the precision of the operands.
48 	   If Fm is a constant, we just grab a constant of a size
49 	   matching the size of the operand in Fn. */
50 	if (MONADIC_INSTRUCTION(opcode))
51 		nType = nDest;
52 	else
53 		nType = fpa11->fType[getFn(opcode)];
54 
55 	if (!CONSTANT_FM(opcode)) {
56 		register unsigned int Fm = getFm(opcode);
57 		if (nType < fpa11->fType[Fm]) {
58 			nType = fpa11->fType[Fm];
59 		}
60 	}
61 
62 	rFd = &fpa11->fpreg[getFd(opcode)];
63 
64 	switch (nType) {
65 	case typeSingle:
66 		nRc = SingleCPDO(opcode, rFd);
67 		break;
68 	case typeDouble:
69 		nRc = DoubleCPDO(opcode, rFd);
70 		break;
71 #ifdef CONFIG_FPE_NWFPE_XP
72 	case typeExtended:
73 		nRc = ExtendedCPDO(opcode, rFd);
74 		break;
75 #endif
76 	default:
77 		nRc = 0;
78 	}
79 
80 	/* The CPDO functions used to always set the destination type
81 	   to be the same as their working size. */
82 
83 	if (nRc != 0) {
84 		/* If the operation succeeded, check to see if the result in the
85 		   destination register is the correct size.  If not force it
86 		   to be. */
87 
88 		fpa11->fType[getFd(opcode)] = nDest;
89 
90 #ifdef CONFIG_FPE_NWFPE_XP
91 		if (nDest != nType) {
92 			switch (nDest) {
93 			case typeSingle:
94 				{
95 					if (typeDouble == nType)
96 						rFd->fSingle = float64_to_float32(rFd->fDouble);
97 					else
98 						rFd->fSingle = floatx80_to_float32(rFd->fExtended);
99 				}
100 				break;
101 
102 			case typeDouble:
103 				{
104 					if (typeSingle == nType)
105 						rFd->fDouble = float32_to_float64(rFd->fSingle);
106 					else
107 						rFd->fDouble = floatx80_to_float64(rFd->fExtended);
108 				}
109 				break;
110 
111 			case typeExtended:
112 				{
113 					if (typeSingle == nType)
114 						rFd->fExtended = float32_to_floatx80(rFd->fSingle);
115 					else
116 						rFd->fExtended = float64_to_floatx80(rFd->fDouble);
117 				}
118 				break;
119 			}
120 		}
121 #else
122 		if (nDest != nType) {
123 			if (nDest == typeSingle)
124 				rFd->fSingle = float64_to_float32(rFd->fDouble);
125 			else
126 				rFd->fDouble = float32_to_float64(rFd->fSingle);
127 		}
128 #endif
129 	}
130 
131 	return nRc;
132 }
133