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"
25 #define QT0 (env->qt0)
26 #define QT1 (env->qt1)
28 static target_ulong
do_check_ieee_exceptions(CPUSPARCState
*env
, uintptr_t ra
)
30 target_ulong status
= get_float_exception_flags(&env
->fp_status
);
31 target_ulong fsr
= env
->fsr
;
33 if (unlikely(status
)) {
34 /* Keep exception flags clear for next time. */
35 set_float_exception_flags(0, &env
->fp_status
);
37 /* Copy IEEE 754 flags into FSR */
38 if (status
& float_flag_invalid
) {
41 if (status
& float_flag_overflow
) {
44 if (status
& float_flag_underflow
) {
47 if (status
& float_flag_divbyzero
) {
50 if (status
& float_flag_inexact
) {
54 if ((fsr
& FSR_CEXC_MASK
) & ((fsr
& FSR_TEM_MASK
) >> 23)) {
55 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
57 /* Unmasked exception, generate a trap. Note that while
58 the helper is marked as NO_WG, we can get away with
59 writing to cpu state along the exception path, since
60 TCG generated code will never see the write. */
61 env
->fsr
= fsr
| FSR_FTT_IEEE_EXCP
;
62 cs
->exception_index
= TT_FP_EXCP
;
63 cpu_loop_exit_restore(cs
, ra
);
65 /* Accumulate exceptions */
66 fsr
|= (fsr
& FSR_CEXC_MASK
) << 5;
73 target_ulong
helper_check_ieee_exceptions(CPUSPARCState
*env
)
75 return do_check_ieee_exceptions(env
, GETPC());
78 #define F_HELPER(name, p) void helper_f##name##p(CPUSPARCState *env)
80 #define F_BINOP(name) \
81 float32 helper_f ## name ## s (CPUSPARCState *env, float32 src1, \
84 return float32_ ## name (src1, src2, &env->fp_status); \
86 float64 helper_f ## name ## d (CPUSPARCState * env, float64 src1,\
89 return float64_ ## name (src1, src2, &env->fp_status); \
93 QT0 = float128_ ## name (QT0, QT1, &env->fp_status); \
102 float64
helper_fsmuld(CPUSPARCState
*env
, float32 src1
, float32 src2
)
104 return float64_mul(float32_to_float64(src1
, &env
->fp_status
),
105 float32_to_float64(src2
, &env
->fp_status
),
109 void helper_fdmulq(CPUSPARCState
*env
, float64 src1
, float64 src2
)
111 QT0
= float128_mul(float64_to_float128(src1
, &env
->fp_status
),
112 float64_to_float128(src2
, &env
->fp_status
),
116 float32
helper_fnegs(float32 src
)
118 return float32_chs(src
);
121 #ifdef TARGET_SPARC64
122 float64
helper_fnegd(float64 src
)
124 return float64_chs(src
);
129 QT0
= float128_chs(QT1
);
133 /* Integer to float conversion. */
134 float32
helper_fitos(CPUSPARCState
*env
, int32_t src
)
136 return int32_to_float32(src
, &env
->fp_status
);
139 float64
helper_fitod(CPUSPARCState
*env
, int32_t src
)
141 return int32_to_float64(src
, &env
->fp_status
);
144 void helper_fitoq(CPUSPARCState
*env
, int32_t src
)
146 QT0
= int32_to_float128(src
, &env
->fp_status
);
149 #ifdef TARGET_SPARC64
150 float32
helper_fxtos(CPUSPARCState
*env
, int64_t src
)
152 return int64_to_float32(src
, &env
->fp_status
);
155 float64
helper_fxtod(CPUSPARCState
*env
, int64_t src
)
157 return int64_to_float64(src
, &env
->fp_status
);
160 void helper_fxtoq(CPUSPARCState
*env
, int64_t src
)
162 QT0
= int64_to_float128(src
, &env
->fp_status
);
167 /* floating point conversion */
168 float32
helper_fdtos(CPUSPARCState
*env
, float64 src
)
170 return float64_to_float32(src
, &env
->fp_status
);
173 float64
helper_fstod(CPUSPARCState
*env
, float32 src
)
175 return float32_to_float64(src
, &env
->fp_status
);
178 float32
helper_fqtos(CPUSPARCState
*env
)
180 return float128_to_float32(QT1
, &env
->fp_status
);
183 void helper_fstoq(CPUSPARCState
*env
, float32 src
)
185 QT0
= float32_to_float128(src
, &env
->fp_status
);
188 float64
helper_fqtod(CPUSPARCState
*env
)
190 return float128_to_float64(QT1
, &env
->fp_status
);
193 void helper_fdtoq(CPUSPARCState
*env
, float64 src
)
195 QT0
= float64_to_float128(src
, &env
->fp_status
);
198 /* Float to integer conversion. */
199 int32_t helper_fstoi(CPUSPARCState
*env
, float32 src
)
201 return float32_to_int32_round_to_zero(src
, &env
->fp_status
);
204 int32_t helper_fdtoi(CPUSPARCState
*env
, float64 src
)
206 return float64_to_int32_round_to_zero(src
, &env
->fp_status
);
209 int32_t helper_fqtoi(CPUSPARCState
*env
)
211 return float128_to_int32_round_to_zero(QT1
, &env
->fp_status
);
214 #ifdef TARGET_SPARC64
215 int64_t helper_fstox(CPUSPARCState
*env
, float32 src
)
217 return float32_to_int64_round_to_zero(src
, &env
->fp_status
);
220 int64_t helper_fdtox(CPUSPARCState
*env
, float64 src
)
222 return float64_to_int64_round_to_zero(src
, &env
->fp_status
);
225 int64_t helper_fqtox(CPUSPARCState
*env
)
227 return float128_to_int64_round_to_zero(QT1
, &env
->fp_status
);
231 float32
helper_fabss(float32 src
)
233 return float32_abs(src
);
236 #ifdef TARGET_SPARC64
237 float64
helper_fabsd(float64 src
)
239 return float64_abs(src
);
242 void helper_fabsq(CPUSPARCState
*env
)
244 QT0
= float128_abs(QT1
);
248 float32
helper_fsqrts(CPUSPARCState
*env
, float32 src
)
250 return float32_sqrt(src
, &env
->fp_status
);
253 float64
helper_fsqrtd(CPUSPARCState
*env
, float64 src
)
255 return float64_sqrt(src
, &env
->fp_status
);
258 void helper_fsqrtq(CPUSPARCState
*env
)
260 QT0
= float128_sqrt(QT1
, &env
->fp_status
);
263 #define GEN_FCMP(name, size, reg1, reg2, FS, E) \
264 target_ulong glue(helper_, name) (CPUSPARCState *env) \
269 ret = glue(size, _compare)(reg1, reg2, &env->fp_status); \
271 ret = glue(size, _compare_quiet)(reg1, reg2, \
274 fsr = do_check_ieee_exceptions(env, GETPC()); \
276 case float_relation_unordered: \
277 fsr |= (FSR_FCC1 | FSR_FCC0) << FS; \
280 case float_relation_less: \
281 fsr &= ~(FSR_FCC1) << FS; \
282 fsr |= FSR_FCC0 << FS; \
284 case float_relation_greater: \
285 fsr &= ~(FSR_FCC0) << FS; \
286 fsr |= FSR_FCC1 << FS; \
289 fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
294 #define GEN_FCMP_T(name, size, FS, E) \
295 target_ulong glue(helper_, name)(CPUSPARCState *env, size src1, size src2)\
300 ret = glue(size, _compare)(src1, src2, &env->fp_status); \
302 ret = glue(size, _compare_quiet)(src1, src2, \
305 fsr = do_check_ieee_exceptions(env, GETPC()); \
307 case float_relation_unordered: \
308 fsr |= (FSR_FCC1 | FSR_FCC0) << FS; \
310 case float_relation_less: \
311 fsr &= ~(FSR_FCC1 << FS); \
312 fsr |= FSR_FCC0 << FS; \
314 case float_relation_greater: \
315 fsr &= ~(FSR_FCC0 << FS); \
316 fsr |= FSR_FCC1 << FS; \
319 fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
325 GEN_FCMP_T(fcmps
, float32
, 0, 0);
326 GEN_FCMP_T(fcmpd
, float64
, 0, 0);
328 GEN_FCMP_T(fcmpes
, float32
, 0, 1);
329 GEN_FCMP_T(fcmped
, float64
, 0, 1);
331 GEN_FCMP(fcmpq
, float128
, QT0
, QT1
, 0, 0);
332 GEN_FCMP(fcmpeq
, float128
, QT0
, QT1
, 0, 1);
334 #ifdef TARGET_SPARC64
335 GEN_FCMP_T(fcmps_fcc1
, float32
, 22, 0);
336 GEN_FCMP_T(fcmpd_fcc1
, float64
, 22, 0);
337 GEN_FCMP(fcmpq_fcc1
, float128
, QT0
, QT1
, 22, 0);
339 GEN_FCMP_T(fcmps_fcc2
, float32
, 24, 0);
340 GEN_FCMP_T(fcmpd_fcc2
, float64
, 24, 0);
341 GEN_FCMP(fcmpq_fcc2
, float128
, QT0
, QT1
, 24, 0);
343 GEN_FCMP_T(fcmps_fcc3
, float32
, 26, 0);
344 GEN_FCMP_T(fcmpd_fcc3
, float64
, 26, 0);
345 GEN_FCMP(fcmpq_fcc3
, float128
, QT0
, QT1
, 26, 0);
347 GEN_FCMP_T(fcmpes_fcc1
, float32
, 22, 1);
348 GEN_FCMP_T(fcmped_fcc1
, float64
, 22, 1);
349 GEN_FCMP(fcmpeq_fcc1
, float128
, QT0
, QT1
, 22, 1);
351 GEN_FCMP_T(fcmpes_fcc2
, float32
, 24, 1);
352 GEN_FCMP_T(fcmped_fcc2
, float64
, 24, 1);
353 GEN_FCMP(fcmpeq_fcc2
, float128
, QT0
, QT1
, 24, 1);
355 GEN_FCMP_T(fcmpes_fcc3
, float32
, 26, 1);
356 GEN_FCMP_T(fcmped_fcc3
, float64
, 26, 1);
357 GEN_FCMP(fcmpeq_fcc3
, float128
, QT0
, QT1
, 26, 1);
362 static void set_fsr(CPUSPARCState
*env
, target_ulong fsr
)
366 switch (fsr
& FSR_RD_MASK
) {
368 rnd_mode
= float_round_nearest_even
;
372 rnd_mode
= float_round_to_zero
;
375 rnd_mode
= float_round_up
;
378 rnd_mode
= float_round_down
;
381 set_float_rounding_mode(rnd_mode
, &env
->fp_status
);
384 target_ulong
helper_ldfsr(CPUSPARCState
*env
, target_ulong old_fsr
,
387 old_fsr
= (new_fsr
& FSR_LDFSR_MASK
) | (old_fsr
& FSR_LDFSR_OLDMASK
);
388 set_fsr(env
, old_fsr
);
392 #ifdef TARGET_SPARC64
393 target_ulong
helper_ldxfsr(CPUSPARCState
*env
, target_ulong old_fsr
,
396 old_fsr
= (new_fsr
& FSR_LDXFSR_MASK
) | (old_fsr
& FSR_LDXFSR_OLDMASK
);
397 set_fsr(env
, old_fsr
);