2 NetWinder Floating Point Emulator
3 (c) Rebel.com, 1998-1999
4 (c) Philip Blundell, 1999
6 Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
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.
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.
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.
24 #include "softfloat.h"
29 #include "fpmodule.inl"
31 extern flag
floatx80_is_nan(floatx80
);
32 extern flag
float64_is_nan( float64
);
33 extern flag
float32_is_nan( float32
);
35 void SetRoundingMode(const unsigned int opcode
);
37 unsigned int PerformFLT(const unsigned int opcode
);
38 unsigned int PerformFIX(const unsigned int opcode
);
41 PerformComparison(const unsigned int opcode
);
43 unsigned int EmulateCPRT(const unsigned int opcode
)
47 //printk("EmulateCPRT(0x%08x)\n",opcode);
49 if (opcode
& 0x800000)
51 /* This is some variant of a comparison (PerformComparison will
52 sort out which one). Since most of the other CPRT
53 instructions are oddball cases of some sort or other it makes
54 sense to pull this out into a fast path. */
55 return PerformComparison(opcode
);
58 /* Hint to GCC that we'd like a jump table rather than a load of CMPs */
59 switch ((opcode
& 0x700000) >> 20)
61 case FLT_CODE
>> 20: nRc
= PerformFLT(opcode
); break;
62 case FIX_CODE
>> 20: nRc
= PerformFIX(opcode
); break;
64 case WFS_CODE
>> 20: writeFPSR(readRegister(getRd(opcode
))); break;
65 case RFS_CODE
>> 20: writeRegister(getRd(opcode
),readFPSR()); break;
67 #if 0 /* We currently have no use for the FPCR, so there's no point
69 case WFC_CODE
>> 20: writeFPCR(readRegister(getRd(opcode
)));
70 case RFC_CODE
>> 20: writeRegister(getRd(opcode
),readFPCR()); break;
79 unsigned int PerformFLT(const unsigned int opcode
)
82 SetRoundingMode(opcode
);
83 SetRoundingPrecision(opcode
);
85 switch (opcode
& MASK_ROUNDING_PRECISION
)
89 fpa11
->fType
[getFn(opcode
)] = typeSingle
;
90 fpa11
->fpreg
[getFn(opcode
)].fSingle
=
91 int32_to_float32(readRegister(getRd(opcode
)));
97 fpa11
->fType
[getFn(opcode
)] = typeDouble
;
98 fpa11
->fpreg
[getFn(opcode
)].fDouble
=
99 int32_to_float64(readRegister(getRd(opcode
)));
105 fpa11
->fType
[getFn(opcode
)] = typeExtended
;
106 fpa11
->fpreg
[getFn(opcode
)].fExtended
=
107 int32_to_floatx80(readRegister(getRd(opcode
)));
117 unsigned int PerformFIX(const unsigned int opcode
)
119 unsigned int nRc
= 1;
120 unsigned int Fn
= getFm(opcode
);
122 SetRoundingMode(opcode
);
124 switch (fpa11
->fType
[Fn
])
128 writeRegister(getRd(opcode
),
129 float32_to_int32(fpa11
->fpreg
[Fn
].fSingle
));
135 writeRegister(getRd(opcode
),
136 float64_to_int32(fpa11
->fpreg
[Fn
].fDouble
));
142 writeRegister(getRd(opcode
),
143 floatx80_to_int32(fpa11
->fpreg
[Fn
].fExtended
));
154 static unsigned int __inline__
155 PerformComparisonOperation(floatx80 Fn
, floatx80 Fm
)
157 unsigned int flags
= 0;
159 /* test for less than condition */
160 if (floatx80_lt(Fn
,Fm
))
162 flags
|= CC_NEGATIVE
;
165 /* test for equal condition */
166 if (floatx80_eq(Fn
,Fm
))
171 /* test for greater than or equal condition */
172 if (floatx80_lt(Fm
,Fn
))
177 writeConditionCodes(flags
);
181 /* This instruction sets the flags N, Z, C, V in the FPSR. */
183 static unsigned int PerformComparison(const unsigned int opcode
)
187 int e_flag
= opcode
& 0x400000; /* 1 if CxFE */
188 int n_flag
= opcode
& 0x200000; /* 1 if CNxx */
189 unsigned int flags
= 0;
191 //printk("PerformComparison(0x%08x)\n",opcode);
196 /* Check for unordered condition and convert all operands to 80-bit
198 ?? Might be some mileage in avoiding this conversion if possible.
199 Eg, if both operands are 32-bit, detect this and do a 32-bit
200 comparison (cheaper than an 80-bit one). */
201 switch (fpa11
->fType
[Fn
])
204 //printk("single.\n");
205 if (float32_is_nan(fpa11
->fpreg
[Fn
].fSingle
))
207 rFn
= float32_to_floatx80(fpa11
->fpreg
[Fn
].fSingle
);
211 //printk("double.\n");
212 if (float64_is_nan(fpa11
->fpreg
[Fn
].fDouble
))
214 rFn
= float64_to_floatx80(fpa11
->fpreg
[Fn
].fDouble
);
218 //printk("extended.\n");
219 if (floatx80_is_nan(fpa11
->fpreg
[Fn
].fExtended
))
221 rFn
= fpa11
->fpreg
[Fn
].fExtended
;
227 if (CONSTANT_FM(opcode
))
229 //printk("Fm is a constant: #%d.\n",Fm);
230 rFm
= getExtendedConstant(Fm
);
231 if (floatx80_is_nan(rFm
))
236 //printk("Fm = r%d which contains a ",Fm);
237 switch (fpa11
->fType
[Fm
])
240 //printk("single.\n");
241 if (float32_is_nan(fpa11
->fpreg
[Fm
].fSingle
))
243 rFm
= float32_to_floatx80(fpa11
->fpreg
[Fm
].fSingle
);
247 //printk("double.\n");
248 if (float64_is_nan(fpa11
->fpreg
[Fm
].fDouble
))
250 rFm
= float64_to_floatx80(fpa11
->fpreg
[Fm
].fDouble
);
254 //printk("extended.\n");
255 if (floatx80_is_nan(fpa11
->fpreg
[Fm
].fExtended
))
257 rFm
= fpa11
->fpreg
[Fm
].fExtended
;
269 return PerformComparisonOperation(rFn
,rFm
);
272 /* ?? The FPA data sheet is pretty vague about this, in particular
273 about whether the non-E comparisons can ever raise exceptions.
274 This implementation is based on a combination of what it says in
275 the data sheet, observation of how the Acorn emulator actually
276 behaves (and how programs expect it to) and guesswork. */
277 flags
|= CC_OVERFLOW
;
278 flags
&= ~(CC_ZERO
| CC_NEGATIVE
);
280 if (BIT_AC
& readFPSR()) flags
|= CC_CARRY
;
282 if (e_flag
) float_raise(float_flag_invalid
);
284 writeConditionCodes(flags
);