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.1 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"
23 #include "fpu/softfloat.h"
25 #define QUIET_NAN 0x7fc00000
26 #define ADD_NAN 0x7fc00001
27 #define SQRT_NAN 0x7fc00004
28 #define DIV_NAN 0x7fc00008
29 #define MUL_NAN 0x7fc00002
30 #define FPU_FS PSW_USB_C
31 #define FPU_FI PSW_USB_V
32 #define FPU_FV PSW_USB_SV
33 #define FPU_FZ PSW_USB_AV
34 #define FPU_FU PSW_USB_SAV
36 #define float32_sqrt_nan make_float32(SQRT_NAN)
37 #define float32_quiet_nan make_float32(QUIET_NAN)
39 /* we don't care about input_denormal */
40 static inline uint8_t f_get_excp_flags(CPUTriCoreState
*env
)
42 return get_float_exception_flags(&env
->fp_status
)
45 | float_flag_underflow
46 | float_flag_output_denormal
47 | float_flag_divbyzero
48 | float_flag_inexact
);
51 static inline float32
f_maddsub_nan_result(float32 arg1
, float32 arg2
,
52 float32 arg3
, float32 result
,
53 uint32_t muladd_negate_c
)
55 uint32_t aSign
, bSign
, cSign
;
56 uint32_t aExp
, bExp
, cExp
;
58 if (float32_is_any_nan(arg1
) || float32_is_any_nan(arg2
) ||
59 float32_is_any_nan(arg3
)) {
61 } else if (float32_is_infinity(arg1
) && float32_is_zero(arg2
)) {
63 } else if (float32_is_zero(arg1
) && float32_is_infinity(arg2
)) {
70 aExp
= (arg1
>> 23) & 0xff;
71 bExp
= (arg2
>> 23) & 0xff;
72 cExp
= (arg3
>> 23) & 0xff;
74 if (muladd_negate_c
) {
77 if (((aExp
== 0xff) || (bExp
== 0xff)) && (cExp
== 0xff)) {
78 if (aSign
^ bSign
^ cSign
) {
87 static void f_update_psw_flags(CPUTriCoreState
*env
, uint8_t flags
)
89 uint8_t some_excp
= 0;
90 set_float_exception_flags(0, &env
->fp_status
);
92 if (flags
& float_flag_invalid
) {
93 env
->FPU_FI
= 1 << 31;
97 if (flags
& float_flag_overflow
) {
98 env
->FPU_FV
= 1 << 31;
102 if (flags
& float_flag_underflow
|| flags
& float_flag_output_denormal
) {
103 env
->FPU_FU
= 1 << 31;
107 if (flags
& float_flag_divbyzero
) {
108 env
->FPU_FZ
= 1 << 31;
112 if (flags
& float_flag_inexact
|| flags
& float_flag_output_denormal
) {
117 env
->FPU_FS
= some_excp
;
120 #define FADD_SUB(op) \
121 uint32_t helper_f##op(CPUTriCoreState *env, uint32_t r1, uint32_t r2) \
123 float32 arg1 = make_float32(r1); \
124 float32 arg2 = make_float32(r2); \
128 f_result = float32_##op(arg2, arg1, &env->fp_status); \
129 flags = f_get_excp_flags(env); \
131 /* If the output is a NaN, but the inputs aren't, \
132 we return a unique value. */ \
133 if ((flags & float_flag_invalid) \
134 && !float32_is_any_nan(arg1) \
135 && !float32_is_any_nan(arg2)) { \
136 f_result = ADD_NAN; \
138 f_update_psw_flags(env, flags); \
142 return (uint32_t)f_result; \
147 uint32_t helper_fmul(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
150 float32 arg1
= make_float32(r1
);
151 float32 arg2
= make_float32(r2
);
154 f_result
= float32_mul(arg1
, arg2
, &env
->fp_status
);
156 flags
= f_get_excp_flags(env
);
158 /* If the output is a NaN, but the inputs aren't,
159 we return a unique value. */
160 if ((flags
& float_flag_invalid
)
161 && !float32_is_any_nan(arg1
)
162 && !float32_is_any_nan(arg2
)) {
165 f_update_psw_flags(env
, flags
);
169 return (uint32_t)f_result
;
174 * Target TriCore QSEED.F significand Lookup Table
176 * The QSEED.F output significand depends on the least-significant
177 * exponent bit and the 6 most-significant significand bits.
179 * IEEE 754 float datatype
180 * partitioned into Sign (S), Exponent (E) and Significand (M):
182 * S E E E E E E E E M M M M M M ...
184 * +------+------+-------+-------+
187 * calculating index for
190 * This lookup table was extracted by analyzing QSEED output
191 * from the real hardware
193 static const uint8_t target_qseed_significand_table
[128] = {
194 253, 252, 245, 244, 239, 238, 231, 230, 225, 224, 217, 216,
195 211, 210, 205, 204, 201, 200, 195, 194, 189, 188, 185, 184,
196 179, 178, 175, 174, 169, 168, 165, 164, 161, 160, 157, 156,
197 153, 152, 149, 148, 145, 144, 141, 140, 137, 136, 133, 132,
198 131, 130, 127, 126, 123, 122, 121, 120, 117, 116, 115, 114,
199 111, 110, 109, 108, 103, 102, 99, 98, 93, 92, 89, 88, 83,
200 82, 79, 78, 75, 74, 71, 70, 67, 66, 63, 62, 59, 58, 55,
201 54, 53, 52, 49, 48, 45, 44, 43, 42, 39, 38, 37, 36, 33,
202 32, 31, 30, 27, 26, 25, 24, 23, 22, 19, 18, 17, 16, 15,
203 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2
206 uint32_t helper_qseed(CPUTriCoreState
*env
, uint32_t r1
)
208 uint32_t arg1
, S
, E
, M
, E_minus_one
, m_idx
;
209 uint32_t new_E
, new_M
, new_S
, result
;
211 arg1
= make_float32(r1
);
213 /* fetch IEEE-754 fields S, E and the uppermost 6-bit of M */
214 S
= extract32(arg1
, 31, 1);
215 E
= extract32(arg1
, 23, 8);
216 M
= extract32(arg1
, 17, 6);
218 if (float32_is_any_nan(arg1
)) {
219 result
= float32_quiet_nan
;
220 } else if (float32_is_zero_or_denormal(arg1
)) {
221 if (float32_is_neg(arg1
)) {
222 result
= float32_infinity
| (1 << 31);
224 result
= float32_infinity
;
226 } else if (float32_is_neg(arg1
)) {
227 result
= float32_sqrt_nan
;
228 } else if (float32_is_infinity(arg1
)) {
229 result
= float32_zero
;
232 m_idx
= ((E_minus_one
& 1) << 6) | M
;
234 new_E
= 0xBD - E_minus_one
/ 2;
235 new_M
= target_qseed_significand_table
[m_idx
];
238 result
= deposit32(result
, 31, 1, new_S
);
239 result
= deposit32(result
, 23, 8, new_E
);
240 result
= deposit32(result
, 15, 8, new_M
);
243 if (float32_is_signaling_nan(arg1
, &env
->fp_status
)
244 || result
== float32_sqrt_nan
) {
245 env
->FPU_FI
= 1 << 31;
251 return (uint32_t) result
;
254 uint32_t helper_fdiv(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
257 float32 arg1
= make_float32(r1
);
258 float32 arg2
= make_float32(r2
);
261 f_result
= float32_div(arg1
, arg2
, &env
->fp_status
);
263 flags
= f_get_excp_flags(env
);
265 /* If the output is a NaN, but the inputs aren't,
266 we return a unique value. */
267 if ((flags
& float_flag_invalid
)
268 && !float32_is_any_nan(arg1
)
269 && !float32_is_any_nan(arg2
)) {
272 f_update_psw_flags(env
, flags
);
277 return (uint32_t)f_result
;
280 uint32_t helper_fmadd(CPUTriCoreState
*env
, uint32_t r1
,
281 uint32_t r2
, uint32_t r3
)
284 float32 arg1
= make_float32(r1
);
285 float32 arg2
= make_float32(r2
);
286 float32 arg3
= make_float32(r3
);
289 f_result
= float32_muladd(arg1
, arg2
, arg3
, 0, &env
->fp_status
);
291 flags
= f_get_excp_flags(env
);
293 if (flags
& float_flag_invalid
) {
294 arg1
= float32_squash_input_denormal(arg1
, &env
->fp_status
);
295 arg2
= float32_squash_input_denormal(arg2
, &env
->fp_status
);
296 arg3
= float32_squash_input_denormal(arg3
, &env
->fp_status
);
297 f_result
= f_maddsub_nan_result(arg1
, arg2
, arg3
, f_result
, 0);
299 f_update_psw_flags(env
, flags
);
303 return (uint32_t)f_result
;
306 uint32_t helper_fmsub(CPUTriCoreState
*env
, uint32_t r1
,
307 uint32_t r2
, uint32_t r3
)
310 float32 arg1
= make_float32(r1
);
311 float32 arg2
= make_float32(r2
);
312 float32 arg3
= make_float32(r3
);
315 f_result
= float32_muladd(arg1
, arg2
, arg3
, float_muladd_negate_product
,
318 flags
= f_get_excp_flags(env
);
320 if (flags
& float_flag_invalid
) {
321 arg1
= float32_squash_input_denormal(arg1
, &env
->fp_status
);
322 arg2
= float32_squash_input_denormal(arg2
, &env
->fp_status
);
323 arg3
= float32_squash_input_denormal(arg3
, &env
->fp_status
);
325 f_result
= f_maddsub_nan_result(arg1
, arg2
, arg3
, f_result
, 1);
327 f_update_psw_flags(env
, flags
);
331 return (uint32_t)f_result
;
334 uint32_t helper_fcmp(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
336 uint32_t result
, flags
;
337 float32 arg1
= make_float32(r1
);
338 float32 arg2
= make_float32(r2
);
340 set_flush_inputs_to_zero(0, &env
->fp_status
);
342 result
= 1 << (float32_compare_quiet(arg1
, arg2
, &env
->fp_status
) + 1);
343 result
|= float32_is_denormal(arg1
) << 4;
344 result
|= float32_is_denormal(arg2
) << 5;
346 flags
= f_get_excp_flags(env
);
348 f_update_psw_flags(env
, flags
);
353 set_flush_inputs_to_zero(1, &env
->fp_status
);
357 uint32_t helper_ftoi(CPUTriCoreState
*env
, uint32_t arg
)
359 float32 f_arg
= make_float32(arg
);
360 int32_t result
, flags
;
362 result
= float32_to_int32(f_arg
, &env
->fp_status
);
364 flags
= f_get_excp_flags(env
);
366 if (float32_is_any_nan(f_arg
)) {
369 f_update_psw_flags(env
, flags
);
373 return (uint32_t)result
;
376 uint32_t helper_itof(CPUTriCoreState
*env
, uint32_t arg
)
380 f_result
= int32_to_float32(arg
, &env
->fp_status
);
382 flags
= f_get_excp_flags(env
);
384 f_update_psw_flags(env
, flags
);
388 return (uint32_t)f_result
;
391 uint32_t helper_utof(CPUTriCoreState
*env
, uint32_t arg
)
396 f_result
= uint32_to_float32(arg
, &env
->fp_status
);
398 flags
= f_get_excp_flags(env
);
400 f_update_psw_flags(env
, flags
);
404 return (uint32_t)f_result
;
407 uint32_t helper_ftoiz(CPUTriCoreState
*env
, uint32_t arg
)
409 float32 f_arg
= make_float32(arg
);
413 result
= float32_to_int32_round_to_zero(f_arg
, &env
->fp_status
);
415 flags
= f_get_excp_flags(env
);
416 if (flags
& float_flag_invalid
) {
417 flags
&= ~float_flag_inexact
;
418 if (float32_is_any_nan(f_arg
)) {
424 f_update_psw_flags(env
, flags
);
432 uint32_t helper_ftouz(CPUTriCoreState
*env
, uint32_t arg
)
434 float32 f_arg
= make_float32(arg
);
438 result
= float32_to_uint32_round_to_zero(f_arg
, &env
->fp_status
);
440 flags
= f_get_excp_flags(env
);
441 if (flags
& float_flag_invalid
) {
442 flags
&= ~float_flag_inexact
;
443 if (float32_is_any_nan(f_arg
)) {
446 } else if (float32_lt_quiet(f_arg
, 0, &env
->fp_status
)) {
447 flags
= float_flag_invalid
;
452 f_update_psw_flags(env
, flags
);
459 void helper_updfl(CPUTriCoreState
*env
, uint32_t arg
)
461 env
->FPU_FS
= extract32(arg
, 7, 1) & extract32(arg
, 15, 1);
462 env
->FPU_FI
= (extract32(arg
, 6, 1) & extract32(arg
, 14, 1)) << 31;
463 env
->FPU_FV
= (extract32(arg
, 5, 1) & extract32(arg
, 13, 1)) << 31;
464 env
->FPU_FZ
= (extract32(arg
, 4, 1) & extract32(arg
, 12, 1)) << 31;
465 env
->FPU_FU
= (extract32(arg
, 3, 1) & extract32(arg
, 11, 1)) << 31;
466 /* clear FX and RM */
467 env
->PSW
&= ~(extract32(arg
, 10, 1) << 26);
468 env
->PSW
|= (extract32(arg
, 2, 1) & extract32(arg
, 10, 1)) << 26;