2 * TriCore emulation for qemu: fpu helper.
4 * Copyright (c) 2016 Bastian Koppelmann University of Paderborn
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/helper-proto.h"
24 #define ADD_NAN 0x7cf00001
25 #define DIV_NAN 0x7fc00008
26 #define MUL_NAN 0x7fc00002
27 #define FPU_FS PSW_USB_C
28 #define FPU_FI PSW_USB_V
29 #define FPU_FV PSW_USB_SV
30 #define FPU_FZ PSW_USB_AV
31 #define FPU_FU PSW_USB_SAV
33 /* we don't care about input_denormal */
34 static inline uint8_t f_get_excp_flags(CPUTriCoreState
*env
)
36 return get_float_exception_flags(&env
->fp_status
)
39 | float_flag_underflow
40 | float_flag_output_denormal
41 | float_flag_divbyzero
42 | float_flag_inexact
);
45 static inline bool f_is_denormal(float32 arg
)
47 return float32_is_zero_or_denormal(arg
) && !float32_is_zero(arg
);
50 static void f_update_psw_flags(CPUTriCoreState
*env
, uint8_t flags
)
52 uint8_t some_excp
= 0;
53 set_float_exception_flags(0, &env
->fp_status
);
55 if (flags
& float_flag_invalid
) {
56 env
->FPU_FI
= 1 << 31;
60 if (flags
& float_flag_overflow
) {
61 env
->FPU_FV
= 1 << 31;
65 if (flags
& float_flag_underflow
|| flags
& float_flag_output_denormal
) {
66 env
->FPU_FU
= 1 << 31;
70 if (flags
& float_flag_divbyzero
) {
71 env
->FPU_FZ
= 1 << 31;
75 if (flags
& float_flag_inexact
|| flags
& float_flag_output_denormal
) {
80 env
->FPU_FS
= some_excp
;
83 #define FADD_SUB(op) \
84 uint32_t helper_f##op(CPUTriCoreState *env, uint32_t r1, uint32_t r2) \
86 float32 arg1 = make_float32(r1); \
87 float32 arg2 = make_float32(r2); \
91 f_result = float32_##op(arg2, arg1, &env->fp_status); \
92 flags = f_get_excp_flags(env); \
94 /* If the output is a NaN, but the inputs aren't, \
95 we return a unique value. */ \
96 if ((flags & float_flag_invalid) \
97 && !float32_is_any_nan(arg1) \
98 && !float32_is_any_nan(arg2)) { \
101 f_update_psw_flags(env, flags); \
105 return (uint32_t)f_result; \
110 uint32_t helper_fmul(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
113 float32 arg1
= make_float32(r1
);
114 float32 arg2
= make_float32(r2
);
117 f_result
= float32_mul(arg1
, arg2
, &env
->fp_status
);
119 flags
= f_get_excp_flags(env
);
121 /* If the output is a NaN, but the inputs aren't,
122 we return a unique value. */
123 if ((flags
& float_flag_invalid
)
124 && !float32_is_any_nan(arg1
)
125 && !float32_is_any_nan(arg2
)) {
128 f_update_psw_flags(env
, flags
);
132 return (uint32_t)f_result
;
136 uint32_t helper_fdiv(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
139 float32 arg1
= make_float32(r1
);
140 float32 arg2
= make_float32(r2
);
143 f_result
= float32_div(arg1
, arg2
, &env
->fp_status
);
145 flags
= f_get_excp_flags(env
);
147 /* If the output is a NaN, but the inputs aren't,
148 we return a unique value. */
149 if ((flags
& float_flag_invalid
)
150 && !float32_is_any_nan(arg1
)
151 && !float32_is_any_nan(arg2
)) {
154 f_update_psw_flags(env
, flags
);
159 return (uint32_t)f_result
;
162 uint32_t helper_fcmp(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
164 uint32_t result
, flags
;
165 float32 arg1
= make_float32(r1
);
166 float32 arg2
= make_float32(r2
);
168 set_flush_inputs_to_zero(0, &env
->fp_status
);
170 result
= 1 << (float32_compare_quiet(arg1
, arg2
, &env
->fp_status
) + 1);
171 result
|= f_is_denormal(arg1
) << 4;
172 result
|= f_is_denormal(arg2
) << 5;
174 flags
= f_get_excp_flags(env
);
176 f_update_psw_flags(env
, flags
);
181 set_flush_inputs_to_zero(1, &env
->fp_status
);
185 uint32_t helper_ftoi(CPUTriCoreState
*env
, uint32_t arg
)
187 float32 f_arg
= make_float32(arg
);
188 int32_t result
, flags
;
190 result
= float32_to_int32(f_arg
, &env
->fp_status
);
192 flags
= f_get_excp_flags(env
);
194 if (float32_is_any_nan(f_arg
)) {
197 f_update_psw_flags(env
, flags
);
201 return (uint32_t)result
;
204 uint32_t helper_itof(CPUTriCoreState
*env
, uint32_t arg
)
208 f_result
= int32_to_float32(arg
, &env
->fp_status
);
210 flags
= f_get_excp_flags(env
);
212 f_update_psw_flags(env
, flags
);
216 return (uint32_t)f_result
;