4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "fpu/softfloat.h"
26 #define QT0 (env->qt0)
27 #define QT1 (env->qt1)
29 static target_ulong
do_check_ieee_exceptions(CPUSPARCState
*env
, uintptr_t ra
)
31 target_ulong status
= get_float_exception_flags(&env
->fp_status
);
32 target_ulong fsr
= env
->fsr
;
34 if (unlikely(status
)) {
35 /* Keep exception flags clear for next time. */
36 set_float_exception_flags(0, &env
->fp_status
);
38 /* Copy IEEE 754 flags into FSR */
39 if (status
& float_flag_invalid
) {
42 if (status
& float_flag_overflow
) {
45 if (status
& float_flag_underflow
) {
48 if (status
& float_flag_divbyzero
) {
51 if (status
& float_flag_inexact
) {
55 if ((fsr
& FSR_CEXC_MASK
) & ((fsr
& FSR_TEM_MASK
) >> 23)) {
56 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
58 /* Unmasked exception, generate a trap. Note that while
59 the helper is marked as NO_WG, we can get away with
60 writing to cpu state along the exception path, since
61 TCG generated code will never see the write. */
62 env
->fsr
= fsr
| FSR_FTT_IEEE_EXCP
;
63 cs
->exception_index
= TT_FP_EXCP
;
64 cpu_loop_exit_restore(cs
, ra
);
66 /* Accumulate exceptions */
67 fsr
|= (fsr
& FSR_CEXC_MASK
) << 5;
74 target_ulong
helper_check_ieee_exceptions(CPUSPARCState
*env
)
76 return do_check_ieee_exceptions(env
, GETPC());
79 #define F_HELPER(name, p) void helper_f##name##p(CPUSPARCState *env)
81 #define F_BINOP(name) \
82 float32 helper_f ## name ## s (CPUSPARCState *env, float32 src1, \
85 return float32_ ## name (src1, src2, &env->fp_status); \
87 float64 helper_f ## name ## d (CPUSPARCState * env, float64 src1,\
90 return float64_ ## name (src1, src2, &env->fp_status); \
94 QT0 = float128_ ## name (QT0, QT1, &env->fp_status); \
103 float64
helper_fsmuld(CPUSPARCState
*env
, float32 src1
, float32 src2
)
105 return float64_mul(float32_to_float64(src1
, &env
->fp_status
),
106 float32_to_float64(src2
, &env
->fp_status
),
110 void helper_fdmulq(CPUSPARCState
*env
, float64 src1
, float64 src2
)
112 QT0
= float128_mul(float64_to_float128(src1
, &env
->fp_status
),
113 float64_to_float128(src2
, &env
->fp_status
),
117 float32
helper_fnegs(float32 src
)
119 return float32_chs(src
);
122 #ifdef TARGET_SPARC64
123 float64
helper_fnegd(float64 src
)
125 return float64_chs(src
);
130 QT0
= float128_chs(QT1
);
134 /* Integer to float conversion. */
135 float32
helper_fitos(CPUSPARCState
*env
, int32_t src
)
137 return int32_to_float32(src
, &env
->fp_status
);
140 float64
helper_fitod(CPUSPARCState
*env
, int32_t src
)
142 return int32_to_float64(src
, &env
->fp_status
);
145 void helper_fitoq(CPUSPARCState
*env
, int32_t src
)
147 QT0
= int32_to_float128(src
, &env
->fp_status
);
150 #ifdef TARGET_SPARC64
151 float32
helper_fxtos(CPUSPARCState
*env
, int64_t src
)
153 return int64_to_float32(src
, &env
->fp_status
);
156 float64
helper_fxtod(CPUSPARCState
*env
, int64_t src
)
158 return int64_to_float64(src
, &env
->fp_status
);
161 void helper_fxtoq(CPUSPARCState
*env
, int64_t src
)
163 QT0
= int64_to_float128(src
, &env
->fp_status
);
168 /* floating point conversion */
169 float32
helper_fdtos(CPUSPARCState
*env
, float64 src
)
171 return float64_to_float32(src
, &env
->fp_status
);
174 float64
helper_fstod(CPUSPARCState
*env
, float32 src
)
176 return float32_to_float64(src
, &env
->fp_status
);
179 float32
helper_fqtos(CPUSPARCState
*env
)
181 return float128_to_float32(QT1
, &env
->fp_status
);
184 void helper_fstoq(CPUSPARCState
*env
, float32 src
)
186 QT0
= float32_to_float128(src
, &env
->fp_status
);
189 float64
helper_fqtod(CPUSPARCState
*env
)
191 return float128_to_float64(QT1
, &env
->fp_status
);
194 void helper_fdtoq(CPUSPARCState
*env
, float64 src
)
196 QT0
= float64_to_float128(src
, &env
->fp_status
);
199 /* Float to integer conversion. */
200 int32_t helper_fstoi(CPUSPARCState
*env
, float32 src
)
202 return float32_to_int32_round_to_zero(src
, &env
->fp_status
);
205 int32_t helper_fdtoi(CPUSPARCState
*env
, float64 src
)
207 return float64_to_int32_round_to_zero(src
, &env
->fp_status
);
210 int32_t helper_fqtoi(CPUSPARCState
*env
)
212 return float128_to_int32_round_to_zero(QT1
, &env
->fp_status
);
215 #ifdef TARGET_SPARC64
216 int64_t helper_fstox(CPUSPARCState
*env
, float32 src
)
218 return float32_to_int64_round_to_zero(src
, &env
->fp_status
);
221 int64_t helper_fdtox(CPUSPARCState
*env
, float64 src
)
223 return float64_to_int64_round_to_zero(src
, &env
->fp_status
);
226 int64_t helper_fqtox(CPUSPARCState
*env
)
228 return float128_to_int64_round_to_zero(QT1
, &env
->fp_status
);
232 float32
helper_fabss(float32 src
)
234 return float32_abs(src
);
237 #ifdef TARGET_SPARC64
238 float64
helper_fabsd(float64 src
)
240 return float64_abs(src
);
243 void helper_fabsq(CPUSPARCState
*env
)
245 QT0
= float128_abs(QT1
);
249 float32
helper_fsqrts(CPUSPARCState
*env
, float32 src
)
251 return float32_sqrt(src
, &env
->fp_status
);
254 float64
helper_fsqrtd(CPUSPARCState
*env
, float64 src
)
256 return float64_sqrt(src
, &env
->fp_status
);
259 void helper_fsqrtq(CPUSPARCState
*env
)
261 QT0
= float128_sqrt(QT1
, &env
->fp_status
);
264 #define GEN_FCMP(name, size, reg1, reg2, FS, E) \
265 target_ulong glue(helper_, name) (CPUSPARCState *env) \
270 ret = glue(size, _compare)(reg1, reg2, &env->fp_status); \
272 ret = glue(size, _compare_quiet)(reg1, reg2, \
275 fsr = do_check_ieee_exceptions(env, GETPC()); \
277 case float_relation_unordered: \
278 fsr |= (FSR_FCC1 | FSR_FCC0) << FS; \
281 case float_relation_less: \
282 fsr &= ~(FSR_FCC1) << FS; \
283 fsr |= FSR_FCC0 << FS; \
285 case float_relation_greater: \
286 fsr &= ~(FSR_FCC0) << FS; \
287 fsr |= FSR_FCC1 << FS; \
290 fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
295 #define GEN_FCMP_T(name, size, FS, E) \
296 target_ulong glue(helper_, name)(CPUSPARCState *env, size src1, size src2)\
301 ret = glue(size, _compare)(src1, src2, &env->fp_status); \
303 ret = glue(size, _compare_quiet)(src1, src2, \
306 fsr = do_check_ieee_exceptions(env, GETPC()); \
308 case float_relation_unordered: \
309 fsr |= (FSR_FCC1 | FSR_FCC0) << FS; \
311 case float_relation_less: \
312 fsr &= ~(FSR_FCC1 << FS); \
313 fsr |= FSR_FCC0 << FS; \
315 case float_relation_greater: \
316 fsr &= ~(FSR_FCC0 << FS); \
317 fsr |= FSR_FCC1 << FS; \
320 fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
326 GEN_FCMP_T(fcmps
, float32
, 0, 0);
327 GEN_FCMP_T(fcmpd
, float64
, 0, 0);
329 GEN_FCMP_T(fcmpes
, float32
, 0, 1);
330 GEN_FCMP_T(fcmped
, float64
, 0, 1);
332 GEN_FCMP(fcmpq
, float128
, QT0
, QT1
, 0, 0);
333 GEN_FCMP(fcmpeq
, float128
, QT0
, QT1
, 0, 1);
335 #ifdef TARGET_SPARC64
336 GEN_FCMP_T(fcmps_fcc1
, float32
, 22, 0);
337 GEN_FCMP_T(fcmpd_fcc1
, float64
, 22, 0);
338 GEN_FCMP(fcmpq_fcc1
, float128
, QT0
, QT1
, 22, 0);
340 GEN_FCMP_T(fcmps_fcc2
, float32
, 24, 0);
341 GEN_FCMP_T(fcmpd_fcc2
, float64
, 24, 0);
342 GEN_FCMP(fcmpq_fcc2
, float128
, QT0
, QT1
, 24, 0);
344 GEN_FCMP_T(fcmps_fcc3
, float32
, 26, 0);
345 GEN_FCMP_T(fcmpd_fcc3
, float64
, 26, 0);
346 GEN_FCMP(fcmpq_fcc3
, float128
, QT0
, QT1
, 26, 0);
348 GEN_FCMP_T(fcmpes_fcc1
, float32
, 22, 1);
349 GEN_FCMP_T(fcmped_fcc1
, float64
, 22, 1);
350 GEN_FCMP(fcmpeq_fcc1
, float128
, QT0
, QT1
, 22, 1);
352 GEN_FCMP_T(fcmpes_fcc2
, float32
, 24, 1);
353 GEN_FCMP_T(fcmped_fcc2
, float64
, 24, 1);
354 GEN_FCMP(fcmpeq_fcc2
, float128
, QT0
, QT1
, 24, 1);
356 GEN_FCMP_T(fcmpes_fcc3
, float32
, 26, 1);
357 GEN_FCMP_T(fcmped_fcc3
, float64
, 26, 1);
358 GEN_FCMP(fcmpeq_fcc3
, float128
, QT0
, QT1
, 26, 1);
363 static void set_fsr(CPUSPARCState
*env
, target_ulong fsr
)
367 switch (fsr
& FSR_RD_MASK
) {
369 rnd_mode
= float_round_nearest_even
;
373 rnd_mode
= float_round_to_zero
;
376 rnd_mode
= float_round_up
;
379 rnd_mode
= float_round_down
;
382 set_float_rounding_mode(rnd_mode
, &env
->fp_status
);
385 target_ulong
helper_ldfsr(CPUSPARCState
*env
, target_ulong old_fsr
,
388 old_fsr
= (new_fsr
& FSR_LDFSR_MASK
) | (old_fsr
& FSR_LDFSR_OLDMASK
);
389 set_fsr(env
, old_fsr
);
393 #ifdef TARGET_SPARC64
394 target_ulong
helper_ldxfsr(CPUSPARCState
*env
, target_ulong old_fsr
,
397 old_fsr
= (new_fsr
& FSR_LDXFSR_MASK
) | (old_fsr
& FSR_LDXFSR_OLDMASK
);
398 set_fsr(env
, old_fsr
);